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