Home | History | Annotate | Download | only in time
      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
      3 // found in the LICENSE file.
      4 
      5 #include "base/time/time.h"
      6 
      7 #include <stdint.h>
      8 #include <time.h>
      9 #include <limits>
     10 #include <string>
     11 
     12 #include "base/compiler_specific.h"
     13 #include "base/logging.h"
     14 #include "base/macros.h"
     15 #include "base/strings/stringprintf.h"
     16 #include "base/threading/platform_thread.h"
     17 #include "build/build_config.h"
     18 #include "testing/gtest/include/gtest/gtest.h"
     19 
     20 namespace base {
     21 
     22 namespace {
     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 
     70 // Specialized test fixture allowing time strings without timezones to be
     71 // tested by comparing them to a known time in the local zone.
     72 // See also pr_time_unittests.cc
     73 class TimeTest : public testing::Test {
     74  protected:
     75   void SetUp() override {
     76     // Use mktime to get a time_t, and turn it into a PRTime by converting
     77     // seconds to microseconds.  Use 15th Oct 2007 12:45:00 local.  This
     78     // must be a time guaranteed to be outside of a DST fallback hour in
     79     // any timezone.
     80     struct tm local_comparison_tm = {
     81       0,            // second
     82       45,           // minute
     83       12,           // hour
     84       15,           // day of month
     85       10 - 1,       // month
     86       2007 - 1900,  // year
     87       0,            // day of week (ignored, output only)
     88       0,            // day of year (ignored, output only)
     89       -1            // DST in effect, -1 tells mktime to figure it out
     90     };
     91 
     92     time_t converted_time = mktime(&local_comparison_tm);
     93     ASSERT_GT(converted_time, 0);
     94     comparison_time_local_ = Time::FromTimeT(converted_time);
     95 
     96     // time_t representation of 15th Oct 2007 12:45:00 PDT
     97     comparison_time_pdt_ = Time::FromTimeT(1192477500);
     98   }
     99 
    100   Time comparison_time_local_;
    101   Time comparison_time_pdt_;
    102 };
    103 
    104 // Test conversions to/from time_t and exploding/unexploding.
    105 TEST_F(TimeTest, TimeT) {
    106   // C library time and exploded time.
    107   time_t now_t_1 = time(NULL);
    108   struct tm tms;
    109 #if defined(OS_WIN)
    110   localtime_s(&tms, &now_t_1);
    111 #elif defined(OS_POSIX)
    112   localtime_r(&now_t_1, &tms);
    113 #endif
    114 
    115   // Convert to ours.
    116   Time our_time_1 = Time::FromTimeT(now_t_1);
    117   Time::Exploded exploded;
    118   our_time_1.LocalExplode(&exploded);
    119 
    120   // This will test both our exploding and our time_t -> Time conversion.
    121   EXPECT_EQ(tms.tm_year + 1900, exploded.year);
    122   EXPECT_EQ(tms.tm_mon + 1, exploded.month);
    123   EXPECT_EQ(tms.tm_mday, exploded.day_of_month);
    124   EXPECT_EQ(tms.tm_hour, exploded.hour);
    125   EXPECT_EQ(tms.tm_min, exploded.minute);
    126   EXPECT_EQ(tms.tm_sec, exploded.second);
    127 
    128   // Convert exploded back to the time struct.
    129   Time our_time_2;
    130   EXPECT_TRUE(Time::FromLocalExploded(exploded, &our_time_2));
    131   EXPECT_TRUE(our_time_1 == our_time_2);
    132 
    133   time_t now_t_2 = our_time_2.ToTimeT();
    134   EXPECT_EQ(now_t_1, now_t_2);
    135 
    136   EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT());
    137   EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT());
    138 
    139   // Conversions of 0 should stay 0.
    140   EXPECT_EQ(0, Time().ToTimeT());
    141   EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
    142 }
    143 
    144 // Test conversions to/from javascript time.
    145 TEST_F(TimeTest, JsTime) {
    146   Time epoch = Time::FromJsTime(0.0);
    147   EXPECT_EQ(epoch, Time::UnixEpoch());
    148   Time t = Time::FromJsTime(700000.3);
    149   EXPECT_EQ(700.0003, t.ToDoubleT());
    150   t = Time::FromDoubleT(800.73);
    151   EXPECT_EQ(800730.0, t.ToJsTime());
    152 }
    153 
    154 #if defined(OS_POSIX)
    155 TEST_F(TimeTest, FromTimeVal) {
    156   Time now = Time::Now();
    157   Time also_now = Time::FromTimeVal(now.ToTimeVal());
    158   EXPECT_EQ(now, also_now);
    159 }
    160 #endif  // OS_POSIX
    161 
    162 TEST_F(TimeTest, FromExplodedWithMilliseconds) {
    163   // Some platform implementations of FromExploded are liable to drop
    164   // milliseconds if we aren't careful.
    165   Time now = Time::NowFromSystemTime();
    166   Time::Exploded exploded1 = {0};
    167   now.UTCExplode(&exploded1);
    168   exploded1.millisecond = 500;
    169   Time time;
    170   EXPECT_TRUE(Time::FromUTCExploded(exploded1, &time));
    171   Time::Exploded exploded2 = {0};
    172   time.UTCExplode(&exploded2);
    173   EXPECT_EQ(exploded1.millisecond, exploded2.millisecond);
    174 }
    175 
    176 TEST_F(TimeTest, ZeroIsSymmetric) {
    177   Time zero_time(Time::FromTimeT(0));
    178   EXPECT_EQ(0, zero_time.ToTimeT());
    179 
    180   EXPECT_EQ(0.0, zero_time.ToDoubleT());
    181 }
    182 
    183 TEST_F(TimeTest, LocalExplode) {
    184   Time a = Time::Now();
    185   Time::Exploded exploded;
    186   a.LocalExplode(&exploded);
    187 
    188   Time b;
    189   EXPECT_TRUE(Time::FromLocalExploded(exploded, &b));
    190 
    191   // The exploded structure doesn't have microseconds, and on Mac & Linux, the
    192   // internal OS conversion uses seconds, which will cause truncation. So we
    193   // can only make sure that the delta is within one second.
    194   EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
    195 }
    196 
    197 TEST_F(TimeTest, UTCExplode) {
    198   Time a = Time::Now();
    199   Time::Exploded exploded;
    200   a.UTCExplode(&exploded);
    201 
    202   Time b;
    203   EXPECT_TRUE(Time::FromUTCExploded(exploded, &b));
    204   EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
    205 }
    206 
    207 TEST_F(TimeTest, LocalMidnight) {
    208   Time::Exploded exploded;
    209   Time::Now().LocalMidnight().LocalExplode(&exploded);
    210   EXPECT_EQ(0, exploded.hour);
    211   EXPECT_EQ(0, exploded.minute);
    212   EXPECT_EQ(0, exploded.second);
    213   EXPECT_EQ(0, exploded.millisecond);
    214 }
    215 
    216 TEST_F(TimeTest, ParseTimeTest1) {
    217   time_t current_time = 0;
    218   time(&current_time);
    219 
    220   const int BUFFER_SIZE = 64;
    221   struct tm local_time = {0};
    222   char time_buf[BUFFER_SIZE] = {0};
    223 #if defined(OS_WIN)
    224   localtime_s(&local_time, &current_time);
    225   asctime_s(time_buf, arraysize(time_buf), &local_time);
    226 #elif defined(OS_POSIX)
    227   localtime_r(&current_time, &local_time);
    228   asctime_r(&local_time, time_buf);
    229 #endif
    230 
    231   Time parsed_time;
    232   EXPECT_TRUE(Time::FromString(time_buf, &parsed_time));
    233   EXPECT_EQ(current_time, parsed_time.ToTimeT());
    234 }
    235 
    236 TEST_F(TimeTest, DayOfWeekSunday) {
    237   Time time;
    238   EXPECT_TRUE(Time::FromString("Sun, 06 May 2012 12:00:00 GMT", &time));
    239   Time::Exploded exploded;
    240   time.UTCExplode(&exploded);
    241   EXPECT_EQ(0, exploded.day_of_week);
    242 }
    243 
    244 TEST_F(TimeTest, DayOfWeekWednesday) {
    245   Time time;
    246   EXPECT_TRUE(Time::FromString("Wed, 09 May 2012 12:00:00 GMT", &time));
    247   Time::Exploded exploded;
    248   time.UTCExplode(&exploded);
    249   EXPECT_EQ(3, exploded.day_of_week);
    250 }
    251 
    252 TEST_F(TimeTest, DayOfWeekSaturday) {
    253   Time time;
    254   EXPECT_TRUE(Time::FromString("Sat, 12 May 2012 12:00:00 GMT", &time));
    255   Time::Exploded exploded;
    256   time.UTCExplode(&exploded);
    257   EXPECT_EQ(6, exploded.day_of_week);
    258 }
    259 
    260 TEST_F(TimeTest, ParseTimeTest2) {
    261   Time parsed_time;
    262   EXPECT_TRUE(Time::FromString("Mon, 15 Oct 2007 19:45:00 GMT", &parsed_time));
    263   EXPECT_EQ(comparison_time_pdt_, parsed_time);
    264 }
    265 
    266 TEST_F(TimeTest, ParseTimeTest3) {
    267   Time parsed_time;
    268   EXPECT_TRUE(Time::FromString("15 Oct 07 12:45:00", &parsed_time));
    269   EXPECT_EQ(comparison_time_local_, parsed_time);
    270 }
    271 
    272 TEST_F(TimeTest, ParseTimeTest4) {
    273   Time parsed_time;
    274   EXPECT_TRUE(Time::FromString("15 Oct 07 19:45 GMT", &parsed_time));
    275   EXPECT_EQ(comparison_time_pdt_, parsed_time);
    276 }
    277 
    278 TEST_F(TimeTest, ParseTimeTest5) {
    279   Time parsed_time;
    280   EXPECT_TRUE(Time::FromString("Mon Oct 15 12:45 PDT 2007", &parsed_time));
    281   EXPECT_EQ(comparison_time_pdt_, parsed_time);
    282 }
    283 
    284 TEST_F(TimeTest, ParseTimeTest6) {
    285   Time parsed_time;
    286   EXPECT_TRUE(Time::FromString("Monday, Oct 15, 2007 12:45 PM", &parsed_time));
    287   EXPECT_EQ(comparison_time_local_, parsed_time);
    288 }
    289 
    290 TEST_F(TimeTest, ParseTimeTest7) {
    291   Time parsed_time;
    292   EXPECT_TRUE(Time::FromString("10/15/07 12:45:00 PM", &parsed_time));
    293   EXPECT_EQ(comparison_time_local_, parsed_time);
    294 }
    295 
    296 TEST_F(TimeTest, ParseTimeTest8) {
    297   Time parsed_time;
    298   EXPECT_TRUE(Time::FromString("15-OCT-2007 12:45pm", &parsed_time));
    299   EXPECT_EQ(comparison_time_local_, parsed_time);
    300 }
    301 
    302 TEST_F(TimeTest, ParseTimeTest9) {
    303   Time parsed_time;
    304   EXPECT_TRUE(Time::FromString("16 Oct 2007 4:45-JST (Tuesday)", &parsed_time));
    305   EXPECT_EQ(comparison_time_pdt_, parsed_time);
    306 }
    307 
    308 TEST_F(TimeTest, ParseTimeTest10) {
    309   Time parsed_time;
    310   EXPECT_TRUE(Time::FromString("15/10/07 12:45", &parsed_time));
    311   EXPECT_EQ(parsed_time, comparison_time_local_);
    312 }
    313 
    314 // Test some of edge cases around epoch, etc.
    315 TEST_F(TimeTest, ParseTimeTestEpoch0) {
    316   Time parsed_time;
    317 
    318   // time_t == epoch == 0
    319   EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:00 +0100 1970",
    320                                &parsed_time));
    321   EXPECT_EQ(0, parsed_time.ToTimeT());
    322   EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:00 GMT 1970",
    323                                &parsed_time));
    324   EXPECT_EQ(0, parsed_time.ToTimeT());
    325 }
    326 
    327 TEST_F(TimeTest, ParseTimeTestEpoch1) {
    328   Time parsed_time;
    329 
    330   // time_t == 1 second after epoch == 1
    331   EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:01 +0100 1970",
    332                                &parsed_time));
    333   EXPECT_EQ(1, parsed_time.ToTimeT());
    334   EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:01 GMT 1970",
    335                                &parsed_time));
    336   EXPECT_EQ(1, parsed_time.ToTimeT());
    337 }
    338 
    339 TEST_F(TimeTest, ParseTimeTestEpoch2) {
    340   Time parsed_time;
    341 
    342   // time_t == 2 seconds after epoch == 2
    343   EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:02 +0100 1970",
    344                                &parsed_time));
    345   EXPECT_EQ(2, parsed_time.ToTimeT());
    346   EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:02 GMT 1970",
    347                                &parsed_time));
    348   EXPECT_EQ(2, parsed_time.ToTimeT());
    349 }
    350 
    351 TEST_F(TimeTest, ParseTimeTestEpochNeg1) {
    352   Time parsed_time;
    353 
    354   // time_t == 1 second before epoch == -1
    355   EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:59 +0100 1970",
    356                                &parsed_time));
    357   EXPECT_EQ(-1, parsed_time.ToTimeT());
    358   EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 1969",
    359                                &parsed_time));
    360   EXPECT_EQ(-1, parsed_time.ToTimeT());
    361 }
    362 
    363 // If time_t is 32 bits, a date after year 2038 will overflow time_t and
    364 // cause timegm() to return -1.  The parsed time should not be 1 second
    365 // before epoch.
    366 TEST_F(TimeTest, ParseTimeTestEpochNotNeg1) {
    367   Time parsed_time;
    368 
    369   EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 2100",
    370                                &parsed_time));
    371   EXPECT_NE(-1, parsed_time.ToTimeT());
    372 }
    373 
    374 TEST_F(TimeTest, ParseTimeTestEpochNeg2) {
    375   Time parsed_time;
    376 
    377   // time_t == 2 seconds before epoch == -2
    378   EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:58 +0100 1970",
    379                                &parsed_time));
    380   EXPECT_EQ(-2, parsed_time.ToTimeT());
    381   EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:58 GMT 1969",
    382                                &parsed_time));
    383   EXPECT_EQ(-2, parsed_time.ToTimeT());
    384 }
    385 
    386 TEST_F(TimeTest, ParseTimeTestEpoch1960) {
    387   Time parsed_time;
    388 
    389   // time_t before Epoch, in 1960
    390   EXPECT_TRUE(Time::FromString("Wed Jun 29 19:40:01 +0100 1960",
    391                                &parsed_time));
    392   EXPECT_EQ(-299999999, parsed_time.ToTimeT());
    393   EXPECT_TRUE(Time::FromString("Wed Jun 29 18:40:01 GMT 1960",
    394                                &parsed_time));
    395   EXPECT_EQ(-299999999, parsed_time.ToTimeT());
    396   EXPECT_TRUE(Time::FromString("Wed Jun 29 17:40:01 GMT 1960",
    397                                &parsed_time));
    398   EXPECT_EQ(-300003599, parsed_time.ToTimeT());
    399 }
    400 
    401 TEST_F(TimeTest, ParseTimeTestEmpty) {
    402   Time parsed_time;
    403   EXPECT_FALSE(Time::FromString("", &parsed_time));
    404 }
    405 
    406 TEST_F(TimeTest, ParseTimeTestInvalidString) {
    407   Time parsed_time;
    408   EXPECT_FALSE(Time::FromString("Monday morning 2000", &parsed_time));
    409 }
    410 
    411 TEST_F(TimeTest, ExplodeBeforeUnixEpoch) {
    412   static const int kUnixEpochYear = 1970;  // In case this changes (ha!).
    413   Time t;
    414   Time::Exploded exploded;
    415 
    416   t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1);
    417   t.UTCExplode(&exploded);
    418   EXPECT_TRUE(exploded.HasValidValues());
    419   // Should be 1969-12-31 23:59:59 999 milliseconds (and 999 microseconds).
    420   EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
    421   EXPECT_EQ(12, exploded.month);
    422   EXPECT_EQ(31, exploded.day_of_month);
    423   EXPECT_EQ(23, exploded.hour);
    424   EXPECT_EQ(59, exploded.minute);
    425   EXPECT_EQ(59, exploded.second);
    426   EXPECT_EQ(999, exploded.millisecond);
    427 
    428   t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1000);
    429   t.UTCExplode(&exploded);
    430   EXPECT_TRUE(exploded.HasValidValues());
    431   // Should be 1969-12-31 23:59:59 999 milliseconds.
    432   EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
    433   EXPECT_EQ(12, exploded.month);
    434   EXPECT_EQ(31, exploded.day_of_month);
    435   EXPECT_EQ(23, exploded.hour);
    436   EXPECT_EQ(59, exploded.minute);
    437   EXPECT_EQ(59, exploded.second);
    438   EXPECT_EQ(999, exploded.millisecond);
    439 
    440   t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1001);
    441   t.UTCExplode(&exploded);
    442   EXPECT_TRUE(exploded.HasValidValues());
    443   // Should be 1969-12-31 23:59:59 998 milliseconds (and 999 microseconds).
    444   EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
    445   EXPECT_EQ(12, exploded.month);
    446   EXPECT_EQ(31, exploded.day_of_month);
    447   EXPECT_EQ(23, exploded.hour);
    448   EXPECT_EQ(59, exploded.minute);
    449   EXPECT_EQ(59, exploded.second);
    450   EXPECT_EQ(998, exploded.millisecond);
    451 
    452   t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1000);
    453   t.UTCExplode(&exploded);
    454   EXPECT_TRUE(exploded.HasValidValues());
    455   // Should be 1969-12-31 23:59:59.
    456   EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
    457   EXPECT_EQ(12, exploded.month);
    458   EXPECT_EQ(31, exploded.day_of_month);
    459   EXPECT_EQ(23, exploded.hour);
    460   EXPECT_EQ(59, exploded.minute);
    461   EXPECT_EQ(59, exploded.second);
    462   EXPECT_EQ(0, exploded.millisecond);
    463 
    464   t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1001);
    465   t.UTCExplode(&exploded);
    466   EXPECT_TRUE(exploded.HasValidValues());
    467   // Should be 1969-12-31 23:59:58 999 milliseconds.
    468   EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
    469   EXPECT_EQ(12, exploded.month);
    470   EXPECT_EQ(31, exploded.day_of_month);
    471   EXPECT_EQ(23, exploded.hour);
    472   EXPECT_EQ(59, exploded.minute);
    473   EXPECT_EQ(58, exploded.second);
    474   EXPECT_EQ(999, exploded.millisecond);
    475 
    476   // Make sure we still handle at/after Unix epoch correctly.
    477   t = Time::UnixEpoch();
    478   t.UTCExplode(&exploded);
    479   EXPECT_TRUE(exploded.HasValidValues());
    480   // Should be 1970-12-31 00:00:00 0 milliseconds.
    481   EXPECT_EQ(kUnixEpochYear, exploded.year);
    482   EXPECT_EQ(1, exploded.month);
    483   EXPECT_EQ(1, exploded.day_of_month);
    484   EXPECT_EQ(0, exploded.hour);
    485   EXPECT_EQ(0, exploded.minute);
    486   EXPECT_EQ(0, exploded.second);
    487   EXPECT_EQ(0, exploded.millisecond);
    488 
    489   t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1);
    490   t.UTCExplode(&exploded);
    491   EXPECT_TRUE(exploded.HasValidValues());
    492   // Should be 1970-01-01 00:00:00 0 milliseconds (and 1 microsecond).
    493   EXPECT_EQ(kUnixEpochYear, exploded.year);
    494   EXPECT_EQ(1, exploded.month);
    495   EXPECT_EQ(1, exploded.day_of_month);
    496   EXPECT_EQ(0, exploded.hour);
    497   EXPECT_EQ(0, exploded.minute);
    498   EXPECT_EQ(0, exploded.second);
    499   EXPECT_EQ(0, exploded.millisecond);
    500 
    501   t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1000);
    502   t.UTCExplode(&exploded);
    503   EXPECT_TRUE(exploded.HasValidValues());
    504   // Should be 1970-01-01 00:00:00 1 millisecond.
    505   EXPECT_EQ(kUnixEpochYear, exploded.year);
    506   EXPECT_EQ(1, exploded.month);
    507   EXPECT_EQ(1, exploded.day_of_month);
    508   EXPECT_EQ(0, exploded.hour);
    509   EXPECT_EQ(0, exploded.minute);
    510   EXPECT_EQ(0, exploded.second);
    511   EXPECT_EQ(1, exploded.millisecond);
    512 
    513   t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1000);
    514   t.UTCExplode(&exploded);
    515   EXPECT_TRUE(exploded.HasValidValues());
    516   // Should be 1970-01-01 00:00:01.
    517   EXPECT_EQ(kUnixEpochYear, exploded.year);
    518   EXPECT_EQ(1, exploded.month);
    519   EXPECT_EQ(1, exploded.day_of_month);
    520   EXPECT_EQ(0, exploded.hour);
    521   EXPECT_EQ(0, exploded.minute);
    522   EXPECT_EQ(1, exploded.second);
    523   EXPECT_EQ(0, exploded.millisecond);
    524 
    525   t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1001);
    526   t.UTCExplode(&exploded);
    527   EXPECT_TRUE(exploded.HasValidValues());
    528   // Should be 1970-01-01 00:00:01 1 millisecond.
    529   EXPECT_EQ(kUnixEpochYear, exploded.year);
    530   EXPECT_EQ(1, exploded.month);
    531   EXPECT_EQ(1, exploded.day_of_month);
    532   EXPECT_EQ(0, exploded.hour);
    533   EXPECT_EQ(0, exploded.minute);
    534   EXPECT_EQ(1, exploded.second);
    535   EXPECT_EQ(1, exploded.millisecond);
    536 }
    537 
    538 TEST_F(TimeTest, Max) {
    539   Time max = Time::Max();
    540   EXPECT_TRUE(max.is_max());
    541   EXPECT_EQ(max, Time::Max());
    542   EXPECT_GT(max, Time::Now());
    543   EXPECT_GT(max, Time());
    544 }
    545 
    546 TEST_F(TimeTest, MaxConversions) {
    547   Time t = Time::Max();
    548   EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.ToInternalValue());
    549 
    550   t = Time::FromDoubleT(std::numeric_limits<double>::infinity());
    551   EXPECT_TRUE(t.is_max());
    552   EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToDoubleT());
    553 
    554   t = Time::FromJsTime(std::numeric_limits<double>::infinity());
    555   EXPECT_TRUE(t.is_max());
    556   EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToJsTime());
    557 
    558   t = Time::FromTimeT(std::numeric_limits<time_t>::max());
    559   EXPECT_TRUE(t.is_max());
    560   EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT());
    561 
    562 #if defined(OS_POSIX)
    563   struct timeval tval;
    564   tval.tv_sec = std::numeric_limits<time_t>::max();
    565   tval.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1;
    566   t = Time::FromTimeVal(tval);
    567   EXPECT_TRUE(t.is_max());
    568   tval = t.ToTimeVal();
    569   EXPECT_EQ(std::numeric_limits<time_t>::max(), tval.tv_sec);
    570   EXPECT_EQ(static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1,
    571       tval.tv_usec);
    572 #endif
    573 
    574 #if defined(OS_MACOSX)
    575   t = Time::FromCFAbsoluteTime(std::numeric_limits<CFAbsoluteTime>::infinity());
    576   EXPECT_TRUE(t.is_max());
    577   EXPECT_EQ(std::numeric_limits<CFAbsoluteTime>::infinity(),
    578             t.ToCFAbsoluteTime());
    579 #endif
    580 
    581 #if defined(OS_WIN)
    582   FILETIME ftime;
    583   ftime.dwHighDateTime = std::numeric_limits<DWORD>::max();
    584   ftime.dwLowDateTime = std::numeric_limits<DWORD>::max();
    585   t = Time::FromFileTime(ftime);
    586   EXPECT_TRUE(t.is_max());
    587   ftime = t.ToFileTime();
    588   EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwHighDateTime);
    589   EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwLowDateTime);
    590 #endif
    591 }
    592 
    593 #if defined(OS_MACOSX)
    594 TEST_F(TimeTest, TimeTOverflow) {
    595   Time t = Time::FromInternalValue(std::numeric_limits<int64_t>::max() - 1);
    596   EXPECT_FALSE(t.is_max());
    597   EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT());
    598 }
    599 #endif
    600 
    601 #if defined(OS_ANDROID)
    602 TEST_F(TimeTest, FromLocalExplodedCrashOnAndroid) {
    603   // This crashed inside Time:: FromLocalExploded() on Android 4.1.2.
    604   // See http://crbug.com/287821
    605   Time::Exploded midnight = {2013,  // year
    606                              10,    // month
    607                              0,     // day_of_week
    608                              13,    // day_of_month
    609                              0,     // hour
    610                              0,     // minute
    611                              0,     // second
    612   };
    613   // The string passed to putenv() must be a char* and the documentation states
    614   // that it 'becomes part of the environment', so use a static buffer.
    615   static char buffer[] = "TZ=America/Santiago";
    616   putenv(buffer);
    617   tzset();
    618   Time t;
    619   EXPECT_TRUE(Time::FromLocalExploded(midnight, &t));
    620   EXPECT_EQ(1381633200, t.ToTimeT());
    621 }
    622 #endif  // OS_ANDROID
    623 
    624 TEST(TimeTicks, Deltas) {
    625   for (int index = 0; index < 50; index++) {
    626     TimeTicks ticks_start = TimeTicks::Now();
    627     base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
    628     TimeTicks ticks_stop = TimeTicks::Now();
    629     TimeDelta delta = ticks_stop - ticks_start;
    630     // Note:  Although we asked for a 10ms sleep, if the
    631     // time clock has a finer granularity than the Sleep()
    632     // clock, it is quite possible to wakeup early.  Here
    633     // is how that works:
    634     //      Time(ms timer)      Time(us timer)
    635     //          5                   5010
    636     //          6                   6010
    637     //          7                   7010
    638     //          8                   8010
    639     //          9                   9000
    640     // Elapsed  4ms                 3990us
    641     //
    642     // Unfortunately, our InMilliseconds() function truncates
    643     // rather than rounds.  We should consider fixing this
    644     // so that our averages come out better.
    645     EXPECT_GE(delta.InMilliseconds(), 9);
    646     EXPECT_GE(delta.InMicroseconds(), 9000);
    647     EXPECT_EQ(delta.InSeconds(), 0);
    648   }
    649 }
    650 
    651 static void HighResClockTest(TimeTicks (*GetTicks)()) {
    652   // IsHighResolution() is false on some systems.  Since the product still works
    653   // even if it's false, it makes this entire test questionable.
    654   if (!TimeTicks::IsHighResolution())
    655     return;
    656 
    657   // Why do we loop here?
    658   // We're trying to measure that intervals increment in a VERY small amount
    659   // of time --  less than 15ms.  Unfortunately, if we happen to have a
    660   // context switch in the middle of our test, the context switch could easily
    661   // exceed our limit.  So, we iterate on this several times.  As long as we're
    662   // able to detect the fine-granularity timers at least once, then the test
    663   // has succeeded.
    664 
    665   const int kTargetGranularityUs = 15000;  // 15ms
    666 
    667   bool success = false;
    668   int retries = 100;  // Arbitrary.
    669   TimeDelta delta;
    670   while (!success && retries--) {
    671     TimeTicks ticks_start = GetTicks();
    672     // Loop until we can detect that the clock has changed.  Non-HighRes timers
    673     // will increment in chunks, e.g. 15ms.  By spinning until we see a clock
    674     // change, we detect the minimum time between measurements.
    675     do {
    676       delta = GetTicks() - ticks_start;
    677     } while (delta.InMilliseconds() == 0);
    678 
    679     if (delta.InMicroseconds() <= kTargetGranularityUs)
    680       success = true;
    681   }
    682 
    683   // In high resolution mode, we expect to see the clock increment
    684   // in intervals less than 15ms.
    685   EXPECT_TRUE(success);
    686 }
    687 
    688 TEST(TimeTicks, HighRes) {
    689   HighResClockTest(&TimeTicks::Now);
    690 }
    691 
    692 // Fails frequently on Android http://crbug.com/352633 with:
    693 // Expected: (delta_thread.InMicroseconds()) > (0), actual: 0 vs 0
    694 #if defined(OS_ANDROID)
    695 #define MAYBE_ThreadNow DISABLED_ThreadNow
    696 #else
    697 #define MAYBE_ThreadNow ThreadNow
    698 #endif
    699 TEST(ThreadTicks, MAYBE_ThreadNow) {
    700   if (ThreadTicks::IsSupported()) {
    701     ThreadTicks::WaitUntilInitialized();
    702     TimeTicks begin = TimeTicks::Now();
    703     ThreadTicks begin_thread = ThreadTicks::Now();
    704     // Make sure that ThreadNow value is non-zero.
    705     EXPECT_GT(begin_thread, ThreadTicks());
    706     // Sleep for 10 milliseconds to get the thread de-scheduled.
    707     base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
    708     ThreadTicks end_thread = ThreadTicks::Now();
    709     TimeTicks end = TimeTicks::Now();
    710     TimeDelta delta = end - begin;
    711     TimeDelta delta_thread = end_thread - begin_thread;
    712     // Make sure that some thread time have elapsed.
    713     EXPECT_GT(delta_thread.InMicroseconds(), 0);
    714     // But the thread time is at least 9ms less than clock time.
    715     TimeDelta difference = delta - delta_thread;
    716     EXPECT_GE(difference.InMicroseconds(), 9000);
    717   }
    718 }
    719 
    720 TEST(TimeTicks, SnappedToNextTickBasic) {
    721   base::TimeTicks phase = base::TimeTicks::FromInternalValue(4000);
    722   base::TimeDelta interval = base::TimeDelta::FromMicroseconds(1000);
    723   base::TimeTicks timestamp;
    724 
    725   // Timestamp in previous interval.
    726   timestamp = base::TimeTicks::FromInternalValue(3500);
    727   EXPECT_EQ(4000,
    728             timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
    729 
    730   // Timestamp in next interval.
    731   timestamp = base::TimeTicks::FromInternalValue(4500);
    732   EXPECT_EQ(5000,
    733             timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
    734 
    735   // Timestamp multiple intervals before.
    736   timestamp = base::TimeTicks::FromInternalValue(2500);
    737   EXPECT_EQ(3000,
    738             timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
    739 
    740   // Timestamp multiple intervals after.
    741   timestamp = base::TimeTicks::FromInternalValue(6500);
    742   EXPECT_EQ(7000,
    743             timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
    744 
    745   // Timestamp on previous interval.
    746   timestamp = base::TimeTicks::FromInternalValue(3000);
    747   EXPECT_EQ(3000,
    748             timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
    749 
    750   // Timestamp on next interval.
    751   timestamp = base::TimeTicks::FromInternalValue(5000);
    752   EXPECT_EQ(5000,
    753             timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
    754 
    755   // Timestamp equal to phase.
    756   timestamp = base::TimeTicks::FromInternalValue(4000);
    757   EXPECT_EQ(4000,
    758             timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
    759 }
    760 
    761 TEST(TimeTicks, SnappedToNextTickOverflow) {
    762   // int(big_timestamp / interval) < 0, so this causes a crash if the number of
    763   // intervals elapsed is attempted to be stored in an int.
    764   base::TimeTicks phase = base::TimeTicks::FromInternalValue(0);
    765   base::TimeDelta interval = base::TimeDelta::FromMicroseconds(4000);
    766   base::TimeTicks big_timestamp =
    767       base::TimeTicks::FromInternalValue(8635916564000);
    768 
    769   EXPECT_EQ(8635916564000,
    770             big_timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
    771   EXPECT_EQ(8635916564000,
    772             big_timestamp.SnappedToNextTick(big_timestamp, interval)
    773                 .ToInternalValue());
    774 }
    775 
    776 TEST(TimeDelta, FromAndIn) {
    777   // static_assert also checks that the contained expression is a constant
    778   // expression, meaning all its components are suitable for initializing global
    779   // variables.
    780   static_assert(TimeDelta::FromDays(2) == TimeDelta::FromHours(48), "");
    781   static_assert(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180), "");
    782   static_assert(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120), "");
    783   static_assert(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000),
    784                 "");
    785   static_assert(
    786       TimeDelta::FromMilliseconds(2) == TimeDelta::FromMicroseconds(2000), "");
    787   static_assert(
    788       TimeDelta::FromSecondsD(2.3) == TimeDelta::FromMilliseconds(2300), "");
    789   static_assert(
    790       TimeDelta::FromMillisecondsD(2.5) == TimeDelta::FromMicroseconds(2500),
    791       "");
    792   EXPECT_EQ(13, TimeDelta::FromDays(13).InDays());
    793   EXPECT_EQ(13, TimeDelta::FromHours(13).InHours());
    794   EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes());
    795   EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds());
    796   EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF());
    797   EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds());
    798   EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF());
    799   EXPECT_EQ(13, TimeDelta::FromSecondsD(13.1).InSeconds());
    800   EXPECT_EQ(13.1, TimeDelta::FromSecondsD(13.1).InSecondsF());
    801   EXPECT_EQ(13, TimeDelta::FromMillisecondsD(13.3).InMilliseconds());
    802   EXPECT_EQ(13.3, TimeDelta::FromMillisecondsD(13.3).InMillisecondsF());
    803   EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds());
    804   EXPECT_EQ(3.456, TimeDelta::FromMillisecondsD(3.45678).InMillisecondsF());
    805 }
    806 
    807 #if defined(OS_POSIX)
    808 TEST(TimeDelta, TimeSpecConversion) {
    809   struct timespec result = TimeDelta::FromSeconds(0).ToTimeSpec();
    810   EXPECT_EQ(result.tv_sec, 0);
    811   EXPECT_EQ(result.tv_nsec, 0);
    812 
    813   result = TimeDelta::FromSeconds(1).ToTimeSpec();
    814   EXPECT_EQ(result.tv_sec, 1);
    815   EXPECT_EQ(result.tv_nsec, 0);
    816 
    817   result = TimeDelta::FromMicroseconds(1).ToTimeSpec();
    818   EXPECT_EQ(result.tv_sec, 0);
    819   EXPECT_EQ(result.tv_nsec, 1000);
    820 
    821   result = TimeDelta::FromMicroseconds(
    822       Time::kMicrosecondsPerSecond + 1).ToTimeSpec();
    823   EXPECT_EQ(result.tv_sec, 1);
    824   EXPECT_EQ(result.tv_nsec, 1000);
    825 }
    826 #endif  // OS_POSIX
    827 
    828 // Our internal time format is serialized in things like databases, so it's
    829 // important that it's consistent across all our platforms.  We use the 1601
    830 // Windows epoch as the internal format across all platforms.
    831 TEST(TimeDelta, WindowsEpoch) {
    832   Time::Exploded exploded;
    833   exploded.year = 1970;
    834   exploded.month = 1;
    835   exploded.day_of_week = 0;  // Should be unusued.
    836   exploded.day_of_month = 1;
    837   exploded.hour = 0;
    838   exploded.minute = 0;
    839   exploded.second = 0;
    840   exploded.millisecond = 0;
    841   Time t;
    842   EXPECT_TRUE(Time::FromUTCExploded(exploded, &t));
    843   // Unix 1970 epoch.
    844   EXPECT_EQ(INT64_C(11644473600000000), t.ToInternalValue());
    845 
    846   // We can't test 1601 epoch, since the system time functions on Linux
    847   // only compute years starting from 1900.
    848 }
    849 
    850 // We could define this separately for Time, TimeTicks and TimeDelta but the
    851 // definitions would be identical anyway.
    852 template <class Any>
    853 std::string AnyToString(Any any) {
    854   std::ostringstream oss;
    855   oss << any;
    856   return oss.str();
    857 }
    858 
    859 TEST(TimeDelta, Magnitude) {
    860   const int64_t zero = 0;
    861   EXPECT_EQ(TimeDelta::FromMicroseconds(zero),
    862             TimeDelta::FromMicroseconds(zero).magnitude());
    863 
    864   const int64_t one = 1;
    865   const int64_t negative_one = -1;
    866   EXPECT_EQ(TimeDelta::FromMicroseconds(one),
    867             TimeDelta::FromMicroseconds(one).magnitude());
    868   EXPECT_EQ(TimeDelta::FromMicroseconds(one),
    869             TimeDelta::FromMicroseconds(negative_one).magnitude());
    870 
    871   const int64_t max_int64_minus_one = std::numeric_limits<int64_t>::max() - 1;
    872   const int64_t min_int64_plus_two = std::numeric_limits<int64_t>::min() + 2;
    873   EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one),
    874             TimeDelta::FromMicroseconds(max_int64_minus_one).magnitude());
    875   EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one),
    876             TimeDelta::FromMicroseconds(min_int64_plus_two).magnitude());
    877 }
    878 
    879 TEST(TimeDelta, Max) {
    880   TimeDelta max = TimeDelta::Max();
    881   EXPECT_TRUE(max.is_max());
    882   EXPECT_EQ(max, TimeDelta::Max());
    883   EXPECT_GT(max, TimeDelta::FromDays(100 * 365));
    884   EXPECT_GT(max, TimeDelta());
    885 }
    886 
    887 bool IsMin(TimeDelta delta) {
    888   return (-delta).is_max();
    889 }
    890 
    891 TEST(TimeDelta, MaxConversions) {
    892   TimeDelta t = TimeDelta::Max();
    893   EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.ToInternalValue());
    894 
    895   EXPECT_EQ(std::numeric_limits<int>::max(), t.InDays());
    896   EXPECT_EQ(std::numeric_limits<int>::max(), t.InHours());
    897   EXPECT_EQ(std::numeric_limits<int>::max(), t.InMinutes());
    898   EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InSecondsF());
    899   EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.InSeconds());
    900   EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InMillisecondsF());
    901   EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.InMilliseconds());
    902   EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.InMillisecondsRoundedUp());
    903 
    904   t = TimeDelta::FromDays(std::numeric_limits<int>::max());
    905   EXPECT_TRUE(t.is_max());
    906 
    907   t = TimeDelta::FromHours(std::numeric_limits<int>::max());
    908   EXPECT_TRUE(t.is_max());
    909 
    910   t = TimeDelta::FromMinutes(std::numeric_limits<int>::max());
    911   EXPECT_TRUE(t.is_max());
    912 
    913   int64_t max_int = std::numeric_limits<int64_t>::max();
    914 
    915   t = TimeDelta::FromSeconds(max_int / Time::kMicrosecondsPerSecond + 1);
    916   EXPECT_TRUE(t.is_max());
    917 
    918   t = TimeDelta::FromMilliseconds(max_int / Time::kMillisecondsPerSecond + 1);
    919   EXPECT_TRUE(t.is_max());
    920 
    921   t = TimeDelta::FromMicroseconds(max_int);
    922   EXPECT_TRUE(t.is_max());
    923 
    924   t = TimeDelta::FromSeconds(-max_int / Time::kMicrosecondsPerSecond - 1);
    925   EXPECT_TRUE(IsMin(t));
    926 
    927   t = TimeDelta::FromMilliseconds(-max_int / Time::kMillisecondsPerSecond - 1);
    928   EXPECT_TRUE(IsMin(t));
    929 
    930   t = TimeDelta::FromMicroseconds(-max_int);
    931   EXPECT_TRUE(IsMin(t));
    932 
    933   t = -TimeDelta::FromMicroseconds(std::numeric_limits<int64_t>::min());
    934   EXPECT_FALSE(IsMin(t));
    935 
    936   t = TimeDelta::FromSecondsD(std::numeric_limits<double>::infinity());
    937   EXPECT_TRUE(t.is_max());
    938 
    939   double max_d = max_int;
    940 
    941   t = TimeDelta::FromSecondsD(max_d / Time::kMicrosecondsPerSecond + 1);
    942   EXPECT_TRUE(t.is_max());
    943 
    944   t = TimeDelta::FromMillisecondsD(std::numeric_limits<double>::infinity());
    945   EXPECT_TRUE(t.is_max());
    946 
    947   t = TimeDelta::FromMillisecondsD(max_d / Time::kMillisecondsPerSecond * 2);
    948   EXPECT_TRUE(t.is_max());
    949 
    950   t = TimeDelta::FromSecondsD(-max_d / Time::kMicrosecondsPerSecond - 1);
    951   EXPECT_TRUE(IsMin(t));
    952 
    953   t = TimeDelta::FromMillisecondsD(-max_d / Time::kMillisecondsPerSecond * 2);
    954   EXPECT_TRUE(IsMin(t));
    955 }
    956 
    957 TEST(TimeDelta, NumericOperators) {
    958   double d = 0.5;
    959   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
    960             TimeDelta::FromMilliseconds(1000) * d);
    961   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
    962             TimeDelta::FromMilliseconds(1000) / d);
    963   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
    964             TimeDelta::FromMilliseconds(1000) *= d);
    965   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
    966             TimeDelta::FromMilliseconds(1000) /= d);
    967   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
    968             d * TimeDelta::FromMilliseconds(1000));
    969 
    970   float f = 0.5;
    971   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
    972             TimeDelta::FromMilliseconds(1000) * f);
    973   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
    974             TimeDelta::FromMilliseconds(1000) / f);
    975   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
    976             TimeDelta::FromMilliseconds(1000) *= f);
    977   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
    978             TimeDelta::FromMilliseconds(1000) /= f);
    979   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
    980             f * TimeDelta::FromMilliseconds(1000));
    981 
    982   int i = 2;
    983   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
    984             TimeDelta::FromMilliseconds(1000) * i);
    985   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
    986             TimeDelta::FromMilliseconds(1000) / i);
    987   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
    988             TimeDelta::FromMilliseconds(1000) *= i);
    989   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
    990             TimeDelta::FromMilliseconds(1000) /= i);
    991   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
    992             i * TimeDelta::FromMilliseconds(1000));
    993 
    994   int64_t i64 = 2;
    995   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
    996             TimeDelta::FromMilliseconds(1000) * i64);
    997   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
    998             TimeDelta::FromMilliseconds(1000) / i64);
    999   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
   1000             TimeDelta::FromMilliseconds(1000) *= i64);
   1001   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
   1002             TimeDelta::FromMilliseconds(1000) /= i64);
   1003   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
   1004             i64 * TimeDelta::FromMilliseconds(1000));
   1005 
   1006   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
   1007             TimeDelta::FromMilliseconds(1000) * 0.5);
   1008   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
   1009             TimeDelta::FromMilliseconds(1000) / 0.5);
   1010   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
   1011             TimeDelta::FromMilliseconds(1000) *= 0.5);
   1012   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
   1013             TimeDelta::FromMilliseconds(1000) /= 0.5);
   1014   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
   1015             0.5 * TimeDelta::FromMilliseconds(1000));
   1016 
   1017   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
   1018             TimeDelta::FromMilliseconds(1000) * 2);
   1019   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
   1020             TimeDelta::FromMilliseconds(1000) / 2);
   1021   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
   1022             TimeDelta::FromMilliseconds(1000) *= 2);
   1023   EXPECT_EQ(TimeDelta::FromMilliseconds(500),
   1024             TimeDelta::FromMilliseconds(1000) /= 2);
   1025   EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
   1026             2 * TimeDelta::FromMilliseconds(1000));
   1027 }
   1028 
   1029 TEST(TimeDelta, Overflows) {
   1030   // Some sanity checks.
   1031   EXPECT_TRUE(TimeDelta::Max().is_max());
   1032   EXPECT_TRUE(IsMin(-TimeDelta::Max()));
   1033   EXPECT_GT(TimeDelta(), -TimeDelta::Max());
   1034 
   1035   TimeDelta large_delta = TimeDelta::Max() - TimeDelta::FromMilliseconds(1);
   1036   TimeDelta large_negative = -large_delta;
   1037   EXPECT_GT(TimeDelta(), large_negative);
   1038   EXPECT_FALSE(large_delta.is_max());
   1039   EXPECT_FALSE(IsMin(-large_negative));
   1040   TimeDelta one_second = TimeDelta::FromSeconds(1);
   1041 
   1042   // Test +, -, * and / operators.
   1043   EXPECT_TRUE((large_delta + one_second).is_max());
   1044   EXPECT_TRUE(IsMin(large_negative + (-one_second)));
   1045   EXPECT_TRUE(IsMin(large_negative - one_second));
   1046   EXPECT_TRUE((large_delta - (-one_second)).is_max());
   1047   EXPECT_TRUE((large_delta * 2).is_max());
   1048   EXPECT_TRUE(IsMin(large_delta * -2));
   1049   EXPECT_TRUE((large_delta / 0.5).is_max());
   1050   EXPECT_TRUE(IsMin(large_delta / -0.5));
   1051 
   1052   // Test +=, -=, *= and /= operators.
   1053   TimeDelta delta = large_delta;
   1054   delta += one_second;
   1055   EXPECT_TRUE(delta.is_max());
   1056   delta = large_negative;
   1057   delta += -one_second;
   1058   EXPECT_TRUE(IsMin(delta));
   1059 
   1060   delta = large_negative;
   1061   delta -= one_second;
   1062   EXPECT_TRUE(IsMin(delta));
   1063   delta = large_delta;
   1064   delta -= -one_second;
   1065   EXPECT_TRUE(delta.is_max());
   1066 
   1067   delta = large_delta;
   1068   delta *= 2;
   1069   EXPECT_TRUE(delta.is_max());
   1070   delta = large_negative;
   1071   delta *= 1.5;
   1072   EXPECT_TRUE(IsMin(delta));
   1073 
   1074   delta = large_delta;
   1075   delta /= 0.5;
   1076   EXPECT_TRUE(delta.is_max());
   1077   delta = large_negative;
   1078   delta /= 0.5;
   1079   EXPECT_TRUE(IsMin(delta));
   1080 
   1081   // Test operations with Time and TimeTicks.
   1082   EXPECT_TRUE((large_delta + Time::Now()).is_max());
   1083   EXPECT_TRUE((large_delta + TimeTicks::Now()).is_max());
   1084   EXPECT_TRUE((Time::Now() + large_delta).is_max());
   1085   EXPECT_TRUE((TimeTicks::Now() + large_delta).is_max());
   1086 
   1087   Time time_now = Time::Now();
   1088   EXPECT_EQ(one_second, (time_now + one_second) - time_now);
   1089   EXPECT_EQ(-one_second, (time_now - one_second) - time_now);
   1090 
   1091   TimeTicks ticks_now = TimeTicks::Now();
   1092   EXPECT_EQ(-one_second, (ticks_now - one_second) - ticks_now);
   1093   EXPECT_EQ(one_second, (ticks_now + one_second) - ticks_now);
   1094 }
   1095 
   1096 TEST(TimeDeltaLogging, DCheckEqCompiles) {
   1097   DCHECK_EQ(TimeDelta(), TimeDelta());
   1098 }
   1099 
   1100 TEST(TimeDeltaLogging, EmptyIsZero) {
   1101   TimeDelta zero;
   1102   EXPECT_EQ("0s", AnyToString(zero));
   1103 }
   1104 
   1105 TEST(TimeDeltaLogging, FiveHundredMs) {
   1106   TimeDelta five_hundred_ms = TimeDelta::FromMilliseconds(500);
   1107   EXPECT_EQ("0.5s", AnyToString(five_hundred_ms));
   1108 }
   1109 
   1110 TEST(TimeDeltaLogging, MinusTenSeconds) {
   1111   TimeDelta minus_ten_seconds = TimeDelta::FromSeconds(-10);
   1112   EXPECT_EQ("-10s", AnyToString(minus_ten_seconds));
   1113 }
   1114 
   1115 TEST(TimeDeltaLogging, DoesNotMessUpFormattingFlags) {
   1116   std::ostringstream oss;
   1117   std::ios_base::fmtflags flags_before = oss.flags();
   1118   oss << TimeDelta();
   1119   EXPECT_EQ(flags_before, oss.flags());
   1120 }
   1121 
   1122 TEST(TimeDeltaLogging, DoesNotMakeStreamBad) {
   1123   std::ostringstream oss;
   1124   oss << TimeDelta();
   1125   EXPECT_TRUE(oss.good());
   1126 }
   1127 
   1128 TEST(TimeLogging, DCheckEqCompiles) {
   1129   DCHECK_EQ(Time(), Time());
   1130 }
   1131 
   1132 TEST(TimeLogging, ChromeBirthdate) {
   1133   Time birthdate;
   1134   ASSERT_TRUE(Time::FromString("Tue, 02 Sep 2008 09:42:18 GMT", &birthdate));
   1135   EXPECT_EQ("2008-09-02 09:42:18.000 UTC", AnyToString(birthdate));
   1136 }
   1137 
   1138 TEST(TimeLogging, DoesNotMessUpFormattingFlags) {
   1139   std::ostringstream oss;
   1140   std::ios_base::fmtflags flags_before = oss.flags();
   1141   oss << Time();
   1142   EXPECT_EQ(flags_before, oss.flags());
   1143 }
   1144 
   1145 TEST(TimeLogging, DoesNotMakeStreamBad) {
   1146   std::ostringstream oss;
   1147   oss << Time();
   1148   EXPECT_TRUE(oss.good());
   1149 }
   1150 
   1151 TEST(TimeTicksLogging, DCheckEqCompiles) {
   1152   DCHECK_EQ(TimeTicks(), TimeTicks());
   1153 }
   1154 
   1155 TEST(TimeTicksLogging, ZeroTime) {
   1156   TimeTicks zero;
   1157   EXPECT_EQ("0 bogo-microseconds", AnyToString(zero));
   1158 }
   1159 
   1160 TEST(TimeTicksLogging, FortyYearsLater) {
   1161   TimeTicks forty_years_later =
   1162       TimeTicks() + TimeDelta::FromDays(365.25 * 40);
   1163   EXPECT_EQ("1262304000000000 bogo-microseconds",
   1164             AnyToString(forty_years_later));
   1165 }
   1166 
   1167 TEST(TimeTicksLogging, DoesNotMessUpFormattingFlags) {
   1168   std::ostringstream oss;
   1169   std::ios_base::fmtflags flags_before = oss.flags();
   1170   oss << TimeTicks();
   1171   EXPECT_EQ(flags_before, oss.flags());
   1172 }
   1173 
   1174 TEST(TimeTicksLogging, DoesNotMakeStreamBad) {
   1175   std::ostringstream oss;
   1176   oss << TimeTicks();
   1177   EXPECT_TRUE(oss.good());
   1178 }
   1179 
   1180 }  // namespace
   1181 
   1182 }  // namespace base
   1183