• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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 // Helper to return a loaded time zone by value (UTC on error).
LoadZone(const std::string & name)634 time_zone LoadZone(const std::string& name) {
635   time_zone tz;
636   load_time_zone(name, &tz);
637   return tz;
638 }
639 
640 // This helper is a macro so that failed expectations show up with the
641 // correct line numbers.
642 #define ExpectTime(tp, tz, y, m, d, hh, mm, ss, off, isdst, zone) \
643   do {                                                            \
644     time_zone::absolute_lookup al = tz.lookup(tp);                \
645     EXPECT_EQ(y, al.cs.year());                                   \
646     EXPECT_EQ(m, al.cs.month());                                  \
647     EXPECT_EQ(d, al.cs.day());                                    \
648     EXPECT_EQ(hh, al.cs.hour());                                  \
649     EXPECT_EQ(mm, al.cs.minute());                                \
650     EXPECT_EQ(ss, al.cs.second());                                \
651     EXPECT_EQ(off, al.offset);                                    \
652     EXPECT_TRUE(isdst == al.is_dst);                              \
653     /* EXPECT_STREQ(zone, al.abbr); */                            \
654   } while (0)
655 
656 // These tests sometimes run on platforms that have zoneinfo data so old
657 // that the transition we are attempting to check does not exist, most
658 // notably Android emulators.  Fortunately, AndroidZoneInfoSource supports
659 // time_zone::version() so, in cases where we've learned that it matters,
660 // we can make the check conditionally.
VersionCmp(time_zone tz,const std::string & target)661 int VersionCmp(time_zone tz, const std::string& target) {
662   std::string version = tz.version();
663   if (version.empty() && !target.empty()) return 1;  // unknown > known
664   return version.compare(target);
665 }
666 
667 }  // namespace
668 
669 #if !defined(__EMSCRIPTEN__)
TEST(TimeZones,LoadZonesConcurrently)670 TEST(TimeZones, LoadZonesConcurrently) {
671   std::promise<void> ready_promise;
672   std::shared_future<void> ready_future(ready_promise.get_future());
673   auto load_zones = [ready_future](std::promise<void>* started,
674                                    std::set<std::string>* failures) {
675     started->set_value();
676     ready_future.wait();
677     for (const char* const* np = kTimeZoneNames; *np != nullptr; ++np) {
678       std::string zone = *np;
679       time_zone tz;
680       if (load_time_zone(zone, &tz)) {
681         EXPECT_EQ(zone, tz.name());
682       } else {
683         failures->insert(zone);
684       }
685     }
686   };
687 
688   const std::size_t n_threads = 128;
689   std::vector<std::thread> threads;
690   std::vector<std::set<std::string>> thread_failures(n_threads);
691   for (std::size_t i = 0; i != n_threads; ++i) {
692     std::promise<void> started;
693     threads.emplace_back(load_zones, &started, &thread_failures[i]);
694     started.get_future().wait();
695   }
696   ready_promise.set_value();
697   for (auto& thread : threads) {
698     thread.join();
699   }
700 
701   // Allow a small number of failures to account for skew between
702   // the contents of kTimeZoneNames and the zoneinfo data source.
703 #if defined(__ANDROID__)
704   // Cater to the possibility of using an even older zoneinfo data
705   // source when running on Android, where it is difficult to override
706   // the bionic tzdata provided by the test environment.
707   const std::size_t max_failures = 20;
708 #else
709   const std::size_t max_failures = 3;
710 #endif
711   std::set<std::string> failures;
712   for (const auto& thread_failure : thread_failures) {
713     failures.insert(thread_failure.begin(), thread_failure.end());
714   }
715   EXPECT_LE(failures.size(), max_failures) << testing::PrintToString(failures);
716 }
717 #endif
718 
TEST(TimeZone,NamedTimeZones)719 TEST(TimeZone, NamedTimeZones) {
720   const time_zone utc = utc_time_zone();
721   EXPECT_EQ("UTC", utc.name());
722   const time_zone nyc = LoadZone("America/New_York");
723   EXPECT_EQ("America/New_York", nyc.name());
724   const time_zone syd = LoadZone("Australia/Sydney");
725   EXPECT_EQ("Australia/Sydney", syd.name());
726   const time_zone fixed0 =
727       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 
TEST(TimeZone,Failures)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 
TEST(TimeZone,Equality)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 =
771       fixed_time_zone(absl::time_internal::cctz::seconds::zero());
772   EXPECT_EQ(fixed_zero, LoadZone(fixed_zero.name()));
773   EXPECT_EQ(fixed_zero, explicit_utc);
774 
775   const time_zone fixed_utc = LoadZone("Fixed/UTC+00:00:00");
776   EXPECT_EQ(fixed_utc, LoadZone(fixed_utc.name()));
777   EXPECT_EQ(fixed_utc, explicit_utc);
778 
779   const time_zone fixed_pos = fixed_time_zone(
780       chrono::hours(3) + chrono::minutes(25) + chrono::seconds(45));
781   EXPECT_EQ(fixed_pos, LoadZone(fixed_pos.name()));
782   EXPECT_NE(fixed_pos, explicit_utc);
783   const time_zone fixed_neg = fixed_time_zone(
784       -(chrono::hours(12) + chrono::minutes(34) + chrono::seconds(56)));
785   EXPECT_EQ(fixed_neg, LoadZone(fixed_neg.name()));
786   EXPECT_NE(fixed_neg, explicit_utc);
787 
788   const time_zone fixed_lim = fixed_time_zone(chrono::hours(24));
789   EXPECT_EQ(fixed_lim, LoadZone(fixed_lim.name()));
790   EXPECT_NE(fixed_lim, explicit_utc);
791   const time_zone fixed_ovfl =
792       fixed_time_zone(chrono::hours(24) + chrono::seconds(1));
793   EXPECT_EQ(fixed_ovfl, LoadZone(fixed_ovfl.name()));
794   EXPECT_EQ(fixed_ovfl, explicit_utc);
795 
796   EXPECT_EQ(fixed_time_zone(chrono::seconds(1)),
797             fixed_time_zone(chrono::seconds(1)));
798 
799   const time_zone local = local_time_zone();
800   EXPECT_EQ(local, LoadZone(local.name()));
801 
802   time_zone la = LoadZone("America/Los_Angeles");
803   time_zone nyc = LoadZone("America/New_York");
804   EXPECT_NE(la, nyc);
805 }
806 
TEST(StdChronoTimePoint,TimeTAlignment)807 TEST(StdChronoTimePoint, TimeTAlignment) {
808   // Ensures that the Unix epoch and the system clock epoch are an integral
809   // number of seconds apart. This simplifies conversions to/from time_t.
810   auto diff =
811       chrono::system_clock::time_point() - chrono::system_clock::from_time_t(0);
812   EXPECT_EQ(chrono::system_clock::time_point::duration::zero(),
813             diff % chrono::seconds(1));
814 }
815 
TEST(BreakTime,TimePointResolution)816 TEST(BreakTime, TimePointResolution) {
817   const time_zone utc = utc_time_zone();
818   const auto t0 = chrono::system_clock::from_time_t(0);
819 
820   ExpectTime(chrono::time_point_cast<chrono::nanoseconds>(t0), utc, 1970, 1, 1,
821              0, 0, 0, 0, false, "UTC");
822   ExpectTime(chrono::time_point_cast<chrono::microseconds>(t0), utc, 1970, 1, 1,
823              0, 0, 0, 0, false, "UTC");
824   ExpectTime(chrono::time_point_cast<chrono::milliseconds>(t0), utc, 1970, 1, 1,
825              0, 0, 0, 0, false, "UTC");
826   ExpectTime(chrono::time_point_cast<chrono::seconds>(t0), utc, 1970, 1, 1, 0,
827              0, 0, 0, false, "UTC");
828   ExpectTime(chrono::time_point_cast<absl::time_internal::cctz::seconds>(t0),
829              utc, 1970, 1, 1, 0, 0, 0, 0, false, "UTC");
830   ExpectTime(chrono::time_point_cast<chrono::minutes>(t0), utc, 1970, 1, 1, 0,
831              0, 0, 0, false, "UTC");
832   ExpectTime(chrono::time_point_cast<chrono::hours>(t0), utc, 1970, 1, 1, 0, 0,
833              0, 0, false, "UTC");
834 }
835 
TEST(BreakTime,LocalTimeInUTC)836 TEST(BreakTime, LocalTimeInUTC) {
837   const time_zone tz = utc_time_zone();
838   const auto tp = chrono::system_clock::from_time_t(0);
839   ExpectTime(tp, tz, 1970, 1, 1, 0, 0, 0, 0, false, "UTC");
840   EXPECT_EQ(weekday::thursday, get_weekday(convert(tp, tz)));
841 }
842 
TEST(BreakTime,LocalTimeInUTCUnaligned)843 TEST(BreakTime, LocalTimeInUTCUnaligned) {
844   const time_zone tz = utc_time_zone();
845   const auto tp =
846       chrono::system_clock::from_time_t(0) - chrono::milliseconds(500);
847   ExpectTime(tp, tz, 1969, 12, 31, 23, 59, 59, 0, false, "UTC");
848   EXPECT_EQ(weekday::wednesday, get_weekday(convert(tp, tz)));
849 }
850 
TEST(BreakTime,LocalTimePosix)851 TEST(BreakTime, LocalTimePosix) {
852   // See IEEE Std 1003.1-1988 B.2.3 General Terms, Epoch.
853   const time_zone tz = utc_time_zone();
854   const auto tp = chrono::system_clock::from_time_t(536457599);
855   ExpectTime(tp, tz, 1986, 12, 31, 23, 59, 59, 0, false, "UTC");
856   EXPECT_EQ(weekday::wednesday, get_weekday(convert(tp, tz)));
857 }
858 
TEST(TimeZoneImpl,LocalTimeInFixed)859 TEST(TimeZoneImpl, LocalTimeInFixed) {
860   const absl::time_internal::cctz::seconds offset =
861       -(chrono::hours(8) + chrono::minutes(33) + chrono::seconds(47));
862   const time_zone tz = fixed_time_zone(offset);
863   const auto tp = chrono::system_clock::from_time_t(0);
864   ExpectTime(tp, tz, 1969, 12, 31, 15, 26, 13, offset.count(), false,
865              "-083347");
866   EXPECT_EQ(weekday::wednesday, get_weekday(convert(tp, tz)));
867 }
868 
TEST(BreakTime,LocalTimeInNewYork)869 TEST(BreakTime, LocalTimeInNewYork) {
870   const time_zone tz = LoadZone("America/New_York");
871   const auto tp = chrono::system_clock::from_time_t(45);
872   ExpectTime(tp, tz, 1969, 12, 31, 19, 0, 45, -5 * 60 * 60, false, "EST");
873   EXPECT_EQ(weekday::wednesday, get_weekday(convert(tp, tz)));
874 }
875 
TEST(BreakTime,LocalTimeInMTV)876 TEST(BreakTime, LocalTimeInMTV) {
877   const time_zone tz = LoadZone("America/Los_Angeles");
878   const auto tp = chrono::system_clock::from_time_t(1380855729);
879   ExpectTime(tp, tz, 2013, 10, 3, 20, 2, 9, -7 * 60 * 60, true, "PDT");
880   EXPECT_EQ(weekday::thursday, get_weekday(convert(tp, tz)));
881 }
882 
TEST(BreakTime,LocalTimeInSydney)883 TEST(BreakTime, LocalTimeInSydney) {
884   const time_zone tz = LoadZone("Australia/Sydney");
885   const auto tp = chrono::system_clock::from_time_t(90);
886   ExpectTime(tp, tz, 1970, 1, 1, 10, 1, 30, 10 * 60 * 60, false, "AEST");
887   EXPECT_EQ(weekday::thursday, get_weekday(convert(tp, tz)));
888 }
889 
TEST(MakeTime,TimePointResolution)890 TEST(MakeTime, TimePointResolution) {
891   const time_zone utc = utc_time_zone();
892   const time_point<chrono::nanoseconds> tp_ns =
893       convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
894   EXPECT_EQ("04:05", format("%M:%E*S", tp_ns, utc));
895   const time_point<chrono::microseconds> tp_us =
896       convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
897   EXPECT_EQ("04:05", format("%M:%E*S", tp_us, utc));
898   const time_point<chrono::milliseconds> tp_ms =
899       convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
900   EXPECT_EQ("04:05", format("%M:%E*S", tp_ms, utc));
901   const time_point<chrono::seconds> tp_s =
902       convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
903   EXPECT_EQ("04:05", format("%M:%E*S", tp_s, utc));
904   const time_point<absl::time_internal::cctz::seconds> tp_s64 =
905       convert(civil_second(2015, 1, 2, 3, 4, 5), utc);
906   EXPECT_EQ("04:05", format("%M:%E*S", tp_s64, utc));
907 
908   // These next two require chrono::time_point_cast because the conversion
909   // from a resolution of seconds (the return value of convert()) to a
910   // coarser resolution requires an explicit cast.
911   const time_point<chrono::minutes> tp_m =
912       chrono::time_point_cast<chrono::minutes>(
913           convert(civil_second(2015, 1, 2, 3, 4, 5), utc));
914   EXPECT_EQ("04:00", format("%M:%E*S", tp_m, utc));
915   const time_point<chrono::hours> tp_h = 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 
TEST(MakeTime,Normalization)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.
TEST(MakeTime,SysSecondsLimits)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));
939   time_point<absl::time_internal::cctz::seconds> tp;
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));
946   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
947   tp = convert(civil_second(292277026596, 12, 4, 15, 30, 8), utc);
948   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
949   tp = convert(civil_second::max(), utc);
950   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
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));
955   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
956   tp = convert(civil_second(292277026596, 12, 5, 5, 30, 8), east);
957   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
958   tp = convert(civil_second::max(), east);
959   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
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));
964   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
965   tp = convert(civil_second(292277026596, 12, 4, 7, 30, 8), west);
966   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
967   tp = convert(civil_second::max(), west);
968   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::max(), tp);
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));
975   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
976   tp = convert(civil_second(-292277022657, 1, 27, 8, 29, 51), utc);
977   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
978   tp = convert(civil_second::min(), utc);
979   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
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));
984   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
985   tp = convert(civil_second(-292277022657, 1, 27, 22, 29, 51), east);
986   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
987   tp = convert(civil_second::min(), east);
988   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
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));
993   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
994   tp = convert(civil_second(-292277022657, 1, 26, 18, 29, 51), west);
995   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
996   tp = convert(civil_second::min(), west);
997   EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
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 
TEST(MakeTime,LocalTimeLibC)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");
1031     time_zone::civil_transition transition;
1032     for (auto tp = zi.lookup(civil_second()).trans;
1033          zi.next_transition(tp, &transition);
1034          tp = zi.lookup(transition.to).trans) {
1035       const auto fcl = zi.lookup(transition.from);
1036       const auto tcl = zi.lookup(transition.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(transition.from, transition.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 = transition.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 = transition.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 
TEST(NextTransition,UTC)1100 TEST(NextTransition, UTC) {
1101   const auto tz = utc_time_zone();
1102   time_zone::civil_transition trans;
1103 
1104   auto tp = time_point<absl::time_internal::cctz::seconds>::min();
1105   EXPECT_FALSE(tz.next_transition(tp, &trans));
1106 
1107   tp = time_point<absl::time_internal::cctz::seconds>::max();
1108   EXPECT_FALSE(tz.next_transition(tp, &trans));
1109 }
1110 
TEST(PrevTransition,UTC)1111 TEST(PrevTransition, UTC) {
1112   const auto tz = utc_time_zone();
1113   time_zone::civil_transition trans;
1114 
1115   auto tp = time_point<absl::time_internal::cctz::seconds>::max();
1116   EXPECT_FALSE(tz.prev_transition(tp, &trans));
1117 
1118   tp = time_point<absl::time_internal::cctz::seconds>::min();
1119   EXPECT_FALSE(tz.prev_transition(tp, &trans));
1120 }
1121 
TEST(NextTransition,AmericaNewYork)1122 TEST(NextTransition, AmericaNewYork) {
1123   const auto tz = LoadZone("America/New_York");
1124   time_zone::civil_transition trans;
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 
1131   tp = time_point<absl::time_internal::cctz::seconds>::max();
1132   EXPECT_FALSE(tz.next_transition(tp, &trans));
1133 
1134   tp = time_point<absl::time_internal::cctz::seconds>::min();
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 
TEST(PrevTransition,AmericaNewYork)1146 TEST(PrevTransition, AmericaNewYork) {
1147   const auto tz = LoadZone("America/New_York");
1148   time_zone::civil_transition trans;
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 
1155   tp = time_point<absl::time_internal::cctz::seconds>::min();
1156   EXPECT_FALSE(tz.prev_transition(tp, &trans));
1157 
1158   tp = time_point<absl::time_internal::cctz::seconds>::max();
1159   EXPECT_TRUE(tz.prev_transition(tp, &trans));
1160   // We have a transition but we don't know which one.
1161 }
1162 
TEST(TimeZoneEdgeCase,AmericaNewYork)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");
1169   tp += absl::time_internal::cctz::seconds(1);
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");
1175   tp += absl::time_internal::cctz::seconds(1);
1176   ExpectTime(tp, tz, 2013, 11, 3, 1, 0, 0, -5 * 3600, false, "EST");
1177 }
1178 
TEST(TimeZoneEdgeCase,AmericaLosAngeles)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");
1185   tp += absl::time_internal::cctz::seconds(1);
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");
1191   tp += absl::time_internal::cctz::seconds(1);
1192   ExpectTime(tp, tz, 2013, 11, 3, 1, 0, 0, -8 * 3600, false, "PST");
1193 }
1194 
TEST(TimeZoneEdgeCase,ArizonaNoTransition)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");
1201   tp += absl::time_internal::cctz::seconds(1);
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");
1207   tp += absl::time_internal::cctz::seconds(1);
1208   ExpectTime(tp, tz, 2013, 11, 3, 2, 0, 0, -7 * 3600, false, "MST");
1209 }
1210 
TEST(TimeZoneEdgeCase,AsiaKathmandu)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");
1220   tp += absl::time_internal::cctz::seconds(1);
1221   ExpectTime(tp, tz, 1986, 1, 1, 0, 15, 0, 5.75 * 3600, false, "+0545");
1222 }
1223 
TEST(TimeZoneEdgeCase,PacificChatham)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");
1233   tp += absl::time_internal::cctz::seconds(1);
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");
1240   tp += absl::time_internal::cctz::seconds(1);
1241   ExpectTime(tp, tz, 2013, 9, 29, 3, 45, 0, 13.75 * 3600, true, "+1345");
1242 }
1243 
TEST(TimeZoneEdgeCase,AustraliaLordHowe)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");
1253   tp += absl::time_internal::cctz::seconds(1);
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");
1260   tp += absl::time_internal::cctz::seconds(1);
1261   ExpectTime(tp, tz, 2013, 10, 6, 2, 30, 0, 11 * 3600, true, "+11");
1262 }
1263 
TEST(TimeZoneEdgeCase,PacificApia)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(convert(tp, tz)));
1278   tp += absl::time_internal::cctz::seconds(1);
1279   ExpectTime(tp, tz, 2011, 12, 31, 0, 0, 0, 14 * 3600, true, "+14");
1280   EXPECT_EQ(365, get_yearday(convert(tp, tz)));
1281 }
1282 
TEST(TimeZoneEdgeCase,AfricaCairo)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");
1293     tp += absl::time_internal::cctz::seconds(1);
1294     ExpectTime(tp, tz, 2014, 5, 16, 1, 0, 0, 3 * 3600, true, "EEST");
1295   }
1296 }
1297 
TEST(TimeZoneEdgeCase,AfricaMonrovia)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");
1308     tp += absl::time_internal::cctz::seconds(1);
1309     ExpectTime(tp, tz, 1972, 1, 7, 0, 44, 30, 0 * 60, false, "GMT");
1310   }
1311 }
1312 
TEST(TimeZoneEdgeCase,AmericaJamaica)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);
1337     tp += absl::time_internal::cctz::seconds(1);
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");
1346   tp += absl::time_internal::cctz::seconds(1);
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 
TEST(TimeZoneEdgeCase,WET)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");
1367   tp += absl::time_internal::cctz::seconds(1);
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 
TEST(TimeZoneEdgeCase,FixedOffsets)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 
TEST(TimeZoneEdgeCase,NegativeYear)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(convert(tp, tz)));
1403   tp -= absl::time_internal::cctz::seconds(1);
1404   ExpectTime(tp, tz, -1, 12, 31, 23, 59, 59, 0 * 3600, false, "UTC");
1405   EXPECT_EQ(weekday::friday, get_weekday(convert(tp, tz)));
1406 }
1407 
TEST(TimeZoneEdgeCase,UTC32bitLimit)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");
1417   tp += absl::time_internal::cctz::seconds(1);
1418   ExpectTime(tp, tz, 2038, 1, 19, 3, 14, 8, 0 * 3600, false, "UTC");
1419 }
1420 
TEST(TimeZoneEdgeCase,UTC5DigitYear)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");
1430   tp += absl::time_internal::cctz::seconds(1);
1431   ExpectTime(tp, tz, 10000, 1, 1, 0, 0, 0, 0 * 3600, false, "UTC");
1432 }
1433 
1434 }  // namespace cctz
1435 }  // namespace time_internal
1436 ABSL_NAMESPACE_END
1437 }  // namespace absl
1438