Merge pull request #344 from quodlibetor/extract-num-days-from-epoch

Add Datelike::num_days_from_epoch
This commit is contained in:
Brandon W Maister 2019-09-20 11:22:58 -04:00 committed by GitHub
commit 9e50bfe034
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 0 deletions

View File

@ -454,6 +454,8 @@ macro_rules! try_opt {
($e:expr) => (match $e { Some(v) => v, None => return None })
}
const EPOCH_NUM_DAYS_FROM_CE: i32 = 719_163;
mod div;
#[cfg(not(feature="clock"))]
mod oldtime;
@ -895,6 +897,7 @@ pub trait Datelike: Sized {
///
/// ~~~
/// 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);
/// ~~~
@ -911,6 +914,21 @@ pub trait Datelike: Sized {
ndays += ((year * 1461) >> 2) - div_100 + (div_100 >> 2);
ndays + self.ordinal() as i32
}
/// Return the number of days since the unix epoch, 1970-01-01
///
/// # Example:
///
/// ```
/// use chrono::{NaiveDate, Datelike};
///
/// assert_eq!(NaiveDate::from_ymd(1970, 1, 1).num_days_from_epoch(), 0);
/// assert_eq!(NaiveDate::from_ymd(1969, 1, 1).num_days_from_epoch(), -365);
/// assert_eq!(NaiveDate::from_ymd(1999, 12, 31).num_days_from_epoch(), 10_956);
/// ```
fn num_days_from_epoch(&self) -> i32 {
self.num_days_from_ce() - EPOCH_NUM_DAYS_FROM_CE
}
}
/// The common set of methods for time component.