use std::time::{SystemTime, UNIX_EPOCH};
#[inline]
#[must_use]
pub fn now_ms() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |d| i64::try_from(d.as_millis()).unwrap_or(i64::MAX))
}
#[inline]
#[must_use]
pub fn now_ns() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |d| i64::try_from(d.as_nanos()).unwrap_or(i64::MAX))
}
#[inline]
#[must_use]
pub fn timestamp_from_ms(ms: i64) -> buffa_types::google::protobuf::Timestamp {
buffa_types::google::protobuf::Timestamp {
seconds: ms.div_euclid(1000),
nanos: i32::try_from(ms.rem_euclid(1000) * 1_000_000).unwrap_or(0),
..Default::default()
}
}
#[inline]
#[must_use]
pub fn now_ts() -> buffa_types::google::protobuf::Timestamp {
timestamp_from_ms(now_ms())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn now_ms_is_plausible_epoch_millis() {
let ms = now_ms();
assert!(ms > 1_000_000_000_000, "now_ms {ms} is not epoch millis");
assert!(ms < 32_503_680_000_000, "now_ms {ms} is implausibly large");
}
#[test]
fn now_ns_is_millis_times_roughly_a_million() {
let ms = now_ms();
let ns = now_ns();
assert!(ns >= ms * 1_000_000, "ns {ns} must not predate ms {ms}");
}
#[test]
fn timestamp_from_ms_splits_seconds_and_nanos() {
let ts = timestamp_from_ms(1_234_567_890);
assert_eq!(ts.seconds, 1_234_567);
assert_eq!(ts.nanos, 890_000_000);
}
#[test]
fn timestamp_from_ms_handles_negative_millis_without_wrapping() {
let ts = timestamp_from_ms(-1);
assert_eq!(ts.seconds, -1);
assert_eq!(ts.nanos, 999_000_000);
}
#[test]
fn timestamp_from_ms_saturates_instead_of_wrapping() {
let ts = timestamp_from_ms(i64::MAX);
assert_eq!(ts.seconds, i64::MAX / 1000);
assert!(ts.nanos >= 0 && ts.nanos < 1_000_000_000);
}
#[test]
fn now_ts_agrees_with_now_ms() {
let ts = now_ts();
let ms = now_ms();
let reconstructed = ts.seconds * 1000 + i64::from(ts.nanos) / 1_000_000;
assert!(
(reconstructed - ms).abs() < 1_000,
"now_ts {reconstructed} must track now_ms {ms}"
);
}
}