Skip to content

Commit

Permalink
Merge pull request #252 from quodlibetor/negative-timestamps
Browse files Browse the repository at this point in the history
Add doctests around negative timestamps
  • Loading branch information
quodlibetor authored Jul 28, 2018
2 parents 55538b5 + a58a833 commit 369ce72
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,16 @@ pub trait Datelike: Sized {
/// Returns `None` when the resulting value would be invalid.
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 {
// 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;
Expand Down
16 changes: 13 additions & 3 deletions src/naive/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/naive/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 369ce72

Please sign in to comment.