Module
pooka/time
src/pooka/time.pookaKeep the calendar clock and the elapsed-time clock on different shelves.
Timestamp measures wall-clock milliseconds; Instant is for monotonic elapsed time. Duration stores integer milliseconds. Negative sleep durations are rejected, and timestamp arithmetic returns new values.
import pooka/time
assert time.seconds(2).as_milliseconds() == 2000
API reference
enum TimeError
TimeError.NegativeDuration
TimeError.DurationTooLarge
class Duration(milliseconds: Int)
An integer millisecond interval; elapsed and wall-clock APIs share this unit without sharing their clocks.
milliseconds: Int
Int
fn(Int) -> pooka/time.Durationas_seconds() -> Float
fn() -> FloatConvert stored milliseconds to floating-point seconds.
import pooka/time
assert time.milliseconds(1500).as_seconds() == 1.5
as_milliseconds() -> Int
fn() -> IntRead the exact stored integer milliseconds.
import pooka/time
assert time.seconds(2).as_milliseconds() == 2000
class Timestamp(milliseconds: Int)
A wall-clock point measured in milliseconds. Arithmetic returns another timestamp.
milliseconds: Int
Int
fn(Int) -> pooka/time.Timestampadd(duration: Duration) -> Timestamp
fn(pooka/time.Duration) -> pooka/time.TimestampReturn a timestamp moved forward by the duration.
import pooka/time
assert time.Timestamp(1000).add(time.milliseconds(25)).milliseconds == 1025
subtract(duration: Duration) -> Timestamp
fn(pooka/time.Duration) -> pooka/time.TimestampReturn a timestamp moved backward by the duration.
import pooka/time
assert time.Timestamp(1000).subtract(time.milliseconds(25)).milliseconds == 975
class Instant
A monotonic clock reading for elapsed measurements. Construct readings with monotonic_now.
new(ticks: Int)
fn(Int) -> pooka/time.InstantInitialize the value from the supplied state. Later updates return new values.
duration_until(end: Instant) -> Duration
fn(pooka/time.Instant) -> pooka/time.DurationMeasure from this instant to end. Keep the start and end in their original order.
import pooka/time
let start = time.monotonic_now()
let elapsed = start.duration_until(time.monotonic_now())
now() -> Timestamp
fn() -> pooka/time.TimestampRead the wall clock as a millisecond timestamp. Use Instant for elapsed measurements.
import pooka/time
let timestamp = time.now()
monotonic_now() -> Instant
fn() -> pooka/time.InstantRead a monotonic instant for measuring elapsed time.
import pooka/time
let started = time.monotonic_now()
duration_between(start: Instant, end: Instant) -> Duration
fn(pooka/time.Instant, pooka/time.Instant) -> pooka/time.DurationSubtract two monotonic instants in start-to-end order.
import pooka/time
let started = time.monotonic_now()
let elapsed = time.duration_between(started, time.monotonic_now())
seconds(value: Int) -> Duration
fn(Int) -> pooka/time.DurationConstruct a duration by converting whole seconds to milliseconds.
import pooka/time
assert time.seconds(2).as_milliseconds() == 2000
milliseconds(value: Int) -> Duration
fn(Int) -> pooka/time.DurationConstruct a duration from integer milliseconds.
import pooka/time
assert time.milliseconds(25).as_milliseconds() == 25
sleep(duration: Duration) -> Result(Nil, TimeError)
fn(pooka/time.Duration) -> Result(Nil, pooka/time.TimeError)Sleep for a nonnegative duration, returning typed errors for unsupported durations.
import pooka/time
assert time.sleep(time.milliseconds(0)) == Ok(Nil)