1 use crate::time::Duration;
3 use crate::sys::cvt_wasi;
6 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
7 pub struct Instant(Duration);
9 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
10 pub struct SystemTime(Duration);
12 pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));
14 fn current_time(clock: u32) -> Duration {
16 let mut ts = mem::zeroed();
17 cvt_wasi(libc::__wasi_clock_time_get(
19 1, // precision... seems ignored though?
23 (ts / 1_000_000_000) as u64,
24 (ts % 1_000_000_000) as u32,
30 pub fn now() -> Instant {
31 Instant(current_time(libc::__WASI_CLOCK_MONOTONIC))
34 pub const fn zero() -> Instant {
35 Instant(Duration::from_secs(0))
38 pub fn actually_monotonic() -> bool {
42 pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
43 self.0.checked_sub(other.0)
46 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
47 Some(Instant(self.0.checked_add(*other)?))
50 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
51 Some(Instant(self.0.checked_sub(*other)?))
56 pub fn now() -> SystemTime {
57 SystemTime(current_time(libc::__WASI_CLOCK_REALTIME))
60 pub fn sub_time(&self, other: &SystemTime)
61 -> Result<Duration, Duration> {
62 self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
65 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
66 Some(SystemTime(self.0.checked_add(*other)?))
69 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
70 Some(SystemTime(self.0.checked_sub(*other)?))