// std/time (ADR 0036): timestamps, durations, the UTC calendar, RFC 3339, // and the `clock` effect's handlers. import std/io import std/process.{type proc} import std/time.{type DateTime, type clock, DateTime} fn rfc :: ns: Int -> String { time.format (time.from_unix_ns ns) } fn parsed :: text: String -> String { match time.parse text { Ok(t) -> "{time.unix_ns t}", Error(_) -> "refused" } } pub fn main :: -> () / { // Formatting: the epoch, before it, fractions, leap days, far years. io.println (rfc 0) io.println (rfc (-1)) io.println (rfc 1500000000) io.println (rfc 951782400123456000) io.println (rfc (253402300799 * 1000000000 + 999999999)) io.println (rfc (253402300800 * 1000000000)) io.println (rfc (-62135596800 * 1000000000)) io.println (rfc (-62167219200 * 1000000000 - 1)) io.println "{time.from_unix_ms 1760000000000}" // The calendar. io.println "{time.to_utc (time.from_unix_ns (-1)):?}" io.println "{time.from_utc DateTime(year: 2000, month: 2, day: 29, hour: 0, minute: 0, second: 0, nanosecond: 0) |> option_ns}" io.println "{time.from_utc DateTime(year: 1900, month: 2, day: 29, hour: 0, minute: 0, second: 0, nanosecond: 0) |> option_ns}" io.println "{time.from_utc DateTime(year: 2026, month: 4, day: 31, hour: 0, minute: 0, second: 0, nanosecond: 0) |> option_ns}" io.println "{time.from_utc DateTime(year: 2026, month: 1, day: 1, hour: 24, minute: 0, second: 0, nanosecond: 0) |> option_ns}" // Parsing: offsets, separators, fractions; and what is refused. io.println (parsed "1970-01-01T00:00:00Z") io.println (parsed "2026-10-13T11:30:00+02:00") io.println (parsed "2026-10-13t09:30:00z") io.println (parsed "2026-10-13 09:30:00.25-00:30") io.println (parsed "2026-10-13T09:30:00.1234567891234Z") io.println (parsed "1969-12-31T23:59:59.999999999Z") io.println (parsed "2026-10-13T09:30:60Z") io.println (parsed "2026-02-30T09:30:00Z") io.println (parsed "2026-10-13T09:30:00") io.println (parsed "2026-10-13T09:30:00.Z") io.println (parsed "2026-10-13T09:30:00+24:00") io.println (parsed "2026-10-13T09:30:00Z ") io.println (parsed "26-10-13T09:30:00Z") io.println "{time.parse (rfc 951782400123456789) == Ok(time.from_unix_ns 951782400123456789)}" // Durations. io.println "{time.seconds 2} {time.milliseconds 1500} {time.nanoseconds 3000} {time.nanoseconds 7} {time.minutes (-1)}" io.println "{time.since (time.from_unix_ms 5000) (time.from_unix_ms 2000)}" io.println "{time.add (time.from_unix_ns 0) (time.minutes 90)}" io.println "{time.in_milliseconds (time.nanoseconds (-1))} {time.seconds 1 < time.seconds 2}" // A fixed clock, and the runtime's (virtual) monotonic clock. io.println "{time.with_fixed_clock (time.from_unix_ms 86400000) fn { time.now () }}" let start = time.monotonic () process.sleep 5 io.println "{time.elapsed (time.monotonic ()) start}" io.println "{time.unix_ms (time.now ()) > 1760000000000}" } fn option_ns :: t: Option -> String { match t { Some(t) -> "{time.unix_ns t}", None -> "None" } } //> 1970-01-01T00:00:00Z //> 1969-12-31T23:59:59.999999999Z //> 1970-01-01T00:00:01.500Z //> 2000-02-29T00:00:00.123456Z //> 9999-12-31T23:59:59.999999999Z //> +10000-01-01T00:00:00Z //> 0001-01-01T00:00:00Z //> -00001-12-31T23:59:59.999999999Z //> 2025-10-09T08:53:20Z //> DateTime { year: 1969, month: 12, day: 31, hour: 23, minute: 59, second: 59, nanosecond: 999999999 } //> 951782400000000000 //> None //> None //> None //> 0 //> 1791883800000000000 //> 1791883800000000000 //> 1791885600250000000 //> 1791883800123456789 //> -1 //> refused //> refused //> refused //> refused //> refused //> refused //> refused //> True //> 2s 1500ms 3us 7ns -60s //> 3s //> 1970-01-01T01:30:00Z //> -1 True //> 1970-01-02T00:00:00Z //> 5ms //> True