1 // Copyright 2019 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "fxjs/fx_date_helpers.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace {
10
11 constexpr double kMilliSecondsInADay = 1000 * 60 * 60 * 24;
12
13 } // namespace
14
TEST(FX_DateHelper,GetYearFromTime)15 TEST(FX_DateHelper, GetYearFromTime) {
16 static constexpr struct {
17 double time_ms;
18 int expected_year;
19 } kTests[] = {
20 {-400 * kMilliSecondsInADay, 1968},
21 {-1, 1969},
22 {0, 1970},
23 {1, 1970},
24 {364.9 * kMilliSecondsInADay, 1970},
25 {365.0 * kMilliSecondsInADay, 1971},
26 {365.1 * kMilliSecondsInADay, 1971},
27 {2 * 365.0 * kMilliSecondsInADay, 1972},
28 // 1972 is a leap year, so there should be an extra day.
29 {3 * 365.0 * kMilliSecondsInADay, 1972},
30 {(3 * 365.0 + 1) * kMilliSecondsInADay, 1973},
31 };
32
33 for (const auto& test : kTests) {
34 EXPECT_EQ(test.expected_year, FX_GetYearFromTime(test.time_ms))
35 << test.time_ms;
36 }
37 }
38
TEST(FX_DateHelper,GetMonthFromTime)39 TEST(FX_DateHelper, GetMonthFromTime) {
40 static constexpr struct {
41 double time_ms;
42 int expected_month; // Zero-based.
43 } kTests[] = {
44 {-400 * kMilliSecondsInADay, 10},
45 {-1, 11},
46 {0, 0},
47 {1, 0},
48 {364.9 * kMilliSecondsInADay, 11},
49 {365.0 * kMilliSecondsInADay, 0},
50 {365.1 * kMilliSecondsInADay, 0},
51 // 1972 is a leap year, so there should be an extra day.
52 {2 * 365.0 * kMilliSecondsInADay, 0},
53 {3 * 365.0 * kMilliSecondsInADay, 11},
54 {(3 * 365.0 + 1) * kMilliSecondsInADay, 0},
55 // Tests boundaries for all months in 1970 not already covered above.
56 {30 * kMilliSecondsInADay, 0},
57 {31 * kMilliSecondsInADay, 1},
58 {58 * kMilliSecondsInADay, 1},
59 {59 * kMilliSecondsInADay, 2},
60 {89 * kMilliSecondsInADay, 2},
61 {90 * kMilliSecondsInADay, 3},
62 {119 * kMilliSecondsInADay, 3},
63 {120 * kMilliSecondsInADay, 4},
64 {150 * kMilliSecondsInADay, 4},
65 {151 * kMilliSecondsInADay, 5},
66 {180 * kMilliSecondsInADay, 5},
67 {181 * kMilliSecondsInADay, 6},
68 {211 * kMilliSecondsInADay, 6},
69 {212 * kMilliSecondsInADay, 7},
70 {242 * kMilliSecondsInADay, 7},
71 {243 * kMilliSecondsInADay, 8},
72 {272 * kMilliSecondsInADay, 8},
73 {273 * kMilliSecondsInADay, 9},
74 {303 * kMilliSecondsInADay, 9},
75 {304 * kMilliSecondsInADay, 10},
76 {333 * kMilliSecondsInADay, 10},
77 {334 * kMilliSecondsInADay, 11},
78 {364 * kMilliSecondsInADay, 11},
79 // Tests boundaries for all months in 1972 not already covered above.
80 {(2 * 365.0 + 30) * kMilliSecondsInADay, 0},
81 {(2 * 365.0 + 31) * kMilliSecondsInADay, 1},
82 {(2 * 365.0 + 59) * kMilliSecondsInADay, 1},
83 {(2 * 365.0 + 60) * kMilliSecondsInADay, 2},
84 {(2 * 365.0 + 90) * kMilliSecondsInADay, 2},
85 {(2 * 365.0 + 91) * kMilliSecondsInADay, 3},
86 {(2 * 365.0 + 120) * kMilliSecondsInADay, 3},
87 {(2 * 365.0 + 121) * kMilliSecondsInADay, 4},
88 {(2 * 365.0 + 151) * kMilliSecondsInADay, 4},
89 {(2 * 365.0 + 152) * kMilliSecondsInADay, 5},
90 {(2 * 365.0 + 181) * kMilliSecondsInADay, 5},
91 {(2 * 365.0 + 182) * kMilliSecondsInADay, 6},
92 {(2 * 365.0 + 212) * kMilliSecondsInADay, 6},
93 {(2 * 365.0 + 213) * kMilliSecondsInADay, 7},
94 {(2 * 365.0 + 243) * kMilliSecondsInADay, 7},
95 {(2 * 365.0 + 244) * kMilliSecondsInADay, 8},
96 {(2 * 365.0 + 273) * kMilliSecondsInADay, 8},
97 {(2 * 365.0 + 274) * kMilliSecondsInADay, 9},
98 {(2 * 365.0 + 304) * kMilliSecondsInADay, 9},
99 {(2 * 365.0 + 305) * kMilliSecondsInADay, 10},
100 {(2 * 365.0 + 334) * kMilliSecondsInADay, 10},
101 {(2 * 365.0 + 335) * kMilliSecondsInADay, 11},
102 };
103
104 for (const auto& test : kTests) {
105 EXPECT_EQ(test.expected_month, FX_GetMonthFromTime(test.time_ms))
106 << test.time_ms;
107 }
108 }
109