Merge pull request #252 from quodlibetor/negative-timestamps

Add doctests around negative timestamps
This commit is contained in:
Brandon W Maister 2018-07-28 17:07:04 -04:00 committed by GitHub
commit 369ce72987
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View File

@ -884,7 +884,16 @@ pub trait Datelike: Sized {
/// Returns `None` when the resulting value would be invalid. /// Returns `None` when the resulting value would be invalid.
fn with_ordinal0(&self, ordinal0: u32) -> Option<Self>; fn with_ordinal0(&self, ordinal0: u32) -> Option<Self>;
/// 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 { 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. // 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; let mut year = self.year() - 1;

View File

@ -255,12 +255,19 @@ impl NaiveDateTime {
/// ///
/// let dt = NaiveDate::from_ymd(2001, 9, 9).and_hms(1, 46, 40); /// let dt = NaiveDate::from_ymd(2001, 9, 9).and_hms(1, 46, 40);
/// assert_eq!(dt.timestamp(), 1_000_000_000); /// 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] #[inline]
pub fn timestamp(&self) -> i64 { pub fn timestamp(&self) -> i64 {
let ndays = i64::from(self.date.num_days_from_ce()); const UNIX_EPOCH_DAY: i64 = 719_163;
let nseconds = i64::from(self.time.num_seconds_from_midnight()); let gregorian_day = i64::from(self.date.num_days_from_ce());
(ndays - 719_163) * 86_400 + nseconds 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. /// 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); /// 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); /// 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] #[inline]
pub fn timestamp_millis(&self) -> i64 { pub fn timestamp_millis(&self) -> i64 {

View File

@ -475,7 +475,7 @@ mod tests {
use Weekday; use Weekday;
use super::{Of, Mdf}; use super::{Of, Mdf};
use super::{YearFlags, A, B, C, D, E, F, G, AG, BA, CB, DC, ED, FE, GF}; 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; use std::u32;
const NONLEAP_FLAGS: [YearFlags; 7] = [A, B, C, D, E, F, G]; const NONLEAP_FLAGS: [YearFlags; 7] = [A, B, C, D, E, F, G];