OLD | NEW |
| (Empty) |
1 // Copyright 2004-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Time unittest | |
17 | |
18 #include <atltime.h> | |
19 | |
20 #include "omaha/base/time.h" | |
21 #include "omaha/base/utils.h" | |
22 #include "omaha/testing/unit_test.h" | |
23 | |
24 namespace omaha { | |
25 | |
26 // Test generation of int32 time values (which uses Time64ToInt32 internally). | |
27 TEST(TimeTest, SystemTimeToInt32NearEpoch) { | |
28 // Try a simple value near the start of int32 time. | |
29 SYSTEMTIME system_time = {1970, // year | |
30 1, // month (1 == January) | |
31 0, // day of week (0 == Sunday) | |
32 2, // day of month | |
33 0, // hour | |
34 0, // minute | |
35 0, // second | |
36 0}; // msec | |
37 system_time.wMilliseconds = 0; | |
38 int32 time1 = SystemTimeToInt32(&system_time); | |
39 system_time.wMilliseconds = 999; | |
40 int32 time2 = SystemTimeToInt32(&system_time); | |
41 | |
42 // Make sure result is independent of milliseconds value. | |
43 ASSERT_EQ(time1, time2); | |
44 | |
45 // 00:00:00 on 1970/01/02 should return the number of seconds in 1 day. | |
46 ASSERT_EQ(time1, (60*60*24)); | |
47 } | |
48 | |
49 // Test an empirical value taken from running dumpbin.exe on a DLL. | |
50 // (IMPORTANT: ran this *after* setting machine's time zone to GMT, | |
51 // without daylight savings). | |
52 // 40AEE7AA time date stamp Sat May 22 05:39:54 2004 | |
53 TEST(TimeTest, SystemTimeToInt32) { | |
54 SYSTEMTIME system_time = {2004, 5, 6, 22, 5, 39, 54, 0}; | |
55 int32 time = SystemTimeToInt32(&system_time); | |
56 ASSERT_EQ(time, 0x40AEE7AA); | |
57 } | |
58 | |
59 // Test conversion between int32 and time64 values. | |
60 // By testing SystemTimeToInt32 above, we've already checked Time64ToInt32 | |
61 // against empirical values, so it's okay to simply test back-and-forth | |
62 // conversion here. | |
63 TEST(TimeTest, Conversion) { | |
64 // Simple checks when starting with int32 values, because time64 has more | |
65 // precision. | |
66 ASSERT_EQ(Time64ToInt32(Int32ToTime64(0x12345678)), 0x12345678); | |
67 ASSERT_EQ(Time64ToInt32(Int32ToTime64(INT_MAX)), INT_MAX); | |
68 ASSERT_EQ(Time64ToInt32(Int32ToTime64(0)), 0); | |
69 | |
70 // Extra conversions when going opposite direction because int32 has less | |
71 // precision. | |
72 ASSERT_EQ(Int32ToTime64(Time64ToInt32(Int32ToTime64(0x12345678))), | |
73 Int32ToTime64(0x12345678)); | |
74 ASSERT_EQ(Int32ToTime64(Time64ToInt32(Int32ToTime64(INT_MAX))), | |
75 Int32ToTime64(INT_MAX)); | |
76 ASSERT_EQ(Int32ToTime64(Time64ToInt32(Int32ToTime64(0))), | |
77 Int32ToTime64(0)); | |
78 } | |
79 | |
80 void TimeToStringTest(FILETIME *ft, bool daylight_savings_time) { | |
81 CTime t(*ft, daylight_savings_time); | |
82 CString date1(t.FormatGmt(_T("%a, %d %b %Y %H:%M:%S GMT"))); | |
83 CString date2(ConvertTimeToGMTString(ft)); | |
84 | |
85 ASSERT_STREQ(date1, date2); | |
86 } | |
87 | |
88 TEST(TimeTest, TimeToStringTest) { | |
89 bool daylight_savings_time = false; | |
90 TIME_ZONE_INFORMATION tz; | |
91 if (GetTimeZoneInformation(&tz) == TIME_ZONE_ID_DAYLIGHT) { | |
92 daylight_savings_time = true; | |
93 } | |
94 | |
95 FILETIME file_time; | |
96 ::GetSystemTimeAsFileTime(&file_time); | |
97 TimeToStringTest(&file_time, daylight_savings_time); | |
98 | |
99 uint64 t = FileTimeToTime64(file_time); | |
100 | |
101 // months | |
102 for (int i = 0; i < 13; i++) { | |
103 t += (24 * kHoursTo100ns) * 28; | |
104 Time64ToFileTime(t, &file_time); | |
105 TimeToStringTest(&file_time, daylight_savings_time); | |
106 } | |
107 | |
108 // days | |
109 for (int i = 0; i < 30; i++) { | |
110 t += (24 * kHoursTo100ns); | |
111 Time64ToFileTime(t, &file_time); | |
112 TimeToStringTest(&file_time, daylight_savings_time); | |
113 } | |
114 | |
115 // hours | |
116 for (int i = 0; i < 24; i++) { | |
117 t += (24 * kHoursTo100ns); | |
118 Time64ToFileTime(t, &file_time); | |
119 TimeToStringTest(&file_time, daylight_savings_time); | |
120 } | |
121 } | |
122 | |
123 TEST(TimeTest, RFC822TimeParsing) { | |
124 SYSTEMTIME time = {0}; | |
125 ASSERT_TRUE(RFC822DateToSystemTime(_T("Mon, 16 May 2005 15:44:18 -0700"), | |
126 &time, | |
127 false)); | |
128 ASSERT_EQ(time.wYear , 2005); | |
129 ASSERT_EQ(time.wMonth , 5); | |
130 ASSERT_EQ(time.wDay , 16); | |
131 ASSERT_EQ(time.wHour , 22); | |
132 ASSERT_EQ(time.wMinute , 44); | |
133 ASSERT_EQ(time.wSecond , 18); | |
134 | |
135 ASSERT_TRUE(RFC822DateToSystemTime(_T("Mon, 16 May 2005 15:44:18 -0700"), | |
136 &time, | |
137 true)); | |
138 ASSERT_EQ(time.wYear , 2005); | |
139 ASSERT_EQ(time.wMonth , 5); | |
140 ASSERT_EQ(time.wDay , 16); | |
141 ASSERT_TRUE(time.wHour == 15 || time.wHour == 14); // daylight saving time | |
142 ASSERT_EQ(time.wMinute , 44); | |
143 ASSERT_EQ(time.wSecond , 18); | |
144 | |
145 ASSERT_TRUE(RFC822DateToSystemTime(_T("Tue, 17 May 2005 02:56:18 +0400"), | |
146 &time, | |
147 false)); | |
148 ASSERT_EQ(time.wYear , 2005); | |
149 ASSERT_EQ(time.wMonth , 5); | |
150 ASSERT_EQ(time.wDay , 16); | |
151 ASSERT_EQ(time.wHour , 22); | |
152 ASSERT_EQ(time.wMinute , 56); | |
153 ASSERT_EQ(time.wSecond , 18); | |
154 | |
155 ASSERT_TRUE(RFC822DateToSystemTime(_T("Tue, 17 May 2005 02:56:18 +0400"), | |
156 &time, | |
157 true)); | |
158 ASSERT_EQ(time.wYear , 2005); | |
159 ASSERT_EQ(time.wMonth , 5); | |
160 ASSERT_EQ(time.wDay , 16); | |
161 ASSERT_TRUE(time.wHour == 15 || time.wHour == 14); // daylight saving time | |
162 ASSERT_EQ(time.wMinute , 56); | |
163 ASSERT_EQ(time.wSecond , 18); | |
164 } | |
165 | |
166 TEST(TimeTest, FileTimeToInt64) { | |
167 { | |
168 FILETIME file_time = {0}; | |
169 EXPECT_EQ(0, FileTimeToInt64(file_time)); | |
170 } | |
171 | |
172 { | |
173 FILETIME file_time = {LONG_MAX, 0}; | |
174 EXPECT_EQ(LONG_MAX, FileTimeToInt64(file_time)); | |
175 } | |
176 | |
177 { | |
178 FILETIME file_time = {ULONG_MAX, 0}; | |
179 EXPECT_EQ(ULONG_MAX, FileTimeToInt64(file_time)); | |
180 } | |
181 | |
182 { | |
183 FILETIME file_time = {ULONG_MAX, ULONG_MAX}; | |
184 EXPECT_EQ(kuint64max, FileTimeToInt64(file_time)); | |
185 } | |
186 } | |
187 | |
188 } // namespace omaha | |
189 | |
OLD | NEW |