tz/utils/
system_time.rs

1//! Some useful system time functions.
2
3use std::time::{Duration, SystemTime};
4
5/// Returns the duration between Unix epoch (`1970-01-01T00:00:00Z`) and now.
6///
7/// The `Ok` variant corresponds to a positive duration, and the `Err` variant to a negative duration.
8fn current_duration_since_epoch() -> Result<Duration, Duration> {
9    SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).map_err(|e| e.duration())
10}
11
12/// Returns the current Unix time in seconds
13pub(crate) fn current_unix_time() -> i64 {
14    match current_duration_since_epoch() {
15        Ok(duration) => 0i64.saturating_add_unsigned(duration.as_secs()),
16        Err(duration) => 0i64.saturating_sub_unsigned(duration.as_secs()),
17    }
18}
19
20/// Returns the total nanoseconds between Unix epoch (`1970-01-01T00:00:00Z`) and now
21pub(crate) fn current_total_nanoseconds() -> i128 {
22    match current_duration_since_epoch() {
23        Ok(duration) => 0i128.saturating_add_unsigned(duration.as_nanos()),
24        Err(duration) => 0i128.saturating_sub_unsigned(duration.as_nanos()),
25    }
26}