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 <time.h> | 7 #include <time.h> |
8 | 8 |
| 9 #include <string> |
| 10 |
9 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 12 #include "base/logging.h" |
11 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
12 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
13 #include "build/build_config.h" | 15 #include "build/build_config.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
16 using base::Time; | 18 namespace base { |
17 using base::TimeDelta; | 19 |
18 using base::TimeTicks; | 20 namespace { |
19 | 21 |
20 // Specialized test fixture allowing time strings without timezones to be | 22 // Specialized test fixture allowing time strings without timezones to be |
21 // tested by comparing them to a known time in the local zone. | 23 // tested by comparing them to a known time in the local zone. |
22 // See also pr_time_unittests.cc | 24 // See also pr_time_unittests.cc |
23 class TimeTest : public testing::Test { | 25 class TimeTest : public testing::Test { |
24 protected: | 26 protected: |
25 virtual void SetUp() override { | 27 virtual void SetUp() override { |
26 // Use mktime to get a time_t, and turn it into a PRTime by converting | 28 // Use mktime to get a time_t, and turn it into a PRTime by converting |
27 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This | 29 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This |
28 // must be a time guaranteed to be outside of a DST fallback hour in | 30 // must be a time guaranteed to be outside of a DST fallback hour in |
(...skipping 744 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
773 exploded.minute = 0; | 775 exploded.minute = 0; |
774 exploded.second = 0; | 776 exploded.second = 0; |
775 exploded.millisecond = 0; | 777 exploded.millisecond = 0; |
776 Time t = Time::FromUTCExploded(exploded); | 778 Time t = Time::FromUTCExploded(exploded); |
777 // Unix 1970 epoch. | 779 // Unix 1970 epoch. |
778 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); | 780 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); |
779 | 781 |
780 // We can't test 1601 epoch, since the system time functions on Linux | 782 // We can't test 1601 epoch, since the system time functions on Linux |
781 // only compute years starting from 1900. | 783 // only compute years starting from 1900. |
782 } | 784 } |
| 785 |
| 786 // We could define this separately for Time, TimeTicks and TimeDelta but the |
| 787 // definitions would be identical anyway. |
| 788 template <class Any> |
| 789 std::string AnyToString(Any any) { |
| 790 std::ostringstream oss; |
| 791 oss << any; |
| 792 return oss.str(); |
| 793 } |
| 794 |
| 795 TEST(TimeDeltaLogging, DCheckEqCompiles) { |
| 796 DCHECK_EQ(TimeDelta(), TimeDelta()); |
| 797 } |
| 798 |
| 799 TEST(TimeDeltaLogging, EmptyIsZero) { |
| 800 TimeDelta zero; |
| 801 EXPECT_EQ("0s", AnyToString(zero)); |
| 802 } |
| 803 |
| 804 TEST(TimeDeltaLogging, FiveHundredMs) { |
| 805 TimeDelta five_hundred_ms = TimeDelta::FromMilliseconds(500); |
| 806 EXPECT_EQ("0.5s", AnyToString(five_hundred_ms)); |
| 807 } |
| 808 |
| 809 TEST(TimeDeltaLogging, MinusTenSeconds) { |
| 810 TimeDelta minus_ten_seconds = TimeDelta::FromSeconds(-10); |
| 811 EXPECT_EQ("-10s", AnyToString(minus_ten_seconds)); |
| 812 } |
| 813 |
| 814 TEST(TimeDeltaLogging, DoesNotMessUpFormattingFlags) { |
| 815 std::ostringstream oss; |
| 816 std::ios_base::fmtflags flags_before = oss.flags(); |
| 817 oss << TimeDelta(); |
| 818 EXPECT_EQ(flags_before, oss.flags()); |
| 819 } |
| 820 |
| 821 TEST(TimeDeltaLogging, DoesNotMakeStreamBad) { |
| 822 std::ostringstream oss; |
| 823 oss << TimeDelta(); |
| 824 EXPECT_TRUE(oss.good()); |
| 825 } |
| 826 |
| 827 TEST(TimeLogging, DCheckEqCompiles) { |
| 828 DCHECK_EQ(Time(), Time()); |
| 829 } |
| 830 |
| 831 TEST(TimeLogging, ChromeBirthdate) { |
| 832 Time birthdate; |
| 833 ASSERT_TRUE(Time::FromString("Tue, 02 Sep 2008 09:42:18 GMT", &birthdate)); |
| 834 EXPECT_EQ("2008-09-02 09:42:18.000 UTC", AnyToString(birthdate)); |
| 835 } |
| 836 |
| 837 TEST(TimeLogging, DoesNotMessUpFormattingFlags) { |
| 838 std::ostringstream oss; |
| 839 std::ios_base::fmtflags flags_before = oss.flags(); |
| 840 oss << Time(); |
| 841 EXPECT_EQ(flags_before, oss.flags()); |
| 842 } |
| 843 |
| 844 TEST(TimeLogging, DoesNotMakeStreamBad) { |
| 845 std::ostringstream oss; |
| 846 oss << Time(); |
| 847 EXPECT_TRUE(oss.good()); |
| 848 } |
| 849 |
| 850 TEST(TimeTicksLogging, DCheckEqCompiles) { |
| 851 DCHECK_EQ(TimeTicks(), TimeTicks()); |
| 852 } |
| 853 |
| 854 TEST(TimeTicksLogging, ZeroTime) { |
| 855 TimeTicks zero; |
| 856 EXPECT_EQ("0 bogo-microseconds", AnyToString(zero)); |
| 857 } |
| 858 |
| 859 TEST(TimeTicksLogging, FortyYearsLater) { |
| 860 TimeTicks forty_years_later = |
| 861 TimeTicks() + TimeDelta::FromDays(365.25 * 40); |
| 862 EXPECT_EQ("1262304000000000 bogo-microseconds", |
| 863 AnyToString(forty_years_later)); |
| 864 } |
| 865 |
| 866 TEST(TimeTicksLogging, DoesNotMessUpFormattingFlags) { |
| 867 std::ostringstream oss; |
| 868 std::ios_base::fmtflags flags_before = oss.flags(); |
| 869 oss << TimeTicks(); |
| 870 EXPECT_EQ(flags_before, oss.flags()); |
| 871 } |
| 872 |
| 873 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { |
| 874 std::ostringstream oss; |
| 875 oss << TimeTicks(); |
| 876 EXPECT_TRUE(oss.good()); |
| 877 } |
| 878 |
| 879 } // namespace |
| 880 |
| 881 } // namespace base |
OLD | NEW |