aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/time/duration.lux
blob: 3deefa9442975a6445e726073c09df5a371e2379 (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
(;module:
  lux
  (lux (control eq
                order)
       (type model)))

(model: #export Duration
  {#;doc "Durations have a resolution of milliseconds."}
  Int

  (def: #export from-millis
    (-> Int Duration)
    (|>. @model))

  (def: #export to-millis
    (-> Duration Int)
    (|>. @repr))

  (do-template [<name> <op>]
    [(def: #export (<name> param subject)
       (-> Duration Duration Duration)
       (@model (<op> (@repr param) (@repr subject))))]

    [merge i.+]
    [frame i.%]
    )

  (def: #export (scale scalar duration)
    (-> Int Duration Duration)
    (@model (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 (from-millis 1_000))
(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))