OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/time/time.h" | 5 #include "base/time/time.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <time.h> | 8 #include <time.h> |
9 #include <limits> | 9 #include <limits> |
10 #include <string> | 10 #include <string> |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 119 |
120 // This will test both our exploding and our time_t -> Time conversion. | 120 // This will test both our exploding and our time_t -> Time conversion. |
121 EXPECT_EQ(tms.tm_year + 1900, exploded.year); | 121 EXPECT_EQ(tms.tm_year + 1900, exploded.year); |
122 EXPECT_EQ(tms.tm_mon + 1, exploded.month); | 122 EXPECT_EQ(tms.tm_mon + 1, exploded.month); |
123 EXPECT_EQ(tms.tm_mday, exploded.day_of_month); | 123 EXPECT_EQ(tms.tm_mday, exploded.day_of_month); |
124 EXPECT_EQ(tms.tm_hour, exploded.hour); | 124 EXPECT_EQ(tms.tm_hour, exploded.hour); |
125 EXPECT_EQ(tms.tm_min, exploded.minute); | 125 EXPECT_EQ(tms.tm_min, exploded.minute); |
126 EXPECT_EQ(tms.tm_sec, exploded.second); | 126 EXPECT_EQ(tms.tm_sec, exploded.second); |
127 | 127 |
128 // Convert exploded back to the time struct. | 128 // Convert exploded back to the time struct. |
129 Time our_time_2 = Time::FromLocalExploded(exploded); | 129 Time our_time_2; |
| 130 EXPECT_TRUE(Time::FromLocalExploded(exploded, &our_time_2)); |
130 EXPECT_TRUE(our_time_1 == our_time_2); | 131 EXPECT_TRUE(our_time_1 == our_time_2); |
131 | 132 |
132 time_t now_t_2 = our_time_2.ToTimeT(); | 133 time_t now_t_2 = our_time_2.ToTimeT(); |
133 EXPECT_EQ(now_t_1, now_t_2); | 134 EXPECT_EQ(now_t_1, now_t_2); |
134 | 135 |
135 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT()); | 136 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT()); |
136 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT()); | 137 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT()); |
137 | 138 |
138 // Conversions of 0 should stay 0. | 139 // Conversions of 0 should stay 0. |
139 EXPECT_EQ(0, Time().ToTimeT()); | 140 EXPECT_EQ(0, Time().ToTimeT()); |
(...skipping 18 matching lines...) Expand all Loading... |
158 } | 159 } |
159 #endif // OS_POSIX | 160 #endif // OS_POSIX |
160 | 161 |
161 TEST_F(TimeTest, FromExplodedWithMilliseconds) { | 162 TEST_F(TimeTest, FromExplodedWithMilliseconds) { |
162 // Some platform implementations of FromExploded are liable to drop | 163 // Some platform implementations of FromExploded are liable to drop |
163 // milliseconds if we aren't careful. | 164 // milliseconds if we aren't careful. |
164 Time now = Time::NowFromSystemTime(); | 165 Time now = Time::NowFromSystemTime(); |
165 Time::Exploded exploded1 = {0}; | 166 Time::Exploded exploded1 = {0}; |
166 now.UTCExplode(&exploded1); | 167 now.UTCExplode(&exploded1); |
167 exploded1.millisecond = 500; | 168 exploded1.millisecond = 500; |
168 Time time = Time::FromUTCExploded(exploded1); | 169 Time time; |
| 170 EXPECT_TRUE(Time::FromUTCExploded(exploded1, &time)); |
169 Time::Exploded exploded2 = {0}; | 171 Time::Exploded exploded2 = {0}; |
170 time.UTCExplode(&exploded2); | 172 time.UTCExplode(&exploded2); |
171 EXPECT_EQ(exploded1.millisecond, exploded2.millisecond); | 173 EXPECT_EQ(exploded1.millisecond, exploded2.millisecond); |
172 } | 174 } |
173 | 175 |
174 TEST_F(TimeTest, ZeroIsSymmetric) { | 176 TEST_F(TimeTest, ZeroIsSymmetric) { |
175 Time zero_time(Time::FromTimeT(0)); | 177 Time zero_time(Time::FromTimeT(0)); |
176 EXPECT_EQ(0, zero_time.ToTimeT()); | 178 EXPECT_EQ(0, zero_time.ToTimeT()); |
177 | 179 |
178 EXPECT_EQ(0.0, zero_time.ToDoubleT()); | 180 EXPECT_EQ(0.0, zero_time.ToDoubleT()); |
179 } | 181 } |
180 | 182 |
181 TEST_F(TimeTest, LocalExplode) { | 183 TEST_F(TimeTest, LocalExplode) { |
182 Time a = Time::Now(); | 184 Time a = Time::Now(); |
183 Time::Exploded exploded; | 185 Time::Exploded exploded; |
184 a.LocalExplode(&exploded); | 186 a.LocalExplode(&exploded); |
185 | 187 |
186 Time b = Time::FromLocalExploded(exploded); | 188 Time b; |
| 189 EXPECT_TRUE(Time::FromLocalExploded(exploded, &b)); |
187 | 190 |
188 // The exploded structure doesn't have microseconds, and on Mac & Linux, the | 191 // The exploded structure doesn't have microseconds, and on Mac & Linux, the |
189 // internal OS conversion uses seconds, which will cause truncation. So we | 192 // internal OS conversion uses seconds, which will cause truncation. So we |
190 // can only make sure that the delta is within one second. | 193 // can only make sure that the delta is within one second. |
191 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); | 194 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); |
192 } | 195 } |
193 | 196 |
194 TEST_F(TimeTest, UTCExplode) { | 197 TEST_F(TimeTest, UTCExplode) { |
195 Time a = Time::Now(); | 198 Time a = Time::Now(); |
196 Time::Exploded exploded; | 199 Time::Exploded exploded; |
197 a.UTCExplode(&exploded); | 200 a.UTCExplode(&exploded); |
198 | 201 |
199 Time b = Time::FromUTCExploded(exploded); | 202 Time b; |
| 203 EXPECT_TRUE(Time::FromUTCExploded(exploded, &b)); |
200 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); | 204 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); |
201 } | 205 } |
202 | 206 |
203 TEST_F(TimeTest, LocalMidnight) { | 207 TEST_F(TimeTest, LocalMidnight) { |
204 Time::Exploded exploded; | 208 Time::Exploded exploded; |
205 Time::Now().LocalMidnight().LocalExplode(&exploded); | 209 Time::Now().LocalMidnight().LocalExplode(&exploded); |
206 EXPECT_EQ(0, exploded.hour); | 210 EXPECT_EQ(0, exploded.hour); |
207 EXPECT_EQ(0, exploded.minute); | 211 EXPECT_EQ(0, exploded.minute); |
208 EXPECT_EQ(0, exploded.second); | 212 EXPECT_EQ(0, exploded.second); |
209 EXPECT_EQ(0, exploded.millisecond); | 213 EXPECT_EQ(0, exploded.millisecond); |
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 13, // day_of_month | 608 13, // day_of_month |
605 0, // hour | 609 0, // hour |
606 0, // minute | 610 0, // minute |
607 0, // second | 611 0, // second |
608 }; | 612 }; |
609 // The string passed to putenv() must be a char* and the documentation states | 613 // The string passed to putenv() must be a char* and the documentation states |
610 // that it 'becomes part of the environment', so use a static buffer. | 614 // that it 'becomes part of the environment', so use a static buffer. |
611 static char buffer[] = "TZ=America/Santiago"; | 615 static char buffer[] = "TZ=America/Santiago"; |
612 putenv(buffer); | 616 putenv(buffer); |
613 tzset(); | 617 tzset(); |
614 Time t = Time::FromLocalExploded(midnight); | 618 Time t; |
| 619 EXPECT_TRUE(Time::FromLocalExploded(midnight, &t)); |
615 EXPECT_EQ(1381633200, t.ToTimeT()); | 620 EXPECT_EQ(1381633200, t.ToTimeT()); |
616 } | 621 } |
617 #endif // OS_ANDROID | 622 #endif // OS_ANDROID |
618 | 623 |
619 TEST(TimeTicks, Deltas) { | 624 TEST(TimeTicks, Deltas) { |
620 for (int index = 0; index < 50; index++) { | 625 for (int index = 0; index < 50; index++) { |
621 TimeTicks ticks_start = TimeTicks::Now(); | 626 TimeTicks ticks_start = TimeTicks::Now(); |
622 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | 627 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
623 TimeTicks ticks_stop = TimeTicks::Now(); | 628 TimeTicks ticks_stop = TimeTicks::Now(); |
624 TimeDelta delta = ticks_stop - ticks_start; | 629 TimeDelta delta = ticks_stop - ticks_start; |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
826 TEST(TimeDelta, WindowsEpoch) { | 831 TEST(TimeDelta, WindowsEpoch) { |
827 Time::Exploded exploded; | 832 Time::Exploded exploded; |
828 exploded.year = 1970; | 833 exploded.year = 1970; |
829 exploded.month = 1; | 834 exploded.month = 1; |
830 exploded.day_of_week = 0; // Should be unusued. | 835 exploded.day_of_week = 0; // Should be unusued. |
831 exploded.day_of_month = 1; | 836 exploded.day_of_month = 1; |
832 exploded.hour = 0; | 837 exploded.hour = 0; |
833 exploded.minute = 0; | 838 exploded.minute = 0; |
834 exploded.second = 0; | 839 exploded.second = 0; |
835 exploded.millisecond = 0; | 840 exploded.millisecond = 0; |
836 Time t = Time::FromUTCExploded(exploded); | 841 Time t; |
| 842 EXPECT_TRUE(Time::FromUTCExploded(exploded, &t)); |
837 // Unix 1970 epoch. | 843 // Unix 1970 epoch. |
838 EXPECT_EQ(INT64_C(11644473600000000), t.ToInternalValue()); | 844 EXPECT_EQ(INT64_C(11644473600000000), t.ToInternalValue()); |
839 | 845 |
840 // We can't test 1601 epoch, since the system time functions on Linux | 846 // We can't test 1601 epoch, since the system time functions on Linux |
841 // only compute years starting from 1900. | 847 // only compute years starting from 1900. |
842 } | 848 } |
843 | 849 |
844 // We could define this separately for Time, TimeTicks and TimeDelta but the | 850 // We could define this separately for Time, TimeTicks and TimeDelta but the |
845 // definitions would be identical anyway. | 851 // definitions would be identical anyway. |
846 template <class Any> | 852 template <class Any> |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1167 | 1173 |
1168 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { | 1174 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { |
1169 std::ostringstream oss; | 1175 std::ostringstream oss; |
1170 oss << TimeTicks(); | 1176 oss << TimeTicks(); |
1171 EXPECT_TRUE(oss.good()); | 1177 EXPECT_TRUE(oss.good()); |
1172 } | 1178 } |
1173 | 1179 |
1174 } // namespace | 1180 } // namespace |
1175 | 1181 |
1176 } // namespace base | 1182 } // namespace base |
OLD | NEW |