time_zone_lookup_test.cc
Go to the documentation of this file.
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 
16 
17 #include <chrono>
18 #include <cstddef>
19 #include <cstdlib>
20 #include <future>
21 #include <limits>
22 #include <string>
23 #include <thread>
24 #include <vector>
25 
27 #include "gtest/gtest.h"
28 
29 namespace chrono = std::chrono;
30 
31 namespace absl {
32 namespace time_internal {
33 namespace cctz {
34 
35 namespace {
36 
37 // A list of known time-zone names.
38 const char* const kTimeZoneNames[] = {
39  "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/Ojinaga",
215  "America/Panama",
216  "America/Pangnirtung",
217  "America/Paramaribo",
218  "America/Phoenix",
219  "America/Port-au-Prince",
220  "America/Port_of_Spain",
221  "America/Porto_Acre",
222  "America/Porto_Velho",
223  "America/Puerto_Rico",
224  "America/Punta_Arenas",
225  "America/Rainy_River",
226  "America/Rankin_Inlet",
227  "America/Recife",
228  "America/Regina",
229  "America/Resolute",
230  "America/Rio_Branco",
231  "America/Rosario",
232  "America/Santa_Isabel",
233  "America/Santarem",
234  "America/Santiago",
235  "America/Santo_Domingo",
236  "America/Sao_Paulo",
237  "America/Scoresbysund",
238  "America/Shiprock",
239  "America/Sitka",
240  "America/St_Barthelemy",
241  "America/St_Johns",
242  "America/St_Kitts",
243  "America/St_Lucia",
244  "America/St_Thomas",
245  "America/St_Vincent",
246  "America/Swift_Current",
247  "America/Tegucigalpa",
248  "America/Thule",
249  "America/Thunder_Bay",
250  "America/Tijuana",
251  "America/Toronto",
252  "America/Tortola",
253  "America/Vancouver",
254  "America/Virgin",
255  "America/Whitehorse",
256  "America/Winnipeg",
257  "America/Yakutat",
258  "America/Yellowknife",
259  "Antarctica/Casey",
260  "Antarctica/Davis",
261  "Antarctica/DumontDUrville",
262  "Antarctica/Macquarie",
263  "Antarctica/Mawson",
264  "Antarctica/McMurdo",
265  "Antarctica/Palmer",
266  "Antarctica/Rothera",
267  "Antarctica/South_Pole",
268  "Antarctica/Syowa",
269  "Antarctica/Troll",
270  "Antarctica/Vostok",
271  "Arctic/Longyearbyen",
272  "Asia/Aden",
273  "Asia/Almaty",
274  "Asia/Amman",
275  "Asia/Anadyr",
276  "Asia/Aqtau",
277  "Asia/Aqtobe",
278  "Asia/Ashgabat",
279  "Asia/Ashkhabad",
280  "Asia/Atyrau",
281  "Asia/Baghdad",
282  "Asia/Bahrain",
283  "Asia/Baku",
284  "Asia/Bangkok",
285  "Asia/Barnaul",
286  "Asia/Beirut",
287  "Asia/Bishkek",
288  "Asia/Brunei",
289  "Asia/Calcutta",
290  "Asia/Chita",
291  "Asia/Choibalsan",
292  "Asia/Chongqing",
293  "Asia/Chungking",
294  "Asia/Colombo",
295  "Asia/Dacca",
296  "Asia/Damascus",
297  "Asia/Dhaka",
298  "Asia/Dili",
299  "Asia/Dubai",
300  "Asia/Dushanbe",
301  "Asia/Famagusta",
302  "Asia/Gaza",
303  "Asia/Harbin",
304  "Asia/Hebron",
305  "Asia/Ho_Chi_Minh",
306  "Asia/Hong_Kong",
307  "Asia/Hovd",
308  "Asia/Irkutsk",
309  "Asia/Istanbul",
310  "Asia/Jakarta",
311  "Asia/Jayapura",
312  "Asia/Jerusalem",
313  "Asia/Kabul",
314  "Asia/Kamchatka",
315  "Asia/Karachi",
316  "Asia/Kashgar",
317  "Asia/Kathmandu",
318  "Asia/Katmandu",
319  "Asia/Khandyga",
320  "Asia/Kolkata",
321  "Asia/Krasnoyarsk",
322  "Asia/Kuala_Lumpur",
323  "Asia/Kuching",
324  "Asia/Kuwait",
325  "Asia/Macao",
326  "Asia/Macau",
327  "Asia/Magadan",
328  "Asia/Makassar",
329  "Asia/Manila",
330  "Asia/Muscat",
331  "Asia/Nicosia",
332  "Asia/Novokuznetsk",
333  "Asia/Novosibirsk",
334  "Asia/Omsk",
335  "Asia/Oral",
336  "Asia/Phnom_Penh",
337  "Asia/Pontianak",
338  "Asia/Pyongyang",
339  "Asia/Qatar",
340  "Asia/Qostanay",
341  "Asia/Qyzylorda",
342  "Asia/Rangoon",
343  "Asia/Riyadh",
344  "Asia/Saigon",
345  "Asia/Sakhalin",
346  "Asia/Samarkand",
347  "Asia/Seoul",
348  "Asia/Shanghai",
349  "Asia/Singapore",
350  "Asia/Srednekolymsk",
351  "Asia/Taipei",
352  "Asia/Tashkent",
353  "Asia/Tbilisi",
354  "Asia/Tehran",
355  "Asia/Tel_Aviv",
356  "Asia/Thimbu",
357  "Asia/Thimphu",
358  "Asia/Tokyo",
359  "Asia/Tomsk",
360  "Asia/Ujung_Pandang",
361  "Asia/Ulaanbaatar",
362  "Asia/Ulan_Bator",
363  "Asia/Urumqi",
364  "Asia/Ust-Nera",
365  "Asia/Vientiane",
366  "Asia/Vladivostok",
367  "Asia/Yakutsk",
368  "Asia/Yangon",
369  "Asia/Yekaterinburg",
370  "Asia/Yerevan",
371  "Atlantic/Azores",
372  "Atlantic/Bermuda",
373  "Atlantic/Canary",
374  "Atlantic/Cape_Verde",
375  "Atlantic/Faeroe",
376  "Atlantic/Faroe",
377  "Atlantic/Jan_Mayen",
378  "Atlantic/Madeira",
379  "Atlantic/Reykjavik",
380  "Atlantic/South_Georgia",
381  "Atlantic/St_Helena",
382  "Atlantic/Stanley",
383  "Australia/ACT",
384  "Australia/Adelaide",
385  "Australia/Brisbane",
386  "Australia/Broken_Hill",
387  "Australia/Canberra",
388  "Australia/Currie",
389  "Australia/Darwin",
390  "Australia/Eucla",
391  "Australia/Hobart",
392  "Australia/LHI",
393  "Australia/Lindeman",
394  "Australia/Lord_Howe",
395  "Australia/Melbourne",
396  "Australia/NSW",
397  "Australia/North",
398  "Australia/Perth",
399  "Australia/Queensland",
400  "Australia/South",
401  "Australia/Sydney",
402  "Australia/Tasmania",
403  "Australia/Victoria",
404  "Australia/West",
405  "Australia/Yancowinna",
406  "Brazil/Acre",
407  "Brazil/DeNoronha",
408  "Brazil/East",
409  "Brazil/West",
410  "CET",
411  "CST6CDT",
412  "Canada/Atlantic",
413  "Canada/Central",
414  "Canada/Eastern",
415  "Canada/Mountain",
416  "Canada/Newfoundland",
417  "Canada/Pacific",
418  "Canada/Saskatchewan",
419  "Canada/Yukon",
420  "Chile/Continental",
421  "Chile/EasterIsland",
422  "Cuba",
423  "EET",
424  "EST",
425  "EST5EDT",
426  "Egypt",
427  "Eire",
428  "Etc/GMT",
429  "Etc/GMT+0",
430  "Etc/GMT+1",
431  "Etc/GMT+10",
432  "Etc/GMT+11",
433  "Etc/GMT+12",
434  "Etc/GMT+2",
435  "Etc/GMT+3",
436  "Etc/GMT+4",
437  "Etc/GMT+5",
438  "Etc/GMT+6",
439  "Etc/GMT+7",
440  "Etc/GMT+8",
441  "Etc/GMT+9",
442  "Etc/GMT-0",
443  "Etc/GMT-1",
444  "Etc/GMT-10",
445  "Etc/GMT-11",
446  "Etc/GMT-12",
447  "Etc/GMT-13",
448  "Etc/GMT-14",
449  "Etc/GMT-2",
450  "Etc/GMT-3",
451  "Etc/GMT-4",
452  "Etc/GMT-5",
453  "Etc/GMT-6",
454  "Etc/GMT-7",
455  "Etc/GMT-8",
456  "Etc/GMT-9",
457  "Etc/GMT0",
458  "Etc/Greenwich",
459  "Etc/UCT",
460  "Etc/UTC",
461  "Etc/Universal",
462  "Etc/Zulu",
463  "Europe/Amsterdam",
464  "Europe/Andorra",
465  "Europe/Astrakhan",
466  "Europe/Athens",
467  "Europe/Belfast",
468  "Europe/Belgrade",
469  "Europe/Berlin",
470  "Europe/Bratislava",
471  "Europe/Brussels",
472  "Europe/Bucharest",
473  "Europe/Budapest",
474  "Europe/Busingen",
475  "Europe/Chisinau",
476  "Europe/Copenhagen",
477  "Europe/Dublin",
478  "Europe/Gibraltar",
479  "Europe/Guernsey",
480  "Europe/Helsinki",
481  "Europe/Isle_of_Man",
482  "Europe/Istanbul",
483  "Europe/Jersey",
484  "Europe/Kaliningrad",
485  "Europe/Kiev",
486  "Europe/Kirov",
487  "Europe/Lisbon",
488  "Europe/Ljubljana",
489  "Europe/London",
490  "Europe/Luxembourg",
491  "Europe/Madrid",
492  "Europe/Malta",
493  "Europe/Mariehamn",
494  "Europe/Minsk",
495  "Europe/Monaco",
496  "Europe/Moscow",
497  "Europe/Nicosia",
498  "Europe/Oslo",
499  "Europe/Paris",
500  "Europe/Podgorica",
501  "Europe/Prague",
502  "Europe/Riga",
503  "Europe/Rome",
504  "Europe/Samara",
505  "Europe/San_Marino",
506  "Europe/Sarajevo",
507  "Europe/Saratov",
508  "Europe/Simferopol",
509  "Europe/Skopje",
510  "Europe/Sofia",
511  "Europe/Stockholm",
512  "Europe/Tallinn",
513  "Europe/Tirane",
514  "Europe/Tiraspol",
515  "Europe/Ulyanovsk",
516  "Europe/Uzhgorod",
517  "Europe/Vaduz",
518  "Europe/Vatican",
519  "Europe/Vienna",
520  "Europe/Vilnius",
521  "Europe/Volgograd",
522  "Europe/Warsaw",
523  "Europe/Zagreb",
524  "Europe/Zaporozhye",
525  "Europe/Zurich",
526  "GB",
527  "GB-Eire",
528  "GMT",
529  "GMT+0",
530  "GMT-0",
531  "GMT0",
532  "Greenwich",
533  "HST",
534  "Hongkong",
535  "Iceland",
536  "Indian/Antananarivo",
537  "Indian/Chagos",
538  "Indian/Christmas",
539  "Indian/Cocos",
540  "Indian/Comoro",
541  "Indian/Kerguelen",
542  "Indian/Mahe",
543  "Indian/Maldives",
544  "Indian/Mauritius",
545  "Indian/Mayotte",
546  "Indian/Reunion",
547  "Iran",
548  "Israel",
549  "Jamaica",
550  "Japan",
551  "Kwajalein",
552  "Libya",
553  "MET",
554  "MST",
555  "MST7MDT",
556  "Mexico/BajaNorte",
557  "Mexico/BajaSur",
558  "Mexico/General",
559  "NZ",
560  "NZ-CHAT",
561  "Navajo",
562  "PRC",
563  "PST8PDT",
564  "Pacific/Apia",
565  "Pacific/Auckland",
566  "Pacific/Bougainville",
567  "Pacific/Chatham",
568  "Pacific/Chuuk",
569  "Pacific/Easter",
570  "Pacific/Efate",
571  "Pacific/Enderbury",
572  "Pacific/Fakaofo",
573  "Pacific/Fiji",
574  "Pacific/Funafuti",
575  "Pacific/Galapagos",
576  "Pacific/Gambier",
577  "Pacific/Guadalcanal",
578  "Pacific/Guam",
579  "Pacific/Honolulu",
580  "Pacific/Johnston",
581  "Pacific/Kiritimati",
582  "Pacific/Kosrae",
583  "Pacific/Kwajalein",
584  "Pacific/Majuro",
585  "Pacific/Marquesas",
586  "Pacific/Midway",
587  "Pacific/Nauru",
588  "Pacific/Niue",
589  "Pacific/Norfolk",
590  "Pacific/Noumea",
591  "Pacific/Pago_Pago",
592  "Pacific/Palau",
593  "Pacific/Pitcairn",
594  "Pacific/Pohnpei",
595  "Pacific/Ponape",
596  "Pacific/Port_Moresby",
597  "Pacific/Rarotonga",
598  "Pacific/Saipan",
599  "Pacific/Samoa",
600  "Pacific/Tahiti",
601  "Pacific/Tarawa",
602  "Pacific/Tongatapu",
603  "Pacific/Truk",
604  "Pacific/Wake",
605  "Pacific/Wallis",
606  "Pacific/Yap",
607  "Poland",
608  "Portugal",
609  "ROC",
610  "ROK",
611  "Singapore",
612  "Turkey",
613  "UCT",
614  "US/Alaska",
615  "US/Aleutian",
616  "US/Arizona",
617  "US/Central",
618  "US/East-Indiana",
619  "US/Eastern",
620  "US/Hawaii",
621  "US/Indiana-Starke",
622  "US/Michigan",
623  "US/Mountain",
624  "US/Pacific",
625  "US/Samoa",
626  "UTC",
627  "Universal",
628  "W-SU",
629  "WET",
630  "Zulu",
631  nullptr
632 };
633 
634 // Helper to return a loaded time zone by value (UTC on error).
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.
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__)
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 
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 = fixed_time_zone(absl::time_internal::cctz::seconds::zero());
728  EXPECT_EQ("UTC", fixed0.name());
729  const time_zone fixed_pos = fixed_time_zone(
730  chrono::hours(3) + chrono::minutes(25) + chrono::seconds(45));
731  EXPECT_EQ("Fixed/UTC+03:25:45", fixed_pos.name());
732  const time_zone fixed_neg = fixed_time_zone(
733  -(chrono::hours(12) + chrono::minutes(34) + chrono::seconds(56)));
734  EXPECT_EQ("Fixed/UTC-12:34:56", fixed_neg.name());
735 }
736 
737 TEST(TimeZone, Failures) {
738  time_zone tz;
739  EXPECT_FALSE(load_time_zone(":America/Los_Angeles", &tz));
740 
741  tz = LoadZone("America/Los_Angeles");
742  EXPECT_FALSE(load_time_zone("Invalid/TimeZone", &tz));
743  EXPECT_EQ(chrono::system_clock::from_time_t(0),
744  convert(civil_second(1970, 1, 1, 0, 0, 0), tz)); // UTC
745 
746  // Ensures that the load still fails on a subsequent attempt.
747  tz = LoadZone("America/Los_Angeles");
748  EXPECT_FALSE(load_time_zone("Invalid/TimeZone", &tz));
749  EXPECT_EQ(chrono::system_clock::from_time_t(0),
750  convert(civil_second(1970, 1, 1, 0, 0, 0), tz)); // UTC
751 
752  // Loading an empty std::string timezone should fail.
753  tz = LoadZone("America/Los_Angeles");
754  EXPECT_FALSE(load_time_zone("", &tz));
755  EXPECT_EQ(chrono::system_clock::from_time_t(0),
756  convert(civil_second(1970, 1, 1, 0, 0, 0), tz)); // UTC
757 }
758 
759 TEST(TimeZone, Equality) {
760  const time_zone a;
761  const time_zone b;
762  EXPECT_EQ(a, b);
763  EXPECT_EQ(a.name(), b.name());
764 
765  const time_zone implicit_utc;
766  const time_zone explicit_utc = utc_time_zone();
767  EXPECT_EQ(implicit_utc, explicit_utc);
768  EXPECT_EQ(implicit_utc.name(), explicit_utc.name());
769 
770  const time_zone fixed_zero = fixed_time_zone(absl::time_internal::cctz::seconds::zero());
771  EXPECT_EQ(fixed_zero, LoadZone(fixed_zero.name()));
772  EXPECT_EQ(fixed_zero, explicit_utc);
773 
774  const time_zone fixed_utc = LoadZone("Fixed/UTC+00:00:00");
775  EXPECT_EQ(fixed_utc, LoadZone(fixed_utc.name()));
776  EXPECT_EQ(fixed_utc, explicit_utc);
777 
778  const time_zone fixed_pos = fixed_time_zone(
779  chrono::hours(3) + chrono::minutes(25) + chrono::seconds(45));
780  EXPECT_EQ(fixed_pos, LoadZone(fixed_pos.name()));
781  EXPECT_NE(fixed_pos, explicit_utc);
782  const time_zone fixed_neg = fixed_time_zone(
783  -(chrono::hours(12) + chrono::minutes(34) + chrono::seconds(56)));
784  EXPECT_EQ(fixed_neg, LoadZone(fixed_neg.name()));
785  EXPECT_NE(fixed_neg, explicit_utc);
786 
787  const time_zone fixed_lim = fixed_time_zone(chrono::hours(24));
788  EXPECT_EQ(fixed_lim, LoadZone(fixed_lim.name()));
789  EXPECT_NE(fixed_lim, explicit_utc);
790  const time_zone fixed_ovfl =
791  fixed_time_zone(chrono::hours(24) + chrono::seconds(1));
792  EXPECT_EQ(fixed_ovfl, LoadZone(fixed_ovfl.name()));
793  EXPECT_EQ(fixed_ovfl, explicit_utc);
794 
795  EXPECT_EQ(fixed_time_zone(chrono::seconds(1)),
797 
798  const time_zone local = local_time_zone();
799  EXPECT_EQ(local, LoadZone(local.name()));
800 
801  time_zone la = LoadZone("America/Los_Angeles");
802  time_zone nyc = LoadZone("America/New_York");
803  EXPECT_NE(la, nyc);
804 }
805 
806 TEST(StdChronoTimePoint, TimeTAlignment) {
807  // Ensures that the Unix epoch and the system clock epoch are an integral
808  // number of seconds apart. This simplifies conversions to/from time_t.
809  auto diff = chrono::system_clock::time_point() -
810  chrono::system_clock::from_time_t(0);
811  EXPECT_EQ(chrono::system_clock::time_point::duration::zero(),
812  diff % chrono::seconds(1));
813 }
814 
815 TEST(BreakTime, TimePointResolution) {
816  const time_zone utc = utc_time_zone();
817  const auto t0 = chrono::system_clock::from_time_t(0);
818 
819  ExpectTime(chrono::time_point_cast<chrono::nanoseconds>(t0), utc,
820  1970, 1, 1, 0, 0, 0, 0, false, "UTC");
821  ExpectTime(chrono::time_point_cast<chrono::microseconds>(t0), utc,
822  1970, 1, 1, 0, 0, 0, 0, false, "UTC");
823  ExpectTime(chrono::time_point_cast<chrono::milliseconds>(t0), utc,
824  1970, 1, 1, 0, 0, 0, 0, false, "UTC");
825  ExpectTime(chrono::time_point_cast<chrono::seconds>(t0), utc,
826  1970, 1, 1, 0, 0, 0, 0, false, "UTC");
827  ExpectTime(chrono::time_point_cast<absl::time_internal::cctz::seconds>(t0), utc,
828  1970, 1, 1, 0, 0, 0, 0, false, "UTC");
829  ExpectTime(chrono::time_point_cast<chrono::minutes>(t0), utc,
830  1970, 1, 1, 0, 0, 0, 0, false, "UTC");
831  ExpectTime(chrono::time_point_cast<chrono::hours>(t0), utc,
832  1970, 1, 1, 0, 0, 0, 0, false, "UTC");
833 }
834 
835 TEST(BreakTime, LocalTimeInUTC) {
836  const time_zone tz = utc_time_zone();
837  const auto tp = chrono::system_clock::from_time_t(0);
838  ExpectTime(tp, tz, 1970, 1, 1, 0, 0, 0, 0, false, "UTC");
839  EXPECT_EQ(weekday::thursday, get_weekday(civil_day(convert(tp, tz))));
840 }
841 
842 TEST(BreakTime, LocalTimeInUTCUnaligned) {
843  const time_zone tz = utc_time_zone();
844  const auto tp =
845  chrono::system_clock::from_time_t(0) - chrono::milliseconds(500);
846  ExpectTime(tp, tz, 1969, 12, 31, 23, 59, 59, 0, false, "UTC");
847  EXPECT_EQ(weekday::wednesday, get_weekday(civil_day(convert(tp, tz))));
848 }
849 
850 TEST(BreakTime, LocalTimePosix) {
851  // See IEEE Std 1003.1-1988 B.2.3 General Terms, Epoch.
852  const time_zone tz = utc_time_zone();
853  const auto tp = chrono::system_clock::from_time_t(536457599);
854  ExpectTime(tp, tz, 1986, 12, 31, 23, 59, 59, 0, false, "UTC");
855  EXPECT_EQ(weekday::wednesday, get_weekday(civil_day(convert(tp, tz))));
856 }
857 
858 TEST(TimeZoneImpl, LocalTimeInFixed) {
860  -(chrono::hours(8) + chrono::minutes(33) + chrono::seconds(47));
861  const time_zone tz = fixed_time_zone(offset);
862  const auto tp = chrono::system_clock::from_time_t(0);
863  ExpectTime(tp, tz, 1969, 12, 31, 15, 26, 13, offset.count(), false,
864  "-083347");
865  EXPECT_EQ(weekday::wednesday, get_weekday(civil_day(convert(tp, tz))));
866 }
867 
868 TEST(BreakTime, LocalTimeInNewYork) {
869  const time_zone tz = LoadZone("America/New_York");
870  const auto tp = chrono::system_clock::from_time_t(45);
871  ExpectTime(tp, tz, 1969, 12, 31, 19, 0, 45, -5 * 60 * 60, false, "EST");
872  EXPECT_EQ(weekday::wednesday, get_weekday(civil_day(convert(tp, tz))));
873 }
874 
875 TEST(BreakTime, LocalTimeInMTV) {
876  const time_zone tz = LoadZone("America/Los_Angeles");
877  const auto tp = chrono::system_clock::from_time_t(1380855729);
878  ExpectTime(tp, tz, 2013, 10, 3, 20, 2, 9, -7 * 60 * 60, true, "PDT");
879  EXPECT_EQ(weekday::thursday, get_weekday(civil_day(convert(tp, tz))));
880 }
881 
882 TEST(BreakTime, LocalTimeInSydney) {
883  const time_zone tz = LoadZone("Australia/Sydney");
884  const auto tp = chrono::system_clock::from_time_t(90);
885  ExpectTime(tp, tz, 1970, 1, 1, 10, 1, 30, 10 * 60 * 60, false, "AEST");
886  EXPECT_EQ(weekday::thursday, get_weekday(civil_day(convert(tp, tz))));
887 }
888 
889 TEST(MakeTime, TimePointResolution) {
890  const time_zone utc = utc_time_zone();
891  const time_point<chrono::nanoseconds> tp_ns =
892  convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
893  EXPECT_EQ("04:05", format("%M:%E*S", tp_ns, utc));
895  convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
896  EXPECT_EQ("04:05", format("%M:%E*S", tp_us, utc));
898  convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
899  EXPECT_EQ("04:05", format("%M:%E*S", tp_ms, utc));
900  const time_point<chrono::seconds> tp_s =
901  convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
902  EXPECT_EQ("04:05", format("%M:%E*S", tp_s, utc));
904  convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
905  EXPECT_EQ("04:05", format("%M:%E*S", tp_s64, utc));
906 
907  // These next two require chrono::time_point_cast because the conversion
908  // from a resolution of seconds (the return value of convert()) to a
909  // coarser resolution requires an explicit cast.
910  const time_point<chrono::minutes> tp_m =
911  chrono::time_point_cast<chrono::minutes>(
912  convert(civil_second(2015, 1, 2, 3, 4, 5), utc));
913  EXPECT_EQ("04:00", format("%M:%E*S", tp_m, utc));
914  const time_point<chrono::hours> tp_h =
915  chrono::time_point_cast<chrono::hours>(
916  convert(civil_second(2015, 1, 2, 3, 4, 5), utc));
917  EXPECT_EQ("00:00", format("%M:%E*S", tp_h, utc));
918 }
919 
920 TEST(MakeTime, Normalization) {
921  const time_zone tz = LoadZone("America/New_York");
922  const auto tp = convert(civil_second(2009, 2, 13, 18, 31, 30), tz);
923  EXPECT_EQ(chrono::system_clock::from_time_t(1234567890), tp);
924 
925  // Now requests for the same time_point but with out-of-range fields.
926  EXPECT_EQ(tp, convert(civil_second(2008, 14, 13, 18, 31, 30), tz)); // month
927  EXPECT_EQ(tp, convert(civil_second(2009, 1, 44, 18, 31, 30), tz)); // day
928  EXPECT_EQ(tp, convert(civil_second(2009, 2, 12, 42, 31, 30), tz)); // hour
929  EXPECT_EQ(tp, convert(civil_second(2009, 2, 13, 17, 91, 30), tz)); // minute
930  EXPECT_EQ(tp, convert(civil_second(2009, 2, 13, 18, 30, 90), tz)); // second
931 }
932 
933 // NOTE: Run this with -ftrapv to detect overflow problems.
934 TEST(MakeTime, SysSecondsLimits) {
935  const char RFC3339[] = "%Y-%m-%dT%H:%M:%S%Ez";
936  const time_zone utc = utc_time_zone();
937  const time_zone east = fixed_time_zone(chrono::hours(14));
938  const time_zone west = fixed_time_zone(-chrono::hours(14));
940 
941  // Approach the maximal time_point<cctz::seconds> value from below.
942  tp = convert(civil_second(292277026596, 12, 4, 15, 30, 6), utc);
943  EXPECT_EQ("292277026596-12-04T15:30:06+00:00", format(RFC3339, tp, utc));
944  tp = convert(civil_second(292277026596, 12, 4, 15, 30, 7), utc);
945  EXPECT_EQ("292277026596-12-04T15:30:07+00:00", format(RFC3339, tp, utc));
947  tp = convert(civil_second(292277026596, 12, 4, 15, 30, 8), utc);
949  tp = convert(civil_second::max(), utc);
951 
952  // Checks that we can also get the maximal value for a far-east zone.
953  tp = convert(civil_second(292277026596, 12, 5, 5, 30, 7), east);
954  EXPECT_EQ("292277026596-12-05T05:30:07+14:00", format(RFC3339, tp, east));
956  tp = convert(civil_second(292277026596, 12, 5, 5, 30, 8), east);
958  tp = convert(civil_second::max(), east);
960 
961  // Checks that we can also get the maximal value for a far-west zone.
962  tp = convert(civil_second(292277026596, 12, 4, 1, 30, 7), west);
963  EXPECT_EQ("292277026596-12-04T01:30:07-14:00", format(RFC3339, tp, west));
965  tp = convert(civil_second(292277026596, 12, 4, 7, 30, 8), west);
967  tp = convert(civil_second::max(), west);
969 
970  // Approach the minimal time_point<cctz::seconds> value from above.
971  tp = convert(civil_second(-292277022657, 1, 27, 8, 29, 53), utc);
972  EXPECT_EQ("-292277022657-01-27T08:29:53+00:00", format(RFC3339, tp, utc));
973  tp = convert(civil_second(-292277022657, 1, 27, 8, 29, 52), utc);
974  EXPECT_EQ("-292277022657-01-27T08:29:52+00:00", format(RFC3339, tp, utc));
976  tp = convert(civil_second(-292277022657, 1, 27, 8, 29, 51), utc);
978  tp = convert(civil_second::min(), utc);
980 
981  // Checks that we can also get the minimal value for a far-east zone.
982  tp = convert(civil_second(-292277022657, 1, 27, 22, 29, 52), east);
983  EXPECT_EQ("-292277022657-01-27T22:29:52+14:00", format(RFC3339, tp, east));
985  tp = convert(civil_second(-292277022657, 1, 27, 22, 29, 51), east);
987  tp = convert(civil_second::min(), east);
989 
990  // Checks that we can also get the minimal value for a far-west zone.
991  tp = convert(civil_second(-292277022657, 1, 26, 18, 29, 52), west);
992  EXPECT_EQ("-292277022657-01-26T18:29:52-14:00", format(RFC3339, tp, west));
994  tp = convert(civil_second(-292277022657, 1, 26, 18, 29, 51), west);
996  tp = convert(civil_second::min(), west);
998 
999  // Some similar checks for the "libc" time-zone implementation.
1000  if (sizeof(std::time_t) >= 8) {
1001  // Checks that "tm_year + 1900", as used by the "libc" implementation,
1002  // can produce year values beyond the range on an int without overflow.
1003 #if defined(_WIN32) || defined(_WIN64)
1004  // localtime_s() and gmtime_s() don't believe in years outside [1970:3000].
1005 #else
1006  const time_zone utc = LoadZone("libc:UTC");
1007  const year_t max_tm_year = year_t{std::numeric_limits<int>::max()} + 1900;
1008  tp = convert(civil_second(max_tm_year, 12, 31, 23, 59, 59), utc);
1009  EXPECT_EQ("2147485547-12-31T23:59:59+00:00", format(RFC3339, tp, utc));
1010  const year_t min_tm_year = year_t{std::numeric_limits<int>::min()} + 1900;
1011  tp = convert(civil_second(min_tm_year, 1, 1, 0, 0, 0), utc);
1012  EXPECT_EQ("-2147481748-01-01T00:00:00+00:00", format(RFC3339, tp, utc));
1013 #endif
1014  }
1015 }
1016 
1017 TEST(MakeTime, LocalTimeLibC) {
1018  // Checks that cctz and libc agree on transition points in [1970:2037].
1019  //
1020  // We limit this test case to environments where:
1021  // 1) we know how to change the time zone used by localtime()/mktime(),
1022  // 2) cctz and localtime()/mktime() will use similar-enough tzdata, and
1023  // 3) we have some idea about how mktime() behaves during transitions.
1024 #if defined(__linux__) && !defined(__ANDROID__)
1025  const char* const ep = getenv("TZ");
1026  std::string tz_name = (ep != nullptr) ? ep : "";
1027  for (const char* const* np = kTimeZoneNames; *np != nullptr; ++np) {
1028  ASSERT_EQ(0, setenv("TZ", *np, 1)); // change what "localtime" means
1029  const auto zi = local_time_zone();
1030  const auto lc = LoadZone("libc:localtime");
1032  for (auto tp = zi.lookup(civil_second()).trans;
1033  zi.next_transition(tp, &trans);
1034  tp = zi.lookup(trans.to).trans) {
1035  const auto fcl = zi.lookup(trans.from);
1036  const auto tcl = zi.lookup(trans.to);
1037  civil_second cs; // compare cs in zi and lc
1038  if (fcl.kind == time_zone::civil_lookup::UNIQUE) {
1039  if (tcl.kind == time_zone::civil_lookup::UNIQUE) {
1040  // Both unique; must be an is_dst or abbr change.
1041  ASSERT_EQ(trans.from, trans.to);
1042  const auto trans = fcl.trans;
1043  const auto tal = zi.lookup(trans);
1044  const auto tprev = trans - absl::time_internal::cctz::seconds(1);
1045  const auto pal = zi.lookup(tprev);
1046  if (pal.is_dst == tal.is_dst) {
1047  ASSERT_STRNE(pal.abbr, tal.abbr);
1048  }
1049  continue;
1050  }
1051  ASSERT_EQ(time_zone::civil_lookup::REPEATED, tcl.kind);
1052  cs = trans.to;
1053  } else {
1054  ASSERT_EQ(time_zone::civil_lookup::UNIQUE, tcl.kind);
1055  ASSERT_EQ(time_zone::civil_lookup::SKIPPED, fcl.kind);
1056  cs = trans.from;
1057  }
1058  if (cs.year() > 2037) break; // limit test time (and to 32-bit time_t)
1059  const auto cl_zi = zi.lookup(cs);
1060  if (zi.lookup(cl_zi.pre).is_dst == zi.lookup(cl_zi.post).is_dst) {
1061  // The "libc" implementation cannot correctly classify transitions
1062  // that don't change the "tm_isdst" flag. In Europe/Volgograd, for
1063  // example, there is a SKIPPED transition from +03 to +04 with dst=F
1064  // on both sides ...
1065  // 1540681199 = 2018-10-28 01:59:59 +03:00:00 [dst=F off=10800]
1066  // 1540681200 = 2018-10-28 03:00:00 +04:00:00 [dst=F off=14400]
1067  // but std::mktime(2018-10-28 02:00:00, tm_isdst=0) fails, unlike,
1068  // say, the similar Europe/Chisinau transition from +02 to +03 ...
1069  // 1521935999 = 2018-03-25 01:59:59 +02:00:00 [dst=F off=7200]
1070  // 1521936000 = 2018-03-25 03:00:00 +03:00:00 [dst=T off=10800]
1071  // where std::mktime(2018-03-25 02:00:00, tm_isdst=0) succeeds and
1072  // returns 1521936000.
1073  continue;
1074  }
1075  if (cs == civil_second(2037, 10, 4, 2, 0, 0)) {
1076  const std::string tzname = *np;
1077  if (tzname == "Africa/Casablanca" || tzname == "Africa/El_Aaiun") {
1078  // The "libc" implementation gets this transition wrong (at least
1079  // until 2018g when it was removed), returning an offset of 3600
1080  // instead of 0. TODO: Revert this when 2018g is ubiquitous.
1081  continue;
1082  }
1083  }
1084  const auto cl_lc = lc.lookup(cs);
1085  SCOPED_TRACE(testing::Message() << "For " << cs << " in " << *np);
1086  EXPECT_EQ(cl_zi.kind, cl_lc.kind);
1087  EXPECT_EQ(cl_zi.pre, cl_lc.pre);
1088  EXPECT_EQ(cl_zi.trans, cl_lc.trans);
1089  EXPECT_EQ(cl_zi.post, cl_lc.post);
1090  }
1091  }
1092  if (ep == nullptr) {
1093  ASSERT_EQ(0, unsetenv("TZ"));
1094  } else {
1095  ASSERT_EQ(0, setenv("TZ", tz_name.c_str(), 1));
1096  }
1097 #endif
1098 }
1099 
1100 TEST(NextTransition, UTC) {
1101  const auto tz = utc_time_zone();
1103 
1105  EXPECT_FALSE(tz.next_transition(tp, &trans));
1106 
1108  EXPECT_FALSE(tz.next_transition(tp, &trans));
1109 }
1110 
1111 TEST(PrevTransition, UTC) {
1112  const auto tz = utc_time_zone();
1114 
1116  EXPECT_FALSE(tz.prev_transition(tp, &trans));
1117 
1119  EXPECT_FALSE(tz.prev_transition(tp, &trans));
1120 }
1121 
1122 TEST(NextTransition, AmericaNewYork) {
1123  const auto tz = LoadZone("America/New_York");
1125 
1126  auto tp = convert(civil_second(2018, 6, 30, 0, 0, 0), tz);
1127  EXPECT_TRUE(tz.next_transition(tp, &trans));
1128  EXPECT_EQ(civil_second(2018, 11, 4, 2, 0, 0), trans.from);
1129  EXPECT_EQ(civil_second(2018, 11, 4, 1, 0, 0), trans.to);
1130 
1132  EXPECT_FALSE(tz.next_transition(tp, &trans));
1133 
1135  EXPECT_TRUE(tz.next_transition(tp, &trans));
1136  if (trans.from == civil_second(1918, 3, 31, 2, 0, 0)) {
1137  // It looks like the tzdata is only 32 bit (probably macOS),
1138  // which bottoms out at 1901-12-13T20:45:52+00:00.
1139  EXPECT_EQ(civil_second(1918, 3, 31, 3, 0, 0), trans.to);
1140  } else {
1141  EXPECT_EQ(civil_second(1883, 11, 18, 12, 3, 58), trans.from);
1142  EXPECT_EQ(civil_second(1883, 11, 18, 12, 0, 0), trans.to);
1143  }
1144 }
1145 
1146 TEST(PrevTransition, AmericaNewYork) {
1147  const auto tz = LoadZone("America/New_York");
1149 
1150  auto tp = convert(civil_second(2018, 6, 30, 0, 0, 0), tz);
1151  EXPECT_TRUE(tz.prev_transition(tp, &trans));
1152  EXPECT_EQ(civil_second(2018, 3, 11, 2, 0, 0), trans.from);
1153  EXPECT_EQ(civil_second(2018, 3, 11, 3, 0, 0), trans.to);
1154 
1156  EXPECT_FALSE(tz.prev_transition(tp, &trans));
1157 
1159  EXPECT_TRUE(tz.prev_transition(tp, &trans));
1160  // We have a transition but we don't know which one.
1161 }
1162 
1163 TEST(TimeZoneEdgeCase, AmericaNewYork) {
1164  const time_zone tz = LoadZone("America/New_York");
1165 
1166  // Spring 1:59:59 -> 3:00:00
1167  auto tp = convert(civil_second(2013, 3, 10, 1, 59, 59), tz);
1168  ExpectTime(tp, tz, 2013, 3, 10, 1, 59, 59, -5 * 3600, false, "EST");
1170  ExpectTime(tp, tz, 2013, 3, 10, 3, 0, 0, -4 * 3600, true, "EDT");
1171 
1172  // Fall 1:59:59 -> 1:00:00
1173  tp = convert(civil_second(2013, 11, 3, 1, 59, 59), tz);
1174  ExpectTime(tp, tz, 2013, 11, 3, 1, 59, 59, -4 * 3600, true, "EDT");
1176  ExpectTime(tp, tz, 2013, 11, 3, 1, 0, 0, -5 * 3600, false, "EST");
1177 }
1178 
1179 TEST(TimeZoneEdgeCase, AmericaLosAngeles) {
1180  const time_zone tz = LoadZone("America/Los_Angeles");
1181 
1182  // Spring 1:59:59 -> 3:00:00
1183  auto tp = convert(civil_second(2013, 3, 10, 1, 59, 59), tz);
1184  ExpectTime(tp, tz, 2013, 3, 10, 1, 59, 59, -8 * 3600, false, "PST");
1186  ExpectTime(tp, tz, 2013, 3, 10, 3, 0, 0, -7 * 3600, true, "PDT");
1187 
1188  // Fall 1:59:59 -> 1:00:00
1189  tp = convert(civil_second(2013, 11, 3, 1, 59, 59), tz);
1190  ExpectTime(tp, tz, 2013, 11, 3, 1, 59, 59, -7 * 3600, true, "PDT");
1192  ExpectTime(tp, tz, 2013, 11, 3, 1, 0, 0, -8 * 3600, false, "PST");
1193 }
1194 
1195 TEST(TimeZoneEdgeCase, ArizonaNoTransition) {
1196  const time_zone tz = LoadZone("America/Phoenix");
1197 
1198  // No transition in Spring.
1199  auto tp = convert(civil_second(2013, 3, 10, 1, 59, 59), tz);
1200  ExpectTime(tp, tz, 2013, 3, 10, 1, 59, 59, -7 * 3600, false, "MST");
1202  ExpectTime(tp, tz, 2013, 3, 10, 2, 0, 0, -7 * 3600, false, "MST");
1203 
1204  // No transition in Fall.
1205  tp = convert(civil_second(2013, 11, 3, 1, 59, 59), tz);
1206  ExpectTime(tp, tz, 2013, 11, 3, 1, 59, 59, -7 * 3600, false, "MST");
1208  ExpectTime(tp, tz, 2013, 11, 3, 2, 0, 0, -7 * 3600, false, "MST");
1209 }
1210 
1211 TEST(TimeZoneEdgeCase, AsiaKathmandu) {
1212  const time_zone tz = LoadZone("Asia/Kathmandu");
1213 
1214  // A non-DST offset change from +0530 to +0545
1215  //
1216  // 504901799 == Tue, 31 Dec 1985 23:59:59 +0530 (+0530)
1217  // 504901800 == Wed, 1 Jan 1986 00:15:00 +0545 (+0545)
1218  auto tp = convert(civil_second(1985, 12, 31, 23, 59, 59), tz);
1219  ExpectTime(tp, tz, 1985, 12, 31, 23, 59, 59, 5.5 * 3600, false, "+0530");
1221  ExpectTime(tp, tz, 1986, 1, 1, 0, 15, 0, 5.75 * 3600, false, "+0545");
1222 }
1223 
1224 TEST(TimeZoneEdgeCase, PacificChatham) {
1225  const time_zone tz = LoadZone("Pacific/Chatham");
1226 
1227  // One-hour DST offset changes, but at atypical values
1228  //
1229  // 1365256799 == Sun, 7 Apr 2013 03:44:59 +1345 (+1345)
1230  // 1365256800 == Sun, 7 Apr 2013 02:45:00 +1245 (+1245)
1231  auto tp = convert(civil_second(2013, 4, 7, 3, 44, 59), tz);
1232  ExpectTime(tp, tz, 2013, 4, 7, 3, 44, 59, 13.75 * 3600, true, "+1345");
1234  ExpectTime(tp, tz, 2013, 4, 7, 2, 45, 0, 12.75 * 3600, false, "+1245");
1235 
1236  // 1380376799 == Sun, 29 Sep 2013 02:44:59 +1245 (+1245)
1237  // 1380376800 == Sun, 29 Sep 2013 03:45:00 +1345 (+1345)
1238  tp = convert(civil_second(2013, 9, 29, 2, 44, 59), tz);
1239  ExpectTime(tp, tz, 2013, 9, 29, 2, 44, 59, 12.75 * 3600, false, "+1245");
1241  ExpectTime(tp, tz, 2013, 9, 29, 3, 45, 0, 13.75 * 3600, true, "+1345");
1242 }
1243 
1244 TEST(TimeZoneEdgeCase, AustraliaLordHowe) {
1245  const time_zone tz = LoadZone("Australia/Lord_Howe");
1246 
1247  // Half-hour DST offset changes
1248  //
1249  // 1365260399 == Sun, 7 Apr 2013 01:59:59 +1100 (+11)
1250  // 1365260400 == Sun, 7 Apr 2013 01:30:00 +1030 (+1030)
1251  auto tp = convert(civil_second(2013, 4, 7, 1, 59, 59), tz);
1252  ExpectTime(tp, tz, 2013, 4, 7, 1, 59, 59, 11 * 3600, true, "+11");
1254  ExpectTime(tp, tz, 2013, 4, 7, 1, 30, 0, 10.5 * 3600, false, "+1030");
1255 
1256  // 1380986999 == Sun, 6 Oct 2013 01:59:59 +1030 (+1030)
1257  // 1380987000 == Sun, 6 Oct 2013 02:30:00 +1100 (+11)
1258  tp = convert(civil_second(2013, 10, 6, 1, 59, 59), tz);
1259  ExpectTime(tp, tz, 2013, 10, 6, 1, 59, 59, 10.5 * 3600, false, "+1030");
1261  ExpectTime(tp, tz, 2013, 10, 6, 2, 30, 0, 11 * 3600, true, "+11");
1262 }
1263 
1264 TEST(TimeZoneEdgeCase, PacificApia) {
1265  const time_zone tz = LoadZone("Pacific/Apia");
1266 
1267  // At the end of December 2011, Samoa jumped forward by one day,
1268  // skipping 30 December from the local calendar, when the nation
1269  // moved to the west of the International Date Line.
1270  //
1271  // A one-day, non-DST offset change
1272  //
1273  // 1325239199 == Thu, 29 Dec 2011 23:59:59 -1000 (-10)
1274  // 1325239200 == Sat, 31 Dec 2011 00:00:00 +1400 (+14)
1275  auto tp = convert(civil_second(2011, 12, 29, 23, 59, 59), tz);
1276  ExpectTime(tp, tz, 2011, 12, 29, 23, 59, 59, -10 * 3600, true, "-10");
1277  EXPECT_EQ(363, get_yearday(civil_day(convert(tp, tz))));
1279  ExpectTime(tp, tz, 2011, 12, 31, 0, 0, 0, 14 * 3600, true, "+14");
1280  EXPECT_EQ(365, get_yearday(civil_day(convert(tp, tz))));
1281 }
1282 
1283 TEST(TimeZoneEdgeCase, AfricaCairo) {
1284  const time_zone tz = LoadZone("Africa/Cairo");
1285 
1286  if (VersionCmp(tz, "2014c") >= 0) {
1287  // An interesting case of midnight not existing.
1288  //
1289  // 1400191199 == Thu, 15 May 2014 23:59:59 +0200 (EET)
1290  // 1400191200 == Fri, 16 May 2014 01:00:00 +0300 (EEST)
1291  auto tp = convert(civil_second(2014, 5, 15, 23, 59, 59), tz);
1292  ExpectTime(tp, tz, 2014, 5, 15, 23, 59, 59, 2 * 3600, false, "EET");
1294  ExpectTime(tp, tz, 2014, 5, 16, 1, 0, 0, 3 * 3600, true, "EEST");
1295  }
1296 }
1297 
1298 TEST(TimeZoneEdgeCase, AfricaMonrovia) {
1299  const time_zone tz = LoadZone("Africa/Monrovia");
1300 
1301  if (VersionCmp(tz, "2017b") >= 0) {
1302  // Strange offset change -00:44:30 -> +00:00:00 (non-DST)
1303  //
1304  // 63593069 == Thu, 6 Jan 1972 23:59:59 -0044 (MMT)
1305  // 63593070 == Fri, 7 Jan 1972 00:44:30 +0000 (GMT)
1306  auto tp = convert(civil_second(1972, 1, 6, 23, 59, 59), tz);
1307  ExpectTime(tp, tz, 1972, 1, 6, 23, 59, 59, -44.5 * 60, false, "MMT");
1309  ExpectTime(tp, tz, 1972, 1, 7, 0, 44, 30, 0 * 60, false, "GMT");
1310  }
1311 }
1312 
1313 TEST(TimeZoneEdgeCase, AmericaJamaica) {
1314  // Jamaica discontinued DST transitions in 1983, and is now at a
1315  // constant -0500. This makes it an interesting edge-case target.
1316  // Note that the 32-bit times used in a (tzh_version == 0) zoneinfo
1317  // file cannot represent the abbreviation-only transition of 1890,
1318  // so we ignore the abbreviation by expecting what we received.
1319  const time_zone tz = LoadZone("America/Jamaica");
1320 
1321  // Before the first transition.
1322  if (!tz.version().empty() && VersionCmp(tz, "2018d") >= 0) {
1323  // We avoid the expectations on the -18430 offset below unless we are
1324  // certain we have commit 907241e (Fix off-by-1 error for Jamaica and
1325  // T&C before 1913) from 2018d. TODO: Remove the "version() not empty"
1326  // part when 2018d is generally available from /usr/share/zoneinfo.
1327  auto tp = convert(civil_second(1889, 12, 31, 0, 0, 0), tz);
1328  ExpectTime(tp, tz, 1889, 12, 31, 0, 0, 0, -18430, false,
1329  tz.lookup(tp).abbr);
1330 
1331  // Over the first (abbreviation-change only) transition.
1332  // -2524503170 == Tue, 31 Dec 1889 23:59:59 -0507 (LMT)
1333  // -2524503169 == Wed, 1 Jan 1890 00:00:00 -0507 (KMT)
1334  tp = convert(civil_second(1889, 12, 31, 23, 59, 59), tz);
1335  ExpectTime(tp, tz, 1889, 12, 31, 23, 59, 59, -18430, false,
1336  tz.lookup(tp).abbr);
1338  ExpectTime(tp, tz, 1890, 1, 1, 0, 0, 0, -18430, false, "KMT");
1339  }
1340 
1341  // Over the last (DST) transition.
1342  // 436341599 == Sun, 30 Oct 1983 01:59:59 -0400 (EDT)
1343  // 436341600 == Sun, 30 Oct 1983 01:00:00 -0500 (EST)
1344  auto tp = convert(civil_second(1983, 10, 30, 1, 59, 59), tz);
1345  ExpectTime(tp, tz, 1983, 10, 30, 1, 59, 59, -4 * 3600, true, "EDT");
1347  ExpectTime(tp, tz, 1983, 10, 30, 1, 0, 0, -5 * 3600, false, "EST");
1348 
1349  // After the last transition.
1350  tp = convert(civil_second(1983, 12, 31, 23, 59, 59), tz);
1351  ExpectTime(tp, tz, 1983, 12, 31, 23, 59, 59, -5 * 3600, false, "EST");
1352 }
1353 
1354 TEST(TimeZoneEdgeCase, WET) {
1355  // Cover some non-existent times within forward transitions.
1356  const time_zone tz = LoadZone("WET");
1357 
1358  // Before the first transition.
1359  auto tp = convert(civil_second(1977, 1, 1, 0, 0, 0), tz);
1360  ExpectTime(tp, tz, 1977, 1, 1, 0, 0, 0, 0, false, "WET");
1361 
1362  // Over the first transition.
1363  // 228877199 == Sun, 3 Apr 1977 00:59:59 +0000 (WET)
1364  // 228877200 == Sun, 3 Apr 1977 02:00:00 +0100 (WEST)
1365  tp = convert(civil_second(1977, 4, 3, 0, 59, 59), tz);
1366  ExpectTime(tp, tz, 1977, 4, 3, 0, 59, 59, 0, false, "WET");
1368  ExpectTime(tp, tz, 1977, 4, 3, 2, 0, 0, 1 * 3600, true, "WEST");
1369 
1370  // A non-existent time within the first transition.
1371  time_zone::civil_lookup cl1 = tz.lookup(civil_second(1977, 4, 3, 1, 15, 0));
1372  EXPECT_EQ(time_zone::civil_lookup::SKIPPED, cl1.kind);
1373  ExpectTime(cl1.pre, tz, 1977, 4, 3, 2, 15, 0, 1 * 3600, true, "WEST");
1374  ExpectTime(cl1.trans, tz, 1977, 4, 3, 2, 0, 0, 1 * 3600, true, "WEST");
1375  ExpectTime(cl1.post, tz, 1977, 4, 3, 0, 15, 0, 0 * 3600, false, "WET");
1376 
1377  // A non-existent time within the second forward transition.
1378  time_zone::civil_lookup cl2 = tz.lookup(civil_second(1978, 4, 2, 1, 15, 0));
1379  EXPECT_EQ(time_zone::civil_lookup::SKIPPED, cl2.kind);
1380  ExpectTime(cl2.pre, tz, 1978, 4, 2, 2, 15, 0, 1 * 3600, true, "WEST");
1381  ExpectTime(cl2.trans, tz, 1978, 4, 2, 2, 0, 0, 1 * 3600, true, "WEST");
1382  ExpectTime(cl2.post, tz, 1978, 4, 2, 0, 15, 0, 0 * 3600, false, "WET");
1383 }
1384 
1385 TEST(TimeZoneEdgeCase, FixedOffsets) {
1386  const time_zone gmtm5 = LoadZone("Etc/GMT+5"); // -0500
1387  auto tp = convert(civil_second(1970, 1, 1, 0, 0, 0), gmtm5);
1388  ExpectTime(tp, gmtm5, 1970, 1, 1, 0, 0, 0, -5 * 3600, false, "-05");
1389  EXPECT_EQ(chrono::system_clock::from_time_t(5 * 3600), tp);
1390 
1391  const time_zone gmtp5 = LoadZone("Etc/GMT-5"); // +0500
1392  tp = convert(civil_second(1970, 1, 1, 0, 0, 0), gmtp5);
1393  ExpectTime(tp, gmtp5, 1970, 1, 1, 0, 0, 0, 5 * 3600, false, "+05");
1394  EXPECT_EQ(chrono::system_clock::from_time_t(-5 * 3600), tp);
1395 }
1396 
1397 TEST(TimeZoneEdgeCase, NegativeYear) {
1398  // Tests transition from year 0 (aka 1BCE) to year -1.
1399  const time_zone tz = utc_time_zone();
1400  auto tp = convert(civil_second(0, 1, 1, 0, 0, 0), tz);
1401  ExpectTime(tp, tz, 0, 1, 1, 0, 0, 0, 0 * 3600, false, "UTC");
1402  EXPECT_EQ(weekday::saturday, get_weekday(civil_day(convert(tp, tz))));
1404  ExpectTime(tp, tz, -1, 12, 31, 23, 59, 59, 0 * 3600, false, "UTC");
1405  EXPECT_EQ(weekday::friday, get_weekday(civil_day(convert(tp, tz))));
1406 }
1407 
1408 TEST(TimeZoneEdgeCase, UTC32bitLimit) {
1409  const time_zone tz = utc_time_zone();
1410 
1411  // Limits of signed 32-bit time_t
1412  //
1413  // 2147483647 == Tue, 19 Jan 2038 03:14:07 +0000 (UTC)
1414  // 2147483648 == Tue, 19 Jan 2038 03:14:08 +0000 (UTC)
1415  auto tp = convert(civil_second(2038, 1, 19, 3, 14, 7), tz);
1416  ExpectTime(tp, tz, 2038, 1, 19, 3, 14, 7, 0 * 3600, false, "UTC");
1418  ExpectTime(tp, tz, 2038, 1, 19, 3, 14, 8, 0 * 3600, false, "UTC");
1419 }
1420 
1421 TEST(TimeZoneEdgeCase, UTC5DigitYear) {
1422  const time_zone tz = utc_time_zone();
1423 
1424  // Rollover to 5-digit year
1425  //
1426  // 253402300799 == Fri, 31 Dec 9999 23:59:59 +0000 (UTC)
1427  // 253402300800 == Sat, 1 Jan 1000 00:00:00 +0000 (UTC)
1428  auto tp = convert(civil_second(9999, 12, 31, 23, 59, 59), tz);
1429  ExpectTime(tp, tz, 9999, 12, 31, 23, 59, 59, 0 * 3600, false, "UTC");
1431  ExpectTime(tp, tz, 10000, 1, 1, 0, 0, 0, 0 * 3600, false, "UTC");
1432 }
1433 
1434 } // namespace cctz
1435 } // namespace time_internal
1436 } // namespace absl
CONSTEXPR_F int get_yearday(const civil_day &cd) noexcept
#define ExpectTime(tp, tz, y, m, d, hh, mm, ss, off, isdst, zone)
time_zone fixed_time_zone(const seconds &offset)
uint32_t version
Definition: graphcycles.cc:278
std::string format(const std::string &fmt, const time_point< D > &tp, const time_zone &tz)
Definition: time_zone.h:315
CONSTEXPR_F weekday get_weekday(const civil_day &cd) noexcept
std::chrono::duration< std::int_fast64_t > seconds
Definition: time_zone.h:37
Definition: algorithm.h:29
static CONSTEXPR_F civil_time() max()
static CONSTEXPR_F civil_time() min()
CONSTEXPR_M year_t year() const noexcept
absolute_lookup lookup(const time_point< seconds > &tp) const
char name[1]
Definition: mutex.cc:296
enum absl::time_internal::cctz::time_zone::civil_lookup::civil_kind kind
std::chrono::time_point< std::chrono::system_clock, D > time_point
Definition: time_zone.h:36
uint64_t b
Definition: layout_test.cc:50
TEST(CivilTime, DefaultConstruction)
bool load_time_zone(const std::string &name, time_zone *tz)
civil_second convert(const time_point< D > &tp, const time_zone &tz)
Definition: time_zone.h:262


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