1 // Copyright 2016 Google Inc. All Rights Reserved.
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 <chrono>
16 #include <cstddef>
17 #include <cstdlib>
18 #include <future>
19 #include <limits>
20 #include <string>
21 #include <thread>
22 #include <vector>
23
24 #include "absl/base/config.h"
25 #include "absl/time/internal/cctz/include/cctz/time_zone.h"
26 #if defined(__linux__)
27 #include <features.h>
28 #endif
29
30 #include "gtest/gtest.h"
31 #include "absl/time/internal/cctz/include/cctz/civil_time.h"
32
33 namespace chrono = std::chrono;
34
35 namespace absl {
36 ABSL_NAMESPACE_BEGIN
37 namespace time_internal {
38 namespace cctz {
39
40 namespace {
41
42 // A list of known time-zone names.
43 const char* const kTimeZoneNames[] = {"Africa/Abidjan",
44 "Africa/Accra",
45 "Africa/Addis_Ababa",
46 "Africa/Algiers",
47 "Africa/Asmara",
48 "Africa/Bamako",
49 "Africa/Bangui",
50 "Africa/Banjul",
51 "Africa/Bissau",
52 "Africa/Blantyre",
53 "Africa/Brazzaville",
54 "Africa/Bujumbura",
55 "Africa/Cairo",
56 "Africa/Casablanca",
57 "Africa/Ceuta",
58 "Africa/Conakry",
59 "Africa/Dakar",
60 "Africa/Dar_es_Salaam",
61 "Africa/Djibouti",
62 "Africa/Douala",
63 "Africa/El_Aaiun",
64 "Africa/Freetown",
65 "Africa/Gaborone",
66 "Africa/Harare",
67 "Africa/Johannesburg",
68 "Africa/Juba",
69 "Africa/Kampala",
70 "Africa/Khartoum",
71 "Africa/Kigali",
72 "Africa/Kinshasa",
73 "Africa/Lagos",
74 "Africa/Libreville",
75 "Africa/Lome",
76 "Africa/Luanda",
77 "Africa/Lubumbashi",
78 "Africa/Lusaka",
79 "Africa/Malabo",
80 "Africa/Maputo",
81 "Africa/Maseru",
82 "Africa/Mbabane",
83 "Africa/Mogadishu",
84 "Africa/Monrovia",
85 "Africa/Nairobi",
86 "Africa/Ndjamena",
87 "Africa/Niamey",
88 "Africa/Nouakchott",
89 "Africa/Ouagadougou",
90 "Africa/Porto-Novo",
91 "Africa/Sao_Tome",
92 "Africa/Timbuktu",
93 "Africa/Tripoli",
94 "Africa/Tunis",
95 "Africa/Windhoek",
96 "America/Adak",
97 "America/Anchorage",
98 "America/Anguilla",
99 "America/Antigua",
100 "America/Araguaina",
101 "America/Argentina/Buenos_Aires",
102 "America/Argentina/Catamarca",
103 "America/Argentina/Cordoba",
104 "America/Argentina/Jujuy",
105 "America/Argentina/La_Rioja",
106 "America/Argentina/Mendoza",
107 "America/Argentina/Rio_Gallegos",
108 "America/Argentina/Salta",
109 "America/Argentina/San_Juan",
110 "America/Argentina/San_Luis",
111 "America/Argentina/Tucuman",
112 "America/Argentina/Ushuaia",
113 "America/Aruba",
114 "America/Asuncion",
115 "America/Atikokan",
116 "America/Atka",
117 "America/Bahia",
118 "America/Bahia_Banderas",
119 "America/Barbados",
120 "America/Belem",
121 "America/Belize",
122 "America/Blanc-Sablon",
123 "America/Boa_Vista",
124 "America/Bogota",
125 "America/Boise",
126 "America/Cambridge_Bay",
127 "America/Campo_Grande",
128 "America/Cancun",
129 "America/Caracas",
130 "America/Cayenne",
131 "America/Cayman",
132 "America/Chicago",
133 "America/Chihuahua",
134 "America/Ciudad_Juarez",
135 "America/Coral_Harbour",
136 "America/Costa_Rica",
137 "America/Creston",
138 "America/Cuiaba",
139 "America/Curacao",
140 "America/Danmarkshavn",
141 "America/Dawson",
142 "America/Dawson_Creek",
143 "America/Denver",
144 "America/Detroit",
145 "America/Dominica",
146 "America/Edmonton",
147 "America/Eirunepe",
148 "America/El_Salvador",
149 "America/Ensenada",
150 "America/Fort_Nelson",
151 "America/Fortaleza",
152 "America/Glace_Bay",
153 "America/Godthab",
154 "America/Goose_Bay",
155 "America/Grand_Turk",
156 "America/Grenada",
157 "America/Guadeloupe",
158 "America/Guatemala",
159 "America/Guayaquil",
160 "America/Guyana",
161 "America/Halifax",
162 "America/Havana",
163 "America/Hermosillo",
164 "America/Indiana/Indianapolis",
165 "America/Indiana/Knox",
166 "America/Indiana/Marengo",
167 "America/Indiana/Petersburg",
168 "America/Indiana/Tell_City",
169 "America/Indiana/Vevay",
170 "America/Indiana/Vincennes",
171 "America/Indiana/Winamac",
172 "America/Inuvik",
173 "America/Iqaluit",
174 "America/Jamaica",
175 "America/Juneau",
176 "America/Kentucky/Louisville",
177 "America/Kentucky/Monticello",
178 "America/Kralendijk",
179 "America/La_Paz",
180 "America/Lima",
181 "America/Los_Angeles",
182 "America/Lower_Princes",
183 "America/Maceio",
184 "America/Managua",
185 "America/Manaus",
186 "America/Marigot",
187 "America/Martinique",
188 "America/Matamoros",
189 "America/Mazatlan",
190 "America/Menominee",
191 "America/Merida",
192 "America/Metlakatla",
193 "America/Mexico_City",
194 "America/Miquelon",
195 "America/Moncton",
196 "America/Monterrey",
197 "America/Montevideo",
198 "America/Montreal",
199 "America/Montserrat",
200 "America/Nassau",
201 "America/New_York",
202 "America/Nipigon",
203 "America/Nome",
204 "America/Noronha",
205 "America/North_Dakota/Beulah",
206 "America/North_Dakota/Center",
207 "America/North_Dakota/New_Salem",
208 "America/Nuuk",
209 "America/Ojinaga",
210 "America/Panama",
211 "America/Pangnirtung",
212 "America/Paramaribo",
213 "America/Phoenix",
214 "America/Port-au-Prince",
215 "America/Port_of_Spain",
216 "America/Porto_Acre",
217 "America/Porto_Velho",
218 "America/Puerto_Rico",
219 "America/Punta_Arenas",
220 "America/Rainy_River",
221 "America/Rankin_Inlet",
222 "America/Recife",
223 "America/Regina",
224 "America/Resolute",
225 "America/Rio_Branco",
226 "America/Santa_Isabel",
227 "America/Santarem",
228 "America/Santiago",
229 "America/Santo_Domingo",
230 "America/Sao_Paulo",
231 "America/Scoresbysund",
232 "America/Shiprock",
233 "America/Sitka",
234 "America/St_Barthelemy",
235 "America/St_Johns",
236 "America/St_Kitts",
237 "America/St_Lucia",
238 "America/St_Thomas",
239 "America/St_Vincent",
240 "America/Swift_Current",
241 "America/Tegucigalpa",
242 "America/Thule",
243 "America/Thunder_Bay",
244 "America/Tijuana",
245 "America/Toronto",
246 "America/Tortola",
247 "America/Vancouver",
248 "America/Virgin",
249 "America/Whitehorse",
250 "America/Winnipeg",
251 "America/Yakutat",
252 "America/Yellowknife",
253 "Antarctica/Casey",
254 "Antarctica/Davis",
255 "Antarctica/DumontDUrville",
256 "Antarctica/Macquarie",
257 "Antarctica/Mawson",
258 "Antarctica/McMurdo",
259 "Antarctica/Palmer",
260 "Antarctica/Rothera",
261 "Antarctica/Syowa",
262 "Antarctica/Troll",
263 "Antarctica/Vostok",
264 "Arctic/Longyearbyen",
265 "Asia/Aden",
266 "Asia/Almaty",
267 "Asia/Amman",
268 "Asia/Anadyr",
269 "Asia/Aqtau",
270 "Asia/Aqtobe",
271 "Asia/Ashgabat",
272 "Asia/Atyrau",
273 "Asia/Baghdad",
274 "Asia/Bahrain",
275 "Asia/Baku",
276 "Asia/Bangkok",
277 "Asia/Barnaul",
278 "Asia/Beirut",
279 "Asia/Bishkek",
280 "Asia/Brunei",
281 "Asia/Chita",
282 "Asia/Choibalsan",
283 "Asia/Chongqing",
284 "Asia/Colombo",
285 "Asia/Damascus",
286 "Asia/Dhaka",
287 "Asia/Dili",
288 "Asia/Dubai",
289 "Asia/Dushanbe",
290 "Asia/Famagusta",
291 "Asia/Gaza",
292 "Asia/Harbin",
293 "Asia/Hebron",
294 "Asia/Ho_Chi_Minh",
295 "Asia/Hong_Kong",
296 "Asia/Hovd",
297 "Asia/Irkutsk",
298 "Asia/Istanbul",
299 "Asia/Jakarta",
300 "Asia/Jayapura",
301 "Asia/Jerusalem",
302 "Asia/Kabul",
303 "Asia/Kamchatka",
304 "Asia/Karachi",
305 "Asia/Kashgar",
306 "Asia/Kathmandu",
307 "Asia/Khandyga",
308 "Asia/Kolkata",
309 "Asia/Krasnoyarsk",
310 "Asia/Kuala_Lumpur",
311 "Asia/Kuching",
312 "Asia/Kuwait",
313 "Asia/Macau",
314 "Asia/Magadan",
315 "Asia/Makassar",
316 "Asia/Manila",
317 "Asia/Muscat",
318 "Asia/Nicosia",
319 "Asia/Novokuznetsk",
320 "Asia/Novosibirsk",
321 "Asia/Omsk",
322 "Asia/Oral",
323 "Asia/Phnom_Penh",
324 "Asia/Pontianak",
325 "Asia/Pyongyang",
326 "Asia/Qatar",
327 "Asia/Qostanay",
328 "Asia/Qyzylorda",
329 "Asia/Riyadh",
330 "Asia/Sakhalin",
331 "Asia/Samarkand",
332 "Asia/Seoul",
333 "Asia/Shanghai",
334 "Asia/Singapore",
335 "Asia/Srednekolymsk",
336 "Asia/Taipei",
337 "Asia/Tashkent",
338 "Asia/Tbilisi",
339 "Asia/Tehran",
340 "Asia/Tel_Aviv",
341 "Asia/Thimphu",
342 "Asia/Tokyo",
343 "Asia/Tomsk",
344 "Asia/Ulaanbaatar",
345 "Asia/Urumqi",
346 "Asia/Ust-Nera",
347 "Asia/Vientiane",
348 "Asia/Vladivostok",
349 "Asia/Yakutsk",
350 "Asia/Yangon",
351 "Asia/Yekaterinburg",
352 "Asia/Yerevan",
353 "Atlantic/Azores",
354 "Atlantic/Bermuda",
355 "Atlantic/Canary",
356 "Atlantic/Cape_Verde",
357 "Atlantic/Faroe",
358 "Atlantic/Jan_Mayen",
359 "Atlantic/Madeira",
360 "Atlantic/Reykjavik",
361 "Atlantic/South_Georgia",
362 "Atlantic/St_Helena",
363 "Atlantic/Stanley",
364 "Australia/Adelaide",
365 "Australia/Brisbane",
366 "Australia/Broken_Hill",
367 "Australia/Canberra",
368 "Australia/Currie",
369 "Australia/Darwin",
370 "Australia/Eucla",
371 "Australia/Hobart",
372 "Australia/Lindeman",
373 "Australia/Lord_Howe",
374 "Australia/Melbourne",
375 "Australia/Perth",
376 "Australia/Sydney",
377 "Australia/Yancowinna",
378 "Etc/GMT",
379 "Etc/GMT+0",
380 "Etc/GMT+1",
381 "Etc/GMT+10",
382 "Etc/GMT+11",
383 "Etc/GMT+12",
384 "Etc/GMT+2",
385 "Etc/GMT+3",
386 "Etc/GMT+4",
387 "Etc/GMT+5",
388 "Etc/GMT+6",
389 "Etc/GMT+7",
390 "Etc/GMT+8",
391 "Etc/GMT+9",
392 "Etc/GMT-0",
393 "Etc/GMT-1",
394 "Etc/GMT-10",
395 "Etc/GMT-11",
396 "Etc/GMT-12",
397 "Etc/GMT-13",
398 "Etc/GMT-14",
399 "Etc/GMT-2",
400 "Etc/GMT-3",
401 "Etc/GMT-4",
402 "Etc/GMT-5",
403 "Etc/GMT-6",
404 "Etc/GMT-7",
405 "Etc/GMT-8",
406 "Etc/GMT-9",
407 "Etc/GMT0",
408 "Etc/Greenwich",
409 "Etc/UCT",
410 "Etc/UTC",
411 "Etc/Universal",
412 "Etc/Zulu",
413 "Europe/Amsterdam",
414 "Europe/Andorra",
415 "Europe/Astrakhan",
416 "Europe/Athens",
417 "Europe/Belfast",
418 "Europe/Belgrade",
419 "Europe/Berlin",
420 "Europe/Bratislava",
421 "Europe/Brussels",
422 "Europe/Bucharest",
423 "Europe/Budapest",
424 "Europe/Busingen",
425 "Europe/Chisinau",
426 "Europe/Copenhagen",
427 "Europe/Dublin",
428 "Europe/Gibraltar",
429 "Europe/Guernsey",
430 "Europe/Helsinki",
431 "Europe/Isle_of_Man",
432 "Europe/Istanbul",
433 "Europe/Jersey",
434 "Europe/Kaliningrad",
435 "Europe/Kirov",
436 "Europe/Kyiv",
437 "Europe/Lisbon",
438 "Europe/Ljubljana",
439 "Europe/London",
440 "Europe/Luxembourg",
441 "Europe/Madrid",
442 "Europe/Malta",
443 "Europe/Mariehamn",
444 "Europe/Minsk",
445 "Europe/Monaco",
446 "Europe/Moscow",
447 "Europe/Nicosia",
448 "Europe/Oslo",
449 "Europe/Paris",
450 "Europe/Podgorica",
451 "Europe/Prague",
452 "Europe/Riga",
453 "Europe/Rome",
454 "Europe/Samara",
455 "Europe/San_Marino",
456 "Europe/Sarajevo",
457 "Europe/Saratov",
458 "Europe/Simferopol",
459 "Europe/Skopje",
460 "Europe/Sofia",
461 "Europe/Stockholm",
462 "Europe/Tallinn",
463 "Europe/Tirane",
464 "Europe/Tiraspol",
465 "Europe/Ulyanovsk",
466 "Europe/Vaduz",
467 "Europe/Vatican",
468 "Europe/Vienna",
469 "Europe/Vilnius",
470 "Europe/Volgograd",
471 "Europe/Warsaw",
472 "Europe/Zagreb",
473 "Europe/Zurich",
474 "Factory",
475 "Indian/Antananarivo",
476 "Indian/Chagos",
477 "Indian/Christmas",
478 "Indian/Cocos",
479 "Indian/Comoro",
480 "Indian/Kerguelen",
481 "Indian/Mahe",
482 "Indian/Maldives",
483 "Indian/Mauritius",
484 "Indian/Mayotte",
485 "Indian/Reunion",
486 "Pacific/Apia",
487 "Pacific/Auckland",
488 "Pacific/Bougainville",
489 "Pacific/Chatham",
490 "Pacific/Chuuk",
491 "Pacific/Easter",
492 "Pacific/Efate",
493 "Pacific/Fakaofo",
494 "Pacific/Fiji",
495 "Pacific/Funafuti",
496 "Pacific/Galapagos",
497 "Pacific/Gambier",
498 "Pacific/Guadalcanal",
499 "Pacific/Guam",
500 "Pacific/Honolulu",
501 "Pacific/Johnston",
502 "Pacific/Kanton",
503 "Pacific/Kiritimati",
504 "Pacific/Kosrae",
505 "Pacific/Kwajalein",
506 "Pacific/Majuro",
507 "Pacific/Marquesas",
508 "Pacific/Midway",
509 "Pacific/Nauru",
510 "Pacific/Niue",
511 "Pacific/Norfolk",
512 "Pacific/Noumea",
513 "Pacific/Pago_Pago",
514 "Pacific/Palau",
515 "Pacific/Pitcairn",
516 "Pacific/Pohnpei",
517 "Pacific/Port_Moresby",
518 "Pacific/Rarotonga",
519 "Pacific/Saipan",
520 "Pacific/Samoa",
521 "Pacific/Tahiti",
522 "Pacific/Tarawa",
523 "Pacific/Tongatapu",
524 "Pacific/Wake",
525 "Pacific/Wallis",
526 "Pacific/Yap",
527 "UTC",
528 nullptr};
529
530 // Helper to return a loaded time zone by value (UTC on error).
LoadZone(const std::string & name)531 time_zone LoadZone(const std::string& name) {
532 time_zone tz;
533 load_time_zone(name, &tz);
534 return tz;
535 }
536
537 // This helper is a macro so that failed expectations show up with the
538 // correct line numbers.
539 #define ExpectTime(tp, tz, y, m, d, hh, mm, ss, off, isdst, zone) \
540 do { \
541 time_zone::absolute_lookup al = tz.lookup(tp); \
542 EXPECT_EQ(y, al.cs.year()); \
543 EXPECT_EQ(m, al.cs.month()); \
544 EXPECT_EQ(d, al.cs.day()); \
545 EXPECT_EQ(hh, al.cs.hour()); \
546 EXPECT_EQ(mm, al.cs.minute()); \
547 EXPECT_EQ(ss, al.cs.second()); \
548 EXPECT_EQ(off, al.offset); \
549 EXPECT_TRUE(isdst == al.is_dst); \
550 /* EXPECT_STREQ(zone, al.abbr); */ \
551 } while (0)
552
553 // These tests sometimes run on platforms that have zoneinfo data so old
554 // that the transition we are attempting to check does not exist, most
555 // notably Android emulators. Fortunately, AndroidZoneInfoSource supports
556 // time_zone::version() so, in cases where we've learned that it matters,
557 // we can make the check conditionally.
VersionCmp(time_zone tz,const std::string & target)558 int VersionCmp(time_zone tz, const std::string& target) {
559 std::string version = tz.version();
560 if (version.empty() && !target.empty()) return 1; // unknown > known
561 return version.compare(target);
562 }
563
564 } // namespace
565
566 #if !defined(__EMSCRIPTEN__)
TEST(TimeZones,LoadZonesConcurrently)567 TEST(TimeZones, LoadZonesConcurrently) {
568 std::promise<void> ready_promise;
569 std::shared_future<void> ready_future(ready_promise.get_future());
570 auto load_zones = [ready_future](std::promise<void>* started,
571 std::set<std::string>* failures) {
572 started->set_value();
573 ready_future.wait();
574 for (const char* const* np = kTimeZoneNames; *np != nullptr; ++np) {
575 std::string zone = *np;
576 time_zone tz;
577 if (load_time_zone(zone, &tz)) {
578 EXPECT_EQ(zone, tz.name());
579 } else {
580 failures->insert(zone);
581 }
582 }
583 };
584
585 const std::size_t n_threads = 128;
586 std::vector<std::thread> threads;
587 std::vector<std::set<std::string>> thread_failures(n_threads);
588 for (std::size_t i = 0; i != n_threads; ++i) {
589 std::promise<void> started;
590 threads.emplace_back(load_zones, &started, &thread_failures[i]);
591 started.get_future().wait();
592 }
593 ready_promise.set_value();
594 for (auto& thread : threads) {
595 thread.join();
596 }
597
598 // Allow a small number of failures to account for skew between
599 // the contents of kTimeZoneNames and the zoneinfo data source.
600 #if defined(__ANDROID__)
601 // Cater to the possibility of using an even older zoneinfo data
602 // source when running on Android, where it is difficult to override
603 // the bionic tzdata provided by the test environment.
604 const std::size_t max_failures = 20;
605 #else
606 const std::size_t max_failures = 3;
607 #endif
608 std::set<std::string> failures;
609 for (const auto& thread_failure : thread_failures) {
610 failures.insert(thread_failure.begin(), thread_failure.end());
611 }
612 EXPECT_LE(failures.size(), max_failures) << testing::PrintToString(failures);
613 }
614 #endif
615
TEST(TimeZone,UTC)616 TEST(TimeZone, UTC) {
617 const time_zone utc = utc_time_zone();
618
619 time_zone loaded_utc;
620 EXPECT_TRUE(load_time_zone("UTC", &loaded_utc));
621 EXPECT_EQ(loaded_utc, utc);
622
623 time_zone loaded_utc0;
624 EXPECT_TRUE(load_time_zone("UTC0", &loaded_utc0));
625 EXPECT_EQ(loaded_utc0, utc);
626
627 time_zone loaded_bad;
628 EXPECT_FALSE(load_time_zone("Invalid/TimeZone", &loaded_bad));
629 EXPECT_EQ(loaded_bad, utc);
630 }
631
TEST(TimeZone,NamedTimeZones)632 TEST(TimeZone, NamedTimeZones) {
633 const time_zone utc = utc_time_zone();
634 EXPECT_EQ("UTC", utc.name());
635 const time_zone nyc = LoadZone("America/New_York");
636 EXPECT_EQ("America/New_York", nyc.name());
637 const time_zone syd = LoadZone("Australia/Sydney");
638 EXPECT_EQ("Australia/Sydney", syd.name());
639 const time_zone fixed0 =
640 fixed_time_zone(absl::time_internal::cctz::seconds::zero());
641 EXPECT_EQ("UTC", fixed0.name());
642 const time_zone fixed_pos = fixed_time_zone(
643 chrono::hours(3) + chrono::minutes(25) + chrono::seconds(45));
644 EXPECT_EQ("Fixed/UTC+03:25:45", fixed_pos.name());
645 const time_zone fixed_neg = fixed_time_zone(
646 -(chrono::hours(12) + chrono::minutes(34) + chrono::seconds(56)));
647 EXPECT_EQ("Fixed/UTC-12:34:56", fixed_neg.name());
648 }
649
TEST(TimeZone,Failures)650 TEST(TimeZone, Failures) {
651 time_zone tz;
652 EXPECT_FALSE(load_time_zone(":America/Los_Angeles", &tz));
653
654 tz = LoadZone("America/Los_Angeles");
655 EXPECT_FALSE(load_time_zone("Invalid/TimeZone", &tz));
656 EXPECT_EQ(chrono::system_clock::from_time_t(0),
657 convert(civil_second(1970, 1, 1, 0, 0, 0), tz)); // UTC
658
659 // Ensures that the load still fails on a subsequent attempt.
660 tz = LoadZone("America/Los_Angeles");
661 EXPECT_FALSE(load_time_zone("Invalid/TimeZone", &tz));
662 EXPECT_EQ(chrono::system_clock::from_time_t(0),
663 convert(civil_second(1970, 1, 1, 0, 0, 0), tz)); // UTC
664
665 // Loading an empty string timezone should fail.
666 tz = LoadZone("America/Los_Angeles");
667 EXPECT_FALSE(load_time_zone("", &tz));
668 EXPECT_EQ(chrono::system_clock::from_time_t(0),
669 convert(civil_second(1970, 1, 1, 0, 0, 0), tz)); // UTC
670 }
671
TEST(TimeZone,Equality)672 TEST(TimeZone, Equality) {
673 const time_zone a;
674 const time_zone b;
675 EXPECT_EQ(a, b);
676 EXPECT_EQ(a.name(), b.name());
677
678 const time_zone implicit_utc;
679 const time_zone explicit_utc = utc_time_zone();
680 EXPECT_EQ(implicit_utc, explicit_utc);
681 EXPECT_EQ(implicit_utc.name(), explicit_utc.name());
682
683 const time_zone fixed_zero =
684 fixed_time_zone(absl::time_internal::cctz::seconds::zero());
685 EXPECT_EQ(fixed_zero, LoadZone(fixed_zero.name()));
686 EXPECT_EQ(fixed_zero, explicit_utc);
687
688 const time_zone fixed_utc = LoadZone("Fixed/UTC+00:00:00");
689 EXPECT_EQ(fixed_utc, LoadZone(fixed_utc.name()));
690 EXPECT_EQ(fixed_utc, explicit_utc);
691
692 const time_zone fixed_pos = fixed_time_zone(
693 chrono::hours(3) + chrono::minutes(25) + chrono::seconds(45));
694 EXPECT_EQ(fixed_pos, LoadZone(fixed_pos.name()));
695 EXPECT_NE(fixed_pos, explicit_utc);
696 const time_zone fixed_neg = fixed_time_zone(
697 -(chrono::hours(12) + chrono::minutes(34) + chrono::seconds(56)));
698 EXPECT_EQ(fixed_neg, LoadZone(fixed_neg.name()));
699 EXPECT_NE(fixed_neg, explicit_utc);
700
701 const time_zone fixed_lim = fixed_time_zone(chrono::hours(24));
702 EXPECT_EQ(fixed_lim, LoadZone(fixed_lim.name()));
703 EXPECT_NE(fixed_lim, explicit_utc);
704 const time_zone fixed_ovfl =
705 fixed_time_zone(chrono::hours(24) + chrono::seconds(1));
706 EXPECT_EQ(fixed_ovfl, LoadZone(fixed_ovfl.name()));
707 EXPECT_EQ(fixed_ovfl, explicit_utc);
708
709 EXPECT_EQ(fixed_time_zone(chrono::seconds(1)),
710 fixed_time_zone(chrono::seconds(1)));
711
712 const time_zone local = local_time_zone();
713 EXPECT_EQ(local, LoadZone(local.name()));
714
715 time_zone la = LoadZone("America/Los_Angeles");
716 time_zone nyc = LoadZone("America/New_York");
717 EXPECT_NE(la, nyc);
718 }
719
TEST(StdChronoTimePoint,TimeTAlignment)720 TEST(StdChronoTimePoint, TimeTAlignment) {
721 // Ensures that the Unix epoch and the system clock epoch are an integral
722 // number of seconds apart. This simplifies conversions to/from time_t.
723 auto diff =
724 chrono::system_clock::time_point() - chrono::system_clock::from_time_t(0);
725 EXPECT_EQ(chrono::system_clock::time_point::duration::zero(),
726 diff % chrono::seconds(1));
727 }
728
TEST(BreakTime,TimePointResolution)729 TEST(BreakTime, TimePointResolution) {
730 const time_zone utc = utc_time_zone();
731 const auto t0 = chrono::system_clock::from_time_t(0);
732
733 ExpectTime(chrono::time_point_cast<chrono::nanoseconds>(t0), utc, 1970, 1, 1,
734 0, 0, 0, 0, false, "UTC");
735 ExpectTime(chrono::time_point_cast<chrono::microseconds>(t0), utc, 1970, 1, 1,
736 0, 0, 0, 0, false, "UTC");
737 ExpectTime(chrono::time_point_cast<chrono::milliseconds>(t0), utc, 1970, 1, 1,
738 0, 0, 0, 0, false, "UTC");
739 ExpectTime(chrono::time_point_cast<chrono::seconds>(t0), utc, 1970, 1, 1, 0,
740 0, 0, 0, false, "UTC");
741 ExpectTime(chrono::time_point_cast<absl::time_internal::cctz::seconds>(t0),
742 utc, 1970, 1, 1, 0, 0, 0, 0, false, "UTC");
743 ExpectTime(chrono::time_point_cast<chrono::minutes>(t0), utc, 1970, 1, 1, 0,
744 0, 0, 0, false, "UTC");
745 ExpectTime(chrono::time_point_cast<chrono::hours>(t0), utc, 1970, 1, 1, 0, 0,
746 0, 0, false, "UTC");
747 }
748
TEST(BreakTime,LocalTimeInUTC)749 TEST(BreakTime, LocalTimeInUTC) {
750 const time_zone tz = utc_time_zone();
751 const auto tp = chrono::system_clock::from_time_t(0);
752 ExpectTime(tp, tz, 1970, 1, 1, 0, 0, 0, 0, false, "UTC");
753 EXPECT_EQ(weekday::thursday, get_weekday(convert(tp, tz)));
754 }
755
TEST(BreakTime,LocalTimeInUTCUnaligned)756 TEST(BreakTime, LocalTimeInUTCUnaligned) {
757 const time_zone tz = utc_time_zone();
758 const auto tp =
759 chrono::system_clock::from_time_t(0) - chrono::milliseconds(500);
760 ExpectTime(tp, tz, 1969, 12, 31, 23, 59, 59, 0, false, "UTC");
761 EXPECT_EQ(weekday::wednesday, get_weekday(convert(tp, tz)));
762 }
763
TEST(BreakTime,LocalTimePosix)764 TEST(BreakTime, LocalTimePosix) {
765 // See IEEE Std 1003.1-1988 B.2.3 General Terms, Epoch.
766 const time_zone tz = utc_time_zone();
767 const auto tp = chrono::system_clock::from_time_t(536457599);
768 ExpectTime(tp, tz, 1986, 12, 31, 23, 59, 59, 0, false, "UTC");
769 EXPECT_EQ(weekday::wednesday, get_weekday(convert(tp, tz)));
770 }
771
TEST(TimeZoneImpl,LocalTimeInFixed)772 TEST(TimeZoneImpl, LocalTimeInFixed) {
773 const absl::time_internal::cctz::seconds offset =
774 -(chrono::hours(8) + chrono::minutes(33) + chrono::seconds(47));
775 const time_zone tz = fixed_time_zone(offset);
776 const auto tp = chrono::system_clock::from_time_t(0);
777 ExpectTime(tp, tz, 1969, 12, 31, 15, 26, 13, offset.count(), false,
778 "-083347");
779 EXPECT_EQ(weekday::wednesday, get_weekday(convert(tp, tz)));
780 }
781
TEST(BreakTime,LocalTimeInNewYork)782 TEST(BreakTime, LocalTimeInNewYork) {
783 const time_zone tz = LoadZone("America/New_York");
784 const auto tp = chrono::system_clock::from_time_t(45);
785 ExpectTime(tp, tz, 1969, 12, 31, 19, 0, 45, -5 * 60 * 60, false, "EST");
786 EXPECT_EQ(weekday::wednesday, get_weekday(convert(tp, tz)));
787 }
788
TEST(BreakTime,LocalTimeInMTV)789 TEST(BreakTime, LocalTimeInMTV) {
790 const time_zone tz = LoadZone("America/Los_Angeles");
791 const auto tp = chrono::system_clock::from_time_t(1380855729);
792 ExpectTime(tp, tz, 2013, 10, 3, 20, 2, 9, -7 * 60 * 60, true, "PDT");
793 EXPECT_EQ(weekday::thursday, get_weekday(convert(tp, tz)));
794 }
795
TEST(BreakTime,LocalTimeInSydney)796 TEST(BreakTime, LocalTimeInSydney) {
797 const time_zone tz = LoadZone("Australia/Sydney");
798 const auto tp = chrono::system_clock::from_time_t(90);
799 ExpectTime(tp, tz, 1970, 1, 1, 10, 1, 30, 10 * 60 * 60, false, "AEST");
800 EXPECT_EQ(weekday::thursday, get_weekday(convert(tp, tz)));
801 }
802
TEST(MakeTime,TimePointResolution)803 TEST(MakeTime, TimePointResolution) {
804 const time_zone utc = utc_time_zone();
805 const time_point<chrono::nanoseconds> tp_ns =
806 convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
807 EXPECT_EQ("04:05", absl::time_internal::cctz::format("%M:%E*S", tp_ns, utc));
808 const time_point<chrono::microseconds> tp_us =
809 convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
810 EXPECT_EQ("04:05", absl::time_internal::cctz::format("%M:%E*S", tp_us, utc));
811 const time_point<chrono::milliseconds> tp_ms =
812 convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
813 EXPECT_EQ("04:05", absl::time_internal::cctz::format("%M:%E*S", tp_ms, utc));
814 const time_point<chrono::seconds> tp_s =
815 convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
816 EXPECT_EQ("04:05", absl::time_internal::cctz::format("%M:%E*S", tp_s, utc));
817 const time_point<absl::time_internal::cctz::seconds> tp_s64 =
818 convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
819 EXPECT_EQ("04:05", absl::time_internal::cctz::format("%M:%E*S", tp_s64, utc));
820
821 // These next two require chrono::time_point_cast because the conversion
822 // from a resolution of seconds (the return value of convert()) to a
823 // coarser resolution requires an explicit cast.
824 const time_point<chrono::minutes> tp_m =
825 chrono::time_point_cast<chrono::minutes>(
826 convert(civil_second(2015, 1, 2, 3, 4, 5), utc));
827 EXPECT_EQ("04:00", absl::time_internal::cctz::format("%M:%E*S", tp_m, utc));
828 const time_point<chrono::hours> tp_h = chrono::time_point_cast<chrono::hours>(
829 convert(civil_second(2015, 1, 2, 3, 4, 5), utc));
830 EXPECT_EQ("00:00", absl::time_internal::cctz::format("%M:%E*S", tp_h, utc));
831 }
832
TEST(MakeTime,Normalization)833 TEST(MakeTime, Normalization) {
834 const time_zone tz = LoadZone("America/New_York");
835 const auto tp = convert(civil_second(2009, 2, 13, 18, 31, 30), tz);
836 EXPECT_EQ(chrono::system_clock::from_time_t(1234567890), tp);
837
838 // Now requests for the same time_point but with out-of-range fields.
839 EXPECT_EQ(tp, convert(civil_second(2008, 14, 13, 18, 31, 30), tz)); // month
840 EXPECT_EQ(tp, convert(civil_second(2009, 1, 44, 18, 31, 30), tz)); // day
841 EXPECT_EQ(tp, convert(civil_second(2009, 2, 12, 42, 31, 30), tz)); // hour
842 EXPECT_EQ(tp, convert(civil_second(2009, 2, 13, 17, 91, 30), tz)); // minute
843 EXPECT_EQ(tp, convert(civil_second(2009, 2, 13, 18, 30, 90), tz)); // second
844 }
845
846 // NOTE: Run this with -ftrapv to detect overflow problems.
TEST(MakeTime,SysSecondsLimits)847 TEST(MakeTime, SysSecondsLimits) {
848 const char RFC3339[] = "%Y-%m-%d%ET%H:%M:%S%Ez";
849 const time_zone utc = utc_time_zone();
850 const time_zone east = fixed_time_zone(chrono::hours(14));
851 const time_zone west = fixed_time_zone(-chrono::hours(14));
852 time_point<absl::time_internal::cctz::seconds> tp;
853
854 // Approach the maximal time_point<cctz::seconds> value from below.
855 tp = convert(civil_second(292277026596, 12, 4, 15, 30, 6), utc);
856 EXPECT_EQ("292277026596-12-04T15:30:06+00:00",
857 absl::time_internal::cctz::format(RFC3339, tp, utc));
858 tp = convert(civil_second(292277026596, 12, 4, 15, 30, 7), utc);
859 EXPECT_EQ("292277026596-12-04T15:30:07+00:00",
860 absl::time_internal::cctz::format(RFC3339, tp, utc));
861 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
862 tp = convert(civil_second(292277026596, 12, 4, 15, 30, 8), utc);
863 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
864 tp = convert(civil_second::max(), utc);
865 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
866
867 // Checks that we can also get the maximal value for a far-east zone.
868 tp = convert(civil_second(292277026596, 12, 5, 5, 30, 7), east);
869 EXPECT_EQ("292277026596-12-05T05:30:07+14:00",
870 absl::time_internal::cctz::format(RFC3339, tp, east));
871 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
872 tp = convert(civil_second(292277026596, 12, 5, 5, 30, 8), east);
873 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
874 tp = convert(civil_second::max(), east);
875 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
876
877 // Checks that we can also get the maximal value for a far-west zone.
878 tp = convert(civil_second(292277026596, 12, 4, 1, 30, 7), west);
879 EXPECT_EQ("292277026596-12-04T01:30:07-14:00",
880 absl::time_internal::cctz::format(RFC3339, tp, west));
881 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
882 tp = convert(civil_second(292277026596, 12, 4, 7, 30, 8), west);
883 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
884 tp = convert(civil_second::max(), west);
885 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
886
887 // Approach the minimal time_point<cctz::seconds> value from above.
888 tp = convert(civil_second(-292277022657, 1, 27, 8, 29, 53), utc);
889 EXPECT_EQ("-292277022657-01-27T08:29:53+00:00",
890 absl::time_internal::cctz::format(RFC3339, tp, utc));
891 tp = convert(civil_second(-292277022657, 1, 27, 8, 29, 52), utc);
892 EXPECT_EQ("-292277022657-01-27T08:29:52+00:00",
893 absl::time_internal::cctz::format(RFC3339, tp, utc));
894 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
895 tp = convert(civil_second(-292277022657, 1, 27, 8, 29, 51), utc);
896 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
897 tp = convert(civil_second::min(), utc);
898 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
899
900 // Checks that we can also get the minimal value for a far-east zone.
901 tp = convert(civil_second(-292277022657, 1, 27, 22, 29, 52), east);
902 EXPECT_EQ("-292277022657-01-27T22:29:52+14:00",
903 absl::time_internal::cctz::format(RFC3339, tp, east));
904 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
905 tp = convert(civil_second(-292277022657, 1, 27, 22, 29, 51), east);
906 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
907 tp = convert(civil_second::min(), east);
908 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
909
910 // Checks that we can also get the minimal value for a far-west zone.
911 tp = convert(civil_second(-292277022657, 1, 26, 18, 29, 52), west);
912 EXPECT_EQ("-292277022657-01-26T18:29:52-14:00",
913 absl::time_internal::cctz::format(RFC3339, tp, west));
914 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
915 tp = convert(civil_second(-292277022657, 1, 26, 18, 29, 51), west);
916 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
917 tp = convert(civil_second::min(), west);
918 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
919
920 // Some similar checks for the "libc" time-zone implementation.
921 if (sizeof(std::time_t) >= 8) {
922 // Checks that "tm_year + 1900", as used by the "libc" implementation,
923 // can produce year values beyond the range on an int without overflow.
924 #if defined(_WIN32) || defined(_WIN64)
925 // localtime_s() and gmtime_s() don't believe in years outside [1970:3000].
926 #else
927 const time_zone cut = LoadZone("libc:UTC");
928 const year_t max_tm_year = year_t{std::numeric_limits<int>::max()} + 1900;
929 tp = convert(civil_second(max_tm_year, 12, 31, 23, 59, 59), cut);
930 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMSCRIPTEN__)
931 // Some gmtime_r() impls fail on extreme positive values.
932 #else
933 EXPECT_EQ("2147485547-12-31T23:59:59+00:00",
934 absl::time_internal::cctz::format(RFC3339, tp, cut));
935 #endif
936 const year_t min_tm_year = year_t{std::numeric_limits<int>::min()} + 1900;
937 tp = convert(civil_second(min_tm_year, 1, 1, 0, 0, 0), cut);
938 #if defined(__Fuchsia__) || defined(__EMSCRIPTEN__)
939 // Some gmtime_r() impls fail on extreme negative values (fxbug.dev/78527).
940 #else
941 EXPECT_EQ("-2147481748-01-01T00:00:00+00:00",
942 absl::time_internal::cctz::format(RFC3339, tp, cut));
943 #endif
944 #endif
945 }
946 }
947
TEST(MakeTime,LocalTimeLibC)948 TEST(MakeTime, LocalTimeLibC) {
949 // Checks that cctz and libc agree on transition points in [1970:2037].
950 //
951 // We limit this test case to environments where:
952 // 1) we know how to change the time zone used by localtime()/mktime(),
953 // 2) cctz and localtime()/mktime() will use similar-enough tzdata, and
954 // 3) we have some idea about how mktime() behaves during transitions.
955 #if defined(__linux__) && defined(__GLIBC__) && !defined(__ANDROID__)
956 const char* const ep = getenv("TZ");
957 std::string tz_name = (ep != nullptr) ? ep : "";
958 for (const char* const* np = kTimeZoneNames; *np != nullptr; ++np) {
959 ASSERT_EQ(0, setenv("TZ", *np, 1)); // change what "localtime" means
960 const auto zi = local_time_zone();
961 const auto lc = LoadZone("libc:localtime");
962 time_zone::civil_transition transition;
963 for (auto tp = zi.lookup(civil_second()).trans;
964 zi.next_transition(tp, &transition);
965 tp = zi.lookup(transition.to).trans) {
966 const auto fcl = zi.lookup(transition.from);
967 const auto tcl = zi.lookup(transition.to);
968 civil_second cs, us; // compare cs and us in zi and lc
969 if (fcl.kind == time_zone::civil_lookup::UNIQUE) {
970 if (tcl.kind == time_zone::civil_lookup::UNIQUE) {
971 // Both unique; must be an is_dst or abbr change.
972 ASSERT_EQ(transition.from, transition.to);
973 const auto trans = fcl.trans;
974 const auto tal = zi.lookup(trans);
975 const auto tprev = trans - absl::time_internal::cctz::seconds(1);
976 const auto pal = zi.lookup(tprev);
977 if (pal.is_dst == tal.is_dst) {
978 ASSERT_STRNE(pal.abbr, tal.abbr);
979 }
980 continue;
981 }
982 ASSERT_EQ(time_zone::civil_lookup::REPEATED, tcl.kind);
983 cs = transition.to;
984 us = transition.from;
985 } else {
986 ASSERT_EQ(time_zone::civil_lookup::UNIQUE, tcl.kind);
987 ASSERT_EQ(time_zone::civil_lookup::SKIPPED, fcl.kind);
988 cs = transition.from;
989 us = transition.to;
990 }
991 if (us.year() > 2037) break; // limit test time (and to 32-bit time_t)
992 const auto cl_zi = zi.lookup(cs);
993 if (zi.lookup(cl_zi.pre).is_dst == zi.lookup(cl_zi.post).is_dst) {
994 // The "libc" implementation cannot correctly classify transitions
995 // that don't change the "tm_isdst" flag. In Europe/Volgograd, for
996 // example, there is a SKIPPED transition from +03 to +04 with dst=F
997 // on both sides ...
998 // 1540681199 = 2018-10-28 01:59:59 +03:00:00 [dst=F off=10800]
999 // 1540681200 = 2018-10-28 03:00:00 +04:00:00 [dst=F off=14400]
1000 // but std::mktime(2018-10-28 02:00:00, tm_isdst=0) fails, unlike,
1001 // say, the similar Europe/Chisinau transition from +02 to +03 ...
1002 // 1521935999 = 2018-03-25 01:59:59 +02:00:00 [dst=F off=7200]
1003 // 1521936000 = 2018-03-25 03:00:00 +03:00:00 [dst=T off=10800]
1004 // where std::mktime(2018-03-25 02:00:00, tm_isdst=0) succeeds and
1005 // returns 1521936000.
1006 continue;
1007 }
1008 if (cs == civil_second(2037, 10, 4, 2, 0, 0)) {
1009 const std::string tzname = *np;
1010 if (tzname == "Africa/Casablanca" || tzname == "Africa/El_Aaiun") {
1011 // The "libc" implementation gets this transition wrong (at least
1012 // until 2018g when it was removed), returning an offset of 3600
1013 // instead of 0. TODO: Revert this when 2018g is ubiquitous.
1014 continue;
1015 }
1016 }
1017 const auto cl_lc = lc.lookup(cs);
1018 SCOPED_TRACE(testing::Message() << "For " << cs << " in " << *np);
1019 EXPECT_EQ(cl_zi.kind, cl_lc.kind);
1020 EXPECT_EQ(cl_zi.pre, cl_lc.pre);
1021 EXPECT_EQ(cl_zi.trans, cl_lc.trans);
1022 EXPECT_EQ(cl_zi.post, cl_lc.post);
1023 const auto ucl_zi = zi.lookup(us);
1024 const auto ucl_lc = lc.lookup(us);
1025 SCOPED_TRACE(testing::Message() << "For " << us << " in " << *np);
1026 EXPECT_EQ(ucl_zi.kind, ucl_lc.kind);
1027 EXPECT_EQ(ucl_zi.pre, ucl_lc.pre);
1028 EXPECT_EQ(ucl_zi.trans, ucl_lc.trans);
1029 EXPECT_EQ(ucl_zi.post, ucl_lc.post);
1030 }
1031 }
1032 if (ep == nullptr) {
1033 ASSERT_EQ(0, unsetenv("TZ"));
1034 } else {
1035 ASSERT_EQ(0, setenv("TZ", tz_name.c_str(), 1));
1036 }
1037 #endif
1038 }
1039
TEST(NextTransition,UTC)1040 TEST(NextTransition, UTC) {
1041 const auto tz = utc_time_zone();
1042 time_zone::civil_transition trans;
1043
1044 auto tp = time_point<absl::time_internal::cctz::seconds>::min();
1045 EXPECT_FALSE(tz.next_transition(tp, &trans));
1046
1047 tp = time_point<absl::time_internal::cctz::seconds>::max();
1048 EXPECT_FALSE(tz.next_transition(tp, &trans));
1049 }
1050
TEST(PrevTransition,UTC)1051 TEST(PrevTransition, UTC) {
1052 const auto tz = utc_time_zone();
1053 time_zone::civil_transition trans;
1054
1055 auto tp = time_point<absl::time_internal::cctz::seconds>::max();
1056 EXPECT_FALSE(tz.prev_transition(tp, &trans));
1057
1058 tp = time_point<absl::time_internal::cctz::seconds>::min();
1059 EXPECT_FALSE(tz.prev_transition(tp, &trans));
1060 }
1061
TEST(NextTransition,AmericaNewYork)1062 TEST(NextTransition, AmericaNewYork) {
1063 const auto tz = LoadZone("America/New_York");
1064 time_zone::civil_transition trans;
1065
1066 auto tp = convert(civil_second(2018, 6, 30, 0, 0, 0), tz);
1067 EXPECT_TRUE(tz.next_transition(tp, &trans));
1068 EXPECT_EQ(civil_second(2018, 11, 4, 2, 0, 0), trans.from);
1069 EXPECT_EQ(civil_second(2018, 11, 4, 1, 0, 0), trans.to);
1070
1071 tp = time_point<absl::time_internal::cctz::seconds>::max();
1072 EXPECT_FALSE(tz.next_transition(tp, &trans));
1073
1074 tp = time_point<absl::time_internal::cctz::seconds>::min();
1075 EXPECT_TRUE(tz.next_transition(tp, &trans));
1076 if (trans.from == civil_second(1918, 3, 31, 2, 0, 0)) {
1077 // It looks like the tzdata is only 32 bit (probably macOS),
1078 // which bottoms out at 1901-12-13T20:45:52+00:00.
1079 EXPECT_EQ(civil_second(1918, 3, 31, 3, 0, 0), trans.to);
1080 } else {
1081 EXPECT_EQ(civil_second(1883, 11, 18, 12, 3, 58), trans.from);
1082 EXPECT_EQ(civil_second(1883, 11, 18, 12, 0, 0), trans.to);
1083 }
1084 }
1085
TEST(PrevTransition,AmericaNewYork)1086 TEST(PrevTransition, AmericaNewYork) {
1087 const auto tz = LoadZone("America/New_York");
1088 time_zone::civil_transition trans;
1089
1090 auto tp = convert(civil_second(2018, 6, 30, 0, 0, 0), tz);
1091 EXPECT_TRUE(tz.prev_transition(tp, &trans));
1092 EXPECT_EQ(civil_second(2018, 3, 11, 2, 0, 0), trans.from);
1093 EXPECT_EQ(civil_second(2018, 3, 11, 3, 0, 0), trans.to);
1094
1095 tp = time_point<absl::time_internal::cctz::seconds>::min();
1096 EXPECT_FALSE(tz.prev_transition(tp, &trans));
1097
1098 tp = time_point<absl::time_internal::cctz::seconds>::max();
1099 EXPECT_TRUE(tz.prev_transition(tp, &trans));
1100 // We have a transition but we don't know which one.
1101 }
1102
TEST(NextTransition,Scan)1103 TEST(NextTransition, Scan) {
1104 for (const char* const* np = kTimeZoneNames; *np != nullptr; ++np) {
1105 SCOPED_TRACE(testing::Message() << "In " << *np);
1106 time_zone tz;
1107 // EXPECT_TRUE(load_time_zone(*np, &tz));
1108 if (!load_time_zone(*np, &tz)) {
1109 continue; // tolerate kTimeZoneNames/zoneinfo skew
1110 }
1111
1112 auto tp = time_point<absl::time_internal::cctz::seconds>::min();
1113 time_zone::civil_transition trans;
1114 while (tz.next_transition(tp, &trans)) {
1115 time_zone::civil_lookup from_cl = tz.lookup(trans.from);
1116 EXPECT_NE(from_cl.kind, time_zone::civil_lookup::REPEATED);
1117 time_zone::civil_lookup to_cl = tz.lookup(trans.to);
1118 EXPECT_NE(to_cl.kind, time_zone::civil_lookup::SKIPPED);
1119
1120 auto trans_tp = to_cl.trans;
1121 time_zone::absolute_lookup trans_al = tz.lookup(trans_tp);
1122 EXPECT_EQ(trans_al.cs, trans.to);
1123 auto pre_trans_tp = trans_tp - absl::time_internal::cctz::seconds(1);
1124 time_zone::absolute_lookup pre_trans_al = tz.lookup(pre_trans_tp);
1125 EXPECT_EQ(pre_trans_al.cs + 1, trans.from);
1126
1127 auto offset_delta = trans_al.offset - pre_trans_al.offset;
1128 EXPECT_EQ(offset_delta, trans.to - trans.from);
1129 if (offset_delta == 0) {
1130 // This "transition" is only an is_dst or abbr change.
1131 EXPECT_EQ(to_cl.kind, time_zone::civil_lookup::UNIQUE);
1132 if (trans_al.is_dst == pre_trans_al.is_dst) {
1133 EXPECT_STRNE(trans_al.abbr, pre_trans_al.abbr);
1134 }
1135 }
1136
1137 tp = trans_tp; // continue scan from transition
1138 }
1139 }
1140 }
1141
TEST(TimeZoneEdgeCase,AmericaNewYork)1142 TEST(TimeZoneEdgeCase, AmericaNewYork) {
1143 const time_zone tz = LoadZone("America/New_York");
1144
1145 // Spring 1:59:59 -> 3:00:00
1146 auto tp = convert(civil_second(2013, 3, 10, 1, 59, 59), tz);
1147 ExpectTime(tp, tz, 2013, 3, 10, 1, 59, 59, -5 * 3600, false, "EST");
1148 tp += absl::time_internal::cctz::seconds(1);
1149 ExpectTime(tp, tz, 2013, 3, 10, 3, 0, 0, -4 * 3600, true, "EDT");
1150
1151 // Fall 1:59:59 -> 1:00:00
1152 tp = convert(civil_second(2013, 11, 3, 1, 59, 59), tz);
1153 ExpectTime(tp, tz, 2013, 11, 3, 1, 59, 59, -4 * 3600, true, "EDT");
1154 tp += absl::time_internal::cctz::seconds(1);
1155 ExpectTime(tp, tz, 2013, 11, 3, 1, 0, 0, -5 * 3600, false, "EST");
1156 }
1157
TEST(TimeZoneEdgeCase,AmericaLosAngeles)1158 TEST(TimeZoneEdgeCase, AmericaLosAngeles) {
1159 const time_zone tz = LoadZone("America/Los_Angeles");
1160
1161 // Spring 1:59:59 -> 3:00:00
1162 auto tp = convert(civil_second(2013, 3, 10, 1, 59, 59), tz);
1163 ExpectTime(tp, tz, 2013, 3, 10, 1, 59, 59, -8 * 3600, false, "PST");
1164 tp += absl::time_internal::cctz::seconds(1);
1165 ExpectTime(tp, tz, 2013, 3, 10, 3, 0, 0, -7 * 3600, true, "PDT");
1166
1167 // Fall 1:59:59 -> 1:00:00
1168 tp = convert(civil_second(2013, 11, 3, 1, 59, 59), tz);
1169 ExpectTime(tp, tz, 2013, 11, 3, 1, 59, 59, -7 * 3600, true, "PDT");
1170 tp += absl::time_internal::cctz::seconds(1);
1171 ExpectTime(tp, tz, 2013, 11, 3, 1, 0, 0, -8 * 3600, false, "PST");
1172 }
1173
TEST(TimeZoneEdgeCase,ArizonaNoTransition)1174 TEST(TimeZoneEdgeCase, ArizonaNoTransition) {
1175 const time_zone tz = LoadZone("America/Phoenix");
1176
1177 // No transition in Spring.
1178 auto tp = convert(civil_second(2013, 3, 10, 1, 59, 59), tz);
1179 ExpectTime(tp, tz, 2013, 3, 10, 1, 59, 59, -7 * 3600, false, "MST");
1180 tp += absl::time_internal::cctz::seconds(1);
1181 ExpectTime(tp, tz, 2013, 3, 10, 2, 0, 0, -7 * 3600, false, "MST");
1182
1183 // No transition in Fall.
1184 tp = convert(civil_second(2013, 11, 3, 1, 59, 59), tz);
1185 ExpectTime(tp, tz, 2013, 11, 3, 1, 59, 59, -7 * 3600, false, "MST");
1186 tp += absl::time_internal::cctz::seconds(1);
1187 ExpectTime(tp, tz, 2013, 11, 3, 2, 0, 0, -7 * 3600, false, "MST");
1188 }
1189
TEST(TimeZoneEdgeCase,AsiaKathmandu)1190 TEST(TimeZoneEdgeCase, AsiaKathmandu) {
1191 const time_zone tz = LoadZone("Asia/Kathmandu");
1192
1193 // A non-DST offset change from +0530 to +0545
1194 //
1195 // 504901799 == Tue, 31 Dec 1985 23:59:59 +0530 (+0530)
1196 // 504901800 == Wed, 1 Jan 1986 00:15:00 +0545 (+0545)
1197 auto tp = convert(civil_second(1985, 12, 31, 23, 59, 59), tz);
1198 ExpectTime(tp, tz, 1985, 12, 31, 23, 59, 59, 5.5 * 3600, false, "+0530");
1199 tp += absl::time_internal::cctz::seconds(1);
1200 ExpectTime(tp, tz, 1986, 1, 1, 0, 15, 0, 5.75 * 3600, false, "+0545");
1201 }
1202
TEST(TimeZoneEdgeCase,PacificChatham)1203 TEST(TimeZoneEdgeCase, PacificChatham) {
1204 const time_zone tz = LoadZone("Pacific/Chatham");
1205
1206 // One-hour DST offset changes, but at atypical values
1207 //
1208 // 1365256799 == Sun, 7 Apr 2013 03:44:59 +1345 (+1345)
1209 // 1365256800 == Sun, 7 Apr 2013 02:45:00 +1245 (+1245)
1210 auto tp = convert(civil_second(2013, 4, 7, 3, 44, 59), tz);
1211 ExpectTime(tp, tz, 2013, 4, 7, 3, 44, 59, 13.75 * 3600, true, "+1345");
1212 tp += absl::time_internal::cctz::seconds(1);
1213 ExpectTime(tp, tz, 2013, 4, 7, 2, 45, 0, 12.75 * 3600, false, "+1245");
1214
1215 // 1380376799 == Sun, 29 Sep 2013 02:44:59 +1245 (+1245)
1216 // 1380376800 == Sun, 29 Sep 2013 03:45:00 +1345 (+1345)
1217 tp = convert(civil_second(2013, 9, 29, 2, 44, 59), tz);
1218 ExpectTime(tp, tz, 2013, 9, 29, 2, 44, 59, 12.75 * 3600, false, "+1245");
1219 tp += absl::time_internal::cctz::seconds(1);
1220 ExpectTime(tp, tz, 2013, 9, 29, 3, 45, 0, 13.75 * 3600, true, "+1345");
1221 }
1222
TEST(TimeZoneEdgeCase,AustraliaLordHowe)1223 TEST(TimeZoneEdgeCase, AustraliaLordHowe) {
1224 const time_zone tz = LoadZone("Australia/Lord_Howe");
1225
1226 // Half-hour DST offset changes
1227 //
1228 // 1365260399 == Sun, 7 Apr 2013 01:59:59 +1100 (+11)
1229 // 1365260400 == Sun, 7 Apr 2013 01:30:00 +1030 (+1030)
1230 auto tp = convert(civil_second(2013, 4, 7, 1, 59, 59), tz);
1231 ExpectTime(tp, tz, 2013, 4, 7, 1, 59, 59, 11 * 3600, true, "+11");
1232 tp += absl::time_internal::cctz::seconds(1);
1233 ExpectTime(tp, tz, 2013, 4, 7, 1, 30, 0, 10.5 * 3600, false, "+1030");
1234
1235 // 1380986999 == Sun, 6 Oct 2013 01:59:59 +1030 (+1030)
1236 // 1380987000 == Sun, 6 Oct 2013 02:30:00 +1100 (+11)
1237 tp = convert(civil_second(2013, 10, 6, 1, 59, 59), tz);
1238 ExpectTime(tp, tz, 2013, 10, 6, 1, 59, 59, 10.5 * 3600, false, "+1030");
1239 tp += absl::time_internal::cctz::seconds(1);
1240 ExpectTime(tp, tz, 2013, 10, 6, 2, 30, 0, 11 * 3600, true, "+11");
1241 }
1242
TEST(TimeZoneEdgeCase,PacificApia)1243 TEST(TimeZoneEdgeCase, PacificApia) {
1244 const time_zone tz = LoadZone("Pacific/Apia");
1245
1246 // At the end of December 2011, Samoa jumped forward by one day,
1247 // skipping 30 December from the local calendar, when the nation
1248 // moved to the west of the International Date Line.
1249 //
1250 // A one-day, non-DST offset change
1251 //
1252 // 1325239199 == Thu, 29 Dec 2011 23:59:59 -1000 (-10)
1253 // 1325239200 == Sat, 31 Dec 2011 00:00:00 +1400 (+14)
1254 auto tp = convert(civil_second(2011, 12, 29, 23, 59, 59), tz);
1255 ExpectTime(tp, tz, 2011, 12, 29, 23, 59, 59, -10 * 3600, true, "-10");
1256 EXPECT_EQ(363, get_yearday(convert(tp, tz)));
1257 tp += absl::time_internal::cctz::seconds(1);
1258 ExpectTime(tp, tz, 2011, 12, 31, 0, 0, 0, 14 * 3600, true, "+14");
1259 EXPECT_EQ(365, get_yearday(convert(tp, tz)));
1260 }
1261
TEST(TimeZoneEdgeCase,AfricaCairo)1262 TEST(TimeZoneEdgeCase, AfricaCairo) {
1263 const time_zone tz = LoadZone("Africa/Cairo");
1264
1265 if (VersionCmp(tz, "2014c") >= 0) {
1266 // An interesting case of midnight not existing.
1267 //
1268 // 1400191199 == Thu, 15 May 2014 23:59:59 +0200 (EET)
1269 // 1400191200 == Fri, 16 May 2014 01:00:00 +0300 (EEST)
1270 auto tp = convert(civil_second(2014, 5, 15, 23, 59, 59), tz);
1271 ExpectTime(tp, tz, 2014, 5, 15, 23, 59, 59, 2 * 3600, false, "EET");
1272 tp += absl::time_internal::cctz::seconds(1);
1273 ExpectTime(tp, tz, 2014, 5, 16, 1, 0, 0, 3 * 3600, true, "EEST");
1274 }
1275 }
1276
TEST(TimeZoneEdgeCase,AfricaMonrovia)1277 TEST(TimeZoneEdgeCase, AfricaMonrovia) {
1278 const time_zone tz = LoadZone("Africa/Monrovia");
1279
1280 if (VersionCmp(tz, "2017b") >= 0) {
1281 // Strange offset change -00:44:30 -> +00:00:00 (non-DST)
1282 //
1283 // 63593069 == Thu, 6 Jan 1972 23:59:59 -0044 (MMT)
1284 // 63593070 == Fri, 7 Jan 1972 00:44:30 +0000 (GMT)
1285 auto tp = convert(civil_second(1972, 1, 6, 23, 59, 59), tz);
1286 ExpectTime(tp, tz, 1972, 1, 6, 23, 59, 59, -44.5 * 60, false, "MMT");
1287 tp += absl::time_internal::cctz::seconds(1);
1288 ExpectTime(tp, tz, 1972, 1, 7, 0, 44, 30, 0 * 60, false, "GMT");
1289 }
1290 }
1291
TEST(TimeZoneEdgeCase,AmericaJamaica)1292 TEST(TimeZoneEdgeCase, AmericaJamaica) {
1293 // Jamaica discontinued DST transitions in 1983, and is now at a
1294 // constant -0500. This makes it an interesting edge-case target.
1295 // Note that the 32-bit times used in a (tzh_version == 0) zoneinfo
1296 // file cannot represent the abbreviation-only transition of 1890,
1297 // so we ignore the abbreviation by expecting what we received.
1298 const time_zone tz = LoadZone("America/Jamaica");
1299
1300 // Before the first transition.
1301 if (!tz.version().empty() && VersionCmp(tz, "2018d") >= 0) {
1302 // We avoid the expectations on the -18430 offset below unless we are
1303 // certain we have commit 907241e (Fix off-by-1 error for Jamaica and
1304 // T&C before 1913) from 2018d. TODO: Remove the "version() not empty"
1305 // part when 2018d is generally available from /usr/share/zoneinfo.
1306 auto tp = convert(civil_second(1889, 12, 31, 0, 0, 0), tz);
1307 ExpectTime(tp, tz, 1889, 12, 31, 0, 0, 0, -18430, false,
1308 tz.lookup(tp).abbr);
1309
1310 // Over the first (abbreviation-change only) transition.
1311 // -2524503170 == Tue, 31 Dec 1889 23:59:59 -0507 (LMT)
1312 // -2524503169 == Wed, 1 Jan 1890 00:00:00 -0507 (KMT)
1313 tp = convert(civil_second(1889, 12, 31, 23, 59, 59), tz);
1314 ExpectTime(tp, tz, 1889, 12, 31, 23, 59, 59, -18430, false,
1315 tz.lookup(tp).abbr);
1316 tp += absl::time_internal::cctz::seconds(1);
1317 ExpectTime(tp, tz, 1890, 1, 1, 0, 0, 0, -18430, false, "KMT");
1318 }
1319
1320 // Over the last (DST) transition.
1321 // 436341599 == Sun, 30 Oct 1983 01:59:59 -0400 (EDT)
1322 // 436341600 == Sun, 30 Oct 1983 01:00:00 -0500 (EST)
1323 auto tp = convert(civil_second(1983, 10, 30, 1, 59, 59), tz);
1324 ExpectTime(tp, tz, 1983, 10, 30, 1, 59, 59, -4 * 3600, true, "EDT");
1325 tp += absl::time_internal::cctz::seconds(1);
1326 ExpectTime(tp, tz, 1983, 10, 30, 1, 0, 0, -5 * 3600, false, "EST");
1327
1328 // After the last transition.
1329 tp = convert(civil_second(1983, 12, 31, 23, 59, 59), tz);
1330 ExpectTime(tp, tz, 1983, 12, 31, 23, 59, 59, -5 * 3600, false, "EST");
1331 }
1332
TEST(TimeZoneEdgeCase,WET)1333 TEST(TimeZoneEdgeCase, WET) {
1334 // Cover some non-existent times within forward transitions.
1335 const time_zone tz = LoadZone("WET");
1336
1337 // Before the first transition.
1338 auto tp = convert(civil_second(1977, 1, 1, 0, 0, 0), tz);
1339 ExpectTime(tp, tz, 1977, 1, 1, 0, 0, 0, 0, false, "WET");
1340
1341 // Over the first transition.
1342 // 228877199 == Sun, 3 Apr 1977 00:59:59 +0000 (WET)
1343 // 228877200 == Sun, 3 Apr 1977 02:00:00 +0100 (WEST)
1344 tp = convert(civil_second(1977, 4, 3, 0, 59, 59), tz);
1345 ExpectTime(tp, tz, 1977, 4, 3, 0, 59, 59, 0, false, "WET");
1346 tp += absl::time_internal::cctz::seconds(1);
1347 ExpectTime(tp, tz, 1977, 4, 3, 2, 0, 0, 1 * 3600, true, "WEST");
1348
1349 // A non-existent time within the first transition.
1350 time_zone::civil_lookup cl1 = tz.lookup(civil_second(1977, 4, 3, 1, 15, 0));
1351 EXPECT_EQ(time_zone::civil_lookup::SKIPPED, cl1.kind);
1352 ExpectTime(cl1.pre, tz, 1977, 4, 3, 2, 15, 0, 1 * 3600, true, "WEST");
1353 ExpectTime(cl1.trans, tz, 1977, 4, 3, 2, 0, 0, 1 * 3600, true, "WEST");
1354 ExpectTime(cl1.post, tz, 1977, 4, 3, 0, 15, 0, 0 * 3600, false, "WET");
1355
1356 // A non-existent time within the second forward transition.
1357 time_zone::civil_lookup cl2 = tz.lookup(civil_second(1978, 4, 2, 1, 15, 0));
1358 EXPECT_EQ(time_zone::civil_lookup::SKIPPED, cl2.kind);
1359 ExpectTime(cl2.pre, tz, 1978, 4, 2, 2, 15, 0, 1 * 3600, true, "WEST");
1360 ExpectTime(cl2.trans, tz, 1978, 4, 2, 2, 0, 0, 1 * 3600, true, "WEST");
1361 ExpectTime(cl2.post, tz, 1978, 4, 2, 0, 15, 0, 0 * 3600, false, "WET");
1362 }
1363
TEST(TimeZoneEdgeCase,FixedOffsets)1364 TEST(TimeZoneEdgeCase, FixedOffsets) {
1365 const time_zone gmtm5 = LoadZone("Etc/GMT+5"); // -0500
1366 auto tp = convert(civil_second(1970, 1, 1, 0, 0, 0), gmtm5);
1367 ExpectTime(tp, gmtm5, 1970, 1, 1, 0, 0, 0, -5 * 3600, false, "-05");
1368 EXPECT_EQ(chrono::system_clock::from_time_t(5 * 3600), tp);
1369
1370 const time_zone gmtp5 = LoadZone("Etc/GMT-5"); // +0500
1371 tp = convert(civil_second(1970, 1, 1, 0, 0, 0), gmtp5);
1372 ExpectTime(tp, gmtp5, 1970, 1, 1, 0, 0, 0, 5 * 3600, false, "+05");
1373 EXPECT_EQ(chrono::system_clock::from_time_t(-5 * 3600), tp);
1374 }
1375
TEST(TimeZoneEdgeCase,NegativeYear)1376 TEST(TimeZoneEdgeCase, NegativeYear) {
1377 // Tests transition from year 0 (aka 1BCE) to year -1.
1378 const time_zone tz = utc_time_zone();
1379 auto tp = convert(civil_second(0, 1, 1, 0, 0, 0), tz);
1380 ExpectTime(tp, tz, 0, 1, 1, 0, 0, 0, 0 * 3600, false, "UTC");
1381 EXPECT_EQ(weekday::saturday, get_weekday(convert(tp, tz)));
1382 tp -= absl::time_internal::cctz::seconds(1);
1383 ExpectTime(tp, tz, -1, 12, 31, 23, 59, 59, 0 * 3600, false, "UTC");
1384 EXPECT_EQ(weekday::friday, get_weekday(convert(tp, tz)));
1385 }
1386
TEST(TimeZoneEdgeCase,UTC32bitLimit)1387 TEST(TimeZoneEdgeCase, UTC32bitLimit) {
1388 const time_zone tz = utc_time_zone();
1389
1390 // Limits of signed 32-bit time_t
1391 //
1392 // 2147483647 == Tue, 19 Jan 2038 03:14:07 +0000 (UTC)
1393 // 2147483648 == Tue, 19 Jan 2038 03:14:08 +0000 (UTC)
1394 auto tp = convert(civil_second(2038, 1, 19, 3, 14, 7), tz);
1395 ExpectTime(tp, tz, 2038, 1, 19, 3, 14, 7, 0 * 3600, false, "UTC");
1396 tp += absl::time_internal::cctz::seconds(1);
1397 ExpectTime(tp, tz, 2038, 1, 19, 3, 14, 8, 0 * 3600, false, "UTC");
1398 }
1399
TEST(TimeZoneEdgeCase,UTC5DigitYear)1400 TEST(TimeZoneEdgeCase, UTC5DigitYear) {
1401 const time_zone tz = utc_time_zone();
1402
1403 // Rollover to 5-digit year
1404 //
1405 // 253402300799 == Fri, 31 Dec 9999 23:59:59 +0000 (UTC)
1406 // 253402300800 == Sat, 1 Jan 1000 00:00:00 +0000 (UTC)
1407 auto tp = convert(civil_second(9999, 12, 31, 23, 59, 59), tz);
1408 ExpectTime(tp, tz, 9999, 12, 31, 23, 59, 59, 0 * 3600, false, "UTC");
1409 tp += absl::time_internal::cctz::seconds(1);
1410 ExpectTime(tp, tz, 10000, 1, 1, 0, 0, 0, 0 * 3600, false, "UTC");
1411 }
1412
1413 } // namespace cctz
1414 } // namespace time_internal
1415 ABSL_NAMESPACE_END
1416 } // namespace absl
1417