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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 | 52 |
53 Time comparison_time_local_; | 53 Time comparison_time_local_; |
54 Time comparison_time_pdt_; | 54 Time comparison_time_pdt_; |
55 }; | 55 }; |
56 | 56 |
57 // Test conversions to/from time_t and exploding/unexploding. | 57 // Test conversions to/from time_t and exploding/unexploding. |
58 TEST_F(TimeTest, TimeT) { | 58 TEST_F(TimeTest, TimeT) { |
59 // C library time and exploded time. | 59 // C library time and exploded time. |
60 time_t now_t_1 = time(NULL); | 60 time_t now_t_1 = time(NULL); |
61 struct tm tms; | 61 struct tm tms; |
62 #if defined(OS_WIN) | 62 #if defined(OS_POSIX) |
63 localtime_s(&tms, &now_t_1); | |
64 #elif defined(OS_POSIX) | |
65 localtime_r(&now_t_1, &tms); | 63 localtime_r(&now_t_1, &tms); |
66 #endif | 64 #endif |
67 | 65 |
68 // Convert to ours. | 66 // Convert to ours. |
69 Time our_time_1 = Time::FromTimeT(now_t_1); | 67 Time our_time_1 = Time::FromTimeT(now_t_1); |
70 Time::Exploded exploded; | 68 Time::Exploded exploded; |
71 our_time_1.LocalExplode(&exploded); | 69 our_time_1.LocalExplode(&exploded); |
72 | 70 |
73 // This will test both our exploding and our time_t -> Time conversion. | 71 // This will test both our exploding and our time_t -> Time conversion. |
74 EXPECT_EQ(tms.tm_year + 1900, exploded.year); | 72 EXPECT_EQ(tms.tm_year + 1900, exploded.year); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 EXPECT_EQ(0, exploded.millisecond); | 160 EXPECT_EQ(0, exploded.millisecond); |
163 } | 161 } |
164 | 162 |
165 TEST_F(TimeTest, ParseTimeTest1) { | 163 TEST_F(TimeTest, ParseTimeTest1) { |
166 time_t current_time = 0; | 164 time_t current_time = 0; |
167 time(¤t_time); | 165 time(¤t_time); |
168 | 166 |
169 const int BUFFER_SIZE = 64; | 167 const int BUFFER_SIZE = 64; |
170 struct tm local_time = {0}; | 168 struct tm local_time = {0}; |
171 char time_buf[BUFFER_SIZE] = {0}; | 169 char time_buf[BUFFER_SIZE] = {0}; |
172 #if defined(OS_WIN) | 170 #if defined(OS_POSIX) |
173 localtime_s(&local_time, ¤t_time); | |
174 asctime_s(time_buf, arraysize(time_buf), &local_time); | |
175 #elif defined(OS_POSIX) | |
176 localtime_r(¤t_time, &local_time); | 171 localtime_r(¤t_time, &local_time); |
177 asctime_r(&local_time, time_buf); | 172 asctime_r(&local_time, time_buf); |
178 #endif | 173 #endif |
179 | 174 |
180 Time parsed_time; | 175 Time parsed_time; |
181 EXPECT_TRUE(Time::FromString(time_buf, &parsed_time)); | 176 EXPECT_TRUE(Time::FromString(time_buf, &parsed_time)); |
182 EXPECT_EQ(current_time, parsed_time.ToTimeT()); | 177 EXPECT_EQ(current_time, parsed_time.ToTimeT()); |
183 } | 178 } |
184 | 179 |
185 TEST_F(TimeTest, DayOfWeekSunday) { | 180 TEST_F(TimeTest, DayOfWeekSunday) { |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 EXPECT_EQ(static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1, | 560 EXPECT_EQ(static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1, |
566 tval.tv_usec); | 561 tval.tv_usec); |
567 #endif | 562 #endif |
568 | 563 |
569 #if defined(OS_MACOSX) | 564 #if defined(OS_MACOSX) |
570 t = Time::FromCFAbsoluteTime(std::numeric_limits<CFAbsoluteTime>::infinity()); | 565 t = Time::FromCFAbsoluteTime(std::numeric_limits<CFAbsoluteTime>::infinity()); |
571 EXPECT_TRUE(t.is_max()); | 566 EXPECT_TRUE(t.is_max()); |
572 EXPECT_EQ(std::numeric_limits<CFAbsoluteTime>::infinity(), | 567 EXPECT_EQ(std::numeric_limits<CFAbsoluteTime>::infinity(), |
573 t.ToCFAbsoluteTime()); | 568 t.ToCFAbsoluteTime()); |
574 #endif | 569 #endif |
575 | |
576 #if defined(OS_WIN) | |
577 FILETIME ftime; | |
578 ftime.dwHighDateTime = std::numeric_limits<DWORD>::max(); | |
579 ftime.dwLowDateTime = std::numeric_limits<DWORD>::max(); | |
580 t = Time::FromFileTime(ftime); | |
581 EXPECT_TRUE(t.is_max()); | |
582 ftime = t.ToFileTime(); | |
583 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwHighDateTime); | |
584 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwLowDateTime); | |
585 #endif | |
586 } | 570 } |
587 | 571 |
588 #if defined(OS_MACOSX) | 572 #if defined(OS_MACOSX) |
589 TEST_F(TimeTest, TimeTOverflow) { | 573 TEST_F(TimeTest, TimeTOverflow) { |
590 Time t = Time::FromInternalValue(std::numeric_limits<int64>::max() - 1); | 574 Time t = Time::FromInternalValue(std::numeric_limits<int64>::max() - 1); |
591 EXPECT_FALSE(t.is_max()); | 575 EXPECT_FALSE(t.is_max()); |
592 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT()); | 576 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT()); |
593 } | 577 } |
594 #endif | 578 #endif |
595 | 579 |
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1094 | 1078 |
1095 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { | 1079 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { |
1096 std::ostringstream oss; | 1080 std::ostringstream oss; |
1097 oss << TimeTicks(); | 1081 oss << TimeTicks(); |
1098 EXPECT_TRUE(oss.good()); | 1082 EXPECT_TRUE(oss.good()); |
1099 } | 1083 } |
1100 | 1084 |
1101 } // namespace | 1085 } // namespace |
1102 | 1086 |
1103 } // namespace base | 1087 } // namespace base |
OLD | NEW |