1/*!
2Provides a centralized helper for getting the current system time.
34We 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.
910This 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*/
1516pub(crate) use self::sys::*;
1718#[cfg(not(all(
19 feature = "js",
20 any(target_arch = "wasm32", target_arch = "wasm64"),
21 target_os = "unknown"
22)))]
23mod sys {
24pub(crate) fn system_time() -> std::time::SystemTime {
25// `SystemTime::now()` should continue to panic on this exact target in
26 // the future as well; Instead of letting `std` panic, we panic first
27 // with a more informative error message.
28if falsecfg!(all(
29 not(feature = "js"),
30 any(target_arch = "wasm32", target_arch = "wasm64"),
31 target_os = "unknown"
32)) {
33{
::core::panicking::panic_fmt(format_args!("getting the current time in wasm32-unknown-unknown is not possible with just the standard library, enable Jiff\'s `js` feature if you are targeting a browser environment"));
};panic!(
34"getting the current time in wasm32-unknown-unknown \
35 is not possible with just the standard library, \
36 enable Jiff's `js` feature if you are \
37 targeting a browser environment",
38 );
39 } else {
40 std::time::SystemTime::now()
41 }
42 }
4344#[cfg(any(
45 feature = "tz-system",
46 feature = "tzdb-zoneinfo",
47 feature = "tzdb-concatenated"
48))]
49pub(crate) fn monotonic_time() -> Option<std::time::Instant> {
50// Same reasoning as above, but we return `None` instead of panicking,
51 // because Jiff can deal with environments that don't provide
52 // monotonic time.
53if cfg!(all(
54 not(feature = "js"),
55 any(target_arch = "wasm32", target_arch = "wasm64"),
56 target_os = "unknown"
57)) {
58None
59} else {
60Some(std::time::Instant::now())
61 }
62 }
63}
6465#[cfg(all(
66 feature = "js",
67 any(target_arch = "wasm32", target_arch = "wasm64"),
68 target_os = "unknown"
69))]
70mod sys {
71pub(crate) fn system_time() -> std::time::SystemTime {
72use std::time::{Duration, SystemTime};
7374let millis = js_sys::Date::new_0().get_time();
75let is_positive = millis.is_sign_positive();
76let millis = millis.abs() as u64;
77let duration = Duration::from_millis(millis);
78let result = if is_positive {
79 SystemTime::UNIX_EPOCH.checked_add(duration)
80 } else {
81 SystemTime::UNIX_EPOCH.checked_sub(duration)
82 };
83// It's a little sad that we have to panic here, but the
84 // standard SystemTime::now() API is infallible, so we kind
85 // of have to match it. With that said, a panic here would be
86 // highly unusual. It would imply that the system time is set
87 // to some extreme timestamp very far in the future or the
88 // past.
89let Some(timestamp) = result else {
90panic!(
91"failed to get current time from Javascript date: \
92 arithmetic on Unix epoch overflowed"
93)
94 };
95 timestamp
96 }
9798#[cfg(any(
99 feature = "tz-system",
100 feature = "tzdb-zoneinfo",
101 feature = "tzdb-concatenated"
102))]
103pub(crate) fn monotonic_time() -> Option<std::time::Instant> {
104// :-(
105None
106}
107}