]>
Commit | Line | Data |
---|---|---|
532ac7d7 XL |
1 | use crate::mem; |
2 | use crate::sys::cloudabi::abi; | |
3 | use crate::time::Duration; | |
2c00a5a8 XL |
4 | |
5 | const NSEC_PER_SEC: abi::timestamp = 1_000_000_000; | |
6 | ||
7 | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] | |
8 | pub struct Instant { | |
9 | t: abi::timestamp, | |
10 | } | |
11 | ||
0731742a | 12 | pub fn checked_dur2intervals(dur: &Duration) -> Option<abi::timestamp> { |
60c5eb7d | 13 | dur.as_secs().checked_mul(NSEC_PER_SEC)?.checked_add(dur.subsec_nanos() as abi::timestamp) |
2c00a5a8 XL |
14 | } |
15 | ||
16 | impl Instant { | |
17 | pub fn now() -> Instant { | |
18 | unsafe { | |
416331ca XL |
19 | let mut t: mem::MaybeUninit<abi::timestamp> = mem::MaybeUninit::uninit(); |
20 | let ret = abi::clock_time_get(abi::clockid::MONOTONIC, 0, t.as_mut_ptr()); | |
2c00a5a8 | 21 | assert_eq!(ret, abi::errno::SUCCESS); |
416331ca | 22 | Instant { t: t.assume_init() } |
2c00a5a8 XL |
23 | } |
24 | } | |
25 | ||
0731742a XL |
26 | pub fn actually_monotonic() -> bool { |
27 | true | |
28 | } | |
29 | ||
30 | pub const fn zero() -> Instant { | |
31 | Instant { t: 0 } | |
32 | } | |
33 | ||
532ac7d7 XL |
34 | pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { |
35 | let diff = self.t.checked_sub(other.t)?; | |
36 | Some(Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32)) | |
2c00a5a8 XL |
37 | } |
38 | ||
0731742a | 39 | pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { |
60c5eb7d | 40 | Some(Instant { t: self.t.checked_add(checked_dur2intervals(other)?)? }) |
2c00a5a8 XL |
41 | } |
42 | ||
0731742a | 43 | pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> { |
60c5eb7d | 44 | Some(Instant { t: self.t.checked_sub(checked_dur2intervals(other)?)? }) |
2c00a5a8 XL |
45 | } |
46 | } | |
47 | ||
48 | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] | |
49 | pub struct SystemTime { | |
50 | t: abi::timestamp, | |
51 | } | |
52 | ||
53 | impl SystemTime { | |
54 | pub fn now() -> SystemTime { | |
55 | unsafe { | |
416331ca XL |
56 | let mut t: mem::MaybeUninit<abi::timestamp> = mem::MaybeUninit::uninit(); |
57 | let ret = abi::clock_time_get(abi::clockid::REALTIME, 0, t.as_mut_ptr()); | |
2c00a5a8 | 58 | assert_eq!(ret, abi::errno::SUCCESS); |
416331ca | 59 | SystemTime { t: t.assume_init() } |
2c00a5a8 XL |
60 | } |
61 | } | |
62 | ||
63 | pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> { | |
64 | if self.t >= other.t { | |
65 | let diff = self.t - other.t; | |
60c5eb7d | 66 | Ok(Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32)) |
2c00a5a8 XL |
67 | } else { |
68 | let diff = other.t - self.t; | |
60c5eb7d | 69 | Err(Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32)) |
2c00a5a8 XL |
70 | } |
71 | } | |
72 | ||
a1dfa0c6 | 73 | pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> { |
60c5eb7d | 74 | Some(SystemTime { t: self.t.checked_add(checked_dur2intervals(other)?)? }) |
2c00a5a8 XL |
75 | } |
76 | ||
0731742a | 77 | pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> { |
60c5eb7d | 78 | Some(SystemTime { t: self.t.checked_sub(checked_dur2intervals(other)?)? }) |
2c00a5a8 XL |
79 | } |
80 | } | |
81 | ||
82 | pub const UNIX_EPOCH: SystemTime = SystemTime { t: 0 }; |