1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/time/time.h"
16
17 #if defined(_MSC_VER)
18 #include <winsock2.h> // for timeval
19 #endif
20
21 #include <chrono> // NOLINT(build/c++11)
22 #include <cstring>
23 #include <ctime>
24 #include <iomanip>
25 #include <limits>
26 #include <string>
27
28 #include "gmock/gmock.h"
29 #include "gtest/gtest.h"
30 #include "absl/numeric/int128.h"
31 #include "absl/time/clock.h"
32 #include "absl/time/internal/test_util.h"
33
34 namespace {
35
36 #if defined(GTEST_USES_SIMPLE_RE) && GTEST_USES_SIMPLE_RE
37 const char kZoneAbbrRE[] = ".*"; // just punt
38 #else
39 const char kZoneAbbrRE[] = "[A-Za-z]{3,4}|[-+][0-9]{2}([0-9]{2})?";
40 #endif
41
42 // This helper is a macro so that failed expectations show up with the
43 // correct line numbers.
44 #define EXPECT_CIVIL_INFO(ci, y, m, d, h, min, s, off, isdst) \
45 do { \
46 EXPECT_EQ(y, ci.cs.year()); \
47 EXPECT_EQ(m, ci.cs.month()); \
48 EXPECT_EQ(d, ci.cs.day()); \
49 EXPECT_EQ(h, ci.cs.hour()); \
50 EXPECT_EQ(min, ci.cs.minute()); \
51 EXPECT_EQ(s, ci.cs.second()); \
52 EXPECT_EQ(off, ci.offset); \
53 EXPECT_EQ(isdst, ci.is_dst); \
54 EXPECT_THAT(ci.zone_abbr, testing::MatchesRegex(kZoneAbbrRE)); \
55 } while (0)
56
57 // A gMock matcher to match timespec values. Use this matcher like:
58 // timespec ts1, ts2;
59 // EXPECT_THAT(ts1, TimespecMatcher(ts2));
60 MATCHER_P(TimespecMatcher, ts, "") {
61 if (ts.tv_sec == arg.tv_sec && ts.tv_nsec == arg.tv_nsec) return true;
62 *result_listener << "expected: {" << ts.tv_sec << ", " << ts.tv_nsec << "} ";
63 *result_listener << "actual: {" << arg.tv_sec << ", " << arg.tv_nsec << "}";
64 return false;
65 }
66
67 // A gMock matcher to match timeval values. Use this matcher like:
68 // timeval tv1, tv2;
69 // EXPECT_THAT(tv1, TimevalMatcher(tv2));
70 MATCHER_P(TimevalMatcher, tv, "") {
71 if (tv.tv_sec == arg.tv_sec && tv.tv_usec == arg.tv_usec) return true;
72 *result_listener << "expected: {" << tv.tv_sec << ", " << tv.tv_usec << "} ";
73 *result_listener << "actual: {" << arg.tv_sec << ", " << arg.tv_usec << "}";
74 return false;
75 }
76
TEST(Time,ConstExpr)77 TEST(Time, ConstExpr) {
78 constexpr absl::Time t0 = absl::UnixEpoch();
79 static_assert(t0 == absl::Time(), "UnixEpoch");
80 constexpr absl::Time t1 = absl::InfiniteFuture();
81 static_assert(t1 != absl::Time(), "InfiniteFuture");
82 constexpr absl::Time t2 = absl::InfinitePast();
83 static_assert(t2 != absl::Time(), "InfinitePast");
84 constexpr absl::Time t3 = absl::FromUnixNanos(0);
85 static_assert(t3 == absl::Time(), "FromUnixNanos");
86 constexpr absl::Time t4 = absl::FromUnixMicros(0);
87 static_assert(t4 == absl::Time(), "FromUnixMicros");
88 constexpr absl::Time t5 = absl::FromUnixMillis(0);
89 static_assert(t5 == absl::Time(), "FromUnixMillis");
90 constexpr absl::Time t6 = absl::FromUnixSeconds(0);
91 static_assert(t6 == absl::Time(), "FromUnixSeconds");
92 constexpr absl::Time t7 = absl::FromTimeT(0);
93 static_assert(t7 == absl::Time(), "FromTimeT");
94 }
95
TEST(Time,ValueSemantics)96 TEST(Time, ValueSemantics) {
97 absl::Time a; // Default construction
98 absl::Time b = a; // Copy construction
99 EXPECT_EQ(a, b);
100 absl::Time c(a); // Copy construction (again)
101 EXPECT_EQ(a, b);
102 EXPECT_EQ(a, c);
103 EXPECT_EQ(b, c);
104 b = c; // Assignment
105 EXPECT_EQ(a, b);
106 EXPECT_EQ(a, c);
107 EXPECT_EQ(b, c);
108 }
109
TEST(Time,UnixEpoch)110 TEST(Time, UnixEpoch) {
111 const auto ci = absl::UTCTimeZone().At(absl::UnixEpoch());
112 EXPECT_EQ(absl::CivilSecond(1970, 1, 1, 0, 0, 0), ci.cs);
113 EXPECT_EQ(absl::ZeroDuration(), ci.subsecond);
114 EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(ci.cs));
115 }
116
TEST(Time,Breakdown)117 TEST(Time, Breakdown) {
118 absl::TimeZone tz = absl::time_internal::LoadTimeZone("America/New_York");
119 absl::Time t = absl::UnixEpoch();
120
121 // The Unix epoch as seen in NYC.
122 auto ci = tz.At(t);
123 EXPECT_CIVIL_INFO(ci, 1969, 12, 31, 19, 0, 0, -18000, false);
124 EXPECT_EQ(absl::ZeroDuration(), ci.subsecond);
125 EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(ci.cs));
126
127 // Just before the epoch.
128 t -= absl::Nanoseconds(1);
129 ci = tz.At(t);
130 EXPECT_CIVIL_INFO(ci, 1969, 12, 31, 18, 59, 59, -18000, false);
131 EXPECT_EQ(absl::Nanoseconds(999999999), ci.subsecond);
132 EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(ci.cs));
133
134 // Some time later.
135 t += absl::Hours(24) * 2735;
136 t += absl::Hours(18) + absl::Minutes(30) + absl::Seconds(15) +
137 absl::Nanoseconds(9);
138 ci = tz.At(t);
139 EXPECT_CIVIL_INFO(ci, 1977, 6, 28, 14, 30, 15, -14400, true);
140 EXPECT_EQ(8, ci.subsecond / absl::Nanoseconds(1));
141 EXPECT_EQ(absl::Weekday::tuesday, absl::GetWeekday(ci.cs));
142 }
143
TEST(Time,AdditiveOperators)144 TEST(Time, AdditiveOperators) {
145 const absl::Duration d = absl::Nanoseconds(1);
146 const absl::Time t0;
147 const absl::Time t1 = t0 + d;
148
149 EXPECT_EQ(d, t1 - t0);
150 EXPECT_EQ(-d, t0 - t1);
151 EXPECT_EQ(t0, t1 - d);
152
153 absl::Time t(t0);
154 EXPECT_EQ(t0, t);
155 t += d;
156 EXPECT_EQ(t0 + d, t);
157 EXPECT_EQ(d, t - t0);
158 t -= d;
159 EXPECT_EQ(t0, t);
160
161 // Tests overflow between subseconds and seconds.
162 t = absl::UnixEpoch();
163 t += absl::Milliseconds(500);
164 EXPECT_EQ(absl::UnixEpoch() + absl::Milliseconds(500), t);
165 t += absl::Milliseconds(600);
166 EXPECT_EQ(absl::UnixEpoch() + absl::Milliseconds(1100), t);
167 t -= absl::Milliseconds(600);
168 EXPECT_EQ(absl::UnixEpoch() + absl::Milliseconds(500), t);
169 t -= absl::Milliseconds(500);
170 EXPECT_EQ(absl::UnixEpoch(), t);
171 }
172
TEST(Time,RelationalOperators)173 TEST(Time, RelationalOperators) {
174 constexpr absl::Time t1 = absl::FromUnixNanos(0);
175 constexpr absl::Time t2 = absl::FromUnixNanos(1);
176 constexpr absl::Time t3 = absl::FromUnixNanos(2);
177
178 static_assert(absl::Time() == t1, "");
179 static_assert(t1 == t1, "");
180 static_assert(t2 == t2, "");
181 static_assert(t3 == t3, "");
182
183 static_assert(t1 < t2, "");
184 static_assert(t2 < t3, "");
185 static_assert(t1 < t3, "");
186
187 static_assert(t1 <= t1, "");
188 static_assert(t1 <= t2, "");
189 static_assert(t2 <= t2, "");
190 static_assert(t2 <= t3, "");
191 static_assert(t3 <= t3, "");
192 static_assert(t1 <= t3, "");
193
194 static_assert(t2 > t1, "");
195 static_assert(t3 > t2, "");
196 static_assert(t3 > t1, "");
197
198 static_assert(t2 >= t2, "");
199 static_assert(t2 >= t1, "");
200 static_assert(t3 >= t3, "");
201 static_assert(t3 >= t2, "");
202 static_assert(t1 >= t1, "");
203 static_assert(t3 >= t1, "");
204 }
205
TEST(Time,Infinity)206 TEST(Time, Infinity) {
207 constexpr absl::Time ifuture = absl::InfiniteFuture();
208 constexpr absl::Time ipast = absl::InfinitePast();
209
210 static_assert(ifuture == ifuture, "");
211 static_assert(ipast == ipast, "");
212 static_assert(ipast < ifuture, "");
213 static_assert(ifuture > ipast, "");
214
215 // Arithmetic saturates
216 EXPECT_EQ(ifuture, ifuture + absl::Seconds(1));
217 EXPECT_EQ(ifuture, ifuture - absl::Seconds(1));
218 EXPECT_EQ(ipast, ipast + absl::Seconds(1));
219 EXPECT_EQ(ipast, ipast - absl::Seconds(1));
220
221 EXPECT_EQ(absl::InfiniteDuration(), ifuture - ifuture);
222 EXPECT_EQ(absl::InfiniteDuration(), ifuture - ipast);
223 EXPECT_EQ(-absl::InfiniteDuration(), ipast - ifuture);
224 EXPECT_EQ(-absl::InfiniteDuration(), ipast - ipast);
225
226 constexpr absl::Time t = absl::UnixEpoch(); // Any finite time.
227 static_assert(t < ifuture, "");
228 static_assert(t > ipast, "");
229
230 EXPECT_EQ(ifuture, t + absl::InfiniteDuration());
231 EXPECT_EQ(ipast, t - absl::InfiniteDuration());
232 }
233
TEST(Time,FloorConversion)234 TEST(Time, FloorConversion) {
235 #define TEST_FLOOR_CONVERSION(TO, FROM) \
236 EXPECT_EQ(1, TO(FROM(1001))); \
237 EXPECT_EQ(1, TO(FROM(1000))); \
238 EXPECT_EQ(0, TO(FROM(999))); \
239 EXPECT_EQ(0, TO(FROM(1))); \
240 EXPECT_EQ(0, TO(FROM(0))); \
241 EXPECT_EQ(-1, TO(FROM(-1))); \
242 EXPECT_EQ(-1, TO(FROM(-999))); \
243 EXPECT_EQ(-1, TO(FROM(-1000))); \
244 EXPECT_EQ(-2, TO(FROM(-1001)));
245
246 TEST_FLOOR_CONVERSION(absl::ToUnixMicros, absl::FromUnixNanos);
247 TEST_FLOOR_CONVERSION(absl::ToUnixMillis, absl::FromUnixMicros);
248 TEST_FLOOR_CONVERSION(absl::ToUnixSeconds, absl::FromUnixMillis);
249 TEST_FLOOR_CONVERSION(absl::ToTimeT, absl::FromUnixMillis);
250
251 #undef TEST_FLOOR_CONVERSION
252
253 // Tests ToUnixNanos.
254 EXPECT_EQ(1, absl::ToUnixNanos(absl::UnixEpoch() + absl::Nanoseconds(3) / 2));
255 EXPECT_EQ(1, absl::ToUnixNanos(absl::UnixEpoch() + absl::Nanoseconds(1)));
256 EXPECT_EQ(0, absl::ToUnixNanos(absl::UnixEpoch() + absl::Nanoseconds(1) / 2));
257 EXPECT_EQ(0, absl::ToUnixNanos(absl::UnixEpoch() + absl::Nanoseconds(0)));
258 EXPECT_EQ(-1,
259 absl::ToUnixNanos(absl::UnixEpoch() - absl::Nanoseconds(1) / 2));
260 EXPECT_EQ(-1, absl::ToUnixNanos(absl::UnixEpoch() - absl::Nanoseconds(1)));
261 EXPECT_EQ(-2,
262 absl::ToUnixNanos(absl::UnixEpoch() - absl::Nanoseconds(3) / 2));
263
264 // Tests ToUniversal, which uses a different epoch than the tests above.
265 EXPECT_EQ(1,
266 absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(101)));
267 EXPECT_EQ(1,
268 absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(100)));
269 EXPECT_EQ(0,
270 absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(99)));
271 EXPECT_EQ(0,
272 absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(1)));
273 EXPECT_EQ(0,
274 absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(0)));
275 EXPECT_EQ(-1,
276 absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(-1)));
277 EXPECT_EQ(-1,
278 absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(-99)));
279 EXPECT_EQ(
280 -1, absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(-100)));
281 EXPECT_EQ(
282 -2, absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(-101)));
283
284 // Tests ToTimespec()/TimeFromTimespec()
285 const struct {
286 absl::Time t;
287 timespec ts;
288 } to_ts[] = {
289 {absl::FromUnixSeconds(1) + absl::Nanoseconds(1), {1, 1}},
290 {absl::FromUnixSeconds(1) + absl::Nanoseconds(1) / 2, {1, 0}},
291 {absl::FromUnixSeconds(1) + absl::Nanoseconds(0), {1, 0}},
292 {absl::FromUnixSeconds(0) + absl::Nanoseconds(0), {0, 0}},
293 {absl::FromUnixSeconds(0) - absl::Nanoseconds(1) / 2, {-1, 999999999}},
294 {absl::FromUnixSeconds(0) - absl::Nanoseconds(1), {-1, 999999999}},
295 {absl::FromUnixSeconds(-1) + absl::Nanoseconds(1), {-1, 1}},
296 {absl::FromUnixSeconds(-1) + absl::Nanoseconds(1) / 2, {-1, 0}},
297 {absl::FromUnixSeconds(-1) + absl::Nanoseconds(0), {-1, 0}},
298 {absl::FromUnixSeconds(-1) - absl::Nanoseconds(1) / 2, {-2, 999999999}},
299 };
300 for (const auto& test : to_ts) {
301 EXPECT_THAT(absl::ToTimespec(test.t), TimespecMatcher(test.ts));
302 }
303 const struct {
304 timespec ts;
305 absl::Time t;
306 } from_ts[] = {
307 {{1, 1}, absl::FromUnixSeconds(1) + absl::Nanoseconds(1)},
308 {{1, 0}, absl::FromUnixSeconds(1) + absl::Nanoseconds(0)},
309 {{0, 0}, absl::FromUnixSeconds(0) + absl::Nanoseconds(0)},
310 {{0, -1}, absl::FromUnixSeconds(0) - absl::Nanoseconds(1)},
311 {{-1, 999999999}, absl::FromUnixSeconds(0) - absl::Nanoseconds(1)},
312 {{-1, 1}, absl::FromUnixSeconds(-1) + absl::Nanoseconds(1)},
313 {{-1, 0}, absl::FromUnixSeconds(-1) + absl::Nanoseconds(0)},
314 {{-1, -1}, absl::FromUnixSeconds(-1) - absl::Nanoseconds(1)},
315 {{-2, 999999999}, absl::FromUnixSeconds(-1) - absl::Nanoseconds(1)},
316 };
317 for (const auto& test : from_ts) {
318 EXPECT_EQ(test.t, absl::TimeFromTimespec(test.ts));
319 }
320
321 // Tests ToTimeval()/TimeFromTimeval() (same as timespec above)
322 const struct {
323 absl::Time t;
324 timeval tv;
325 } to_tv[] = {
326 {absl::FromUnixSeconds(1) + absl::Microseconds(1), {1, 1}},
327 {absl::FromUnixSeconds(1) + absl::Microseconds(1) / 2, {1, 0}},
328 {absl::FromUnixSeconds(1) + absl::Microseconds(0), {1, 0}},
329 {absl::FromUnixSeconds(0) + absl::Microseconds(0), {0, 0}},
330 {absl::FromUnixSeconds(0) - absl::Microseconds(1) / 2, {-1, 999999}},
331 {absl::FromUnixSeconds(0) - absl::Microseconds(1), {-1, 999999}},
332 {absl::FromUnixSeconds(-1) + absl::Microseconds(1), {-1, 1}},
333 {absl::FromUnixSeconds(-1) + absl::Microseconds(1) / 2, {-1, 0}},
334 {absl::FromUnixSeconds(-1) + absl::Microseconds(0), {-1, 0}},
335 {absl::FromUnixSeconds(-1) - absl::Microseconds(1) / 2, {-2, 999999}},
336 };
337 for (const auto& test : to_tv) {
338 EXPECT_THAT(ToTimeval(test.t), TimevalMatcher(test.tv));
339 }
340 const struct {
341 timeval tv;
342 absl::Time t;
343 } from_tv[] = {
344 {{1, 1}, absl::FromUnixSeconds(1) + absl::Microseconds(1)},
345 {{1, 0}, absl::FromUnixSeconds(1) + absl::Microseconds(0)},
346 {{0, 0}, absl::FromUnixSeconds(0) + absl::Microseconds(0)},
347 {{0, -1}, absl::FromUnixSeconds(0) - absl::Microseconds(1)},
348 {{-1, 999999}, absl::FromUnixSeconds(0) - absl::Microseconds(1)},
349 {{-1, 1}, absl::FromUnixSeconds(-1) + absl::Microseconds(1)},
350 {{-1, 0}, absl::FromUnixSeconds(-1) + absl::Microseconds(0)},
351 {{-1, -1}, absl::FromUnixSeconds(-1) - absl::Microseconds(1)},
352 {{-2, 999999}, absl::FromUnixSeconds(-1) - absl::Microseconds(1)},
353 };
354 for (const auto& test : from_tv) {
355 EXPECT_EQ(test.t, absl::TimeFromTimeval(test.tv));
356 }
357
358 // Tests flooring near negative infinity.
359 const int64_t min_plus_1 = std::numeric_limits<int64_t>::min() + 1;
360 EXPECT_EQ(min_plus_1, absl::ToUnixSeconds(absl::FromUnixSeconds(min_plus_1)));
361 EXPECT_EQ(std::numeric_limits<int64_t>::min(),
362 absl::ToUnixSeconds(absl::FromUnixSeconds(min_plus_1) -
363 absl::Nanoseconds(1) / 2));
364
365 // Tests flooring near positive infinity.
366 EXPECT_EQ(std::numeric_limits<int64_t>::max(),
367 absl::ToUnixSeconds(
368 absl::FromUnixSeconds(std::numeric_limits<int64_t>::max()) +
369 absl::Nanoseconds(1) / 2));
370 EXPECT_EQ(std::numeric_limits<int64_t>::max(),
371 absl::ToUnixSeconds(
372 absl::FromUnixSeconds(std::numeric_limits<int64_t>::max())));
373 EXPECT_EQ(std::numeric_limits<int64_t>::max() - 1,
374 absl::ToUnixSeconds(
375 absl::FromUnixSeconds(std::numeric_limits<int64_t>::max()) -
376 absl::Nanoseconds(1) / 2));
377 }
378
TEST(Time,RoundtripConversion)379 TEST(Time, RoundtripConversion) {
380 #define TEST_CONVERSION_ROUND_TRIP(SOURCE, FROM, TO, MATCHER) \
381 EXPECT_THAT(TO(FROM(SOURCE)), MATCHER(SOURCE))
382
383 // FromUnixNanos() and ToUnixNanos()
384 int64_t now_ns = absl::GetCurrentTimeNanos();
385 TEST_CONVERSION_ROUND_TRIP(-1, absl::FromUnixNanos, absl::ToUnixNanos,
386 testing::Eq);
387 TEST_CONVERSION_ROUND_TRIP(0, absl::FromUnixNanos, absl::ToUnixNanos,
388 testing::Eq);
389 TEST_CONVERSION_ROUND_TRIP(1, absl::FromUnixNanos, absl::ToUnixNanos,
390 testing::Eq);
391 TEST_CONVERSION_ROUND_TRIP(now_ns, absl::FromUnixNanos, absl::ToUnixNanos,
392 testing::Eq)
393 << now_ns;
394
395 // FromUnixMicros() and ToUnixMicros()
396 int64_t now_us = absl::GetCurrentTimeNanos() / 1000;
397 TEST_CONVERSION_ROUND_TRIP(-1, absl::FromUnixMicros, absl::ToUnixMicros,
398 testing::Eq);
399 TEST_CONVERSION_ROUND_TRIP(0, absl::FromUnixMicros, absl::ToUnixMicros,
400 testing::Eq);
401 TEST_CONVERSION_ROUND_TRIP(1, absl::FromUnixMicros, absl::ToUnixMicros,
402 testing::Eq);
403 TEST_CONVERSION_ROUND_TRIP(now_us, absl::FromUnixMicros, absl::ToUnixMicros,
404 testing::Eq)
405 << now_us;
406
407 // FromUnixMillis() and ToUnixMillis()
408 int64_t now_ms = absl::GetCurrentTimeNanos() / 1000000;
409 TEST_CONVERSION_ROUND_TRIP(-1, absl::FromUnixMillis, absl::ToUnixMillis,
410 testing::Eq);
411 TEST_CONVERSION_ROUND_TRIP(0, absl::FromUnixMillis, absl::ToUnixMillis,
412 testing::Eq);
413 TEST_CONVERSION_ROUND_TRIP(1, absl::FromUnixMillis, absl::ToUnixMillis,
414 testing::Eq);
415 TEST_CONVERSION_ROUND_TRIP(now_ms, absl::FromUnixMillis, absl::ToUnixMillis,
416 testing::Eq)
417 << now_ms;
418
419 // FromUnixSeconds() and ToUnixSeconds()
420 int64_t now_s = std::time(nullptr);
421 TEST_CONVERSION_ROUND_TRIP(-1, absl::FromUnixSeconds, absl::ToUnixSeconds,
422 testing::Eq);
423 TEST_CONVERSION_ROUND_TRIP(0, absl::FromUnixSeconds, absl::ToUnixSeconds,
424 testing::Eq);
425 TEST_CONVERSION_ROUND_TRIP(1, absl::FromUnixSeconds, absl::ToUnixSeconds,
426 testing::Eq);
427 TEST_CONVERSION_ROUND_TRIP(now_s, absl::FromUnixSeconds, absl::ToUnixSeconds,
428 testing::Eq)
429 << now_s;
430
431 // FromTimeT() and ToTimeT()
432 time_t now_time_t = std::time(nullptr);
433 TEST_CONVERSION_ROUND_TRIP(-1, absl::FromTimeT, absl::ToTimeT, testing::Eq);
434 TEST_CONVERSION_ROUND_TRIP(0, absl::FromTimeT, absl::ToTimeT, testing::Eq);
435 TEST_CONVERSION_ROUND_TRIP(1, absl::FromTimeT, absl::ToTimeT, testing::Eq);
436 TEST_CONVERSION_ROUND_TRIP(now_time_t, absl::FromTimeT, absl::ToTimeT,
437 testing::Eq)
438 << now_time_t;
439
440 // TimeFromTimeval() and ToTimeval()
441 timeval tv;
442 tv.tv_sec = -1;
443 tv.tv_usec = 0;
444 TEST_CONVERSION_ROUND_TRIP(tv, absl::TimeFromTimeval, absl::ToTimeval,
445 TimevalMatcher);
446 tv.tv_sec = -1;
447 tv.tv_usec = 999999;
448 TEST_CONVERSION_ROUND_TRIP(tv, absl::TimeFromTimeval, absl::ToTimeval,
449 TimevalMatcher);
450 tv.tv_sec = 0;
451 tv.tv_usec = 0;
452 TEST_CONVERSION_ROUND_TRIP(tv, absl::TimeFromTimeval, absl::ToTimeval,
453 TimevalMatcher);
454 tv.tv_sec = 0;
455 tv.tv_usec = 1;
456 TEST_CONVERSION_ROUND_TRIP(tv, absl::TimeFromTimeval, absl::ToTimeval,
457 TimevalMatcher);
458 tv.tv_sec = 1;
459 tv.tv_usec = 0;
460 TEST_CONVERSION_ROUND_TRIP(tv, absl::TimeFromTimeval, absl::ToTimeval,
461 TimevalMatcher);
462
463 // TimeFromTimespec() and ToTimespec()
464 timespec ts;
465 ts.tv_sec = -1;
466 ts.tv_nsec = 0;
467 TEST_CONVERSION_ROUND_TRIP(ts, absl::TimeFromTimespec, absl::ToTimespec,
468 TimespecMatcher);
469 ts.tv_sec = -1;
470 ts.tv_nsec = 999999999;
471 TEST_CONVERSION_ROUND_TRIP(ts, absl::TimeFromTimespec, absl::ToTimespec,
472 TimespecMatcher);
473 ts.tv_sec = 0;
474 ts.tv_nsec = 0;
475 TEST_CONVERSION_ROUND_TRIP(ts, absl::TimeFromTimespec, absl::ToTimespec,
476 TimespecMatcher);
477 ts.tv_sec = 0;
478 ts.tv_nsec = 1;
479 TEST_CONVERSION_ROUND_TRIP(ts, absl::TimeFromTimespec, absl::ToTimespec,
480 TimespecMatcher);
481 ts.tv_sec = 1;
482 ts.tv_nsec = 0;
483 TEST_CONVERSION_ROUND_TRIP(ts, absl::TimeFromTimespec, absl::ToTimespec,
484 TimespecMatcher);
485
486 // FromUDate() and ToUDate()
487 double now_ud = absl::GetCurrentTimeNanos() / 1000000;
488 TEST_CONVERSION_ROUND_TRIP(-1.5, absl::FromUDate, absl::ToUDate,
489 testing::DoubleEq);
490 TEST_CONVERSION_ROUND_TRIP(-1, absl::FromUDate, absl::ToUDate,
491 testing::DoubleEq);
492 TEST_CONVERSION_ROUND_TRIP(-0.5, absl::FromUDate, absl::ToUDate,
493 testing::DoubleEq);
494 TEST_CONVERSION_ROUND_TRIP(0, absl::FromUDate, absl::ToUDate,
495 testing::DoubleEq);
496 TEST_CONVERSION_ROUND_TRIP(0.5, absl::FromUDate, absl::ToUDate,
497 testing::DoubleEq);
498 TEST_CONVERSION_ROUND_TRIP(1, absl::FromUDate, absl::ToUDate,
499 testing::DoubleEq);
500 TEST_CONVERSION_ROUND_TRIP(1.5, absl::FromUDate, absl::ToUDate,
501 testing::DoubleEq);
502 TEST_CONVERSION_ROUND_TRIP(now_ud, absl::FromUDate, absl::ToUDate,
503 testing::DoubleEq)
504 << std::fixed << std::setprecision(17) << now_ud;
505
506 // FromUniversal() and ToUniversal()
507 int64_t now_uni = ((719162LL * (24 * 60 * 60)) * (1000 * 1000 * 10)) +
508 (absl::GetCurrentTimeNanos() / 100);
509 TEST_CONVERSION_ROUND_TRIP(-1, absl::FromUniversal, absl::ToUniversal,
510 testing::Eq);
511 TEST_CONVERSION_ROUND_TRIP(0, absl::FromUniversal, absl::ToUniversal,
512 testing::Eq);
513 TEST_CONVERSION_ROUND_TRIP(1, absl::FromUniversal, absl::ToUniversal,
514 testing::Eq);
515 TEST_CONVERSION_ROUND_TRIP(now_uni, absl::FromUniversal, absl::ToUniversal,
516 testing::Eq)
517 << now_uni;
518
519 #undef TEST_CONVERSION_ROUND_TRIP
520 }
521
522 template <typename Duration>
MakeChronoUnixTime(const Duration & d)523 std::chrono::system_clock::time_point MakeChronoUnixTime(const Duration& d) {
524 return std::chrono::system_clock::from_time_t(0) + d;
525 }
526
TEST(Time,FromChrono)527 TEST(Time, FromChrono) {
528 EXPECT_EQ(absl::FromTimeT(-1),
529 absl::FromChrono(std::chrono::system_clock::from_time_t(-1)));
530 EXPECT_EQ(absl::FromTimeT(0),
531 absl::FromChrono(std::chrono::system_clock::from_time_t(0)));
532 EXPECT_EQ(absl::FromTimeT(1),
533 absl::FromChrono(std::chrono::system_clock::from_time_t(1)));
534
535 EXPECT_EQ(
536 absl::FromUnixMillis(-1),
537 absl::FromChrono(MakeChronoUnixTime(std::chrono::milliseconds(-1))));
538 EXPECT_EQ(absl::FromUnixMillis(0),
539 absl::FromChrono(MakeChronoUnixTime(std::chrono::milliseconds(0))));
540 EXPECT_EQ(absl::FromUnixMillis(1),
541 absl::FromChrono(MakeChronoUnixTime(std::chrono::milliseconds(1))));
542
543 // Chrono doesn't define exactly its range and precision (neither does
544 // absl::Time), so let's simply test +/- ~100 years to make sure things work.
545 const auto century_sec = 60 * 60 * 24 * 365 * int64_t{100};
546 const auto century = std::chrono::seconds(century_sec);
547 const auto chrono_future = MakeChronoUnixTime(century);
548 const auto chrono_past = MakeChronoUnixTime(-century);
549 EXPECT_EQ(absl::FromUnixSeconds(century_sec),
550 absl::FromChrono(chrono_future));
551 EXPECT_EQ(absl::FromUnixSeconds(-century_sec), absl::FromChrono(chrono_past));
552
553 // Roundtrip them both back to chrono.
554 EXPECT_EQ(chrono_future,
555 absl::ToChronoTime(absl::FromUnixSeconds(century_sec)));
556 EXPECT_EQ(chrono_past,
557 absl::ToChronoTime(absl::FromUnixSeconds(-century_sec)));
558 }
559
TEST(Time,ToChronoTime)560 TEST(Time, ToChronoTime) {
561 EXPECT_EQ(std::chrono::system_clock::from_time_t(-1),
562 absl::ToChronoTime(absl::FromTimeT(-1)));
563 EXPECT_EQ(std::chrono::system_clock::from_time_t(0),
564 absl::ToChronoTime(absl::FromTimeT(0)));
565 EXPECT_EQ(std::chrono::system_clock::from_time_t(1),
566 absl::ToChronoTime(absl::FromTimeT(1)));
567
568 EXPECT_EQ(MakeChronoUnixTime(std::chrono::milliseconds(-1)),
569 absl::ToChronoTime(absl::FromUnixMillis(-1)));
570 EXPECT_EQ(MakeChronoUnixTime(std::chrono::milliseconds(0)),
571 absl::ToChronoTime(absl::FromUnixMillis(0)));
572 EXPECT_EQ(MakeChronoUnixTime(std::chrono::milliseconds(1)),
573 absl::ToChronoTime(absl::FromUnixMillis(1)));
574
575 // Time before the Unix epoch should floor, not trunc.
576 const auto tick = absl::Nanoseconds(1) / 4;
577 EXPECT_EQ(std::chrono::system_clock::from_time_t(0) -
578 std::chrono::system_clock::duration(1),
579 absl::ToChronoTime(absl::UnixEpoch() - tick));
580 }
581
582 // Check that absl::int128 works as a std::chrono::duration representation.
TEST(Time,Chrono128)583 TEST(Time, Chrono128) {
584 // Define a std::chrono::time_point type whose time[sic]_since_epoch() is
585 // a signed 128-bit count of attoseconds. This has a range and resolution
586 // (currently) beyond those of absl::Time, and undoubtedly also beyond those
587 // of std::chrono::system_clock::time_point.
588 //
589 // Note: The to/from-chrono support should probably be updated to handle
590 // such wide representations.
591 using Timestamp =
592 std::chrono::time_point<std::chrono::system_clock,
593 std::chrono::duration<absl::int128, std::atto>>;
594
595 // Expect that we can round-trip the std::chrono::system_clock::time_point
596 // extremes through both absl::Time and Timestamp, and that Timestamp can
597 // handle the (current) absl::Time extremes.
598 //
599 // Note: We should use std::chrono::floor() instead of time_point_cast(),
600 // but floor() is only available since c++17.
601 for (const auto tp : {std::chrono::system_clock::time_point::min(),
602 std::chrono::system_clock::time_point::max()}) {
603 EXPECT_EQ(tp, absl::ToChronoTime(absl::FromChrono(tp)));
604 EXPECT_EQ(tp, std::chrono::time_point_cast<
605 std::chrono::system_clock::time_point::duration>(
606 std::chrono::time_point_cast<Timestamp::duration>(tp)));
607 }
608 Timestamp::duration::rep v = std::numeric_limits<int64_t>::min();
609 v *= Timestamp::duration::period::den;
610 auto ts = Timestamp(Timestamp::duration(v));
611 ts += std::chrono::duration<int64_t, std::atto>(0);
612 EXPECT_EQ(std::numeric_limits<int64_t>::min(),
613 ts.time_since_epoch().count() / Timestamp::duration::period::den);
614 EXPECT_EQ(0,
615 ts.time_since_epoch().count() % Timestamp::duration::period::den);
616 v = std::numeric_limits<int64_t>::max();
617 v *= Timestamp::duration::period::den;
618 ts = Timestamp(Timestamp::duration(v));
619 ts += std::chrono::duration<int64_t, std::atto>(999999999750000000);
620 EXPECT_EQ(std::numeric_limits<int64_t>::max(),
621 ts.time_since_epoch().count() / Timestamp::duration::period::den);
622 EXPECT_EQ(999999999750000000,
623 ts.time_since_epoch().count() % Timestamp::duration::period::den);
624 }
625
TEST(Time,TimeZoneAt)626 TEST(Time, TimeZoneAt) {
627 const absl::TimeZone nyc =
628 absl::time_internal::LoadTimeZone("America/New_York");
629 const std::string fmt = "%a, %e %b %Y %H:%M:%S %z (%Z)";
630
631 // A non-transition where the civil time is unique.
632 absl::CivilSecond nov01(2013, 11, 1, 8, 30, 0);
633 const auto nov01_ci = nyc.At(nov01);
634 EXPECT_EQ(absl::TimeZone::TimeInfo::UNIQUE, nov01_ci.kind);
635 EXPECT_EQ("Fri, 1 Nov 2013 08:30:00 -0400 (EDT)",
636 absl::FormatTime(fmt, nov01_ci.pre, nyc));
637 EXPECT_EQ(nov01_ci.pre, nov01_ci.trans);
638 EXPECT_EQ(nov01_ci.pre, nov01_ci.post);
639 EXPECT_EQ(nov01_ci.pre, absl::FromCivil(nov01, nyc));
640
641 // A Spring DST transition, when there is a gap in civil time
642 // and we prefer the later of the possible interpretations of a
643 // non-existent time.
644 absl::CivilSecond mar13(2011, 3, 13, 2, 15, 0);
645 const auto mar_ci = nyc.At(mar13);
646 EXPECT_EQ(absl::TimeZone::TimeInfo::SKIPPED, mar_ci.kind);
647 EXPECT_EQ("Sun, 13 Mar 2011 03:15:00 -0400 (EDT)",
648 absl::FormatTime(fmt, mar_ci.pre, nyc));
649 EXPECT_EQ("Sun, 13 Mar 2011 03:00:00 -0400 (EDT)",
650 absl::FormatTime(fmt, mar_ci.trans, nyc));
651 EXPECT_EQ("Sun, 13 Mar 2011 01:15:00 -0500 (EST)",
652 absl::FormatTime(fmt, mar_ci.post, nyc));
653 EXPECT_EQ(mar_ci.trans, absl::FromCivil(mar13, nyc));
654
655 // A Fall DST transition, when civil times are repeated and
656 // we prefer the earlier of the possible interpretations of an
657 // ambiguous time.
658 absl::CivilSecond nov06(2011, 11, 6, 1, 15, 0);
659 const auto nov06_ci = nyc.At(nov06);
660 EXPECT_EQ(absl::TimeZone::TimeInfo::REPEATED, nov06_ci.kind);
661 EXPECT_EQ("Sun, 6 Nov 2011 01:15:00 -0400 (EDT)",
662 absl::FormatTime(fmt, nov06_ci.pre, nyc));
663 EXPECT_EQ("Sun, 6 Nov 2011 01:00:00 -0500 (EST)",
664 absl::FormatTime(fmt, nov06_ci.trans, nyc));
665 EXPECT_EQ("Sun, 6 Nov 2011 01:15:00 -0500 (EST)",
666 absl::FormatTime(fmt, nov06_ci.post, nyc));
667 EXPECT_EQ(nov06_ci.pre, absl::FromCivil(nov06, nyc));
668
669 // Check that (time_t) -1 is handled correctly.
670 absl::CivilSecond minus1(1969, 12, 31, 18, 59, 59);
671 const auto minus1_cl = nyc.At(minus1);
672 EXPECT_EQ(absl::TimeZone::TimeInfo::UNIQUE, minus1_cl.kind);
673 EXPECT_EQ(-1, absl::ToTimeT(minus1_cl.pre));
674 EXPECT_EQ("Wed, 31 Dec 1969 18:59:59 -0500 (EST)",
675 absl::FormatTime(fmt, minus1_cl.pre, nyc));
676 EXPECT_EQ("Wed, 31 Dec 1969 23:59:59 +0000 (UTC)",
677 absl::FormatTime(fmt, minus1_cl.pre, absl::UTCTimeZone()));
678 }
679
680 // FromCivil(CivilSecond(year, mon, day, hour, min, sec), UTCTimeZone())
681 // has a specialized fastpath implementation, which we exercise here.
TEST(Time,FromCivilUTC)682 TEST(Time, FromCivilUTC) {
683 const absl::TimeZone utc = absl::UTCTimeZone();
684 const std::string fmt = "%a, %e %b %Y %H:%M:%S %z (%Z)";
685 const int kMax = std::numeric_limits<int>::max();
686 const int kMin = std::numeric_limits<int>::min();
687 absl::Time t;
688
689 // 292091940881 is the last positive year to use the fastpath.
690 t = absl::FromCivil(
691 absl::CivilSecond(292091940881, kMax, kMax, kMax, kMax, kMax), utc);
692 EXPECT_EQ("Fri, 25 Nov 292277026596 12:21:07 +0000 (UTC)",
693 absl::FormatTime(fmt, t, utc));
694 t = absl::FromCivil(
695 absl::CivilSecond(292091940882, kMax, kMax, kMax, kMax, kMax), utc);
696 EXPECT_EQ("infinite-future", absl::FormatTime(fmt, t, utc)); // no overflow
697
698 // -292091936940 is the last negative year to use the fastpath.
699 t = absl::FromCivil(
700 absl::CivilSecond(-292091936940, kMin, kMin, kMin, kMin, kMin), utc);
701 EXPECT_EQ("Fri, 1 Nov -292277022657 10:37:52 +0000 (UTC)",
702 absl::FormatTime(fmt, t, utc));
703 t = absl::FromCivil(
704 absl::CivilSecond(-292091936941, kMin, kMin, kMin, kMin, kMin), utc);
705 EXPECT_EQ("infinite-past", absl::FormatTime(fmt, t, utc)); // no underflow
706
707 // Check that we're counting leap years correctly.
708 t = absl::FromCivil(absl::CivilSecond(1900, 2, 28, 23, 59, 59), utc);
709 EXPECT_EQ("Wed, 28 Feb 1900 23:59:59 +0000 (UTC)",
710 absl::FormatTime(fmt, t, utc));
711 t = absl::FromCivil(absl::CivilSecond(1900, 3, 1, 0, 0, 0), utc);
712 EXPECT_EQ("Thu, 1 Mar 1900 00:00:00 +0000 (UTC)",
713 absl::FormatTime(fmt, t, utc));
714 t = absl::FromCivil(absl::CivilSecond(2000, 2, 29, 23, 59, 59), utc);
715 EXPECT_EQ("Tue, 29 Feb 2000 23:59:59 +0000 (UTC)",
716 absl::FormatTime(fmt, t, utc));
717 t = absl::FromCivil(absl::CivilSecond(2000, 3, 1, 0, 0, 0), utc);
718 EXPECT_EQ("Wed, 1 Mar 2000 00:00:00 +0000 (UTC)",
719 absl::FormatTime(fmt, t, utc));
720 }
721
TEST(Time,ToTM)722 TEST(Time, ToTM) {
723 const absl::TimeZone utc = absl::UTCTimeZone();
724
725 // Compares the results of ToTM() to gmtime_r() for lots of times over the
726 // course of a few days.
727 const absl::Time start =
728 absl::FromCivil(absl::CivilSecond(2014, 1, 2, 3, 4, 5), utc);
729 const absl::Time end =
730 absl::FromCivil(absl::CivilSecond(2014, 1, 5, 3, 4, 5), utc);
731 for (absl::Time t = start; t < end; t += absl::Seconds(30)) {
732 const struct tm tm_bt = ToTM(t, utc);
733 const time_t tt = absl::ToTimeT(t);
734 struct tm tm_lc;
735 #ifdef _WIN32
736 gmtime_s(&tm_lc, &tt);
737 #else
738 gmtime_r(&tt, &tm_lc);
739 #endif
740 EXPECT_EQ(tm_lc.tm_year, tm_bt.tm_year);
741 EXPECT_EQ(tm_lc.tm_mon, tm_bt.tm_mon);
742 EXPECT_EQ(tm_lc.tm_mday, tm_bt.tm_mday);
743 EXPECT_EQ(tm_lc.tm_hour, tm_bt.tm_hour);
744 EXPECT_EQ(tm_lc.tm_min, tm_bt.tm_min);
745 EXPECT_EQ(tm_lc.tm_sec, tm_bt.tm_sec);
746 EXPECT_EQ(tm_lc.tm_wday, tm_bt.tm_wday);
747 EXPECT_EQ(tm_lc.tm_yday, tm_bt.tm_yday);
748 EXPECT_EQ(tm_lc.tm_isdst, tm_bt.tm_isdst);
749
750 ASSERT_FALSE(HasFailure());
751 }
752
753 // Checks that the tm_isdst field is correct when in standard time.
754 const absl::TimeZone nyc =
755 absl::time_internal::LoadTimeZone("America/New_York");
756 absl::Time t = absl::FromCivil(absl::CivilSecond(2014, 3, 1, 0, 0, 0), nyc);
757 struct tm tm = ToTM(t, nyc);
758 EXPECT_FALSE(tm.tm_isdst);
759
760 // Checks that the tm_isdst field is correct when in daylight time.
761 t = absl::FromCivil(absl::CivilSecond(2014, 4, 1, 0, 0, 0), nyc);
762 tm = ToTM(t, nyc);
763 EXPECT_TRUE(tm.tm_isdst);
764
765 // Checks overflow.
766 tm = ToTM(absl::InfiniteFuture(), nyc);
767 EXPECT_EQ(std::numeric_limits<int>::max() - 1900, tm.tm_year);
768 EXPECT_EQ(11, tm.tm_mon);
769 EXPECT_EQ(31, tm.tm_mday);
770 EXPECT_EQ(23, tm.tm_hour);
771 EXPECT_EQ(59, tm.tm_min);
772 EXPECT_EQ(59, tm.tm_sec);
773 EXPECT_EQ(4, tm.tm_wday);
774 EXPECT_EQ(364, tm.tm_yday);
775 EXPECT_FALSE(tm.tm_isdst);
776
777 // Checks underflow.
778 tm = ToTM(absl::InfinitePast(), nyc);
779 EXPECT_EQ(std::numeric_limits<int>::min(), tm.tm_year);
780 EXPECT_EQ(0, tm.tm_mon);
781 EXPECT_EQ(1, tm.tm_mday);
782 EXPECT_EQ(0, tm.tm_hour);
783 EXPECT_EQ(0, tm.tm_min);
784 EXPECT_EQ(0, tm.tm_sec);
785 EXPECT_EQ(0, tm.tm_wday);
786 EXPECT_EQ(0, tm.tm_yday);
787 EXPECT_FALSE(tm.tm_isdst);
788 }
789
TEST(Time,FromTM)790 TEST(Time, FromTM) {
791 const absl::TimeZone nyc =
792 absl::time_internal::LoadTimeZone("America/New_York");
793
794 // Verifies that tm_isdst doesn't affect anything when the time is unique.
795 struct tm tm;
796 std::memset(&tm, 0, sizeof(tm));
797 tm.tm_year = 2014 - 1900;
798 tm.tm_mon = 6 - 1;
799 tm.tm_mday = 28;
800 tm.tm_hour = 1;
801 tm.tm_min = 2;
802 tm.tm_sec = 3;
803 tm.tm_isdst = -1;
804 absl::Time t = FromTM(tm, nyc);
805 EXPECT_EQ("2014-06-28T01:02:03-04:00", absl::FormatTime(t, nyc)); // DST
806 tm.tm_isdst = 0;
807 t = FromTM(tm, nyc);
808 EXPECT_EQ("2014-06-28T01:02:03-04:00", absl::FormatTime(t, nyc)); // DST
809 tm.tm_isdst = 1;
810 t = FromTM(tm, nyc);
811 EXPECT_EQ("2014-06-28T01:02:03-04:00", absl::FormatTime(t, nyc)); // DST
812
813 // Adjusts tm to refer to an ambiguous time.
814 tm.tm_year = 2014 - 1900;
815 tm.tm_mon = 11 - 1;
816 tm.tm_mday = 2;
817 tm.tm_hour = 1;
818 tm.tm_min = 30;
819 tm.tm_sec = 42;
820 tm.tm_isdst = -1;
821 t = FromTM(tm, nyc);
822 EXPECT_EQ("2014-11-02T01:30:42-04:00", absl::FormatTime(t, nyc)); // DST
823 tm.tm_isdst = 0;
824 t = FromTM(tm, nyc);
825 EXPECT_EQ("2014-11-02T01:30:42-05:00", absl::FormatTime(t, nyc)); // STD
826 tm.tm_isdst = 1;
827 t = FromTM(tm, nyc);
828 EXPECT_EQ("2014-11-02T01:30:42-04:00", absl::FormatTime(t, nyc)); // DST
829
830 // Adjusts tm to refer to a skipped time.
831 tm.tm_year = 2014 - 1900;
832 tm.tm_mon = 3 - 1;
833 tm.tm_mday = 9;
834 tm.tm_hour = 2;
835 tm.tm_min = 30;
836 tm.tm_sec = 42;
837 tm.tm_isdst = -1;
838 t = FromTM(tm, nyc);
839 EXPECT_EQ("2014-03-09T03:30:42-04:00", absl::FormatTime(t, nyc)); // DST
840 tm.tm_isdst = 0;
841 t = FromTM(tm, nyc);
842 EXPECT_EQ("2014-03-09T01:30:42-05:00", absl::FormatTime(t, nyc)); // STD
843 tm.tm_isdst = 1;
844 t = FromTM(tm, nyc);
845 EXPECT_EQ("2014-03-09T03:30:42-04:00", absl::FormatTime(t, nyc)); // DST
846
847 // Adjusts tm to refer to a time with a year larger than 2147483647.
848 tm.tm_year = 2147483647 - 1900 + 1;
849 tm.tm_mon = 6 - 1;
850 tm.tm_mday = 28;
851 tm.tm_hour = 1;
852 tm.tm_min = 2;
853 tm.tm_sec = 3;
854 tm.tm_isdst = -1;
855 t = FromTM(tm, absl::UTCTimeZone());
856 EXPECT_EQ("2147483648-06-28T01:02:03+00:00",
857 absl::FormatTime(t, absl::UTCTimeZone()));
858
859 // Adjusts tm to refer to a time with a very large month.
860 tm.tm_year = 2019 - 1900;
861 tm.tm_mon = 2147483647;
862 tm.tm_mday = 28;
863 tm.tm_hour = 1;
864 tm.tm_min = 2;
865 tm.tm_sec = 3;
866 tm.tm_isdst = -1;
867 t = FromTM(tm, absl::UTCTimeZone());
868 EXPECT_EQ("178958989-08-28T01:02:03+00:00",
869 absl::FormatTime(t, absl::UTCTimeZone()));
870 }
871
TEST(Time,TMRoundTrip)872 TEST(Time, TMRoundTrip) {
873 const absl::TimeZone nyc =
874 absl::time_internal::LoadTimeZone("America/New_York");
875
876 // Test round-tripping across a skipped transition
877 absl::Time start = absl::FromCivil(absl::CivilHour(2014, 3, 9, 0), nyc);
878 absl::Time end = absl::FromCivil(absl::CivilHour(2014, 3, 9, 4), nyc);
879 for (absl::Time t = start; t < end; t += absl::Minutes(1)) {
880 struct tm tm = ToTM(t, nyc);
881 absl::Time rt = FromTM(tm, nyc);
882 EXPECT_EQ(rt, t);
883 }
884
885 // Test round-tripping across an ambiguous transition
886 start = absl::FromCivil(absl::CivilHour(2014, 11, 2, 0), nyc);
887 end = absl::FromCivil(absl::CivilHour(2014, 11, 2, 4), nyc);
888 for (absl::Time t = start; t < end; t += absl::Minutes(1)) {
889 struct tm tm = ToTM(t, nyc);
890 absl::Time rt = FromTM(tm, nyc);
891 EXPECT_EQ(rt, t);
892 }
893
894 // Test round-tripping of unique instants crossing a day boundary
895 start = absl::FromCivil(absl::CivilHour(2014, 6, 27, 22), nyc);
896 end = absl::FromCivil(absl::CivilHour(2014, 6, 28, 4), nyc);
897 for (absl::Time t = start; t < end; t += absl::Minutes(1)) {
898 struct tm tm = ToTM(t, nyc);
899 absl::Time rt = FromTM(tm, nyc);
900 EXPECT_EQ(rt, t);
901 }
902 }
903
TEST(Time,Range)904 TEST(Time, Range) {
905 // The API's documented range is +/- 100 billion years.
906 const absl::Duration range = absl::Hours(24) * 365.2425 * 100000000000;
907
908 // Arithmetic and comparison still works at +/-range around base values.
909 absl::Time bases[2] = {absl::UnixEpoch(), absl::Now()};
910 for (const auto base : bases) {
911 absl::Time bottom = base - range;
912 EXPECT_GT(bottom, bottom - absl::Nanoseconds(1));
913 EXPECT_LT(bottom, bottom + absl::Nanoseconds(1));
914 absl::Time top = base + range;
915 EXPECT_GT(top, top - absl::Nanoseconds(1));
916 EXPECT_LT(top, top + absl::Nanoseconds(1));
917 absl::Duration full_range = 2 * range;
918 EXPECT_EQ(full_range, top - bottom);
919 EXPECT_EQ(-full_range, bottom - top);
920 }
921 }
922
TEST(Time,Limits)923 TEST(Time, Limits) {
924 // It is an implementation detail that Time().rep_ == ZeroDuration(),
925 // and that the resolution of a Duration is 1/4 of a nanosecond.
926 const absl::Time zero;
927 const absl::Time max =
928 zero + absl::Seconds(std::numeric_limits<int64_t>::max()) +
929 absl::Nanoseconds(999999999) + absl::Nanoseconds(3) / 4;
930 const absl::Time min =
931 zero + absl::Seconds(std::numeric_limits<int64_t>::min());
932
933 // Some simple max/min bounds checks.
934 EXPECT_LT(max, absl::InfiniteFuture());
935 EXPECT_GT(min, absl::InfinitePast());
936 EXPECT_LT(zero, max);
937 EXPECT_GT(zero, min);
938 EXPECT_GE(absl::UnixEpoch(), min);
939 EXPECT_LT(absl::UnixEpoch(), max);
940
941 // Check sign of Time differences.
942 EXPECT_LT(absl::ZeroDuration(), max - zero);
943 EXPECT_LT(absl::ZeroDuration(),
944 zero - absl::Nanoseconds(1) / 4 - min); // avoid zero - min
945
946 // Arithmetic works at max - 0.25ns and min + 0.25ns.
947 EXPECT_GT(max, max - absl::Nanoseconds(1) / 4);
948 EXPECT_LT(min, min + absl::Nanoseconds(1) / 4);
949 }
950
TEST(Time,ConversionSaturation)951 TEST(Time, ConversionSaturation) {
952 const absl::TimeZone utc = absl::UTCTimeZone();
953 absl::Time t;
954
955 const auto max_time_t = std::numeric_limits<time_t>::max();
956 const auto min_time_t = std::numeric_limits<time_t>::min();
957 time_t tt = max_time_t - 1;
958 t = absl::FromTimeT(tt);
959 tt = absl::ToTimeT(t);
960 EXPECT_EQ(max_time_t - 1, tt);
961 t += absl::Seconds(1);
962 tt = absl::ToTimeT(t);
963 EXPECT_EQ(max_time_t, tt);
964 t += absl::Seconds(1); // no effect
965 tt = absl::ToTimeT(t);
966 EXPECT_EQ(max_time_t, tt);
967
968 tt = min_time_t + 1;
969 t = absl::FromTimeT(tt);
970 tt = absl::ToTimeT(t);
971 EXPECT_EQ(min_time_t + 1, tt);
972 t -= absl::Seconds(1);
973 tt = absl::ToTimeT(t);
974 EXPECT_EQ(min_time_t, tt);
975 t -= absl::Seconds(1); // no effect
976 tt = absl::ToTimeT(t);
977 EXPECT_EQ(min_time_t, tt);
978
979 const auto max_timeval_sec =
980 std::numeric_limits<decltype(timeval::tv_sec)>::max();
981 const auto min_timeval_sec =
982 std::numeric_limits<decltype(timeval::tv_sec)>::min();
983 timeval tv;
984 tv.tv_sec = max_timeval_sec;
985 tv.tv_usec = 999998;
986 t = absl::TimeFromTimeval(tv);
987 tv = ToTimeval(t);
988 EXPECT_EQ(max_timeval_sec, tv.tv_sec);
989 EXPECT_EQ(999998, tv.tv_usec);
990 t += absl::Microseconds(1);
991 tv = ToTimeval(t);
992 EXPECT_EQ(max_timeval_sec, tv.tv_sec);
993 EXPECT_EQ(999999, tv.tv_usec);
994 t += absl::Microseconds(1); // no effect
995 tv = ToTimeval(t);
996 EXPECT_EQ(max_timeval_sec, tv.tv_sec);
997 EXPECT_EQ(999999, tv.tv_usec);
998
999 tv.tv_sec = min_timeval_sec;
1000 tv.tv_usec = 1;
1001 t = absl::TimeFromTimeval(tv);
1002 tv = ToTimeval(t);
1003 EXPECT_EQ(min_timeval_sec, tv.tv_sec);
1004 EXPECT_EQ(1, tv.tv_usec);
1005 t -= absl::Microseconds(1);
1006 tv = ToTimeval(t);
1007 EXPECT_EQ(min_timeval_sec, tv.tv_sec);
1008 EXPECT_EQ(0, tv.tv_usec);
1009 t -= absl::Microseconds(1); // no effect
1010 tv = ToTimeval(t);
1011 EXPECT_EQ(min_timeval_sec, tv.tv_sec);
1012 EXPECT_EQ(0, tv.tv_usec);
1013
1014 const auto max_timespec_sec =
1015 std::numeric_limits<decltype(timespec::tv_sec)>::max();
1016 const auto min_timespec_sec =
1017 std::numeric_limits<decltype(timespec::tv_sec)>::min();
1018 timespec ts;
1019 ts.tv_sec = max_timespec_sec;
1020 ts.tv_nsec = 999999998;
1021 t = absl::TimeFromTimespec(ts);
1022 ts = absl::ToTimespec(t);
1023 EXPECT_EQ(max_timespec_sec, ts.tv_sec);
1024 EXPECT_EQ(999999998, ts.tv_nsec);
1025 t += absl::Nanoseconds(1);
1026 ts = absl::ToTimespec(t);
1027 EXPECT_EQ(max_timespec_sec, ts.tv_sec);
1028 EXPECT_EQ(999999999, ts.tv_nsec);
1029 t += absl::Nanoseconds(1); // no effect
1030 ts = absl::ToTimespec(t);
1031 EXPECT_EQ(max_timespec_sec, ts.tv_sec);
1032 EXPECT_EQ(999999999, ts.tv_nsec);
1033
1034 ts.tv_sec = min_timespec_sec;
1035 ts.tv_nsec = 1;
1036 t = absl::TimeFromTimespec(ts);
1037 ts = absl::ToTimespec(t);
1038 EXPECT_EQ(min_timespec_sec, ts.tv_sec);
1039 EXPECT_EQ(1, ts.tv_nsec);
1040 t -= absl::Nanoseconds(1);
1041 ts = absl::ToTimespec(t);
1042 EXPECT_EQ(min_timespec_sec, ts.tv_sec);
1043 EXPECT_EQ(0, ts.tv_nsec);
1044 t -= absl::Nanoseconds(1); // no effect
1045 ts = absl::ToTimespec(t);
1046 EXPECT_EQ(min_timespec_sec, ts.tv_sec);
1047 EXPECT_EQ(0, ts.tv_nsec);
1048
1049 // Checks how TimeZone::At() saturates on infinities.
1050 auto ci = utc.At(absl::InfiniteFuture());
1051 EXPECT_CIVIL_INFO(ci, std::numeric_limits<int64_t>::max(), 12, 31, 23, 59, 59,
1052 0, false);
1053 EXPECT_EQ(absl::InfiniteDuration(), ci.subsecond);
1054 EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(ci.cs));
1055 EXPECT_EQ(365, absl::GetYearDay(ci.cs));
1056 EXPECT_STREQ("-00", ci.zone_abbr); // artifact of TimeZone::At()
1057 ci = utc.At(absl::InfinitePast());
1058 EXPECT_CIVIL_INFO(ci, std::numeric_limits<int64_t>::min(), 1, 1, 0, 0, 0, 0,
1059 false);
1060 EXPECT_EQ(-absl::InfiniteDuration(), ci.subsecond);
1061 EXPECT_EQ(absl::Weekday::sunday, absl::GetWeekday(ci.cs));
1062 EXPECT_EQ(1, absl::GetYearDay(ci.cs));
1063 EXPECT_STREQ("-00", ci.zone_abbr); // artifact of TimeZone::At()
1064
1065 // Approach the maximal Time value from below.
1066 t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 6), utc);
1067 EXPECT_EQ("292277026596-12-04T15:30:06+00:00",
1068 absl::FormatTime(absl::RFC3339_full, t, utc));
1069 t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 7), utc);
1070 EXPECT_EQ("292277026596-12-04T15:30:07+00:00",
1071 absl::FormatTime(absl::RFC3339_full, t, utc));
1072 EXPECT_EQ(
1073 absl::UnixEpoch() + absl::Seconds(std::numeric_limits<int64_t>::max()),
1074 t);
1075
1076 // Checks that we can also get the maximal Time value for a far-east zone.
1077 const absl::TimeZone plus14 = absl::FixedTimeZone(14 * 60 * 60);
1078 t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 5, 30, 7), plus14);
1079 EXPECT_EQ("292277026596-12-05T05:30:07+14:00",
1080 absl::FormatTime(absl::RFC3339_full, t, plus14));
1081 EXPECT_EQ(
1082 absl::UnixEpoch() + absl::Seconds(std::numeric_limits<int64_t>::max()),
1083 t);
1084
1085 // One second later should push us to infinity.
1086 t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 8), utc);
1087 EXPECT_EQ("infinite-future", absl::FormatTime(absl::RFC3339_full, t, utc));
1088
1089 // Approach the minimal Time value from above.
1090 t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 53), utc);
1091 EXPECT_EQ("-292277022657-01-27T08:29:53+00:00",
1092 absl::FormatTime(absl::RFC3339_full, t, utc));
1093 t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 52), utc);
1094 EXPECT_EQ("-292277022657-01-27T08:29:52+00:00",
1095 absl::FormatTime(absl::RFC3339_full, t, utc));
1096 EXPECT_EQ(
1097 absl::UnixEpoch() + absl::Seconds(std::numeric_limits<int64_t>::min()),
1098 t);
1099
1100 // Checks that we can also get the minimal Time value for a far-west zone.
1101 const absl::TimeZone minus12 = absl::FixedTimeZone(-12 * 60 * 60);
1102 t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 26, 20, 29, 52),
1103 minus12);
1104 EXPECT_EQ("-292277022657-01-26T20:29:52-12:00",
1105 absl::FormatTime(absl::RFC3339_full, t, minus12));
1106 EXPECT_EQ(
1107 absl::UnixEpoch() + absl::Seconds(std::numeric_limits<int64_t>::min()),
1108 t);
1109
1110 // One second before should push us to -infinity.
1111 t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 51), utc);
1112 EXPECT_EQ("infinite-past", absl::FormatTime(absl::RFC3339_full, t, utc));
1113 }
1114
1115 // In zones with POSIX-style recurring rules we use special logic to
1116 // handle conversions in the distant future. Here we check the limits
1117 // of those conversions, particularly with respect to integer overflow.
TEST(Time,ExtendedConversionSaturation)1118 TEST(Time, ExtendedConversionSaturation) {
1119 const absl::TimeZone syd =
1120 absl::time_internal::LoadTimeZone("Australia/Sydney");
1121 const absl::TimeZone nyc =
1122 absl::time_internal::LoadTimeZone("America/New_York");
1123 const absl::Time max =
1124 absl::FromUnixSeconds(std::numeric_limits<int64_t>::max());
1125 absl::TimeZone::CivilInfo ci;
1126 absl::Time t;
1127
1128 // The maximal time converted in each zone.
1129 ci = syd.At(max);
1130 EXPECT_CIVIL_INFO(ci, 292277026596, 12, 5, 2, 30, 7, 39600, true);
1131 t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 7), syd);
1132 EXPECT_EQ(max, t);
1133 ci = nyc.At(max);
1134 EXPECT_CIVIL_INFO(ci, 292277026596, 12, 4, 10, 30, 7, -18000, false);
1135 t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 7), nyc);
1136 EXPECT_EQ(max, t);
1137
1138 // One second later should push us to infinity.
1139 t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 8), syd);
1140 EXPECT_EQ(absl::InfiniteFuture(), t);
1141 t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 8), nyc);
1142 EXPECT_EQ(absl::InfiniteFuture(), t);
1143
1144 // And we should stick there.
1145 t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 9), syd);
1146 EXPECT_EQ(absl::InfiniteFuture(), t);
1147 t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 9), nyc);
1148 EXPECT_EQ(absl::InfiniteFuture(), t);
1149
1150 // All the way up to a saturated date/time, without overflow.
1151 t = absl::FromCivil(absl::CivilSecond::max(), syd);
1152 EXPECT_EQ(absl::InfiniteFuture(), t);
1153 t = absl::FromCivil(absl::CivilSecond::max(), nyc);
1154 EXPECT_EQ(absl::InfiniteFuture(), t);
1155 }
1156
TEST(Time,FromCivilAlignment)1157 TEST(Time, FromCivilAlignment) {
1158 const absl::TimeZone utc = absl::UTCTimeZone();
1159 const absl::CivilSecond cs(2015, 2, 3, 4, 5, 6);
1160 absl::Time t = absl::FromCivil(cs, utc);
1161 EXPECT_EQ("2015-02-03T04:05:06+00:00", absl::FormatTime(t, utc));
1162 t = absl::FromCivil(absl::CivilMinute(cs), utc);
1163 EXPECT_EQ("2015-02-03T04:05:00+00:00", absl::FormatTime(t, utc));
1164 t = absl::FromCivil(absl::CivilHour(cs), utc);
1165 EXPECT_EQ("2015-02-03T04:00:00+00:00", absl::FormatTime(t, utc));
1166 t = absl::FromCivil(absl::CivilDay(cs), utc);
1167 EXPECT_EQ("2015-02-03T00:00:00+00:00", absl::FormatTime(t, utc));
1168 t = absl::FromCivil(absl::CivilMonth(cs), utc);
1169 EXPECT_EQ("2015-02-01T00:00:00+00:00", absl::FormatTime(t, utc));
1170 t = absl::FromCivil(absl::CivilYear(cs), utc);
1171 EXPECT_EQ("2015-01-01T00:00:00+00:00", absl::FormatTime(t, utc));
1172 }
1173
TEST(Time,LegacyDateTime)1174 TEST(Time, LegacyDateTime) {
1175 const absl::TimeZone utc = absl::UTCTimeZone();
1176 const std::string ymdhms = "%Y-%m-%d %H:%M:%S";
1177 const int kMax = std::numeric_limits<int>::max();
1178 const int kMin = std::numeric_limits<int>::min();
1179 absl::Time t;
1180
1181 t = absl::FromDateTime(std::numeric_limits<absl::civil_year_t>::max(), kMax,
1182 kMax, kMax, kMax, kMax, utc);
1183 EXPECT_EQ("infinite-future",
1184 absl::FormatTime(ymdhms, t, utc)); // no overflow
1185 t = absl::FromDateTime(std::numeric_limits<absl::civil_year_t>::min(), kMin,
1186 kMin, kMin, kMin, kMin, utc);
1187 EXPECT_EQ("infinite-past", absl::FormatTime(ymdhms, t, utc)); // no overflow
1188
1189 // Check normalization.
1190 EXPECT_TRUE(absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, utc).normalized);
1191 t = absl::FromDateTime(2015, 1, 1, 0, 0, 60, utc);
1192 EXPECT_EQ("2015-01-01 00:01:00", absl::FormatTime(ymdhms, t, utc));
1193 t = absl::FromDateTime(2015, 1, 1, 0, 60, 0, utc);
1194 EXPECT_EQ("2015-01-01 01:00:00", absl::FormatTime(ymdhms, t, utc));
1195 t = absl::FromDateTime(2015, 1, 1, 24, 0, 0, utc);
1196 EXPECT_EQ("2015-01-02 00:00:00", absl::FormatTime(ymdhms, t, utc));
1197 t = absl::FromDateTime(2015, 1, 32, 0, 0, 0, utc);
1198 EXPECT_EQ("2015-02-01 00:00:00", absl::FormatTime(ymdhms, t, utc));
1199 t = absl::FromDateTime(2015, 13, 1, 0, 0, 0, utc);
1200 EXPECT_EQ("2016-01-01 00:00:00", absl::FormatTime(ymdhms, t, utc));
1201 t = absl::FromDateTime(2015, 13, 32, 60, 60, 60, utc);
1202 EXPECT_EQ("2016-02-03 13:01:00", absl::FormatTime(ymdhms, t, utc));
1203 t = absl::FromDateTime(2015, 1, 1, 0, 0, -1, utc);
1204 EXPECT_EQ("2014-12-31 23:59:59", absl::FormatTime(ymdhms, t, utc));
1205 t = absl::FromDateTime(2015, 1, 1, 0, -1, 0, utc);
1206 EXPECT_EQ("2014-12-31 23:59:00", absl::FormatTime(ymdhms, t, utc));
1207 t = absl::FromDateTime(2015, 1, 1, -1, 0, 0, utc);
1208 EXPECT_EQ("2014-12-31 23:00:00", absl::FormatTime(ymdhms, t, utc));
1209 t = absl::FromDateTime(2015, 1, -1, 0, 0, 0, utc);
1210 EXPECT_EQ("2014-12-30 00:00:00", absl::FormatTime(ymdhms, t, utc));
1211 t = absl::FromDateTime(2015, -1, 1, 0, 0, 0, utc);
1212 EXPECT_EQ("2014-11-01 00:00:00", absl::FormatTime(ymdhms, t, utc));
1213 t = absl::FromDateTime(2015, -1, -1, -1, -1, -1, utc);
1214 EXPECT_EQ("2014-10-29 22:58:59", absl::FormatTime(ymdhms, t, utc));
1215 }
1216
TEST(Time,NextTransitionUTC)1217 TEST(Time, NextTransitionUTC) {
1218 const auto tz = absl::UTCTimeZone();
1219 absl::TimeZone::CivilTransition trans;
1220
1221 auto t = absl::InfinitePast();
1222 EXPECT_FALSE(tz.NextTransition(t, &trans));
1223
1224 t = absl::InfiniteFuture();
1225 EXPECT_FALSE(tz.NextTransition(t, &trans));
1226 }
1227
TEST(Time,PrevTransitionUTC)1228 TEST(Time, PrevTransitionUTC) {
1229 const auto tz = absl::UTCTimeZone();
1230 absl::TimeZone::CivilTransition trans;
1231
1232 auto t = absl::InfiniteFuture();
1233 EXPECT_FALSE(tz.PrevTransition(t, &trans));
1234
1235 t = absl::InfinitePast();
1236 EXPECT_FALSE(tz.PrevTransition(t, &trans));
1237 }
1238
TEST(Time,NextTransitionNYC)1239 TEST(Time, NextTransitionNYC) {
1240 const auto tz = absl::time_internal::LoadTimeZone("America/New_York");
1241 absl::TimeZone::CivilTransition trans;
1242
1243 auto t = absl::FromCivil(absl::CivilSecond(2018, 6, 30, 0, 0, 0), tz);
1244 EXPECT_TRUE(tz.NextTransition(t, &trans));
1245 EXPECT_EQ(absl::CivilSecond(2018, 11, 4, 2, 0, 0), trans.from);
1246 EXPECT_EQ(absl::CivilSecond(2018, 11, 4, 1, 0, 0), trans.to);
1247
1248 t = absl::InfiniteFuture();
1249 EXPECT_FALSE(tz.NextTransition(t, &trans));
1250
1251 t = absl::InfinitePast();
1252 EXPECT_TRUE(tz.NextTransition(t, &trans));
1253 if (trans.from == absl::CivilSecond(1918, 03, 31, 2, 0, 0)) {
1254 // It looks like the tzdata is only 32 bit (probably macOS),
1255 // which bottoms out at 1901-12-13T20:45:52+00:00.
1256 EXPECT_EQ(absl::CivilSecond(1918, 3, 31, 3, 0, 0), trans.to);
1257 } else {
1258 EXPECT_EQ(absl::CivilSecond(1883, 11, 18, 12, 3, 58), trans.from);
1259 EXPECT_EQ(absl::CivilSecond(1883, 11, 18, 12, 0, 0), trans.to);
1260 }
1261 }
1262
TEST(Time,PrevTransitionNYC)1263 TEST(Time, PrevTransitionNYC) {
1264 const auto tz = absl::time_internal::LoadTimeZone("America/New_York");
1265 absl::TimeZone::CivilTransition trans;
1266
1267 auto t = absl::FromCivil(absl::CivilSecond(2018, 6, 30, 0, 0, 0), tz);
1268 EXPECT_TRUE(tz.PrevTransition(t, &trans));
1269 EXPECT_EQ(absl::CivilSecond(2018, 3, 11, 2, 0, 0), trans.from);
1270 EXPECT_EQ(absl::CivilSecond(2018, 3, 11, 3, 0, 0), trans.to);
1271
1272 t = absl::InfinitePast();
1273 EXPECT_FALSE(tz.PrevTransition(t, &trans));
1274
1275 t = absl::InfiniteFuture();
1276 EXPECT_TRUE(tz.PrevTransition(t, &trans));
1277 // We have a transition but we don't know which one.
1278 }
1279
1280 } // namespace
1281