web_time/web.rs
1//! Platform-specific extensions to [`web-time`](crate) for the Web platform.
2
3#![allow(clippy::absolute_paths)]
4
5use std::time::SystemTime as StdSystemTime;
6
7use crate::SystemTime;
8
9/// Web-specific extension to [`web_time::SystemTime`](crate::SystemTime).
10pub trait SystemTimeExt {
11 /// Convert [`web_time::SystemTime`](crate::SystemTime) to
12 /// [`std::time::SystemTime`].
13 ///
14 /// # Note
15 ///
16 /// This might give a misleading impression of compatibility!
17 ///
18 /// Considering this functionality will probably be used to interact with
19 /// incompatible APIs of other dependencies, care should be taken that the
20 /// dependency in question doesn't call [`std::time::SystemTime::now()`]
21 /// internally, which would panic.
22 fn to_std(self) -> std::time::SystemTime;
23
24 /// Convert [`std::time::SystemTime`] to
25 /// [`web_time::SystemTime`](crate::SystemTime).
26 ///
27 /// # Note
28 ///
29 /// This might give a misleading impression of compatibility!
30 ///
31 /// Considering this functionality will probably be used to interact with
32 /// incompatible APIs of other dependencies, care should be taken that the
33 /// dependency in question doesn't call [`std::time::SystemTime::now()`]
34 /// internally, which would panic.
35 fn from_std(time: std::time::SystemTime) -> SystemTime;
36}
37
38impl SystemTimeExt for SystemTime {
39 fn to_std(self) -> std::time::SystemTime {
40 StdSystemTime::UNIX_EPOCH + self.0
41 }
42
43 fn from_std(time: std::time::SystemTime) -> SystemTime {
44 Self::UNIX_EPOCH
45 + time
46 .duration_since(StdSystemTime::UNIX_EPOCH)
47 .expect("found `SystemTime` earlier then unix epoch")
48 }
49}