From b832ba336a8d899fbaaf44a686357c5a3e595214 Mon Sep 17 00:00:00 2001 From: Jared Vann Date: Thu, 21 Feb 2019 20:32:09 +0000 Subject: [PATCH 1/2] Add functions to present DateTime in microseconds since epoch --- src/datetime.rs | 24 ++++++++++++++++++++++++ src/naive/datetime.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/datetime.rs b/src/datetime.rs index e7a0ce681f..c72f53940c 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -140,6 +140,30 @@ impl DateTime { self.datetime.timestamp_millis() } + /// Returns the number of non-leap-microseconds since January 1, 1970 UTC + /// + /// Note that this does reduce the number of years that can be represented + /// from ~584 Billion to ~584 Thousand. (If this is a problem, please file + /// an issue to let me know what domain needs microsecond precision over + /// millenia, I'm curious.) + /// + /// # Example + /// + /// ~~~~ + /// use chrono::Utc; + /// use chrono::TimeZone; + /// + /// let dt = Utc.ymd(1970, 1, 1).and_hms_micro(0, 0, 1, 444); + /// assert_eq!(dt.timestamp_micros(), 1_000_444); + /// + /// let dt = Utc.ymd(2001, 9, 9).and_hms_micro(1, 46, 40, 555); + /// assert_eq!(dt.timestamp_micros(), 1_000_000_000_000_555); + /// ~~~~ + #[inline] + pub fn timestamp_micros(&self) -> i64 { + self.datetime.timestamp_micros() + } + /// Returns the number of non-leap-nanoseconds since January 1, 1970 UTC /// /// Note that this does reduce the number of years that can be represented diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index 26348160a8..234b0997ce 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -313,6 +313,33 @@ impl NaiveDateTime { as_ms + i64::from(self.timestamp_subsec_millis()) } + /// Returns the number of non-leap *microseconds* since midnight on January 1, 1970. + /// + /// Note that this does *not* account for the timezone! + /// The true "UNIX timestamp" would count seconds since the midnight *UTC* on the epoch. + /// + /// Note also that this does reduce the number of years that can be + /// represented from ~584 Billion to ~584 Thousand. (If this is a problem, + /// please file an issue to let me know what domain needs microsecond + /// precision over millenia, I'm curious.) + /// + /// # Example + /// + /// ~~~~ + /// use chrono::NaiveDate; + /// + /// let dt = NaiveDate::from_ymd(1970, 1, 1).and_hms_micro(0, 0, 1, 444); + /// assert_eq!(dt.timestamp_micros(), 1_000_444); + /// + /// let dt = NaiveDate::from_ymd(2001, 9, 9).and_hms_micro(1, 46, 40, 555); + /// assert_eq!(dt.timestamp_micros(), 1_000_000_000_000_555); + /// ~~~~ + #[inline] + pub fn timestamp_micros(&self) -> i64 { + let as_us = self.timestamp() * 1_000_000; + as_us + i64::from(self.timestamp_subsec_micros()) + } + /// Returns the number of non-leap *nanoseconds* since midnight on January 1, 1970. /// /// Note that this does *not* account for the timezone! From b3ce83550b3c622173589d0de7ec7e8272712437 Mon Sep 17 00:00:00 2001 From: Jared Vann Date: Thu, 21 Feb 2019 20:36:45 +0000 Subject: [PATCH 2/2] Add serde functions for microsecond timestamps --- CHANGELOG.md | 1 + src/datetime.rs | 159 +++++++++++++++++++++++++++++++++++++++++++++++- src/lib.rs | 2 + 3 files changed, 161 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffd9262e9a..525db47c0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Versions with only mechanical changes will be omitted from the following list. ## 0.4.20 (unreleased) * Add more formatting documentation and examples. +* Add support for microseconds timestamps serde serialization/deserialization (#304) ## 0.4.19 diff --git a/src/datetime.rs b/src/datetime.rs index c72f53940c..72d177dce0 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -1306,7 +1306,164 @@ pub mod serde { } } - /// Ser/de to/from optional timestamps in nanoseconds + /// Ser/de to/from timestamps in microseconds + /// + /// Intended for use with `serde`'s `with` attribute. + /// + /// # Example: + /// + /// ```rust + /// # // We mark this ignored so that we can test on 1.13 (which does not + /// # // support custom derive), and run tests with --ignored on beta and + /// # // nightly to actually trigger these. + /// # + /// # #[macro_use] extern crate serde_derive; + /// # #[macro_use] extern crate serde_json; + /// # extern crate chrono; + /// # use chrono::{TimeZone, DateTime, Utc}; + /// use chrono::serde::ts_microseconds; + /// #[derive(Deserialize, Serialize)] + /// struct S { + /// #[serde(with = "ts_microseconds")] + /// time: DateTime + /// } + /// + /// # fn example() -> Result { + /// let time = Utc.ymd(2018, 5, 17).and_hms_micro(02, 04, 59, 918355); + /// let my_s = S { + /// time: time.clone(), + /// }; + /// + /// let as_string = serde_json::to_string(&my_s)?; + /// assert_eq!(as_string, r#"{"time":1526522699918355}"#); + /// let my_s: S = serde_json::from_str(&as_string)?; + /// assert_eq!(my_s.time, time); + /// # Ok(my_s) + /// # } + /// # fn main() { example().unwrap(); } + /// ``` + pub mod ts_microseconds { + use core::fmt; + + use serdelib::{de, ser}; + + use offset::TimeZone; + use {DateTime, Utc}; + + use super::serde_from; + + /// Serialize a UTC datetime into an integer number of microseconds since the epoch + /// + /// Intended for use with `serde`s `serialize_with` attribute. + /// + /// # Example: + /// + /// ```rust + /// # // We mark this ignored so that we can test on 1.13 (which does not + /// # // support custom derive), and run tests with --ignored on beta and + /// # // nightly to actually trigger these. + /// # + /// # #[macro_use] extern crate serde_derive; + /// # #[macro_use] extern crate serde_json; + /// # extern crate chrono; + /// # use chrono::{TimeZone, DateTime, Utc}; + /// use chrono::serde::ts_microseconds::serialize as to_micro_ts; + /// #[derive(Serialize)] + /// struct S { + /// #[serde(serialize_with = "to_micro_ts")] + /// time: DateTime + /// } + /// + /// # fn example() -> Result { + /// let my_s = S { + /// time: Utc.ymd(2018, 5, 17).and_hms_micro(02, 04, 59, 918355), + /// }; + /// let as_string = serde_json::to_string(&my_s)?; + /// assert_eq!(as_string, r#"{"time":1526522699918355}"#); + /// # Ok(as_string) + /// # } + /// # fn main() { example().unwrap(); } + /// ``` + pub fn serialize(dt: &DateTime, serializer: S) -> Result + where + S: ser::Serializer, + { + serializer.serialize_i64(dt.timestamp_micros()) + } + + /// Deserialize a `DateTime` from a microsecond timestamp + /// + /// Intended for use with `serde`s `deserialize_with` attribute. + /// + /// # Example: + /// + /// ```rust + /// # // We mark this ignored so that we can test on 1.13 (which does not + /// # // support custom derive), and run tests with --ignored on beta and + /// # // nightly to actually trigger these. + /// # + /// # #[macro_use] extern crate serde_derive; + /// # #[macro_use] extern crate serde_json; + /// # extern crate chrono; + /// # use chrono::{DateTime, Utc}; + /// use chrono::serde::ts_microseconds::deserialize as from_micro_ts; + /// #[derive(Deserialize)] + /// struct S { + /// #[serde(deserialize_with = "from_micro_ts")] + /// time: DateTime + /// } + /// + /// # fn example() -> Result { + /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?; + /// # Ok(my_s) + /// # } + /// # fn main() { example().unwrap(); } + /// ``` + pub fn deserialize<'de, D>(d: D) -> Result, D::Error> + where + D: de::Deserializer<'de>, + { + #[allow(deprecated)] + Ok(try!(d.deserialize_i64(MicroSecondsTimestampVisitor))) + } + + struct MicroSecondsTimestampVisitor; + + impl<'de> de::Visitor<'de> for MicroSecondsTimestampVisitor { + type Value = DateTime; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "a unix timestamp in microseconds") + } + + /// Deserialize a timestamp in milliseconds since the epoch + fn visit_i64(self, value: i64) -> Result, E> + where + E: de::Error, + { + serde_from( + Utc.timestamp_opt(value / 1000_000, ((value % 1000_000) * 1_000) as u32), + &value, + ) + } + + /// Deserialize a timestamp in milliseconds since the epoch + fn visit_u64(self, value: u64) -> Result, E> + where + E: de::Error, + { + serde_from( + Utc.timestamp_opt( + (value / 1000_000) as i64, + ((value % 1000_000) * 1_000) as u32, + ), + &value, + ) + } + } + } + + /// Ser/de to/from timestamps in nanoseconds /// /// Intended for use with `serde`'s `with` attribute. /// diff --git a/src/lib.rs b/src/lib.rs index e5045ce19e..7d77868782 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -427,6 +427,8 @@ redundant_static_lifetimes, // the field-init shorthand (which this lint recommends) was stabilized in rust 1.17. redundant_field_names, + // #[non_exhaustive] was introduced in 1.40 + manual_non_exhaustive, // `matches!` was stabilized in 1.42 match_like_matches_macro, // Changing trivially_copy_pass_by_ref would require an incompatible version