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