Part 1: Add {From,To}Millis.

I think this is just oversight.  Generally, we should anticipate what
our consumers need to do to interact with Mentat, and producing milli-
and micro-second timestamps is part of that need.
This commit is contained in:
Nick Alexander 2018-06-19 14:01:45 -07:00
parent 3744982cd9
commit 1c0602fa00
2 changed files with 24 additions and 0 deletions

View file

@ -57,9 +57,11 @@ pub use parse::ParseError;
pub use uuid::ParseError as UuidParseError;
pub use types::{
FromMicros,
FromMillis,
Span,
SpannedValue,
ToMicros,
ToMillis,
Value,
ValueAndSpan,
};

View file

@ -649,6 +649,28 @@ impl ToMicros for DateTime<Utc> {
}
}
pub trait FromMillis {
fn from_millis(ts: i64) -> Self;
}
impl FromMillis for DateTime<Utc> {
fn from_millis(ts: i64) -> Self {
Utc.timestamp(ts / 1_000, ((ts % 1_000).abs() as u32) * 1_000)
}
}
pub trait ToMillis {
fn to_millis(&self) -> i64;
}
impl ToMillis for DateTime<Utc> {
fn to_millis(&self) -> i64 {
let major: i64 = self.timestamp() * 1_000;
let minor: i64 = self.timestamp_subsec_millis() as i64;
major + minor
}
}
#[cfg(test)]
mod test {
extern crate chrono;