-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.echo
More file actions
84 lines (71 loc) · 1.71 KB
/
Copy pathtime.echo
File metadata and controls
84 lines (71 loc) · 1.71 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
; std/time — wall clock and sleep.
; Privileged std may `/ runtime` and call `runtime.*` (docs/stdlib.md).
;
; Public API:
; now_ms — milliseconds since Unix epoch (UTC), i64
; sleep_ms — sleep at least `ms` milliseconds (no-op if ms ≤ 0)
;
; Suite: xo test std/time.echo (XO_TEST).
/ runtime
/ std/test
/ std/str
$ now_ms = () {
^ runtime.now_ms()
}
$ sleep_ms = (ms) {
runtime.sleep_ms(ms)
^
}
; Monotonic milliseconds since process start (not wall clock).
$ mono_ms = () {
^ runtime.now_mono_ms()
}
; Format wall-ms with a strftime-like pattern (chrono; e.g. "%Y-%m-%d").
$ format = (ms, fmt) {
^ runtime.time_format(ms, fmt)
}
; Parse text with pattern → result ms since epoch, or ! "parse failed".
$ parse = (s, fmt) {
$ r = runtime.time_parse(s, fmt)
? r.ok == 0 {
! "parse failed"
}
^ r.val
}
; --- suite ---
test.it("now_ms_nonneg", () {
$ t = now_ms()
test.true(t >= 0)
; Epoch is far in the past; after 2020-01-01 ≈ 1.5e12 ms.
test.true(t > 1500000000000)
})
test.it("sleep_ms_zero", () {
sleep_ms(0)
sleep_ms(-1)
test.eq(1, 1)
})
test.it("sleep_ms_advances", () {
$ a = now_ms()
sleep_ms(30)
$ b = now_ms()
; Allow small clock noise; should not go backwards by much and usually advances.
test.true(b >= a)
test.true(b - a >= 20)
})
test.it("mono_ms_nonneg", () {
test.true(mono_ms() >= 0)
})
test.it("format_parse_day", () {
$ ms = 1700000000000
$ s = format(ms, "%Y-%m-%d")
test.true(str.starts_with(s, "2023"))
| parse(s, "%Y-%m-%d") {
$ t {
test.true(t > 0)
}
! e {
test.fail(e)
}
}
})
\ now_ms, sleep_ms, mono_ms, format, parse