1 // Copyright 2019 The PDFium Authors
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 "core/fxcrt/fake_time_test.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace {
11
12 constexpr double kMilliSecondsInADay = 1000 * 60 * 60 * 24;
13
14 } // namespace
15
16 using fxjs::ConversionStatus;
17
TEST(FX_DateHelper,GetYearFromTime)18 TEST(FX_DateHelper, GetYearFromTime) {
19 static constexpr struct {
20 double time_ms;
21 int expected_year;
22 } kTests[] = {
23 {-400 * kMilliSecondsInADay, 1968},
24 {-1, 1969},
25 {0, 1970},
26 {1, 1970},
27 {364.9 * kMilliSecondsInADay, 1970},
28 {365.0 * kMilliSecondsInADay, 1971},
29 {365.1 * kMilliSecondsInADay, 1971},
30 {2 * 365.0 * kMilliSecondsInADay, 1972},
31 // 1972 is a leap year, so there should be an extra day.
32 {3 * 365.0 * kMilliSecondsInADay, 1972},
33 {(3 * 365.0 + 1) * kMilliSecondsInADay, 1973},
34 };
35
36 for (const auto& test : kTests) {
37 EXPECT_EQ(test.expected_year, FX_GetYearFromTime(test.time_ms))
38 << test.time_ms;
39 }
40 }
41
TEST(FX_DateHelper,GetMonthFromTime)42 TEST(FX_DateHelper, GetMonthFromTime) {
43 static constexpr struct {
44 double time_ms;
45 int expected_month; // Zero-based.
46 } kTests[] = {
47 {-400 * kMilliSecondsInADay, 10},
48 {-1, 11},
49 {0, 0},
50 {1, 0},
51 {364.9 * kMilliSecondsInADay, 11},
52 {365.0 * kMilliSecondsInADay, 0},
53 {365.1 * kMilliSecondsInADay, 0},
54 // 1972 is a leap year, so there should be an extra day.
55 {2 * 365.0 * kMilliSecondsInADay, 0},
56 {3 * 365.0 * kMilliSecondsInADay, 11},
57 {(3 * 365.0 + 1) * kMilliSecondsInADay, 0},
58 // Tests boundaries for all months in 1970 not already covered above.
59 {30 * kMilliSecondsInADay, 0},
60 {31 * kMilliSecondsInADay, 1},
61 {58 * kMilliSecondsInADay, 1},
62 {59 * kMilliSecondsInADay, 2},
63 {89 * kMilliSecondsInADay, 2},
64 {90 * kMilliSecondsInADay, 3},
65 {119 * kMilliSecondsInADay, 3},
66 {120 * kMilliSecondsInADay, 4},
67 {150 * kMilliSecondsInADay, 4},
68 {151 * kMilliSecondsInADay, 5},
69 {180 * kMilliSecondsInADay, 5},
70 {181 * kMilliSecondsInADay, 6},
71 {211 * kMilliSecondsInADay, 6},
72 {212 * kMilliSecondsInADay, 7},
73 {242 * kMilliSecondsInADay, 7},
74 {243 * kMilliSecondsInADay, 8},
75 {272 * kMilliSecondsInADay, 8},
76 {273 * kMilliSecondsInADay, 9},
77 {303 * kMilliSecondsInADay, 9},
78 {304 * kMilliSecondsInADay, 10},
79 {333 * kMilliSecondsInADay, 10},
80 {334 * kMilliSecondsInADay, 11},
81 {364 * kMilliSecondsInADay, 11},
82 // Tests boundaries for all months in 1972 not already covered above.
83 {(2 * 365.0 + 30) * kMilliSecondsInADay, 0},
84 {(2 * 365.0 + 31) * kMilliSecondsInADay, 1},
85 {(2 * 365.0 + 59) * kMilliSecondsInADay, 1},
86 {(2 * 365.0 + 60) * kMilliSecondsInADay, 2},
87 {(2 * 365.0 + 90) * kMilliSecondsInADay, 2},
88 {(2 * 365.0 + 91) * kMilliSecondsInADay, 3},
89 {(2 * 365.0 + 120) * kMilliSecondsInADay, 3},
90 {(2 * 365.0 + 121) * kMilliSecondsInADay, 4},
91 {(2 * 365.0 + 151) * kMilliSecondsInADay, 4},
92 {(2 * 365.0 + 152) * kMilliSecondsInADay, 5},
93 {(2 * 365.0 + 181) * kMilliSecondsInADay, 5},
94 {(2 * 365.0 + 182) * kMilliSecondsInADay, 6},
95 {(2 * 365.0 + 212) * kMilliSecondsInADay, 6},
96 {(2 * 365.0 + 213) * kMilliSecondsInADay, 7},
97 {(2 * 365.0 + 243) * kMilliSecondsInADay, 7},
98 {(2 * 365.0 + 244) * kMilliSecondsInADay, 8},
99 {(2 * 365.0 + 273) * kMilliSecondsInADay, 8},
100 {(2 * 365.0 + 274) * kMilliSecondsInADay, 9},
101 {(2 * 365.0 + 304) * kMilliSecondsInADay, 9},
102 {(2 * 365.0 + 305) * kMilliSecondsInADay, 10},
103 {(2 * 365.0 + 334) * kMilliSecondsInADay, 10},
104 {(2 * 365.0 + 335) * kMilliSecondsInADay, 11},
105 };
106
107 for (const auto& test : kTests) {
108 EXPECT_EQ(test.expected_month, FX_GetMonthFromTime(test.time_ms))
109 << test.time_ms;
110 }
111 }
112
113 using FXDateHelperFakeTimeTest = FakeTimeTest;
114
TEST_F(FXDateHelperFakeTimeTest,ParseDateUsingFormatWithEmptyParams)115 TEST_F(FXDateHelperFakeTimeTest, ParseDateUsingFormatWithEmptyParams) {
116 double result = 0.0;
117 EXPECT_EQ(ConversionStatus::kSuccess,
118 FX_ParseDateUsingFormat(L"", L"", &result));
119 EXPECT_DOUBLE_EQ(1'587'654'321'000, result);
120 EXPECT_EQ(ConversionStatus::kSuccess,
121 FX_ParseDateUsingFormat(L"value", L"", &result));
122 EXPECT_DOUBLE_EQ(1'587'654'321'000, result);
123 EXPECT_EQ(ConversionStatus::kSuccess,
124 FX_ParseDateUsingFormat(L"", L"format", &result));
125 EXPECT_DOUBLE_EQ(1'587'654'321'000, result);
126 }
127
TEST_F(FXDateHelperFakeTimeTest,ParseDateUsingFormatForValidMonthDay)128 TEST_F(FXDateHelperFakeTimeTest, ParseDateUsingFormatForValidMonthDay) {
129 double result = 0.0;
130 EXPECT_EQ(ConversionStatus::kSuccess,
131 FX_ParseDateUsingFormat(L"01/02/2000", L"mm/dd/yyyy", &result));
132 EXPECT_DOUBLE_EQ(946'825'521'000, result);
133 EXPECT_EQ(ConversionStatus::kSuccess,
134 FX_ParseDateUsingFormat(L"1/2/2000", L"m/d/yyyy", &result));
135 EXPECT_DOUBLE_EQ(946'825'521'000, result);
136 EXPECT_EQ(ConversionStatus::kSuccess,
137 FX_ParseDateUsingFormat(L"1-2-2000", L"m-d-yyyy", &result));
138 EXPECT_DOUBLE_EQ(946'825'521'000, result);
139 EXPECT_EQ(ConversionStatus::kSuccess,
140 FX_ParseDateUsingFormat(L"2-1-2000", L"d-m-yyyy", &result));
141 EXPECT_DOUBLE_EQ(946'825'521'000, result);
142
143 EXPECT_EQ(ConversionStatus::kSuccess,
144 FX_ParseDateUsingFormat(L"11/12/2000", L"mm/dd/yyyy", &result));
145 EXPECT_DOUBLE_EQ(973'955'121'000, result);
146 EXPECT_EQ(ConversionStatus::kSuccess,
147 FX_ParseDateUsingFormat(L"11/12/2000", L"m/d/yyyy", &result));
148 EXPECT_DOUBLE_EQ(973'955'121'000, result);
149 EXPECT_EQ(ConversionStatus::kSuccess,
150 FX_ParseDateUsingFormat(L"11-12-2000", L"m-d-yyyy", &result));
151 EXPECT_DOUBLE_EQ(973'955'121'000, result);
152 EXPECT_EQ(ConversionStatus::kSuccess,
153 FX_ParseDateUsingFormat(L"12-11-2000", L"d-m-yyyy", &result));
154 EXPECT_DOUBLE_EQ(973'955'121'000, result);
155 }
156