aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/time/instant.lux
blob: 25df6407ccdf46655e8b532fb8ccf93cd7b4f4be (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
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
(.module:
  [lux #*
   ["@" target]
   [abstract
    [equivalence (#+ Equivalence)]
    [order (#+ Order)]
    [enum (#+ Enum)]
    [codec (#+ Codec)]
    [monad (#+ Monad do)]]
   [control
    [io (#+ IO io)]
    ["." try]
    ["." exception (#+ exception:)]
    ["<>" parser
     ["<t>" text (#+ Parser)]]]
   [data
    ["." maybe]
    ["." text ("#\." monoid)]
    [collection
     ["." row]]]
   [math
    [number
     ["i" int]
     ["f" frac]]]
   [type
    abstract]]
  ["." // (#+ Time)
   ["." duration (#+ Duration)]
   ["." year (#+ Year)]
   ["." month (#+ Month)]
   ["." day (#+ Day)]
   ["." date (#+ Date)]])

(abstract: #export Instant
  Int

  {#.doc "Instant is defined as milliseconds since the epoch."}

  (def: #export from_millis
    (-> Int Instant)
    (|>> :abstraction))

  (def: #export to_millis
    (-> Instant Int)
    (|>> :representation))

  (def: #export (span from to)
    (-> Instant Instant Duration)
    (duration.from_millis (i.- (:representation from) (:representation to))))

  (def: #export (shift duration instant)
    (-> Duration Instant Instant)
    (:abstraction (i.+ (duration.to_millis duration) (:representation instant))))

  (def: #export (relative instant)
    (-> Instant Duration)
    (|> instant :representation duration.from_millis))

  (def: #export (absolute offset)
    (-> Duration Instant)
    (|> offset duration.to_millis :abstraction))

  (structure: #export equivalence
    (Equivalence Instant)
    
    (def: (= param subject)
      (\ i.equivalence = (:representation param) (:representation subject))))

  (structure: #export order
    (Order Instant)
    
    (def: &equivalence ..equivalence)
    (def: (< param subject)
      (\ i.order < (:representation param) (:representation subject))))

  (`` (structure: #export enum
        (Enum Instant)
        
        (def: &order ..order)
        (~~ (template [<name>]
              [(def: <name>
                 (|>> :representation (\ i.enum <name>) :abstraction))]

              [succ] [pred]
              ))))
  )

(def: #export epoch
  {#.doc "The instant corresponding to 1970-01-01T00:00:00Z"}
  Instant
  (..from_millis +0))

(def: millis_per_day
  (duration.query duration.milli_second duration.day))

(def: (split_date_time instant)
  (-> Instant [Date Duration])
  (let [offset (..to_millis instant)
        bce? (i.< +0 offset)
        [days day_time] (if bce?
                          (let [[days millis] (i./% ..millis_per_day offset)]
                            (case millis
                              +0 [days millis]
                              _ [(dec days) (i.+ ..millis_per_day millis)]))
                          (i./% ..millis_per_day offset))]
    [(date.from_days days)
     (duration.from_millis day_time)]))

(template [<value> <definition>]
  [(def: <definition> Text <value>)]

  ["T" date_suffix]
  ["Z" time_suffix]
  )

(def: (clock_time duration)
  (-> Duration Time)
  (let [time (if (\ duration.order < duration.empty duration)
               (duration.merge duration.day duration)
               duration)]
    (|> time duration.to_millis .nat //.from_millis try.assume)))

(def: (encode instant)
  (-> Instant Text)
  (let [[date time] (..split_date_time instant)
        time (..clock_time time)]
    ($_ text\compose
        (\ date.codec encode date) ..date_suffix
        (\ //.codec encode time) ..time_suffix)))

(def: parser
  (Parser Instant)
  (do {! <>.monad}
    [days (\ ! map date.to_days date.parser)
     _ (<t>.this ..date_suffix)
     time (\ ! map //.to_millis //.parser)
     _ (<t>.this ..time_suffix)]
    (wrap (|> (if (i.< +0 days)
                (|> duration.day
                    (duration.up (.nat (i.* -1 days)))
                    duration.inverse)
                (duration.up (.nat days) duration.day))
              (duration.merge (duration.up time duration.milli_second))
              ..absolute))))

(structure: #export codec
  {#.doc (doc "Based on ISO 8601."
              "For example: 2017-01-15T21:14:51.827Z")}
  (Codec Text Instant)
  
  (def: encode ..encode)
  (def: decode (<t>.run ..parser)))

(def: #export now
  (IO Instant)
  (io (..from_millis (for {@.old ("jvm invokestatic:java.lang.System:currentTimeMillis:")
                           @.jvm (|> ("jvm member invoke static" [] "java.lang.System" "currentTimeMillis" [])
                                     (: (primitive "java.lang.Long"))
                                     (:coerce Int))
                           @.js (let [date ("js object new" ("js constant" "Date") [])]
                                  (|> ("js object do" "getTime" date [])
                                      (:coerce Frac)
                                      "lux f64 i64"))
                           @.python (let [time ("python import" "time")]
                                      (|> ("python object do" "time" time)
                                          (:coerce Frac)
                                          (f.* +1,000.0)
                                          "lux f64 i64"))
                           @.lua (|> ("lua constant" "os.time")
                                     "lua apply"
                                     (:coerce Int)
                                     (i.* +1,000))
                           @.ruby (let [% ("ruby constant" "Time")
                                        % ("ruby object do" "now" %)]
                                    (|> ("ruby object do" "to_f" %)
                                        (:coerce Frac)
                                        (f.* +1,000.0)
                                        "lux f64 i64"))
                           @.php (|> ("php constant" "time")
                                     "php apply"
                                     (:coerce Int)
                                     (i.* +1,000))
                           @.scheme (|> ("scheme constant" "current-second")
                                        (:coerce Int)
                                        (i.* +1,000)
                                        ("scheme apply" ("scheme constant" "exact"))
                                        ("scheme apply" ("scheme constant" "truncate")))
                           @.common_lisp (|> ("common_lisp constant" "get-universal-time")
                                             "common_lisp apply"
                                             (:coerce Int)
                                             (i.* +1,000))
                           }))))

(template [<field> <type> <post_processing>]
  [(def: #export (<field> instant)
     (-> Instant <type>)
     (let [[date time] (..split_date_time instant)]
       (|> <field> <post_processing>)))]

  [date Date (|>)]
  [time Time ..clock_time]
  )

(def: #export (day_of_week instant)
  (-> Instant Day)
  (let [offset (..relative instant)
        days (duration.query duration.day offset)
        day_time (duration.frame duration.day offset)
        days (if (and (duration.negative? offset)
                      (not (duration.neutral? day_time)))
               (dec days)
               days)
        ## 1970/01/01 was a Thursday
        y1970m0d0 +4]
    (case (|> y1970m0d0
              (i.+ days) (i.% +7)
              ## This is done to turn negative days into positive days.
              (i.+ +7) (i.% +7))
      +0 #day.Sunday
      +1 #day.Monday
      +2 #day.Tuesday
      +3 #day.Wednesday
      +4 #day.Thursday
      +5 #day.Friday
      +6 #day.Saturday
      _ (undefined))))

(def: #export (from_date_time date time)
  (-> Date Time Instant)
  (|> (date.to_days date)
      (i.* (duration.to_millis duration.day))
      (i.+ (.int (//.to_millis time)))
      ..from_millis))