1pub(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 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 None
81 }
82}