web_time/time/
system_time.rs

1//! Re-implementation of [`std::time::SystemTime`].
2
3use std::error::Error;
4use std::fmt::{self, Display, Formatter};
5use std::ops::{Add, AddAssign, Sub, SubAssign};
6use std::time::Duration;
7
8/// See [`std::time::SystemTime`].
9#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
10pub struct SystemTime(pub(crate) Duration);
11
12impl SystemTime {
13	/// See [`std::time::SystemTime::UNIX_EPOCH`].
14	pub const UNIX_EPOCH: Self = Self(Duration::ZERO);
15
16	/// See [`std::time::SystemTime::now()`].
17	#[must_use]
18	#[allow(clippy::missing_panics_doc)]
19	pub fn now() -> Self {
20		#[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
21		let ms = js_sys::Date::now() as i64;
22		let ms = ms.try_into().expect("found negative timestamp");
23
24		Self(Duration::from_millis(ms))
25	}
26
27	/// See [`std::time::SystemTime::duration_since()`].
28	#[allow(clippy::missing_errors_doc, clippy::trivially_copy_pass_by_ref)]
29	pub fn duration_since(&self, earlier: Self) -> Result<Duration, SystemTimeError> {
30		if self.0 < earlier.0 {
31			Err(SystemTimeError(earlier.0 - self.0))
32		} else {
33			Ok(self.0 - earlier.0)
34		}
35	}
36
37	/// See [`std::time::SystemTime::elapsed()`].
38	#[allow(clippy::missing_errors_doc, clippy::trivially_copy_pass_by_ref)]
39	pub fn elapsed(&self) -> Result<Duration, SystemTimeError> {
40		Self::now().duration_since(*self)
41	}
42
43	/// See [`std::time::SystemTime::checked_add()`].
44	#[allow(clippy::trivially_copy_pass_by_ref)]
45	pub fn checked_add(&self, duration: Duration) -> Option<Self> {
46		self.0.checked_add(duration).map(SystemTime)
47	}
48
49	/// See [`std::time::SystemTime::checked_sub()`].
50	#[allow(clippy::trivially_copy_pass_by_ref)]
51	pub fn checked_sub(&self, duration: Duration) -> Option<Self> {
52		self.0.checked_sub(duration).map(SystemTime)
53	}
54}
55
56impl Add<Duration> for SystemTime {
57	type Output = Self;
58
59	/// # Panics
60	///
61	/// This function may panic if the resulting point in time cannot be
62	/// represented by the underlying data structure. See
63	/// [`SystemTime::checked_add`] for a version without panic.
64	fn add(self, dur: Duration) -> Self {
65		self.checked_add(dur)
66			.expect("overflow when adding duration to instant")
67	}
68}
69
70impl AddAssign<Duration> for SystemTime {
71	fn add_assign(&mut self, other: Duration) {
72		*self = *self + other;
73	}
74}
75
76impl Sub<Duration> for SystemTime {
77	type Output = Self;
78
79	fn sub(self, dur: Duration) -> Self {
80		self.checked_sub(dur)
81			.expect("overflow when subtracting duration from instant")
82	}
83}
84
85impl SubAssign<Duration> for SystemTime {
86	fn sub_assign(&mut self, other: Duration) {
87		*self = *self - other;
88	}
89}
90
91/// See [`std::time::SystemTimeError`].
92#[derive(Clone, Debug)]
93#[allow(missing_copy_implementations)]
94pub struct SystemTimeError(Duration);
95
96impl SystemTimeError {
97	/// See [`std::time::SystemTimeError::duration()`].
98	#[must_use]
99	#[allow(clippy::missing_const_for_fn)]
100	pub fn duration(&self) -> Duration {
101		self.0
102	}
103}
104
105impl Display for SystemTimeError {
106	fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
107		write!(formatter, "second time provided was later than self")
108	}
109}
110
111impl Error for SystemTimeError {}