-
Notifications
You must be signed in to change notification settings - Fork 376
/
datetime.test.lua
executable file
·2116 lines (1928 loc) · 89.9 KB
/
datetime.test.lua
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env tarantool
local tap = require('tap')
local test = tap.test('errno')
local date = require('datetime')
local ffi = require('ffi')
local json = require('json')
local msgpack = require('msgpack')
local TZ = date.TZ
--[[
Workaround for #6599 where we may randomly fail on AWS Graviton machines,
while it's working properly when jit disabled.
--]]
if jit.arch == 'arm64' then
jit.off()
jit.flush()
end
test:plan(39)
local INT_MAX = 2147483647
-- minimum supported date - -5879610-06-22
local MIN_DATE_YEAR = -5879610
-- maximum supported date - 5879611-07-11
local MAX_DATE_YEAR = 5879611
local SECS_PER_DAY = 86400
local AVERAGE_DAYS_YEAR = 365.25
local AVERAGE_WEEK_YEAR = AVERAGE_DAYS_YEAR / 7
local MAX_YEAR_RANGE = MAX_DATE_YEAR - MIN_DATE_YEAR
local MAX_MONTH_RANGE = MAX_YEAR_RANGE * 12
local MAX_WEEK_RANGE = MAX_YEAR_RANGE * AVERAGE_WEEK_YEAR
local MAX_DAY_RANGE = MAX_YEAR_RANGE * AVERAGE_DAYS_YEAR
local MAX_HOUR_RANGE = MAX_DAY_RANGE * 24
local MAX_MIN_RANGE = MAX_HOUR_RANGE * 60
local MAX_SEC_RANGE = MAX_DAY_RANGE * SECS_PER_DAY
local MAX_NSEC_RANGE = INT_MAX
local MAX_USEC_RANGE = math.floor(MAX_NSEC_RANGE / 1e3)
local MAX_MSEC_RANGE = math.floor(MAX_NSEC_RANGE / 1e6)
local incompat_types = 'incompatible types for datetime comparison'
local only_integer_ts = 'only integer values allowed in timestamp'..
' if nsec, usec, or msecs provided'
local only_one_of = 'only one of nsec, usec or msecs may be defined'..
' simultaneously'
local int_ival_exp = 'sec: integer value expected, but received number'
local timestamp_and_ymd = 'timestamp is not allowed if year/month/day provided'
local timestamp_and_hms = 'timestamp is not allowed if hour/min/sec provided'
local str_or_num_exp = 'tzoffset: string or number expected, but received'
local numeric_exp = 'numeric value expected, but received '
local expected_interval_but = 'expected interval or table, but received'
local expected_datetime_but = 'expected datetime, interval or table, but received'
-- various error message generators
local function exp_datetime(name, value)
return ("%s: expected datetime, but received %s"):format(name, type(value))
end
local function table_expected(msg, value)
return ("%s: expected table, but received %s"):
format(msg, type(value))
end
local function expected_str(msg, value)
return ("%s: expected string, but received %s"):format(msg, type(value))
end
local function invalid_days_in_mon(d, M, y)
return ('invalid number of days %d in month %d for %d'):format(d, M, y)
end
local function range_check_error(name, value, range)
return ('value %s of %s is out of allowed range [%s, %s]'):
format(value, name, range[1], range[2])
end
local function range_check_3_error(name, value, range)
return ('value %d of %s is out of allowed range [%d, %d..%d]'):
format(value, name, range[1], range[2], range[3])
end
local function ival_overflow(op, name, value, max)
return ('%s moves value %s of %s out of allowed range [%s, %s]'):
format(op, value, name, -max, max)
end
local function invalid_date(y, M, d)
return ('date %d-%02d-%02d is invalid'):format(y, M, d)
end
local function invalid_tz_fmt_error(val)
return ('invalid time-zone format %s'):format(val)
end
local function invalid_date_fmt_error(str)
return ('invalid date format %s'):format(str)
end
local function assert_raises(test, error_msg, func, ...)
local ok, err = pcall(func, ...)
local err_tail = err and err:gsub("^.+:%d+: ", "") or ''
return test:is(not ok and err_tail, error_msg,
('"%s" received, "%s" expected'):format(err_tail, error_msg))
end
local function assert_raises_like(test, error_msg, func, ...)
local ok, err = pcall(func, ...)
local err_tail = err and err:gsub("^.+:%d+: ", "") or ''
return test:like(not ok and err_tail, error_msg,
('"%s" received, "%s" expected'):format(err_tail, error_msg))
end
-- Basic interval string representation generator for interval arguments.
local fields_str = {
{ 'year', 'years' },
{ 'month', 'months' },
{ 'week', 'weeks' },
{ 'day', 'days' },
{ 'hour', 'hours' },
{ 'min', 'minutes' },
{ 'sec', 'seconds' },
{ 'msec', 'nanoseconds', 1e6 },
{ 'usec', 'nanoseconds', 1e3 },
{ 'nsec', 'nanoseconds' },
}
local function iv_str_repr(tab_iv)
local iv_repr = ''
for _, field in ipairs(fields_str) do
local name, field_repr, mult = unpack(field)
local value = tab_iv[name]
if value ~= nil then
if mult ~= nil then
value = value * mult
end
if iv_repr == '' then
iv_repr = ('%+d %s'):format(value, field_repr)
else
iv_repr = iv_repr .. (', %d %s'):format(value, field_repr)
end
elseif (name == 'sec') and (iv_repr == '') then
iv_repr = ('%+d %s'):format(0, field_repr)
end
end
return iv_repr
end
test:test("Datetime API checks", function(test)
test:plan(12)
local ts = date.new()
local local_format = ts.format
local local_totable = ts.totable
local local_set = ts.set
test:is(local_format(ts), "1970-01-01T00:00:00Z", "correct :format")
local table_expected = {
sec = 0, min = 0, wday = 5, day = 1, nsec = 0,
isdst = false, yday = 1, tzoffset = 0, month = 1,
year = 1970, hour = 0
}
test:is_deeply(local_totable(ts), table_expected, "correct :totable")
local date_expected = date.new()
date_expected.epoch = 1
test:is(local_set(ts, {sec = 1}), date_expected, "correct :set")
-- check results of wrong arguments passed
assert_raises(test, exp_datetime("datetime.format()", 123),
function() return local_format(123) end)
assert_raises(test, exp_datetime("datetime.format()", "1970-01-01"),
function() return local_format("1970-01-01") end)
assert_raises(test, exp_datetime("datetime.format()"),
function() return local_format() end)
assert_raises(test, exp_datetime("datetime.totable()", 123),
function() return local_totable(123) end)
assert_raises(test, exp_datetime("datetime.totable()", "1970-01-01"),
function() return local_totable("1970-01-01") end)
assert_raises(test, exp_datetime("datetime.totable()"),
function() return local_totable() end)
assert_raises(test, exp_datetime("datetime.set()", 123),
function() return local_set(123) end)
assert_raises(test, exp_datetime("datetime.set()", "1970-01-01"),
function() return local_set("1970-01-01") end)
assert_raises(test, exp_datetime("datetime.set()"),
function() return local_set() end)
end)
test:test("Default date creation and comparison", function(test)
test:plan(37)
-- check empty arguments
local ts1 = date.new()
test:is(ts1.epoch, 0, "ts.epoch ==0")
test:is(ts1.nsec, 0, "ts.nsec == 0")
test:is(ts1.tzoffset, 0, "ts.tzoffset == 0")
test:is(tostring(ts1), "1970-01-01T00:00:00Z", "tostring(ts1)")
-- check empty table
local ts2 = date.new{}
test:is(ts2.epoch, 0, "ts.epoch ==0")
test:is(ts2.nsec, 0, "ts.nsec == 0")
test:is(ts2.tzoffset, 0, "ts.tzoffset == 0")
test:is(tostring(ts2), "1970-01-01T00:00:00Z", "tostring(ts2)")
-- check their equivalence
test:is(ts1, ts2, "ts1 == ts2")
test:is(ts1 ~= ts2, false, "not ts1 != ts2")
test:isnt(ts1, nil, "ts1 != {}")
test:isnt(ts1, {}, "ts1 != {}")
test:isnt(ts1, "1970-01-01T00:00:00Z", "ts1 != '1970-01-01T00:00:00Z'")
test:isnt(ts1, 19700101, "ts1 != 19700101")
test:isnt(nil, ts1, "{} ~= ts1")
test:isnt({}, ts1 ,"{} ~= ts1")
test:isnt("1970-01-01T00:00:00Z", ts1, "'1970-01-01T00:00' ~= ts1")
test:isnt(19700101, ts1, "ts1 ~= ts1")
test:is(ts1 < ts2, false, "not ts1 < ts2")
test:is(ts1 > ts2, false, "not ts1 < ts2")
test:is(ts1 <= ts2, true, "not ts1 < ts2")
test:is(ts1 >= ts2, true, "not ts1 < ts2")
-- check is_datetime
test:is(date.is_datetime(ts1), true, "ts1 is datetime")
test:is(date.is_datetime(ts2), true, "ts2 is datetime")
test:is(date.is_datetime({}), false, "bogus is not datetime")
-- check comparison errors -- ==, ~= not raise errors, other
-- comparison operators should raise
assert_raises(test, incompat_types, function() return ts1 < nil end)
assert_raises(test, incompat_types, function() return ts1 < 123 end)
assert_raises(test, incompat_types, function() return ts1 < '1970-01-01' end)
assert_raises(test, incompat_types, function() return ts1 <= nil end)
assert_raises(test, incompat_types, function() return ts1 <= 123 end)
assert_raises(test, incompat_types, function() return ts1 <= '1970-01-01' end)
assert_raises(test, incompat_types, function() return ts1 > nil end)
assert_raises(test, incompat_types, function() return ts1 > 123 end)
assert_raises(test, incompat_types, function() return ts1 > '1970-01-01' end)
assert_raises(test, incompat_types, function() return ts1 >= nil end)
assert_raises(test, incompat_types, function() return ts1 >= 123 end)
assert_raises(test, incompat_types, function() return ts1 >= '1970-01-01' end)
end)
test:test("Simple date creation by attributes", function(test)
test:plan(15)
local ts
local obj = {}
local attribs = {
{ 'year', 2000, '2000-01-01T00:00:00Z' },
{ 'month', 11, '2000-11-01T00:00:00Z' },
{ 'day', 30, '2000-11-30T00:00:00Z' },
{ 'hour', 6, '2000-11-30T06:00:00Z' },
{ 'min', 12, '2000-11-30T06:12:00Z' },
{ 'sec', 23, '2000-11-30T06:12:23Z' },
{ 'tzoffset', -8*60, '2000-11-30T06:12:23-0800' },
{ 'tzoffset', '+0800', '2000-11-30T06:12:23+0800' },
{ 'tz', 'MSK', '2000-11-30T06:12:23 MSK' },
{ 'tz', 'Z', '2000-11-30T06:12:23Z' },
}
for _, row in pairs(attribs) do
local key, value, str = unpack(row)
obj[key] = value
ts = date.new(obj)
test:is(tostring(ts), str, ('{%s = %s}, expected %s'):
format(key, value, str))
end
test:is(tostring(date.new{timestamp = 1630359071.125}),
'2021-08-30T21:31:11.125Z', '{timestamp}')
test:is(tostring(date.new{timestamp = 1630359071, msec = 123}),
'2021-08-30T21:31:11.123Z', '{timestamp.msec}')
test:is(tostring(date.new{timestamp = 1630359071, usec = 123}),
'2021-08-30T21:31:11.000123Z', '{timestamp.usec}')
test:is(tostring(date.new{timestamp = 1630359071, nsec = 123}),
'2021-08-30T21:31:11.000000123Z', '{timestamp.nsec}')
test:is(tostring(date.new{timestamp = -0.1}),
'1969-12-31T23:59:59.900Z', '{negative timestamp}')
end)
test:test("Simple date creation by attributes - check failed", function(test)
test:plan(83)
local boundary_checks = {
{'year', {MIN_DATE_YEAR, MAX_DATE_YEAR}},
{'month', {1, 12}},
{'day', {1, 31, -1}},
{'hour', {0, 23}},
{'min', {0, 59}},
{'sec', {0, 60}},
{'usec', {0, 1e6}},
{'msec', {0, 1e3}},
{'nsec', {0, 1e9}},
{'tzoffset', {-720, 840}, str_or_num_exp},
}
local ts = date.new()
for _, row in pairs(boundary_checks) do
local attr_name, bounds, expected_msg = unpack(row)
local left, right, extra = unpack(bounds)
if extra == nil then
assert_raises(test,
range_check_error(attr_name, left - 1,
{left, right}),
function() date.new{ [attr_name] = left - 1} end)
assert_raises(test,
range_check_error(attr_name, right + 1,
{left, right}),
function() date.new{ [attr_name] = right + 1} end)
assert_raises(test,
range_check_error(attr_name, left - 50,
{left, right}),
function() date.new{ [attr_name] = left - 50} end)
assert_raises(test,
range_check_error(attr_name, right + 50,
{left, right}),
function() date.new{ [attr_name] = right + 50} end)
else -- special case for {day = -1}
assert_raises(test,
range_check_3_error(attr_name, left - 1,
{extra, left, right}),
function() date.new{ [attr_name] = left - 1} end)
assert_raises(test,
range_check_3_error(attr_name, right + 1,
{extra, left, right}),
function() date.new{ [attr_name] = right + 1} end)
assert_raises(test,
range_check_3_error(attr_name, left - 50,
{extra, left, right}),
function() date.new{ [attr_name] = left - 50} end)
assert_raises(test,
range_check_3_error(attr_name, right + 50,
{extra, left, right}),
function() date.new{ [attr_name] = right + 50} end)
end
-- tzoffset uses different message to others
expected_msg = expected_msg or numeric_exp
assert_raises_like(test, expected_msg,
function() date.new{[attr_name] = {}} end)
assert_raises_like(test, expected_msg,
function() date.new{[attr_name] = ts} end)
end
local specific_errors = {
{only_one_of, { nsec = 123456, usec = 123}},
{only_one_of, { nsec = 123456, msec = 123}},
{only_one_of, { usec = 123, msec = 123}},
{only_one_of, { nsec = 123456, usec = 123, msec = 123}},
{only_integer_ts, { timestamp = 12345.125, usec = 123}},
{only_integer_ts, { timestamp = 12345.125, msec = 123}},
{only_integer_ts, { timestamp = 12345.125, nsec = 123}},
{timestamp_and_ymd, {timestamp = 1630359071.125, month = 9 }},
{timestamp_and_ymd, {timestamp = 1630359071.125, month = 9 }},
{timestamp_and_ymd, {timestamp = 1630359071.125, day = 29 }},
{timestamp_and_hms, {timestamp = 1630359071.125, hour = 20 }},
{timestamp_and_hms, {timestamp = 1630359071.125, min = 10 }},
{timestamp_and_hms, {timestamp = 1630359071.125, sec = 29 }},
{table_expected('datetime.new()', '2001-01-01'), '2001-01-01'},
{table_expected('datetime.new()', 20010101), 20010101},
{range_check_3_error('day', 32, {-1, 1, 31}),
{year = 2021, month = 6, day = 32}},
{invalid_days_in_mon(31, 6, 2021), { year = 2021, month = 6, day = 31}},
{invalid_date(-5879610, 6, 21),
{year = -5879610, month = 6, day = 21}},
{invalid_date(-5879610, 1, 1),
{year = -5879610, month = 1, day = 1}},
{range_check_error('year', -16009610, {MIN_DATE_YEAR, MAX_DATE_YEAR}),
{year = -16009610, month = 12, day = 31}},
{range_check_error('year', 16009610, {MIN_DATE_YEAR, MAX_DATE_YEAR}),
{year = 16009610, month = 1, day = 1}},
{invalid_date(MAX_DATE_YEAR, 9, 1),
{year = MAX_DATE_YEAR, month = 9, day = 1}},
{invalid_date(MAX_DATE_YEAR, 7, 12),
{year = MAX_DATE_YEAR, month = 7, day = 12}},
}
for _, row in pairs(specific_errors) do
local err_msg, attribs = unpack(row)
assert_raises(test, err_msg, function() date.new(attribs) end)
end
end)
test:test("Formatting limits", function(test)
test:plan(15)
local ts = date.new()
local len = ffi.C.tnt_datetime_to_string(ts, nil, 0)
test:is(len, 20, 'tostring() with NULL')
local buff = ffi.new('char[?]', len + 1)
len = ffi.C.tnt_datetime_to_string(ts, buff, len + 1)
test:is(len, 20, 'tostring() with non-NULL')
test:is(ffi.string(buff), '1970-01-01T00:00:00Z', 'Epoch string')
local fmt = '%d/%m/%Y'
len = ffi.C.tnt_datetime_strftime(ts, nil, 0, fmt)
test:is(len, 10, 'format(fmt) with NULL')
local strfmt_sz = 128
buff = ffi.new('char[?]', strfmt_sz)
len = ffi.C.tnt_datetime_strftime(ts, buff, strfmt_sz, fmt)
test:is(len, 10, 'format(fmt) with non-NULL')
test:is(ffi.string(buff), '01/01/1970', 'Epoch string (fmt)')
local bogus_fmt = string.rep('1', 10000)
len = ffi.C.tnt_datetime_strftime(ts, nil, 0, bogus_fmt)
test:is(len, #bogus_fmt, 'format(fmt) with NULL and bogus format')
local s = ts:format(bogus_fmt)
test:is(s, bogus_fmt, 'bogus format #1')
s = ts:format(bogus_fmt..'%T')
test:is(s, bogus_fmt..'00:00:00', 'bogus format #2')
-- checks for boundary conditions in datetime_strftime
-- which uses 128-bytes long stash buffer for fast-track allocations
for len = 127,129 do
bogus_fmt = string.rep('1', len)
s = ts:format(bogus_fmt)
test:is(len, #bogus_fmt, ('bogus format lengths check %d'):format(len))
test:is(s, bogus_fmt, 'bogus format equality check')
end
end)
test:test("Simple tests for parser", function(test)
test:plan(12)
test:ok(date.parse("1970-01-01T01:00:00Z") ==
date.new{year=1970, mon=1, day=1, hour=1, min=0, sec=0})
test:ok(date.parse("1970-01-01T01:00:00Z", {format = 'iso8601'}) ==
date.new{year=1970, mon=1, day=1, hour=1, min=0, sec=0})
test:ok(date.parse("1970-01-01T01:00:00Z", {format = 'rfc3339'}) ==
date.new{year=1970, mon=1, day=1, hour=1, min=0, sec=0})
test:ok(date.parse("2020-01-01T01:00:00+00:00", {format = 'rfc3339'}) ==
date.parse("2020-01-01T01:00:00+00:00", {format = 'iso8601'}))
test:ok(date.parse("1970-01-01T02:00:00+02:00") ==
date.new{year=1970, mon=1, day=1, hour=2, min=0, sec=0, tzoffset=120})
test:ok(date.parse("1970-01-01T01:00:00", {tzoffset = 120}) ==
date.new{year=1970, mon=1, day=1, hour=1, min=0, sec=0, tzoffset=120})
test:ok(date.parse("1970-01-01T01:00:00", {tzoffset = '+0200'}) ==
date.new{year=1970, mon=1, day=1, hour=1, min=0, sec=0, tzoffset=120})
test:ok(date.parse("1970-01-01T01:00:00", {tzoffset = '+02:00'}) ==
date.new{year=1970, mon=1, day=1, hour=1, min=0, sec=0, tzoffset=120})
test:ok(date.parse("1970-01-01T01:00:00Z", {tzoffset = '+02:00'}) ==
date.new{year=1970, mon=1, day=1, hour=1, min=0, sec=0, tzoffset=0})
test:ok(date.parse("1970-01-01T01:00:00+01:00", {tzoffset = '+02:00'}) ==
date.new{year=1970, mon=1, day=1, hour=1, min=0, sec=0, tzoffset=60})
test:ok(date.parse("1970-01-01T02:00:00Z") <
date.new{year=1970, mon=1, day=1, hour=2, min=0, sec=1})
test:ok(date.parse("1970-01-01T02:00:00Z") <=
date.new{year=1970, mon=1, day=1, hour=2, min=0, sec=0})
end)
test:test("Multiple tests for parser (with nanoseconds)", function(test)
test:plan(211)
-- borrowed from
-- github.com/chansen/p5-time-moment/blob/master/t/180_from_string.t
local tests =
{
--{ iso-8601 string, epoch, nanoseconds, tz-offset, do reverse check?}
{'0001-01-01T00:00:00Z', -62135596800, 0, 0, 0},
{'00010101T000000Z', -62135596800, 0, 0, 0},
{'0001-W01-1T00:00:00Z', -62135596800, 0, 0, 0},
{'0001W011T000000Z', -62135596800, 0, 0, 0},
{'0001-001T00:00:00Z', -62135596800, 0, 0, 0},
{'0001001T000000Z', -62135596800, 0, 0, 0},
{'1970-01-01T00:00:00Z', 0, 0, 0, 1},
{'1970-01-01T02:00:00+0200', 0, 0, 120, 1},
{'1970-01-01T01:30:00+0130', 0, 0, 90, 1},
{'1970-01-01T01:00:00+0100', 0, 0, 60, 1},
{'1970-01-01T00:01:00+0001', 0, 0, 1, 1},
{'1970-01-01T00:00:00Z', 0, 0, 0, 1},
{'1969-12-31T23:59:00-0001', 0, 0, -1, 1},
{'1969-12-31T23:00:00-0100', 0, 0, -60, 1},
{'1969-12-31T22:30:00-0130', 0, 0, -90, 1},
{'1969-12-31T22:00:00-0200', 0, 0, -120, 1},
{'1970-01-01T00:00:00.123456789Z', 0, 123456789, 0, 1},
{'1970-01-01T00:00:00.12345678Z', 0, 123456780, 0, 0},
{'1970-01-01T00:00:00.1234567Z', 0, 123456700, 0, 0},
{'1970-01-01T00:00:00.123456Z', 0, 123456000, 0, 1},
{'1970-01-01T00:00:00.12345Z', 0, 123450000, 0, 0},
{'1970-01-01T00:00:00.1234Z', 0, 123400000, 0, 0},
{'1970-01-01T00:00:00.123Z', 0, 123000000, 0, 1},
{'1970-01-01T00:00:00.12Z', 0, 120000000, 0, 0},
{'1970-01-01T00:00:00.1Z', 0, 100000000, 0, 0},
{'1970-01-01T00:00:00.01Z', 0, 10000000, 0, 0},
{'1970-01-01T00:00:00.001Z', 0, 1000000, 0, 1},
{'1970-01-01T00:00:00.0001Z', 0, 100000, 0, 0},
{'1970-01-01T00:00:00.00001Z', 0, 10000, 0, 0},
{'1970-01-01T00:00:00.000001Z', 0, 1000, 0, 1},
{'1970-01-01T00:00:00.0000001Z', 0, 100, 0, 0},
{'1970-01-01T00:00:00.00000001Z', 0, 10, 0, 0},
{'1970-01-01T00:00:00.000000001Z', 0, 1, 0, 1},
{'1970-01-01T00:00:00.000000009Z', 0, 9, 0, 1},
{'1970-01-01T00:00:00.00000009Z', 0, 90, 0, 0},
{'1970-01-01T00:00:00.0000009Z', 0, 900, 0, 0},
{'1970-01-01T00:00:00.000009Z', 0, 9000, 0, 1},
{'1970-01-01T00:00:00.00009Z', 0, 90000, 0, 0},
{'1970-01-01T00:00:00.0009Z', 0, 900000, 0, 0},
{'1970-01-01T00:00:00.009Z', 0, 9000000, 0, 1},
{'1970-01-01T00:00:00.09Z', 0, 90000000, 0, 0},
{'1970-01-01T00:00:00.9Z', 0, 900000000, 0, 0},
{'1970-01-01T00:00:00.99Z', 0, 990000000, 0, 0},
{'1970-01-01T00:00:00.999Z', 0, 999000000, 0, 1},
{'1970-01-01T00:00:00.9999Z', 0, 999900000, 0, 0},
{'1970-01-01T00:00:00.99999Z', 0, 999990000, 0, 0},
{'1970-01-01T00:00:00.999999Z', 0, 999999000, 0, 1},
{'1970-01-01T00:00:00.9999999Z', 0, 999999900, 0, 0},
{'1970-01-01T00:00:00.99999999Z', 0, 999999990, 0, 0},
{'1970-01-01T00:00:00.999999999Z', 0, 999999999, 0, 1},
{'1970-01-01T00:00:00.0Z', 0, 0, 0, 0},
{'1970-01-01T00:00:00.00Z', 0, 0, 0, 0},
{'1970-01-01T00:00:00.000Z', 0, 0, 0, 0},
{'1970-01-01T00:00:00.0000Z', 0, 0, 0, 0},
{'1970-01-01T00:00:00.00000Z', 0, 0, 0, 0},
{'1970-01-01T00:00:00.000000Z', 0, 0, 0, 0},
{'1970-01-01T00:00:00.0000000Z', 0, 0, 0, 0},
{'1970-01-01T00:00:00.00000000Z', 0, 0, 0, 0},
{'1970-01-01T00:00:00.000000000Z', 0, 0, 0, 0},
{'1973-11-29T21:33:09Z', 123456789, 0, 0, 1},
{'2013-10-28T17:51:56Z', 1382982716, 0, 0, 1},
{'9999-12-31T23:59:59Z', 253402300799, 0, 0, 1},
}
for _, value in ipairs(tests) do
local str, epoch, nsec, tzoffset, check
str, epoch, nsec, tzoffset, check = unpack(value)
local dt = date.parse(str)
test:is(dt.epoch, epoch, ('%s: dt.epoch == %d'):format(str, epoch))
test:is(dt.nsec, nsec, ('%s: dt.nsec == %d'):format(str, nsec))
test:is(dt.tzoffset, tzoffset, ('%s: dt.tzoffset == %d'):format(str, tzoffset))
if check > 0 then
test:is(str, tostring(dt), ('%s == tostring(%s)'):
format(str, tostring(dt)))
end
end
end)
local function create_date_string(date)
local year, month, day = date.year or 1970, date.month or 1, date.day or 1
local hour, min, sec = date.hour or 0, date.min or 0, date.sec or 0
return ('%04d-%02d-%02dT%02d:%02d:%02dZ'):format(year, month, day, hour, min, sec)
end
test:test("Check parsing of full supported years range", function(test)
test:plan(63)
local valid_years = {
-5879610, -5879000, -5800000, -2e6, -1e5, -1e4, -9999, -2000, -1000,
0, 1, 1000, 1900, 1970, 2000, 9999,
1e4, 1e6, 2e6, 5e6, 5879611
}
local fmt = '%FT%T%z'
for _, y in ipairs(valid_years) do
local txt = ('%04d-06-22'):format(y)
local dt = date.parse(txt)
test:isnt(dt, nil, dt)
local out_txt = tostring(dt)
local out_dt = date.parse(out_txt)
test:is(dt, out_dt, ('default parse of %s (%s == %s)'):
format(out_txt, dt, out_dt))
local fmt_dt = date.parse(out_txt, {format = fmt})
test:is(dt, fmt_dt, ('parse via format %s (%s == %s)'):
format(fmt, dt, fmt_dt))
end
end)
local function couldnt_parse(txt)
return ("could not parse '%s'"):format(txt)
end
test:test("Check parsing of dates with invalid attributes", function(test)
test:plan(32)
local boundary_checks = {
{'month', {1, 12}},
{'day', {1, 31, -1}},
{'hour', {0, 23}},
{'min', {0, 59}},
{'sec', {0, 59}},
}
for _, row in pairs(boundary_checks) do
local attr_name, bounds = unpack(row)
local left, right = unpack(bounds)
local txt = create_date_string{[attr_name] = left}
local dt, len = date.parse(txt)
test:ok(dt ~= nil, dt)
test:ok(len == #txt, len)
local txt = create_date_string{[attr_name] = right}
dt, len = date.parse(txt)
test:ok(dt ~= nil, dt)
test:ok(len == #txt, len)
-- expected error
if left > 0 then
txt = create_date_string{[attr_name] = left - 1}
assert_raises(test, couldnt_parse(txt),
function() dt, len = date.parse(txt) end)
end
txt = create_date_string{[attr_name] = right + 2}
assert_raises(test, couldnt_parse(txt),
function() dt, len = date.parse(txt) end)
txt = create_date_string{[attr_name] = right + 50}
assert_raises(test, couldnt_parse(txt),
function() dt, len = date.parse(txt) end)
end
end)
test:test("Parsing of timezone abbrevs", function(test)
test:plan(220)
local zone_abbrevs = {
-- military
A = 1*60, B = 2*60, C = 3*60,
D = 4*60, E = 5*60, F = 6*60,
G = 7*60, H = 8*60, I = 9*60,
K = 10*60, L = 11*60, M = 12*60,
N = -1*60, O = -2*60, P = -3*60,
Q = -4*60, R = -5*60, S = -6*60,
T = -7*60, U = -8*60, V = -9*60,
W = -10*60, X = -11*60, Y = -12*60,
Z = 0,
-- universal
GMT = 0, UTC = 0, UT = 0,
-- some non ambiguous
MSK = 3 * 60, MCK = 3 * 60, CET = 1 * 60,
AMDT = 5 * 60, BDST = 1 * 60, IRKT = 8 * 60,
KST = 9 * 60, PDT = -7 * 60, WET = 0 * 60,
HOVDST = 8 * 60, CHODST = 9 * 60,
-- Olson
['Europe/Moscow'] = 180,
['Africa/Abidjan'] = 0,
['America/Argentina/Buenos_Aires'] = -180,
['Asia/Krasnoyarsk'] = 420,
['Pacific/Fiji'] = 720,
}
local exp_pattern = '^2020%-02%-10T00:00'
local base_date = '2020-02-10T0000 '
for zone, offset in pairs(zone_abbrevs) do
local date_text = base_date .. zone
local date, len = date.parse(date_text)
test:isnt(date, nil, 'parse ' .. zone)
test:ok(len > #base_date, 'length longer than ' .. #base_date)
test:is(1, tostring(date):find(exp_pattern), 'expected prefix')
test:is(date.tzoffset, offset, 'expected offset')
test:is(date.tz, zone, 'expected timezone name')
end
end)
test:test("Parsing of timezone names (tzindex)", function(test)
test:plan(396)
local zone_abbrevs = {
-- military
A = 1, B = 2, C = 3,
D = 4, E = 5, F = 6,
G = 7, H = 8, I = 9,
K = 10, L = 11, M = 12,
N = 13, O = 14, P = 15,
Q = 16, R = 17, S = 18,
T = 19, U = 20, V = 21,
W = 22, X = 23, Y = 24,
Z = 25,
-- universal
GMT = 186, UTC = 296, UT = 112,
-- some non ambiguous
MSK = 238, MCK = 232, CET = 155,
AMDT = 336, BDST = 344, IRKT = 409,
KST = 226, PDT = 264, WET = 314,
HOVDST = 664, CHODST = 656,
-- Olson
['Europe/Moscow'] = 947,
['Africa/Abidjan'] = 672,
['America/Argentina/Buenos_Aires'] = 694,
['Asia/Krasnoyarsk'] = 861,
['Pacific/Fiji'] = 984,
}
local exp_pattern = '^2020%-02%-10T00:00'
local base_date = '2020-02-10T0000 '
for zone, index in pairs(zone_abbrevs) do
local date_text = base_date .. zone
local date, len = date.parse(date_text)
print(zone, index)
test:isnt(date, nil, 'parse ' .. zone)
local tzname = date.tz
local tzindex = date.tzindex
test:is(tzindex, index, 'expected tzindex')
test:is(tzname, zone, 'expected timezone name')
test:is(TZ[tzindex], tzname, ('TZ[%d] => %s'):format(tzindex, tzname))
test:is(TZ[tzname], tzindex, ('TZ[%s] => %d'):format(tzname, tzindex))
test:ok(len > #base_date, 'length longer than ' .. #base_date)
local txt = tostring(date)
test:is(1, txt:find(exp_pattern), 'expected prefix')
test:is(zone, txt:sub(#txt - #zone + 1, #txt), 'sub of ' .. txt)
txt = date:format('%FT%T %Z')
test:is(zone, txt:sub(#txt - #zone + 1, #txt), 'sub of ' .. txt)
end
end)
local function error_ambiguous(s)
return ("could not parse '%s' - ambiguous timezone"):format(s)
end
local function error_generic(s)
return ("could not parse '%s'"):format(s)
end
test:test("Parsing of timezone names (errors)", function(test)
test:plan(9)
local zones_arratic = {
-- ambiguous
AT = error_ambiguous, BT = error_ambiguous,
ACT = error_ambiguous, BST = error_ambiguous,
GST = error_ambiguous, WAT = error_ambiguous,
AZOST = error_ambiguous,
-- generic errors
['XXX'] = error_generic,
['A-_'] = error_generic,
}
local base_date = '2020-02-10T0000 '
for zone, error_function in pairs(zones_arratic) do
local date_text = base_date .. zone
assert_raises(test, error_function(date_text),
function() return date.parse(date_text) end)
end
end)
test:test("Daylight saving checks", function (test)
--[[
Check various dates in `Europe/Moscow` timezone for their
proper daylight saving settings.
Tzdata defines these rules for `Europe/Moscow` time-zone:
```
Zone Europe/Moscow 2:30:17 - LMT 1880
2:30:17 - MMT 1916 Jul 3 # Moscow Mean Time
2:31:19 Russia %s 1919 Jul 1 0:00u
3:00 Russia %s 1921 10
3:00 Russia Europe/Moscow/Europe/Moscow 1922 10
2:00 - EET 1930 Jun 21
3:00 Russia Europe/Moscow/Europe/Moscow 1991 03 31 2:00s
2:00 Russia EE%sT 1992 Jan 19 2:00s
3:00 Russia Europe/Moscow/Europe/Moscow 2011 03 27 2:00s
4:00 - Europe/Moscow 2014 10 26 2:00s
3:00 - Europe/Moscow
```
Either you could see the same table dumped in more or less
human-readable form using `zdump` utility:
`zdump -c 2004,2022 -v Europe/Moscow`
]]
test:plan(30)
local moments = {
-- string, isdst?, tzoffset (mins)
{'2004-10-31T02:00:00 Europe/Moscow', false, 3 * 60},
{'2005-03-27T03:00:00 Europe/Moscow', true, 4 * 60},
{'2005-10-30T02:00:00 Europe/Moscow', false, 3 * 60},
{'2006-03-26T03:00:00 Europe/Moscow', true, 4 * 60},
{'2006-10-29T02:00:00 Europe/Moscow', false, 3 * 60},
{'2007-03-25T03:00:00 Europe/Moscow', true, 4 * 60},
{'2007-10-28T02:00:00 Europe/Moscow', false, 3 * 60},
{'2008-03-30T03:00:00 Europe/Moscow', true, 4 * 60},
{'2008-10-26T02:00:00 Europe/Moscow', false, 3 * 60},
{'2009-03-29T03:00:00 Europe/Moscow', true, 4 * 60},
{'2009-10-25T02:00:00 Europe/Moscow', false, 3 * 60},
{'2010-03-28T03:00:00 Europe/Moscow', true, 4 * 60},
{'2010-10-31T02:00:00 Europe/Moscow', false, 3 * 60},
{'2011-03-27T03:00:00 Europe/Moscow', false, 4 * 60},
{'2014-10-26T01:00:00 Europe/Moscow', false, 3 * 60},
}
for _, row in pairs(moments) do
local str, isdst, tzoffset = unpack(row)
local dt = date.parse(str)
test:is(dt.isdst, isdst,
('%s: isdst = %s'):format(tostring(dt), dt.isdst))
test:is(dt.tzoffset, tzoffset,
('%s: tzoffset = %s'):format(tostring(dt), dt.tzoffset))
end
end)
test:test("Datetime string formatting", function(test)
test:plan(11)
local t = date.new()
test:is(t.epoch, 0, ('t.epoch == %d'):format(tonumber(t.epoch)))
test:is(t.nsec, 0, ('t.nsec == %d'):format(t.nsec))
test:is(t.tzoffset, 0, ('t.tzoffset == %d'):format(t.tzoffset))
test:is(t:format('%d/%m/%Y'), '01/01/1970', '%s: format #1')
test:is(t:format('%A %d. %B %Y'), 'Thursday 01. January 1970', 'format #2')
test:is(t:format('%FT%T%z'), '1970-01-01T00:00:00+0000', 'format #3')
test:is(t:format('%FT%T.%f%z'), '1970-01-01T00:00:00.000+0000', 'format #4')
test:is(t:format('%FT%T.%4f%z'), '1970-01-01T00:00:00.0000+0000', 'format #5')
test:is(t:format(), '1970-01-01T00:00:00Z', 'format #6')
test:is(t:format('%64424509441f'), '000000000', 'format #7')
assert_raises(test, expected_str('datetime.strftime()', 1234),
function() t:format(1234) end)
end)
local function check_variant_formats(test, base, variants)
for _, var in pairs(variants) do
local year, str, strf = unpack(var)
local obj = base
obj.year = year
local t = date.new(obj)
test:is(t:format(), str, ('default format for year = %d'):format(year))
test:is(t:format('%FT%T.%f%z'), strf,
('strftime for year = %d'):format(year))
end
end
test:test("Datetime formatting of huge dates", function(test)
test:plan(22)
local base = {month = 6, day = 10, hour = 12, min = 10, sec = 10}
local variants = {
{-5000000, '-5000000-06-10T12:10:10Z', '-5000000-06-10T12:10:10.000+0000'},
{-10000, '-10000-06-10T12:10:10Z', '-10000-06-10T12:10:10.000+0000'},
{-1, '-001-06-10T12:10:10Z', '-001-06-10T12:10:10.000+0000'},
{1, '0001-06-10T12:10:10Z', '0001-06-10T12:10:10.000+0000'},
{10, '0010-06-10T12:10:10Z', '0010-06-10T12:10:10.000+0000'},
{10000, '10000-06-10T12:10:10Z', '10000-06-10T12:10:10.000+0000'},
{5000000, '5000000-06-10T12:10:10Z', '5000000-06-10T12:10:10.000+0000'},
}
check_variant_formats(test, base, variants)
local base = {month = 10, day = 8}
local variants = {
{-10000, '-10000-10-08T00:00:00Z', '-10000-10-08T00:00:00.000+0000'},
{-1, '-001-10-08T00:00:00Z', '-001-10-08T00:00:00.000+0000'},
{1, '0001-10-08T00:00:00Z', '0001-10-08T00:00:00.000+0000'},
{10000, '10000-10-08T00:00:00Z', '10000-10-08T00:00:00.000+0000'},
}
check_variant_formats(test, base, variants)
end)
local strftime_formats = {
{ '%A', 1, 'Thursday' },
{ '%a', 1, 'Thu' },
{ '%B', 1, 'January' },
{ '%b', 1, 'Jan' },
{ '%h', 1, 'Jan' },
{ '%C', 0, '19' },
{ '%c', 1, 'Thu Jan 1 03:00:00 1970' },
{ '%D', 1, '01/01/70' },
{ '%m/%d/%y', 1, '01/01/70' },
{ '%d', 1, '01' },
{ '%Ec', 1, 'Thu Jan 1 03:00:00 1970' },
{ '%EC', 0, '19' },
{ '%Ex', 1, '01/01/70' },
{ '%EX', 1, '03:00:00' },
{ '%Ey', 1, '70' },
{ '%EY', 1, '1970' },
{ '%Od', 1, '01' },
{ '%oe', 0, 'oe' },
{ '%OH', 1, '03' },
{ '%OI', 1, '03' },
{ '%Om', 1, '01' },
{ '%OM', 1, '00' },
{ '%OS', 1, '00' },
{ '%Ou', 1, '4' },
{ '%OU', 1, '00' },
{ '%OV', 0, '01' },
{ '%Ow', 1, '4' },
{ '%OW', 1, '00' },
{ '%Oy', 1, '70' },
{ '%e', 1, ' 1' },
{ '%F', 1, '1970-01-01' },
{ '%Y-%m-%d', 1, '1970-01-01' },
{ '%H', 1, '03' },
{ '%I', 1, '03' },
{ '%j', 1, '001' },
{ '%k', 1, ' 3' },
{ '%l', 1, ' 3' },
{ '%M', 1, '00' },
{ '%m', 1, '01' },
{ '%n', 1, '\n' },
{ '%p', 1, 'AM' },
{ '%R', 1, '03:00' },
{ '%H:%M', 1, '03:00' },
{ '%r', 1, '03:00:00 AM' },
{ '%I:%M:%S %p', 1, '03:00:00 AM' },
{ '%S', 1, '00' },
{ '%s', 1, '10800' },
{ '%f', 1, '125' },
{ '%3f', 0, '125' },
{ '%6f', 0, '125000' },
{ '%6d', 0, '6d' },
{ '%3D', 0, '3D' },
{ '%T', 1, '03:00:00' },
{ '%H:%M:%S', 1, '03:00:00' },
{ '%t', 1, '\t' },
{ '%U', 1, '00' },
{ '%u', 1, '4' },
{ '%V', 0, '01' },
{ '%G', 1, '1970' },
{ '%g', 1, '70' },
{ '%v', 1, ' 1-Jan-1970' },
{ '%e-%b-%Y', 1, ' 1-Jan-1970' },
{ '%W', 1, '00' },
{ '%w', 1, '4' },
{ '%X', 1, '03:00:00' },
{ '%x', 1, '01/01/70' },
{ '%y', 1, '70' },
{ '%Y', 1, '1970' },
{ '%z', 1, '+0300' },
{ '%%', 1, '%' },
{ '%Y-%m-%dT%H:%M:%S.%9f%z', 1, '1970-01-01T03:00:00.125000000+0300' },
{ '%Y-%m-%dT%H:%M:%S.%f%z', 1, '1970-01-01T03:00:00.125+0300' },
{ '%Y-%m-%dT%H:%M:%S.%f', 1, '1970-01-01T03:00:00.125' },
{ '%FT%T.%f', 1, '1970-01-01T03:00:00.125' },
{ '%FT%T.%f%z', 1, '1970-01-01T03:00:00.125+0300' },
{ '%FT%T.%9f%z', 1, '1970-01-01T03:00:00.125000000+0300' },
}
test:test("Datetime string formatting detailed", function(test)
test:plan(77)
local T = date.new{ timestamp = 0.125 }
T:set{ hour = 3, tzoffset = 180 }
test:is(tostring(T), '1970-01-01T03:00:00.125+0300', 'tostring()')
for _, row in pairs(strftime_formats) do
local fmt, _, value = unpack(row)
test:is(T:format(fmt), value,
('format %s, expected %s'):format(fmt, value))
end
end)
test:test("Datetime string parsing by format (detailed)", function(test)
test:plan(68)
local T = date.new{ timestamp = 0.125 }
T:set{ hour = 3, tzoffset = 180 }
test:is(tostring(T), '1970-01-01T03:00:00.125+0300', 'tostring()')
for _, row in pairs(strftime_formats) do
local fmt, check, value = unpack(row)
if check > 0 then
local res = date.parse(value, {format = fmt})
test:is(res ~= nil, true, ('parse of %s'):format(fmt))
end
end
end)
test:test("__index functions()", function(test)
test:plan(16)
-- 2000-01-29T03:30:12 MSK'
local ts = date.new{sec = 12, min = 30, hour = 3,
tzoffset = 0, day = 29, month = 1, year = 2000,
nsec = 123000000, tz = 'MSK'}
test:is(ts.year, 2000, 'ts.year')
test:is(ts.yday, 29, 'ts.yday')
test:is(ts.month, 1, 'ts.month')
test:is(ts.day, 29, 'ts.day')
test:is(ts.wday, 7, 'ts.wday')
test:is(ts.min, 30, 'ts.min')
test:is(ts.hour, 3, 'ts.hour')
test:is(ts.min, 30, 'ts.min')
test:is(ts.sec, 12, 'ts.sec')
test:is(ts.isdst, false, "ts.isdst")
test:is(ts.tzoffset, 180, "ts.tzoffset")
test:is(ts.timestamp, 949105812.123, "ts.timestamp")
test:is(ts.nsec, 123000000, 'ts.nsec')
test:is(ts.usec, 123000, 'ts.usec')
test:is(ts.msec, 123, 'ts.msec')
test:is(ts.tz, 'MSK', 'ts.tz')
end)
test:test("Time interval tostring()", function(test)
test:plan(19)
local ivals = {
{{sec = 1}, '+1 seconds'},
{{sec = 49}, '+49 seconds'},
{{min = 10, sec = 30}, '+10 minutes, 30 seconds'},
{{hour = 1, min = 59, sec = 10}, '+1 hours, 59 minutes, 10 seconds'},
{{hour = 12, min = 10, sec = 30}, '+12 hours, 10 minutes, 30 seconds'},
{{day = 2, hour = 8, min = 10, sec = 30},
'+2 days, 8 hours, 10 minutes, 30 seconds'},
{{week = 10, hour = 8, min = 10, sec = 30},
'+10 weeks, 8 hours, 10 minutes, 30 seconds'},
{{month = 20, week = 10, hour = 8, min = 10, sec = 30},
'+20 months, 10 weeks, 8 hours, 10 minutes, 30 seconds'},
{{year = 10, month = 20, week = 10, hour = 8, min = 10, sec = 30},
'+10 years, 20 months, 10 weeks, 8 hours, 10 minutes, 30 seconds'},
{{year = 1e4, month = 20, week = 10, hour = 8, min = 10, sec = 30},
'+10000 years, 20 months, 10 weeks, 8 hours, 10 minutes, 30 seconds'},
{{year = 5e6, month = 20, week = 10, hour = 8, min = 10, sec = 30},
'+5000000 years, 20 months, 10 weeks, 8 hours, 10 minutes, 30 seconds'},
{{year = -5e6, month = -20, week = -10, hour = -8, min = -10, sec = -30},
'-5000000 years, -20 months, -10 weeks, -8 hours, -10 minutes, -30 seconds'},
{{month = -20, week = -10, hour = -8, min = -10, sec = -30},
'-20 months, -10 weeks, -8 hours, -10 minutes, -30 seconds'},
{{week = -10, hour = -8, min = -10, sec = -30},
'-10 weeks, -8 hours, -10 minutes, -30 seconds'},
{{hour = -12, min = -10, sec = -30}, '-12 hours, -10 minutes, -30 seconds'},
{{hour = -1, min = -59, sec = -10}, '-1 hours, -59 minutes, -10 seconds'},
{{min = -10, sec = -30}, '-10 minutes, -30 seconds'},