blob: f2eb63863f870f7a6a0d31bd6ed1e0e49623813e (
plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
(.module:
lux
(lux (control eq
order
codec
["p" parser]
[monad #+ do])
(data [number "int/" Codec<Text,Int> Number<Int>]
[text "text/" Monoid<Text>]
(text ["l" lexer])
["e" error])
(type abstract)))
(abstract: #export Duration
{#.doc "Durations have a resolution of milliseconds."}
Int
(def: #export from-millis
(-> Int Duration)
(|>> @abstract))
(def: #export to-millis
(-> Duration Int)
(|>> @repr))
(do-template [<name> <op>]
[(def: #export (<name> param subject)
(-> Duration Duration Duration)
(@abstract (<op> (@repr param) (@repr subject))))]
[merge i/+]
[frame i/%]
)
(def: #export (scale scalar duration)
(-> Int Duration Duration)
(@abstract (i/* scalar (@repr duration))))
(def: #export (query param subject)
(-> Duration Duration Int)
(i// (@repr param) (@repr subject)))
(struct: #export _ (Eq Duration)
(def: (= param subject)
(i/= (@repr param) (@repr subject))))
(struct: #export _ (Order Duration)
(def: eq Eq<Duration>)
(do-template [<name> <op>]
[(def: (<name> param subject)
(<op> (@repr param) (@repr subject)))]
[< i/<]
[<= i/<=]
[> i/>]
[>= i/>=]
))
(do-template [<name> <op>]
[(def: #export (<name> duration)
(-> Duration Bool)
(<op> 0 (@repr duration)))]
[positive? i/>]
[negative? i/<]
[neutral? i/=])
)
(def: #export empty Duration (from-millis 0))
(def: #export milli Duration (from-millis 1))
(def: #export second Duration (scale 1_000 milli))
(def: #export minute Duration (scale 60 second))
(def: #export hour Duration (scale 60 minute))
(def: #export day Duration (scale 24 hour))
(def: #export week Duration (scale 7 day))
(def: #export normal-year Duration (scale 365 day))
(def: #export leap-year Duration (merge day normal-year))
(def: (encode duration)
(-> Duration Text)
(if (:: Eq<Duration> = empty duration)
"0ms"
(let [signed? (negative? duration)
[days time-left] [(query day duration) (frame day duration)]
days (if signed?
(int/abs days)
days)
time-left (if signed?
(scale -1 time-left)
time-left)
[hours time-left] [(query hour time-left) (frame hour time-left)]
[minutes time-left] [(query minute time-left) (frame minute time-left)]
[seconds time-left] [(query second time-left) (frame second time-left)]
millis (to-millis time-left)]
($_ text/compose
(if signed? "-" "")
(if (i/= 0 days) "" (text/compose (int/encode days) "D"))
(if (i/= 0 hours) "" (text/compose (int/encode hours) "h"))
(if (i/= 0 minutes) "" (text/compose (int/encode minutes) "m"))
(if (i/= 0 seconds) "" (text/compose (int/encode seconds) "s"))
(if (i/= 0 millis) "" (text/compose (int/encode millis) "ms"))
))))
(def: (lex-section suffix)
(-> Text (l.Lexer Int))
(|> (p.codec number.Codec<Text,Int> (l.many l.decimal))
(p.before (p.seq (l.this suffix) (p.not l.alpha)))
(p.default 0)))
(def: lex-duration
(l.Lexer Duration)
(do p.Monad<Parser>
[signed? (l.this? "-")
#let [sign (function [raw] (if signed? (i/* -1 raw) raw))]
utc-day (lex-section "D")
utc-hour (lex-section "h")
utc-minute (lex-section "m")
_ (p.assert "Invalid minute."
(and (i/>= 0 utc-minute)
(i/<= 59 utc-minute)))
utc-second (lex-section "s")
_ (p.assert "Invalid second."
(and (i/>= 0 utc-second)
(i/<= 59 utc-second)))
utc-millis (lex-section "ms")
_ (p.assert "Invalid milli-seconds."
(and (i/>= 0 utc-millis)
(i/<= 999 utc-millis)))]
(wrap (|> empty
(merge (scale (sign utc-day) day))
(merge (scale (sign utc-hour) hour))
(merge (scale (sign utc-minute) minute))
(merge (scale (sign utc-second) second))
(merge (scale (sign utc-millis) milli))))))
(def: (decode input)
(-> Text (e.Error Duration))
(l.run input lex-duration))
(struct: #export _
{#.doc "For example: 15D21h14m51s827ms"}
(Codec Text Duration)
(def: encode encode)
(def: decode decode))
|