// 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; /// ISO 8601 time with timezone. #[derive(Clone)] pub struct Time { time: NaiveTime, offset: Off, } impl Time { /// 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 { 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(&self, offset: Off2) -> Time { Time::from_utc(self.time, offset) } /// Formats the time in the specified format string. /// See the `format` module on the supported escape sequences. #[inline] pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a> { DelayedFormat::new_with_offset(None, Some(self.local()), &self.offset, fmt) } /// Returns a view to the local time. fn local(&self) -> NaiveTime { self.offset.to_local_time(&self.time) } } impl Timelike for Time { #[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> { self.local().with_hour(hour) .and_then(|time| self.offset.from_local_time(&time).single()) } #[inline] fn with_minute(&self, min: u32) -> Option> { self.local().with_minute(min) .and_then(|time| self.offset.from_local_time(&time).single()) } #[inline] fn with_second(&self, sec: u32) -> Option> { self.local().with_second(sec) .and_then(|time| self.offset.from_local_time(&time).single()) } #[inline] fn with_nanosecond(&self, nano: u32) -> Option> { 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 PartialEq> for Time { fn eq(&self, other: &Time) -> bool { self.time == other.time } } impl Eq for Time { } impl PartialOrd for Time { fn partial_cmp(&self, other: &Time) -> Option { self.time.partial_cmp(&other.time) } } impl Ord for Time { fn cmp(&self, other: &Time) -> Ordering { self.time.cmp(&other.time) } } impl hash::Hash for Time { fn hash(&self, state: &mut hash::sip::SipState) { self.time.hash(state) } } impl Add for Time { type Output = Time; fn add(self, rhs: Duration) -> Time { Time { time: self.time + rhs, offset: self.offset } } } impl Sub> for Time { type Output = Duration; fn sub(self, rhs: Time) -> Duration { self.time - rhs.time } } impl Sub for Time { type Output = Time; #[inline] fn sub(self, rhs: Duration) -> Time { self.add(-rhs) } } impl fmt::Show for Time { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}{}", self.local(), self.offset) } }