30 lines
851 B
Rust
30 lines
851 B
Rust
use serde::Serialize;
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
/// Wall-clock instant stored as nanoseconds since the Unix epoch.
|
|
///
|
|
/// The README's data model sketches this field as `Instant`, but `Instant` has no
|
|
/// epoch and can't be serialized to the frontend, so wall-clock time is stored here
|
|
/// instead.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
|
|
pub struct Timestamp(i64);
|
|
|
|
impl Timestamp {
|
|
pub fn now() -> Self {
|
|
let nanos = SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.expect("system clock is before the Unix epoch")
|
|
.as_nanos() as i64;
|
|
Timestamp(nanos)
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn from_millis(millis: i64) -> Self {
|
|
Timestamp(millis * 1_000_000)
|
|
}
|
|
|
|
pub fn as_millis(&self) -> i64 {
|
|
self.0 / 1_000_000
|
|
}
|
|
}
|