1 /*-------------------------------------------------------------------------
4 * Functions for determining the default timezone to use.
6 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
9 * src/bin/initdb/findtimezone.c
11 *-------------------------------------------------------------------------
13 #include "postgres_fe.h"
22 /* Ideally this would be in a .h file, but it hardly seems worth the trouble */
23 extern const char *select_default_timezone(const char *share_path
);
27 static char tzdirpath
[MAXPGPATH
];
32 * Return full pathname of timezone data directory
34 * In this file, tzdirpath is assumed to be set up by select_default_timezone.
40 /* normal case: timezone stuff is under our share dir */
43 /* we're configured to use system's timezone database */
50 * Given a timezone name, open() the timezone data file. Return the
51 * file descriptor if successful, -1 if not.
53 * This is simpler than the backend function of the same name because
54 * we assume that the input string has the correct case already, so there
55 * is no need for case-folding. (This is obviously true if we got the file
56 * name from the filesystem to start with. The only other place it can come
57 * from is the environment variable TZ, and there seems no need to allow
58 * case variation in that; other programs aren't likely to.)
60 * If "canonname" is not NULL, then on success the canonical spelling of the
61 * given name is stored there (the buffer must be > TZ_STRLEN_MAX bytes!).
62 * This is redundant but kept for compatibility with the backend code.
65 pg_open_tzfile(const char *name
, char *canonname
)
67 char fullname
[MAXPGPATH
];
70 strlcpy(canonname
, name
, TZ_STRLEN_MAX
+ 1);
72 strlcpy(fullname
, pg_TZDIR(), sizeof(fullname
));
73 if (strlen(fullname
) + 1 + strlen(name
) >= MAXPGPATH
)
74 return -1; /* not gonna fit */
75 strcat(fullname
, "/");
76 strcat(fullname
, name
);
78 return open(fullname
, O_RDONLY
| PG_BINARY
, 0);
84 * Load a timezone definition.
85 * Does not verify that the timezone is acceptable!
87 * This corresponds to the backend's pg_tzset(), except that we only support
88 * one loaded timezone at a time.
91 pg_load_tz(const char *name
)
95 if (strlen(name
) > TZ_STRLEN_MAX
)
96 return NULL
; /* not going to fit */
99 * "GMT" is always sent to tzparse(); see comments for pg_tzset().
101 if (strcmp(name
, "GMT") == 0)
103 if (!tzparse(name
, &tz
.state
, true))
105 /* This really, really should not happen ... */
109 else if (tzload(name
, NULL
, &tz
.state
, true) != 0)
111 if (name
[0] == ':' || !tzparse(name
, &tz
.state
, false))
113 return NULL
; /* unknown timezone */
117 strcpy(tz
.TZname
, name
);
124 * The following block of code attempts to determine which timezone in our
125 * timezone database is the best match for the active system timezone.
127 * On most systems, we rely on trying to match the observable behavior of
128 * the C library's localtime() function. The database zone that matches
129 * furthest into the past is the one to use. Often there will be several
130 * zones with identical rankings (since the IANA database assigns multiple
131 * names to many zones). We break ties by first checking for "preferred"
132 * names (such as "UTC"), and then arbitrarily by preferring shorter, then
133 * alphabetically earlier zone names. (If we did not explicitly prefer
134 * "UTC", we would get the alias name "UCT" instead due to alphabetic
137 * Many modern systems use the IANA database, so if we can determine the
138 * system's idea of which zone it is using and its behavior matches our zone
139 * of the same name, we can skip the rather-expensive search through all the
140 * zones in our database. This short-circuit path also ensures that we spell
141 * the zone name the same way the system setting does, even in the presence
142 * of multiple aliases for the same zone.
144 * Win32's native knowledge about timezones appears to be too incomplete
145 * and too different from the IANA database for the above matching strategy
146 * to be of any use. But there is just a limited number of timezones
147 * available, so we can rely on a handmade mapping table instead.
152 #define T_DAY ((time_t) (60*60*24))
153 #define T_WEEK ((time_t) (60*60*24*7))
154 #define T_MONTH ((time_t) (60*60*24*31))
156 #define MAX_TEST_TIMES (52*100) /* 100 years */
161 time_t test_times
[MAX_TEST_TIMES
];
164 static bool check_system_link_file(const char *linkname
, struct tztry
*tt
,
166 static void scan_available_timezones(char *tzdir
, char *tzdirsub
,
168 int *bestscore
, char *bestzonename
);
172 * Get GMT offset from a system struct tm
175 get_timezone_offset(struct tm
*tm
)
177 #if defined(HAVE_STRUCT_TM_TM_ZONE)
178 return tm
->tm_gmtoff
;
179 #elif defined(HAVE_INT_TIMEZONE)
180 return -TIMEZONE_GLOBAL
;
182 #error No way to determine TZ? Can this happen?
187 * Convenience subroutine to convert y/m/d to time_t (NOT pg_time_t)
190 build_time_t(int year
, int month
, int day
)
194 memset(&tm
, 0, sizeof(tm
));
196 tm
.tm_mon
= month
- 1;
197 tm
.tm_year
= year
- 1900;
204 * Does a system tm value match one we computed ourselves?
207 compare_tm(struct tm
*s
, struct pg_tm
*p
)
209 if (s
->tm_sec
!= p
->tm_sec
||
210 s
->tm_min
!= p
->tm_min
||
211 s
->tm_hour
!= p
->tm_hour
||
212 s
->tm_mday
!= p
->tm_mday
||
213 s
->tm_mon
!= p
->tm_mon
||
214 s
->tm_year
!= p
->tm_year
||
215 s
->tm_wday
!= p
->tm_wday
||
216 s
->tm_yday
!= p
->tm_yday
||
217 s
->tm_isdst
!= p
->tm_isdst
)
223 * See how well a specific timezone setting matches the system behavior
225 * We score a timezone setting according to the number of test times it
226 * matches. (The test times are ordered later-to-earlier, but this routine
227 * doesn't actually know that; it just scans until the first non-match.)
229 * We return -1 for a completely unusable setting; this is worse than the
230 * score of zero for a setting that works but matches not even the first
234 score_timezone(const char *tzname
, struct tztry
*tt
)
240 char cbuf
[TZ_STRLEN_MAX
+ 1];
243 /* Load timezone definition */
244 tz
= pg_load_tz(tzname
);
246 return -1; /* unrecognized zone name */
248 /* Reject if leap seconds involved */
249 if (!pg_tz_acceptable(tz
))
251 #ifdef DEBUG_IDENTIFY_TIMEZONE
252 fprintf(stderr
, "Reject TZ \"%s\": uses leap seconds\n", tzname
);
257 /* Check for match at all the test times */
258 for (i
= 0; i
< tt
->n_test_times
; i
++)
260 pgtt
= (pg_time_t
) (tt
->test_times
[i
]);
261 pgtm
= pg_localtime(&pgtt
, tz
);
263 return -1; /* probably shouldn't happen */
264 systm
= localtime(&(tt
->test_times
[i
]));
267 #ifdef DEBUG_IDENTIFY_TIMEZONE
268 fprintf(stderr
, "TZ \"%s\" scores %d: at %ld %04d-%02d-%02d %02d:%02d:%02d %s, system had no data\n",
269 tzname
, i
, (long) pgtt
,
270 pgtm
->tm_year
+ 1900, pgtm
->tm_mon
+ 1, pgtm
->tm_mday
,
271 pgtm
->tm_hour
, pgtm
->tm_min
, pgtm
->tm_sec
,
272 pgtm
->tm_isdst
? "dst" : "std");
276 if (!compare_tm(systm
, pgtm
))
278 #ifdef DEBUG_IDENTIFY_TIMEZONE
279 fprintf(stderr
, "TZ \"%s\" scores %d: at %ld %04d-%02d-%02d %02d:%02d:%02d %s versus %04d-%02d-%02d %02d:%02d:%02d %s\n",
280 tzname
, i
, (long) pgtt
,
281 pgtm
->tm_year
+ 1900, pgtm
->tm_mon
+ 1, pgtm
->tm_mday
,
282 pgtm
->tm_hour
, pgtm
->tm_min
, pgtm
->tm_sec
,
283 pgtm
->tm_isdst
? "dst" : "std",
284 systm
->tm_year
+ 1900, systm
->tm_mon
+ 1, systm
->tm_mday
,
285 systm
->tm_hour
, systm
->tm_min
, systm
->tm_sec
,
286 systm
->tm_isdst
? "dst" : "std");
290 if (systm
->tm_isdst
>= 0)
292 /* Check match of zone names, too */
293 if (pgtm
->tm_zone
== NULL
)
294 return -1; /* probably shouldn't happen */
295 memset(cbuf
, 0, sizeof(cbuf
));
296 strftime(cbuf
, sizeof(cbuf
) - 1, "%Z", systm
); /* zone abbr */
297 if (strcmp(cbuf
, pgtm
->tm_zone
) != 0)
299 #ifdef DEBUG_IDENTIFY_TIMEZONE
300 fprintf(stderr
, "TZ \"%s\" scores %d: at %ld \"%s\" versus \"%s\"\n",
301 tzname
, i
, (long) pgtt
,
302 pgtm
->tm_zone
, cbuf
);
309 #ifdef DEBUG_IDENTIFY_TIMEZONE
310 fprintf(stderr
, "TZ \"%s\" gets max score %d\n", tzname
, i
);
317 * Test whether given zone name is a perfect match to localtime() behavior
320 perfect_timezone_match(const char *tzname
, struct tztry
*tt
)
322 return (score_timezone(tzname
, tt
) == tt
->n_test_times
);
327 * Try to identify a timezone name (in our terminology) that best matches the
328 * observed behavior of the system localtime() function.
331 identify_system_timezone(void)
333 static char resultbuf
[TZ_STRLEN_MAX
+ 1];
340 char tmptzdir
[MAXPGPATH
];
342 char std_zone_name
[TZ_STRLEN_MAX
+ 1],
343 dst_zone_name
[TZ_STRLEN_MAX
+ 1];
344 char cbuf
[TZ_STRLEN_MAX
+ 1];
346 /* Initialize OS timezone library */
350 * Set up the list of dates to be probed to see how well our timezone
351 * matches the system zone. We first probe January and July of the
352 * current year; this serves to quickly eliminate the vast majority of the
353 * TZ database entries. If those dates match, we probe every week for 100
354 * years backwards from the current July. (Weekly resolution is good
355 * enough to identify DST transition rules, since everybody switches on
356 * Sundays.) This is sufficient to cover most of the Unix time_t range,
357 * and we don't want to look further than that since many systems won't
358 * have sane TZ behavior further back anyway. The further back the zone
359 * matches, the better we score it. This may seem like a rather random
360 * way of doing things, but experience has shown that system-supplied
361 * timezone definitions are likely to have DST behavior that is right for
362 * the recent past and not so accurate further back. Scoring in this way
363 * allows us to recognize zones that have some commonality with the IANA
364 * database, without insisting on exact match. (Note: we probe Thursdays,
365 * not Sundays, to avoid triggering DST-transition bugs in localtime
369 tm
= localtime(&tnow
);
371 return NULL
; /* give up if localtime is broken... */
372 thisyear
= tm
->tm_year
+ 1900;
374 t
= build_time_t(thisyear
, 1, 15);
377 * Round back to GMT midnight Thursday. This depends on the knowledge
378 * that the time_t origin is Thu Jan 01 1970. (With a different origin
379 * we'd be probing some other day of the week, but it wouldn't matter
380 * anyway unless localtime() had DST-transition bugs.)
385 tt
.test_times
[tt
.n_test_times
++] = t
;
387 t
= build_time_t(thisyear
, 7, 15);
390 tt
.test_times
[tt
.n_test_times
++] = t
;
392 while (tt
.n_test_times
< MAX_TEST_TIMES
)
395 tt
.test_times
[tt
.n_test_times
++] = t
;
399 * Try to avoid the brute-force search by seeing if we can recognize the
400 * system's timezone setting directly.
402 * Currently we just check /etc/localtime; there are other conventions for
403 * this, but that seems to be the only one used on enough platforms to be
404 * worth troubling over.
406 if (check_system_link_file("/etc/localtime", &tt
, resultbuf
))
409 /* No luck, so search for the best-matching timezone file */
410 strlcpy(tmptzdir
, pg_TZDIR(), sizeof(tmptzdir
));
413 scan_available_timezones(tmptzdir
, tmptzdir
+ strlen(tmptzdir
) + 1,
415 &bestscore
, resultbuf
);
418 /* Ignore IANA's rather silly "Factory" zone; use GMT instead */
419 if (strcmp(resultbuf
, "Factory") == 0)
425 * Couldn't find a match in the database, so next we try constructed zone
426 * names (like "PST8PDT").
428 * First we need to determine the names of the local standard and daylight
429 * zones. The idea here is to scan forward from today until we have seen
430 * both zones, if both are in use.
432 memset(std_zone_name
, 0, sizeof(std_zone_name
));
433 memset(dst_zone_name
, 0, sizeof(dst_zone_name
));
439 * Round back to a GMT midnight so results don't depend on local time of
442 tnow
-= (tnow
% T_DAY
);
445 * We have to look a little further ahead than one year, in case today is
446 * just past a DST boundary that falls earlier in the year than the next
447 * similar boundary. Arbitrarily scan up to 14 months.
449 for (t
= tnow
; t
<= tnow
+ T_MONTH
* 14; t
+= T_MONTH
)
454 if (tm
->tm_isdst
< 0)
456 if (tm
->tm_isdst
== 0 && std_zone_name
[0] == '\0')
459 memset(cbuf
, 0, sizeof(cbuf
));
460 strftime(cbuf
, sizeof(cbuf
) - 1, "%Z", tm
); /* zone abbr */
461 strcpy(std_zone_name
, cbuf
);
462 std_ofs
= get_timezone_offset(tm
);
464 if (tm
->tm_isdst
> 0 && dst_zone_name
[0] == '\0')
467 memset(cbuf
, 0, sizeof(cbuf
));
468 strftime(cbuf
, sizeof(cbuf
) - 1, "%Z", tm
); /* zone abbr */
469 strcpy(dst_zone_name
, cbuf
);
471 /* Done if found both */
472 if (std_zone_name
[0] && dst_zone_name
[0])
476 /* We should have found a STD zone name by now... */
477 if (std_zone_name
[0] == '\0')
479 #ifdef DEBUG_IDENTIFY_TIMEZONE
480 fprintf(stderr
, "could not determine system time zone\n");
482 return NULL
; /* go to GMT */
485 /* If we found DST then try STD<ofs>DST */
486 if (dst_zone_name
[0] != '\0')
488 snprintf(resultbuf
, sizeof(resultbuf
), "%s%d%s",
489 std_zone_name
, -std_ofs
/ 3600, dst_zone_name
);
490 if (score_timezone(resultbuf
, &tt
) > 0)
494 /* Try just the STD timezone (works for GMT at least) */
495 strcpy(resultbuf
, std_zone_name
);
496 if (score_timezone(resultbuf
, &tt
) > 0)
500 snprintf(resultbuf
, sizeof(resultbuf
), "%s%d",
501 std_zone_name
, -std_ofs
/ 3600);
502 if (score_timezone(resultbuf
, &tt
) > 0)
506 * Did not find the timezone. Fallback to use a GMT zone. Note that the
507 * IANA timezone database names the GMT-offset zones in POSIX style: plus
508 * is west of Greenwich. It's unfortunate that this is opposite of SQL
509 * conventions. Should we therefore change the names? Probably not...
511 snprintf(resultbuf
, sizeof(resultbuf
), "Etc/GMT%s%d",
512 (-std_ofs
> 0) ? "+" : "", -std_ofs
/ 3600);
514 #ifdef DEBUG_IDENTIFY_TIMEZONE
515 fprintf(stderr
, "could not recognize system time zone, using \"%s\"\n",
522 * Examine a system-provided symlink file to see if it tells us the timezone.
524 * Unfortunately, there is little standardization of how the system default
525 * timezone is determined in the absence of a TZ environment setting.
526 * But a common strategy is to create a symlink at a well-known place.
527 * If "linkname" identifies a readable symlink, and the tail of its contents
528 * matches a zone name we know, and the actual behavior of localtime() agrees
529 * with what we think that zone means, then we may use that zone name.
531 * We insist on a perfect behavioral match, which might not happen if the
532 * system has a different IANA database version than we do; but in that case
533 * it seems best to fall back to the brute-force search.
535 * linkname is the symlink file location to probe.
537 * tt tells about the system timezone behavior we need to match.
539 * If we successfully identify a zone name, store it in *bestzonename and
540 * return true; else return false. bestzonename must be a buffer of length
544 check_system_link_file(const char *linkname
, struct tztry
*tt
,
548 char link_target
[MAXPGPATH
];
550 const char *cur_name
;
553 * Try to read the symlink. If not there, not a symlink, etc etc, just
554 * quietly fail; the precise reason needn't concern us.
556 len
= readlink(linkname
, link_target
, sizeof(link_target
));
557 if (len
< 0 || len
>= sizeof(link_target
))
559 link_target
[len
] = '\0';
561 #ifdef DEBUG_IDENTIFY_TIMEZONE
562 fprintf(stderr
, "symbolic link \"%s\" contains \"%s\"\n",
563 linkname
, link_target
);
567 * The symlink is probably of the form "/path/to/zones/zone/name", or
568 * possibly it is a relative path. Nobody puts their zone DB directly in
569 * the root directory, so we can definitely skip the first component; but
570 * after that it's trial-and-error to identify which path component begins
573 cur_name
= link_target
;
576 /* Advance to next segment of path */
577 cur_name
= strchr(cur_name
+ 1, '/');
578 if (cur_name
== NULL
)
580 /* If there are consecutive slashes, skip all, as the kernel would */
584 } while (*cur_name
== '/');
587 * Test remainder of path to see if it is a matching zone name.
588 * Relative paths might contain ".."; we needn't bother testing if the
589 * first component is that. Also defend against overlength names.
591 if (*cur_name
&& *cur_name
!= '.' &&
592 strlen(cur_name
) <= TZ_STRLEN_MAX
&&
593 perfect_timezone_match(cur_name
, tt
))
596 strcpy(bestzonename
, cur_name
);
601 /* Couldn't extract a matching zone name */
604 /* No symlinks? Forget it */
610 * Given a timezone name, determine whether it should be preferred over other
611 * names which are equally good matches. The output is arbitrary but we will
612 * use 0 for "neutral" default preference; larger values are more preferred.
615 zone_name_pref(const char *zonename
)
618 * Prefer UTC over alternatives such as UCT. Also prefer Etc/UTC over
619 * Etc/UCT; but UTC is preferred to Etc/UTC.
621 if (strcmp(zonename
, "UTC") == 0)
623 if (strcmp(zonename
, "Etc/UTC") == 0)
627 * We don't want to pick "localtime" or "posixrules", unless we can find
628 * no other name for the prevailing zone. Those aren't real zone names.
630 if (strcmp(zonename
, "localtime") == 0 ||
631 strcmp(zonename
, "posixrules") == 0)
638 * Recursively scan the timezone database looking for the best match to
639 * the system timezone behavior.
641 * tzdir points to a buffer of size MAXPGPATH. On entry, it holds the
642 * pathname of a directory containing TZ files. We internally modify it
643 * to hold pathnames of sub-directories and files, but must restore it
644 * to its original contents before exit.
646 * tzdirsub points to the part of tzdir that represents the subfile name
647 * (ie, tzdir + the original directory name length, plus one for the
650 * tt tells about the system timezone behavior we need to match.
652 * *bestscore and *bestzonename on entry hold the best score found so far
653 * and the name of the best zone. We overwrite them if we find a better
654 * score. bestzonename must be a buffer of length TZ_STRLEN_MAX + 1.
657 scan_available_timezones(char *tzdir
, char *tzdirsub
, struct tztry
*tt
,
658 int *bestscore
, char *bestzonename
)
660 int tzdir_orig_len
= strlen(tzdir
);
664 names
= pgfnames(tzdir
);
668 for (namep
= names
; *namep
; namep
++)
673 /* Ignore . and .., plus any other "hidden" files */
677 snprintf(tzdir
+ tzdir_orig_len
, MAXPGPATH
- tzdir_orig_len
,
680 if (stat(tzdir
, &statbuf
) != 0)
682 #ifdef DEBUG_IDENTIFY_TIMEZONE
683 fprintf(stderr
, "could not stat \"%s\": %s\n",
684 tzdir
, strerror(errno
));
686 tzdir
[tzdir_orig_len
] = '\0';
690 if (S_ISDIR(statbuf
.st_mode
))
692 /* Recurse into subdirectory */
693 scan_available_timezones(tzdir
, tzdirsub
, tt
,
694 bestscore
, bestzonename
);
698 /* Load and test this file */
699 int score
= score_timezone(tzdirsub
, tt
);
701 if (score
> *bestscore
)
704 strlcpy(bestzonename
, tzdirsub
, TZ_STRLEN_MAX
+ 1);
706 else if (score
== *bestscore
)
708 /* Consider how to break a tie */
709 int namepref
= (zone_name_pref(tzdirsub
) -
710 zone_name_pref(bestzonename
));
714 (strlen(tzdirsub
) < strlen(bestzonename
) ||
715 (strlen(tzdirsub
) == strlen(bestzonename
) &&
716 strcmp(tzdirsub
, bestzonename
) < 0))))
717 strlcpy(bestzonename
, tzdirsub
, TZ_STRLEN_MAX
+ 1);
722 tzdir
[tzdir_orig_len
] = '\0';
725 pgfnames_cleanup(names
);
731 const char *stdname
; /* Windows name of standard timezone */
732 const char *dstname
; /* Windows name of daylight timezone */
733 const char *pgtzname
; /* Name of pgsql timezone to map to */
738 * This list was built from the contents of the registry at
739 * HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time
740 * Zones on Windows 7, Windows 10, and Windows Server 2019.
742 * The zones have been matched to IANA timezones by looking at the cities
743 * listed in the win32 display name (in the comment here) in most cases.
746 /* (UTC+04:30) Kabul */
747 "Afghanistan Standard Time", "Afghanistan Daylight Time",
751 /* (UTC-09:00) Alaska */
752 "Alaskan Standard Time", "Alaskan Daylight Time",
756 /* (UTC-10:00) Aleutian Islands */
757 "Aleutian Standard Time", "Aleutian Daylight Time",
761 /* (UTC+07:00) Barnaul, Gorno-Altaysk */
762 "Altai Standard Time", "Altai Daylight Time",
766 /* (UTC+03:00) Kuwait, Riyadh */
767 "Arab Standard Time", "Arab Daylight Time",
771 /* (UTC+04:00) Abu Dhabi, Muscat */
772 "Arabian Standard Time", "Arabian Daylight Time",
776 /* (UTC+03:00) Baghdad */
777 "Arabic Standard Time", "Arabic Daylight Time",
781 /* (UTC-03:00) City of Buenos Aires */
782 "Argentina Standard Time", "Argentina Daylight Time",
783 "America/Buenos_Aires"
786 /* (UTC+04:00) Baku, Tbilisi, Yerevan */
787 "Armenian Standard Time", "Armenian Daylight Time",
791 /* (UTC+04:00) Astrakhan, Ulyanovsk */
792 "Astrakhan Standard Time", "Astrakhan Daylight Time",
796 /* (UTC-04:00) Atlantic Time (Canada) */
797 "Atlantic Standard Time", "Atlantic Daylight Time",
801 /* (UTC+09:30) Darwin */
802 "AUS Central Standard Time", "AUS Central Daylight Time",
806 /* (UTC+08:45) Eucla */
807 "Aus Central W. Standard Time", "Aus Central W. Daylight Time",
811 /* (UTC+10:00) Canberra, Melbourne, Sydney */
812 "AUS Eastern Standard Time", "AUS Eastern Daylight Time",
816 /* (UTC+04:00) Baku */
817 "Azerbaijan Standard Time", "Azerbaijan Daylight Time",
821 /* (UTC-01:00) Azores */
822 "Azores Standard Time", "Azores Daylight Time",
826 /* (UTC-03:00) Salvador */
827 "Bahia Standard Time", "Bahia Daylight Time",
831 /* (UTC+06:00) Dhaka */
832 "Bangladesh Standard Time", "Bangladesh Daylight Time",
836 /* (UTC+11:00) Bougainville Island */
837 "Bougainville Standard Time", "Bougainville Daylight Time",
838 "Pacific/Bougainville"
841 /* (UTC+03:00) Minsk */
842 "Belarus Standard Time", "Belarus Daylight Time",
846 /* (UTC-01:00) Cabo Verde Is. */
847 "Cabo Verde Standard Time", "Cabo Verde Daylight Time",
848 "Atlantic/Cape_Verde"
851 /* (UTC+12:45) Chatham Islands */
852 "Chatham Islands Standard Time", "Chatham Islands Daylight Time",
856 /* (UTC-06:00) Saskatchewan */
857 "Canada Central Standard Time", "Canada Central Daylight Time",
858 "Canada/Saskatchewan"
861 /* (UTC-01:00) Cape Verde Is. */
862 "Cape Verde Standard Time", "Cape Verde Daylight Time",
863 "Atlantic/Cape_Verde"
866 /* (UTC+04:00) Yerevan */
867 "Caucasus Standard Time", "Caucasus Daylight Time",
871 /* (UTC+09:30) Adelaide */
872 "Cen. Australia Standard Time", "Cen. Australia Daylight Time",
875 /* Central America (other than Mexico) generally does not observe DST */
877 /* (UTC-06:00) Central America */
878 "Central America Standard Time", "Central America Daylight Time",
882 /* (UTC+06:00) Astana */
883 "Central Asia Standard Time", "Central Asia Daylight Time",
887 /* (UTC-04:00) Cuiaba */
888 "Central Brazilian Standard Time", "Central Brazilian Daylight Time",
892 /* (UTC+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague */
893 "Central Europe Standard Time", "Central Europe Daylight Time",
897 /* (UTC+01:00) Sarajevo, Skopje, Warsaw, Zagreb */
898 "Central European Standard Time", "Central European Daylight Time",
902 /* (UTC+11:00) Solomon Is., New Caledonia */
903 "Central Pacific Standard Time", "Central Pacific Daylight Time",
907 /* (UTC-06:00) Central Time (US & Canada) */
908 "Central Standard Time", "Central Daylight Time",
912 /* (UTC-06:00) Guadalajara, Mexico City, Monterrey */
913 "Central Standard Time (Mexico)", "Central Daylight Time (Mexico)",
914 "America/Mexico_City"
917 /* (UTC+08:00) Beijing, Chongqing, Hong Kong, Urumqi */
918 "China Standard Time", "China Daylight Time",
922 /* (UTC-05:00) Havana */
923 "Cuba Standard Time", "Cuba Daylight Time",
927 /* (UTC-12:00) International Date Line West */
928 "Dateline Standard Time", "Dateline Daylight Time",
932 /* (UTC+03:00) Nairobi */
933 "E. Africa Standard Time", "E. Africa Daylight Time",
937 /* (UTC+10:00) Brisbane */
938 "E. Australia Standard Time", "E. Australia Daylight Time",
942 /* (UTC+02:00) Chisinau */
943 "E. Europe Standard Time", "E. Europe Daylight Time",
947 /* (UTC-03:00) Brasilia */
948 "E. South America Standard Time", "E. South America Daylight Time",
952 /* (UTC-05:00) Eastern Time (US & Canada) */
953 "Eastern Standard Time", "Eastern Daylight Time",
957 /* (UTC-05:00) Chetumal */
958 "Eastern Standard Time (Mexico)", "Eastern Daylight Time (Mexico)",
959 "America/Mexico_City"
962 /* (UTC-06:00) Easter Island */
963 "Easter Island Standard Time", "Easter Island Daylight Time",
967 /* (UTC+02:00) Cairo */
968 "Egypt Standard Time", "Egypt Daylight Time",
972 /* (UTC+05:00) Ekaterinburg */
973 "Ekaterinburg Standard Time (RTZ 4)", "Ekaterinburg Daylight Time",
977 /* (UTC+12:00) Fiji */
978 "Fiji Standard Time", "Fiji Daylight Time",
982 /* (UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius */
983 "FLE Standard Time", "FLE Daylight Time",
987 /* (UTC+04:00) Tbilisi */
988 "Georgian Standard Time", "Georgian Daylight Time",
992 /* (UTC+00:00) Dublin, Edinburgh, Lisbon, London */
993 "GMT Standard Time", "GMT Daylight Time",
997 /* (UTC-03:00) Greenland */
998 "Greenland Standard Time", "Greenland Daylight Time",
1002 /* (UTC+00:00) Monrovia, Reykjavik */
1003 "Greenwich Standard Time", "Greenwich Daylight Time",
1007 /* (UTC+02:00) Athens, Bucharest */
1008 "GTB Standard Time", "GTB Daylight Time",
1012 /* (UTC-05:00) Haiti */
1013 "Haiti Standard Time", "Haiti Daylight Time",
1017 /* (UTC-10:00) Hawaii */
1018 "Hawaiian Standard Time", "Hawaiian Daylight Time",
1022 /* (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi */
1023 "India Standard Time", "India Daylight Time",
1027 /* (UTC+03:30) Tehran */
1028 "Iran Standard Time", "Iran Daylight Time",
1032 /* (UTC+02:00) Jerusalem */
1033 "Jerusalem Standard Time", "Jerusalem Daylight Time",
1037 /* (UTC+02:00) Amman */
1038 "Jordan Standard Time", "Jordan Daylight Time",
1042 /* (UTC+12:00) Petropavlovsk-Kamchatsky - Old */
1043 "Kamchatka Standard Time", "Kamchatka Daylight Time",
1047 /* (UTC+09:00) Seoul */
1048 "Korea Standard Time", "Korea Daylight Time",
1052 /* (UTC+02:00) Tripoli */
1053 "Libya Standard Time", "Libya Daylight Time",
1057 /* (UTC+14:00) Kiritimati Island */
1058 "Line Islands Standard Time", "Line Islands Daylight Time",
1059 "Pacific/Kiritimati"
1062 /* (UTC+10:30) Lord Howe Island */
1063 "Lord Howe Standard Time", "Lord Howe Daylight Time",
1064 "Australia/Lord_Howe"
1067 /* (UTC+11:00) Magadan */
1068 "Magadan Standard Time", "Magadan Daylight Time",
1072 /* (UTC-03:00) Punta Arenas */
1073 "Magallanes Standard Time", "Magallanes Daylight Time",
1074 "America/Punta_Arenas"
1077 /* (UTC-09:30) Marquesas Islands */
1078 "Marquesas Standard Time", "Marquesas Daylight Time",
1082 /* (UTC+04:00) Port Louis */
1083 "Mauritius Standard Time", "Mauritius Daylight Time",
1087 /* (UTC-06:00) Guadalajara, Mexico City, Monterrey */
1088 "Mexico Standard Time", "Mexico Daylight Time",
1089 "America/Mexico_City"
1092 /* (UTC-07:00) Chihuahua, La Paz, Mazatlan */
1093 "Mexico Standard Time 2", "Mexico Daylight Time 2",
1097 /* (UTC-02:00) Mid-Atlantic - Old */
1098 "Mid-Atlantic Standard Time", "Mid-Atlantic Daylight Time",
1099 "Atlantic/South_Georgia"
1102 /* (UTC+02:00) Beirut */
1103 "Middle East Standard Time", "Middle East Daylight Time",
1107 /* (UTC-03:00) Montevideo */
1108 "Montevideo Standard Time", "Montevideo Daylight Time",
1109 "America/Montevideo"
1112 /* (UTC+01:00) Casablanca */
1113 "Morocco Standard Time", "Morocco Daylight Time",
1117 /* (UTC-07:00) Mountain Time (US & Canada) */
1118 "Mountain Standard Time", "Mountain Daylight Time",
1122 /* (UTC-07:00) Chihuahua, La Paz, Mazatlan */
1123 "Mountain Standard Time (Mexico)", "Mountain Daylight Time (Mexico)",
1127 /* (UTC+06:30) Yangon (Rangoon) */
1128 "Myanmar Standard Time", "Myanmar Daylight Time",
1132 /* (UTC+06:00) Novosibirsk (RTZ 5) */
1133 "N. Central Asia Standard Time", "N. Central Asia Daylight Time",
1137 /* (UTC+02:00) Windhoek */
1138 "Namibia Standard Time", "Namibia Daylight Time",
1142 /* (UTC+05:45) Kathmandu */
1143 "Nepal Standard Time", "Nepal Daylight Time",
1147 /* (UTC+12:00) Auckland, Wellington */
1148 "New Zealand Standard Time", "New Zealand Daylight Time",
1152 /* (UTC-03:30) Newfoundland */
1153 "Newfoundland Standard Time", "Newfoundland Daylight Time",
1154 "Canada/Newfoundland"
1157 /* (UTC+11:00) Norfolk Island */
1158 "Norfolk Standard Time", "Norfolk Daylight Time",
1162 /* (UTC+08:00) Irkutsk, Ulaan Bataar */
1163 "North Asia East Standard Time", "North Asia East Daylight Time",
1167 /* (UTC+07:00) Krasnoyarsk */
1168 "North Asia Standard Time", "North Asia Daylight Time",
1172 /* (UTC+09:00) Pyongyang */
1173 "North Korea Standard Time", "North Korea Daylight Time",
1177 /* (UTC+07:00) Novosibirsk */
1178 "Novosibirsk Standard Time", "Novosibirsk Daylight Time",
1182 /* (UTC+06:00) Omsk */
1183 "Omsk Standard Time", "Omsk Daylight Time",
1187 /* (UTC-04:00) Santiago */
1188 "Pacific SA Standard Time", "Pacific SA Daylight Time",
1192 /* (UTC-08:00) Pacific Time (US & Canada) */
1193 "Pacific Standard Time", "Pacific Daylight Time",
1197 /* (UTC-08:00) Baja California */
1198 "Pacific Standard Time (Mexico)", "Pacific Daylight Time (Mexico)",
1202 /* (UTC+05:00) Islamabad, Karachi */
1203 "Pakistan Standard Time", "Pakistan Daylight Time",
1207 /* (UTC-04:00) Asuncion */
1208 "Paraguay Standard Time", "Paraguay Daylight Time",
1212 /* (UTC+05:00) Qyzylorda */
1213 "Qyzylorda Standard Time", "Qyzylorda Daylight Time",
1217 /* (UTC+01:00) Brussels, Copenhagen, Madrid, Paris */
1218 "Romance Standard Time", "Romance Daylight Time",
1222 /* (UTC+02:00) Kaliningrad */
1223 "Russia TZ 1 Standard Time", "Russia TZ 1 Daylight Time",
1224 "Europe/Kaliningrad"
1227 /* (UTC+03:00) Moscow, St. Petersburg */
1228 "Russia TZ 2 Standard Time", "Russia TZ 2 Daylight Time",
1232 /* (UTC+04:00) Izhevsk, Samara */
1233 "Russia TZ 3 Standard Time", "Russia TZ 3 Daylight Time",
1237 /* (UTC+05:00) Ekaterinburg */
1238 "Russia TZ 4 Standard Time", "Russia TZ 4 Daylight Time",
1239 "Asia/Yekaterinburg"
1242 /* (UTC+06:00) Novosibirsk (RTZ 5) */
1243 "Russia TZ 5 Standard Time", "Russia TZ 5 Daylight Time",
1247 /* (UTC+07:00) Krasnoyarsk */
1248 "Russia TZ 6 Standard Time", "Russia TZ 6 Daylight Time",
1252 /* (UTC+08:00) Irkutsk */
1253 "Russia TZ 7 Standard Time", "Russia TZ 7 Daylight Time",
1257 /* (UTC+09:00) Yakutsk */
1258 "Russia TZ 8 Standard Time", "Russia TZ 8 Daylight Time",
1262 /* (UTC+10:00) Vladivostok */
1263 "Russia TZ 9 Standard Time", "Russia TZ 9 Daylight Time",
1267 /* (UTC+11:00) Chokurdakh */
1268 "Russia TZ 10 Standard Time", "Russia TZ 10 Daylight Time",
1272 /* (UTC+12:00) Anadyr, Petropavlovsk-Kamchatsky */
1273 "Russia TZ 11 Standard Time", "Russia TZ 11 Daylight Time",
1277 /* (UTC+03:00) Moscow, St. Petersburg, Volgograd */
1278 "Russian Standard Time", "Russian Daylight Time",
1282 /* (UTC-03:00) Cayenne, Fortaleza */
1283 "SA Eastern Standard Time", "SA Eastern Daylight Time",
1284 "America/Buenos_Aires"
1287 /* (UTC-05:00) Bogota, Lima, Quito, Rio Branco */
1288 "SA Pacific Standard Time", "SA Pacific Daylight Time",
1292 /* (UTC-04:00) Georgetown, La Paz, Manaus, San Juan */
1293 "SA Western Standard Time", "SA Western Daylight Time",
1297 /* (UTC-03:00) Saint Pierre and Miquelon */
1298 "Saint Pierre Standard Time", "Saint Pierre Daylight Time",
1302 /* (UTC+13:00) Samoa */
1303 "Samoa Standard Time", "Samoa Daylight Time",
1307 /* (UTC+00:00) Sao Tome */
1308 "Sao Tome Standard Time", "Sao Tome Daylight Time",
1312 /* (UTC+04:00) Saratov */
1313 "Saratov Standard Time", "Saratov Daylight Time",
1317 /* (UTC+07:00) Bangkok, Hanoi, Jakarta */
1318 "SE Asia Standard Time", "SE Asia Daylight Time",
1322 /* (UTC+08:00) Kuala Lumpur, Singapore */
1323 "Malay Peninsula Standard Time", "Malay Peninsula Daylight Time",
1327 /* (UTC+11:00) Sakhalin */
1328 "Sakhalin Standard Time", "Sakhalin Daylight Time",
1332 /* (UTC+02:00) Harare, Pretoria */
1333 "South Africa Standard Time", "South Africa Daylight Time",
1337 /* (UTC+05:30) Sri Jayawardenepura */
1338 "Sri Lanka Standard Time", "Sri Lanka Daylight Time",
1342 /* (UTC+02:00) Khartoum */
1343 "Sudan Standard Time", "Sudan Daylight Time",
1347 /* (UTC+02:00) Damascus */
1348 "Syria Standard Time", "Syria Daylight Time",
1352 /* (UTC+08:00) Taipei */
1353 "Taipei Standard Time", "Taipei Daylight Time",
1357 /* (UTC+10:00) Hobart */
1358 "Tasmania Standard Time", "Tasmania Daylight Time",
1362 /* (UTC-03:00) Araguaina */
1363 "Tocantins Standard Time", "Tocantins Daylight Time",
1367 /* (UTC+09:00) Osaka, Sapporo, Tokyo */
1368 "Tokyo Standard Time", "Tokyo Daylight Time",
1372 /* (UTC+13:00) Nuku'alofa */
1373 "Tonga Standard Time", "Tonga Daylight Time",
1377 /* (UTC+07:00) Tomsk */
1378 "Tomsk Standard Time", "Tomsk Daylight Time",
1382 /* (UTC+09:00) Chita */
1383 "Transbaikal Standard Time", "Transbaikal Daylight Time",
1387 /* (UTC+03:00) Istanbul */
1388 "Turkey Standard Time", "Turkey Daylight Time",
1392 /* (UTC-05:00) Turks and Caicos */
1393 "Turks and Caicos Standard Time", "Turks and Caicos Daylight Time",
1394 "America/Grand_Turk"
1397 /* (UTC+08:00) Ulaanbaatar */
1398 "Ulaanbaatar Standard Time", "Ulaanbaatar Daylight Time",
1402 /* (UTC-05:00) Indiana (East) */
1403 "US Eastern Standard Time", "US Eastern Daylight Time",
1407 /* (UTC-07:00) Arizona */
1408 "US Mountain Standard Time", "US Mountain Daylight Time",
1412 /* (UTC) Coordinated Universal Time */
1413 "Coordinated Universal Time", "Coordinated Universal Time",
1417 /* (UTC+12:00) Coordinated Universal Time+12 */
1422 /* (UTC+13:00) Coordinated Universal Time+13 */
1427 /* (UTC-02:00) Coordinated Universal Time-02 */
1432 /* (UTC-08:00) Coordinated Universal Time-08 */
1437 /* (UTC-09:00) Coordinated Universal Time-09 */
1442 /* (UTC-11:00) Coordinated Universal Time-11 */
1447 /* (UTC-04:00) Caracas */
1448 "Venezuela Standard Time", "Venezuela Daylight Time",
1452 /* (UTC+10:00) Vladivostok (RTZ 9) */
1453 "Vladivostok Standard Time", "Vladivostok Daylight Time",
1457 /* (UTC+04:00) Volgograd */
1458 "Volgograd Standard Time", "Volgograd Daylight Time",
1462 /* (UTC+08:00) Perth */
1463 "W. Australia Standard Time", "W. Australia Daylight Time",
1467 /* Could not find a match for this one (just a guess). Excluded for now. */
1469 /* (UTC+01:00) West Central Africa */
1470 "W. Central Africa Standard Time", "W. Central Africa Daylight Time",
1475 /* (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna */
1476 "W. Europe Standard Time", "W. Europe Daylight Time",
1480 /* (UTC+07:00) Hovd */
1481 "W. Mongolia Standard Time", "W. Mongolia Daylight Time",
1485 /* (UTC+05:00) Ashgabat, Tashkent */
1486 "West Asia Standard Time", "West Asia Daylight Time",
1490 /* (UTC+02:00) Gaza, Hebron */
1491 "West Bank Gaza Standard Time", "West Bank Gaza Daylight Time",
1495 /* (UTC+10:00) Guam, Port Moresby */
1496 "West Pacific Standard Time", "West Pacific Daylight Time",
1500 /* (UTC+09:00) Yakutsk */
1501 "Yakutsk Standard Time", "Yakutsk Daylight Time",
1510 identify_system_timezone(void)
1514 char localtzname
[256];
1515 time_t t
= time(NULL
);
1516 struct tm
*tm
= localtime(&t
);
1522 #ifdef DEBUG_IDENTIFY_TIMEZONE
1523 fprintf(stderr
, "could not identify system time zone: localtime() failed\n");
1525 return NULL
; /* go to GMT */
1528 memset(tzname
, 0, sizeof(tzname
));
1529 strftime(tzname
, sizeof(tzname
) - 1, "%Z", tm
);
1531 for (i
= 0; win32_tzmap
[i
].stdname
!= NULL
; i
++)
1533 if (strcmp(tzname
, win32_tzmap
[i
].stdname
) == 0 ||
1534 strcmp(tzname
, win32_tzmap
[i
].dstname
) == 0)
1536 #ifdef DEBUG_IDENTIFY_TIMEZONE
1537 fprintf(stderr
, "TZ \"%s\" matches system time zone \"%s\"\n",
1538 win32_tzmap
[i
].pgtzname
, tzname
);
1540 return win32_tzmap
[i
].pgtzname
;
1545 * Localized Windows versions return localized names for the timezone.
1546 * Scan the registry to find the English name, and then try matching
1547 * against our table again.
1549 memset(localtzname
, 0, sizeof(localtzname
));
1550 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
1551 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones",
1554 &rootKey
) != ERROR_SUCCESS
)
1556 #ifdef DEBUG_IDENTIFY_TIMEZONE
1557 fprintf(stderr
, "could not open registry key to identify system time zone: error code %lu\n",
1560 return NULL
; /* go to GMT */
1563 for (idx
= 0;; idx
++)
1572 memset(keyname
, 0, sizeof(keyname
));
1573 namesize
= sizeof(keyname
);
1574 if ((r
= RegEnumKeyEx(rootKey
,
1581 &lastwrite
)) != ERROR_SUCCESS
)
1583 if (r
== ERROR_NO_MORE_ITEMS
)
1585 #ifdef DEBUG_IDENTIFY_TIMEZONE
1586 fprintf(stderr
, "could not enumerate registry subkeys to identify system time zone: %d\n",
1592 if ((r
= RegOpenKeyEx(rootKey
, keyname
, 0, KEY_READ
, &key
)) != ERROR_SUCCESS
)
1594 #ifdef DEBUG_IDENTIFY_TIMEZONE
1595 fprintf(stderr
, "could not open registry subkey to identify system time zone: %d\n",
1601 memset(zonename
, 0, sizeof(zonename
));
1602 namesize
= sizeof(zonename
);
1603 if ((r
= RegQueryValueEx(key
, "Std", NULL
, NULL
, (unsigned char *) zonename
, &namesize
)) != ERROR_SUCCESS
)
1605 #ifdef DEBUG_IDENTIFY_TIMEZONE
1606 fprintf(stderr
, "could not query value for key \"std\" to identify system time zone \"%s\": %d\n",
1610 continue; /* Proceed to look at the next timezone */
1612 if (strcmp(tzname
, zonename
) == 0)
1615 strcpy(localtzname
, keyname
);
1619 memset(zonename
, 0, sizeof(zonename
));
1620 namesize
= sizeof(zonename
);
1621 if ((r
= RegQueryValueEx(key
, "Dlt", NULL
, NULL
, (unsigned char *) zonename
, &namesize
)) != ERROR_SUCCESS
)
1623 #ifdef DEBUG_IDENTIFY_TIMEZONE
1624 fprintf(stderr
, "could not query value for key \"dlt\" to identify system time zone \"%s\": %d\n",
1628 continue; /* Proceed to look at the next timezone */
1630 if (strcmp(tzname
, zonename
) == 0)
1632 /* Matched DST zone */
1633 strcpy(localtzname
, keyname
);
1641 RegCloseKey(rootKey
);
1645 /* Found a localized name, so scan for that one too */
1646 for (i
= 0; win32_tzmap
[i
].stdname
!= NULL
; i
++)
1648 if (strcmp(localtzname
, win32_tzmap
[i
].stdname
) == 0 ||
1649 strcmp(localtzname
, win32_tzmap
[i
].dstname
) == 0)
1651 #ifdef DEBUG_IDENTIFY_TIMEZONE
1652 fprintf(stderr
, "TZ \"%s\" matches localized system time zone \"%s\" (\"%s\")\n",
1653 win32_tzmap
[i
].pgtzname
, tzname
, localtzname
);
1655 return win32_tzmap
[i
].pgtzname
;
1660 #ifdef DEBUG_IDENTIFY_TIMEZONE
1661 fprintf(stderr
, "could not find a match for system time zone \"%s\"\n",
1664 return NULL
; /* go to GMT */
1670 * Return true if the given zone name is valid and is an "acceptable" zone.
1673 validate_zone(const char *tzname
)
1677 if (!tzname
|| !tzname
[0])
1680 tz
= pg_load_tz(tzname
);
1684 if (!pg_tz_acceptable(tz
))
1691 * Identify a suitable default timezone setting based on the environment.
1693 * The installation share_path must be passed in, as that is the default
1694 * location for the timezone database directory.
1696 * We first look to the TZ environment variable. If not found or not
1697 * recognized by our own code, we see if we can identify the timezone
1698 * from the behavior of the system timezone library. When all else fails,
1699 * return NULL, indicating that we should default to GMT.
1702 select_default_timezone(const char *share_path
)
1706 /* Initialize timezone directory path, if needed */
1708 snprintf(tzdirpath
, sizeof(tzdirpath
), "%s/timezone", share_path
);
1711 /* Check TZ environment variable */
1712 tzname
= getenv("TZ");
1713 if (validate_zone(tzname
))
1716 /* Nope, so try to identify the system timezone */
1717 tzname
= identify_system_timezone();
1718 if (validate_zone(tzname
))