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


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:58