aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/time/duration.lux
blob: 2f3d019ddb39aa35ab8f32f7580db207ef4cbbee (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
(;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])
             ["R" result])
       (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))

(def: (encode duration)
  (-> Duration Text)
  (if (:: Eq<Duration> = empty duration)
    "0ms"
    (let [signed? (negative? duration)
          [hours time-left] [(query hour duration) (frame hour duration)]
          hours (if signed?
                  (int/abs hours)
                  hours)
          time-left (if signed?
                      (scale -1 time-left)
                      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/append
          (if signed? "-" "")
          (if (i.= 0 hours) "" (text/append (int/encode hours) "h"))
          (if (i.= 0 minutes) "" (text/append (int/encode minutes) "m"))
          (if (i.= 0 seconds) "" (text/append (int/encode seconds) "s"))
          (if (i.= 0 millis) "" (text/append (int/encode millis) "ms"))
          ))))

(def: (lex-section suffix)
  (-> Text (l;Lexer Int))
  (|> (l;codec number;Codec<Text,Int> (l;many l;decimal))
      (p;before (l;this suffix))
      (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-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-hour) hour))
              (merge (scale (sign utc-minute) minute))
              (merge (scale (sign utc-second) second))
              (merge (scale (sign utc-millis) milli))))))

(def: (decode input)
  (-> Text (R;Result Duration))
  (l;run input lex-duration))

(struct: #export _
  {#;doc "Based on ISO 8601.

          For example: 21h14m51s827ms"}
  (Codec Text Duration)
  (def: encode encode)
  (def: decode decode))