From 80ed400689e9dc3cd9c11d7fcd373fc702c26592 Mon Sep 17 00:00:00 2001 From: Kang Seonghoon Date: Fri, 29 Aug 2014 18:14:08 +0900 Subject: [PATCH] added missing `.offset()` methods; UTC/FixedOffset now implement Eq. --- src/date.rs | 6 ++++++ src/datetime.rs | 12 ++++++++++++ src/offset.rs | 4 ++-- src/time.rs | 6 ++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/date.rs b/src/date.rs index 3b927d6..a9554c7 100644 --- a/src/date.rs +++ b/src/date.rs @@ -159,6 +159,12 @@ impl Date { self.date.pred_opt().map(|date| Date::from_utc(date, self.offset.clone())) } + /// Retrieves an associated offset. + #[inline] + pub fn offset<'a>(&'a self) -> &'a Off { + &self.offset + } + /// Formats the date in the specified format string. /// See the `format` module on the supported escape sequences. #[inline] diff --git a/src/datetime.rs b/src/datetime.rs index 8a9068d..9d32b90 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -49,6 +49,12 @@ impl DateTime { self.datetime.num_seconds_from_unix_epoch() } + /// Retrieves an associated offset. + #[inline] + pub fn offset<'a>(&'a self) -> &'a Off { + &self.offset + } + /// Formats the combined date and time in the specified format string. /// See the `format` module on the supported escape sequences. #[inline] @@ -207,7 +213,9 @@ mod tests { #[test] #[allow(uppercase_variables)] fn test_datetime_offset() { + let EST = FixedOffset::east(5*60*60); let EDT = FixedOffset::east(4*60*60); + assert_eq!(UTC.ymd(2014, 5, 6).and_hms(7, 8, 9).to_string(), "2014-05-06T07:08:09Z".to_string()); assert_eq!(EDT.ymd(2014, 5, 6).and_hms(7, 8, 9).to_string(), @@ -217,6 +225,10 @@ mod tests { UTC.ymd(2014, 5, 6).and_hms(8, 9, 10)); assert_eq!(UTC.ymd(2014, 5, 6).and_hms(7, 8, 9) - EDT.ymd(2014, 5, 6).and_hms(10, 11, 12), Duration::seconds(3600 - 3*60 - 3)); + + assert_eq!(*UTC.ymd(2014, 5, 6).and_hms(7, 8, 9).offset(), UTC); + assert_eq!(*EDT.ymd(2014, 5, 6).and_hms(7, 8, 9).offset(), EDT); + assert!(*EDT.ymd(2014, 5, 6).and_hms(7, 8, 9).offset() != EST); } } diff --git a/src/offset.rs b/src/offset.rs index 931f291..840e8d5 100644 --- a/src/offset.rs +++ b/src/offset.rs @@ -244,7 +244,7 @@ pub trait Offset: Clone + fmt::Show { } /// The UTC timescale. This is the most efficient offset when you don't need the local time. -#[deriving(Clone)] +#[deriving(Clone, PartialEq, Eq)] pub struct UTC; impl UTC { @@ -283,7 +283,7 @@ impl fmt::Show for UTC { } /// The fixed offset, from UTC-23:59:59 to UTC+23:59:59. -#[deriving(Clone)] +#[deriving(Clone, PartialEq, Eq)] pub struct FixedOffset { local_minus_utc: i32, } diff --git a/src/time.rs b/src/time.rs index 68266f2..8f5796b 100644 --- a/src/time.rs +++ b/src/time.rs @@ -29,6 +29,12 @@ impl Time { Time { time: time, offset: offset } } + /// Retrieves an associated offset. + #[inline] + pub fn offset<'a>(&'a self) -> &'a Off { + &self.offset + } + /// Formats the time in the specified format string. /// See the `format` module on the supported escape sequences. #[inline]