1 | /* |
2 | * Copyright (C) 2015 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | |
28 | #include "Test.h" |
29 | #include <wtf/DateMath.h> |
30 | |
31 | namespace TestWebKitAPI { |
32 | |
33 | // Note: The results of these function look weird if you do not understand the following mappings: |
34 | // dayOfWeek: [0, 6] 0 being Monday, day: [1, 31], month: [0, 11], year: ex: 2011, |
35 | // hours: [0, 23], minutes: [0, 59], seconds: [0, 59], utcOffset: [-720,720]. |
36 | |
37 | TEST(WTF_DateMath, dateToDaysFrom1970) |
38 | { |
39 | EXPECT_EQ(0.0, dateToDaysFrom1970(1970, 0, 1)); |
40 | EXPECT_EQ(157.0, dateToDaysFrom1970(1970, 5, 7)); |
41 | EXPECT_EQ(-145.0, dateToDaysFrom1970(1969, 7, 9)); |
42 | EXPECT_EQ(16322, dateToDaysFrom1970(2014, 8, 9)); |
43 | } |
44 | |
45 | |
46 | TEST(WTF_DateMath, isLeapYear) |
47 | { |
48 | EXPECT_TRUE(isLeapYear(1804)); |
49 | EXPECT_FALSE(isLeapYear(1900)); |
50 | EXPECT_TRUE(isLeapYear(1968)); |
51 | EXPECT_TRUE(isLeapYear(1976)); |
52 | EXPECT_TRUE(isLeapYear(2000)); |
53 | EXPECT_FALSE(isLeapYear(2010)); |
54 | EXPECT_TRUE(isLeapYear(2012)); |
55 | EXPECT_FALSE(isLeapYear(2100)); |
56 | } |
57 | |
58 | TEST(WTF_DateMath, msToYear) |
59 | { |
60 | EXPECT_EQ(1962, msToYear(-220953600000)); |
61 | EXPECT_EQ(1970, msToYear(0)); |
62 | EXPECT_EQ(1970, msToYear(100)); |
63 | EXPECT_EQ(1977, msToYear(220953600000)); |
64 | EXPECT_EQ(2013, msToYear(1365318000000)); |
65 | } |
66 | |
67 | TEST(WTF_DateMath, msToDays) |
68 | { |
69 | EXPECT_EQ(0, msToDays(0)); |
70 | EXPECT_EQ(2557, msToDays(220953600000)); |
71 | EXPECT_EQ(255, msToDays(22095360000)); |
72 | EXPECT_EQ(25, msToDays(2209536000)); |
73 | EXPECT_EQ(2, msToDays(220953600)); |
74 | EXPECT_EQ(0, msToDays(22095360)); |
75 | EXPECT_EQ(0, msToDays(2209536)); |
76 | } |
77 | |
78 | TEST(WTF_DateMath, msToMinutes) |
79 | { |
80 | EXPECT_EQ(0, msToMinutes(0)); |
81 | EXPECT_EQ(0, msToMinutes(220953600000)); |
82 | EXPECT_EQ(36, msToMinutes(22095360000)); |
83 | EXPECT_EQ(36, msToMinutes(22095360000)); |
84 | EXPECT_EQ(45, msToMinutes(2209536000)); |
85 | EXPECT_EQ(22, msToMinutes(220953600)); |
86 | EXPECT_EQ(8, msToMinutes(22095360)); |
87 | EXPECT_EQ(36, msToMinutes(2209536)); |
88 | } |
89 | |
90 | TEST(WTF_DateMath, msToHours) |
91 | { |
92 | EXPECT_EQ(0, msToHours(0)); |
93 | EXPECT_EQ(8, msToHours(220953600000)); |
94 | EXPECT_EQ(17, msToHours(22095360000)); |
95 | EXPECT_EQ(13, msToHours(2209536000)); |
96 | EXPECT_EQ(13, msToHours(220953600)); |
97 | EXPECT_EQ(6, msToHours(22095360)); |
98 | EXPECT_EQ(0, msToHours(2209536)); |
99 | } |
100 | |
101 | TEST(WTF_DateMath, dayInYear) |
102 | { |
103 | EXPECT_EQ(59, dayInYear(2015, 2, 1)); |
104 | EXPECT_EQ(60, dayInYear(2012, 2, 1)); |
105 | EXPECT_EQ(0, dayInYear(2015, 0, 1)); |
106 | EXPECT_EQ(31, dayInYear(2015, 1, 1)); |
107 | } |
108 | |
109 | TEST(WTF_DateMath, monthFromDayInYear) |
110 | { |
111 | EXPECT_EQ(2, monthFromDayInYear(59, false)); |
112 | EXPECT_EQ(1, monthFromDayInYear(59, true)); |
113 | EXPECT_EQ(2, monthFromDayInYear(60, true)); |
114 | EXPECT_EQ(0, monthFromDayInYear(0, false)); |
115 | EXPECT_EQ(0, monthFromDayInYear(0, true)); |
116 | EXPECT_EQ(1, monthFromDayInYear(31, true)); |
117 | EXPECT_EQ(1, monthFromDayInYear(31, false)); |
118 | } |
119 | |
120 | TEST(WTF_DateMath, dayInMonthFromDayInYear) |
121 | { |
122 | EXPECT_EQ(1, dayInMonthFromDayInYear(0, false)); |
123 | EXPECT_EQ(1, dayInMonthFromDayInYear(0, true)); |
124 | EXPECT_EQ(1, dayInMonthFromDayInYear(59, false)); |
125 | EXPECT_EQ(29, dayInMonthFromDayInYear(59, true)); |
126 | EXPECT_EQ(1, dayInMonthFromDayInYear(60, true)); |
127 | EXPECT_EQ(1, dayInMonthFromDayInYear(0, false)); |
128 | EXPECT_EQ(1, dayInMonthFromDayInYear(0, true)); |
129 | EXPECT_EQ(31, dayInMonthFromDayInYear(30, true)); |
130 | EXPECT_EQ(31, dayInMonthFromDayInYear(30, false)); |
131 | EXPECT_EQ(31, dayInMonthFromDayInYear(365, true)); |
132 | EXPECT_EQ(32, dayInMonthFromDayInYear(365, false)); |
133 | EXPECT_EQ(32, dayInMonthFromDayInYear(366, true)); |
134 | } |
135 | |
136 | TEST(WTF_DateMath, calculateLocalTimeOffset) |
137 | { |
138 | // DST Start: April 30, 1967 (02:00 am) |
139 | LocalTimeOffset dstStart1967 = calculateLocalTimeOffset(-84301200000, WTF::LocalTime); |
140 | EXPECT_TRUE(dstStart1967.isDST); |
141 | EXPECT_EQ(-25200000, dstStart1967.offset); |
142 | |
143 | // November 1, 1967 (02:00 am) |
144 | LocalTimeOffset dstAlmostEnd1967 = calculateLocalTimeOffset(-68317200000, WTF::LocalTime); |
145 | EXPECT_TRUE(dstAlmostEnd1967.isDST); |
146 | EXPECT_EQ(-25200000, dstAlmostEnd1967.offset); |
147 | |
148 | // DST End: November 11, 1967 (02:00 am) |
149 | LocalTimeOffset dstEnd1967 = calculateLocalTimeOffset(-67536000000, WTF::LocalTime); |
150 | EXPECT_FALSE(dstEnd1967.isDST); |
151 | EXPECT_EQ(-25200000, dstStart1967.offset); |
152 | |
153 | // DST Start: April 3, 1988 (02:00 am) |
154 | LocalTimeOffset dstStart1988 = calculateLocalTimeOffset(576054000000, WTF::LocalTime); |
155 | EXPECT_TRUE(dstStart1988.isDST); |
156 | EXPECT_EQ(-25200000, dstStart1988.offset); |
157 | |
158 | // DST End: November 4, 2012 (02:00 am) |
159 | LocalTimeOffset dstEnd2012 = calculateLocalTimeOffset(1352012400000, WTF::LocalTime); |
160 | EXPECT_FALSE(dstEnd2012.isDST); |
161 | EXPECT_EQ(-28800000, dstEnd2012.offset); |
162 | |
163 | // DST Begin: March 8, 2015 |
164 | LocalTimeOffset dstBegin2015 = calculateLocalTimeOffset(1425801600000, WTF::LocalTime); |
165 | EXPECT_TRUE(dstBegin2015.isDST); |
166 | EXPECT_EQ(-25200000, dstBegin2015.offset); |
167 | |
168 | LocalTimeOffset dstBegin2015UTC = calculateLocalTimeOffset(1425801600000, WTF::UTCTime); |
169 | EXPECT_FALSE(dstBegin2015UTC.isDST); |
170 | EXPECT_EQ(-28800000, dstBegin2015UTC.offset); |
171 | |
172 | // DST End: November 1, 2015 |
173 | LocalTimeOffset dstEnd2015 = calculateLocalTimeOffset(1446361200000, WTF::LocalTime); |
174 | EXPECT_FALSE(dstEnd2015.isDST); |
175 | EXPECT_EQ(-28800000, dstEnd2015.offset); |
176 | |
177 | // DST Begin: March 13, 2016 |
178 | LocalTimeOffset dstBegin2016 = calculateLocalTimeOffset(1458111600000, WTF::LocalTime); |
179 | EXPECT_TRUE(dstBegin2016.isDST); |
180 | EXPECT_EQ(-25200000, dstBegin2016.offset); |
181 | |
182 | // DST End: November 6, 2016 |
183 | LocalTimeOffset dstEnd2016 = calculateLocalTimeOffset(1478415600000, WTF::LocalTime); |
184 | EXPECT_FALSE(dstEnd2016.isDST); |
185 | EXPECT_EQ(-28800000, dstEnd2016.offset); |
186 | |
187 | // DST Begin: March 12, 2017 |
188 | LocalTimeOffset dstBegin2017 = calculateLocalTimeOffset(1489305600000, WTF::LocalTime); |
189 | EXPECT_TRUE(dstBegin2017.isDST); |
190 | EXPECT_EQ(-25200000, dstBegin2017.offset); |
191 | |
192 | // DST End: November 5, 2017 |
193 | LocalTimeOffset dstEnd2017 = calculateLocalTimeOffset(1509865200000, WTF::LocalTime); |
194 | EXPECT_FALSE(dstEnd2017.isDST); |
195 | EXPECT_EQ(-28800000, dstEnd2017.offset); |
196 | |
197 | // DST Begin: March 11, 2018 |
198 | LocalTimeOffset dstBegin2018 = calculateLocalTimeOffset(1520755200000, WTF::LocalTime); |
199 | EXPECT_TRUE(dstBegin2018.isDST); |
200 | EXPECT_EQ(-25200000, dstBegin2018.offset); |
201 | |
202 | // DST End: November 4, 2018 |
203 | LocalTimeOffset dstEnd2018 = calculateLocalTimeOffset(1541314800000, WTF::LocalTime); |
204 | EXPECT_FALSE(dstEnd2018.isDST); |
205 | EXPECT_EQ(-28800000, dstEnd2018.offset); |
206 | } |
207 | |
208 | } // namespace TestWebKitAPI |
209 | |