2021-03-20 19:38:44 +00:00
|
|
|
//! Module containing helper functions to manipulate time
|
2021-03-15 15:21:41 +00:00
|
|
|
use chrono::{SecondsFormat, TimeZone, Utc};
|
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
|
2021-03-20 19:38:44 +00:00
|
|
|
/// Returns milliseconds since UNIX Epoch
|
2021-03-15 15:21:41 +00:00
|
|
|
pub fn now_msec() -> u64 {
|
|
|
|
SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.expect("Fix your clock :o")
|
|
|
|
.as_millis() as u64
|
|
|
|
}
|
|
|
|
|
2021-12-14 12:55:11 +00:00
|
|
|
/// Increment logical clock
|
|
|
|
pub fn increment_logical_clock(prev: u64) -> u64 {
|
|
|
|
std::cmp::max(prev + 1, now_msec())
|
|
|
|
}
|
|
|
|
|
2021-03-20 19:38:44 +00:00
|
|
|
/// Convert a timestamp represented as milliseconds since UNIX Epoch to
|
|
|
|
/// its RFC3339 representation, such as "2021-01-01T12:30:00Z"
|
2021-03-15 15:21:41 +00:00
|
|
|
pub fn msec_to_rfc3339(msecs: u64) -> String {
|
|
|
|
let secs = msecs as i64 / 1000;
|
|
|
|
let nanos = (msecs as i64 % 1000) as u32 * 1_000_000;
|
|
|
|
let timestamp = Utc.timestamp(secs, nanos);
|
2021-05-03 20:45:42 +00:00
|
|
|
timestamp.to_rfc3339_opts(SecondsFormat::Millis, true)
|
2021-03-15 15:21:41 +00:00
|
|
|
}
|