chrono/src/time.rs

151 lines
4.3 KiB
Rust

// This is a part of rust-chrono.
// Copyright (c) 2014-2015, Kang Seonghoon.
// See README.md and LICENSE.txt for details.
/*!
* ISO 8601 time with timezone.
*/
use std::{fmt, hash};
use std::cmp::Ordering;
use std::ops::{Add, Sub};
use Timelike;
use offset::Offset;
use duration::Duration;
use naive::time::NaiveTime;
use format::{DelayedFormat, StrftimeItems};
/// ISO 8601 time with timezone.
#[derive(Clone)]
pub struct Time<Off> {
time: NaiveTime,
offset: Off,
}
impl<Off:Offset> Time<Off> {
/// Makes a new `Time` with given *UTC* time and offset.
/// The local time should be constructed via the `Offset` trait.
#[inline]
pub fn from_utc(time: NaiveTime, offset: Off) -> Time<Off> {
Time { time: time, offset: offset }
}
/// Retrieves an associated offset.
#[inline]
pub fn offset<'a>(&'a self) -> &'a Off {
&self.offset
}
/// Changes the associated offset.
/// This does not change the actual `Time` (but will change the string representation).
#[inline]
pub fn with_offset<Off2:Offset>(&self, offset: Off2) -> Time<Off2> {
Time::from_utc(self.time, offset)
}
/// Returns a view to the local time.
fn local(&self) -> NaiveTime {
self.offset.to_local_time(&self.time)
}
}
impl<Off: Offset + fmt::Display> Time<Off> {
/// Formats the time in the specified format string.
/// See the `format::strftime` module on the supported escape sequences.
#[inline]
pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> {
DelayedFormat::new_with_offset(None, Some(self.local()), &self.offset,
StrftimeItems::new(fmt))
}
}
impl<Off:Offset> Timelike for Time<Off> {
#[inline] fn hour(&self) -> u32 { self.local().hour() }
#[inline] fn minute(&self) -> u32 { self.local().minute() }
#[inline] fn second(&self) -> u32 { self.local().second() }
#[inline] fn nanosecond(&self) -> u32 { self.local().nanosecond() }
#[inline]
fn with_hour(&self, hour: u32) -> Option<Time<Off>> {
self.local().with_hour(hour)
.and_then(|time| self.offset.from_local_time(&time).single())
}
#[inline]
fn with_minute(&self, min: u32) -> Option<Time<Off>> {
self.local().with_minute(min)
.and_then(|time| self.offset.from_local_time(&time).single())
}
#[inline]
fn with_second(&self, sec: u32) -> Option<Time<Off>> {
self.local().with_second(sec)
.and_then(|time| self.offset.from_local_time(&time).single())
}
#[inline]
fn with_nanosecond(&self, nano: u32) -> Option<Time<Off>> {
self.local().with_nanosecond(nano)
.and_then(|time| self.offset.from_local_time(&time).single())
}
#[inline]
fn num_seconds_from_midnight(&self) -> u32 { self.local().num_seconds_from_midnight() }
}
impl<Off:Offset, Off2:Offset> PartialEq<Time<Off2>> for Time<Off> {
fn eq(&self, other: &Time<Off2>) -> bool { self.time == other.time }
}
impl<Off:Offset> Eq for Time<Off> {
}
impl<Off:Offset> PartialOrd for Time<Off> {
fn partial_cmp(&self, other: &Time<Off>) -> Option<Ordering> {
self.time.partial_cmp(&other.time)
}
}
impl<Off:Offset> Ord for Time<Off> {
fn cmp(&self, other: &Time<Off>) -> Ordering { self.time.cmp(&other.time) }
}
impl<Off: Offset, H: hash::Hasher + hash::Writer> hash::Hash<H> for Time<Off> {
fn hash(&self, state: &mut H) { self.time.hash(state) }
}
impl<Off:Offset> Add<Duration> for Time<Off> {
type Output = Time<Off>;
fn add(self, rhs: Duration) -> Time<Off> {
Time { time: self.time + rhs, offset: self.offset }
}
}
impl<Off:Offset, Off2:Offset> Sub<Time<Off2>> for Time<Off> {
type Output = Duration;
fn sub(self, rhs: Time<Off2>) -> Duration { self.time - rhs.time }
}
impl<Off:Offset> Sub<Duration> for Time<Off> {
type Output = Time<Off>;
#[inline]
fn sub(self, rhs: Duration) -> Time<Off> { self.add(-rhs) }
}
impl<Off: Offset> fmt::Debug for Time<Off> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}{:?}", self.local(), self.offset)
}
}
impl<Off: Offset + fmt::Display> fmt::Display for Time<Off> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.local(), self.offset)
}
}