summaryrefslogtreecommitdiff
path: root/test/ruby/test_time.rb
blob: 445518ab57915ff9b01f9cf11dc9f09b880abbf9 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
# frozen_string_literal: false
require 'test/unit'
require 'delegate'
require 'timeout'
require 'delegate'

class TestTime < Test::Unit::TestCase
  def setup
    @verbose = $VERBOSE
  end

  def teardown
    $VERBOSE = @verbose
  end

  def in_timezone(zone)
    orig_zone = ENV['TZ']

    ENV['TZ'] = zone
    yield
  ensure
    ENV['TZ'] = orig_zone
  end

  def no_leap_seconds?
    # 1972-06-30T23:59:60Z is the first leap second.
    Time.utc(1972, 7, 1, 0, 0, 0) - Time.utc(1972, 6, 30, 23, 59, 59) == 1
  end

  def get_t2000
    if no_leap_seconds?
      # Sat Jan 01 00:00:00 UTC 2000
      Time.at(946684800).gmtime
    else
      Time.utc(2000, 1, 1)
    end
  end

  def test_new
    assert_equal(Time.new(2000,1,1,0,0,0), Time.new(2000))
    assert_equal(Time.new(2000,2,1,0,0,0), Time.new("2000", "Feb"))
    assert_equal(Time.utc(2000,2,10), Time.new(2000,2,10, 11,0,0, 3600*11))
    assert_equal(Time.utc(2000,2,10), Time.new(2000,2,9, 13,0,0, -3600*11))
    assert_equal(Time.utc(2000,2,29,23,0,0), Time.new(2000, 3, 1, 0, 0, 0, 3600))
    assert_equal(Time.utc(2000,2,10), Time.new(2000,2,10, 11,0,0, "+11:00"))
    assert_equal(Rational(1,2), Time.new(2000,2,10, 11,0,5.5, "+11:00").subsec)
    bug4090 = '[ruby-dev:42631]'
    tm = [2001,2,28,23,59,30]
    t = Time.new(*tm, "-12:00")
    assert_equal([2001,2,28,23,59,30,-43200], [t.year, t.month, t.mday, t.hour, t.min, t.sec, t.gmt_offset], bug4090)
    assert_raise(ArgumentError) { Time.new(2000,1,1, 0,0,0, "+01:60") }
    msg = /invalid value for Integer/
    assert_raise_with_message(ArgumentError, msg) { Time.new(2021, 1, 1, "+09:99") }
    assert_raise_with_message(ArgumentError, msg) { Time.new(2021, 1, "+09:99") }
    assert_raise_with_message(ArgumentError, msg) { Time.new(2021, "+09:99") }
  end

  def test_time_add()
    assert_equal(Time.utc(2000, 3, 21, 3, 30) + 3 * 3600,
                 Time.utc(2000, 3, 21, 6, 30))
    assert_equal(Time.utc(2000, 3, 21, 3, 30) + (-3 * 3600),
                 Time.utc(2000, 3, 21, 0, 30))
    assert_equal(0, (Time.at(1.1) + 0.9).usec)

    assert_predicate((Time.utc(2000, 4, 1) + 24), :utc?)
    assert_not_predicate((Time.local(2000, 4, 1) + 24), :utc?)

    t = Time.new(2000, 4, 1, 0, 0, 0, "+01:00") + 24
    assert_not_predicate(t, :utc?)
    assert_equal(3600, t.utc_offset)
    t = Time.new(2000, 4, 1, 0, 0, 0, "+02:00") + 24
    assert_not_predicate(t, :utc?)
    assert_equal(7200, t.utc_offset)
  end

  def test_time_subt()
    assert_equal(Time.utc(2000, 3, 21, 3, 30) - 3 * 3600,
                 Time.utc(2000, 3, 21, 0, 30))
    assert_equal(Time.utc(2000, 3, 21, 3, 30) - (-3 * 3600),
                 Time.utc(2000, 3, 21, 6, 30))
    assert_equal(900000, (Time.at(1.1) - 0.2).usec)
  end

  def test_time_time()
    assert_equal(Time.utc(2000, 3, 21, 3, 30)  \
                -Time.utc(2000, 3, 21, 0, 30), 3*3600)
    assert_equal(Time.utc(2000, 3, 21, 0, 30)  \
                -Time.utc(2000, 3, 21, 3, 30), -3*3600)
  end

  def negative_time_t?
    begin
      Time.at(-1)
      true
    rescue ArgumentError
      false
    end
  end

  def test_timegm
    if negative_time_t?
      assert_equal(-0x80000000, Time.utc(1901, 12, 13, 20, 45, 52).tv_sec)
      assert_equal(-2, Time.utc(1969, 12, 31, 23, 59, 58).tv_sec)
      assert_equal(-1, Time.utc(1969, 12, 31, 23, 59, 59).tv_sec)
    end

    assert_equal(0, Time.utc(1970, 1, 1, 0, 0, 0).tv_sec) # the Epoch
    assert_equal(1, Time.utc(1970, 1, 1, 0, 0, 1).tv_sec)
    assert_equal(31535999, Time.utc(1970, 12, 31, 23, 59, 59).tv_sec)
    assert_equal(31536000, Time.utc(1971, 1, 1, 0, 0, 0).tv_sec)
    assert_equal(78796799, Time.utc(1972, 6, 30, 23, 59, 59).tv_sec)

    if no_leap_seconds?
      assert_equal(78796800, Time.utc(1972, 7, 1, 0, 0, 0).tv_sec)
      assert_equal(78796801, Time.utc(1972, 7, 1, 0, 0, 1).tv_sec)
      assert_equal(946684800, Time.utc(2000, 1, 1, 0, 0, 0).tv_sec)

      # Giveup to try 2nd test because some state is changed.
      omit if Test::Unit::Runner.current_repeat_count > 0

      assert_equal(0x7fffffff, Time.utc(2038, 1, 19, 3, 14, 7).tv_sec)
      assert_equal(0x80000000, Time.utc(2038, 1, 19, 3, 14, 8).tv_sec)
    else
      assert_equal(2, Time.utc(1972, 7, 1, 0, 0, 0) - Time.utc(1972, 6, 30, 23, 59, 59))
      assert_equal(78796800, Time.utc(1972, 6, 30, 23, 59, 60).tv_sec)
      assert_equal(78796801, Time.utc(1972, 7, 1, 0, 0, 0).tv_sec)
      assert_equal(78796802, Time.utc(1972, 7, 1, 0, 0, 1).tv_sec)
      assert_equal(946684822, Time.utc(2000, 1, 1, 0, 0, 0).tv_sec)
    end
  end

  def test_strtime
    t = nil
    assert_nothing_raised { t = Time.utc("2000", "1", "2" , "3", "4", "5") }
    assert_equal(Time.utc(2000,1,2,3,4,5), t)
  end

  def test_huge_difference
    if negative_time_t?
      assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) - 0xffffffff, "[ruby-dev:22619]")
      assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) + (-0xffffffff))
      assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) + 0xffffffff, "[ruby-dev:22619]")
      assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) - (-0xffffffff))
    end
  end

  def test_big_minus
    begin
      bigtime0 = Time.at(2**60)
      bigtime1 = Time.at(2**60+1)
    rescue RangeError
      return
    end
    assert_equal(1.0, bigtime1 - bigtime0)
  end

  def test_at
    assert_equal(100000, Time.at("0.1".to_r).usec)
    assert_equal(10000, Time.at("0.01".to_r).usec)
    assert_equal(1000, Time.at("0.001".to_r).usec)
    assert_equal(100, Time.at("0.0001".to_r).usec)
    assert_equal(10, Time.at("0.00001".to_r).usec)
    assert_equal(1, Time.at("0.000001".to_r).usec)
    assert_equal(100000000, Time.at("0.1".to_r).nsec)
    assert_equal(10000000, Time.at("0.01".to_r).nsec)
    assert_equal(1000000, Time.at("0.001".to_r).nsec)
    assert_equal(100000, Time.at("0.0001".to_r).nsec)
    assert_equal(10000, Time.at("0.00001".to_r).nsec)
    assert_equal(1000, Time.at("0.000001".to_r).nsec)
    assert_equal(100, Time.at("0.0000001".to_r).nsec)
    assert_equal(10, Time.at("0.00000001".to_r).nsec)
    assert_equal(1, Time.at("0.000000001".to_r).nsec)
    assert_equal(100000, Time.at(0.1).usec)
    assert_equal(10000, Time.at(0.01).usec)
    assert_equal(1000, Time.at(0.001).usec)
    assert_equal(100, Time.at(0.0001).usec)
    assert_equal(10, Time.at(0.00001).usec)
    assert_equal(3, Time.at(0.000003).usec)
    assert_equal(100000000, Time.at(0.1).nsec)
    assert_equal(10000000, Time.at(0.01).nsec)
    assert_equal(1000000, Time.at(0.001).nsec)
    assert_equal(100000, Time.at(0.0001).nsec)
    assert_equal(10000, Time.at(0.00001).nsec)
    assert_equal(3000, Time.at(0.000003).nsec)
    assert_equal(200, Time.at(0.0000002r).nsec)
    assert_in_delta(200, Time.at(0.0000002).nsec, 1, "should be within FP error")
    assert_equal(10, Time.at(0.00000001).nsec)
    assert_equal(1, Time.at(0.000000001).nsec)

    assert_equal(0, Time.at(1e-10).nsec)
    assert_equal(0, Time.at(4e-10).nsec)
    assert_equal(0, Time.at(6e-10).nsec)
    assert_equal(1, Time.at(14e-10).nsec)
    assert_equal(1, Time.at(16e-10).nsec)
    if negative_time_t?
      assert_equal(999999999, Time.at(-1e-10).nsec)
      assert_equal(999999999, Time.at(-4e-10).nsec)
      assert_equal(999999999, Time.at(-6e-10).nsec)
      assert_equal(999999998, Time.at(-14e-10).nsec)
      assert_equal(999999998, Time.at(-16e-10).nsec)
    end

    t = Time.at(-4611686019).utc
    assert_equal(1823, t.year)

    t = Time.at(4611686018, 999999).utc
    assert_equal(2116, t.year)
    assert_equal("0.999999".to_r, t.subsec)

    t = Time.at(2**40 + "1/3".to_r, 9999999999999).utc
    assert_equal(36812, t.year)

    t = Time.at(-0x3fff_ffff_ffff_ffff)
    assert_equal(-146138510344, t.year)
    t = Time.at(-0x4000_0000_0000_0000)
    assert_equal(-146138510344, t.year)
    t = Time.at(-0x4000_0000_0000_0001)
    assert_equal(-146138510344, t.year)
    t = Time.at(-0x5000_0000_0000_0001)
    assert_equal(-182673138422, t.year)
    t = Time.at(-0x6000_0000_0000_0000)
    assert_equal(-219207766501, t.year)

    t = Time.at(0).utc
    assert_equal([1970,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
    t = Time.at(-86400).utc
    assert_equal([1969,12,31, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
    t = Time.at(-86400 * (400 * 365 + 97)).utc
    assert_equal([1970-400,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
    t = Time.at(-86400 * (400 * 365 + 97)*1000).utc
    assert_equal([1970-400*1000,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
    t = Time.at(-86400 * (400 * 365 + 97)*2421).utc
    assert_equal([1970-400*2421,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
    t = Time.at(-86400 * (400 * 365 + 97)*1000000).utc
    assert_equal([1970-400*1000000,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])

    t = Time.at(-30613683110400).utc
    assert_equal([-968139,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
    t = Time.at(-30613683110401).utc
    assert_equal([-968140,12,31, 23,59,59], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  end

  def test_at2
    assert_equal(100, Time.at(0, 0.1).nsec)
    assert_equal(10, Time.at(0, 0.01).nsec)
    assert_equal(1, Time.at(0, 0.001).nsec)
  end

  def test_at_splat
    assert_equal(Time.at(1, 2), Time.at(*[1, 2]))
  end

  def test_at_with_unit
    assert_equal(123456789, Time.at(0, 123456789, :nanosecond).nsec)
    assert_equal(123456789, Time.at(0, 123456789, :nsec).nsec)
    assert_equal(123456000, Time.at(0, 123456, :microsecond).nsec)
    assert_equal(123456000, Time.at(0, 123456, :usec).nsec)
    assert_equal(123000000, Time.at(0, 123, :millisecond).nsec)
    assert_raise(ArgumentError){ Time.at(0, 1, 2) }
    assert_raise(ArgumentError){ Time.at(0, 1, :invalid) }
    assert_raise(ArgumentError){ Time.at(0, 1, nil) }
  end

  def test_at_rational
    assert_equal(1, Time.at(Rational(1,1) / 1000000000).nsec)
    assert_equal(1, Time.at(1167609600 + Rational(1,1) / 1000000000).nsec)
  end

  def test_utc_subsecond
    assert_equal(500000, Time.utc(2007,1,1,0,0,1.5).usec)
    assert_equal(100000, Time.utc(2007,1,1,0,0,Rational(11,10)).usec)
  end

  def test_eq_nsec
    assert_equal(Time.at(0, 0.123), Time.at(0, 0.123))
    assert_not_equal(Time.at(0, 0.123), Time.at(0, 0.124))
  end

  def assert_marshal_roundtrip(t)
    iv_names = t.instance_variables
    iv_vals1 = iv_names.map {|n| t.instance_variable_get n }
    m = Marshal.dump(t)
    t2 = Marshal.load(m)
    iv_vals2 = iv_names.map {|n| t2.instance_variable_get n }
    assert_equal(t, t2)
    assert_equal(iv_vals1, iv_vals2)
    t2
  end

  def test_marshal_nsec
    assert_marshal_roundtrip(Time.at(0, 0.123))
    assert_marshal_roundtrip(Time.at(0, 0.120))
  end

  def test_marshal_nsec_191
    # generated by ruby 1.9.1p376
    m = "\x04\bIu:\tTime\r \x80\x11\x80@\xE2\x01\x00\x06:\rsubmicro\"\ax\x90"
    t = Marshal.load(m)
    assert_equal(Time.at(Rational(123456789, 1000000000)), t, "[ruby-dev:40133]")
  end

  def test_marshal_rational
    assert_marshal_roundtrip(Time.at(0, Rational(1,3)))
    assert_not_match(/Rational/, Marshal.dump(Time.at(0, Rational(1,3))))
  end

  def test_marshal_ivar
    t = Time.at(123456789, 987654.321)
    t.instance_eval { @var = 135 }
    assert_marshal_roundtrip(t)
    assert_marshal_roundtrip(Marshal.load(Marshal.dump(t)))
  end

  def test_marshal_timezone
    bug = '[ruby-dev:40063]'

    t1 = Time.gm(2000)
    m = Marshal.dump(t1.getlocal("-02:00"))
    t2 = Marshal.load(m)
    assert_equal(t1, t2)
    assert_equal(-7200, t2.utc_offset, bug)
    m = Marshal.dump(t1.getlocal("+08:15"))
    t2 = Marshal.load(m)
    assert_equal(t1, t2)
    assert_equal(29700, t2.utc_offset, bug)
  end

  def test_marshal_zone
    t = Time.utc(2013, 2, 24)
    assert_equal('UTC', t.zone)
    assert_equal('UTC', Marshal.load(Marshal.dump(t)).zone)

    in_timezone('JST-9') do
      t = Time.local(2013, 2, 24)
      assert_equal('JST', Time.local(2013, 2, 24).zone)
      t = Marshal.load(Marshal.dump(t))
      assert_equal('JST', t.zone)
      assert_equal('JST', (t+1).zone, '[ruby-core:81892] [Bug #13710]')
    end
  end

  def test_marshal_zone_gc
    assert_separately(%w(--disable-gems), <<-'end;', timeout: 30)
      ENV["TZ"] = "JST-9"
      s = Marshal.dump(Time.now)
      t = Marshal.load(s)
      n = 0
      done = 100000
      while t.zone.dup == "JST" && n < done
        n += 1
      end
      assert_equal done, n, "Bug #9652"
      assert_equal "JST", t.zone, "Bug #9652"
    end;
  end

  def test_marshal_to_s
    t1 = Time.new(2011,11,8, 0,42,25, 9*3600)
    t2 = Time.at(Marshal.load(Marshal.dump(t1)))
    assert_equal("2011-11-08 00:42:25 +0900", t2.to_s,
      "[ruby-dev:44827] [Bug #5586]")
  end

  Bug8795 = '[ruby-core:56648] [Bug #8795]'

  def test_marshal_broken_offset
    data = "\x04\bIu:\tTime\r\xEFF\x1C\x80\x00\x00\x00\x00\x06:\voffset"
    t1 = t2 = nil
    in_timezone('UTC') do
      assert_nothing_raised(TypeError, ArgumentError, Bug8795) do
        t1 = Marshal.load(data + "T")
        t2 = Marshal.load(data + "\"\x0ebadoffset")
      end
      assert_equal(0, t1.utc_offset)
      assert_equal(0, t2.utc_offset)
    end
  end

  def test_marshal_broken_zone
    data = "\x04\bIu:\tTime\r\xEFF\x1C\x80\x00\x00\x00\x00\x06:\tzone"
    t1 = t2 = nil
    in_timezone('UTC') do
      assert_nothing_raised(TypeError, ArgumentError, Bug8795) do
        t1 = Marshal.load(data + "T")
        t2 = Marshal.load(data + "\"\b\0\0\0")
      end
      assert_equal('UTC', t1.zone)
      assert_equal('UTC', t2.zone)
    end
  end

  def test_marshal_broken_month
    data = "\x04\x08u:\tTime\r\x20\x7c\x1e\xc0\x00\x00\x00\x00"
    assert_equal(Time.utc(2022, 4, 1), Marshal.load(data))
  end

  def test_marshal_distant_past
    assert_marshal_roundtrip(Time.utc(1890, 1, 1))
    assert_marshal_roundtrip(Time.utc(-4.5e9, 1, 1))
  end

  def test_marshal_distant_future
    assert_marshal_roundtrip(Time.utc(30000, 1, 1))
    assert_marshal_roundtrip(Time.utc(5.67e9, 4, 8))
  end

  def test_at3
    t2000 = get_t2000
    assert_equal(t2000, Time.at(t2000))
#    assert_raise(RangeError) do
#      Time.at(2**31-1, 1_000_000)
#      Time.at(2**63-1, 1_000_000)
#    end
#    assert_raise(RangeError) do
#      Time.at(-2**31, -1_000_000)
#      Time.at(-2**63, -1_000_000)
#    end
  end

  def test_utc_or_local
    t2000 = get_t2000
    assert_equal(t2000, Time.gm(2000))
    assert_equal(t2000, Time.gm(0, 0, 0, 1, 1, 2000, :foo, :bar, false, :baz))
    assert_equal(t2000, Time.gm(2000, "jan"))
    assert_equal(t2000, Time.gm(2000, "1"))
    assert_equal(t2000, Time.gm(2000, 1, 1, 0, 0, 0, 0))
    assert_equal(t2000, Time.gm(2000, 1, 1, 0, 0, 0, "0"))
    assert_equal(t2000, Time.gm(2000, 1, 1, 0, 0, "0", :foo, :foo))
    assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -1, :foo, :foo) }
    assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -1.0, :foo, :foo) }
    assert_raise(RangeError) do
      Time.gm(2000, 1, 1, 0, 0, 10_000_000_000_000_000_001.0, :foo, :foo)
    end
    assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -(2**31), :foo, :foo) }
    o = Object.new
    def o.to_int; 0; end
    def o.to_r; nil; end
    assert_raise(TypeError) { Time.gm(2000, 1, 1, 0, 0, o, :foo, :foo) }
    class << o; remove_method(:to_r); end
    def o.to_r; ""; end
    assert_raise(TypeError) { Time.gm(2000, 1, 1, 0, 0, o, :foo, :foo) }
    class << o; remove_method(:to_r); end
    def o.to_r; Rational(11); end
    assert_equal(11, Time.gm(2000, 1, 1, 0, 0, o).sec)
    o = Object.new
    def o.to_int; 10; end
    assert_equal(10, Time.gm(2000, 1, 1, 0, 0, o).sec)
    assert_raise(ArgumentError) { Time.gm(2000, 13) }

    t = Time.local(2000)
    assert_equal(t.gmt_offset, t2000 - t)

    assert_equal(-4427700000, Time.utc(-4427700000,12,1).year)
    assert_equal(-2**30+10, Time.utc(-2**30+10,1,1).year)

    assert_raise(ArgumentError) { Time.gm(2000, 1, -1) }
    assert_raise(ArgumentError) { Time.gm(2000, 1, 2**30 + 1) }
    assert_raise(ArgumentError) { Time.gm(2000, 1, -2**30 + 1) }
  end

  def test_time_interval
    m = Thread::Mutex.new.lock
    assert_nothing_raised {
      Timeout.timeout(10) {
        m.sleep(0)
      }
    }
    assert_raise(ArgumentError) { m.sleep(-1) }
    assert_raise(TypeError) { m.sleep("") }
    assert_raise(TypeError) { sleep("") }
    obj = eval("class C\u{1f5ff}; self; end").new
    assert_raise_with_message(TypeError, /C\u{1f5ff}/) {m.sleep(obj)}
    assert_raise_with_message(TypeError, /C\u{1f5ff}/) {sleep(obj)}
  end

  def test_to_f
    t2000 = Time.at(946684800).gmtime
    assert_equal(946684800.0, t2000.to_f)
  end

  def test_to_f_accuracy
    # https://bugs.ruby-lang.org/issues/10135#note-1
    f = 1381089302.195
    assert_equal(f, Time.at(f).to_f, "[ruby-core:64373] [Bug #10135] note-1")
  end

  def test_cmp
    t2000 = get_t2000
    assert_equal(-1, t2000 <=> Time.gm(2001))
    assert_equal(1, t2000 <=> Time.gm(1999))
    assert_nil(t2000 <=> 0)
  end

  def test_eql
    t2000 = get_t2000
    assert_operator(t2000, :eql?, t2000)
    assert_not_operator(t2000, :eql?, Time.gm(2001))
  end

  def test_utc_p
    assert_predicate(Time.gm(2000), :gmt?)
    assert_not_predicate(Time.local(2000), :gmt?)
    assert_not_predicate(Time.at(0), :gmt?)
  end

  def test_hash
    t2000 = get_t2000
    assert_kind_of(Integer, t2000.hash)
  end

  def test_reinitialize
    bug8099 = '[ruby-core:53436] [Bug #8099]'
    t2000 = get_t2000
    assert_raise(TypeError, bug8099) {
      t2000.send(:initialize, 2013, 03, 14)
    }
    assert_equal(get_t2000, t2000, bug8099)
  end

  def test_init_copy
    t2000 = get_t2000
    assert_equal(t2000, t2000.dup)
    assert_raise(TypeError) do
      t2000.instance_eval { initialize_copy(nil) }
    end
  end

  def test_localtime_gmtime
    assert_nothing_raised do
      t = Time.gm(2000)
      assert_predicate(t, :gmt?)
      t.localtime
      assert_not_predicate(t, :gmt?)
      t.localtime
      assert_not_predicate(t, :gmt?)
      t.gmtime
      assert_predicate(t, :gmt?)
      t.gmtime
      assert_predicate(t, :gmt?)
    end

    t1 = Time.gm(2000)
    t2 = t1.getlocal
    assert_equal(t1, t2)
    t3 = t1.getlocal("-02:00")
    assert_equal(t1, t3)
    assert_equal(-7200, t3.utc_offset)
    assert_equal([1999, 12, 31, 22, 0, 0], [t3.year, t3.mon, t3.mday, t3.hour, t3.min, t3.sec])
    t1.localtime
    assert_equal(t1, t2)
    assert_equal(t1.gmt?, t2.gmt?)
    assert_equal(t1, t3)

    t1 = Time.local(2000)
    t2 = t1.getgm
    assert_equal(t1, t2)
    t3 = t1.getlocal("-02:00")
    assert_equal(t1, t3)
    assert_equal(-7200, t3.utc_offset)
    t1.gmtime
    assert_equal(t1, t2)
    assert_equal(t1.gmt?, t2.gmt?)
    assert_equal(t1, t3)
  end

  def test_asctime
    t2000 = get_t2000
    assert_equal("Sat Jan  1 00:00:00 2000", t2000.asctime)
    assert_equal(Encoding::US_ASCII, t2000.asctime.encoding)
    assert_kind_of(String, Time.at(0).asctime)
  end

  def test_to_s
    t2000 = get_t2000
    assert_equal("2000-01-01 00:00:00 UTC", t2000.to_s)
    assert_equal(Encoding::US_ASCII, t2000.to_s.encoding)
    assert_kind_of(String, Time.at(946684800).getlocal.to_s)
    assert_equal(Time.at(946684800).getlocal.to_s, Time.at(946684800).to_s)
  end

  def test_inspect
    t2000 = get_t2000
    assert_equal("2000-01-01 00:00:00 UTC", t2000.inspect)
    assert_equal(Encoding::US_ASCII, t2000.inspect.encoding)
    assert_kind_of(String, Time.at(946684800).getlocal.inspect)
    assert_equal(Time.at(946684800).getlocal.inspect, Time.at(946684800).inspect)

    t2000 = get_t2000 + 1/10r
    assert_equal("2000-01-01 00:00:00.1 UTC", t2000.inspect)
    t2000 = get_t2000 + 1/1000000000r
    assert_equal("2000-01-01 00:00:00.000000001 UTC", t2000.inspect)
    t2000 = get_t2000 + 1/10000000000r
    assert_equal("2000-01-01 00:00:00 1/10000000000 UTC", t2000.inspect)
    t2000 = get_t2000 + 0.1
    assert_equal("2000-01-01 00:00:00 3602879701896397/36028797018963968 UTC", t2000.inspect)

    t2000 = get_t2000
    t2000 = t2000.localtime(9*3600)
    assert_equal("2000-01-01 09:00:00 +0900", t2000.inspect)

    t2000 = get_t2000.localtime(9*3600) + 1/10r
    assert_equal("2000-01-01 09:00:00.1 +0900", t2000.inspect)

    t2000 = get_t2000
    assert_equal("2000-01-01 09:12:00 +0912", t2000.localtime(9*3600+12*60).inspect)
    assert_equal("2000-01-01 09:12:34 +091234", t2000.localtime(9*3600+12*60+34).inspect)
  end

  def assert_zone_encoding(time)
    zone = time.zone
    assert_predicate(zone, :valid_encoding?)
    if zone.ascii_only?
      assert_equal(Encoding::US_ASCII, zone.encoding)
    else
      enc = Encoding.default_internal || Encoding.find('locale')
      assert_equal(enc, zone.encoding)
    end
  end

  def test_zone
    assert_zone_encoding Time.now
    t = Time.now.utc
    assert_equal("UTC", t.zone)
    assert_nil(t.getlocal(0).zone)
    assert_nil(t.getlocal("+02:00").zone)
  end

  def test_plus_minus
    t2000 = get_t2000
    # assert_raise(RangeError) { t2000 + 10000000000 }
    # assert_raise(RangeError)  t2000 - 3094168449 }
    # assert_raise(RangeError) { t2000 + 1200798848 }
    assert_raise(TypeError) { t2000 + Time.now }
  end

  def test_plus_type
    t0 = Time.utc(2000,1,1)
    n0 = t0.to_i
    n1 = n0+1
    t1 = Time.at(n1)
    assert_equal(t1, t0 + 1)
    assert_equal(t1, t0 + 1.0)
    assert_equal(t1, t0 + Rational(1,1))
    assert_equal(t1, t0 + SimpleDelegator.new(1))
    assert_equal(t1, t0 + SimpleDelegator.new(1.0))
    assert_equal(t1, t0 + SimpleDelegator.new(Rational(1,1)))
    assert_raise(TypeError) { t0 + nil }
    assert_raise(TypeError) { t0 + "1" }
    assert_raise(TypeError) { t0 + SimpleDelegator.new("1") }
    assert_equal(0.5, (t0 + 1.5).subsec)
    assert_equal(Rational(1,3), (t0 + Rational(4,3)).subsec)
    assert_equal(0.5, (t0 + SimpleDelegator.new(1.5)).subsec)
    assert_equal(Rational(1,3), (t0 + SimpleDelegator.new(Rational(4,3))).subsec)
  end

  def test_minus
    t = Time.at(-4611686018).utc - 100
    assert_equal(1823, t.year)
  end

  def test_readers
    t2000 = get_t2000
    assert_equal(0, t2000.sec)
    assert_equal(0, t2000.min)
    assert_equal(0, t2000.hour)
    assert_equal(1, t2000.mday)
    assert_equal(1, t2000.mon)
    assert_equal(2000, t2000.year)
    assert_equal(6, t2000.wday)
    assert_equal(1, t2000.yday)
    assert_equal(false, t2000.isdst)
    assert_equal("UTC", t2000.zone)
    assert_zone_encoding(t2000)
    assert_equal(0, t2000.gmt_offset)
    assert_not_predicate(t2000, :sunday?)
    assert_not_predicate(t2000, :monday?)
    assert_not_predicate(t2000, :tuesday?)
    assert_not_predicate(t2000, :wednesday?)
    assert_not_predicate(t2000, :thursday?)
    assert_not_predicate(t2000, :friday?)
    assert_predicate(t2000, :saturday?)
    assert_equal([0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], t2000.to_a)

    t = Time.at(946684800).getlocal
    assert_equal(t.sec, Time.at(946684800).sec)
    assert_equal(t.min, Time.at(946684800).min)
    assert_equal(t.hour, Time.at(946684800).hour)
    assert_equal(t.mday, Time.at(946684800).mday)
    assert_equal(t.mon, Time.at(946684800).mon)
    assert_equal(t.year, Time.at(946684800).year)
    assert_equal(t.wday, Time.at(946684800).wday)
    assert_equal(t.yday, Time.at(946684800).yday)
    assert_equal(t.isdst, Time.at(946684800).isdst)
    assert_equal(t.zone, Time.at(946684800).zone)
    assert_zone_encoding(Time.at(946684800))
    assert_equal(t.gmt_offset, Time.at(946684800).gmt_offset)
    assert_equal(t.sunday?, Time.at(946684800).sunday?)
    assert_equal(t.monday?, Time.at(946684800).monday?)
    assert_equal(t.tuesday?, Time.at(946684800).tuesday?)
    assert_equal(t.wednesday?, Time.at(946684800).wednesday?)
    assert_equal(t.thursday?, Time.at(946684800).thursday?)
    assert_equal(t.friday?, Time.at(946684800).friday?)
    assert_equal(t.saturday?, Time.at(946684800).saturday?)
    assert_equal(t.to_a, Time.at(946684800).to_a)
  end

  def test_strftime
    t2000 = get_t2000
    t = Time.at(946684800).getlocal
    assert_equal("Sat", t2000.strftime("%a"))
    assert_equal("Saturday", t2000.strftime("%A"))
    assert_equal("Jan", t2000.strftime("%b"))
    assert_equal("January", t2000.strftime("%B"))
    assert_kind_of(String, t2000.strftime("%c"))
    assert_equal("01", t2000.strftime("%d"))
    assert_equal("00", t2000.strftime("%H"))
    assert_equal("12", t2000.strftime("%I"))
    assert_equal("001", t2000.strftime("%j"))
    assert_equal("01", t2000.strftime("%m"))
    assert_equal("00", t2000.strftime("%M"))
    assert_equal("AM", t2000.strftime("%p"))
    assert_equal("00", t2000.strftime("%S"))
    assert_equal("00", t2000.strftime("%U"))
    assert_equal("00", t2000.strftime("%W"))
    assert_equal("6", t2000.strftime("%w"))
    assert_equal("01/01/00", t2000.strftime("%x"))
    assert_equal("00:00:00", t2000.strftime("%X"))
    assert_equal("00", t2000.strftime("%y"))
    assert_equal("2000", t2000.strftime("%Y"))
    assert_equal("UTC", t2000.strftime("%Z"))
    assert_equal("%", t2000.strftime("%%"))
    assert_equal("0", t2000.strftime("%-S"))
    assert_equal("12:00:00 AM", t2000.strftime("%r"))
    assert_equal("Sat 2000-01-01T00:00:00", t2000.strftime("%3a %FT%T"))

    assert_warning(/strftime called with empty format string/) do
      assert_equal("", t2000.strftime(""))
    end
    assert_equal("foo\0bar\x0000\x0000\x0000", t2000.strftime("foo\0bar\0%H\0%M\0%S"))
    assert_equal("foo" * 1000, t2000.strftime("foo" * 1000))

    t = Time.mktime(2000, 1, 1)
    assert_equal("Sat", t.strftime("%a"))
  end

  def test_strftime_subsec
    t = Time.at(946684800, 123456.789)
    assert_equal("123", t.strftime("%3N"))
    assert_equal("123456", t.strftime("%6N"))
    assert_equal("123456789", t.strftime("%9N"))
    assert_equal("1234567890", t.strftime("%10N"))
    assert_equal("123456789", t.strftime("%0N"))
  end

  def test_strftime_sec
    t = get_t2000.getlocal
    assert_equal("000", t.strftime("%3S"))
  end

  def test_strftime_seconds_from_epoch
    t = Time.at(946684800, 123456.789)
    assert_equal("946684800", t.strftime("%s"))
    assert_equal("946684800", t.utc.strftime("%s"))

    t = Time.at(10000000000000000000000)
    assert_equal("<<10000000000000000000000>>", t.strftime("<<%s>>"))
    assert_equal("<<010000000000000000000000>>", t.strftime("<<%24s>>"))
    assert_equal("<<010000000000000000000000>>", t.strftime("<<%024s>>"))
    assert_equal("<< 10000000000000000000000>>", t.strftime("<<%_24s>>"))
  end

  def test_strftime_zone
    t = Time.mktime(2001, 10, 1)
    assert_equal("2001-10-01", t.strftime("%F"))
    assert_equal(Encoding::UTF_8, t.strftime("\u3042%Z").encoding)
    assert_equal(true, t.strftime("\u3042%Z").valid_encoding?)
  end

  def test_strftime_flags
    t = Time.mktime(2001, 10, 1, 2, 0, 0)
    assert_equal("01", t.strftime("%d"))
    assert_equal("01", t.strftime("%0d"))
    assert_equal(" 1", t.strftime("%_d"))
    assert_equal(" 1", t.strftime("%e"))
    assert_equal("01", t.strftime("%0e"))
    assert_equal(" 1", t.strftime("%_e"))
    assert_equal("AM", t.strftime("%p"))
    assert_equal("am", t.strftime("%#p"))
    assert_equal("am", t.strftime("%P"))
    assert_equal("AM", t.strftime("%#P"))
    assert_equal("02", t.strftime("%H"))
    assert_equal("02", t.strftime("%0H"))
    assert_equal(" 2", t.strftime("%_H"))
    assert_equal("02", t.strftime("%I"))
    assert_equal("02", t.strftime("%0I"))
    assert_equal(" 2", t.strftime("%_I"))
    assert_equal(" 2", t.strftime("%k"))
    assert_equal("02", t.strftime("%0k"))
    assert_equal(" 2", t.strftime("%_k"))
    assert_equal(" 2", t.strftime("%l"))
    assert_equal("02", t.strftime("%0l"))
    assert_equal(" 2", t.strftime("%_l"))
    t = Time.mktime(2001, 10, 1, 14, 0, 0)
    assert_equal("PM", t.strftime("%p"))
    assert_equal("pm", t.strftime("%#p"))
    assert_equal("pm", t.strftime("%P"))
    assert_equal("PM", t.strftime("%#P"))
    assert_equal("14", t.strftime("%H"))
    assert_equal("14", t.strftime("%0H"))
    assert_equal("14", t.strftime("%_H"))
    assert_equal("02", t.strftime("%I"))
    assert_equal("02", t.strftime("%0I"))
    assert_equal(" 2", t.strftime("%_I"))
    assert_equal("14", t.strftime("%k"))
    assert_equal("14", t.strftime("%0k"))
    assert_equal("14", t.strftime("%_k"))
    assert_equal(" 2", t.strftime("%l"))
    assert_equal("02", t.strftime("%0l"))
    assert_equal(" 2", t.strftime("%_l"))
    assert_equal("MON", t.strftime("%^a"))
    assert_equal("OCT", t.strftime("%^b"))

    t = get_t2000
    assert_equal("UTC", t.strftime("%^Z"))
    assert_equal("utc", t.strftime("%#Z"))
    assert_equal("SAT JAN  1 00:00:00 2000", t.strftime("%^c"))
  end

  def test_strftime_invalid_flags
    t = Time.mktime(2001, 10, 1, 2, 0, 0)
    assert_equal("%4^p", t.strftime("%4^p"), 'prec after flag')
  end

  def test_strftime_year
    t = Time.utc(1,1,4)
    assert_equal("0001", t.strftime("%Y"))
    assert_equal("0001", t.strftime("%G"))

    t = Time.utc(0,1,4)
    assert_equal("0000", t.strftime("%Y"))
    assert_equal("0000", t.strftime("%G"))

    t = Time.utc(-1,1,4)
    assert_equal("-0001", t.strftime("%Y"))
    assert_equal("-0001", t.strftime("%G"))

    t = Time.utc(10000000000000000000000,1,1)
    assert_equal("<<10000000000000000000000>>", t.strftime("<<%Y>>"))
    assert_equal("<<010000000000000000000000>>", t.strftime("<<%24Y>>"))
    assert_equal("<<010000000000000000000000>>", t.strftime("<<%024Y>>"))
    assert_equal("<< 10000000000000000000000>>", t.strftime("<<%_24Y>>"))
  end

  def test_strftime_weeknum
    # [ruby-dev:37155]
    t = Time.mktime(1970, 1, 18)
    assert_equal("0", t.strftime("%w"))
    assert_equal("7", t.strftime("%u"))
  end

  def test_strftime_ctrlchar
    # [ruby-dev:37160]
    t2000 = get_t2000
    assert_equal("\t", t2000.strftime("%t"))
    assert_equal("\t", t2000.strftime("%0t"))
    assert_equal("\t", t2000.strftime("%1t"))
    assert_equal("  \t", t2000.strftime("%3t"))
    assert_equal("00\t", t2000.strftime("%03t"))
    assert_equal("\n", t2000.strftime("%n"))
    assert_equal("\n", t2000.strftime("%0n"))
    assert_equal("\n", t2000.strftime("%1n"))
    assert_equal("  \n", t2000.strftime("%3n"))
    assert_equal("00\n", t2000.strftime("%03n"))
  end

  def test_strftime_weekflags
    # [ruby-dev:37162]
    t2000 = get_t2000
    assert_equal("SAT", t2000.strftime("%#a"))
    assert_equal("SATURDAY", t2000.strftime("%#A"))
    assert_equal("JAN", t2000.strftime("%#b"))
    assert_equal("JANUARY", t2000.strftime("%#B"))
    assert_equal("JAN", t2000.strftime("%#h"))
    assert_equal("FRIDAY", Time.local(2008,1,4).strftime("%#A"))
  end

  def test_strftime_rational
    t = Time.utc(2000,3,14, 6,53,"58.979323846".to_r) # Pi Day
    assert_equal("03/14/2000  6:53:58.97932384600000000000000000000",
                 t.strftime("%m/%d/%Y %l:%M:%S.%29N"))
    assert_equal("03/14/2000  6:53:58.9793238460",
                 t.strftime("%m/%d/%Y %l:%M:%S.%10N"))
    assert_equal("03/14/2000  6:53:58.979323846",
                 t.strftime("%m/%d/%Y %l:%M:%S.%9N"))
    assert_equal("03/14/2000  6:53:58.97932384",
                 t.strftime("%m/%d/%Y %l:%M:%S.%8N"))

    t = Time.utc(1592,3,14, 6,53,"58.97932384626433832795028841971".to_r) # Pi Day
    assert_equal("03/14/1592  6:53:58.97932384626433832795028841971",
                 t.strftime("%m/%d/%Y %l:%M:%S.%29N"))
    assert_equal("03/14/1592  6:53:58.9793238462",
                 t.strftime("%m/%d/%Y %l:%M:%S.%10N"))
    assert_equal("03/14/1592  6:53:58.979323846",
                 t.strftime("%m/%d/%Y %l:%M:%S.%9N"))
    assert_equal("03/14/1592  6:53:58.97932384",
                 t.strftime("%m/%d/%Y %l:%M:%S.%8N"))
  end

  def test_strftime_far_future
    # [ruby-core:33985]
    assert_equal("3000000000", Time.at(3000000000).strftime('%s'))
  end

  def test_strftime_too_wide
    assert_equal(8192, Time.now.strftime('%8192z').size)
  end

  def test_strftime_wide_precision
    t2000 = get_t2000
    s = t2000.strftime("%28c")
    assert_equal(28, s.size)
    assert_equal(t2000.strftime("%c"), s.strip)
  end

  def test_strfimte_zoneoffset
    t2000 = get_t2000
    t = t2000.getlocal("+09:00:00")
    assert_equal("+0900", t.strftime("%z"))
    assert_equal("+09:00", t.strftime("%:z"))
    assert_equal("+09:00:00", t.strftime("%::z"))
    assert_equal("+09", t.strftime("%:::z"))

    t = t2000.getlocal("+09:00:01")
    assert_equal("+0900", t.strftime("%z"))
    assert_equal("+09:00", t.strftime("%:z"))
    assert_equal("+09:00:01", t.strftime("%::z"))
    assert_equal("+09:00:01", t.strftime("%:::z"))

    assert_equal("+0000", t2000.strftime("%z"))
    assert_equal("-0000", t2000.strftime("%-z"))
    assert_equal("-00:00", t2000.strftime("%-:z"))
    assert_equal("-00:00:00", t2000.strftime("%-::z"))

    t = t2000.getlocal("+00:00")
    assert_equal("+0000", t.strftime("%z"))
    assert_equal("+0000", t.strftime("%-z"))
    assert_equal("+00:00", t.strftime("%-:z"))
    assert_equal("+00:00:00", t.strftime("%-::z"))
  end

  def test_strftime_padding
    bug4458 = '[ruby-dev:43287]'
    t2000 = get_t2000
    t = t2000.getlocal("+09:00")
    assert_equal("+0900", t.strftime("%z"))
    assert_equal("+09:00", t.strftime("%:z"))
    assert_equal("      +900", t.strftime("%_10z"), bug4458)
    assert_equal("+000000900", t.strftime("%10z"), bug4458)
    assert_equal("     +9:00", t.strftime("%_10:z"), bug4458)
    assert_equal("+000009:00", t.strftime("%10:z"), bug4458)
    assert_equal("  +9:00:00", t.strftime("%_10::z"), bug4458)
    assert_equal("+009:00:00", t.strftime("%10::z"), bug4458)
    assert_equal("+000000009", t.strftime("%10:::z"))
    t = t2000.getlocal("-05:00")
    assert_equal("-0500", t.strftime("%z"))
    assert_equal("-05:00", t.strftime("%:z"))
    assert_equal("      -500", t.strftime("%_10z"), bug4458)
    assert_equal("-000000500", t.strftime("%10z"), bug4458)
    assert_equal("     -5:00", t.strftime("%_10:z"), bug4458)
    assert_equal("-000005:00", t.strftime("%10:z"), bug4458)
    assert_equal("  -5:00:00", t.strftime("%_10::z"), bug4458)
    assert_equal("-005:00:00", t.strftime("%10::z"), bug4458)
    assert_equal("-000000005", t.strftime("%10:::z"))

    bug6323 = '[ruby-core:44447]'
    t = t2000.getlocal("+00:36")
    assert_equal("      +036", t.strftime("%_10z"), bug6323)
    assert_equal("+000000036", t.strftime("%10z"), bug6323)
    assert_equal("     +0:36", t.strftime("%_10:z"), bug6323)
    assert_equal("+000000:36", t.strftime("%10:z"), bug6323)
    assert_equal("  +0:36:00", t.strftime("%_10::z"), bug6323)
    assert_equal("+000:36:00", t.strftime("%10::z"), bug6323)
    assert_equal("+000000:36", t.strftime("%10:::z"))
    t = t2000.getlocal("-00:55")
    assert_equal("      -055", t.strftime("%_10z"), bug6323)
    assert_equal("-000000055", t.strftime("%10z"), bug6323)
    assert_equal("     -0:55", t.strftime("%_10:z"), bug6323)
    assert_equal("-000000:55", t.strftime("%10:z"), bug6323)
    assert_equal("  -0:55:00", t.strftime("%_10::z"), bug6323)
    assert_equal("-000:55:00", t.strftime("%10::z"), bug6323)
    assert_equal("-000000:55", t.strftime("%10:::z"))
  end

  def test_strftime_invalid_modifier
    t2000 = get_t2000
    t = t2000.getlocal("+09:00")
    assert_equal("%:y", t.strftime("%:y"), 'invalid conversion after : modifier')
    assert_equal("%:0z", t.strftime("%:0z"), 'flag after : modifier')
    assert_equal("%:10z", t.strftime("%:10z"), 'prec after : modifier')
    assert_equal("%Ob", t.strftime("%Ob"), 'invalid conversion after locale modifier')
    assert_equal("%Eb", t.strftime("%Eb"), 'invalid conversion after locale modifier')
    assert_equal("%O0y", t.strftime("%O0y"), 'flag after locale modifier')
    assert_equal("%E0y", t.strftime("%E0y"), 'flag after locale modifier')
    assert_equal("%O10y", t.strftime("%O10y"), 'prec after locale modifier')
    assert_equal("%E10y", t.strftime("%E10y"), 'prec after locale modifier')
  end

  def test_delegate
    d1 = SimpleDelegator.new(t1 = Time.utc(2000))
    d2 = SimpleDelegator.new(t2 = Time.utc(2001))
    assert_equal(-1, t1 <=> t2)
    assert_equal(1, t2 <=> t1)
    assert_equal(-1, d1 <=> d2)
    assert_equal(1, d2 <=> d1)
  end

  def test_to_r
    assert_kind_of(Rational, Time.new(2000,1,1,0,0,Rational(4,3)).to_r)
    assert_kind_of(Rational, Time.utc(1970).to_r)
  end

  def test_round
    t = Time.utc(1999,12,31, 23,59,59)
    t2 = (t+0.4).round
    assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+0.49).round
    assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+0.5).round
    assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+1.4).round
    assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+1.49).round
    assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+1.5).round
    assert_equal([1,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)

    t2 = (t+0.123456789).round(4)
    assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
    assert_equal(Rational(1235,10000), t2.subsec)

    off = 0.0
    100.times {|i|
      t2 = (t+off).round(1)
      assert_equal(Rational(i % 10, 10), t2.subsec)
      off += 0.1
    }
  end

  def test_floor
    t = Time.utc(1999,12,31, 23,59,59)
    t2 = (t+0.4).floor
    assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+0.49).floor
    assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+0.5).floor
    assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+1.4).floor
    assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+1.49).floor
    assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+1.5).floor
    assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)

    t2 = (t+0.123456789).floor(4)
    assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
    assert_equal(Rational(1234,10000), t2.subsec)
  end

  def test_ceil
    t = Time.utc(1999,12,31, 23,59,59)
    t2 = (t+0.4).ceil
    assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+0.49).ceil
    assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+0.5).ceil
    assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+1.4).ceil
    assert_equal([1,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+1.49).ceil
    assert_equal([1,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)
    t2 = (t+1.5).ceil
    assert_equal([1,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
    assert_equal(0, t2.subsec)

    t2 = (t+0.123456789).ceil(4)
    assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
    assert_equal(Rational(1235,10000), t2.subsec)

    time = Time.utc(2016, 4, 23, 0, 0, 0.123456789r)
    assert_equal(time, time.ceil(9))
    assert_equal(time, time.ceil(10))
    assert_equal(time, time.ceil(11))
  end

  def test_getlocal_dont_share_eigenclass
    bug5012 = "[ruby-dev:44071]"

    t0 = Time.now
    class << t0; end
    t1 = t0.getlocal

    def t0.m
      0
    end

    assert_raise(NoMethodError, bug5012) { t1.m }
  end

  def test_sec_str
    bug6193 = '[ruby-core:43569]'
    t = nil
    assert_nothing_raised(bug6193) {t = Time.new(2012, 1, 2, 3, 4, "5")}
    assert_equal(Time.new(2012, 1, 2, 3, 4, 5), t, bug6193)
  end

  def test_past
    [
      [-(1 << 100), 1, 1, 0, 0, 0],
      [-4000, 1, 1, 0, 0, 0],
      [-3000, 1, 1, 0, 0, 0],
    ].each {|year, mon, day, hour, min, sec|
      t = Time.utc(year, mon, day, hour, min, sec)
      assert_equal(year, t.year)
      assert_equal(mon, t.mon)
      assert_equal(day, t.day)
      assert_equal(hour, t.hour)
      assert_equal(min, t.min)
      assert_equal(sec, t.sec)
    }
  end

  def test_1901
    assert_equal(-0x80000001, Time.utc(1901, 12, 13, 20, 45, 51).tv_sec)
    [
      [1901, 12, 13, 20, 45, 50],
      [1901, 12, 13, 20, 45, 51],
      [1901, 12, 13, 20, 45, 52], # -0x80000000
      [1901, 12, 13, 20, 45, 53],
    ].each {|year, mon, day, hour, min, sec|
      t = Time.utc(year, mon, day, hour, min, sec)
      assert_equal(year, t.year)
      assert_equal(mon, t.mon)
      assert_equal(day, t.day)
      assert_equal(hour, t.hour)
      assert_equal(min, t.min)
      assert_equal(sec, t.sec)
    }
  end

  def test_1970
    assert_equal(0, Time.utc(1970, 1, 1, 0, 0, 0).tv_sec)
    [
      [1969, 12, 31, 23, 59, 59],
      [1970, 1, 1, 0, 0, 0],
      [1970, 1, 1, 0, 0, 1],
    ].each {|year, mon, day, hour, min, sec|
      t = Time.utc(year, mon, day, hour, min, sec)
      assert_equal(year, t.year)
      assert_equal(mon, t.mon)
      assert_equal(day, t.day)
      assert_equal(hour, t.hour)
      assert_equal(min, t.min)
      assert_equal(sec, t.sec)
    }
  end

  def test_2038
    # Giveup to try 2nd test because some state is changed.
    omit if Test::Unit::Runner.current_repeat_count > 0

    if no_leap_seconds?
      assert_equal(0x80000000, Time.utc(2038, 1, 19, 3, 14, 8).tv_sec)
    end
    [
      [2038, 1, 19, 3, 14, 7],
      [2038, 1, 19, 3, 14, 8],
      [2038, 1, 19, 3, 14, 9],
      [2039, 1, 1, 0, 0, 0],
    ].each {|year, mon, day, hour, min, sec|
      t = Time.utc(year, mon, day, hour, min, sec)
      assert_equal(year, t.year)
      assert_equal(mon, t.mon)
      assert_equal(day, t.day)
      assert_equal(hour, t.hour)
      assert_equal(min, t.min)
      assert_equal(sec, t.sec)
    }
    assert_equal(Time.local(2038,3,1), Time.local(2038,2,29))
    assert_equal(Time.local(2038,3,2), Time.local(2038,2,30))
    assert_equal(Time.local(2038,3,3), Time.local(2038,2,31))
    assert_equal(Time.local(2040,2,29), Time.local(2040,2,29))
    assert_equal(Time.local(2040,3,1), Time.local(2040,2,30))
    assert_equal(Time.local(2040,3,2), Time.local(2040,2,31))
    n = 2 ** 64
    n += 400 - n % 400 # n is over 2^64 and multiple of 400
    assert_equal(Time.local(n,2,29),Time.local(n,2,29))
    assert_equal(Time.local(n,3,1), Time.local(n,2,30))
    assert_equal(Time.local(n,3,2), Time.local(n,2,31))
    n += 100
    assert_equal(Time.local(n,3,1), Time.local(n,2,29))
    assert_equal(Time.local(n,3,2), Time.local(n,2,30))
    assert_equal(Time.local(n,3,3), Time.local(n,2,31))
    n += 4
    assert_equal(Time.local(n,2,29),Time.local(n,2,29))
    assert_equal(Time.local(n,3,1), Time.local(n,2,30))
    assert_equal(Time.local(n,3,2), Time.local(n,2,31))
    n += 1
    assert_equal(Time.local(n,3,1), Time.local(n,2,29))
    assert_equal(Time.local(n,3,2), Time.local(n,2,30))
    assert_equal(Time.local(n,3,3), Time.local(n,2,31))
  end

  def test_future
    [
      [3000, 1, 1, 0, 0, 0],
      [4000, 1, 1, 0, 0, 0],
      [1 << 100, 1, 1, 0, 0, 0],
    ].each {|year, mon, day, hour, min, sec|
      t = Time.utc(year, mon, day, hour, min, sec)
      assert_equal(year, t.year)
      assert_equal(mon, t.mon)
      assert_equal(day, t.day)
      assert_equal(hour, t.hour)
      assert_equal(min, t.min)
      assert_equal(sec, t.sec)
    }
  end

  def test_getlocal_utc
    t = Time.gm(2000)
    assert_equal [00, 00, 00,  1,  1, 2000], (t1 = t.getlocal("UTC")).to_a[0, 6]
    assert_predicate t1, :utc?
    assert_equal [00, 00, 00,  1,  1, 2000], (t1 = t.getlocal("-0000")).to_a[0, 6]
    assert_predicate t1, :utc?
    assert_equal [00, 00, 00,  1,  1, 2000], (t1 = t.getlocal("+0000")).to_a[0, 6]
    assert_not_predicate t1, :utc?
  end

  def test_getlocal_utc_offset
    t = Time.gm(2000)
    assert_equal [00, 30, 21, 31, 12, 1999], t.getlocal("-02:30").to_a[0, 6]
    assert_equal [00, 00,  9,  1,  1, 2000], t.getlocal("+09:00").to_a[0, 6]
    assert_equal [20, 29, 21, 31, 12, 1999], t.getlocal("-02:30:40").to_a[0, 6]
    assert_equal [35, 10,  9,  1,  1, 2000], t.getlocal("+09:10:35").to_a[0, 6]
    assert_equal [00, 30, 21, 31, 12, 1999], t.getlocal("-0230").to_a[0, 6]
    assert_equal [00, 00,  9,  1,  1, 2000], t.getlocal("+0900").to_a[0, 6]
    assert_equal [20, 29, 21, 31, 12, 1999], t.getlocal("-023040").to_a[0, 6]
    assert_equal [35, 10,  9,  1,  1, 2000], t.getlocal("+091035").to_a[0, 6]
    assert_raise(ArgumentError) {t.getlocal("-02:3040")}
    assert_raise(ArgumentError) {t.getlocal("+0910:35")}
  end

  def test_getlocal_nil
    now = Time.now
    now2 = nil
    now3 = nil
    assert_nothing_raised {
      now2 = now.getlocal
      now3 = now.getlocal(nil)
    }
    assert_equal now2, now3
    assert_equal now2.zone, now3.zone
  end

  def test_strftime_yearday_on_last_day_of_year
    t = Time.utc(2015, 12, 31, 0, 0, 0)
    assert_equal("365", t.strftime("%j"))
    t = Time.utc(2016, 12, 31, 0, 0, 0)
    assert_equal("366", t.strftime("%j"))

    t = Time.utc(2015, 12, 30, 20, 0, 0).getlocal("+05:00")
    assert_equal("365", t.strftime("%j"))
    t = Time.utc(2016, 12, 30, 20, 0, 0).getlocal("+05:00")
    assert_equal("366", t.strftime("%j"))

    t = Time.utc(2016, 1, 1, 1, 0, 0).getlocal("-05:00")
    assert_equal("365", t.strftime("%j"))
    t = Time.utc(2017, 1, 1, 1, 0, 0).getlocal("-05:00")
    assert_equal("366", t.strftime("%j"))
  end

  def test_num_exact_error
    bad = EnvUtil.labeled_class("BadValue").new
    x = EnvUtil.labeled_class("Inexact") do
      def to_s; "Inexact"; end
      define_method(:to_int) {bad}
      define_method(:to_r) {bad}
    end.new
    assert_raise_with_message(TypeError, /Inexact/) {Time.at(x)}
  end

  def test_memsize
    # Time objects are common in some code, try to keep them small
    omit "Time object size test" if /^(?:i.?86|x86_64)-linux/ !~ RUBY_PLATFORM
    omit "GC is in debug" if GC::INTERNAL_CONSTANTS[:DEBUG]
    require 'objspace'
    t = Time.at(0)
    size = GC::INTERNAL_CONSTANTS[:RVALUE_SIZE]
    case size
    when 20 then expect = 50
    when 24 then expect = 54
    when 40 then expect = 86
    when 48 then expect = 94
    else
      flunk "Unsupported RVALUE_SIZE=#{size}, update test_memsize"
    end
    assert_equal expect, ObjectSpace.memsize_of(t)
  rescue LoadError => e
    omit "failed to load objspace: #{e.message}"
  end
end