1use std::time::SystemTime;
7
8#[allow(unreachable_code)]
10fn time_nanos() -> u128 {
11 #[cfg(target_arch = "wasm32")]
12 {
13 return crate::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]
24#[allow(clippy::cast_possible_truncation)]
25pub fn now_secs() -> u64 {
26 (time_nanos() / 1_000_000_000) as u64
27}
28
29#[must_use]
31#[allow(clippy::cast_possible_truncation)]
32pub fn now_millis() -> u64 {
33 (time_nanos() / 1_000_000) as u64
34}
35
36#[must_use]
38#[allow(clippy::cast_possible_truncation)]
39pub fn now_micros() -> u64 {
40 (time_nanos() / 1_000) as u64
41}
42
43#[must_use]
45#[allow(clippy::cast_possible_truncation)]
46pub fn now_nanos() -> u64 {
47 time_nanos() as u64
48}
49
50#[cfg(test)]
55pub mod test {
56 use super::*;
57
58 #[test]
59 fn test_now_secs_sanity() {
60 let now = now_secs();
61 let current_year_secs = 1_700_000_000; assert!(now > current_year_secs);
63 }
64}