aboutsummaryrefslogtreecommitdiff
path: root/tests/test/datetime.scm
blob: 1051e203f832af6382260e0f9599601328e053ec (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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
;;; Commentary:
;; Tests date, time, and datetime creation,
;; (output) formatting, and arithmetic.
;;; Code:

(define-module (test datetime)
  :use-module (srfi srfi-64)
  :use-module (srfi srfi-88)
  :use-module ((datetime)
               :select (date+ date-
                              time+
                              time-
                              year
                              month
                              day
                              date
                              time
                              datetime
                              datetime+
                              datetime<=?
                              datetime-difference
                              datetime-
                              leap-year?
                              string->date
                              string->time
                              string->datetime
                              parse-month
                              days-in-interval))
  :use-module ((ice-9 format) :select (format))
  :use-module ((hnh util) :select (let*))
  :use-module ((ice-9 i18n) :select (make-locale))
  :use-module ((guile) :select (LC_TIME)))

(test-equal
  "empty time"
  (time)
  #00:00:00)

(test-assert
  "Synatx date"
  #2020-01-01)

(test-assert
  "Test year type"
  (integer? (year (date year: 2020))))

(test-assert
  "Test mmnth type"
  (integer? (month (date month: 1))))

(test-assert
  "Test day type"
  (integer? (day (date day: 1))))

(test-equal
  "Manual print (any)"
  "2020-10-10"
  (let ((d #2020-10-10))
    (format #f "~a-~a-~a" (year d) (month d) (day d))))

(test-equal
  "Manual print (number)"
  "2020-10-10"
  (let ((d #2020-10-10))
    (format #f "~d-~d-~d" (year d) (month d) (day d))))

(test-equal
  "Date print"
  "#2020-01-01"
  (format
    #f
    "~a"
    #2020-01-01))

(test-equal
  "Syntax date="
  (date year: 2020 month: 1 day: 1)
  #2020-01-01)

(test-equal
  "Syntax time="
  (time hour: 13 minute: 37 second: 0)
  #13:37:00)

(test-equal
  "Syntax Datetime="
  (datetime year: 2020 month: 1 day: 1
            hour: 13 minute: 37 second: 0)
  #2020-01-01T13:37:00)

(test-equal
  #2020-02-28
  (date- #2020-03-05
         (date day: 6)))

(test-equal
  #2020-02-29
  (date- #2020-03-05
         (date day: 5)))

(test-equal
  #2020-03-01
  (date- #2020-03-05
         (date day: 4)))

(test-equal
  "date+ day"
  #2020-10-10
  (date+ #2020-10-01
         (date day: 9)))

(test-equal
  "date+ month"
  #2020-10-10
  (date+ #2020-01-10
         (date month: 9)))

(test-equal
  "date+ day/month"
  #2020-10-10
  (date+ #2020-01-01
         (date day: 9 month: 9)))

(test-assert
  "date+ first literal"
  (date+ #2020-01-01
         (date day: 0)))

(test-assert
  "date+ second literal"
  (date+ #0001-01-01
         #0001-00-00))

(test-assert
  "date+ both literal"
  (date+ #2020-01-01
         #0000-00-00))

(test-equal
  "date+ year overflow"
  #2019-01-01
  (date+ #2018-12-31
         (date day: 1)))

(test-equal
  "date- year overflow"
  #2018-12-31
  (date- #2019-01-01
         (date day: 1)))

(test-equal
  "date- large"
  #0001-01-01
  (date- #2020-01-01
         #2019-00-00))

(test-equal
  "date- equal"
  (date year: -1 month: 11 day: 31)
  (date- #2020-01-01
         #2020-01-01))

(test-equal
  #2020-01-01T10:00:00
  (datetime
    date:
    #2020-01-01
    time:
    #10:00:00))

(test-equal
  #2020-01-01T10:00:00
  (datetime+
    (datetime
      date:
      #2020-01-01)
    (datetime
      time:
      #10:00:00)))

(test-equal
  #2020-10-09T14:00:00
  (datetime-
    #2020-10-10T00:00:00
    (datetime
      time:
      #10:00:00)))

(test-equal
  #2020-09-24T14:00:00
  (datetime-
    #2020-10-10T00:00:00
    #0000-00-15T10:00:00))

(test-equal
  #2020-03-10
  (date+ #2020-03-01
         (date day: 4)
         (date day: 5)))

(let* ((diff overflow
             (time- #10:20:30
                    #10:20:30)))
  (test-equal
    "time- self"
    #00:00:00
    diff)
  (test-equal "time- self overflow" 0 overflow))

(let* ((diff overflow
             (time- #10:00:00
                    #10:00:01)))
  (test-equal
    "time- overflow 1s"
    #23:59:59
    diff)
  (test-equal
    "time- overflow 1s overflow"
    1
    overflow))

(let* ((diff overflow
             (time- #10:00:00
                    (time hour: (+ 48 4)))))
  (test-equal
    "time- overflow multiple"
    #06:00:00
    diff)
  (test-equal
    "time- overflow multiple overflow"
    2
    overflow))

(test-equal
  "datetime-difference self"
  #0000-00-00T00:00:00
  (datetime-difference
    (datetime
      date:
      #2020-01-01)
    (datetime
      date:
      #2020-01-01)))

;; NOTE
;; at the time of writing this returns #2020-02-00
;; The general question is, how is the last in a month handled?
(test-equal
  #2020-01-31
  (date+ #2019-12-31
         (date month: 1)))

(test-assert (leap-year? 2020))

(test-equal
  "Add to Leap day"
  #2020-02-29
  (date+ #2020-02-28
         (date day: 1)))

(test-equal
  "Parse ISO"
  #2021-12-30T13:53:33
  (string->datetime
    "2021-12-30T13:53:33"
    "~Y-~m-~dT~H:~M:~S"))

(test-equal
  "Parse ical date-time"
  #2021-12-30T13:53:33
  (string->datetime
    "20211230T135333"
    "~Y~m~dT~H~M~S"))

(test-equal
  "Parse single hour (padded)"
  (time hour: 5)
  (string->time "05" "~H"))

(test-equal
  "Parse single hour (non-padded)"
  (time hour: 5)
  (string->time "5" "~H"))

(test-equal
  "Parse month (swedish)"
  (date month: 5)
  (string->date
    "Maj"
    "~b"
    (make-locale LC_TIME "sv_SE.UTF-8")))

(test-equal
  "Parse month (english)"
  (date month: 5)
  (string->date
    "May"
    "~b"
    (make-locale LC_TIME "en_US.UTF-8")))

(test-equal
  "AM/PM AM"
  (time hour: 10)
  (string->time "10 AM" "~H ~p"))

(test-equal
  "AM/PM PM"
  (time hour: 22)
  (string->time "10 PM" "~H ~p"))

(test-equal
  "AM/PM AM 12"
  (time hour: 0)
  (string->time "12 AM" "~H ~p"))

(test-equal
  "AM/PM PM 12"
  (time hour: 12)
  (string->time "12 PM" "~H ~p"))

(test-equal
  "AM/PM PM (prefix)"
  (time hour: 22)
  (string->time "PM 10" "~p ~H"))

(test-equal
  "Parse complicated 1"
  #2021-12-30T10:56:00
  (string->datetime
    "Dec. 30, 2021, 10:56"
    "~b. ~d, ~Y, ~H:~M"
    (make-locale LC_TIME "en_US.UTF-8")))

(test-equal
  "Parse complicated 2"
  #2021-12-30T10:56:00
  (string->datetime
    "Dec. 30, 2021, 10:56 a.m."
    "~b. ~d, ~Y, ~H:~M"
    (make-locale LC_TIME "en_US.UTF-8")))

(test-equal
  "Parse complicated 3"
  #2021-12-30T22:56:00
  (string->datetime
    "Dec. 30, 2021, 10:56 p.m."
    "~b. ~d, ~Y, ~H:~M ~p"
    (make-locale LC_TIME "en_US.UTF-8")))

(test-equal
  "Parse date single digit day"
  (date day: 6)
  (string->date "6" "~d"))

(test-equal
  "Parse date single digit day, trailing comma"
  (date day: 6)
  (string->date "6," "~d,"))

(test-equal
  "Parse date single digit day, trailing comma + space"
  (date day: 6)
  (string->date "6, " "~d, "))

(define en_US
  (make-locale LC_TIME "en_US.UTF-8"))

(define sv_SE
  (make-locale LC_TIME "sv_SE.UTF-8"))

(test-equal 1 (parse-month "jan" en_US))

(test-equal 1 (parse-month "jan" sv_SE))

(test-equal 12 (parse-month "dec" en_US))

(test-equal -1 (parse-month "inv" en_US))

(test-equal 5 (parse-month "mAJ" sv_SE))

(test-equal
  "Days in regular year"
  365
  (days-in-interval
    #2021-01-01
    #2021-12-31))

(test-equal
  "Days in leap year"
  366
  (days-in-interval
    #2020-01-01
    #2020-12-31))