blob: f8a62cbdaa3e7f101cd44b8e11cdde2533f1c0fe [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
initial.commit3f4a7322008-07-27 06:49:38 +09005#include <time.h>
6
pinkerton@google.com75d74022008-08-14 07:55:45 +09007#include "base/platform_thread.h"
deanm@google.com69dced82008-09-02 17:28:37 +09008#include "base/time.h"
9#include "build/build_config.h"
initial.commit3f4a7322008-07-27 06:49:38 +090010#include "testing/gtest/include/gtest/gtest.h"
11
dsh@google.com0f8dd262008-10-28 05:43:33 +090012using base::Time;
13using base::TimeDelta;
14using base::TimeTicks;
15
initial.commit3f4a7322008-07-27 06:49:38 +090016// Test conversions to/from time_t and exploding/unexploding.
17TEST(Time, TimeT) {
18 // C library time and exploded time.
19 time_t now_t_1 = time(NULL);
20 struct tm tms;
pinkerton@google.com75d74022008-08-14 07:55:45 +090021#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090022 localtime_s(&tms, &now_t_1);
pinkerton@google.com75d74022008-08-14 07:55:45 +090023#elif defined(OS_POSIX)
24 localtime_r(&now_t_1, &tms);
25#endif
initial.commit3f4a7322008-07-27 06:49:38 +090026
27 // Convert to ours.
28 Time our_time_1 = Time::FromTimeT(now_t_1);
29 Time::Exploded exploded;
30 our_time_1.LocalExplode(&exploded);
31
32 // This will test both our exploding and our time_t -> Time conversion.
33 EXPECT_EQ(tms.tm_year + 1900, exploded.year);
34 EXPECT_EQ(tms.tm_mon + 1, exploded.month);
35 EXPECT_EQ(tms.tm_mday, exploded.day_of_month);
36 EXPECT_EQ(tms.tm_hour, exploded.hour);
37 EXPECT_EQ(tms.tm_min, exploded.minute);
38 EXPECT_EQ(tms.tm_sec, exploded.second);
39
40 // Convert exploded back to the time struct.
41 Time our_time_2 = Time::FromLocalExploded(exploded);
42 EXPECT_TRUE(our_time_1 == our_time_2);
43
44 time_t now_t_2 = our_time_2.ToTimeT();
45 EXPECT_EQ(now_t_1, now_t_2);
46
deanm@google.com02781222008-08-19 18:16:49 +090047 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT());
48 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT());
49
initial.commit3f4a7322008-07-27 06:49:38 +090050 // Conversions of 0 should stay 0.
51 EXPECT_EQ(0, Time().ToTimeT());
52 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
53}
54
55TEST(Time, ZeroIsSymmetric) {
deanm@google.com02781222008-08-19 18:16:49 +090056 Time zero_time(Time::FromTimeT(0));
57 EXPECT_EQ(0, zero_time.ToTimeT());
58
59 EXPECT_EQ(0.0, zero_time.ToDoubleT());
initial.commit3f4a7322008-07-27 06:49:38 +090060}
61
62TEST(Time, LocalExplode) {
63 Time a = Time::Now();
64 Time::Exploded exploded;
65 a.LocalExplode(&exploded);
66
67 Time b = Time::FromLocalExploded(exploded);
68
brettw@chromium.orgd2f97822009-08-26 11:53:36 +090069 // The exploded structure doesn't have microseconds, and on Mac & Linux, the
70 // internal OS conversion uses seconds, which will cause truncation. So we
71 // can only make sure that the delta is within one second.
72 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
initial.commit3f4a7322008-07-27 06:49:38 +090073}
74
75TEST(Time, UTCExplode) {
76 Time a = Time::Now();
77 Time::Exploded exploded;
78 a.UTCExplode(&exploded);
79
80 Time b = Time::FromUTCExploded(exploded);
brettw@chromium.orgd2f97822009-08-26 11:53:36 +090081 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
initial.commit3f4a7322008-07-27 06:49:38 +090082}
83
deanm@google.com02781222008-08-19 18:16:49 +090084TEST(Time, LocalMidnight) {
85 Time::Exploded exploded;
86 Time::Now().LocalMidnight().LocalExplode(&exploded);
87 EXPECT_EQ(0, exploded.hour);
88 EXPECT_EQ(0, exploded.minute);
89 EXPECT_EQ(0, exploded.second);
90 EXPECT_EQ(0, exploded.millisecond);
91}
92
initial.commit3f4a7322008-07-27 06:49:38 +090093TEST(TimeTicks, Deltas) {
mbelshe@google.com556bc842008-09-26 12:00:00 +090094 for (int index = 0; index < 500; index++) {
95 TimeTicks ticks_start = TimeTicks::Now();
96 PlatformThread::Sleep(10);
97 TimeTicks ticks_stop = TimeTicks::Now();
98 TimeDelta delta = ticks_stop - ticks_start;
99 // Note: Although we asked for a 10ms sleep, if the
100 // time clock has a finer granularity than the Sleep()
101 // clock, it is quite possible to wakeup early. Here
102 // is how that works:
103 // Time(ms timer) Time(us timer)
104 // 5 5010
105 // 6 6010
106 // 7 7010
107 // 8 8010
108 // 9 9000
109 // Elapsed 4ms 3990us
110 //
111 // Unfortunately, our InMilliseconds() function truncates
112 // rather than rounds. We should consider fixing this
113 // so that our averages come out better.
114 EXPECT_GE(delta.InMilliseconds(), 9);
115 EXPECT_GE(delta.InMicroseconds(), 9000);
116 EXPECT_EQ(delta.InSeconds(), 0);
117 }
initial.commit3f4a7322008-07-27 06:49:38 +0900118}
119
mbelshe@google.com556bc842008-09-26 12:00:00 +0900120TEST(TimeTicks, HighResNow) {
121 TimeTicks ticks_start = TimeTicks::HighResNow();
deanm@google.comd6c2dee2008-09-04 03:18:32 +0900122 PlatformThread::Sleep(10);
mbelshe@google.com556bc842008-09-26 12:00:00 +0900123 TimeTicks ticks_stop = TimeTicks::HighResNow();
deanm@google.comd6c2dee2008-09-04 03:18:32 +0900124 TimeDelta delta = ticks_stop - ticks_start;
mbelshe@google.com556bc842008-09-26 12:00:00 +0900125 EXPECT_GE(delta.InMicroseconds(), 9000);
deanm@google.comd6c2dee2008-09-04 03:18:32 +0900126}
127
deanm@google.com02781222008-08-19 18:16:49 +0900128TEST(TimeDelta, FromAndIn) {
129 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48));
130 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180));
131 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120));
132 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000));
133 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) ==
134 TimeDelta::FromMicroseconds(2000));
135 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays());
136 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours());
137 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes());
138 EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds());
139 EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF());
140 EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds());
141 EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF());
142 EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds());
143}
brettw@chromium.orgd2f97822009-08-26 11:53:36 +0900144
145// Our internal time format is serialized in things like databases, so it's
146// important that it's consistent across all our platforms. We use the 1601
147// Windows epoch as the internal format across all platforms.
148TEST(TimeDelta, WindowsEpoch) {
149 Time::Exploded exploded;
150 exploded.year = 1970;
151 exploded.month = 1;
152 exploded.day_of_week = 0; // Should be unusued.
153 exploded.day_of_month = 1;
154 exploded.hour = 0;
155 exploded.minute = 0;
156 exploded.second = 0;
157 exploded.millisecond = 0;
158 Time t = Time::FromUTCExploded(exploded);
159 // Unix 1970 epoch.
160 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue());
161
162 // We can't test 1601 epoch, since the system time functions on Linux
163 // only compute years starting from 1900.
164}