1 #![deny(unsafe_op_in_unsafe_fn)]
3 use crate::time::Duration;
5 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
6 pub struct Instant(Duration);
8 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
9 pub struct SystemTime(Duration);
11 pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));
13 fn current_time(clock: u32) -> Duration {
16 clock, 1, // precision... seems ignored though?
20 Duration::new((ts / 1_000_000_000) as u64, (ts % 1_000_000_000) as u32)
24 pub fn now() -> Instant {
25 Instant(current_time(wasi::CLOCKID_MONOTONIC))
28 pub const fn zero() -> Instant {
29 Instant(Duration::from_secs(0))
32 pub fn actually_monotonic() -> bool {
36 pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
37 self.0.checked_sub(other.0)
40 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
41 Some(Instant(self.0.checked_add(*other)?))
44 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
45 Some(Instant(self.0.checked_sub(*other)?))
50 pub fn now() -> SystemTime {
51 SystemTime(current_time(wasi::CLOCKID_REALTIME))
54 pub fn from_wasi_timestamp(ts: wasi::Timestamp) -> SystemTime {
55 SystemTime(Duration::from_nanos(ts))
58 pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
59 self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
62 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
63 Some(SystemTime(self.0.checked_add(*other)?))
66 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
67 Some(SystemTime(self.0.checked_sub(*other)?))