]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/cloudabi/time.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys / cloudabi / time.rs
CommitLineData
532ac7d7
XL
1use crate::mem;
2use crate::sys::cloudabi::abi;
3use crate::time::Duration;
2c00a5a8
XL
4
5const NSEC_PER_SEC: abi::timestamp = 1_000_000_000;
6
7#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
8pub struct Instant {
9 t: abi::timestamp,
10}
11
0731742a 12pub 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
16impl 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)]
49pub struct SystemTime {
50 t: abi::timestamp,
51}
52
53impl 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
82pub const UNIX_EPOCH: SystemTime = SystemTime { t: 0 };