louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // std/time (ADR 0036): timestamps, durations, the UTC calendar, RFC 3339, |
| 2 | // and the `clock` effect's handlers. |
| 3 | |
| 4 | import std/io |
| 5 | import std/process.{type proc} |
| 6 | import std/time.{type DateTime, type clock, DateTime} |
| 7 | |
| 8 | fn rfc :: ns: Int -> String |
| 9 | { |
| 10 | time.format (time.from_unix_ns ns) |
| 11 | } |
| 12 | |
| 13 | fn parsed :: text: String -> String |
| 14 | { |
| 15 | match time.parse text { Ok(t) -> "{time.unix_ns t}", Error(_) -> "refused" } |
| 16 | } |
| 17 | |
| 18 | pub fn main :: -> () / <io, proc, clock> |
| 19 | { |
| 20 | // Formatting: the epoch, before it, fractions, leap days, far years. |
| 21 | io.println (rfc 0) |
| 22 | io.println (rfc (-1)) |
| 23 | io.println (rfc 1500000000) |
| 24 | io.println (rfc 951782400123456000) |
| 25 | io.println (rfc (253402300799 * 1000000000 + 999999999)) |
| 26 | io.println (rfc (253402300800 * 1000000000)) |
| 27 | io.println (rfc (-62135596800 * 1000000000)) |
| 28 | io.println (rfc (-62167219200 * 1000000000 - 1)) |
| 29 | io.println "{time.from_unix_ms 1760000000000}" |
| 30 | |
| 31 | // The calendar. |
| 32 | io.println "{time.to_utc (time.from_unix_ns (-1)):?}" |
| 33 | io.println |
| 34 | "{time.from_utc DateTime(year: 2000, month: 2, day: 29, hour: 0, minute: 0, second: 0, nanosecond: 0) |> option_ns}" |
| 35 | io.println |
| 36 | "{time.from_utc DateTime(year: 1900, month: 2, day: 29, hour: 0, minute: 0, second: 0, nanosecond: 0) |> option_ns}" |
| 37 | io.println |
| 38 | "{time.from_utc DateTime(year: 2026, month: 4, day: 31, hour: 0, minute: 0, second: 0, nanosecond: 0) |> option_ns}" |
| 39 | io.println |
| 40 | "{time.from_utc DateTime(year: 2026, month: 1, day: 1, hour: 24, minute: 0, second: 0, nanosecond: 0) |> option_ns}" |
| 41 | |
| 42 | // Parsing: offsets, separators, fractions; and what is refused. |
| 43 | io.println (parsed "1970-01-01T00:00:00Z") |
| 44 | io.println (parsed "2026-10-13T11:30:00+02:00") |
| 45 | io.println (parsed "2026-10-13t09:30:00z") |
| 46 | io.println (parsed "2026-10-13 09:30:00.25-00:30") |
| 47 | io.println (parsed "2026-10-13T09:30:00.1234567891234Z") |
| 48 | io.println (parsed "1969-12-31T23:59:59.999999999Z") |
| 49 | io.println (parsed "2026-10-13T09:30:60Z") |
| 50 | io.println (parsed "2026-02-30T09:30:00Z") |
| 51 | io.println (parsed "2026-10-13T09:30:00") |
| 52 | io.println (parsed "2026-10-13T09:30:00.Z") |
| 53 | io.println (parsed "2026-10-13T09:30:00+24:00") |
| 54 | io.println (parsed "2026-10-13T09:30:00Z ") |
| 55 | io.println (parsed "26-10-13T09:30:00Z") |
| 56 | io.println |
| 57 | "{time.parse (rfc 951782400123456789) == Ok(time.from_unix_ns 951782400123456789)}" |
| 58 | |
| 59 | // Durations. |
| 60 | io.println |
| 61 | "{time.seconds 2} {time.milliseconds 1500} {time.nanoseconds 3000} {time.nanoseconds 7} {time.minutes (-1)}" |
| 62 | io.println "{time.since (time.from_unix_ms 5000) (time.from_unix_ms 2000)}" |
| 63 | io.println "{time.add (time.from_unix_ns 0) (time.minutes 90)}" |
| 64 | io.println |
| 65 | "{time.in_milliseconds (time.nanoseconds (-1))} {time.seconds 1 < time.seconds 2}" |
| 66 | |
| 67 | // A fixed clock, and the runtime's (virtual) monotonic clock. |
| 68 | io.println |
| 69 | "{time.with_fixed_clock (time.from_unix_ms 86400000) fn { time.now () }}" |
| 70 | let start = time.monotonic () |
| 71 | process.sleep 5 |
| 72 | io.println "{time.elapsed (time.monotonic ()) start}" |
| 73 | io.println "{time.unix_ms (time.now ()) > 1760000000000}" |
| 74 | } |
| 75 | |
| 76 | fn option_ns :: t: Option<time.Timestamp> -> String |
| 77 | { |
| 78 | match t { Some(t) -> "{time.unix_ns t}", None -> "None" } |
| 79 | } |
| 80 | //> 1970-01-01T00:00:00Z |
| 81 | //> 1969-12-31T23:59:59.999999999Z |
| 82 | //> 1970-01-01T00:00:01.500Z |
| 83 | //> 2000-02-29T00:00:00.123456Z |
| 84 | //> 9999-12-31T23:59:59.999999999Z |
| 85 | //> +10000-01-01T00:00:00Z |
| 86 | //> 0001-01-01T00:00:00Z |
| 87 | //> -00001-12-31T23:59:59.999999999Z |
| 88 | //> 2025-10-09T08:53:20Z |
| 89 | //> DateTime { year: 1969, month: 12, day: 31, hour: 23, minute: 59, second: 59, nanosecond: 999999999 } |
| 90 | //> 951782400000000000 |
| 91 | //> None |
| 92 | //> None |
| 93 | //> None |
| 94 | //> 0 |
| 95 | //> 1791883800000000000 |
| 96 | //> 1791883800000000000 |
| 97 | //> 1791885600250000000 |
| 98 | //> 1791883800123456789 |
| 99 | //> -1 |
| 100 | //> refused |
| 101 | //> refused |
| 102 | //> refused |
| 103 | //> refused |
| 104 | //> refused |
| 105 | //> refused |
| 106 | //> refused |
| 107 | //> True |
| 108 | //> 2s 1500ms 3us 7ns -60s |
| 109 | //> 3s |
| 110 | //> 1970-01-01T01:30:00Z |
| 111 | //> -1 True |
| 112 | //> 1970-01-02T00:00:00Z |
| 113 | //> 5ms |
| 114 | //> True |