// This is a part of rust-chrono. // Copyright (c) 2014-2015, Kang Seonghoon. // See README.md and LICENSE.txt for details. /*! * ISO 8601 date and time with time zone. */ use std::{str, fmt, hash}; use std::cmp::Ordering; use std::ops::{Add, Sub}; use {Weekday, Timelike, Datelike}; use offset::{TimeZone, Offset}; use offset::utc::UTC; use offset::local::Local; use offset::fixed::FixedOffset; use duration::Duration; use naive::time::NaiveTime; use naive::datetime::NaiveDateTime; use date::Date; use format::{Item, Numeric, Pad, Fixed}; use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems}; /// ISO 8601 combined date and time with time zone. #[derive(Clone)] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] pub struct DateTime { datetime: NaiveDateTime, offset: Tz::Offset, } impl DateTime { /// Makes a new `DateTime` with given *UTC* datetime and offset. /// The local datetime should be constructed via the `TimeZone` trait. // // note: this constructor is purposedly not named to `new` to discourage the direct usage. #[inline] pub fn from_utc(datetime: NaiveDateTime, offset: Tz::Offset) -> DateTime { DateTime { datetime: datetime, offset: offset } } /// Retrieves a date component. #[inline] pub fn date(&self) -> Date { Date::from_utc(self.naive_local().date(), self.offset.clone()) } /// Retrieves a time component. /// Unlike `date`, this is not associated to the time zone. #[inline] pub fn time(&self) -> NaiveTime { self.datetime.time() + self.offset.local_minus_utc() } /// Returns the number of non-leap seconds since January 1, 1970 0:00:00 UTC /// (aka "UNIX timestamp"). #[inline] pub fn timestamp(&self) -> i64 { self.datetime.timestamp() } /// *Deprecated*: Same to `DateTime::timestamp`. #[inline] pub fn num_seconds_from_unix_epoch(&self) -> i64 { self.timestamp() } /// Retrieves an associated offset from UTC. #[inline] pub fn offset<'a>(&'a self) -> &'a Tz::Offset { &self.offset } /// Retrieves an associated time zone. #[inline] pub fn timezone(&self) -> Tz { TimeZone::from_offset(&self.offset) } /// Changes the associated time zone. /// This does not change the actual `DateTime` (but will change the string representation). #[inline] pub fn with_timezone(&self, tz: &Tz2) -> DateTime { tz.from_utc_datetime(&self.datetime) } /// Adds given `Duration` to the current date and time. /// /// Returns `None` when it will result in overflow. #[inline] pub fn checked_add(self, rhs: Duration) -> Option> { let datetime = try_opt!(self.datetime.checked_add(rhs)); Some(DateTime { datetime: datetime, offset: self.offset }) } /// Subtracts given `Duration` from the current date and time. /// /// Returns `None` when it will result in overflow. #[inline] pub fn checked_sub(self, rhs: Duration) -> Option> { let datetime = try_opt!(self.datetime.checked_sub(rhs)); Some(DateTime { datetime: datetime, offset: self.offset }) } /// Returns a view to the naive UTC datetime. #[inline] pub fn naive_utc(&self) -> NaiveDateTime { self.datetime } /// Returns a view to the naive local datetime. #[inline] pub fn naive_local(&self) -> NaiveDateTime { self.datetime + self.offset.local_minus_utc() } } /// Maps the local datetime to other datetime with given conversion function. fn map_local(dt: &DateTime, mut f: F) -> Option> where F: FnMut(NaiveDateTime) -> Option { f(dt.naive_local()).and_then(|datetime| dt.timezone().from_local_datetime(&datetime).single()) } impl DateTime { /// Parses an RFC 2822 date and time string such as `Tue, 1 Jul 2003 10:52:37 +0200`, /// then returns a new `DateTime` with a parsed `FixedOffset`. pub fn parse_from_rfc2822(s: &str) -> ParseResult> { const ITEMS: &'static [Item<'static>] = &[Item::Fixed(Fixed::RFC2822)]; let mut parsed = Parsed::new(); try!(parse(&mut parsed, s, ITEMS.iter().cloned())); parsed.to_datetime() } /// Parses an RFC 3339 and ISO 8601 date and time string such as `1996-12-19T16:39:57-08:00`, /// then returns a new `DateTime` with a parsed `FixedOffset`. /// /// Why isn't this named `parse_from_iso8601`? That's because ISO 8601 allows some freedom /// over the syntax and RFC 3339 exercises that freedom to rigidly define a fixed format. pub fn parse_from_rfc3339(s: &str) -> ParseResult> { const ITEMS: &'static [Item<'static>] = &[Item::Fixed(Fixed::RFC3339)]; let mut parsed = Parsed::new(); try!(parse(&mut parsed, s, ITEMS.iter().cloned())); parsed.to_datetime() } /// Parses a string with the specified format string and /// returns a new `DateTime` with a parsed `FixedOffset`. /// See the [`format::strftime` module](../format/strftime/index.html) /// on the supported escape sequences. /// /// See also `Offset::datetime_from_str` which gives a local `DateTime` on specific time zone. pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult> { let mut parsed = Parsed::new(); try!(parse(&mut parsed, s, StrftimeItems::new(fmt))); parsed.to_datetime() } } impl DateTime where Tz::Offset: fmt::Display { /// Returns an RFC 2822 date and time string such as `Tue, 1 Jul 2003 10:52:37 +0200`. pub fn to_rfc2822(&self) -> String { const ITEMS: &'static [Item<'static>] = &[Item::Fixed(Fixed::RFC2822)]; self.format_with_items(ITEMS.iter().cloned()).to_string() } /// Returns an RFC 3339 and ISO 8601 date and time string such as `1996-12-19T16:39:57-08:00`. pub fn to_rfc3339(&self) -> String { const ITEMS: &'static [Item<'static>] = &[Item::Fixed(Fixed::RFC3339)]; self.format_with_items(ITEMS.iter().cloned()).to_string() } /// Formats the combined date and time with the specified formatting items. #[inline] pub fn format_with_items<'a, I>(&self, items: I) -> DelayedFormat where I: Iterator> + Clone { let local = self.naive_local(); DelayedFormat::new_with_offset(Some(local.date()), Some(local.time()), &self.offset, items) } /// Formats the combined date and time with the specified format string. /// See the [`format::strftime` module](../format/strftime/index.html) /// on the supported escape sequences. #[inline] pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat> { self.format_with_items(StrftimeItems::new(fmt)) } } impl Datelike for DateTime { #[inline] fn year(&self) -> i32 { self.naive_local().year() } #[inline] fn month(&self) -> u32 { self.naive_local().month() } #[inline] fn month0(&self) -> u32 { self.naive_local().month0() } #[inline] fn day(&self) -> u32 { self.naive_local().day() } #[inline] fn day0(&self) -> u32 { self.naive_local().day0() } #[inline] fn ordinal(&self) -> u32 { self.naive_local().ordinal() } #[inline] fn ordinal0(&self) -> u32 { self.naive_local().ordinal0() } #[inline] fn weekday(&self) -> Weekday { self.naive_local().weekday() } #[inline] fn isoweekdate(&self) -> (i32, u32, Weekday) { self.naive_local().isoweekdate() } #[inline] fn with_year(&self, year: i32) -> Option> { map_local(self, |datetime| datetime.with_year(year)) } #[inline] fn with_month(&self, month: u32) -> Option> { map_local(self, |datetime| datetime.with_month(month)) } #[inline] fn with_month0(&self, month0: u32) -> Option> { map_local(self, |datetime| datetime.with_month0(month0)) } #[inline] fn with_day(&self, day: u32) -> Option> { map_local(self, |datetime| datetime.with_day(day)) } #[inline] fn with_day0(&self, day0: u32) -> Option> { map_local(self, |datetime| datetime.with_day0(day0)) } #[inline] fn with_ordinal(&self, ordinal: u32) -> Option> { map_local(self, |datetime| datetime.with_ordinal(ordinal)) } #[inline] fn with_ordinal0(&self, ordinal0: u32) -> Option> { map_local(self, |datetime| datetime.with_ordinal0(ordinal0)) } } impl Timelike for DateTime { #[inline] fn hour(&self) -> u32 { self.naive_local().hour() } #[inline] fn minute(&self) -> u32 { self.naive_local().minute() } #[inline] fn second(&self) -> u32 { self.naive_local().second() } #[inline] fn nanosecond(&self) -> u32 { self.naive_local().nanosecond() } #[inline] fn with_hour(&self, hour: u32) -> Option> { map_local(self, |datetime| datetime.with_hour(hour)) } #[inline] fn with_minute(&self, min: u32) -> Option> { map_local(self, |datetime| datetime.with_minute(min)) } #[inline] fn with_second(&self, sec: u32) -> Option> { map_local(self, |datetime| datetime.with_second(sec)) } #[inline] fn with_nanosecond(&self, nano: u32) -> Option> { map_local(self, |datetime| datetime.with_nanosecond(nano)) } } // we need them as automatic impls cannot handle associated types impl Copy for DateTime where ::Offset: Copy {} unsafe impl Send for DateTime where ::Offset: Send {} impl PartialEq> for DateTime { fn eq(&self, other: &DateTime) -> bool { self.datetime == other.datetime } } impl Eq for DateTime { } impl PartialOrd for DateTime { fn partial_cmp(&self, other: &DateTime) -> Option { self.datetime.partial_cmp(&other.datetime) } } impl Ord for DateTime { fn cmp(&self, other: &DateTime) -> Ordering { self.datetime.cmp(&other.datetime) } } impl hash::Hash for DateTime { fn hash(&self, state: &mut H) { self.datetime.hash(state) } } impl Add for DateTime { type Output = DateTime; #[inline] fn add(self, rhs: Duration) -> DateTime { self.checked_add(rhs).expect("`DateTime + Duration` overflowed") } } impl Sub> for DateTime { type Output = Duration; #[inline] fn sub(self, rhs: DateTime) -> Duration { self.datetime - rhs.datetime } } impl Sub for DateTime { type Output = DateTime; #[inline] fn sub(self, rhs: Duration) -> DateTime { self.checked_sub(rhs).expect("`DateTime - Duration` overflowed") } } impl fmt::Debug for DateTime { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}{:?}", self.naive_local(), self.offset) } } impl fmt::Display for DateTime where Tz::Offset: fmt::Display { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{} {}", self.naive_local(), self.offset) } } impl str::FromStr for DateTime { type Err = ParseError; fn from_str(s: &str) -> ParseResult> { const ITEMS: &'static [Item<'static>] = &[ Item::Space(""), Item::Numeric(Numeric::Year, Pad::Zero), Item::Space(""), Item::Literal("-"), Item::Space(""), Item::Numeric(Numeric::Month, Pad::Zero), Item::Space(""), Item::Literal("-"), Item::Space(""), Item::Numeric(Numeric::Day, Pad::Zero), Item::Space(""), Item::Literal("T"), // XXX shouldn't this be case-insensitive? Item::Space(""), Item::Numeric(Numeric::Hour, Pad::Zero), Item::Space(""), Item::Literal(":"), Item::Space(""), Item::Numeric(Numeric::Minute, Pad::Zero), Item::Space(""), Item::Literal(":"), Item::Space(""), Item::Numeric(Numeric::Second, Pad::Zero), Item::Fixed(Fixed::Nanosecond), Item::Space(""), Item::Fixed(Fixed::TimezoneOffsetZ), Item::Space(""), ]; let mut parsed = Parsed::new(); try!(parse(&mut parsed, s, ITEMS.iter().cloned())); parsed.to_datetime() } } impl str::FromStr for DateTime { type Err = ParseError; fn from_str(s: &str) -> ParseResult> { s.parse::>().map(|dt| dt.with_timezone(&UTC)) } } impl str::FromStr for DateTime { type Err = ParseError; fn from_str(s: &str) -> ParseResult> { s.parse::>().map(|dt| dt.with_timezone(&Local)) } } #[cfg(feature = "serde")] mod serde { use super::DateTime; use offset::TimeZone; use offset::utc::UTC; use offset::local::Local; use offset::fixed::FixedOffset; use std::fmt::Display; use serde::{ser, de}; impl ser::Serialize for DateTime where Tz::Offset: Display { fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: ser::Serializer { // Debug formatting is correct RFC3339, and it allows Zulu. serializer.visit_str(&format!("{:?}", self)) } } struct DateTimeVisitor; impl de::Visitor for DateTimeVisitor { type Value = DateTime; fn visit_str(&mut self, value: &str) -> Result, E> where E: de::Error { value.parse().map_err(|err| E::syntax(&format!("{}", err))) } } impl de::Deserialize for DateTime { fn deserialize(deserializer: &mut D) -> Result where D: de::Deserializer { deserializer.visit(DateTimeVisitor) } } impl de::Deserialize for DateTime { fn deserialize(deserializer: &mut D) -> Result where D: de::Deserializer { deserializer.visit(DateTimeVisitor).map(|dt| dt.with_timezone(&UTC)) } } impl de::Deserialize for DateTime { fn deserialize(deserializer: &mut D) -> Result where D: de::Deserializer { deserializer.visit(DateTimeVisitor).map(|dt| dt.with_timezone(&Local)) } } } #[cfg(test)] mod tests { use super::DateTime; use Datelike; use naive::time::NaiveTime; use naive::date::NaiveDate; use duration::Duration; use offset::TimeZone; use offset::utc::UTC; use offset::local::Local; use offset::fixed::FixedOffset; #[test] #[allow(non_snake_case)] fn test_datetime_offset() { let EST = FixedOffset::west(5*60*60); let EDT = FixedOffset::west(4*60*60); let KST = FixedOffset::east(9*60*60); assert_eq!(format!("{}", UTC.ymd(2014, 5, 6).and_hms(7, 8, 9)), "2014-05-06 07:08:09 UTC"); assert_eq!(format!("{}", EDT.ymd(2014, 5, 6).and_hms(7, 8, 9)), "2014-05-06 07:08:09 -04:00"); assert_eq!(format!("{}", KST.ymd(2014, 5, 6).and_hms(7, 8, 9)), "2014-05-06 07:08:09 +09:00"); assert_eq!(format!("{:?}", UTC.ymd(2014, 5, 6).and_hms(7, 8, 9)), "2014-05-06T07:08:09Z"); assert_eq!(format!("{:?}", EDT.ymd(2014, 5, 6).and_hms(7, 8, 9)), "2014-05-06T07:08:09-04:00"); assert_eq!(format!("{:?}", KST.ymd(2014, 5, 6).and_hms(7, 8, 9)), "2014-05-06T07:08:09+09:00"); // edge cases assert_eq!(format!("{:?}", UTC.ymd(2014, 5, 6).and_hms(0, 0, 0)), "2014-05-06T00:00:00Z"); assert_eq!(format!("{:?}", EDT.ymd(2014, 5, 6).and_hms(0, 0, 0)), "2014-05-06T00:00:00-04:00"); assert_eq!(format!("{:?}", KST.ymd(2014, 5, 6).and_hms(0, 0, 0)), "2014-05-06T00:00:00+09:00"); assert_eq!(format!("{:?}", UTC.ymd(2014, 5, 6).and_hms(23, 59, 59)), "2014-05-06T23:59:59Z"); assert_eq!(format!("{:?}", EDT.ymd(2014, 5, 6).and_hms(23, 59, 59)), "2014-05-06T23:59:59-04:00"); assert_eq!(format!("{:?}", KST.ymd(2014, 5, 6).and_hms(23, 59, 59)), "2014-05-06T23:59:59+09:00"); assert_eq!(UTC.ymd(2014, 5, 6).and_hms(7, 8, 9), EDT.ymd(2014, 5, 6).and_hms(3, 8, 9)); assert_eq!(UTC.ymd(2014, 5, 6).and_hms(7, 8, 9) + Duration::seconds(3600 + 60 + 1), 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(-7*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); } #[test] fn test_datetime_date_and_time() { let tz = FixedOffset::east(5*60*60); let d = tz.ymd(2014, 5, 6).and_hms(7, 8, 9); assert_eq!(d.time(), NaiveTime::from_hms(7, 8, 9)); assert_eq!(d.date(), tz.ymd(2014, 5, 6)); assert_eq!(d.date().naive_local(), NaiveDate::from_ymd(2014, 5, 6)); assert_eq!(d.date().and_time(d.time()), Some(d)); let tz = FixedOffset::east(4*60*60); let d = tz.ymd(2016, 5, 4).and_hms(3, 2, 1); assert_eq!(d.time(), NaiveTime::from_hms(3, 2, 1)); assert_eq!(d.date(), tz.ymd(2016, 5, 4)); assert_eq!(d.date().naive_local(), NaiveDate::from_ymd(2016, 5, 4)); assert_eq!(d.date().and_time(d.time()), Some(d)); let tz = FixedOffset::west(13*60*60); let d = tz.ymd(2017, 8, 9).and_hms(12, 34, 56); assert_eq!(d.time(), NaiveTime::from_hms(12, 34, 56)); assert_eq!(d.date(), tz.ymd(2017, 8, 9)); assert_eq!(d.date().naive_local(), NaiveDate::from_ymd(2017, 8, 9)); assert_eq!(d.date().and_time(d.time()), Some(d)); } #[test] fn test_datetime_with_timezone() { let local_now = Local::now(); let utc_now = local_now.with_timezone(&UTC); let local_now2 = utc_now.with_timezone(&Local); assert_eq!(local_now, local_now2); } #[test] #[allow(non_snake_case)] fn test_datetime_rfc2822_and_rfc3339() { let EDT = FixedOffset::east(5*60*60); assert_eq!(UTC.ymd(2015, 2, 18).and_hms(23, 16, 9).to_rfc2822(), "Wed, 18 Feb 2015 23:16:09 +0000"); assert_eq!(UTC.ymd(2015, 2, 18).and_hms(23, 16, 9).to_rfc3339(), "2015-02-18T23:16:09+00:00"); assert_eq!(EDT.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150).to_rfc2822(), "Wed, 18 Feb 2015 23:16:09 +0500"); assert_eq!(EDT.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150).to_rfc3339(), "2015-02-18T23:16:09.150+05:00"); assert_eq!(EDT.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567).to_rfc2822(), "Wed, 18 Feb 2015 23:59:60 +0500"); assert_eq!(EDT.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567).to_rfc3339(), "2015-02-18T23:59:60.234567+05:00"); assert_eq!(DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 +0000"), Ok(FixedOffset::east(0).ymd(2015, 2, 18).and_hms(23, 16, 9))); assert_eq!(DateTime::parse_from_rfc3339("2015-02-18T23:16:09Z"), Ok(FixedOffset::east(0).ymd(2015, 2, 18).and_hms(23, 16, 9))); assert_eq!(DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:59:60 +0500"), Ok(EDT.ymd(2015, 2, 18).and_hms_milli(23, 59, 59, 1_000))); assert_eq!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+05:00"), Ok(EDT.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567))); } #[test] fn test_datetime_from_str() { assert_eq!("2015-2-18T23:16:9.15Z".parse::>(), Ok(FixedOffset::east(0).ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150))); assert_eq!("2015-2-18T13:16:9.15-10:00".parse::>(), Ok(FixedOffset::west(10 * 3600).ymd(2015, 2, 18).and_hms_milli(13, 16, 9, 150))); assert!("2015-2-18T23:16:9.15".parse::>().is_err()); assert_eq!("2015-2-18T23:16:9.15Z".parse::>(), Ok(UTC.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150))); assert_eq!("2015-2-18T13:16:9.15-10:00".parse::>(), Ok(UTC.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150))); assert!("2015-2-18T23:16:9.15".parse::>().is_err()); // no test for `DateTime`, we cannot verify that much. } #[test] fn test_datetime_parse_from_str() { let ymdhms = |y,m,d,h,n,s,off| FixedOffset::east(off).ymd(y,m,d).and_hms(h,n,s); assert_eq!(DateTime::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"), Ok(ymdhms(2014, 5, 7, 12, 34, 56, 570*60))); // ignore offset assert!(DateTime::parse_from_str("20140507000000", "%Y%m%d%H%M%S").is_err()); // no offset assert!(DateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT").is_err()); assert_eq!(UTC.datetime_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT"), Ok(UTC.ymd(2013, 8, 9).and_hms(23, 54, 35))); } #[test] fn test_datetime_format_with_local() { // if we are not around the year boundary, local and UTC date should have the same year let dt = Local::now().with_month(5).unwrap(); assert_eq!(dt.format("%Y").to_string(), dt.with_timezone(&UTC).format("%Y").to_string()); } #[test] fn test_datetime_is_copy() { // UTC is known to be `Copy`. let a = UTC::now(); let b = a; assert_eq!(a, b); } #[test] fn test_datetime_is_send() { use std::thread; // UTC is known to be `Send`. let a = UTC::now(); thread::spawn(move || { let _ = a; }).join().unwrap(); } #[cfg(feature = "serde")] extern crate serde_json; #[cfg(feature = "serde")] #[test] fn test_serde_serialize() { use self::serde_json::to_string; let date = UTC.ymd(2014, 7, 24).and_hms(12, 34, 6); let serialized = to_string(&date).unwrap(); assert_eq!(serialized, "\"2014-07-24T12:34:06Z\""); } #[cfg(feature = "serde")] #[test] fn test_serde_deserialize() { use self::serde_json::from_str; let date = UTC.ymd(2014, 7, 24).and_hms(12, 34, 6); let deserialized: DateTime = from_str("\"2014-07-24T12:34:06Z\"").unwrap(); assert_eq!(deserialized, date); } }