-
Notifications
You must be signed in to change notification settings - Fork 9
/
time_unittest.cc
2554 lines (2193 loc) · 96.7 KB
/
time_unittest.cc
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
863
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/time/time.h"
#include <stdint.h>
#include <time.h>
#include <limits>
#include <optional>
#include <string>
#include "base/build_time.h"
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/environment.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/to_string.h"
#include "base/test/gtest_util.h"
#include "base/threading/platform_thread.h"
#include "base/time/time_override.h"
#include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/common/unicode/utypes.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_android.h"
#elif BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_CHROMEOS)
#include "base/test/icu_test_util.h"
#elif BUILDFLAG(IS_WIN)
#include <windows.h>
#endif
namespace base {
namespace {
#if BUILDFLAG(IS_FUCHSIA)
// Hawaii does not observe daylight saving time, which is useful for having a
// constant offset when faking the time zone.
const char kHonoluluTimeZoneId[] = "Pacific/Honolulu";
const int kHonoluluOffsetHours = -10;
const int kHonoluluOffsetSeconds = kHonoluluOffsetHours * 60 * 60;
#endif
#if BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_CHROMEOS)
const char kThaiLocale[] = "th-TH";
const char kBangkokTimeZoneId[] = "Asia/Bangkok";
// Returns the total offset (including Daylight Saving Time) of the timezone
// with |timezone_id| at |time|, or std::nullopt in case of failure.
std::optional<base::TimeDelta> GetTimeZoneOffsetAtTime(const char* timezone_id,
Time time) {
std::unique_ptr<icu::TimeZone> tz(icu::TimeZone::createTimeZone(timezone_id));
if (*tz == icu::TimeZone::getUnknown()) {
return {};
}
int32_t raw_offset = 0;
int32_t dst_offset = 0;
UErrorCode ec = U_ZERO_ERROR;
tz->getOffset(time.InSecondsFSinceUnixEpoch(), false, raw_offset, dst_offset,
ec);
if (!U_SUCCESS(ec)) {
return {};
}
return base::Milliseconds(raw_offset dst_offset);
}
TimeDelta TimePassedAfterMidnight(const Time::Exploded& time) {
return base::Hours(time.hour) base::Minutes(time.minute)
base::Seconds(time.second) base::Milliseconds(time.millisecond);
}
// Timezone environment variable
class ScopedLibcTZ {
public:
explicit ScopedLibcTZ(const std::string& timezone) {
auto env = base::Environment::Create();
std::string old_timezone_value;
if (env->GetVar(kTZ, &old_timezone_value)) {
old_timezone_ = old_timezone_value;
}
if (!env->SetVar(kTZ, timezone)) {
success_ = false;
}
tzset();
}
~ScopedLibcTZ() {
auto env = base::Environment::Create();
if (old_timezone_.has_value()) {
CHECK(env->SetVar(kTZ, old_timezone_.value()));
} else {
CHECK(env->UnSetVar(kTZ));
}
}
ScopedLibcTZ(const ScopedLibcTZ& other) = delete;
ScopedLibcTZ& operator=(const ScopedLibcTZ& other) = delete;
bool is_success() const { return success_; }
private:
static constexpr char kTZ[] = "TZ";
bool success_ = true;
std::optional<std::string> old_timezone_;
};
constexpr char ScopedLibcTZ::kTZ[];
#endif // BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_CHROMEOS)
TEST(TimeTestOutOfBounds, FromExplodedOutOfBoundsTime) {
// FromUTCExploded must set time to Time(0) and failure, if the day is set to
// 31 on a 28-30 day month. Test |exploded| returns Time(0) on 31st of
// February and 31st of April. New implementation handles this.
const struct DateTestData {
Time::Exploded explode;
bool is_valid;
} kDateTestData[] = {
// 31st of February
{{2016, 2, 0, 31, 12, 30, 0, 0}, true},
// 31st of April
{{2016, 4, 0, 31, 8, 43, 0, 0}, true},
// Negative month
{{2016, -5, 0, 2, 4, 10, 0, 0}, false},
// Negative date of month
{{2016, 6, 0, -15, 2, 50, 0, 0}, false},
// Negative hours
{{2016, 7, 0, 10, -11, 29, 0, 0}, false},
// Negative minutes
{{2016, 3, 0, 14, 10, -29, 0, 0}, false},
// Negative seconds
{{2016, 10, 0, 25, 7, 47, -30, 0}, false},
// Negative milliseconds
{{2016, 10, 0, 25, 7, 47, 20, -500}, false},
// Hours are too large
{{2016, 7, 0, 10, 26, 29, 0, 0}, false},
// Minutes are too large
{{2016, 3, 0, 14, 10, 78, 0, 0}, false},
// Seconds are too large
{{2016, 10, 0, 25, 7, 47, 234, 0}, false},
// Milliseconds are too large
{{2016, 10, 0, 25, 6, 31, 23, 1643}, false},
// Test overflow. Time is valid, but overflow case
// results in Time(0).
{{9840633, 1, 0, 1, 1, 1, 0, 0}, true},
// Underflow will fail as well.
{{-9840633, 1, 0, 1, 1, 1, 0, 0}, true},
// Test integer overflow and underflow cases for the values themselves.
{{std::numeric_limits<int>::min(), 1, 0, 1, 1, 1, 0, 0}, true},
{{std::numeric_limits<int>::max(), 1, 0, 1, 1, 1, 0, 0}, true},
{{2016, std::numeric_limits<int>::min(), 0, 1, 1, 1, 0, 0}, false},
{{2016, std::numeric_limits<int>::max(), 0, 1, 1, 1, 0, 0}, false},
};
for (const auto& test : kDateTestData) {
EXPECT_EQ(test.explode.HasValidValues(), test.is_valid);
base::Time result;
EXPECT_FALSE(base::Time::FromUTCExploded(test.explode, &result));
EXPECT_TRUE(result.is_null());
EXPECT_FALSE(base::Time::FromLocalExploded(test.explode, &result));
EXPECT_TRUE(result.is_null());
}
}
// Specialized test fixture allowing time strings without timezones to be
// tested by comparing them to a known time in the local zone.
// See also pr_time_unittests.cc
class TimeTest : public testing::Test {
protected:
#if BUILDFLAG(IS_FUCHSIA)
// POSIX local time functions always use UTC on Fuchsia. As this is not very
// interesting for any "local" tests, set a different default ICU timezone for
// the test. This only affects code that uses ICU, such as Exploded time.
// Chicago is a non-Pacific time zone known to observe daylight saving time.
TimeTest() : chicago_time_("America/Chicago") {}
test::ScopedRestoreDefaultTimezone chicago_time_;
#endif
void SetUp() override {
// Use mktime to get a time_t, and turn it into a PRTime by converting
// seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This
// must be a time guaranteed to be outside of a DST fallback hour in
// any timezone.
struct tm local_comparison_tm = {
0, // second
45, // minute
12, // hour
15, // day of month
10 - 1, // month
2007 - 1900, // year
0, // day of week (ignored, output only)
0, // day of year (ignored, output only)
-1 // DST in effect, -1 tells mktime to figure it out
};
time_t converted_time = mktime(&local_comparison_tm);
ASSERT_GT(converted_time, 0);
comparison_time_local_ = Time::FromTimeT(converted_time);
// time_t representation of 15th Oct 2007 12:45:00 PDT
comparison_time_pdt_ = Time::FromTimeT(1192477500);
}
Time comparison_time_local_;
Time comparison_time_pdt_;
};
// Test conversion to/from TimeDeltas elapsed since the Windows epoch.
// Conversions should be idempotent and non-lossy.
TEST_F(TimeTest, DeltaSinceWindowsEpoch) {
constexpr TimeDelta delta = Microseconds(123);
EXPECT_EQ(delta,
Time::FromDeltaSinceWindowsEpoch(delta).ToDeltaSinceWindowsEpoch());
const Time now = Time::Now();
const Time actual =
Time::FromDeltaSinceWindowsEpoch(now.ToDeltaSinceWindowsEpoch());
EXPECT_EQ(now, actual);
// Null times should remain null after a round-trip conversion. This is an
// important invariant for the common use case of serialization
// deserialization.
const Time should_be_null =
Time::FromDeltaSinceWindowsEpoch(Time().ToDeltaSinceWindowsEpoch());
EXPECT_TRUE(should_be_null.is_null());
{
constexpr Time constexpr_time =
Time::FromDeltaSinceWindowsEpoch(Microseconds(123));
constexpr TimeDelta constexpr_delta =
constexpr_time.ToDeltaSinceWindowsEpoch();
static_assert(constexpr_delta == delta);
}
}
// Test conversion to/from time_t.
TEST_F(TimeTest, TimeT) {
EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT());
EXPECT_EQ(10.0, Time().FromTimeT(10).InSecondsFSinceUnixEpoch());
// Conversions of 0 should stay 0.
EXPECT_EQ(0, Time().ToTimeT());
EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
}
// Test conversions to/from time_t and exploding/unexploding (utc time).
TEST_F(TimeTest, UTCTimeT) {
// C library time and exploded time.
time_t now_t_1 = time(nullptr);
struct tm tms;
#if BUILDFLAG(IS_WIN)
gmtime_s(&tms, &now_t_1);
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
gmtime_r(&now_t_1, &tms);
#endif
// Convert to ours.
Time our_time_1 = Time::FromTimeT(now_t_1);
Time::Exploded exploded;
our_time_1.UTCExplode(&exploded);
// This will test both our exploding and our time_t -> Time conversion.
EXPECT_EQ(tms.tm_year 1900, exploded.year);
EXPECT_EQ(tms.tm_mon 1, exploded.month);
EXPECT_EQ(tms.tm_mday, exploded.day_of_month);
EXPECT_EQ(tms.tm_hour, exploded.hour);
EXPECT_EQ(tms.tm_min, exploded.minute);
EXPECT_EQ(tms.tm_sec, exploded.second);
// Convert exploded back to the time struct.
Time our_time_2;
EXPECT_TRUE(Time::FromUTCExploded(exploded, &our_time_2));
EXPECT_TRUE(our_time_1 == our_time_2);
time_t now_t_2 = our_time_2.ToTimeT();
EXPECT_EQ(now_t_1, now_t_2);
}
// Test conversions to/from time_t and exploding/unexploding (local time).
TEST_F(TimeTest, LocalTimeT) {
// C library time and exploded time.
time_t now_t_1 = time(nullptr);
struct tm tms;
#if BUILDFLAG(IS_WIN)
localtime_s(&tms, &now_t_1);
#elif BUILDFLAG(IS_POSIX)
localtime_r(&now_t_1, &tms);
#elif BUILDFLAG(IS_FUCHSIA)
// POSIX local time functions always use UTC on Fuchsia, so set a known time
// zone and manually obtain the local |tms| values by using an adjusted input.
test::ScopedRestoreDefaultTimezone honolulu_time(kHonoluluTimeZoneId);
time_t adjusted_now_t_1 = now_t_1 kHonoluluOffsetSeconds;
localtime_r(&adjusted_now_t_1, &tms);
#endif
// Convert to ours.
Time our_time_1 = Time::FromTimeT(now_t_1);
Time::Exploded exploded;
our_time_1.LocalExplode(&exploded);
// This will test both our exploding and our time_t -> Time conversion.
EXPECT_EQ(tms.tm_year 1900, exploded.year);
EXPECT_EQ(tms.tm_mon 1, exploded.month);
EXPECT_EQ(tms.tm_mday, exploded.day_of_month);
EXPECT_EQ(tms.tm_hour, exploded.hour);
EXPECT_EQ(tms.tm_min, exploded.minute);
EXPECT_EQ(tms.tm_sec, exploded.second);
// Convert exploded back to the time struct.
Time our_time_2;
EXPECT_TRUE(Time::FromLocalExploded(exploded, &our_time_2));
EXPECT_TRUE(our_time_1 == our_time_2);
time_t now_t_2 = our_time_2.ToTimeT();
EXPECT_EQ(now_t_1, now_t_2);
}
// Test conversions to/from javascript time.
TEST_F(TimeTest, JsTime) {
Time epoch = Time::FromMillisecondsSinceUnixEpoch(0.0);
EXPECT_EQ(epoch, Time::UnixEpoch());
Time t = Time::FromMillisecondsSinceUnixEpoch(700000.3);
EXPECT_EQ(700.0003, t.InSecondsFSinceUnixEpoch());
t = Time::FromSecondsSinceUnixEpoch(800.73);
EXPECT_EQ(800730.0, t.InMillisecondsFSinceUnixEpoch());
// 1601-01-01 isn't round-trip with InMillisecondsFSinceUnixEpoch().
const double kWindowsEpoch = -11644473600000.0;
Time time = Time::FromMillisecondsSinceUnixEpoch(kWindowsEpoch);
EXPECT_TRUE(time.is_null());
EXPECT_NE(kWindowsEpoch, time.InMillisecondsFSinceUnixEpoch());
EXPECT_EQ(kWindowsEpoch, time.InMillisecondsFSinceUnixEpochIgnoringNull());
}
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
TEST_F(TimeTest, FromTimeVal) {
Time now = Time::Now();
Time also_now = Time::FromTimeVal(now.ToTimeVal());
EXPECT_EQ(now, also_now);
}
#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
TEST_F(TimeTest, FromExplodedWithMilliseconds) {
// Some platform implementations of FromExploded are liable to drop
// milliseconds if we aren't careful.
Time now = Time::NowFromSystemTime();
Time::Exploded exploded1 = {0};
now.UTCExplode(&exploded1);
exploded1.millisecond = 500;
Time time;
EXPECT_TRUE(Time::FromUTCExploded(exploded1, &time));
Time::Exploded exploded2 = {0};
time.UTCExplode(&exploded2);
EXPECT_EQ(exploded1.millisecond, exploded2.millisecond);
}
TEST_F(TimeTest, ZeroIsSymmetric) {
Time zero_time(Time::FromTimeT(0));
EXPECT_EQ(0, zero_time.ToTimeT());
EXPECT_EQ(0.0, zero_time.InSecondsFSinceUnixEpoch());
}
// Note that this test does not check whether the implementation correctly
// accounts for the local time zone.
TEST_F(TimeTest, LocalExplode) {
Time a = Time::Now();
Time::Exploded exploded;
a.LocalExplode(&exploded);
Time b;
EXPECT_TRUE(Time::FromLocalExploded(exploded, &b));
// The exploded structure doesn't have microseconds, and on Mac & Linux, the
// internal OS conversion uses seconds, which will cause truncation. So we
// can only make sure that the delta is within one second.
EXPECT_LT(a - b, Seconds(1));
}
TEST_F(TimeTest, UTCExplode) {
Time a = Time::Now();
Time::Exploded exploded;
a.UTCExplode(&exploded);
Time b;
EXPECT_TRUE(Time::FromUTCExploded(exploded, &b));
// The exploded structure doesn't have microseconds, and on Mac & Linux, the
// internal OS conversion uses seconds, which will cause truncation. So we
// can only make sure that the delta is within one second.
EXPECT_LT(a - b, Seconds(1));
}
TEST_F(TimeTest, UTCMidnight) {
Time::Exploded exploded;
Time::Now().UTCMidnight().UTCExplode(&exploded);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(0, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
}
// Note that this test does not check whether the implementation correctly
// accounts for the local time zone.
TEST_F(TimeTest, LocalMidnight) {
Time::Exploded exploded;
Time::Now().LocalMidnight().LocalExplode(&exploded);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(0, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
}
// These tests require the ability to fake the local time zone.
#if BUILDFLAG(IS_FUCHSIA)
TEST_F(TimeTest, LocalExplodeIsLocal) {
// Set the default time zone to a zone with an offset different from UTC.
test::ScopedRestoreDefaultTimezone honolulu_time(kHonoluluTimeZoneId);
// The member contains useful values for this test, which uses it as UTC.
Time comparison_time_utc(comparison_time_local_);
Time::Exploded utc_exploded;
comparison_time_utc.UTCExplode(&utc_exploded);
Time::Exploded local_exploded;
comparison_time_utc.LocalExplode(&local_exploded);
// The year, month, and day are the same because the (negative) offset is
// smaller than the hour in the test time. Similarly, there is no underflow
// for hour.
EXPECT_EQ(utc_exploded.year, local_exploded.year);
EXPECT_EQ(utc_exploded.month, local_exploded.month);
EXPECT_EQ(utc_exploded.day_of_week, local_exploded.day_of_week);
EXPECT_EQ(utc_exploded.day_of_month, local_exploded.day_of_month);
EXPECT_EQ(utc_exploded.hour kHonoluluOffsetHours, local_exploded.hour);
EXPECT_EQ(utc_exploded.minute, local_exploded.minute);
EXPECT_EQ(utc_exploded.second, local_exploded.second);
EXPECT_EQ(utc_exploded.millisecond, local_exploded.millisecond);
Time time_from_local_exploded;
EXPECT_TRUE(
Time::FromLocalExploded(local_exploded, &time_from_local_exploded));
EXPECT_EQ(comparison_time_utc, time_from_local_exploded);
// Unexplode the local time using the non-local method.
// The resulting time should be offset hours earlier.
Time time_from_utc_exploded;
EXPECT_TRUE(Time::FromUTCExploded(local_exploded, &time_from_utc_exploded));
EXPECT_EQ(comparison_time_utc Hours(kHonoluluOffsetHours),
time_from_utc_exploded);
}
TEST_F(TimeTest, LocalMidnightIsLocal) {
// Set the default time zone to a zone with an offset different from UTC.
test::ScopedRestoreDefaultTimezone honolulu_time(kHonoluluTimeZoneId);
// The member contains useful values for this test, which uses it as UTC.
Time comparison_time_utc(comparison_time_local_);
Time::Exploded utc_midnight_exploded;
comparison_time_utc.UTCMidnight().UTCExplode(&utc_midnight_exploded);
// Local midnight exploded in UTC will have an offset hour instead of 0.
Time::Exploded local_midnight_utc_exploded;
comparison_time_utc.LocalMidnight().UTCExplode(&local_midnight_utc_exploded);
// The year, month, and day are the same because the (negative) offset is
// smaller than the hour in the test time and thus both midnights round down
// on the same day.
EXPECT_EQ(utc_midnight_exploded.year, local_midnight_utc_exploded.year);
EXPECT_EQ(utc_midnight_exploded.month, local_midnight_utc_exploded.month);
EXPECT_EQ(utc_midnight_exploded.day_of_week,
local_midnight_utc_exploded.day_of_week);
EXPECT_EQ(utc_midnight_exploded.day_of_month,
local_midnight_utc_exploded.day_of_month);
EXPECT_EQ(0, utc_midnight_exploded.hour);
EXPECT_EQ(0 - kHonoluluOffsetHours, local_midnight_utc_exploded.hour);
EXPECT_EQ(0, local_midnight_utc_exploded.minute);
EXPECT_EQ(0, local_midnight_utc_exploded.second);
EXPECT_EQ(0, local_midnight_utc_exploded.millisecond);
// Local midnight exploded in local time will have no offset.
Time::Exploded local_midnight_exploded;
comparison_time_utc.LocalMidnight().LocalExplode(&local_midnight_exploded);
EXPECT_EQ(utc_midnight_exploded.year, local_midnight_exploded.year);
EXPECT_EQ(utc_midnight_exploded.month, local_midnight_exploded.month);
EXPECT_EQ(utc_midnight_exploded.day_of_week,
local_midnight_exploded.day_of_week);
EXPECT_EQ(utc_midnight_exploded.day_of_month,
local_midnight_exploded.day_of_month);
EXPECT_EQ(0, local_midnight_exploded.hour);
EXPECT_EQ(0, local_midnight_exploded.minute);
EXPECT_EQ(0, local_midnight_exploded.second);
EXPECT_EQ(0, local_midnight_exploded.millisecond);
}
#endif // BUILDFLAG(IS_FUCHSIA)
TEST_F(TimeTest, ParseTimeTest1) {
time_t current_time = 0;
time(¤t_time);
struct tm local_time = {};
char time_buf[64] = {};
#if BUILDFLAG(IS_WIN)
localtime_s(&local_time, ¤t_time);
asctime_s(time_buf, std::size(time_buf), &local_time);
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
localtime_r(¤t_time, &local_time);
asctime_r(&local_time, time_buf);
#endif
Time parsed_time;
EXPECT_TRUE(Time::FromString(time_buf, &parsed_time));
EXPECT_EQ(current_time, parsed_time.ToTimeT());
}
TEST_F(TimeTest, DayOfWeekSunday) {
Time time;
EXPECT_TRUE(Time::FromString("Sun, 06 May 2012 12:00:00 GMT", &time));
Time::Exploded exploded;
time.UTCExplode(&exploded);
EXPECT_EQ(0, exploded.day_of_week);
}
TEST_F(TimeTest, DayOfWeekWednesday) {
Time time;
EXPECT_TRUE(Time::FromString("Wed, 09 May 2012 12:00:00 GMT", &time));
Time::Exploded exploded;
time.UTCExplode(&exploded);
EXPECT_EQ(3, exploded.day_of_week);
}
TEST_F(TimeTest, DayOfWeekSaturday) {
Time time;
EXPECT_TRUE(Time::FromString("Sat, 12 May 2012 12:00:00 GMT", &time));
Time::Exploded exploded;
time.UTCExplode(&exploded);
EXPECT_EQ(6, exploded.day_of_week);
}
TEST_F(TimeTest, ParseTimeTest2) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Mon, 15 Oct 2007 19:45:00 GMT", &parsed_time));
EXPECT_EQ(comparison_time_pdt_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest3) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("15 Oct 07 12:45:00", &parsed_time));
EXPECT_EQ(comparison_time_local_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest4) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("15 Oct 07 19:45 GMT", &parsed_time));
EXPECT_EQ(comparison_time_pdt_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest5) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Mon Oct 15 12:45 PDT 2007", &parsed_time));
EXPECT_EQ(comparison_time_pdt_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest6) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Monday, Oct 15, 2007 12:45 PM", &parsed_time));
EXPECT_EQ(comparison_time_local_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest7) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("10/15/07 12:45:00 PM", &parsed_time));
EXPECT_EQ(comparison_time_local_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest8) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("15-OCT-2007 12:45pm", &parsed_time));
EXPECT_EQ(comparison_time_local_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest9) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("16 Oct 2007 4:45-JST (Tuesday)", &parsed_time));
EXPECT_EQ(comparison_time_pdt_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest10) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("15/10/07 12:45", &parsed_time));
EXPECT_EQ(parsed_time, comparison_time_local_);
}
TEST_F(TimeTest, ParseTimeTest11) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("2007-10-15 12:45:00", &parsed_time));
EXPECT_EQ(parsed_time, comparison_time_local_);
}
// Test some of edge cases around epoch, etc.
TEST_F(TimeTest, ParseTimeTestEpoch0) {
Time parsed_time;
// time_t == epoch == 0
EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:00 0100 1970",
&parsed_time));
EXPECT_EQ(0, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:00 GMT 1970",
&parsed_time));
EXPECT_EQ(0, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEpoch1) {
Time parsed_time;
// time_t == 1 second after epoch == 1
EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:01 0100 1970",
&parsed_time));
EXPECT_EQ(1, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:01 GMT 1970",
&parsed_time));
EXPECT_EQ(1, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEpoch2) {
Time parsed_time;
// time_t == 2 seconds after epoch == 2
EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:02 0100 1970",
&parsed_time));
EXPECT_EQ(2, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:02 GMT 1970",
&parsed_time));
EXPECT_EQ(2, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEpochNeg1) {
Time parsed_time;
// time_t == 1 second before epoch == -1
EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:59 0100 1970",
&parsed_time));
EXPECT_EQ(-1, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 1969",
&parsed_time));
EXPECT_EQ(-1, parsed_time.ToTimeT());
}
// If time_t is 32 bits, a date after year 2038 will overflow time_t and
// cause timegm() to return -1. The parsed time should not be 1 second
// before epoch.
TEST_F(TimeTest, ParseTimeTestEpochNotNeg1) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 2100",
&parsed_time));
EXPECT_NE(-1, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEpochNeg2) {
Time parsed_time;
// time_t == 2 seconds before epoch == -2
EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:58 0100 1970",
&parsed_time));
EXPECT_EQ(-2, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:58 GMT 1969",
&parsed_time));
EXPECT_EQ(-2, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEpoch1960) {
Time parsed_time;
// time_t before Epoch, in 1960
EXPECT_TRUE(Time::FromString("Wed Jun 29 19:40:01 0100 1960",
&parsed_time));
EXPECT_EQ(-299999999, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Wed Jun 29 18:40:01 GMT 1960",
&parsed_time));
EXPECT_EQ(-299999999, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Wed Jun 29 17:40:01 GMT 1960",
&parsed_time));
EXPECT_EQ(-300003599, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEmpty) {
Time parsed_time;
EXPECT_FALSE(Time::FromString("", &parsed_time));
}
TEST_F(TimeTest, ParseTimeTestInvalidString) {
Time parsed_time;
EXPECT_FALSE(Time::FromString("Monday morning 2000", &parsed_time));
}
TEST_F(TimeTest, ExplodeBeforeUnixEpoch) {
static const int kUnixEpochYear = 1970; // In case this changes (ha!).
Time t;
Time::Exploded exploded;
t = Time::UnixEpoch() - Microseconds(1);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
// Should be 1969-12-31 23:59:59 999 milliseconds (and 999 microseconds).
EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
EXPECT_EQ(12, exploded.month);
EXPECT_EQ(31, exploded.day_of_month);
EXPECT_EQ(23, exploded.hour);
EXPECT_EQ(59, exploded.minute);
EXPECT_EQ(59, exploded.second);
EXPECT_EQ(999, exploded.millisecond);
t = Time::UnixEpoch() - Microseconds(999);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
// Should be 1969-12-31 23:59:59 999 milliseconds (and 1 microsecond).
EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
EXPECT_EQ(12, exploded.month);
EXPECT_EQ(31, exploded.day_of_month);
EXPECT_EQ(23, exploded.hour);
EXPECT_EQ(59, exploded.minute);
EXPECT_EQ(59, exploded.second);
EXPECT_EQ(999, exploded.millisecond);
t = Time::UnixEpoch() - Microseconds(1000);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
// Should be 1969-12-31 23:59:59 999 milliseconds.
EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
EXPECT_EQ(12, exploded.month);
EXPECT_EQ(31, exploded.day_of_month);
EXPECT_EQ(23, exploded.hour);
EXPECT_EQ(59, exploded.minute);
EXPECT_EQ(59, exploded.second);
EXPECT_EQ(999, exploded.millisecond);
t = Time::UnixEpoch() - Microseconds(1001);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
// Should be 1969-12-31 23:59:59 998 milliseconds (and 999 microseconds).
EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
EXPECT_EQ(12, exploded.month);
EXPECT_EQ(31, exploded.day_of_month);
EXPECT_EQ(23, exploded.hour);
EXPECT_EQ(59, exploded.minute);
EXPECT_EQ(59, exploded.second);
EXPECT_EQ(998, exploded.millisecond);
t = Time::UnixEpoch() - Milliseconds(1000);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
// Should be 1969-12-31 23:59:59.
EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
EXPECT_EQ(12, exploded.month);
EXPECT_EQ(31, exploded.day_of_month);
EXPECT_EQ(23, exploded.hour);
EXPECT_EQ(59, exploded.minute);
EXPECT_EQ(59, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
t = Time::UnixEpoch() - Milliseconds(1001);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
// Should be 1969-12-31 23:59:58 999 milliseconds.
EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
EXPECT_EQ(12, exploded.month);
EXPECT_EQ(31, exploded.day_of_month);
EXPECT_EQ(23, exploded.hour);
EXPECT_EQ(59, exploded.minute);
EXPECT_EQ(58, exploded.second);
EXPECT_EQ(999, exploded.millisecond);
// Make sure we still handle at/after Unix epoch correctly.
t = Time::UnixEpoch();
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
// Should be 1970-12-31 00:00:00 0 milliseconds.
EXPECT_EQ(kUnixEpochYear, exploded.year);
EXPECT_EQ(1, exploded.month);
EXPECT_EQ(1, exploded.day_of_month);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(0, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
t = Time::UnixEpoch() Microseconds(1);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
// Should be 1970-01-01 00:00:00 0 milliseconds (and 1 microsecond).
EXPECT_EQ(kUnixEpochYear, exploded.year);
EXPECT_EQ(1, exploded.month);
EXPECT_EQ(1, exploded.day_of_month);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(0, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
t = Time::UnixEpoch() Microseconds(999);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
// Should be 1970-01-01 00:00:00 0 milliseconds (and 999 microseconds).
EXPECT_EQ(kUnixEpochYear, exploded.year);
EXPECT_EQ(1, exploded.month);
EXPECT_EQ(1, exploded.day_of_month);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(0, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
t = Time::UnixEpoch() Microseconds(1000);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
// Should be 1970-01-01 00:00:00 1 millisecond.
EXPECT_EQ(kUnixEpochYear, exploded.year);
EXPECT_EQ(1, exploded.month);
EXPECT_EQ(1, exploded.day_of_month);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(0, exploded.second);
EXPECT_EQ(1, exploded.millisecond);
t = Time::UnixEpoch() Milliseconds(1000);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
// Should be 1970-01-01 00:00:01.
EXPECT_EQ(kUnixEpochYear, exploded.year);
EXPECT_EQ(1, exploded.month);
EXPECT_EQ(1, exploded.day_of_month);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(1, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
t = Time::UnixEpoch() Milliseconds(1001);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
// Should be 1970-01-01 00:00:01 1 millisecond.
EXPECT_EQ(kUnixEpochYear, exploded.year);
EXPECT_EQ(1, exploded.month);
EXPECT_EQ(1, exploded.day_of_month);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(1, exploded.second);
EXPECT_EQ(1, exploded.millisecond);
}
TEST_F(TimeTest, Max) {
constexpr Time kMax = Time::Max();
static_assert(kMax.is_max());
static_assert(kMax == Time::Max());
EXPECT_GT(kMax, Time::Now());
static_assert(kMax > Time());
EXPECT_TRUE((Time::Now() - kMax).is_negative());
EXPECT_TRUE((kMax - Time::Now()).is_positive());
}
TEST_F(TimeTest, MaxConversions) {
constexpr Time kMax = Time::Max();
static_assert(std::numeric_limits<int64_t>::max() == kMax.ToInternalValue(),
"");
Time t =
Time::FromSecondsSinceUnixEpoch(std::numeric_limits<double>::infinity());
EXPECT_TRUE(t.is_max());
EXPECT_EQ(std::numeric_limits<double>::infinity(),
t.InSecondsFSinceUnixEpoch());
t = Time::FromMillisecondsSinceUnixEpoch(
std::numeric_limits<double>::infinity());
EXPECT_TRUE(t.is_max());
EXPECT_EQ(std::numeric_limits<double>::infinity(),
t.InMillisecondsFSinceUnixEpoch());
t = Time::FromTimeT(std::numeric_limits<time_t>::max());
EXPECT_TRUE(t.is_max());
EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT());
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
struct timeval tval;
tval.tv_sec = std::numeric_limits<time_t>::max();
tval.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1;
t = Time::FromTimeVal(tval);
EXPECT_TRUE(t.is_max());
tval = t.ToTimeVal();
EXPECT_EQ(std::numeric_limits<time_t>::max(), tval.tv_sec);
EXPECT_EQ(static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1,
tval.tv_usec);
#endif
#if BUILDFLAG(IS_APPLE)
t = Time::FromCFAbsoluteTime(std::numeric_limits<CFAbsoluteTime>::infinity());
EXPECT_TRUE(t.is_max());
EXPECT_EQ(std::numeric_limits<CFAbsoluteTime>::infinity(),
t.ToCFAbsoluteTime());
#endif
#if BUILDFLAG(IS_WIN)
FILETIME ftime;
ftime.dwHighDateTime = std::numeric_limits<DWORD>::max();
ftime.dwLowDateTime = std::numeric_limits<DWORD>::max();
t = Time::FromFileTime(ftime);
EXPECT_TRUE(t.is_max());
ftime = t.ToFileTime();
EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwHighDateTime);
EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwLowDateTime);
#endif
}
TEST_F(TimeTest, Min) {
constexpr Time kMin = Time::Min();
static_assert(kMin.is_min());
static_assert(kMin == Time::Min());
EXPECT_LT(kMin, Time::Now());
static_assert(kMin < Time());
EXPECT_TRUE((Time::Now() - kMin).is_positive());
EXPECT_TRUE((kMin - Time::Now()).is_negative());
}
TEST_F(TimeTest, TimeTOverflow) {
// We always expect Max and Min Time values to map to the extreme of the range
// of time_t because we have things that make this assumption - Even if such a
// time were representable in time_t.
EXPECT_EQ(std::numeric_limits<time_t>::max(), Time::Max().ToTimeT());
EXPECT_EQ(std::numeric_limits<time_t>::min(), Time::Min().ToTimeT());
// In the bad old days time_t was 32 bit. Occasionally it still is.
// Usually it is 64 bit. It must be one or the other.
constexpr bool time_t_is_32_bit = sizeof(time_t) == sizeof(int32_t);
static_assert(time_t_is_32_bit || sizeof(time_t) == sizeof(int64_t));
// base::Time internally represents time as microseconds since the Windows
// epoch as an int64_t. When time_t is a int64_t of seconds since the Unix
// epoch, time_t can represent the maxiumum value of base::Time. A 32 bit
// time_t can not represent it.
// If we have a 32 bit time_t, check that a non-infinite value of one
// microsecond less than the max value of a base::Time still maps to the max
// value of time_t.
if (time_t_is_32_bit) {
constexpr Time kMaxMinusOne =
Time() base::Microseconds(std::numeric_limits<int64_t>::max() - 1);
static_assert(!kMaxMinusOne.is_max());
EXPECT_EQ(std::numeric_limits<time_t>::max(), kMaxMinusOne.ToTimeT());
}
// Converting a base::Time to a time_t subtracts the value of the UnixEpoch in
// microseconds since the Windows epoch from the current time value. As such
// we expect a value of the minimum time plus one, subtracted by the UnixEpoch
// value to be clamped by the TimeDelta math, meaning that we will see a
// minimum value in the time_t, 32 bit or 64 bit
constexpr Time kMinPlusOne =
Time() base::Microseconds(std::numeric_limits<int64_t>::min() 1);
static_assert(!kMinPlusOne.is_min());
EXPECT_EQ(std::numeric_limits<time_t>::min(), kMinPlusOne.ToTimeT());
// We also expect the same behaviour for Min plus the Unix Epoch.
constexpr Time kMinPlusUnix =
Time() base::Microseconds(std::numeric_limits<int64_t>::min()
Time::kTimeTToMicrosecondsOffset);
static_assert(!kMinPlusUnix.is_min());
EXPECT_EQ(std::numeric_limits<time_t>::min(), kMinPlusUnix.ToTimeT());
// We expect Min plus the UnixEpoch plus 1 in microseconds to convert back to
// one more than Min - a negative number of microseconds far before the
// Windows epoch of 1601-01-01. It will representable in seconds as a 64 bit
// time_t, but not on a 32 bit time_t, which can only represent values
// starting from 1901-12-13
constexpr Time kMinPlusUnixPlusOne =
Time() base::Microseconds(std::numeric_limits<int64_t>::min()
Time::kTimeTToMicrosecondsOffset 1);
static_assert(!kMinPlusUnixPlusOne.is_min());
if (time_t_is_32_bit) {
EXPECT_EQ(std::numeric_limits<time_t>::min(),
kMinPlusUnixPlusOne.ToTimeT());
} else {
EXPECT_NE(std::numeric_limits<time_t>::min(),
kMinPlusUnixPlusOne.ToTimeT());
}
}
#if BUILDFLAG(IS_ANDROID)
TEST_F(TimeTest, FromLocalExplodedCrashOnAndroid) {
// This crashed inside Time:: FromLocalExploded() on Android 4.1.2.
// See http://crbug.com/287821
Time::Exploded midnight = {2013, // year