about summary refs log tree commit diff
path: root/library/std/src/sys/pal/wasi/time.rs
blob: 892661b312b2f02bdaf16ff1badd1a84832c828f (plain)
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
#![forbid(unsafe_op_in_unsafe_fn)]

use crate::time::Duration;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct Instant(Duration);

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct SystemTime(Duration);

pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));

fn current_time(clock: wasi::Clockid) -> Duration {
    let ts = unsafe {
        wasi::clock_time_get(
            clock, 1, // precision... seems ignored though?
        )
        .unwrap()
    };
    Duration::new((ts / 1_000_000_000) as u64, (ts % 1_000_000_000) as u32)
}

impl Instant {
    pub fn now() -> Instant {
        Instant(current_time(wasi::CLOCKID_MONOTONIC))
    }

    pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
        self.0.checked_sub(other.0)
    }

    pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
        Some(Instant(self.0.checked_add(*other)?))
    }

    pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
        Some(Instant(self.0.checked_sub(*other)?))
    }
}

impl SystemTime {
    pub fn now() -> SystemTime {
        SystemTime(current_time(wasi::CLOCKID_REALTIME))
    }

    #[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
    pub const fn from_wasi_timestamp(ts: wasi::Timestamp) -> SystemTime {
        SystemTime(Duration::from_nanos(ts))
    }

    #[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
    pub const fn to_wasi_timestamp(&self) -> Option<wasi::Timestamp> {
        // FIXME: const TryInto
        let ns = self.0.as_nanos();
        if ns <= u64::MAX as u128 { Some(ns as u64) } else { None }
    }

    #[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
    pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
        // FIXME: ok_or_else with const closures
        match self.0.checked_sub(other.0) {
            Some(duration) => Ok(duration),
            None => Err(other.0 - self.0),
        }
    }

    #[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
    pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
        Some(SystemTime(self.0.checked_add(*other)?))
    }

    #[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
    pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
        Some(SystemTime(self.0.checked_sub(*other)?))
    }
}