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> |
11 | 11 |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
| 24 TEST(TimeTestOutOfBounds, FromExplodedOutOfBoundsTime) { |
| 25 // FromUTCExploded must set time to Time(0) and failure, if the day is set to |
| 26 // 31 on a 28-30 day month. Test |exploded| returns Time(0) on 31st of |
| 27 // February and 31st of April. New implementation handles this. |
| 28 |
| 29 const struct DateTestData { |
| 30 Time::Exploded explode; |
| 31 bool is_valid; |
| 32 } kDateTestData[] = { |
| 33 // 31st of February |
| 34 {{2016, 2, 0, 31, 12, 30, 0, 0}, true}, |
| 35 // 31st of April |
| 36 {{2016, 4, 0, 31, 8, 43, 0, 0}, true}, |
| 37 // Negative month |
| 38 {{2016, -5, 0, 2, 4, 10, 0, 0}, false}, |
| 39 // Negative date of month |
| 40 {{2016, 6, 0, -15, 2, 50, 0, 0}, false}, |
| 41 // Negative hours |
| 42 {{2016, 7, 0, 10, -11, 29, 0, 0}, false}, |
| 43 // Negative minutes |
| 44 {{2016, 3, 0, 14, 10, -29, 0, 0}, false}, |
| 45 // Negative seconds |
| 46 {{2016, 10, 0, 25, 7, 47, -30, 0}, false}, |
| 47 // Negative milliseconds |
| 48 {{2016, 10, 0, 25, 7, 47, 20, -500}, false}, |
| 49 // Hours are too large |
| 50 {{2016, 7, 0, 10, 26, 29, 0, 0}, false}, |
| 51 // Minutes are too large |
| 52 {{2016, 3, 0, 14, 10, 78, 0, 0}, false}, |
| 53 // Seconds are too large |
| 54 {{2016, 10, 0, 25, 7, 47, 234, 0}, false}, |
| 55 // Milliseconds are too large |
| 56 {{2016, 10, 0, 25, 6, 31, 23, 1643}, false}, |
| 57 }; |
| 58 |
| 59 for (const auto& test : kDateTestData) { |
| 60 EXPECT_EQ(test.explode.HasValidValues(), test.is_valid); |
| 61 |
| 62 base::Time result; |
| 63 EXPECT_FALSE(base::Time::FromUTCExploded(test.explode, &result)); |
| 64 EXPECT_TRUE(result.is_null()); |
| 65 EXPECT_FALSE(base::Time::FromLocalExploded(test.explode, &result)); |
| 66 EXPECT_TRUE(result.is_null()); |
| 67 } |
| 68 } |
| 69 |
24 // Specialized test fixture allowing time strings without timezones to be | 70 // Specialized test fixture allowing time strings without timezones to be |
25 // tested by comparing them to a known time in the local zone. | 71 // tested by comparing them to a known time in the local zone. |
26 // See also pr_time_unittests.cc | 72 // See also pr_time_unittests.cc |
27 class TimeTest : public testing::Test { | 73 class TimeTest : public testing::Test { |
28 protected: | 74 protected: |
29 void SetUp() override { | 75 void SetUp() override { |
30 // Use mktime to get a time_t, and turn it into a PRTime by converting | 76 // Use mktime to get a time_t, and turn it into a PRTime by converting |
31 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This | 77 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This |
32 // must be a time guaranteed to be outside of a DST fallback hour in | 78 // must be a time guaranteed to be outside of a DST fallback hour in |
33 // any timezone. | 79 // any timezone. |
(...skipping 1087 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1121 | 1167 |
1122 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { | 1168 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { |
1123 std::ostringstream oss; | 1169 std::ostringstream oss; |
1124 oss << TimeTicks(); | 1170 oss << TimeTicks(); |
1125 EXPECT_TRUE(oss.good()); | 1171 EXPECT_TRUE(oss.good()); |
1126 } | 1172 } |
1127 | 1173 |
1128 } // namespace | 1174 } // namespace |
1129 | 1175 |
1130 } // namespace base | 1176 } // namespace base |
OLD | NEW |