| The data model, in Nim d333b6f nandi 18h ago | 1 | ## The date arithmetic, against the cases that are easy to get wrong. |
| 2 | |
| 3 | import std/unittest |
| 4 | import frq/clock |
| 5 | |
| 6 | suite "the civil-date round trip": |
| 7 | test "the epoch": |
| 8 | check daysFromCivil(1970, 1, 1) == 0 |
| 9 | check civilFromDays(0) == (1970'i64, 1'i64, 1'i64) |
| 10 | |
| 11 | test "round-trips every day from 1901 to 2052": |
| 12 | # The same range the Clojure was checked against java.time.LocalDate over. |
| 13 | var d = daysFromCivil(1901, 1, 1) |
| 14 | let last = daysFromCivil(2052, 12, 31) |
| 15 | while d <= last: |
| 16 | let (y, m, dd) = civilFromDays(d) |
| 17 | check daysFromCivil(y, m, dd) == d |
| 18 | d += 1 |
| 19 | |
| 20 | test "leap days exist and non-leap ones do not": |
| 21 | check civilFromDays(daysFromCivil(2024, 2, 29)) == (2024'i64, 2'i64, 29'i64) |
| 22 | # 2100 is not a leap year; Feb 29 there rolls into March. |
| 23 | check civilFromDays(daysFromCivil(2100, 2, 29)) == (2100'i64, 3'i64, 1'i64) |
| 24 | |
| 25 | test "before the epoch, where rounding toward zero would be a day out": |
| 26 | check civilFromDays(daysFromCivil(1969, 12, 31)) == (1969'i64, 12'i64, 31'i64) |
| 27 | check daysFromCivil(1969, 12, 31) == -1 |
| 28 | |
| 29 | suite "floorDiv": |
| 30 | test "rounds down rather than toward zero": |
| 31 | check floorDiv(-1, 86400) == -1 |
| 32 | check floorDiv(7, 2) == 3 |
| 33 | check floorDiv(-7, 2) == -4 |
| 34 | test "floorMod is never negative for a positive divisor": |
| 35 | check floorMod(-1, 86400) == 86399 |
| 36 | |
| 37 | suite "parseTimeTag": |
| 38 | # The expected values are python's `datetime(...).timestamp()`, not arithmetic |
| 39 | # done by hand — the first draft of this test was twelve days out and the |
| 40 | # implementation was right. |
| 41 | test "a real tag": |
| 42 | let (ms, ok) = parseTimeTag("time=2026-08-30T07:05:09.000Z") |
| 43 | check ok |
| 44 | check ms == 1788073509000'i64 |
| 45 | |
| 46 | test "finds it among other tags": |
| 47 | let (ms, ok) = parseTimeTag("account=alice;time=2026-08-30T07:05:09.000Z;msgid=x") |
| 48 | check ok |
| 49 | check ms == 1788073509000'i64 |
| 50 | |
| 51 | test "no tag at all": |
| 52 | check not parseTimeTag("account=alice")[1] |
| 53 | check not parseTimeTag("")[1] |
| 54 | |
| 55 | test "a malformed value is refused rather than guessed at": |
| 56 | check not parseTimeTag("time=not-a-time")[1] |
| 57 | check not parseTimeTag("time=2026-08-30")[1] |