package wasi:clocks@0.2.0;

/// WASI Monotonic Clock is a clock API intended to let users measure elapsed
/// time.
///
/// It is intended to be portable at least between Unix-family platforms and
/// Windows.
///
/// A monotonic clock is a clock which has an unspecified initial value, and
/// successive reads of the clock will produce non-decreasing values.
///
/// It is intended for measuring elapsed time.
interface monotonic-clock {
  use wasi:io/poll@0.2.0.{pollable};

  /// An instant in time, in nanoseconds. An instant is relative to an
  /// unspecified initial value, and can only be compared to instances from
  /// the same monotonic-clock.
  type instant = u64;

  /// A duration of time, in nanoseconds.
  type duration = u64;

  /// Read the current value of the clock.
  ///
  /// The clock is monotonic, therefore calling this function repeatedly will
  /// produce a sequence of non-decreasing values.
  now: func() -> instant;

  /// Query the resolution of the clock. Returns the duration of time
  /// corresponding to a clock tick.
  resolution: func() -> duration;

  /// Create a `pollable` which will resolve once the specified instant
  /// occured.
  subscribe-instant: func(when: instant) -> pollable;

  /// Create a `pollable` which will resolve once the given duration has
  /// elapsed, starting at the time at which this function was called.
  /// occured.
  subscribe-duration: func(when: duration) -> pollable;
}

/// WASI Wall Clock is a clock API intended to let users query the current
/// time. The name "wall" makes an analogy to a "clock on the wall", which
/// is not necessarily monotonic as it may be reset.
///
/// It is intended to be portable at least between Unix-family platforms and
/// Windows.
///
/// A wall clock is a clock which measures the date and time according to
/// some external reference.
///
/// External references may be reset, so this clock is not necessarily
/// monotonic, making it unsuitable for measuring elapsed time.
///
/// It is intended for reporting the current date and time for humans.
interface wall-clock {
  /// A time and date in seconds plus nanoseconds.
  record datetime {
    seconds: u64,
    nanoseconds: u32,
  }

  /// Read the current value of the clock.
  ///
  /// This clock is not monotonic, therefore calling this function repeatedly
  /// will not necessarily produce a sequence of non-decreasing values.
  ///
  /// The returned timestamps represent the number of seconds since
  /// 1970-01-01T00:00:00Z, also known as [POSIX's Seconds Since the Epoch],
  /// also known as [Unix Time].
  ///
  /// The nanoseconds field of the output is always less than 1000000000.
  ///
  /// [POSIX's Seconds Since the Epoch]: https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd_chap04.html#tag_21_04_16
  /// [Unix Time]: https://en.wikipedia.org/wiki/Unix_time
  now: func() -> datetime;

  /// Query the resolution of the clock.
  ///
  /// The nanoseconds field of the output is always less than 1000000000.
  resolution: func() -> datetime;
}

world imports {
  import wasi:io/poll@0.2.0;
  import monotonic-clock;
  import wall-clock;
}
