added missing `.offset()` methods; UTC/FixedOffset now implement Eq.

This commit is contained in:
Kang Seonghoon 2014-08-29 18:14:08 +09:00
parent 31696204e7
commit 80ed400689
4 changed files with 26 additions and 2 deletions

View File

@ -159,6 +159,12 @@ impl<Off:Offset> Date<Off> {
self.date.pred_opt().map(|date| Date::from_utc(date, self.offset.clone())) 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. /// Formats the date in the specified format string.
/// See the `format` module on the supported escape sequences. /// See the `format` module on the supported escape sequences.
#[inline] #[inline]

View File

@ -49,6 +49,12 @@ impl<Off:Offset> DateTime<Off> {
self.datetime.num_seconds_from_unix_epoch() 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. /// Formats the combined date and time in the specified format string.
/// See the `format` module on the supported escape sequences. /// See the `format` module on the supported escape sequences.
#[inline] #[inline]
@ -207,7 +213,9 @@ mod tests {
#[test] #[test]
#[allow(uppercase_variables)] #[allow(uppercase_variables)]
fn test_datetime_offset() { fn test_datetime_offset() {
let EST = FixedOffset::east(5*60*60);
let EDT = FixedOffset::east(4*60*60); let EDT = FixedOffset::east(4*60*60);
assert_eq!(UTC.ymd(2014, 5, 6).and_hms(7, 8, 9).to_string(), assert_eq!(UTC.ymd(2014, 5, 6).and_hms(7, 8, 9).to_string(),
"2014-05-06T07:08:09Z".to_string()); "2014-05-06T07:08:09Z".to_string());
assert_eq!(EDT.ymd(2014, 5, 6).and_hms(7, 8, 9).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)); 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), 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)); 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);
} }
} }

View File

@ -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. /// 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; pub struct UTC;
impl 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. /// The fixed offset, from UTC-23:59:59 to UTC+23:59:59.
#[deriving(Clone)] #[deriving(Clone, PartialEq, Eq)]
pub struct FixedOffset { pub struct FixedOffset {
local_minus_utc: i32, local_minus_utc: i32,
} }

View File

@ -29,6 +29,12 @@ impl<Off:Offset> Time<Off> {
Time { time: time, offset: offset } 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. /// Formats the time in the specified format string.
/// See the `format` module on the supported escape sequences. /// See the `format` module on the supported escape sequences.
#[inline] #[inline]