web_time/
web.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! Platform-specific extensions to [`web-time`](crate) for the Web platform.

#![allow(clippy::absolute_paths)]

use std::time::SystemTime as StdSystemTime;

use crate::SystemTime;

#[cfg(all(
	target_arch = "wasm32",
	any(target_os = "unknown", target_os = "none"),
	not(feature = "std"),
))]
#[doc(hidden)]
mod std {
	pub mod time {
		pub struct SystemTime;
	}
}

/// Web-specific extension to [`web_time::SystemTime`](crate::SystemTime).
pub trait SystemTimeExt {
	/// Convert [`web_time::SystemTime`](crate::SystemTime) to
	/// [`std::time::SystemTime`].
	///
	/// # Note
	///
	/// This might give a misleading impression of compatibility!
	///
	/// Considering this functionality will probably be used to interact with
	/// incompatible APIs of other dependencies, care should be taken that the
	/// dependency in question doesn't call [`std::time::SystemTime::now()`]
	/// internally, which would panic.
	#[cfg_attr(
		all(
			target_arch = "wasm32",
			any(target_os = "unknown", target_os = "none"),
			not(feature = "std"),
		),
		doc = "",
		doc = "[`std::time::SystemTime`]: https://doc.rust-lang.org/std/time/struct.SystemTime.html",
		doc = "[`std::time::SystemTime::now()`]: https://doc.rust-lang.org/std/time/struct.SystemTime.html#method.now"
	)]
	fn to_std(self) -> std::time::SystemTime;

	/// Convert [`std::time::SystemTime`] to
	/// [`web_time::SystemTime`](crate::SystemTime).
	///
	/// # Note
	///
	/// This might give a misleading impression of compatibility!
	///
	/// Considering this functionality will probably be used to interact with
	/// incompatible APIs of other dependencies, care should be taken that the
	/// dependency in question doesn't call [`std::time::SystemTime::now()`]
	/// internally, which would panic.
	#[cfg_attr(
		all(
			target_arch = "wasm32",
			any(target_os = "unknown", target_os = "none"),
			not(feature = "std"),
		),
		doc = "",
		doc = "[`std::time::SystemTime`]: https://doc.rust-lang.org/std/time/struct.SystemTime.html",
		doc = "[`std::time::SystemTime::now()`]: https://doc.rust-lang.org/std/time/struct.SystemTime.html#method.now"
	)]
	fn from_std(time: std::time::SystemTime) -> SystemTime;
}

impl SystemTimeExt for SystemTime {
	fn to_std(self) -> std::time::SystemTime {
		StdSystemTime::UNIX_EPOCH + self.0
	}

	fn from_std(time: std::time::SystemTime) -> SystemTime {
		Self::UNIX_EPOCH
			+ time
				.duration_since(StdSystemTime::UNIX_EPOCH)
				.expect("found `SystemTime` earlier than unix epoch")
	}
}