Skip to content

Commit

Permalink
Add functions to present DateTime in microseconds since epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredvann authored and quodlibetor committed Nov 13, 2020
1 parent 7373bc7 commit b832ba3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ impl<Tz: TimeZone> DateTime<Tz> {
self.datetime.timestamp_millis()
}

/// Returns the number of non-leap-microseconds since January 1, 1970 UTC
///
/// Note that this does reduce the number of years that can be represented
/// from ~584 Billion to ~584 Thousand. (If this is a problem, please file
/// an issue to let me know what domain needs microsecond precision over
/// millenia, I'm curious.)
///
/// # Example
///
/// ~~~~
/// use chrono::Utc;
/// use chrono::TimeZone;
///
/// let dt = Utc.ymd(1970, 1, 1).and_hms_micro(0, 0, 1, 444);
/// assert_eq!(dt.timestamp_micros(), 1_000_444);
///
/// let dt = Utc.ymd(2001, 9, 9).and_hms_micro(1, 46, 40, 555);
/// assert_eq!(dt.timestamp_micros(), 1_000_000_000_000_555);
/// ~~~~
#[inline]
pub fn timestamp_micros(&self) -> i64 {
self.datetime.timestamp_micros()
}

/// Returns the number of non-leap-nanoseconds since January 1, 1970 UTC
///
/// Note that this does reduce the number of years that can be represented
Expand Down
27 changes: 27 additions & 0 deletions src/naive/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,33 @@ impl NaiveDateTime {
as_ms + i64::from(self.timestamp_subsec_millis())
}

/// Returns the number of non-leap *microseconds* since midnight on January 1, 1970.
///
/// Note that this does *not* account for the timezone!
/// The true "UNIX timestamp" would count seconds since the midnight *UTC* on the epoch.
///
/// Note also that this does reduce the number of years that can be
/// represented from ~584 Billion to ~584 Thousand. (If this is a problem,
/// please file an issue to let me know what domain needs microsecond
/// precision over millenia, I'm curious.)
///
/// # Example
///
/// ~~~~
/// use chrono::NaiveDate;
///
/// let dt = NaiveDate::from_ymd(1970, 1, 1).and_hms_micro(0, 0, 1, 444);
/// assert_eq!(dt.timestamp_micros(), 1_000_444);
///
/// let dt = NaiveDate::from_ymd(2001, 9, 9).and_hms_micro(1, 46, 40, 555);
/// assert_eq!(dt.timestamp_micros(), 1_000_000_000_000_555);
/// ~~~~
#[inline]
pub fn timestamp_micros(&self) -> i64 {
let as_us = self.timestamp() * 1_000_000;
as_us + i64::from(self.timestamp_subsec_micros())
}

/// Returns the number of non-leap *nanoseconds* since midnight on January 1, 1970.
///
/// Note that this does *not* account for the timezone!
Expand Down

0 comments on commit b832ba3

Please sign in to comment.