Module: Jade::Stdlib::Clock

Extended by:
Clock, Compiled
Included in:
Clock
Defined in:
lib/jade/stdlib/clock.rb

Instance Method Summary collapse

Methods included from Compiled

entry, generate_entry, resolve_imports, symbols

Instance Method Details

#codeObject



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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/jade/stdlib/clock.rb', line 21

def code
  <<~JADE
    module Clock exposing (
      Duration,
      Instant,
      add,
      at_time,
      days,
      diff,
      epoch,
      from_iso,
      hours,
      in_days,
      in_hours,
      in_millis,
      in_minutes,
      in_seconds,
      millis,
      minutes,
      now,
      on_date,
      parts,
      seconds,
      to_iso,
    )

    import Decode exposing (Decodable, Decoder, Value)
    import Encode exposing (Encodable)


    type Instant = Instant(Int)


    type Duration = Duration(Int)


    uses Jade::Clock::Runtime with
      now_raw : Task({ millis: Int }, Never)
    end


    def now -> Task(Instant, Never)
      raw <- now_raw()
      Task.succeed(Instant(raw.millis))
    end


    def epoch -> Instant
      Instant(0)
    end


    def millis(n: Int) -> Duration
      Duration(n)
    end


    def in_millis(d: Duration) -> Int
      Duration(n) = d
      n
    end


    def seconds(n: Int) -> Duration
      Duration(n * 1000)
    end


    def in_seconds(d: Duration) -> Int
      in_millis(d) / 1000
    end


    def minutes(n: Int) -> Duration
      Duration(n * 60000)
    end


    def in_minutes(d: Duration) -> Int
      in_millis(d) / 60000
    end


    def hours(n: Int) -> Duration
      Duration(n * 3600000)
    end


    def in_hours(d: Duration) -> Int
      in_millis(d) / 3600000
    end


    def days(n: Int) -> Duration
      Duration(n * 86400000)
    end


    def in_days(d: Duration) -> Int
      in_millis(d) / 86400000
    end


    def parts(d: Duration) -> {
      days: Int,
      hours: Int,
      minutes: Int,
      seconds: Int,
      millis: Int,
    }
      ms = in_millis(d)
      {
        days: ms / 86400000,
        hours: mod(ms, 86400000) / 3600000,
        minutes: mod(ms, 3600000) / 60000,
        seconds: mod(ms, 60000) / 1000,
        millis: mod(ms, 1000),
      }
    end


    def add(i: Instant, d: Duration) -> Instant
      Instant(ms) = i
      Instant(ms + in_millis(d))
    end


    def diff(a: Instant, b: Instant) -> Duration
      Instant(ams) = a
      Instant(bms) = b
      Duration(bms - ams)
    end


    def on_date(i: Instant) -> Calendar.Date
      Instant(ms) = i
      Calendar.from_rata_die(floor_div(ms, 86400000) + 719163)
    end


    def at_time(i: Instant) -> {
      hour: Int,
      minute: Int,
      second: Int,
      millisecond: Int,
    }
      Instant(ms) = i
      day_ms = mod(ms, 86400000)
      {
        hour: day_ms / 3600000,
        minute: mod(day_ms, 3600000) / 60000,
        second: mod(day_ms, 60000) / 1000,
        millisecond: mod(day_ms, 1000),
      }
    end


    def floor_div(a: Int, b: Int) -> Int
      q = a / b
      a < 0 && mod(a, b) != 0 ? q - 1 : q
    end


    def to_iso(i: Instant) -> String
      d = on_date(i)
      tod = at_time(i)
      Calendar.to_iso_string(d)
        ++ "T"
        ++ pad2(tod.hour)
        ++ ":"
        ++ pad2(tod.minute)
        ++ ":"
        ++ pad2(tod.second)
        ++ "Z"
    end


    def pad2(n: Int) -> String
      s = String.from_int(n)
      String.length(s) < 2 ? "0" ++ s : s
    end


    def from_iso(s: String) -> Result(Instant, String)
      case split_dt(s)
      in Just((date_part, time_part))
        case (Calendar.from_iso_string(date_part), parse_time(time_part))
        in (Ok(d), Ok((h, m, sec, sub))) then Ok(combine(d, h, m, sec, sub))
        else Err("invalid timestamp: " ++ s)
        end
      in Nothing then Err("invalid timestamp: " ++ s)
      end
    end


    def split_dt(s: String) -> Maybe((String, String))
      case String.split(s, "T")
      in [d, t] then Just((d, t))
      else
        case String.split(s, " ")
        in [d, t] then Just((d, t))
        else Nothing
        end
      end
    end


    def parse_time(s: String) -> Result((Int, Int, Int, Int), String)
      case String.split(strip_z(s), ":")
      in [h, m, sec_part]
        case (String.to_int(h), String.to_int(m), parse_seconds(sec_part))
        in (Just(hi), Just(mi), Just((si, ms))) then Ok((hi, mi, si, ms))
        else Err("bad time: " ++ s)
        end
      else Err("bad time: " ++ s)
      end
    end


    def strip_z(s: String) -> String
      case String.split(s, "Z")
      in [head, _] then head
      in [head] then head
      else s
      end
    end


    def parse_seconds(s: String) -> Maybe((Int, Int))
      case String.split(s, ".")
      in [secs]
        case String.to_int(secs)
        in Just(si) then Just((si, 0))
        in Nothing then Nothing
        end
      in [secs, frac]
        case (String.to_int(secs), String.to_int(frac))
        in (Just(si), Just(fi))
          Just((si, frac_to_millis(fi, String.length(frac))))
        else Nothing
        end
      else Nothing
      end
    end


    def frac_to_millis(n: Int, digits: Int) -> Int
      digits == 3 ? n : digits < 3 ? n * pow10(3 - digits) : n / pow10(digits - 3)
    end


    def pow10(n: Int) -> Int
      n <= 0 ? 1 : 10 * pow10(n - 1)
    end


    def combine(
      d: Calendar.Date,
      hour: Int,
      minute: Int,
      second: Int,
      sub_ms: Int,
    ) -> Instant
      day_count = Calendar.to_rata_die(d) - 719163
      Instant(
        day_count * 86400000 + hour * 3600000 + minute * 60000 + second * 1000 + sub_ms,
      )
    end


    def compare_instant(a: Instant, b: Instant) -> Ordering
      Instant(ams) = a
      Instant(bms) = b
      compare(ams, bms)
    end


    def instant_eq(a: Instant, b: Instant) -> Bool
      Instant(ams) = a
      Instant(bms) = b
      ams == bms
    end


    implements Comparable(Instant) with
      compare: compare_instant
    end


    implements Eq(Instant) with
      (==): instant_eq
    end


    def parse_instant(s: String) -> Decoder(Instant)
      Decode.from_result(from_iso(s))
    end


    implements Decodable(Instant) with
      decoder: -> { Decode.string |> Decode.and_then(parse_instant) }
    end


    implements Encodable(Instant) with
      encoder: (i) -> { Encode.string(to_iso(i)) }
    end


    implements Decodable(Duration) with
      decoder: -> { Decode.map(Decode.int, Duration) }
    end


    implements Encodable(Duration) with
      encoder: (d) -> { Encode.int(in_millis(d)) }
    end
    JADE
end

#default_importsObject



17
18
19
# File 'lib/jade/stdlib/clock.rb', line 17

def default_imports
  []
end

#importsObject



13
14
15
# File 'lib/jade/stdlib/clock.rb', line 13

def imports
  [Basics, Maybe, Result, Task, String, Tuple, Decode, Encode, Calendar]
end

#uriObject



9
10
11
# File 'lib/jade/stdlib/clock.rb', line 9

def uri
  'clock.jd'
end