nandi/frqpublic Fork 0
c69a09b441eccce09f7ae881ec88b7e0dcbe87fa
Commits
Clone
git clone https://git.rickub.com/nandi/frq.git
git clone ssh://git@rickub.com/nandi/frq.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

The data model, in Nim d333b6f · on c69a09b441eccce09f7ae881ec88b7e0dcbe87fa · nandi · 12h ago
tclock.nim · 57 lines · 2.1 KBNim Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
## The date arithmetic, against the cases that are easy to get wrong.

import std/unittest
import frq/clock

suite "the civil-date round trip":
  test "the epoch":
    check daysFromCivil(1970, 1, 1) == 0
    check civilFromDays(0) == (1970'i64, 1'i64, 1'i64)

  test "round-trips every day from 1901 to 2052":
    # The same range the Clojure was checked against java.time.LocalDate over.
    var d = daysFromCivil(1901, 1, 1)
    let last = daysFromCivil(2052, 12, 31)
    while d <= last:
      let (y, m, dd) = civilFromDays(d)
      check daysFromCivil(y, m, dd) == d
      d += 1

  test "leap days exist and non-leap ones do not":
    check civilFromDays(daysFromCivil(2024, 2, 29)) == (2024'i64, 2'i64, 29'i64)
    # 2100 is not a leap year; Feb 29 there rolls into March.
    check civilFromDays(daysFromCivil(2100, 2, 29)) == (2100'i64, 3'i64, 1'i64)

  test "before the epoch, where rounding toward zero would be a day out":
    check civilFromDays(daysFromCivil(1969, 12, 31)) == (1969'i64, 12'i64, 31'i64)
    check daysFromCivil(1969, 12, 31) == -1

suite "floorDiv":
  test "rounds down rather than toward zero":
    check floorDiv(-1, 86400) == -1
    check floorDiv(7, 2) == 3
    check floorDiv(-7, 2) == -4
  test "floorMod is never negative for a positive divisor":
    check floorMod(-1, 86400) == 86399

suite "parseTimeTag":
  # The expected values are python's `datetime(...).timestamp()`, not arithmetic
  # done by hand — the first draft of this test was twelve days out and the
  # implementation was right.
  test "a real tag":
    let (ms, ok) = parseTimeTag("time=2026-08-30T07:05:09.000Z")
    check ok
    check ms == 1788073509000'i64

  test "finds it among other tags":
    let (ms, ok) = parseTimeTag("account=alice;time=2026-08-30T07:05:09.000Z;msgid=x")
    check ok
    check ms == 1788073509000'i64

  test "no tag at all":
    check not parseTimeTag("account=alice")[1]
    check not parseTimeTag("")[1]

  test "a malformed value is refused rather than guessed at":
    check not parseTimeTag("time=not-a-time")[1]
    check not parseTimeTag("time=2026-08-30")[1]