Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests and handling for negative timestamps #252

Merged
merged 1 commit into from
Jul 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,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