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/Kiritimati",
583 "Pacific/Kosrae",
584 "Pacific/Kwajalein",
585 "Pacific/Majuro",
586 "Pacific/Marquesas",
587 "Pacific/Midway",
588 "Pacific/Nauru",
589 "Pacific/Niue",
590 "Pacific/Norfolk",
591 "Pacific/Noumea",
592 "Pacific/Pago_Pago",
593 "Pacific/Palau",
594 "Pacific/Pitcairn",
595 "Pacific/Pohnpei",
596 "Pacific/Ponape",
597 "Pacific/Port_Moresby",
598 "Pacific/Rarotonga",
599 "Pacific/Saipan",
600 "Pacific/Samoa",
601 "Pacific/Tahiti",
602 "Pacific/Tarawa",
603 "Pacific/Tongatapu",
604 "Pacific/Truk",
605 "Pacific/Wake",
606 "Pacific/Wallis",
607 "Pacific/Yap",
608 "Poland",
609 "Portugal",
610 "ROC",
611 "ROK",
612 "Singapore",
613 "Turkey",
614 "UCT",
615 "US/Alaska",
616 "US/Aleutian",
617 "US/Arizona",
618 "US/Central",
619 "US/East-Indiana",
620 "US/Eastern",
621 "US/Hawaii",
622 "US/Indiana-Starke",
623 "US/Michigan",
624 "US/Mountain",
625 "US/Pacific",
626 "US/Samoa",
627 "UTC",
628 "Universal",
629 "W-SU",
630 "WET",
631 "Zulu",
632 nullptr};
633
634 // Helper to return a loaded time zone by value (UTC on error).
LoadZone(const std::string & name)635 time_zone LoadZone(const std::string& name) {
636 time_zone tz;
637 load_time_zone(name, &tz);
638 return tz;
639 }
640
641 // This helper is a macro so that failed expectations show up with the
642 // correct line numbers.
643 #define ExpectTime(tp, tz, y, m, d, hh, mm, ss, off, isdst, zone) \
644 do { \
645 time_zone::absolute_lookup al = tz.lookup(tp); \
646 EXPECT_EQ(y, al.cs.year()); \
647 EXPECT_EQ(m, al.cs.month()); \
648 EXPECT_EQ(d, al.cs.day()); \
649 EXPECT_EQ(hh, al.cs.hour()); \
650 EXPECT_EQ(mm, al.cs.minute()); \
651 EXPECT_EQ(ss, al.cs.second()); \
652 EXPECT_EQ(off, al.offset); \
653 EXPECT_TRUE(isdst == al.is_dst); \
654 /* EXPECT_STREQ(zone, al.abbr); */ \
655 } while (0)
656
657 // These tests sometimes run on platforms that have zoneinfo data so old
658 // that the transition we are attempting to check does not exist, most
659 // notably Android emulators. Fortunately, AndroidZoneInfoSource supports
660 // time_zone::version() so, in cases where we've learned that it matters,
661 // we can make the check conditionally.
VersionCmp(time_zone tz,const std::string & target)662 int VersionCmp(time_zone tz, const std::string& target) {
663 std::string version = tz.version();
664 if (version.empty() && !target.empty()) return 1; // unknown > known
665 return version.compare(target);
666 }
667
668 } // namespace
669
670 #if !defined(__EMSCRIPTEN__)
TEST(TimeZones,LoadZonesConcurrently)671 TEST(TimeZones, LoadZonesConcurrently) {
672 std::promise<void> ready_promise;
673 std::shared_future<void> ready_future(ready_promise.get_future());
674 auto load_zones = [ready_future](std::promise<void>* started,
675 std::set<std::string>* failures) {
676 started->set_value();
677 ready_future.wait();
678 for (const char* const* np = kTimeZoneNames; *np != nullptr; ++np) {
679 std::string zone = *np;
680 time_zone tz;
681 if (load_time_zone(zone, &tz)) {
682 EXPECT_EQ(zone, tz.name());
683 } else {
684 failures->insert(zone);
685 }
686 }
687 };
688
689 const std::size_t n_threads = 128;
690 std::vector<std::thread> threads;
691 std::vector<std::set<std::string>> thread_failures(n_threads);
692 for (std::size_t i = 0; i != n_threads; ++i) {
693 std::promise<void> started;
694 threads.emplace_back(load_zones, &started, &thread_failures[i]);
695 started.get_future().wait();
696 }
697 ready_promise.set_value();
698 for (auto& thread : threads) {
699 thread.join();
700 }
701
702 // Allow a small number of failures to account for skew between
703 // the contents of kTimeZoneNames and the zoneinfo data source.
704 #if defined(__ANDROID__)
705 // Cater to the possibility of using an even older zoneinfo data
706 // source when running on Android, where it is difficult to override
707 // the bionic tzdata provided by the test environment.
708 const std::size_t max_failures = 20;
709 #else
710 const std::size_t max_failures = 3;
711 #endif
712 std::set<std::string> failures;
713 for (const auto& thread_failure : thread_failures) {
714 failures.insert(thread_failure.begin(), thread_failure.end());
715 }
716 EXPECT_LE(failures.size(), max_failures) << testing::PrintToString(failures);
717 }
718 #endif
719
TEST(TimeZone,NamedTimeZones)720 TEST(TimeZone, NamedTimeZones) {
721 const time_zone utc = utc_time_zone();
722 EXPECT_EQ("UTC", utc.name());
723 const time_zone nyc = LoadZone("America/New_York");
724 EXPECT_EQ("America/New_York", nyc.name());
725 const time_zone syd = LoadZone("Australia/Sydney");
726 EXPECT_EQ("Australia/Sydney", syd.name());
727 const time_zone fixed0 =
728 fixed_time_zone(absl::time_internal::cctz::seconds::zero());
729 EXPECT_EQ("UTC", fixed0.name());
730 const time_zone fixed_pos = fixed_time_zone(
731 chrono::hours(3) + chrono::minutes(25) + chrono::seconds(45));
732 EXPECT_EQ("Fixed/UTC+03:25:45", fixed_pos.name());
733 const time_zone fixed_neg = fixed_time_zone(
734 -(chrono::hours(12) + chrono::minutes(34) + chrono::seconds(56)));
735 EXPECT_EQ("Fixed/UTC-12:34:56", fixed_neg.name());
736 }
737
TEST(TimeZone,Failures)738 TEST(TimeZone, Failures) {
739 time_zone tz;
740 EXPECT_FALSE(load_time_zone(":America/Los_Angeles", &tz));
741
742 tz = LoadZone("America/Los_Angeles");
743 EXPECT_FALSE(load_time_zone("Invalid/TimeZone", &tz));
744 EXPECT_EQ(chrono::system_clock::from_time_t(0),
745 convert(civil_second(1970, 1, 1, 0, 0, 0), tz)); // UTC
746
747 // Ensures that the load still fails on a subsequent attempt.
748 tz = LoadZone("America/Los_Angeles");
749 EXPECT_FALSE(load_time_zone("Invalid/TimeZone", &tz));
750 EXPECT_EQ(chrono::system_clock::from_time_t(0),
751 convert(civil_second(1970, 1, 1, 0, 0, 0), tz)); // UTC
752
753 // Loading an empty string timezone should fail.
754 tz = LoadZone("America/Los_Angeles");
755 EXPECT_FALSE(load_time_zone("", &tz));
756 EXPECT_EQ(chrono::system_clock::from_time_t(0),
757 convert(civil_second(1970, 1, 1, 0, 0, 0), tz)); // UTC
758 }
759
TEST(TimeZone,Equality)760 TEST(TimeZone, Equality) {
761 const time_zone a;
762 const time_zone b;
763 EXPECT_EQ(a, b);
764 EXPECT_EQ(a.name(), b.name());
765
766 const time_zone implicit_utc;
767 const time_zone explicit_utc = utc_time_zone();
768 EXPECT_EQ(implicit_utc, explicit_utc);
769 EXPECT_EQ(implicit_utc.name(), explicit_utc.name());
770
771 const time_zone fixed_zero =
772 fixed_time_zone(absl::time_internal::cctz::seconds::zero());
773 EXPECT_EQ(fixed_zero, LoadZone(fixed_zero.name()));
774 EXPECT_EQ(fixed_zero, explicit_utc);
775
776 const time_zone fixed_utc = LoadZone("Fixed/UTC+00:00:00");
777 EXPECT_EQ(fixed_utc, LoadZone(fixed_utc.name()));
778 EXPECT_EQ(fixed_utc, explicit_utc);
779
780 const time_zone fixed_pos = fixed_time_zone(
781 chrono::hours(3) + chrono::minutes(25) + chrono::seconds(45));
782 EXPECT_EQ(fixed_pos, LoadZone(fixed_pos.name()));
783 EXPECT_NE(fixed_pos, explicit_utc);
784 const time_zone fixed_neg = fixed_time_zone(
785 -(chrono::hours(12) + chrono::minutes(34) + chrono::seconds(56)));
786 EXPECT_EQ(fixed_neg, LoadZone(fixed_neg.name()));
787 EXPECT_NE(fixed_neg, explicit_utc);
788
789 const time_zone fixed_lim = fixed_time_zone(chrono::hours(24));
790 EXPECT_EQ(fixed_lim, LoadZone(fixed_lim.name()));
791 EXPECT_NE(fixed_lim, explicit_utc);
792 const time_zone fixed_ovfl =
793 fixed_time_zone(chrono::hours(24) + chrono::seconds(1));
794 EXPECT_EQ(fixed_ovfl, LoadZone(fixed_ovfl.name()));
795 EXPECT_EQ(fixed_ovfl, explicit_utc);
796
797 EXPECT_EQ(fixed_time_zone(chrono::seconds(1)),
798 fixed_time_zone(chrono::seconds(1)));
799
800 const time_zone local = local_time_zone();
801 EXPECT_EQ(local, LoadZone(local.name()));
802
803 time_zone la = LoadZone("America/Los_Angeles");
804 time_zone nyc = LoadZone("America/New_York");
805 EXPECT_NE(la, nyc);
806 }
807
TEST(StdChronoTimePoint,TimeTAlignment)808 TEST(StdChronoTimePoint, TimeTAlignment) {
809 // Ensures that the Unix epoch and the system clock epoch are an integral
810 // number of seconds apart. This simplifies conversions to/from time_t.
811 auto diff =
812 chrono::system_clock::time_point() - chrono::system_clock::from_time_t(0);
813 EXPECT_EQ(chrono::system_clock::time_point::duration::zero(),
814 diff % chrono::seconds(1));
815 }
816
TEST(BreakTime,TimePointResolution)817 TEST(BreakTime, TimePointResolution) {
818 const time_zone utc = utc_time_zone();
819 const auto t0 = chrono::system_clock::from_time_t(0);
820
821 ExpectTime(chrono::time_point_cast<chrono::nanoseconds>(t0), utc, 1970, 1, 1,
822 0, 0, 0, 0, false, "UTC");
823 ExpectTime(chrono::time_point_cast<chrono::microseconds>(t0), utc, 1970, 1, 1,
824 0, 0, 0, 0, false, "UTC");
825 ExpectTime(chrono::time_point_cast<chrono::milliseconds>(t0), utc, 1970, 1, 1,
826 0, 0, 0, 0, false, "UTC");
827 ExpectTime(chrono::time_point_cast<chrono::seconds>(t0), utc, 1970, 1, 1, 0,
828 0, 0, 0, false, "UTC");
829 ExpectTime(chrono::time_point_cast<absl::time_internal::cctz::seconds>(t0),
830 utc, 1970, 1, 1, 0, 0, 0, 0, false, "UTC");
831 ExpectTime(chrono::time_point_cast<chrono::minutes>(t0), utc, 1970, 1, 1, 0,
832 0, 0, 0, false, "UTC");
833 ExpectTime(chrono::time_point_cast<chrono::hours>(t0), utc, 1970, 1, 1, 0, 0,
834 0, 0, false, "UTC");
835 }
836
TEST(BreakTime,LocalTimeInUTC)837 TEST(BreakTime, LocalTimeInUTC) {
838 const time_zone tz = utc_time_zone();
839 const auto tp = chrono::system_clock::from_time_t(0);
840 ExpectTime(tp, tz, 1970, 1, 1, 0, 0, 0, 0, false, "UTC");
841 EXPECT_EQ(weekday::thursday, get_weekday(convert(tp, tz)));
842 }
843
TEST(BreakTime,LocalTimeInUTCUnaligned)844 TEST(BreakTime, LocalTimeInUTCUnaligned) {
845 const time_zone tz = utc_time_zone();
846 const auto tp =
847 chrono::system_clock::from_time_t(0) - chrono::milliseconds(500);
848 ExpectTime(tp, tz, 1969, 12, 31, 23, 59, 59, 0, false, "UTC");
849 EXPECT_EQ(weekday::wednesday, get_weekday(convert(tp, tz)));
850 }
851
TEST(BreakTime,LocalTimePosix)852 TEST(BreakTime, LocalTimePosix) {
853 // See IEEE Std 1003.1-1988 B.2.3 General Terms, Epoch.
854 const time_zone tz = utc_time_zone();
855 const auto tp = chrono::system_clock::from_time_t(536457599);
856 ExpectTime(tp, tz, 1986, 12, 31, 23, 59, 59, 0, false, "UTC");
857 EXPECT_EQ(weekday::wednesday, get_weekday(convert(tp, tz)));
858 }
859
TEST(TimeZoneImpl,LocalTimeInFixed)860 TEST(TimeZoneImpl, LocalTimeInFixed) {
861 const absl::time_internal::cctz::seconds offset =
862 -(chrono::hours(8) + chrono::minutes(33) + chrono::seconds(47));
863 const time_zone tz = fixed_time_zone(offset);
864 const auto tp = chrono::system_clock::from_time_t(0);
865 ExpectTime(tp, tz, 1969, 12, 31, 15, 26, 13, offset.count(), false,
866 "-083347");
867 EXPECT_EQ(weekday::wednesday, get_weekday(convert(tp, tz)));
868 }
869
TEST(BreakTime,LocalTimeInNewYork)870 TEST(BreakTime, LocalTimeInNewYork) {
871 const time_zone tz = LoadZone("America/New_York");
872 const auto tp = chrono::system_clock::from_time_t(45);
873 ExpectTime(tp, tz, 1969, 12, 31, 19, 0, 45, -5 * 60 * 60, false, "EST");
874 EXPECT_EQ(weekday::wednesday, get_weekday(convert(tp, tz)));
875 }
876
TEST(BreakTime,LocalTimeInMTV)877 TEST(BreakTime, LocalTimeInMTV) {
878 const time_zone tz = LoadZone("America/Los_Angeles");
879 const auto tp = chrono::system_clock::from_time_t(1380855729);
880 ExpectTime(tp, tz, 2013, 10, 3, 20, 2, 9, -7 * 60 * 60, true, "PDT");
881 EXPECT_EQ(weekday::thursday, get_weekday(convert(tp, tz)));
882 }
883
TEST(BreakTime,LocalTimeInSydney)884 TEST(BreakTime, LocalTimeInSydney) {
885 const time_zone tz = LoadZone("Australia/Sydney");
886 const auto tp = chrono::system_clock::from_time_t(90);
887 ExpectTime(tp, tz, 1970, 1, 1, 10, 1, 30, 10 * 60 * 60, false, "AEST");
888 EXPECT_EQ(weekday::thursday, get_weekday(convert(tp, tz)));
889 }
890
TEST(MakeTime,TimePointResolution)891 TEST(MakeTime, TimePointResolution) {
892 const time_zone utc = utc_time_zone();
893 const time_point<chrono::nanoseconds> tp_ns =
894 convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
895 EXPECT_EQ("04:05", format("%M:%E*S", tp_ns, utc));
896 const time_point<chrono::microseconds> tp_us =
897 convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
898 EXPECT_EQ("04:05", format("%M:%E*S", tp_us, utc));
899 const time_point<chrono::milliseconds> tp_ms =
900 convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
901 EXPECT_EQ("04:05", format("%M:%E*S", tp_ms, utc));
902 const time_point<chrono::seconds> tp_s =
903 convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
904 EXPECT_EQ("04:05", format("%M:%E*S", tp_s, utc));
905 const time_point<absl::time_internal::cctz::seconds> tp_s64 =
906 convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
907 EXPECT_EQ("04:05", format("%M:%E*S", tp_s64, utc));
908
909 // These next two require chrono::time_point_cast because the conversion
910 // from a resolution of seconds (the return value of convert()) to a
911 // coarser resolution requires an explicit cast.
912 const time_point<chrono::minutes> tp_m =
913 chrono::time_point_cast<chrono::minutes>(
914 convert(civil_second(2015, 1, 2, 3, 4, 5), utc));
915 EXPECT_EQ("04:00", format("%M:%E*S", tp_m, utc));
916 const time_point<chrono::hours> tp_h = chrono::time_point_cast<chrono::hours>(
917 convert(civil_second(2015, 1, 2, 3, 4, 5), utc));
918 EXPECT_EQ("00:00", format("%M:%E*S", tp_h, utc));
919 }
920
TEST(MakeTime,Normalization)921 TEST(MakeTime, Normalization) {
922 const time_zone tz = LoadZone("America/New_York");
923 const auto tp = convert(civil_second(2009, 2, 13, 18, 31, 30), tz);
924 EXPECT_EQ(chrono::system_clock::from_time_t(1234567890), tp);
925
926 // Now requests for the same time_point but with out-of-range fields.
927 EXPECT_EQ(tp, convert(civil_second(2008, 14, 13, 18, 31, 30), tz)); // month
928 EXPECT_EQ(tp, convert(civil_second(2009, 1, 44, 18, 31, 30), tz)); // day
929 EXPECT_EQ(tp, convert(civil_second(2009, 2, 12, 42, 31, 30), tz)); // hour
930 EXPECT_EQ(tp, convert(civil_second(2009, 2, 13, 17, 91, 30), tz)); // minute
931 EXPECT_EQ(tp, convert(civil_second(2009, 2, 13, 18, 30, 90), tz)); // second
932 }
933
934 // NOTE: Run this with -ftrapv to detect overflow problems.
TEST(MakeTime,SysSecondsLimits)935 TEST(MakeTime, SysSecondsLimits) {
936 const char RFC3339[] = "%Y-%m-%d%ET%H:%M:%S%Ez";
937 const time_zone utc = utc_time_zone();
938 const time_zone east = fixed_time_zone(chrono::hours(14));
939 const time_zone west = fixed_time_zone(-chrono::hours(14));
940 time_point<absl::time_internal::cctz::seconds> tp;
941
942 // Approach the maximal time_point<cctz::seconds> value from below.
943 tp = convert(civil_second(292277026596, 12, 4, 15, 30, 6), utc);
944 EXPECT_EQ("292277026596-12-04T15:30:06+00:00", format(RFC3339, tp, utc));
945 tp = convert(civil_second(292277026596, 12, 4, 15, 30, 7), utc);
946 EXPECT_EQ("292277026596-12-04T15:30:07+00:00", format(RFC3339, tp, utc));
947 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
948 tp = convert(civil_second(292277026596, 12, 4, 15, 30, 8), utc);
949 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
950 tp = convert(civil_second::max(), utc);
951 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
952
953 // Checks that we can also get the maximal value for a far-east zone.
954 tp = convert(civil_second(292277026596, 12, 5, 5, 30, 7), east);
955 EXPECT_EQ("292277026596-12-05T05:30:07+14:00", format(RFC3339, tp, east));
956 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
957 tp = convert(civil_second(292277026596, 12, 5, 5, 30, 8), east);
958 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
959 tp = convert(civil_second::max(), east);
960 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
961
962 // Checks that we can also get the maximal value for a far-west zone.
963 tp = convert(civil_second(292277026596, 12, 4, 1, 30, 7), west);
964 EXPECT_EQ("292277026596-12-04T01:30:07-14:00", format(RFC3339, tp, west));
965 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
966 tp = convert(civil_second(292277026596, 12, 4, 7, 30, 8), west);
967 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
968 tp = convert(civil_second::max(), west);
969 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
970
971 // Approach the minimal time_point<cctz::seconds> value from above.
972 tp = convert(civil_second(-292277022657, 1, 27, 8, 29, 53), utc);
973 EXPECT_EQ("-292277022657-01-27T08:29:53+00:00", format(RFC3339, tp, utc));
974 tp = convert(civil_second(-292277022657, 1, 27, 8, 29, 52), utc);
975 EXPECT_EQ("-292277022657-01-27T08:29:52+00:00", format(RFC3339, tp, utc));
976 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
977 tp = convert(civil_second(-292277022657, 1, 27, 8, 29, 51), utc);
978 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
979 tp = convert(civil_second::min(), utc);
980 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
981
982 // Checks that we can also get the minimal value for a far-east zone.
983 tp = convert(civil_second(-292277022657, 1, 27, 22, 29, 52), east);
984 EXPECT_EQ("-292277022657-01-27T22:29:52+14:00", format(RFC3339, tp, east));
985 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
986 tp = convert(civil_second(-292277022657, 1, 27, 22, 29, 51), east);
987 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
988 tp = convert(civil_second::min(), east);
989 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
990
991 // Checks that we can also get the minimal value for a far-west zone.
992 tp = convert(civil_second(-292277022657, 1, 26, 18, 29, 52), west);
993 EXPECT_EQ("-292277022657-01-26T18:29:52-14:00", format(RFC3339, tp, west));
994 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
995 tp = convert(civil_second(-292277022657, 1, 26, 18, 29, 51), west);
996 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
997 tp = convert(civil_second::min(), west);
998 EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
999
1000 // Some similar checks for the "libc" time-zone implementation.
1001 if (sizeof(std::time_t) >= 8) {
1002 // Checks that "tm_year + 1900", as used by the "libc" implementation,
1003 // can produce year values beyond the range on an int without overflow.
1004 #if defined(_WIN32) || defined(_WIN64)
1005 // localtime_s() and gmtime_s() don't believe in years outside [1970:3000].
1006 #else
1007 const time_zone cut = LoadZone("libc:UTC");
1008 const year_t max_tm_year = year_t{std::numeric_limits<int>::max()} + 1900;
1009 tp = convert(civil_second(max_tm_year, 12, 31, 23, 59, 59), cut);
1010 #if defined(__FreeBSD__) || defined(__OpenBSD__)
1011 // The BSD gmtime_r() fails on extreme positive tm_year values.
1012 #else
1013 EXPECT_EQ("2147485547-12-31T23:59:59+00:00", format(RFC3339, tp, cut));
1014 #endif
1015 const year_t min_tm_year = year_t{std::numeric_limits<int>::min()} + 1900;
1016 tp = convert(civil_second(min_tm_year, 1, 1, 0, 0, 0), cut);
1017 EXPECT_EQ("-2147481748-01-01T00:00:00+00:00", format(RFC3339, tp, cut));
1018 #endif
1019 }
1020 }
1021
TEST(MakeTime,LocalTimeLibC)1022 TEST(MakeTime, LocalTimeLibC) {
1023 // Checks that cctz and libc agree on transition points in [1970:2037].
1024 //
1025 // We limit this test case to environments where:
1026 // 1) we know how to change the time zone used by localtime()/mktime(),
1027 // 2) cctz and localtime()/mktime() will use similar-enough tzdata, and
1028 // 3) we have some idea about how mktime() behaves during transitions.
1029 #if defined(__linux__) && !defined(__ANDROID__)
1030 const char* const ep = getenv("TZ");
1031 std::string tz_name = (ep != nullptr) ? ep : "";
1032 for (const char* const* np = kTimeZoneNames; *np != nullptr; ++np) {
1033 ASSERT_EQ(0, setenv("TZ", *np, 1)); // change what "localtime" means
1034 const auto zi = local_time_zone();
1035 const auto lc = LoadZone("libc:localtime");
1036 time_zone::civil_transition transition;
1037 for (auto tp = zi.lookup(civil_second()).trans;
1038 zi.next_transition(tp, &transition);
1039 tp = zi.lookup(transition.to).trans) {
1040 const auto fcl = zi.lookup(transition.from);
1041 const auto tcl = zi.lookup(transition.to);
1042 civil_second cs; // compare cs in zi and lc
1043 if (fcl.kind == time_zone::civil_lookup::UNIQUE) {
1044 if (tcl.kind == time_zone::civil_lookup::UNIQUE) {
1045 // Both unique; must be an is_dst or abbr change.
1046 ASSERT_EQ(transition.from, transition.to);
1047 const auto trans = fcl.trans;
1048 const auto tal = zi.lookup(trans);
1049 const auto tprev = trans - absl::time_internal::cctz::seconds(1);
1050 const auto pal = zi.lookup(tprev);
1051 if (pal.is_dst == tal.is_dst) {
1052 ASSERT_STRNE(pal.abbr, tal.abbr);
1053 }
1054 continue;
1055 }
1056 ASSERT_EQ(time_zone::civil_lookup::REPEATED, tcl.kind);
1057 cs = transition.to;
1058 } else {
1059 ASSERT_EQ(time_zone::civil_lookup::UNIQUE, tcl.kind);
1060 ASSERT_EQ(time_zone::civil_lookup::SKIPPED, fcl.kind);
1061 cs = transition.from;
1062 }
1063 if (cs.year() > 2037) break; // limit test time (and to 32-bit time_t)
1064 const auto cl_zi = zi.lookup(cs);
1065 if (zi.lookup(cl_zi.pre).is_dst == zi.lookup(cl_zi.post).is_dst) {
1066 // The "libc" implementation cannot correctly classify transitions
1067 // that don't change the "tm_isdst" flag. In Europe/Volgograd, for
1068 // example, there is a SKIPPED transition from +03 to +04 with dst=F
1069 // on both sides ...
1070 // 1540681199 = 2018-10-28 01:59:59 +03:00:00 [dst=F off=10800]
1071 // 1540681200 = 2018-10-28 03:00:00 +04:00:00 [dst=F off=14400]
1072 // but std::mktime(2018-10-28 02:00:00, tm_isdst=0) fails, unlike,
1073 // say, the similar Europe/Chisinau transition from +02 to +03 ...
1074 // 1521935999 = 2018-03-25 01:59:59 +02:00:00 [dst=F off=7200]
1075 // 1521936000 = 2018-03-25 03:00:00 +03:00:00 [dst=T off=10800]
1076 // where std::mktime(2018-03-25 02:00:00, tm_isdst=0) succeeds and
1077 // returns 1521936000.
1078 continue;
1079 }
1080 if (cs == civil_second(2037, 10, 4, 2, 0, 0)) {
1081 const std::string tzname = *np;
1082 if (tzname == "Africa/Casablanca" || tzname == "Africa/El_Aaiun") {
1083 // The "libc" implementation gets this transition wrong (at least
1084 // until 2018g when it was removed), returning an offset of 3600
1085 // instead of 0. TODO: Revert this when 2018g is ubiquitous.
1086 continue;
1087 }
1088 }
1089 const auto cl_lc = lc.lookup(cs);
1090 SCOPED_TRACE(testing::Message() << "For " << cs << " in " << *np);
1091 EXPECT_EQ(cl_zi.kind, cl_lc.kind);
1092 EXPECT_EQ(cl_zi.pre, cl_lc.pre);
1093 EXPECT_EQ(cl_zi.trans, cl_lc.trans);
1094 EXPECT_EQ(cl_zi.post, cl_lc.post);
1095 }
1096 }
1097 if (ep == nullptr) {
1098 ASSERT_EQ(0, unsetenv("TZ"));
1099 } else {
1100 ASSERT_EQ(0, setenv("TZ", tz_name.c_str(), 1));
1101 }
1102 #endif
1103 }
1104
TEST(NextTransition,UTC)1105 TEST(NextTransition, UTC) {
1106 const auto tz = utc_time_zone();
1107 time_zone::civil_transition trans;
1108
1109 auto tp = time_point<absl::time_internal::cctz::seconds>::min();
1110 EXPECT_FALSE(tz.next_transition(tp, &trans));
1111
1112 tp = time_point<absl::time_internal::cctz::seconds>::max();
1113 EXPECT_FALSE(tz.next_transition(tp, &trans));
1114 }
1115
TEST(PrevTransition,UTC)1116 TEST(PrevTransition, UTC) {
1117 const auto tz = utc_time_zone();
1118 time_zone::civil_transition trans;
1119
1120 auto tp = time_point<absl::time_internal::cctz::seconds>::max();
1121 EXPECT_FALSE(tz.prev_transition(tp, &trans));
1122
1123 tp = time_point<absl::time_internal::cctz::seconds>::min();
1124 EXPECT_FALSE(tz.prev_transition(tp, &trans));
1125 }
1126
TEST(NextTransition,AmericaNewYork)1127 TEST(NextTransition, AmericaNewYork) {
1128 const auto tz = LoadZone("America/New_York");
1129 time_zone::civil_transition trans;
1130
1131 auto tp = convert(civil_second(2018, 6, 30, 0, 0, 0), tz);
1132 EXPECT_TRUE(tz.next_transition(tp, &trans));
1133 EXPECT_EQ(civil_second(2018, 11, 4, 2, 0, 0), trans.from);
1134 EXPECT_EQ(civil_second(2018, 11, 4, 1, 0, 0), trans.to);
1135
1136 tp = time_point<absl::time_internal::cctz::seconds>::max();
1137 EXPECT_FALSE(tz.next_transition(tp, &trans));
1138
1139 tp = time_point<absl::time_internal::cctz::seconds>::min();
1140 EXPECT_TRUE(tz.next_transition(tp, &trans));
1141 if (trans.from == civil_second(1918, 3, 31, 2, 0, 0)) {
1142 // It looks like the tzdata is only 32 bit (probably macOS),
1143 // which bottoms out at 1901-12-13T20:45:52+00:00.
1144 EXPECT_EQ(civil_second(1918, 3, 31, 3, 0, 0), trans.to);
1145 } else {
1146 EXPECT_EQ(civil_second(1883, 11, 18, 12, 3, 58), trans.from);
1147 EXPECT_EQ(civil_second(1883, 11, 18, 12, 0, 0), trans.to);
1148 }
1149 }
1150
TEST(PrevTransition,AmericaNewYork)1151 TEST(PrevTransition, AmericaNewYork) {
1152 const auto tz = LoadZone("America/New_York");
1153 time_zone::civil_transition trans;
1154
1155 auto tp = convert(civil_second(2018, 6, 30, 0, 0, 0), tz);
1156 EXPECT_TRUE(tz.prev_transition(tp, &trans));
1157 EXPECT_EQ(civil_second(2018, 3, 11, 2, 0, 0), trans.from);
1158 EXPECT_EQ(civil_second(2018, 3, 11, 3, 0, 0), trans.to);
1159
1160 tp = time_point<absl::time_internal::cctz::seconds>::min();
1161 EXPECT_FALSE(tz.prev_transition(tp, &trans));
1162
1163 tp = time_point<absl::time_internal::cctz::seconds>::max();
1164 EXPECT_TRUE(tz.prev_transition(tp, &trans));
1165 // We have a transition but we don't know which one.
1166 }
1167
TEST(TimeZoneEdgeCase,AmericaNewYork)1168 TEST(TimeZoneEdgeCase, AmericaNewYork) {
1169 const time_zone tz = LoadZone("America/New_York");
1170
1171 // Spring 1:59:59 -> 3:00:00
1172 auto tp = convert(civil_second(2013, 3, 10, 1, 59, 59), tz);
1173 ExpectTime(tp, tz, 2013, 3, 10, 1, 59, 59, -5 * 3600, false, "EST");
1174 tp += absl::time_internal::cctz::seconds(1);
1175 ExpectTime(tp, tz, 2013, 3, 10, 3, 0, 0, -4 * 3600, true, "EDT");
1176
1177 // Fall 1:59:59 -> 1:00:00
1178 tp = convert(civil_second(2013, 11, 3, 1, 59, 59), tz);
1179 ExpectTime(tp, tz, 2013, 11, 3, 1, 59, 59, -4 * 3600, true, "EDT");
1180 tp += absl::time_internal::cctz::seconds(1);
1181 ExpectTime(tp, tz, 2013, 11, 3, 1, 0, 0, -5 * 3600, false, "EST");
1182 }
1183
TEST(TimeZoneEdgeCase,AmericaLosAngeles)1184 TEST(TimeZoneEdgeCase, AmericaLosAngeles) {
1185 const time_zone tz = LoadZone("America/Los_Angeles");
1186
1187 // Spring 1:59:59 -> 3:00:00
1188 auto tp = convert(civil_second(2013, 3, 10, 1, 59, 59), tz);
1189 ExpectTime(tp, tz, 2013, 3, 10, 1, 59, 59, -8 * 3600, false, "PST");
1190 tp += absl::time_internal::cctz::seconds(1);
1191 ExpectTime(tp, tz, 2013, 3, 10, 3, 0, 0, -7 * 3600, true, "PDT");
1192
1193 // Fall 1:59:59 -> 1:00:00
1194 tp = convert(civil_second(2013, 11, 3, 1, 59, 59), tz);
1195 ExpectTime(tp, tz, 2013, 11, 3, 1, 59, 59, -7 * 3600, true, "PDT");
1196 tp += absl::time_internal::cctz::seconds(1);
1197 ExpectTime(tp, tz, 2013, 11, 3, 1, 0, 0, -8 * 3600, false, "PST");
1198 }
1199
TEST(TimeZoneEdgeCase,ArizonaNoTransition)1200 TEST(TimeZoneEdgeCase, ArizonaNoTransition) {
1201 const time_zone tz = LoadZone("America/Phoenix");
1202
1203 // No transition in Spring.
1204 auto tp = convert(civil_second(2013, 3, 10, 1, 59, 59), tz);
1205 ExpectTime(tp, tz, 2013, 3, 10, 1, 59, 59, -7 * 3600, false, "MST");
1206 tp += absl::time_internal::cctz::seconds(1);
1207 ExpectTime(tp, tz, 2013, 3, 10, 2, 0, 0, -7 * 3600, false, "MST");
1208
1209 // No transition in Fall.
1210 tp = convert(civil_second(2013, 11, 3, 1, 59, 59), tz);
1211 ExpectTime(tp, tz, 2013, 11, 3, 1, 59, 59, -7 * 3600, false, "MST");
1212 tp += absl::time_internal::cctz::seconds(1);
1213 ExpectTime(tp, tz, 2013, 11, 3, 2, 0, 0, -7 * 3600, false, "MST");
1214 }
1215
TEST(TimeZoneEdgeCase,AsiaKathmandu)1216 TEST(TimeZoneEdgeCase, AsiaKathmandu) {
1217 const time_zone tz = LoadZone("Asia/Kathmandu");
1218
1219 // A non-DST offset change from +0530 to +0545
1220 //
1221 // 504901799 == Tue, 31 Dec 1985 23:59:59 +0530 (+0530)
1222 // 504901800 == Wed, 1 Jan 1986 00:15:00 +0545 (+0545)
1223 auto tp = convert(civil_second(1985, 12, 31, 23, 59, 59), tz);
1224 ExpectTime(tp, tz, 1985, 12, 31, 23, 59, 59, 5.5 * 3600, false, "+0530");
1225 tp += absl::time_internal::cctz::seconds(1);
1226 ExpectTime(tp, tz, 1986, 1, 1, 0, 15, 0, 5.75 * 3600, false, "+0545");
1227 }
1228
TEST(TimeZoneEdgeCase,PacificChatham)1229 TEST(TimeZoneEdgeCase, PacificChatham) {
1230 const time_zone tz = LoadZone("Pacific/Chatham");
1231
1232 // One-hour DST offset changes, but at atypical values
1233 //
1234 // 1365256799 == Sun, 7 Apr 2013 03:44:59 +1345 (+1345)
1235 // 1365256800 == Sun, 7 Apr 2013 02:45:00 +1245 (+1245)
1236 auto tp = convert(civil_second(2013, 4, 7, 3, 44, 59), tz);
1237 ExpectTime(tp, tz, 2013, 4, 7, 3, 44, 59, 13.75 * 3600, true, "+1345");
1238 tp += absl::time_internal::cctz::seconds(1);
1239 ExpectTime(tp, tz, 2013, 4, 7, 2, 45, 0, 12.75 * 3600, false, "+1245");
1240
1241 // 1380376799 == Sun, 29 Sep 2013 02:44:59 +1245 (+1245)
1242 // 1380376800 == Sun, 29 Sep 2013 03:45:00 +1345 (+1345)
1243 tp = convert(civil_second(2013, 9, 29, 2, 44, 59), tz);
1244 ExpectTime(tp, tz, 2013, 9, 29, 2, 44, 59, 12.75 * 3600, false, "+1245");
1245 tp += absl::time_internal::cctz::seconds(1);
1246 ExpectTime(tp, tz, 2013, 9, 29, 3, 45, 0, 13.75 * 3600, true, "+1345");
1247 }
1248
TEST(TimeZoneEdgeCase,AustraliaLordHowe)1249 TEST(TimeZoneEdgeCase, AustraliaLordHowe) {
1250 const time_zone tz = LoadZone("Australia/Lord_Howe");
1251
1252 // Half-hour DST offset changes
1253 //
1254 // 1365260399 == Sun, 7 Apr 2013 01:59:59 +1100 (+11)
1255 // 1365260400 == Sun, 7 Apr 2013 01:30:00 +1030 (+1030)
1256 auto tp = convert(civil_second(2013, 4, 7, 1, 59, 59), tz);
1257 ExpectTime(tp, tz, 2013, 4, 7, 1, 59, 59, 11 * 3600, true, "+11");
1258 tp += absl::time_internal::cctz::seconds(1);
1259 ExpectTime(tp, tz, 2013, 4, 7, 1, 30, 0, 10.5 * 3600, false, "+1030");
1260
1261 // 1380986999 == Sun, 6 Oct 2013 01:59:59 +1030 (+1030)
1262 // 1380987000 == Sun, 6 Oct 2013 02:30:00 +1100 (+11)
1263 tp = convert(civil_second(2013, 10, 6, 1, 59, 59), tz);
1264 ExpectTime(tp, tz, 2013, 10, 6, 1, 59, 59, 10.5 * 3600, false, "+1030");
1265 tp += absl::time_internal::cctz::seconds(1);
1266 ExpectTime(tp, tz, 2013, 10, 6, 2, 30, 0, 11 * 3600, true, "+11");
1267 }
1268
TEST(TimeZoneEdgeCase,PacificApia)1269 TEST(TimeZoneEdgeCase, PacificApia) {
1270 const time_zone tz = LoadZone("Pacific/Apia");
1271
1272 // At the end of December 2011, Samoa jumped forward by one day,
1273 // skipping 30 December from the local calendar, when the nation
1274 // moved to the west of the International Date Line.
1275 //
1276 // A one-day, non-DST offset change
1277 //
1278 // 1325239199 == Thu, 29 Dec 2011 23:59:59 -1000 (-10)
1279 // 1325239200 == Sat, 31 Dec 2011 00:00:00 +1400 (+14)
1280 auto tp = convert(civil_second(2011, 12, 29, 23, 59, 59), tz);
1281 ExpectTime(tp, tz, 2011, 12, 29, 23, 59, 59, -10 * 3600, true, "-10");
1282 EXPECT_EQ(363, get_yearday(convert(tp, tz)));
1283 tp += absl::time_internal::cctz::seconds(1);
1284 ExpectTime(tp, tz, 2011, 12, 31, 0, 0, 0, 14 * 3600, true, "+14");
1285 EXPECT_EQ(365, get_yearday(convert(tp, tz)));
1286 }
1287
TEST(TimeZoneEdgeCase,AfricaCairo)1288 TEST(TimeZoneEdgeCase, AfricaCairo) {
1289 const time_zone tz = LoadZone("Africa/Cairo");
1290
1291 if (VersionCmp(tz, "2014c") >= 0) {
1292 // An interesting case of midnight not existing.
1293 //
1294 // 1400191199 == Thu, 15 May 2014 23:59:59 +0200 (EET)
1295 // 1400191200 == Fri, 16 May 2014 01:00:00 +0300 (EEST)
1296 auto tp = convert(civil_second(2014, 5, 15, 23, 59, 59), tz);
1297 ExpectTime(tp, tz, 2014, 5, 15, 23, 59, 59, 2 * 3600, false, "EET");
1298 tp += absl::time_internal::cctz::seconds(1);
1299 ExpectTime(tp, tz, 2014, 5, 16, 1, 0, 0, 3 * 3600, true, "EEST");
1300 }
1301 }
1302
TEST(TimeZoneEdgeCase,AfricaMonrovia)1303 TEST(TimeZoneEdgeCase, AfricaMonrovia) {
1304 const time_zone tz = LoadZone("Africa/Monrovia");
1305
1306 if (VersionCmp(tz, "2017b") >= 0) {
1307 // Strange offset change -00:44:30 -> +00:00:00 (non-DST)
1308 //
1309 // 63593069 == Thu, 6 Jan 1972 23:59:59 -0044 (MMT)
1310 // 63593070 == Fri, 7 Jan 1972 00:44:30 +0000 (GMT)
1311 auto tp = convert(civil_second(1972, 1, 6, 23, 59, 59), tz);
1312 ExpectTime(tp, tz, 1972, 1, 6, 23, 59, 59, -44.5 * 60, false, "MMT");
1313 tp += absl::time_internal::cctz::seconds(1);
1314 ExpectTime(tp, tz, 1972, 1, 7, 0, 44, 30, 0 * 60, false, "GMT");
1315 }
1316 }
1317
TEST(TimeZoneEdgeCase,AmericaJamaica)1318 TEST(TimeZoneEdgeCase, AmericaJamaica) {
1319 // Jamaica discontinued DST transitions in 1983, and is now at a
1320 // constant -0500. This makes it an interesting edge-case target.
1321 // Note that the 32-bit times used in a (tzh_version == 0) zoneinfo
1322 // file cannot represent the abbreviation-only transition of 1890,
1323 // so we ignore the abbreviation by expecting what we received.
1324 const time_zone tz = LoadZone("America/Jamaica");
1325
1326 // Before the first transition.
1327 if (!tz.version().empty() && VersionCmp(tz, "2018d") >= 0) {
1328 // We avoid the expectations on the -18430 offset below unless we are
1329 // certain we have commit 907241e (Fix off-by-1 error for Jamaica and
1330 // T&C before 1913) from 2018d. TODO: Remove the "version() not empty"
1331 // part when 2018d is generally available from /usr/share/zoneinfo.
1332 auto tp = convert(civil_second(1889, 12, 31, 0, 0, 0), tz);
1333 ExpectTime(tp, tz, 1889, 12, 31, 0, 0, 0, -18430, false,
1334 tz.lookup(tp).abbr);
1335
1336 // Over the first (abbreviation-change only) transition.
1337 // -2524503170 == Tue, 31 Dec 1889 23:59:59 -0507 (LMT)
1338 // -2524503169 == Wed, 1 Jan 1890 00:00:00 -0507 (KMT)
1339 tp = convert(civil_second(1889, 12, 31, 23, 59, 59), tz);
1340 ExpectTime(tp, tz, 1889, 12, 31, 23, 59, 59, -18430, false,
1341 tz.lookup(tp).abbr);
1342 tp += absl::time_internal::cctz::seconds(1);
1343 ExpectTime(tp, tz, 1890, 1, 1, 0, 0, 0, -18430, false, "KMT");
1344 }
1345
1346 // Over the last (DST) transition.
1347 // 436341599 == Sun, 30 Oct 1983 01:59:59 -0400 (EDT)
1348 // 436341600 == Sun, 30 Oct 1983 01:00:00 -0500 (EST)
1349 auto tp = convert(civil_second(1983, 10, 30, 1, 59, 59), tz);
1350 ExpectTime(tp, tz, 1983, 10, 30, 1, 59, 59, -4 * 3600, true, "EDT");
1351 tp += absl::time_internal::cctz::seconds(1);
1352 ExpectTime(tp, tz, 1983, 10, 30, 1, 0, 0, -5 * 3600, false, "EST");
1353
1354 // After the last transition.
1355 tp = convert(civil_second(1983, 12, 31, 23, 59, 59), tz);
1356 ExpectTime(tp, tz, 1983, 12, 31, 23, 59, 59, -5 * 3600, false, "EST");
1357 }
1358
TEST(TimeZoneEdgeCase,WET)1359 TEST(TimeZoneEdgeCase, WET) {
1360 // Cover some non-existent times within forward transitions.
1361 const time_zone tz = LoadZone("WET");
1362
1363 // Before the first transition.
1364 auto tp = convert(civil_second(1977, 1, 1, 0, 0, 0), tz);
1365 ExpectTime(tp, tz, 1977, 1, 1, 0, 0, 0, 0, false, "WET");
1366
1367 // Over the first transition.
1368 // 228877199 == Sun, 3 Apr 1977 00:59:59 +0000 (WET)
1369 // 228877200 == Sun, 3 Apr 1977 02:00:00 +0100 (WEST)
1370 tp = convert(civil_second(1977, 4, 3, 0, 59, 59), tz);
1371 ExpectTime(tp, tz, 1977, 4, 3, 0, 59, 59, 0, false, "WET");
1372 tp += absl::time_internal::cctz::seconds(1);
1373 ExpectTime(tp, tz, 1977, 4, 3, 2, 0, 0, 1 * 3600, true, "WEST");
1374
1375 // A non-existent time within the first transition.
1376 time_zone::civil_lookup cl1 = tz.lookup(civil_second(1977, 4, 3, 1, 15, 0));
1377 EXPECT_EQ(time_zone::civil_lookup::SKIPPED, cl1.kind);
1378 ExpectTime(cl1.pre, tz, 1977, 4, 3, 2, 15, 0, 1 * 3600, true, "WEST");
1379 ExpectTime(cl1.trans, tz, 1977, 4, 3, 2, 0, 0, 1 * 3600, true, "WEST");
1380 ExpectTime(cl1.post, tz, 1977, 4, 3, 0, 15, 0, 0 * 3600, false, "WET");
1381
1382 // A non-existent time within the second forward transition.
1383 time_zone::civil_lookup cl2 = tz.lookup(civil_second(1978, 4, 2, 1, 15, 0));
1384 EXPECT_EQ(time_zone::civil_lookup::SKIPPED, cl2.kind);
1385 ExpectTime(cl2.pre, tz, 1978, 4, 2, 2, 15, 0, 1 * 3600, true, "WEST");
1386 ExpectTime(cl2.trans, tz, 1978, 4, 2, 2, 0, 0, 1 * 3600, true, "WEST");
1387 ExpectTime(cl2.post, tz, 1978, 4, 2, 0, 15, 0, 0 * 3600, false, "WET");
1388 }
1389
TEST(TimeZoneEdgeCase,FixedOffsets)1390 TEST(TimeZoneEdgeCase, FixedOffsets) {
1391 const time_zone gmtm5 = LoadZone("Etc/GMT+5"); // -0500
1392 auto tp = convert(civil_second(1970, 1, 1, 0, 0, 0), gmtm5);
1393 ExpectTime(tp, gmtm5, 1970, 1, 1, 0, 0, 0, -5 * 3600, false, "-05");
1394 EXPECT_EQ(chrono::system_clock::from_time_t(5 * 3600), tp);
1395
1396 const time_zone gmtp5 = LoadZone("Etc/GMT-5"); // +0500
1397 tp = convert(civil_second(1970, 1, 1, 0, 0, 0), gmtp5);
1398 ExpectTime(tp, gmtp5, 1970, 1, 1, 0, 0, 0, 5 * 3600, false, "+05");
1399 EXPECT_EQ(chrono::system_clock::from_time_t(-5 * 3600), tp);
1400 }
1401
TEST(TimeZoneEdgeCase,NegativeYear)1402 TEST(TimeZoneEdgeCase, NegativeYear) {
1403 // Tests transition from year 0 (aka 1BCE) to year -1.
1404 const time_zone tz = utc_time_zone();
1405 auto tp = convert(civil_second(0, 1, 1, 0, 0, 0), tz);
1406 ExpectTime(tp, tz, 0, 1, 1, 0, 0, 0, 0 * 3600, false, "UTC");
1407 EXPECT_EQ(weekday::saturday, get_weekday(convert(tp, tz)));
1408 tp -= absl::time_internal::cctz::seconds(1);
1409 ExpectTime(tp, tz, -1, 12, 31, 23, 59, 59, 0 * 3600, false, "UTC");
1410 EXPECT_EQ(weekday::friday, get_weekday(convert(tp, tz)));
1411 }
1412
TEST(TimeZoneEdgeCase,UTC32bitLimit)1413 TEST(TimeZoneEdgeCase, UTC32bitLimit) {
1414 const time_zone tz = utc_time_zone();
1415
1416 // Limits of signed 32-bit time_t
1417 //
1418 // 2147483647 == Tue, 19 Jan 2038 03:14:07 +0000 (UTC)
1419 // 2147483648 == Tue, 19 Jan 2038 03:14:08 +0000 (UTC)
1420 auto tp = convert(civil_second(2038, 1, 19, 3, 14, 7), tz);
1421 ExpectTime(tp, tz, 2038, 1, 19, 3, 14, 7, 0 * 3600, false, "UTC");
1422 tp += absl::time_internal::cctz::seconds(1);
1423 ExpectTime(tp, tz, 2038, 1, 19, 3, 14, 8, 0 * 3600, false, "UTC");
1424 }
1425
TEST(TimeZoneEdgeCase,UTC5DigitYear)1426 TEST(TimeZoneEdgeCase, UTC5DigitYear) {
1427 const time_zone tz = utc_time_zone();
1428
1429 // Rollover to 5-digit year
1430 //
1431 // 253402300799 == Fri, 31 Dec 9999 23:59:59 +0000 (UTC)
1432 // 253402300800 == Sat, 1 Jan 1000 00:00:00 +0000 (UTC)
1433 auto tp = convert(civil_second(9999, 12, 31, 23, 59, 59), tz);
1434 ExpectTime(tp, tz, 9999, 12, 31, 23, 59, 59, 0 * 3600, false, "UTC");
1435 tp += absl::time_internal::cctz::seconds(1);
1436 ExpectTime(tp, tz, 10000, 1, 1, 0, 0, 0, 0 * 3600, false, "UTC");
1437 }
1438
1439 } // namespace cctz
1440 } // namespace time_internal
1441 ABSL_NAMESPACE_END
1442 } // namespace absl
1443