/// Time, as an effect (ADR 0036): timestamps, durations, UTC dates and /// RFC 3339. /// /// let t = time.now() // a Timestamp /// show(t) // "2026-10-13T09:30:00.250Z" /// time.parse("2026-10-13T11:30:00+02:00") // Ok(the same instant) /// /// `now` and `monotonic` read the `clock` effect. The runtime handles it /// around `main`, each test and each actor: `now_unix_ns` is the system's /// real-time clock; `monotonic_ns` is the scheduler's clock, which is /// virtual until the program opens a socket (ADR 0004, Revision 3), so a /// program without sockets measures what `process.sleep` waited, the same /// on every run. A test can fix the time instead: /// /// time.with_fixed_clock(time.from_unix_ns(0), fn() { time.now() }) /// /// A handler does not follow `process.spawn`: a new actor always gets the /// system's clock. UTC only: there are no time zones here. import std/bytes import std/string /// Clocks. pub effect clock { /// Nanoseconds since 1970-01-01T00:00:00Z (the system's clock: it can /// jump backwards when the system's time is set). fn now_unix_ns :: -> Int /// Nanoseconds since an arbitrary origin, never going backwards: for /// measuring durations. fn monotonic_ns :: -> Int } /// An instant: nanoseconds since 1970-01-01T00:00:00Z (before it when /// negative). Shown in RFC 3339. @derive Eq Ord Hash pub opaque type Timestamp { Timestamp(Int) } /// A span of time, in nanoseconds (negative when going back). @derive Eq Ord Hash pub opaque type Duration { Duration(Int) } /// The current time. pub fn now :: -> Timestamp / { Timestamp(now_unix_ns ()) } /// The time on the monotonic clock: compare two to measure a duration. pub fn monotonic :: -> Duration / { Duration(monotonic_ns ()) } /// The instant `ns` nanoseconds after the Unix epoch. pub fn from_unix_ns :: ns: Int -> Timestamp { Timestamp(ns) } /// The instant `ms` milliseconds after the Unix epoch. pub fn from_unix_ms :: ms: Int -> Timestamp { Timestamp(ms * 1000000) } /// Nanoseconds since the Unix epoch. pub fn unix_ns :: t: Timestamp -> Int { match t { Timestamp(ns) -> ns } } /// Milliseconds since the Unix epoch, rounded down. pub fn unix_ms :: t: Timestamp -> Int { floor_div (unix_ns t) 1000000 } pub fn nanoseconds :: n: Int -> Duration { Duration(n) } pub fn milliseconds :: n: Int -> Duration { Duration(n * 1000000) } pub fn seconds :: n: Int -> Duration { Duration(n * 1000000000) } pub fn minutes :: n: Int -> Duration { Duration(n * 60000000000) } /// The duration in nanoseconds. pub fn in_nanoseconds :: d: Duration -> Int { match d { Duration(ns) -> ns } } /// The duration in milliseconds, rounded down. pub fn in_milliseconds :: d: Duration -> Int { floor_div (in_nanoseconds d) 1000000 } /// `t` moved by `d`. pub fn add :: t: Timestamp d: Duration -> Timestamp { Timestamp(unix_ns t + in_nanoseconds d) } /// The time from `earlier` to `later` (negative if `later` is before). pub fn since :: later: Timestamp earlier: Timestamp -> Duration { Duration(unix_ns later - unix_ns earlier) } /// The time from monotonic reading `start` to `end`. pub fn elapsed :: end: Duration start: Duration -> Duration { Duration(in_nanoseconds end - in_nanoseconds start) } /// Run `action` with `now()` always `at`, and `monotonic()` always 0. pub fn with_fixed_clock :: at: Timestamp action: fn() -> a / -> a / { handle { action () } with { now_unix_ns -> unix_ns at, monotonic_ns -> 0 } } // ---- UTC calendar ------------------------------------------------------------------ /// A date and time in UTC. @derive Eq Debug pub type DateTime { DateTime( year: Int , month: Int , day: Int , hour: Int , minute: Int , second: Int , nanosecond: Int ) } /// `t`'s date and time in UTC. pub fn to_utc :: t: Timestamp -> DateTime { let ns = unix_ns t let days = floor_div ns 86400000000000 let in_day = ns - days * 86400000000000 let (year, month, day) = civil_from_days days let secs = in_day / 1000000000 DateTime( year , month , day , secs / 3600 , secs / 60 % 60 , secs % 60 , in_day % 1000000000 ) } /// The instant of a UTC date and time; `None` if a field is out of range /// (February 30, hour 24, second 60...). pub fn from_utc :: dt: DateTime -> Option { match dt { DateTime ( y , mo , d , h , mi , s , ns ) -> if mo < 1 || mo > 12 || d < 1 || d > days_in_month y mo || h < 0 || h > 23 || mi < 0 || mi > 59 || s < 0 || s > 59 || ns < 0 || ns > 999999999 { None } else { Some( Timestamp( ((days_from_civil y mo d * 24 + h) * 60 + mi) * 60000000000 + s * 1000000000 + ns ) ) } } } /// Days since 1970-01-01 of a proleptic Gregorian date (Howard Hinnant's /// `days_from_civil`). fn days_from_civil :: y0: Int m: Int d: Int -> Int { let y = if m <= 2 { y0 - 1 } else { y0 } let era = floor_div y 400 let yoe = y - era * 400 let mp = if m > 2 { m - 3 } else { m + 9 } let doy = (153 * mp + 2) / 5 + d - 1 let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy era * 146097 + doe - 719468 } /// The date of a day number since 1970-01-01 (Hinnant's `civil_from_days`). fn civil_from_days :: z0: Int -> (Int, Int, Int) { let z = z0 + 719468 let era = floor_div z 146097 let doe = z - era * 146097 let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100) let mp = (5 * doy + 2) / 153 let d = doy - (153 * mp + 2) / 5 + 1 let m = if mp < 10 { mp + 3 } else { mp - 9 } let y = yoe + era * 400 (if m <= 2 { y + 1 } else { y }, m, d) } fn is_leap :: y: Int -> Bool { y % 4 == 0 && (y % 100 != 0 || y % 400 == 0) } fn days_in_month :: y: Int m: Int -> Int { match m { 2 -> if is_leap y { 29 } else { 28 }, 4 | 6 | 9 | 11 -> 30, _ -> 31 } } /// `a / b` rounded towards minus infinity (`/` truncates), for `b > 0`. fn floor_div :: a: Int b: Int -> Int { let q = a / b if a % b < 0 { q - 1 } else { q } } // ---- RFC 3339 ---------------------------------------------------------------------- /// `t` in RFC 3339, in UTC: `2026-10-13T09:30:00Z`, with 3, 6 or 9 /// fraction digits when they aren't zero. Years outside 0–9999 use the /// expanded form (`+10000-01-01T00:00:00Z`). pub fn format :: t: Timestamp -> String { match to_utc t { DateTime ( y , mo , d , h , mi , s , ns ) -> "{year y}-{pad mo 2}-{pad d 2}T{pad h 2}:{pad mi 2}:{pad s 2}{fraction ns}Z" } } fn year :: y: Int -> String { if y >= 0 && y <= 9999 { pad y 4 } else if y < 0 { "-{pad (-y) 5}" } else { "+{pad y 5}" } } fn fraction :: ns: Int -> String { if ns == 0 { "" } else if ns % 1000000 == 0 { ".{pad (ns / 1000000) 3}" } else if ns % 1000 == 0 { ".{pad (ns / 1000) 6}" } else { ".{pad ns 9}" } } /// `n` (not negative) with zeros in front, to `width` digits at least. fn pad :: n: Int width: Int -> String { let digits = show n "{string.repeat "0" (width - string.length digits)}{digits}" } /// An RFC 3339 date and time, converted to UTC: `2026-10-13T11:30:00+02:00`. /// Accepts `T`, `t` or a space between date and time, 1 to 9 fraction /// digits (more are cut), and `Z`, `z` or `±HH:MM`. Years have 4 digits; /// a leap second (`:60`) is refused. pub fn parse :: text: String -> Result { let b = bytes.from_string text let fail = Error("not an RFC 3339 date and time: {text:?}") match ( digits b 0 4 , digits b 5 2 , digits b 8 2 , digits b 11 2 , digits b 14 2 , digits b 17 2 ) { ( Some(y) , Some(mo) , Some(d) , Some(h) , Some(mi) , Some(s) ) -> if !( at b 4 "-" && at b 7 "-" && (at b 10 "T" || at b 10 "t" || at b 10 " ") && at b 13 ":" && at b 16 ":" ) { fail } else { let (ns, i) = fraction_at b 19 match (offset_at b i, from_utc DateTime(y, mo, d, h, mi, s, ns)) { (Some(off), Some(t)) -> Ok(add t (minutes (-off))), _ -> fail } } , _ -> fail } } /// The `n`-digit number at byte `i`, if there is one. fn digits :: b: Bytes i: Int n: Int -> Option { digits_from b i n 0 } fn digits_from :: b: Bytes i: Int n: Int acc: Int -> Option { if n == 0 { Some(acc) } else { match bytes.get b i { Some(c) if c >= 48 && c <= 57 -> digits_from b (i + 1) (n - 1) (acc * 10 + c - 48) , _ -> None } } } /// Whether byte `i` is `c` (one ASCII character). fn at :: b: Bytes i: Int c: String -> Bool { bytes.get b i == bytes.get (bytes.from_string c) 0 } /// A fraction `.ddd` at byte `i`: nanoseconds, and where it ends. fn fraction_at :: b: Bytes i: Int -> (Int, Int) { if at b i "." { fraction_digits b (i + 1) 0 0 } else { (0, i) } } fn fraction_digits :: b: Bytes i: Int n: Int acc: Int -> (Int, Int) { match bytes.get b i { Some(c) if c >= 48 && c <= 57 -> if n < 9 { fraction_digits b (i + 1) (n + 1) (acc * 10 + c - 48) } else { fraction_digits b (i + 1) n acc } , _ -> (acc * power10 (9 - n), if n == 0 { i - 1 } else { i }) } } fn power10 :: n: Int -> Int { if n == 0 { 1 } else { 10 * power10 (n - 1) } } /// The offset at byte `i`, in minutes east of UTC, if the text ends with /// it. fn offset_at :: b: Bytes i: Int -> Option { if (at b i "Z" || at b i "z") && bytes.length b == i + 1 { Some(0) } else if (at b i "+" || at b i "-") && at b (i + 3) ":" && bytes.length b == i + 6 { match (digits b (i + 1) 2, digits b (i + 4) 2) { (Some(h), Some(m)) if h <= 23 && m <= 59 -> Some(if at b i "-" { -(h * 60 + m) } else { h * 60 + m }) , _ -> None } } else { None } } impl Show { fn show :: t { format t } } impl Debug { fn debug :: t { "time.Timestamp({format t})" } } /// In the largest unit that keeps it exact: `2s`, `1500ms`, `3us`, /// `7ns`. impl Show { fn show :: d { let ns = in_nanoseconds d if ns % 1000000000 == 0 { "{ns / 1000000000}s" } else if ns % 1000000 == 0 { "{ns / 1000000}ms" } else if ns % 1000 == 0 { "{ns / 1000}us" } else { "{ns}ns" } } } impl Debug { fn debug :: d { "time.Duration({show d})" } }