1use std::time::SystemTime;
7
8#[allow(unreachable_code)]
10fn time_nanos() -> u128 {
11 #[cfg(target_arch = "wasm32")]
12 {
13 return ::ic_cdk::api::time() as u128;
14 }
15
16 match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
17 Ok(d) => d.as_nanos(),
18 Err(_) => 0,
19 }
20}
21
22#[must_use]
26#[allow(clippy::cast_possible_truncation)]
27pub fn now_secs() -> u64 {
28 (time_nanos() / 1_000_000_000) as u64
29}
30
31#[must_use]
35#[allow(clippy::cast_possible_truncation)]
36pub fn now_millis() -> u64 {
37 (time_nanos() / 1_000_000) as u64
38}
39
40#[must_use]
44#[allow(clippy::cast_possible_truncation)]
45pub fn now_micros() -> u64 {
46 (time_nanos() / 1_000) as u64
47}
48
49#[must_use]
53#[allow(clippy::cast_possible_truncation)]
54pub fn now_nanos() -> u64 {
55 time_nanos() as u64
56}
57
58#[cfg(test)]
63pub mod test {
64 use super::*;
65
66 #[test]
67 fn test_now_secs_sanity() {
68 let now = now_secs();
69 let current_year_secs = 1_700_000_000; assert!(now > current_year_secs);
71 }
72}