]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/wasm/time.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys / wasm / time.rs
1 use crate::time::Duration;
2
3 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
4 pub struct Instant(Duration);
5
6 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
7 pub struct SystemTime(Duration);
8
9 pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));
10
11 impl Instant {
12 pub fn now() -> Instant {
13 panic!("time not implemented on wasm32-unknown-unknown")
14 }
15
16 pub const fn zero() -> Instant {
17 Instant(Duration::from_secs(0))
18 }
19
20 pub fn actually_monotonic() -> bool {
21 false
22 }
23
24 pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
25 self.0.checked_sub(other.0)
26 }
27
28 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
29 Some(Instant(self.0.checked_add(*other)?))
30 }
31
32 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
33 Some(Instant(self.0.checked_sub(*other)?))
34 }
35 }
36
37 impl SystemTime {
38 pub fn now() -> SystemTime {
39 panic!("time not implemented on wasm32-unknown-unknown")
40 }
41
42 pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
43 self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
44 }
45
46 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
47 Some(SystemTime(self.0.checked_add(*other)?))
48 }
49
50 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
51 Some(SystemTime(self.0.checked_sub(*other)?))
52 }
53 }