jiff/
now.rs

1/*!
2Provides a centralized helper for getting the current system time.
3
4We generally rely on the standard library for this, but the standard library
5(by design) does not provide this for the `wasm{32,64}-unknown-unknown`
6targets. Instead, Jiff provides an opt-in `js` feature that only applies on the
7aforementioned target. Specifically, when enabled, it assumes a web context and
8runs JavaScript code to get the current time.
9
10This also exposes a "fallible" API for querying monotonic time. Since we only
11use monotonic time for managing expiration for caches, in the case where we
12can't get monotonic time (easily), we just consider the cache to always be
13expired. ¯\_(ツ)_/¯
14*/
15
16pub(crate) use self::sys::*;
17
18#[cfg(not(all(
19    feature = "js",
20    any(target_arch = "wasm32", target_arch = "wasm64"),
21    target_os = "unknown"
22)))]
23mod sys {
24    pub(crate) fn system_time() -> std::time::SystemTime {
25        std::time::SystemTime::now()
26    }
27
28    #[cfg(any(
29        feature = "tz-system",
30        feature = "tzdb-zoneinfo",
31        feature = "tzdb-concatenated"
32    ))]
33    pub(crate) fn monotonic_time() -> Option<std::time::Instant> {
34        Some(std::time::Instant::now())
35    }
36}
37
38#[cfg(all(
39    feature = "js",
40    any(target_arch = "wasm32", target_arch = "wasm64"),
41    target_os = "unknown"
42))]
43mod sys {
44    pub(crate) fn system_time() -> std::time::SystemTime {
45        use std::time::{Duration, SystemTime};
46
47        #[cfg(not(feature = "std"))]
48        use crate::util::libm::Float;
49
50        let millis = js_sys::Date::new_0().get_time();
51        let sign = millis.signum();
52        let millis = millis.abs() as u64;
53        let duration = Duration::from_millis(millis);
54        let result = if sign >= 0.0 {
55            SystemTime::UNIX_EPOCH.checked_add(duration)
56        } else {
57            SystemTime::UNIX_EPOCH.checked_sub(duration)
58        };
59        // It's a little sad that we have to panic here, but the standard
60        // SystemTime::now() API is infallible, so we kind of have to match it.
61        // With that said, a panic here would be highly unusual. It would imply
62        // that the system time is set to some extreme timestamp very far in the
63        // future or the past.
64        let Some(timestamp) = result else {
65            panic!(
66                "failed to get current time: \
67             subtracting {duration:?} from Unix epoch overflowed"
68            )
69        };
70        timestamp
71    }
72
73    #[cfg(any(
74        feature = "tz-system",
75        feature = "tzdb-zoneinfo",
76        feature = "tzdb-concatenated"
77    ))]
78    pub(crate) fn monotonic_time() -> Option<std::time::Instant> {
79        // :-(
80        None
81    }
82}