From a58a8333242f7ca3c39a143a4ff2ff2c22e52947 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Wed, 13 Jun 2018 09:10:31 -0400 Subject: [PATCH] Add doctests for negative timestamps --- src/lib.rs | 11 ++++++++++- src/naive/datetime.rs | 16 +++++++++++++--- src/naive/internals.rs | 2 +- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 658221b..51d58bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -871,7 +871,16 @@ pub trait Datelike: Sized { /// Returns `None` when the resulting value would be invalid. fn with_ordinal0(&self, ordinal0: u32) -> Option; - /// Returns the number of days since January 1, 1 (Day 1) in the proleptic Gregorian calendar. + /// Returns the number of days since January 1, Year 1 (aka Day 1) in the + /// proleptic Gregorian calendar. + /// + /// # Example: + /// + /// ~~~ + /// use chrono::{NaiveDate, Datelike}; + /// assert_eq!(NaiveDate::from_ymd(1970, 1, 1).num_days_from_ce(), 719163); + /// assert_eq!(NaiveDate::from_ymd(0, 1, 1).num_days_from_ce(), -365); + /// ~~~ fn num_days_from_ce(&self) -> i32 { // we know this wouldn't overflow since year is limited to 1/2^13 of i32's full range. let mut year = self.year() - 1; diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index bb3f33f..f648535 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -255,12 +255,19 @@ impl NaiveDateTime { /// /// let dt = NaiveDate::from_ymd(2001, 9, 9).and_hms(1, 46, 40); /// assert_eq!(dt.timestamp(), 1_000_000_000); + /// + /// let dt = NaiveDate::from_ymd(1969, 12, 31).and_hms(23, 59, 59); + /// assert_eq!(dt.timestamp(), -1); + /// + /// let dt = NaiveDate::from_ymd(-1, 1, 1).and_hms(0, 0, 0); + /// assert_eq!(dt.timestamp(), -62198755200); /// ~~~~ #[inline] pub fn timestamp(&self) -> i64 { - let ndays = i64::from(self.date.num_days_from_ce()); - let nseconds = i64::from(self.time.num_seconds_from_midnight()); - (ndays - 719_163) * 86_400 + nseconds + const UNIX_EPOCH_DAY: i64 = 719_163; + let gregorian_day = i64::from(self.date.num_days_from_ce()); + let seconds_from_midnight = i64::from(self.time.num_seconds_from_midnight()); + (gregorian_day - UNIX_EPOCH_DAY) * 86_400 + seconds_from_midnight } /// Returns the number of non-leap *milliseconds* since midnight on January 1, 1970. @@ -283,6 +290,9 @@ impl NaiveDateTime { /// /// let dt = NaiveDate::from_ymd(2001, 9, 9).and_hms_milli(1, 46, 40, 555); /// assert_eq!(dt.timestamp_millis(), 1_000_000_000_555); + /// + /// let dt = NaiveDate::from_ymd(1969, 12, 31).and_hms_milli(23, 59, 59, 100); + /// assert_eq!(dt.timestamp_millis(), -900); /// ~~~~ #[inline] pub fn timestamp_millis(&self) -> i64 { diff --git a/src/naive/internals.rs b/src/naive/internals.rs index fbf331d..dd9d535 100644 --- a/src/naive/internals.rs +++ b/src/naive/internals.rs @@ -475,7 +475,7 @@ mod tests { use Weekday; use super::{Of, Mdf}; use super::{YearFlags, A, B, C, D, E, F, G, AG, BA, CB, DC, ED, FE, GF}; - use num_iter::range_inclusive; + use self::num_iter::range_inclusive; use std::u32; const NONLEAP_FLAGS: [YearFlags; 7] = [A, B, C, D, E, F, G];