*
* findtimezone.c
* Functions for determining the default timezone to use.
*
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
*
* IDENTIFICATION
* src/bin/initdb/findtimezone.c
*
* -------------------------------------------------------------------------
*/
#include "postgres_fe.h"
#include <time.h>
#include <fcntl.h>
#include "pgtz.h"
#include "bin/elog.h"
#define TZNAME_LENGTH 128
#define LOCAL_TZNAME_LENGTH 256
extern const char* select_default_timezone(const char* share_path);
#ifndef SYSTEMTZDIR
static char tzdirpath[MAXPGPATH];
#endif
#ifndef ERROR_LIMIT_LEN
#define ERROR_LIMIT_LEN 256
#endif
* Return full pathname of timezone data directory
*
* In this file, tzdirpath is assumed to be set up by select_default_timezone.
*/
static const char* pg_TZDIR(void)
{
#ifndef SYSTEMTZDIR
return tzdirpath;
#else
return SYSTEMTZDIR;
#endif
}
* Given a timezone name, open() the timezone data file. Return the
* file descriptor if successful, -1 if not.
*
* This is simpler than the backend function of the same name because
* we assume that the input string has the correct case already, so there
* is no need for case-folding. (This is obviously true if we got the file
* name from the filesystem to start with. The only other place it can come
* from is the environment variable TZ, and there seems no need to allow
* case variation in that; other programs aren't likely to.)
*
* If "canonname" is not NULL, then on success the canonical spelling of the
* given name is stored there (the buffer must be > TZ_STRLEN_MAX bytes!).
* This is redundant but kept for compatibility with the backend code.
*/
int pg_open_tzfile(const char* name, char* canonname)
{
char fullname[MAXPGPATH];
errno_t rc = 0;
if (canonname != NULL) {
rc = strncpy_s(canonname, TZ_STRLEN_MAX + 1, name, TZ_STRLEN_MAX);
securec_check_c(rc, "\0", "\0");
}
rc = strcpy_s(fullname, MAXPGPATH, pg_TZDIR());
securec_check_c(rc, "\0", "\0");
if (strlen(fullname) + 1 + strlen(name) >= MAXPGPATH)
return -1;
rc = strcat_s(fullname, MAXPGPATH, "/");
securec_check_c(rc, "\0", "\0");
rc = strcat_s(fullname, MAXPGPATH, name);
securec_check_c(rc, "\0", "\0");
return open(fullname, O_RDONLY | PG_BINARY, 0);
}
* Load a timezone definition.
* Does not verify that the timezone is acceptable!
*
* This corresponds to the backend's pg_tzset(), except that we only support
* one loaded timezone at a time.
*/
static pg_tz* pg_load_tz(const char* name)
{
static pg_tz tz;
errno_t rc = 0;
if (strlen(name) > TZ_STRLEN_MAX)
return NULL;
* "GMT" is always sent to tzparse(); see comments for pg_tzset().
*/
if (strcmp(name, "GMT") == 0) {
if (tzparse(name, &tz.state, TRUE) != 0) {
return NULL;
}
} else if (tzload(name, NULL, &tz.state, TRUE) != 0) {
if (name[0] == ':' || tzparse(name, &tz.state, FALSE) != 0) {
return NULL;
}
}
rc = strcpy_s(tz.TZname, (TZ_STRLEN_MAX + 1), name);
securec_check_c(rc, "\0", "\0");
return &tz;
}
* The following block of code attempts to determine which timezone in our
* timezone database is the best match for the active system timezone.
*
* On most systems, we rely on trying to match the observable behavior of
* the C library's localtime() function. The database zone that matches
* furthest into the past is the one to use. Often there will be several
* zones with identical rankings (since the Olson database assigns multiple
* names to many zones). We break ties arbitrarily by preferring shorter,
* then alphabetically earlier zone names.
*
* Win32's native knowledge about timezones appears to be too incomplete
* and too different from the Olson database for the above matching strategy
* to be of any use. But there is just a limited number of timezones
* available, so we can rely on a handmade mapping table instead.
*/
#ifndef WIN32
#define T_DAY ((time_t)(60 * 60 * 24))
#define T_WEEK ((time_t)(60 * 60 * 24 * 7))
#define T_MONTH ((time_t)(60 * 60 * 24 * 31))
#define MAX_TEST_TIMES (52 * 100)
struct tztry {
int n_test_times;
time_t test_times[MAX_TEST_TIMES];
};
* Get GMT offset from a system struct tm
*/
static int get_timezone_offset(struct tm* tm_val)
{
#if defined(HAVE_STRUCT_TM_TM_ZONE)
return tm_val->tm_gmtoff;
#elif defined(HAVE_INT_TIMEZONE)
return -TIMEZONE_GLOBAL;
#else
#error No way to determine TZ? Can this happen?
#endif
}
* Convenience subroutine to convert y/m/d to time_t (NOT pg_time_t)
*/
static time_t build_time_t(int year, int month, int day)
{
struct tm tm_val;
errno_t rc = 0;
rc = memset_s(&tm_val, sizeof(tm_val), 0, sizeof(tm_val));
securec_check_c(rc, "\0", "\0");
tm_val.tm_mday = day;
tm_val.tm_mon = month - 1;
tm_val.tm_year = year - 1900;
return mktime(&tm_val);
}
* Does a system tm value match one we computed ourselves?
*/
static bool compare_tm(struct tm* s, struct pg_tm* p)
{
if (s->tm_sec != p->tm_sec || s->tm_min != p->tm_min || s->tm_hour != p->tm_hour || s->tm_mday != p->tm_mday ||
s->tm_mon != p->tm_mon || s->tm_year != p->tm_year || s->tm_wday != p->tm_wday || s->tm_yday != p->tm_yday ||
s->tm_isdst != p->tm_isdst)
return false;
return true;
}
* See how well a specific timezone setting matches the system behavior
*
* We score a timezone setting according to the number of test times it
* matches. (The test times are ordered later-to-earlier, but this routine
* doesn't actually know that; it just scans until the first non-match.)
*
* We return -1 for a completely unusable setting; this is worse than the
* score of zero for a setting that works but matches not even the first
* test time.
*/
static int score_timezone(const char* tz_name, struct tztry* tt)
{
int i;
pg_time_t pgtt;
struct tm* systm = NULL;
struct pg_tm* pgtm = NULL;
char cbuf[TZ_STRLEN_MAX + 1];
pg_tz* tz = NULL;
errno_t rc = 0;
tz = pg_load_tz(tz_name);
if (tz == NULL)
return -1;
if (!pg_tz_acceptable(tz)) {
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("Reject TZ \"%s\": uses leap seconds\n", tz_name);
#endif
return -1;
}
for (i = 0; i < tt->n_test_times; i++) {
pgtt = (pg_time_t)(tt->test_times[i]);
pgtm = pg_localtime(&pgtt, tz);
if (pgtm == NULL) {
return -1;
}
systm = localtime(&(tt->test_times[i]));
if (systm == NULL) {
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("TZ \"%s\" scores %d: at %ld %04d-%02d-%02d %02d:%02d:%02d %s, system had no data\n",
tz_name,
i,
(long)pgtt,
pgtm->tm_year + 1900,
pgtm->tm_mon + 1,
pgtm->tm_mday,
pgtm->tm_hour,
pgtm->tm_min,
pgtm->tm_sec,
pgtm->tm_isdst ? "dst" : "std");
#endif
return i;
}
if (!compare_tm(systm, pgtm)) {
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("TZ \"%s\" scores %d: at %ld %04d-%02d-%02d %02d:%02d:%02d %s versus %04d-%02d-%02d "
"%02d:%02d:%02d %s\n",
tz_name,
i,
(long)pgtt,
pgtm->tm_year + 1900,
pgtm->tm_mon + 1,
pgtm->tm_mday,
pgtm->tm_hour,
pgtm->tm_min,
pgtm->tm_sec,
pgtm->tm_isdst ? "dst" : "std",
systm->tm_year + 1900,
systm->tm_mon + 1,
systm->tm_mday,
systm->tm_hour,
systm->tm_min,
systm->tm_sec,
systm->tm_isdst ? "dst" : "std");
#endif
return i;
}
if (systm->tm_isdst >= 0) {
if (pgtm->tm_zone == NULL)
return -1;
rc = memset_s(cbuf, (TZ_STRLEN_MAX + 1), 0, sizeof(cbuf));
securec_check_c(rc, "\0", "\0");
(void)strftime(cbuf, sizeof(cbuf) - 1, "%Z", systm);
if (strcmp(cbuf, pgtm->tm_zone) != 0) {
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr(
"TZ \"%s\" scores %d: at %ld \"%s\" versus \"%s\"\n", tz_name, i, (long)pgtt, pgtm->tm_zone, cbuf);
#endif
return i;
}
}
}
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("TZ \"%s\" gets max score %d\n", tz_name, i);
#endif
return i;
}
* Try to identify a timezone name (in our terminology) that best matches the
* observed behavior of the system timezone library. We cannot assume that
* the system TZ environment setting (if indeed there is one) matches our
* terminology, so we ignore it and just look at what localtime() returns.
*/
const char* identify_system_timezone(void)
{
static char resultbuf[TZ_STRLEN_MAX + 1];
time_t tnow;
time_t t;
struct tztry tt = {0};
struct tm* tm_val = NULL;
int thisyear;
int bestscore;
char tmptzdir[MAXPGPATH];
int std_ofs;
char std_zone_name[TZ_STRLEN_MAX + 1], dst_zone_name[TZ_STRLEN_MAX + 1];
char cbuf[TZ_STRLEN_MAX + 1];
errno_t rc = 0;
int nRet = 0;
tzset();
* Set up the list of dates to be probed to see how well our timezone
* matches the system zone. We first probe January and July of the
* current year; this serves to quickly eliminate the vast majority of the
* TZ database entries. If those dates match, we probe every week for 100
* years backwards from the current July. (Weekly resolution is good
* enough to identify DST transition rules, since everybody switches on
* Sundays.) This is sufficient to cover most of the Unix time_t range,
* and we don't want to look further than that since many systems won't
* have sane TZ behavior further back anyway. The further back the zone
* matches, the better we score it. This may seem like a rather random
* way of doing things, but experience has shown that system-supplied
* timezone definitions are likely to have DST behavior that is right for
* the recent past and not so accurate further back. Scoring in this way
* allows us to recognize zones that have some commonality with the Olson
* database, without insisting on exact match. (Note: we probe Thursdays,
* not Sundays, to avoid triggering DST-transition bugs in localtime
* itself.)
*/
tnow = time(NULL);
tm_val = localtime(&tnow);
if (tm_val == NULL) {
return NULL;
}
thisyear = tm_val->tm_year + 1900;
t = build_time_t(thisyear, 1, 15);
* Round back to GMT midnight Thursday. This depends on the knowledge
* that the time_t origin is Thu Jan 01 1970. (With a different origin
* we'd be probing some other day of the week, but it wouldn't matter
* anyway unless localtime() had DST-transition bugs.)
*/
t -= (t % T_WEEK);
tt.n_test_times = 0;
tt.test_times[tt.n_test_times++] = t;
t = build_time_t(thisyear, 7, 15);
t -= (t % T_WEEK);
tt.test_times[tt.n_test_times++] = t;
while (tt.n_test_times < MAX_TEST_TIMES) {
t -= T_WEEK;
tt.test_times[tt.n_test_times++] = t;
}
rc = strncpy_s(tmptzdir, MAXPGPATH, pg_TZDIR(), MAXPGPATH - 1);
securec_check_c(rc, "\0", "\0");
bestscore = -1;
resultbuf[0] = '\0';
scan_available_timezones(tmptzdir, tmptzdir + strlen(tmptzdir) + 1, &tt, &bestscore, resultbuf);
if (bestscore > 0) {
if (strcmp(resultbuf, "Factory") == 0)
return NULL;
return resultbuf;
}
* Couldn't find a match in the database, so next we try constructed zone
* names (like "PST8PDT").
*
* First we need to determine the names of the local standard and daylight
* zones. The idea here is to scan forward from today until we have seen
* both zones, if both are in use.
*/
rc = memset_s(std_zone_name, (TZ_STRLEN_MAX + 1), 0, sizeof(std_zone_name));
securec_check_c(rc, "\0", "\0");
rc = memset_s(dst_zone_name, (TZ_STRLEN_MAX + 1), 0, sizeof(dst_zone_name));
securec_check_c(rc, "\0", "\0");
std_ofs = 0;
tnow = time(NULL);
* Round back to a GMT midnight so results don't depend on local time of
* day
*/
tnow -= (tnow % T_DAY);
* We have to look a little further ahead than one year, in case today is
* just past a DST boundary that falls earlier in the year than the next
* similar boundary. Arbitrarily scan up to 14 months.
*/
for (t = tnow; t <= tnow + T_MONTH * 14; t += T_MONTH) {
tm_val = localtime(&t);
if (tm_val == NULL) {
continue;
}
if (tm_val->tm_isdst < 0) {
continue;
}
if (tm_val->tm_isdst == 0 && std_zone_name[0] == '\0') {
rc = memset_s(cbuf, (TZ_STRLEN_MAX + 1), 0, sizeof(cbuf));
securec_check_c(rc, "\0", "\0");
(void)strftime(cbuf, sizeof(cbuf) - 1, "%Z", tm_val);
rc = strcpy_s(std_zone_name, (TZ_STRLEN_MAX + 1), cbuf);
securec_check_c(rc, "\0", "\0");
std_ofs = get_timezone_offset(tm_val);
}
if (tm_val->tm_isdst > 0 && dst_zone_name[0] == '\0') {
rc = memset_s(cbuf, (TZ_STRLEN_MAX + 1), 0, sizeof(cbuf));
securec_check_c(rc, "\0", "\0");
(void)strftime(cbuf, sizeof(cbuf) - 1, "%Z", tm_val);
rc = strcpy_s(dst_zone_name, (TZ_STRLEN_MAX + 1), cbuf);
securec_check_c(rc, "\0", "\0");
}
if (std_zone_name[0] && dst_zone_name[0])
break;
}
if (std_zone_name[0] == '\0') {
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("could not determine system time zone\n");
#endif
return NULL;
}
if (dst_zone_name[0] != '\0') {
nRet = snprintf_s(resultbuf,
sizeof(resultbuf),
sizeof(resultbuf) - 1,
"%s%d%s",
std_zone_name,
-std_ofs / 3600,
dst_zone_name);
securec_check_ss_c(nRet, "\0", "\0");
if (score_timezone(resultbuf, &tt) > 0)
return resultbuf;
}
rc = strcpy_s(resultbuf, (TZ_STRLEN_MAX + 1), std_zone_name);
securec_check_c(rc, "\0", "\0");
if (score_timezone(resultbuf, &tt) > 0)
return resultbuf;
nRet = snprintf_s(resultbuf, sizeof(resultbuf), sizeof(resultbuf) - 1, "%s%d", std_zone_name, -std_ofs / 3600);
securec_check_ss_c(nRet, "\0", "\0");
if (score_timezone(resultbuf, &tt) > 0)
return resultbuf;
* Did not find the timezone. Fallback to use a GMT zone. Note that the
* Olson timezone database names the GMT-offset zones in POSIX style: plus
* is west of Greenwich. It's unfortunate that this is opposite of SQL
* conventions. Should we therefore change the names? Probably not...
*/
nRet = snprintf_s(
resultbuf, sizeof(resultbuf), sizeof(resultbuf) - 1, "Etc/GMT%s%d", (-std_ofs > 0) ? "+" : "", -std_ofs / 3600);
securec_check_ss_c(nRet, "\0", "\0");
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("could not recognize system time zone, using \"%s\"\n", resultbuf);
#endif
return resultbuf;
}
* Recursively scan the timezone database looking for the best match to
* the system timezone behavior.
*
* tzdir points to a buffer of size MAXPGPATH. On entry, it holds the
* pathname of a directory containing TZ files. We internally modify it
* to hold pathnames of sub-directories and files, but must restore it
* to its original contents before exit.
*
* tzdirsub points to the part of tzdir that represents the subfile name
* (ie, tzdir + the original directory name length, plus one for the
* first added '/').
*
* tt tells about the system timezone behavior we need to match.
*
* *bestscore and *bestzonename on entry hold the best score found so far
* and the name of the best zone. We overwrite them if we find a better
* score. bestzonename must be a buffer of length TZ_STRLEN_MAX + 1.
*/
void scan_available_timezones(char* tzdir, char* tzdirsub, struct tztry* tt, int* bestscore, char* bestzonename)
{
int tzdir_orig_len = strlen(tzdir);
char** names;
char** namep;
names = pgfnames(tzdir);
if (names == NULL) {
return;
}
for (namep = names; *namep != NULL; namep++) {
char* name = *namep;
struct stat statbuf;
int nRet = 0;
if (name[0] == '.') {
continue;
}
nRet = snprintf_s(
tzdir + tzdir_orig_len, (MAXPGPATH - tzdir_orig_len), MAXPGPATH - tzdir_orig_len - 1, "/%s", name);
securec_check_ss_c(nRet, "\0", "\0");
if (stat(tzdir, &statbuf) != 0) {
#ifdef DEBUG_IDENTIFY_TIMEZONE
char errBuffer[ERROR_LIMIT_LEN];
write_stderr("could not stat \"%s\": %s\n", tzdir, pqStrerror(errno, errBuffer, ERROR_LIMIT_LEN));
#endif
tzdir[tzdir_orig_len] = '\0';
continue;
}
if (S_ISDIR(statbuf.st_mode)) {
scan_available_timezones(tzdir, tzdirsub, tt, bestscore, bestzonename);
} else {
int score = score_timezone(tzdirsub, tt);
if (score > *bestscore) {
*bestscore = score;
nRet = strncpy_s(bestzonename, TZ_STRLEN_MAX + 1, tzdirsub, TZ_STRLEN_MAX);
securec_check_ss_c(nRet, "\0", "\0");
} else if (score == *bestscore) {
if (strlen(tzdirsub) < strlen(bestzonename) ||
(strlen(tzdirsub) == strlen(bestzonename) && strcmp(tzdirsub, bestzonename) < 0)) {
nRet = strncpy_s(bestzonename, TZ_STRLEN_MAX + 1, tzdirsub, TZ_STRLEN_MAX);
securec_check_ss_c(nRet, "\0", "\0");
}
}
}
tzdir[tzdir_orig_len] = '\0';
}
pgfnames_cleanup(names);
}
#else
static const struct {
const char* stdname;
const char* dstname;
const char* pgtzname;
} win32_tzmap[] =
{
* This list was built from the contents of the registry at
* HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time
* Zones on Windows 2003 R2.
*
* The zones have been matched to Olson timezones by looking at the cities
* listed in the win32 display name (in the comment here) in most cases.
*/
{"Afghanistan Standard Time", "Afghanistan Daylight Time", "Asia/Kabul"},
{"Alaskan Standard Time", "Alaskan Daylight Time", "US/Alaska"},
{"Arab Standard Time", "Arab Daylight Time", "Asia/Kuwait"},
{"Arabian Standard Time", "Arabian Daylight Time", "Asia/Muscat"},
{"Arabic Standard Time", "Arabic Daylight Time", "Asia/Baghdad"},
{"Argentina Standard Time", "Argentina Daylight Time", "America/Buenos_Aires"},
{"Armenian Standard Time", "Armenian Daylight Time", "Asia/Yerevan"},
{"Atlantic Standard Time",
"Atlantic Daylight Time",
"Canada/Atlantic"},
{"AUS Central Standard Time", "AUS Central Daylight Time", "Australia/Darwin"},
{"AUS Eastern Standard Time",
"AUS Eastern Daylight Time",
"Australia/Canberra"},
{"Azerbaijan Standard Time", "Azerbaijan Daylight Time", "Asia/Baku"},
{"Azores Standard Time", "Azores Daylight Time", "Atlantic/Azores"},
{"Bangladesh Standard Time", "Bangladesh Daylight Time", "Asia/Dhaka"},
{"Canada Central Standard Time",
"Canada Central Daylight Time",
"Canada/Saskatchewan"},
{"Cape Verde Standard Time",
"Cape Verde Daylight Time",
"Atlantic/Cape_Verde"},
{"Caucasus Standard Time", "Caucasus Daylight Time", "Asia/Baku"},
{"Cen. Australia Standard Time",
"Cen. Australia Daylight Time",
"Australia/Adelaide"},
{"Central America Standard Time", "Central America Daylight Time", "CST6"},
{"Central Asia Standard Time", "Central Asia Daylight Time", "Asia/Dhaka"},
{"Central Brazilian Standard Time",
"Central Brazilian Daylight Time",
"America/Cuiaba"},
{"Central Europe Standard Time",
"Central Europe Daylight Time",
"Europe/Belgrade"},
* Ljubljana, Prague */
{"Central European Standard Time",
"Central European Daylight Time",
"Europe/Sarajevo"},
* Zagreb */
{"Central Pacific Standard Time",
"Central Pacific Daylight Time",
"Pacific/Noumea"},
* Caledonia */
{"Central Standard Time", "Central Daylight Time", "US/Central"},
{"Central Standard Time (Mexico)",
"Central Daylight Time (Mexico)",
"America/Mexico_City"},
* Monterrey - New */
{"China Standard Time", "China Daylight Time", "Asia/Hong_Kong"},
* Urumqi */
{"Dateline Standard Time",
"Dateline Daylight Time",
"Etc/GMT+12"},
{"E. Africa Standard Time", "E. Africa Daylight Time", "Africa/Nairobi"},
{"E. Australia Standard Time", "E. Australia Daylight Time", "Australia/Brisbane"},
{"E. Europe Standard Time", "E. Europe Daylight Time", "Europe/Bucharest"},
{"E. South America Standard Time",
"E. South America Daylight Time",
"America/Araguaina"},
{"Eastern Standard Time", "Eastern Daylight Time", "US/Eastern"},
{"Egypt Standard Time", "Egypt Daylight Time", "Africa/Cairo"},
{"Ekaterinburg Standard Time",
"Ekaterinburg Daylight Time",
"Asia/Yekaterinburg"},
{"Fiji Standard Time", "Fiji Daylight Time", "Pacific/Fiji"},
{"FLE Standard Time", "FLE Daylight Time", "Europe/Helsinki"},
* Tallinn, Vilnius */
{"Georgian Standard Time", "Georgian Daylight Time", "Asia/Tbilisi"},
{"GMT Standard Time", "GMT Daylight Time", "Europe/London"},
* Edinburgh, Lisbon, London */
{"Greenland Standard Time", "Greenland Daylight Time", "America/Godthab"},
{"Greenwich Standard Time", "Greenwich Daylight Time", "Africa/Casablanca"},
{"GTB Standard Time", "GTB Daylight Time", "Europe/Athens"},
{"Hawaiian Standard Time", "Hawaiian Daylight Time", "US/Hawaii"},
{"India Standard Time", "India Daylight Time", "Asia/Calcutta"},
* Delhi */
{"Iran Standard Time", "Iran Daylight Time", "Asia/Tehran"},
{"Jerusalem Standard Time", "Jerusalem Daylight Time", "Asia/Jerusalem"},
{"Jordan Standard Time", "Jordan Daylight Time", "Asia/Amman"},
{"Kamchatka Standard Time",
"Kamchatka Daylight Time",
"Asia/Kamchatka"},
{"Korea Standard Time", "Korea Daylight Time", "Asia/Seoul"},
{"Mauritius Standard Time", "Mauritius Daylight Time", "Indian/Mauritius"},
{"Mexico Standard Time", "Mexico Daylight Time", "America/Mexico_City"},
* City, Monterrey */
{"Mexico Standard Time 2",
"Mexico Daylight Time 2",
"America/Chihuahua"},
{"Mid-Atlantic Standard Time",
"Mid-Atlantic Daylight Time",
"Atlantic/South_Georgia"},
{"Middle East Standard Time", "Middle East Daylight Time", "Asia/Beirut"},
{"Montevideo Standard Time", "Montevideo Daylight Time", "America/Montevideo"},
{"Morocco Standard Time", "Morocco Daylight Time", "Africa/Casablanca"},
{"Mountain Standard Time",
"Mountain Daylight Time",
"US/Mountain"},
{"Mountain Standard Time (Mexico)",
"Mountain Daylight Time (Mexico)",
"America/Chihuahua"},
* New */
{"Myanmar Standard Time", "Myanmar Daylight Time", "Asia/Rangoon"},
{"N. Central Asia Standard Time",
"N. Central Asia Daylight Time",
"Asia/Novosibirsk"},
{"Namibia Standard Time", "Namibia Daylight Time", "Africa/Windhoek"},
{"Nepal Standard Time", "Nepal Daylight Time", "Asia/Katmandu"},
{"New Zealand Standard Time",
"New Zealand Daylight Time",
"Pacific/Auckland"},
{"Newfoundland Standard Time",
"Newfoundland Daylight Time",
"Canada/Newfoundland"},
{"North Asia East Standard Time",
"North Asia East Daylight Time",
"Asia/Irkutsk"},
{"North Asia Standard Time", "North Asia Daylight Time", "Asia/Krasnoyarsk"},
{"Pacific SA Standard Time", "Pacific SA Daylight Time", "America/Santiago"},
{"Pacific Standard Time", "Pacific Daylight Time", "US/Pacific"},
* Tijuana */
{"Pacific Standard Time (Mexico)",
"Pacific Daylight Time (Mexico)",
"America/Tijuana"},
{"Pakistan Standard Time", "Pakistan Daylight Time", "Asia/Karachi"},
{"Paraguay Standard Time", "Paraguay Daylight Time", "America/Asuncion"},
{"Romance Standard Time", "Romance Daylight Time", "Europe/Brussels"},
* Madrid, Paris */
{"Russian Standard Time", "Russian Daylight Time", "Europe/Moscow"},
* Volgograd */
{"SA Eastern Standard Time",
"SA Eastern Daylight Time",
"America/Buenos_Aires"},
{"SA Pacific Standard Time",
"SA Pacific Daylight Time",
"America/Bogota"},
{"SA Western Standard Time", "SA Western Daylight Time", "America/Caracas"},
{"Samoa Standard Time", "Samoa Daylight Time", "Pacific/Midway"},
{"SE Asia Standard Time", "SE Asia Daylight Time", "Asia/Bangkok"},
{"Malay Peninsula Standard Time",
"Malay Peninsula Daylight Time",
"Asia/Kuala_Lumpur"},
{"South Africa Standard Time",
"South Africa Daylight Time",
"Africa/Harare"},
{"Sri Lanka Standard Time", "Sri Lanka Daylight Time", "Asia/Colombo"},
{"Taipei Standard Time", "Taipei Daylight Time", "Asia/Taipei"},
{"Tasmania Standard Time", "Tasmania Daylight Time", "Australia/Hobart"},
{"Tokyo Standard Time", "Tokyo Daylight Time", "Asia/Tokyo"},
{"Tonga Standard Time", "Tonga Daylight Time", "Pacific/Tongatapu"},
{
"Ulaanbaatar Standard Time",
"Ulaanbaatar Daylight Time",
"Asia/Ulaanbaatar",
},
{"US Eastern Standard Time", "US Eastern Daylight Time", "US/Eastern"},
{"US Mountain Standard Time", "US Mountain Daylight Time", "US/Arizona"},
{"Coordinated Universal Time", "Coordinated Universal Time", "UTC"},
{"UTC+12", "UTC+12", "Etc/GMT+12"},
{"UTC-02", "UTC-02", "Etc/GMT-02"},
{"UTC-11", "UTC-11", "Etc/GMT-11"},
{
"Venezuela Standard Time",
"Venezuela Daylight Time",
"America/Caracas",
},
{"Vladivostok Standard Time", "Vladivostok Daylight Time", "Asia/Vladivostok"},
{"W. Australia Standard Time", "W. Australia Daylight Time", "Australia/Perth"},
#ifdef NOT_USED
{"W. Central Africa Standard Time",
"W. Central Africa Daylight Time",
"WAT"},
#endif
{"W. Europe Standard Time", "W. Europe Daylight Time", "CET"},
* Stockholm, Vienna */
{"West Asia Standard Time",
"West Asia Daylight Time",
"Asia/Karachi"},
{"West Pacific Standard Time",
"West Pacific Daylight Time",
"Pacific/Guam"},
{"Yakutsk Standard Time", "Yakutsk Daylight Time", "Asia/Yakutsk"},
{NULL, NULL, NULL}};
const char* identify_system_timezone(void)
{
int i;
char tzname[TZNAME_LENGTH];
char localtzname[LOCAL_TZNAME_LENGTH];
time_t t = time(NULL);
struct tm* tm = localtime(&t);
HKEY rootKey = NULL;
int idx;
errno_t rc = 0;
if (tm == NULL) {
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("could not identify system time zone: localtime() failed\n");
#endif
return NULL;
}
rc = memset_s(tzname, TZNAME_LENGTH, 0, sizeof(tzname));
securec_check_c(rc, "\0", "\0");
(void)strftime(tzname, sizeof(tzname) - 1, "%Z", tm);
for (i = 0; win32_tzmap[i].stdname != NULL; i++) {
if (strcmp(tzname, win32_tzmap[i].stdname) == 0 || strcmp(tzname, win32_tzmap[i].dstname) == 0) {
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("TZ \"%s\" matches system time zone \"%s\"\n", win32_tzmap[i].pgtzname, tzname);
#endif
return win32_tzmap[i].pgtzname;
}
}
* Localized Windows versions return localized names for the timezone.
* Scan the registry to find the English name, and then try matching
* against our table again.
*/
rc = memset_s(localtzname, LOCAL_TZNAME_LENGTH, 0, sizeof(localtzname));
securec_check_c(rc, "\0", "\0");
if (RegOpenKeyEx(
HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &rootKey) !=
ERROR_SUCCESS) {
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("could not open registry key to identify system time zone: error code %lu\n", GetLastError());
#endif
return NULL;
}
for (idx = 0;; idx++) {
char keyname[LOCAL_TZNAME_LENGTH];
char zonename[LOCAL_TZNAME_LENGTH];
DWORD namesize;
FILETIME lastwrite;
HKEY key = NULL;
LONG r;
rc = memset_s(keyname, LOCAL_TZNAME_LENGTH, 0, sizeof(keyname));
securec_check_c(rc, "\0", "\0");
namesize = sizeof(keyname);
if ((r = RegEnumKeyEx(rootKey, idx, keyname, &namesize, NULL, NULL, NULL, &lastwrite)) != ERROR_SUCCESS) {
if (r == ERROR_NO_MORE_ITEMS)
break;
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("could not enumerate registry subkeys to identify system time zone: %d\n", (int)r);
#endif
break;
}
if ((r = RegOpenKeyEx(rootKey, keyname, 0, KEY_READ, &key)) != ERROR_SUCCESS) {
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("could not open registry subkey to identify system time zone: %d\n", (int)r);
#endif
break;
}
rc = memset_s(zonename, LOCAL_TZNAME_LENGTH, 0, sizeof(zonename));
securec_check_c(rc, "\0", "\0");
namesize = sizeof(zonename);
if ((r = RegQueryValueEx(key, "Std", NULL, NULL, (unsigned char*)zonename, &namesize)) != ERROR_SUCCESS) {
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr(
"could not query value for key \"std\" to identify system time zone \"%s\": %d\n", keyname, (int)r);
#endif
RegCloseKey(key);
continue;
}
if (strcmp(tzname, zonename) == 0) {
rc = strcpy_s(localtzname, LOCAL_TZNAME_LENGTH, keyname);
securec_check_c(rc, "\0", "\0");
RegCloseKey(key);
break;
}
rc = memset_s(zonename, LOCAL_TZNAME_LENGTH, 0, sizeof(zonename));
securec_check_c(rc, "\0", "\0");
namesize = sizeof(zonename);
if ((r = RegQueryValueEx(key, "Dlt", NULL, NULL, (unsigned char*)zonename, &namesize)) != ERROR_SUCCESS) {
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr(
"could not query value for key \"dlt\" to identify system time zone \"%s\": %d\n", keyname, (int)r);
#endif
RegCloseKey(key);
continue;
}
if (strcmp(tzname, zonename) == 0) {
rc = strcpy_s(localtzname, LOCAL_TZNAME_LENGTH, keyname);
securec_check_c(rc, "\0", "\0");
RegCloseKey(key);
break;
}
RegCloseKey(key);
}
RegCloseKey(rootKey);
if (localtzname[0]) {
for (i = 0; win32_tzmap[i].stdname != NULL; i++) {
if (strcmp(localtzname, win32_tzmap[i].stdname) == 0 || strcmp(localtzname, win32_tzmap[i].dstname) == 0) {
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("TZ \"%s\" matches localized system time zone \"%s\" (\"%s\")\n",
win32_tzmap[i].pgtzname,
tzname,
localtzname);
#endif
return win32_tzmap[i].pgtzname;
}
}
}
#ifdef DEBUG_IDENTIFY_TIMEZONE
write_stderr("could not find a match for system time zone \"%s\"\n", tzname);
#endif
return NULL;
}
#endif
* Return true if the given zone name is valid and is an "acceptable" zone.
*/
static bool validate_zone(const char* tz_name)
{
pg_tz* tz = NULL;
if ((tz_name == NULL) || !tz_name[0]) {
return false;
}
tz = pg_load_tz(tz_name);
if (tz == NULL) {
return false;
}
if (!pg_tz_acceptable(tz)) {
return false;
}
return true;
}
* Identify a suitable default timezone setting based on the environment.
*
* The installation share_path must be passed in, as that is the default
* location for the timezone database directory.
*
* We first look to the TZ environment variable. If not found or not
* recognized by our own code, we see if we can identify the timezone
* from the behavior of the system timezone library. When all else fails,
* return NULL, indicating that we should default to GMT.
*/
const char* select_default_timezone(const char* share_path)
{
const char* tz_name = NULL;
int nRet = 0;
#ifndef SYSTEMTZDIR
nRet = snprintf_s(tzdirpath, sizeof(tzdirpath), sizeof(tzdirpath) - 1, "%s/timezone", share_path);
securec_check_ss_c(nRet, "\0", "\0");
#endif
tz_name = getenv("TZ");
if (validate_zone(tz_name))
return tz_name;
tz_name = identify_system_timezone();
if (validate_zone(tz_name))
return tz_name;
return NULL;
}