Header And Logo

PostgreSQL
| The world's most advanced open source database.

findtimezone.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * findtimezone.c
00004  *    Functions for determining the default timezone to use.
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  *
00008  * IDENTIFICATION
00009  *    src/bin/initdb/findtimezone.c
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 #include "postgres_fe.h"
00014 
00015 #include <fcntl.h>
00016 #include <sys/stat.h>
00017 #include <time.h>
00018 
00019 #include "pgtz.h"
00020 
00021 /* Ideally this would be in a .h file, but it hardly seems worth the trouble */
00022 extern const char *select_default_timezone(const char *share_path);
00023 
00024 
00025 #ifndef SYSTEMTZDIR
00026 static char tzdirpath[MAXPGPATH];
00027 #endif
00028 
00029 
00030 /*
00031  * Return full pathname of timezone data directory
00032  *
00033  * In this file, tzdirpath is assumed to be set up by select_default_timezone.
00034  */
00035 static const char *
00036 pg_TZDIR(void)
00037 {
00038 #ifndef SYSTEMTZDIR
00039     /* normal case: timezone stuff is under our share dir */
00040     return tzdirpath;
00041 #else
00042     /* we're configured to use system's timezone database */
00043     return SYSTEMTZDIR;
00044 #endif
00045 }
00046 
00047 
00048 /*
00049  * Given a timezone name, open() the timezone data file.  Return the
00050  * file descriptor if successful, -1 if not.
00051  *
00052  * This is simpler than the backend function of the same name because
00053  * we assume that the input string has the correct case already, so there
00054  * is no need for case-folding.  (This is obviously true if we got the file
00055  * name from the filesystem to start with.  The only other place it can come
00056  * from is the environment variable TZ, and there seems no need to allow
00057  * case variation in that; other programs aren't likely to.)
00058  *
00059  * If "canonname" is not NULL, then on success the canonical spelling of the
00060  * given name is stored there (the buffer must be > TZ_STRLEN_MAX bytes!).
00061  * This is redundant but kept for compatibility with the backend code.
00062  */
00063 int
00064 pg_open_tzfile(const char *name, char *canonname)
00065 {
00066     char        fullname[MAXPGPATH];
00067 
00068     if (canonname)
00069         strlcpy(canonname, name, TZ_STRLEN_MAX + 1);
00070 
00071     strcpy(fullname, pg_TZDIR());
00072     if (strlen(fullname) + 1 + strlen(name) >= MAXPGPATH)
00073         return -1;              /* not gonna fit */
00074     strcat(fullname, "/");
00075     strcat(fullname, name);
00076 
00077     return open(fullname, O_RDONLY | PG_BINARY, 0);
00078 }
00079 
00080 
00081 
00082 /*
00083  * Load a timezone definition.
00084  * Does not verify that the timezone is acceptable!
00085  *
00086  * This corresponds to the backend's pg_tzset(), except that we only support
00087  * one loaded timezone at a time.
00088  */
00089 static pg_tz *
00090 pg_load_tz(const char *name)
00091 {
00092     static pg_tz tz;
00093 
00094     if (strlen(name) > TZ_STRLEN_MAX)
00095         return NULL;            /* not going to fit */
00096 
00097     /*
00098      * "GMT" is always sent to tzparse(); see comments for pg_tzset().
00099      */
00100     if (strcmp(name, "GMT") == 0)
00101     {
00102         if (tzparse(name, &tz.state, TRUE) != 0)
00103         {
00104             /* This really, really should not happen ... */
00105             return NULL;
00106         }
00107     }
00108     else if (tzload(name, NULL, &tz.state, TRUE) != 0)
00109     {
00110         if (name[0] == ':' || tzparse(name, &tz.state, FALSE) != 0)
00111         {
00112             return NULL;        /* unknown timezone */
00113         }
00114     }
00115 
00116     strcpy(tz.TZname, name);
00117 
00118     return &tz;
00119 }
00120 
00121 
00122 /*
00123  * The following block of code attempts to determine which timezone in our
00124  * timezone database is the best match for the active system timezone.
00125  *
00126  * On most systems, we rely on trying to match the observable behavior of
00127  * the C library's localtime() function.  The database zone that matches
00128  * furthest into the past is the one to use.  Often there will be several
00129  * zones with identical rankings (since the Olson database assigns multiple
00130  * names to many zones).  We break ties arbitrarily by preferring shorter,
00131  * then alphabetically earlier zone names.
00132  *
00133  * Win32's native knowledge about timezones appears to be too incomplete
00134  * and too different from the Olson database for the above matching strategy
00135  * to be of any use. But there is just a limited number of timezones
00136  * available, so we can rely on a handmade mapping table instead.
00137  */
00138 
00139 #ifndef WIN32
00140 
00141 #define T_DAY   ((time_t) (60*60*24))
00142 #define T_WEEK  ((time_t) (60*60*24*7))
00143 #define T_MONTH ((time_t) (60*60*24*31))
00144 
00145 #define MAX_TEST_TIMES (52*100) /* 100 years */
00146 
00147 struct tztry
00148 {
00149     int         n_test_times;
00150     time_t      test_times[MAX_TEST_TIMES];
00151 };
00152 
00153 static void scan_available_timezones(char *tzdir, char *tzdirsub,
00154                          struct tztry * tt,
00155                          int *bestscore, char *bestzonename);
00156 
00157 
00158 /*
00159  * Get GMT offset from a system struct tm
00160  */
00161 static int
00162 get_timezone_offset(struct tm * tm)
00163 {
00164 #if defined(HAVE_STRUCT_TM_TM_ZONE)
00165     return tm->tm_gmtoff;
00166 #elif defined(HAVE_INT_TIMEZONE)
00167     return -TIMEZONE_GLOBAL;
00168 #else
00169 #error No way to determine TZ? Can this happen?
00170 #endif
00171 }
00172 
00173 /*
00174  * Convenience subroutine to convert y/m/d to time_t (NOT pg_time_t)
00175  */
00176 static time_t
00177 build_time_t(int year, int month, int day)
00178 {
00179     struct tm   tm;
00180 
00181     memset(&tm, 0, sizeof(tm));
00182     tm.tm_mday = day;
00183     tm.tm_mon = month - 1;
00184     tm.tm_year = year - 1900;
00185 
00186     return mktime(&tm);
00187 }
00188 
00189 /*
00190  * Does a system tm value match one we computed ourselves?
00191  */
00192 static bool
00193 compare_tm(struct tm * s, struct pg_tm * p)
00194 {
00195     if (s->tm_sec != p->tm_sec ||
00196         s->tm_min != p->tm_min ||
00197         s->tm_hour != p->tm_hour ||
00198         s->tm_mday != p->tm_mday ||
00199         s->tm_mon != p->tm_mon ||
00200         s->tm_year != p->tm_year ||
00201         s->tm_wday != p->tm_wday ||
00202         s->tm_yday != p->tm_yday ||
00203         s->tm_isdst != p->tm_isdst)
00204         return false;
00205     return true;
00206 }
00207 
00208 /*
00209  * See how well a specific timezone setting matches the system behavior
00210  *
00211  * We score a timezone setting according to the number of test times it
00212  * matches.  (The test times are ordered later-to-earlier, but this routine
00213  * doesn't actually know that; it just scans until the first non-match.)
00214  *
00215  * We return -1 for a completely unusable setting; this is worse than the
00216  * score of zero for a setting that works but matches not even the first
00217  * test time.
00218  */
00219 static int
00220 score_timezone(const char *tzname, struct tztry * tt)
00221 {
00222     int         i;
00223     pg_time_t   pgtt;
00224     struct tm  *systm;
00225     struct pg_tm *pgtm;
00226     char        cbuf[TZ_STRLEN_MAX + 1];
00227     pg_tz      *tz;
00228 
00229     /* Load timezone definition */
00230     tz = pg_load_tz(tzname);
00231     if (!tz)
00232         return -1;              /* unrecognized zone name */
00233 
00234     /* Reject if leap seconds involved */
00235     if (!pg_tz_acceptable(tz))
00236     {
00237 #ifdef DEBUG_IDENTIFY_TIMEZONE
00238         fprintf(stderr, "Reject TZ \"%s\": uses leap seconds\n", tzname);
00239 #endif
00240         return -1;
00241     }
00242 
00243     /* Check for match at all the test times */
00244     for (i = 0; i < tt->n_test_times; i++)
00245     {
00246         pgtt = (pg_time_t) (tt->test_times[i]);
00247         pgtm = pg_localtime(&pgtt, tz);
00248         if (!pgtm)
00249             return -1;          /* probably shouldn't happen */
00250         systm = localtime(&(tt->test_times[i]));
00251         if (!systm)
00252         {
00253 #ifdef DEBUG_IDENTIFY_TIMEZONE
00254             fprintf(stderr, "TZ \"%s\" scores %d: at %ld %04d-%02d-%02d %02d:%02d:%02d %s, system had no data\n",
00255                     tzname, i, (long) pgtt,
00256                     pgtm->tm_year + 1900, pgtm->tm_mon + 1, pgtm->tm_mday,
00257                     pgtm->tm_hour, pgtm->tm_min, pgtm->tm_sec,
00258                     pgtm->tm_isdst ? "dst" : "std");
00259 #endif
00260             return i;
00261         }
00262         if (!compare_tm(systm, pgtm))
00263         {
00264 #ifdef DEBUG_IDENTIFY_TIMEZONE
00265             fprintf(stderr, "TZ \"%s\" scores %d: at %ld %04d-%02d-%02d %02d:%02d:%02d %s versus %04d-%02d-%02d %02d:%02d:%02d %s\n",
00266                     tzname, i, (long) pgtt,
00267                     pgtm->tm_year + 1900, pgtm->tm_mon + 1, pgtm->tm_mday,
00268                     pgtm->tm_hour, pgtm->tm_min, pgtm->tm_sec,
00269                     pgtm->tm_isdst ? "dst" : "std",
00270                     systm->tm_year + 1900, systm->tm_mon + 1, systm->tm_mday,
00271                     systm->tm_hour, systm->tm_min, systm->tm_sec,
00272                     systm->tm_isdst ? "dst" : "std");
00273 #endif
00274             return i;
00275         }
00276         if (systm->tm_isdst >= 0)
00277         {
00278             /* Check match of zone names, too */
00279             if (pgtm->tm_zone == NULL)
00280                 return -1;      /* probably shouldn't happen */
00281             memset(cbuf, 0, sizeof(cbuf));
00282             strftime(cbuf, sizeof(cbuf) - 1, "%Z", systm);      /* zone abbr */
00283             if (strcmp(cbuf, pgtm->tm_zone) != 0)
00284             {
00285 #ifdef DEBUG_IDENTIFY_TIMEZONE
00286                 fprintf(stderr, "TZ \"%s\" scores %d: at %ld \"%s\" versus \"%s\"\n",
00287                         tzname, i, (long) pgtt,
00288                         pgtm->tm_zone, cbuf);
00289 #endif
00290                 return i;
00291             }
00292         }
00293     }
00294 
00295 #ifdef DEBUG_IDENTIFY_TIMEZONE
00296     fprintf(stderr, "TZ \"%s\" gets max score %d\n", tzname, i);
00297 #endif
00298 
00299     return i;
00300 }
00301 
00302 
00303 /*
00304  * Try to identify a timezone name (in our terminology) that best matches the
00305  * observed behavior of the system timezone library.  We cannot assume that
00306  * the system TZ environment setting (if indeed there is one) matches our
00307  * terminology, so we ignore it and just look at what localtime() returns.
00308  */
00309 static const char *
00310 identify_system_timezone(void)
00311 {
00312     static char resultbuf[TZ_STRLEN_MAX + 1];
00313     time_t      tnow;
00314     time_t      t;
00315     struct tztry tt;
00316     struct tm  *tm;
00317     int         thisyear;
00318     int         bestscore;
00319     char        tmptzdir[MAXPGPATH];
00320     int         std_ofs;
00321     char        std_zone_name[TZ_STRLEN_MAX + 1],
00322                 dst_zone_name[TZ_STRLEN_MAX + 1];
00323     char        cbuf[TZ_STRLEN_MAX + 1];
00324 
00325     /* Initialize OS timezone library */
00326     tzset();
00327 
00328     /*
00329      * Set up the list of dates to be probed to see how well our timezone
00330      * matches the system zone.  We first probe January and July of the
00331      * current year; this serves to quickly eliminate the vast majority of the
00332      * TZ database entries.  If those dates match, we probe every week for 100
00333      * years backwards from the current July.  (Weekly resolution is good
00334      * enough to identify DST transition rules, since everybody switches on
00335      * Sundays.)  This is sufficient to cover most of the Unix time_t range,
00336      * and we don't want to look further than that since many systems won't
00337      * have sane TZ behavior further back anyway.  The further back the zone
00338      * matches, the better we score it.  This may seem like a rather random
00339      * way of doing things, but experience has shown that system-supplied
00340      * timezone definitions are likely to have DST behavior that is right for
00341      * the recent past and not so accurate further back. Scoring in this way
00342      * allows us to recognize zones that have some commonality with the Olson
00343      * database, without insisting on exact match. (Note: we probe Thursdays,
00344      * not Sundays, to avoid triggering DST-transition bugs in localtime
00345      * itself.)
00346      */
00347     tnow = time(NULL);
00348     tm = localtime(&tnow);
00349     if (!tm)
00350         return NULL;            /* give up if localtime is broken... */
00351     thisyear = tm->tm_year + 1900;
00352 
00353     t = build_time_t(thisyear, 1, 15);
00354 
00355     /*
00356      * Round back to GMT midnight Thursday.  This depends on the knowledge
00357      * that the time_t origin is Thu Jan 01 1970.  (With a different origin
00358      * we'd be probing some other day of the week, but it wouldn't matter
00359      * anyway unless localtime() had DST-transition bugs.)
00360      */
00361     t -= (t % T_WEEK);
00362 
00363     tt.n_test_times = 0;
00364     tt.test_times[tt.n_test_times++] = t;
00365 
00366     t = build_time_t(thisyear, 7, 15);
00367     t -= (t % T_WEEK);
00368 
00369     tt.test_times[tt.n_test_times++] = t;
00370 
00371     while (tt.n_test_times < MAX_TEST_TIMES)
00372     {
00373         t -= T_WEEK;
00374         tt.test_times[tt.n_test_times++] = t;
00375     }
00376 
00377     /* Search for the best-matching timezone file */
00378     strcpy(tmptzdir, pg_TZDIR());
00379     bestscore = -1;
00380     resultbuf[0] = '\0';
00381     scan_available_timezones(tmptzdir, tmptzdir + strlen(tmptzdir) + 1,
00382                              &tt,
00383                              &bestscore, resultbuf);
00384     if (bestscore > 0)
00385     {
00386         /* Ignore Olson's rather silly "Factory" zone; use GMT instead */
00387         if (strcmp(resultbuf, "Factory") == 0)
00388             return NULL;
00389         return resultbuf;
00390     }
00391 
00392     /*
00393      * Couldn't find a match in the database, so next we try constructed zone
00394      * names (like "PST8PDT").
00395      *
00396      * First we need to determine the names of the local standard and daylight
00397      * zones.  The idea here is to scan forward from today until we have seen
00398      * both zones, if both are in use.
00399      */
00400     memset(std_zone_name, 0, sizeof(std_zone_name));
00401     memset(dst_zone_name, 0, sizeof(dst_zone_name));
00402     std_ofs = 0;
00403 
00404     tnow = time(NULL);
00405 
00406     /*
00407      * Round back to a GMT midnight so results don't depend on local time of
00408      * day
00409      */
00410     tnow -= (tnow % T_DAY);
00411 
00412     /*
00413      * We have to look a little further ahead than one year, in case today is
00414      * just past a DST boundary that falls earlier in the year than the next
00415      * similar boundary.  Arbitrarily scan up to 14 months.
00416      */
00417     for (t = tnow; t <= tnow + T_MONTH * 14; t += T_MONTH)
00418     {
00419         tm = localtime(&t);
00420         if (!tm)
00421             continue;
00422         if (tm->tm_isdst < 0)
00423             continue;
00424         if (tm->tm_isdst == 0 && std_zone_name[0] == '\0')
00425         {
00426             /* found STD zone */
00427             memset(cbuf, 0, sizeof(cbuf));
00428             strftime(cbuf, sizeof(cbuf) - 1, "%Z", tm); /* zone abbr */
00429             strcpy(std_zone_name, cbuf);
00430             std_ofs = get_timezone_offset(tm);
00431         }
00432         if (tm->tm_isdst > 0 && dst_zone_name[0] == '\0')
00433         {
00434             /* found DST zone */
00435             memset(cbuf, 0, sizeof(cbuf));
00436             strftime(cbuf, sizeof(cbuf) - 1, "%Z", tm); /* zone abbr */
00437             strcpy(dst_zone_name, cbuf);
00438         }
00439         /* Done if found both */
00440         if (std_zone_name[0] && dst_zone_name[0])
00441             break;
00442     }
00443 
00444     /* We should have found a STD zone name by now... */
00445     if (std_zone_name[0] == '\0')
00446     {
00447 #ifdef DEBUG_IDENTIFY_TIMEZONE
00448         fprintf(stderr, "could not determine system time zone\n");
00449 #endif
00450         return NULL;            /* go to GMT */
00451     }
00452 
00453     /* If we found DST then try STD<ofs>DST */
00454     if (dst_zone_name[0] != '\0')
00455     {
00456         snprintf(resultbuf, sizeof(resultbuf), "%s%d%s",
00457                  std_zone_name, -std_ofs / 3600, dst_zone_name);
00458         if (score_timezone(resultbuf, &tt) > 0)
00459             return resultbuf;
00460     }
00461 
00462     /* Try just the STD timezone (works for GMT at least) */
00463     strcpy(resultbuf, std_zone_name);
00464     if (score_timezone(resultbuf, &tt) > 0)
00465         return resultbuf;
00466 
00467     /* Try STD<ofs> */
00468     snprintf(resultbuf, sizeof(resultbuf), "%s%d",
00469              std_zone_name, -std_ofs / 3600);
00470     if (score_timezone(resultbuf, &tt) > 0)
00471         return resultbuf;
00472 
00473     /*
00474      * Did not find the timezone.  Fallback to use a GMT zone.  Note that the
00475      * Olson timezone database names the GMT-offset zones in POSIX style: plus
00476      * is west of Greenwich.  It's unfortunate that this is opposite of SQL
00477      * conventions.  Should we therefore change the names? Probably not...
00478      */
00479     snprintf(resultbuf, sizeof(resultbuf), "Etc/GMT%s%d",
00480              (-std_ofs > 0) ? "+" : "", -std_ofs / 3600);
00481 
00482 #ifdef DEBUG_IDENTIFY_TIMEZONE
00483     fprintf(stderr, "could not recognize system time zone, using \"%s\"\n",
00484             resultbuf);
00485 #endif
00486     return resultbuf;
00487 }
00488 
00489 /*
00490  * Recursively scan the timezone database looking for the best match to
00491  * the system timezone behavior.
00492  *
00493  * tzdir points to a buffer of size MAXPGPATH.  On entry, it holds the
00494  * pathname of a directory containing TZ files.  We internally modify it
00495  * to hold pathnames of sub-directories and files, but must restore it
00496  * to its original contents before exit.
00497  *
00498  * tzdirsub points to the part of tzdir that represents the subfile name
00499  * (ie, tzdir + the original directory name length, plus one for the
00500  * first added '/').
00501  *
00502  * tt tells about the system timezone behavior we need to match.
00503  *
00504  * *bestscore and *bestzonename on entry hold the best score found so far
00505  * and the name of the best zone.  We overwrite them if we find a better
00506  * score.  bestzonename must be a buffer of length TZ_STRLEN_MAX + 1.
00507  */
00508 static void
00509 scan_available_timezones(char *tzdir, char *tzdirsub, struct tztry * tt,
00510                          int *bestscore, char *bestzonename)
00511 {
00512     int         tzdir_orig_len = strlen(tzdir);
00513     char      **names;
00514     char      **namep;
00515 
00516     names = pgfnames(tzdir);
00517     if (!names)
00518         return;
00519 
00520     for (namep = names; *namep; namep++)
00521     {
00522         char       *name = *namep;
00523         struct stat statbuf;
00524 
00525         /* Ignore . and .., plus any other "hidden" files */
00526         if (name[0] == '.')
00527             continue;
00528 
00529         snprintf(tzdir + tzdir_orig_len, MAXPGPATH - tzdir_orig_len,
00530                  "/%s", name);
00531 
00532         if (stat(tzdir, &statbuf) != 0)
00533         {
00534 #ifdef DEBUG_IDENTIFY_TIMEZONE
00535             fprintf(stderr, "could not stat \"%s\": %s\n",
00536                     tzdir, strerror(errno));
00537 #endif
00538             tzdir[tzdir_orig_len] = '\0';
00539             continue;
00540         }
00541 
00542         if (S_ISDIR(statbuf.st_mode))
00543         {
00544             /* Recurse into subdirectory */
00545             scan_available_timezones(tzdir, tzdirsub, tt,
00546                                      bestscore, bestzonename);
00547         }
00548         else
00549         {
00550             /* Load and test this file */
00551             int         score = score_timezone(tzdirsub, tt);
00552 
00553             if (score > *bestscore)
00554             {
00555                 *bestscore = score;
00556                 strlcpy(bestzonename, tzdirsub, TZ_STRLEN_MAX + 1);
00557             }
00558             else if (score == *bestscore)
00559             {
00560                 /* Consider how to break a tie */
00561                 if (strlen(tzdirsub) < strlen(bestzonename) ||
00562                     (strlen(tzdirsub) == strlen(bestzonename) &&
00563                      strcmp(tzdirsub, bestzonename) < 0))
00564                     strlcpy(bestzonename, tzdirsub, TZ_STRLEN_MAX + 1);
00565             }
00566         }
00567 
00568         /* Restore tzdir */
00569         tzdir[tzdir_orig_len] = '\0';
00570     }
00571 
00572     pgfnames_cleanup(names);
00573 }
00574 #else                           /* WIN32 */
00575 
00576 static const struct
00577 {
00578     const char *stdname;        /* Windows name of standard timezone */
00579     const char *dstname;        /* Windows name of daylight timezone */
00580     const char *pgtzname;       /* Name of pgsql timezone to map to */
00581 }   win32_tzmap[] =
00582 
00583 {
00584     /*
00585      * This list was built from the contents of the registry at
00586      * HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time
00587      * Zones on Windows 2003 R2.
00588      *
00589      * The zones have been matched to Olson timezones by looking at the cities
00590      * listed in the win32 display name (in the comment here) in most cases.
00591      */
00592     {
00593         "Afghanistan Standard Time", "Afghanistan Daylight Time",
00594         "Asia/Kabul"
00595     },                          /* (GMT+04:30) Kabul */
00596     {
00597         "Alaskan Standard Time", "Alaskan Daylight Time",
00598         "US/Alaska"
00599     },                          /* (GMT-09:00) Alaska */
00600     {
00601         "Arab Standard Time", "Arab Daylight Time",
00602         "Asia/Kuwait"
00603     },                          /* (GMT+03:00) Kuwait, Riyadh */
00604     {
00605         "Arabian Standard Time", "Arabian Daylight Time",
00606         "Asia/Muscat"
00607     },                          /* (GMT+04:00) Abu Dhabi, Muscat */
00608     {
00609         "Arabic Standard Time", "Arabic Daylight Time",
00610         "Asia/Baghdad"
00611     },                          /* (GMT+03:00) Baghdad */
00612     {
00613         "Argentina Standard Time", "Argentina Daylight Time",
00614         "America/Buenos_Aires"
00615     },                          /* (GMT-03:00) Buenos Aires */
00616     {
00617         "Armenian Standard Time", "Armenian Daylight Time",
00618         "Asia/Yerevan"
00619     },                          /* (GMT+04:00) Yerevan */
00620     {
00621         "Atlantic Standard Time", "Atlantic Daylight Time",
00622         "Canada/Atlantic"
00623     },                          /* (GMT-04:00) Atlantic Time (Canada) */
00624     {
00625         "AUS Central Standard Time", "AUS Central Daylight Time",
00626         "Australia/Darwin"
00627     },                          /* (GMT+09:30) Darwin */
00628     {
00629         "AUS Eastern Standard Time", "AUS Eastern Daylight Time",
00630         "Australia/Canberra"
00631     },                          /* (GMT+10:00) Canberra, Melbourne, Sydney */
00632     {
00633         "Azerbaijan Standard Time", "Azerbaijan Daylight Time",
00634         "Asia/Baku"
00635     },                          /* (GMT+04:00) Baku */
00636     {
00637         "Azores Standard Time", "Azores Daylight Time",
00638         "Atlantic/Azores"
00639     },                          /* (GMT-01:00) Azores */
00640     {
00641         "Bangladesh Standard Time", "Bangladesh Daylight Time",
00642         "Asia/Dhaka"
00643     },                          /* (GMT+06:00) Dhaka */
00644     {
00645         "Canada Central Standard Time", "Canada Central Daylight Time",
00646         "Canada/Saskatchewan"
00647     },                          /* (GMT-06:00) Saskatchewan */
00648     {
00649         "Cape Verde Standard Time", "Cape Verde Daylight Time",
00650         "Atlantic/Cape_Verde"
00651     },                          /* (GMT-01:00) Cape Verde Is. */
00652     {
00653         "Caucasus Standard Time", "Caucasus Daylight Time",
00654         "Asia/Baku"
00655     },                          /* (GMT+04:00) Baku, Tbilisi, Yerevan */
00656     {
00657         "Cen. Australia Standard Time", "Cen. Australia Daylight Time",
00658         "Australia/Adelaide"
00659     },                          /* (GMT+09:30) Adelaide */
00660     /* Central America (other than Mexico) generally does not observe DST */
00661     {
00662         "Central America Standard Time", "Central America Daylight Time",
00663         "CST6"
00664     },                          /* (GMT-06:00) Central America */
00665     {
00666         "Central Asia Standard Time", "Central Asia Daylight Time",
00667         "Asia/Dhaka"
00668     },                          /* (GMT+06:00) Astana, Dhaka */
00669     {
00670         "Central Brazilian Standard Time", "Central Brazilian Daylight Time",
00671         "America/Cuiaba"
00672     },                          /* (GMT-04:00) Cuiaba */
00673     {
00674         "Central Europe Standard Time", "Central Europe Daylight Time",
00675         "Europe/Belgrade"
00676     },                          /* (GMT+01:00) Belgrade, Bratislava, Budapest,
00677                                  * Ljubljana, Prague */
00678     {
00679         "Central European Standard Time", "Central European Daylight Time",
00680         "Europe/Sarajevo"
00681     },                          /* (GMT+01:00) Sarajevo, Skopje, Warsaw,
00682                                  * Zagreb */
00683     {
00684         "Central Pacific Standard Time", "Central Pacific Daylight Time",
00685         "Pacific/Noumea"
00686     },                          /* (GMT+11:00) Magadan, Solomon Is., New
00687                                  * Caledonia */
00688     {
00689         "Central Standard Time", "Central Daylight Time",
00690         "US/Central"
00691     },                          /* (GMT-06:00) Central Time (US & Canada) */
00692     {
00693         "Central Standard Time (Mexico)", "Central Daylight Time (Mexico)",
00694         "America/Mexico_City"
00695     },                          /* (GMT-06:00) Guadalajara, Mexico City,
00696                                  * Monterrey - New */
00697     {
00698         "China Standard Time", "China Daylight Time",
00699         "Asia/Hong_Kong"
00700     },                          /* (GMT+08:00) Beijing, Chongqing, Hong Kong,
00701                                  * Urumqi */
00702     {
00703         "Dateline Standard Time", "Dateline Daylight Time",
00704         "Etc/GMT+12"
00705     },                          /* (GMT-12:00) International Date Line West */
00706     {
00707         "E. Africa Standard Time", "E. Africa Daylight Time",
00708         "Africa/Nairobi"
00709     },                          /* (GMT+03:00) Nairobi */
00710     {
00711         "E. Australia Standard Time", "E. Australia Daylight Time",
00712         "Australia/Brisbane"
00713     },                          /* (GMT+10:00) Brisbane */
00714     {
00715         "E. Europe Standard Time", "E. Europe Daylight Time",
00716         "Europe/Bucharest"
00717     },                          /* (GMT+02:00) Bucharest */
00718     {
00719         "E. South America Standard Time", "E. South America Daylight Time",
00720         "America/Araguaina"
00721     },                          /* (GMT-03:00) Brasilia */
00722     {
00723         "Eastern Standard Time", "Eastern Daylight Time",
00724         "US/Eastern"
00725     },                          /* (GMT-05:00) Eastern Time (US & Canada) */
00726     {
00727         "Egypt Standard Time", "Egypt Daylight Time",
00728         "Africa/Cairo"
00729     },                          /* (GMT+02:00) Cairo */
00730     {
00731         "Ekaterinburg Standard Time", "Ekaterinburg Daylight Time",
00732         "Asia/Yekaterinburg"
00733     },                          /* (GMT+05:00) Ekaterinburg */
00734     {
00735         "Fiji Standard Time", "Fiji Daylight Time",
00736         "Pacific/Fiji"
00737     },                          /* (GMT+12:00) Fiji, Kamchatka, Marshall Is. */
00738     {
00739         "FLE Standard Time", "FLE Daylight Time",
00740         "Europe/Helsinki"
00741     },                          /* (GMT+02:00) Helsinki, Kyiv, Riga, Sofia,
00742                                  * Tallinn, Vilnius */
00743     {
00744         "Georgian Standard Time", "Georgian Daylight Time",
00745         "Asia/Tbilisi"
00746     },                          /* (GMT+03:00) Tbilisi */
00747     {
00748         "GMT Standard Time", "GMT Daylight Time",
00749         "Europe/London"
00750     },                          /* (GMT) Greenwich Mean Time : Dublin,
00751                                  * Edinburgh, Lisbon, London */
00752     {
00753         "Greenland Standard Time", "Greenland Daylight Time",
00754         "America/Godthab"
00755     },                          /* (GMT-03:00) Greenland */
00756     {
00757         "Greenwich Standard Time", "Greenwich Daylight Time",
00758         "Africa/Casablanca"
00759     },                          /* (GMT) Casablanca, Monrovia */
00760     {
00761         "GTB Standard Time", "GTB Daylight Time",
00762         "Europe/Athens"
00763     },                          /* (GMT+02:00) Athens, Istanbul, Minsk */
00764     {
00765         "Hawaiian Standard Time", "Hawaiian Daylight Time",
00766         "US/Hawaii"
00767     },                          /* (GMT-10:00) Hawaii */
00768     {
00769         "India Standard Time", "India Daylight Time",
00770         "Asia/Calcutta"
00771     },                          /* (GMT+05:30) Chennai, Kolkata, Mumbai, New
00772                                  * Delhi */
00773     {
00774         "Iran Standard Time", "Iran Daylight Time",
00775         "Asia/Tehran"
00776     },                          /* (GMT+03:30) Tehran */
00777     {
00778         "Jerusalem Standard Time", "Jerusalem Daylight Time",
00779         "Asia/Jerusalem"
00780     },                          /* (GMT+02:00) Jerusalem */
00781     {
00782         "Jordan Standard Time", "Jordan Daylight Time",
00783         "Asia/Amman"
00784     },                          /* (GMT+02:00) Amman */
00785     {
00786         "Kamchatka Standard Time", "Kamchatka Daylight Time",
00787         "Asia/Kamchatka"
00788     },                          /* (GMT+12:00) Petropavlovsk-Kamchatsky */
00789     {
00790         "Korea Standard Time", "Korea Daylight Time",
00791         "Asia/Seoul"
00792     },                          /* (GMT+09:00) Seoul */
00793     {
00794         "Mauritius Standard Time", "Mauritius Daylight Time",
00795         "Indian/Mauritius"
00796     },                          /* (GMT+04:00) Port Louis */
00797     {
00798         "Mexico Standard Time", "Mexico Daylight Time",
00799         "America/Mexico_City"
00800     },                          /* (GMT-06:00) Guadalajara, Mexico City,
00801                                  * Monterrey */
00802     {
00803         "Mexico Standard Time 2", "Mexico Daylight Time 2",
00804         "America/Chihuahua"
00805     },                          /* (GMT-07:00) Chihuahua, La Paz, Mazatlan */
00806     {
00807         "Mid-Atlantic Standard Time", "Mid-Atlantic Daylight Time",
00808         "Atlantic/South_Georgia"
00809     },                          /* (GMT-02:00) Mid-Atlantic */
00810     {
00811         "Middle East Standard Time", "Middle East Daylight Time",
00812         "Asia/Beirut"
00813     },                          /* (GMT+02:00) Beirut */
00814     {
00815         "Montevideo Standard Time", "Montevideo Daylight Time",
00816         "America/Montevideo"
00817     },                          /* (GMT-03:00) Montevideo */
00818     {
00819         "Morocco Standard Time", "Morocco Daylight Time",
00820         "Africa/Casablanca"
00821     },                          /* (GMT) Casablanca */
00822     {
00823         "Mountain Standard Time", "Mountain Daylight Time",
00824         "US/Mountain"
00825     },                          /* (GMT-07:00) Mountain Time (US & Canada) */
00826     {
00827         "Mountain Standard Time (Mexico)", "Mountain Daylight Time (Mexico)",
00828         "America/Chihuahua"
00829     },                          /* (GMT-07:00) Chihuahua, La Paz, Mazatlan -
00830                                  * New */
00831     {
00832         "Myanmar Standard Time", "Myanmar Daylight Time",
00833         "Asia/Rangoon"
00834     },                          /* (GMT+06:30) Rangoon */
00835     {
00836         "N. Central Asia Standard Time", "N. Central Asia Daylight Time",
00837         "Asia/Novosibirsk"
00838     },                          /* (GMT+06:00) Novosibirsk */
00839     {
00840         "Namibia Standard Time", "Namibia Daylight Time",
00841         "Africa/Windhoek"
00842     },                          /* (GMT+02:00) Windhoek */
00843     {
00844         "Nepal Standard Time", "Nepal Daylight Time",
00845         "Asia/Katmandu"
00846     },                          /* (GMT+05:45) Kathmandu */
00847     {
00848         "New Zealand Standard Time", "New Zealand Daylight Time",
00849         "Pacific/Auckland"
00850     },                          /* (GMT+12:00) Auckland, Wellington */
00851     {
00852         "Newfoundland Standard Time", "Newfoundland Daylight Time",
00853         "Canada/Newfoundland"
00854     },                          /* (GMT-03:30) Newfoundland */
00855     {
00856         "North Asia East Standard Time", "North Asia East Daylight Time",
00857         "Asia/Irkutsk"
00858     },                          /* (GMT+08:00) Irkutsk, Ulaan Bataar */
00859     {
00860         "North Asia Standard Time", "North Asia Daylight Time",
00861         "Asia/Krasnoyarsk"
00862     },                          /* (GMT+07:00) Krasnoyarsk */
00863     {
00864         "Pacific SA Standard Time", "Pacific SA Daylight Time",
00865         "America/Santiago"
00866     },                          /* (GMT-04:00) Santiago */
00867     {
00868         "Pacific Standard Time", "Pacific Daylight Time",
00869         "US/Pacific"
00870     },                          /* (GMT-08:00) Pacific Time (US & Canada);
00871                                  * Tijuana */
00872     {
00873         "Pacific Standard Time (Mexico)", "Pacific Daylight Time (Mexico)",
00874         "America/Tijuana"
00875     },                          /* (GMT-08:00) Tijuana, Baja California */
00876     {
00877         "Pakistan Standard Time", "Pakistan Daylight Time",
00878         "Asia/Karachi"
00879     },                          /* (GMT+05:00) Islamabad, Karachi */
00880     {
00881         "Paraguay Standard Time", "Paraguay Daylight Time",
00882         "America/Asuncion"
00883     },                          /* (GMT-04:00) Asuncion */
00884     {
00885         "Romance Standard Time", "Romance Daylight Time",
00886         "Europe/Brussels"
00887     },                          /* (GMT+01:00) Brussels, Copenhagen, Madrid,
00888                                  * Paris */
00889     {
00890         "Russian Standard Time", "Russian Daylight Time",
00891         "Europe/Moscow"
00892     },                          /* (GMT+03:00) Moscow, St. Petersburg,
00893                                  * Volgograd */
00894     {
00895         "SA Eastern Standard Time", "SA Eastern Daylight Time",
00896         "America/Buenos_Aires"
00897     },                          /* (GMT-03:00) Buenos Aires, Georgetown */
00898     {
00899         "SA Pacific Standard Time", "SA Pacific Daylight Time",
00900         "America/Bogota"
00901     },                          /* (GMT-05:00) Bogota, Lima, Quito */
00902     {
00903         "SA Western Standard Time", "SA Western Daylight Time",
00904         "America/Caracas"
00905     },                          /* (GMT-04:00) Caracas, La Paz */
00906     {
00907         "Samoa Standard Time", "Samoa Daylight Time",
00908         "Pacific/Midway"
00909     },                          /* (GMT-11:00) Midway Island, Samoa */
00910     {
00911         "SE Asia Standard Time", "SE Asia Daylight Time",
00912         "Asia/Bangkok"
00913     },                          /* (GMT+07:00) Bangkok, Hanoi, Jakarta */
00914     {
00915         "Malay Peninsula Standard Time", "Malay Peninsula Daylight Time",
00916         "Asia/Kuala_Lumpur"
00917     },                          /* (GMT+08:00) Kuala Lumpur, Singapore */
00918     {
00919         "South Africa Standard Time", "South Africa Daylight Time",
00920         "Africa/Harare"
00921     },                          /* (GMT+02:00) Harare, Pretoria */
00922     {
00923         "Sri Lanka Standard Time", "Sri Lanka Daylight Time",
00924         "Asia/Colombo"
00925     },                          /* (GMT+06:00) Sri Jayawardenepura */
00926     {
00927         "Taipei Standard Time", "Taipei Daylight Time",
00928         "Asia/Taipei"
00929     },                          /* (GMT+08:00) Taipei */
00930     {
00931         "Tasmania Standard Time", "Tasmania Daylight Time",
00932         "Australia/Hobart"
00933     },                          /* (GMT+10:00) Hobart */
00934     {
00935         "Tokyo Standard Time", "Tokyo Daylight Time",
00936         "Asia/Tokyo"
00937     },                          /* (GMT+09:00) Osaka, Sapporo, Tokyo */
00938     {
00939         "Tonga Standard Time", "Tonga Daylight Time",
00940         "Pacific/Tongatapu"
00941     },                          /* (GMT+13:00) Nuku'alofa */
00942     {
00943         "Ulaanbaatar Standard Time", "Ulaanbaatar Daylight Time",
00944         "Asia/Ulaanbaatar",
00945     },                          /* (GMT+08:00) Ulaanbaatar */
00946     {
00947         "US Eastern Standard Time", "US Eastern Daylight Time",
00948         "US/Eastern"
00949     },                          /* (GMT-05:00) Indiana (East) */
00950     {
00951         "US Mountain Standard Time", "US Mountain Daylight Time",
00952         "US/Arizona"
00953     },                          /* (GMT-07:00) Arizona */
00954     {
00955         "Coordinated Universal Time", "Coordinated Universal Time",
00956         "UTC"
00957     },                          /* (GMT) Coordinated Universal Time */
00958     {
00959         "UTC+12", "UTC+12",
00960         "Etc/GMT+12"
00961     },                          /* (GMT+12:00) Coordinated Universal Time+12 */
00962     {
00963         "UTC-02", "UTC-02",
00964         "Etc/GMT-02"
00965     },                          /* (GMT-02:00) Coordinated Universal Time-02 */
00966     {
00967         "UTC-11", "UTC-11",
00968         "Etc/GMT-11"
00969     },                          /* (GMT-11:00) Coordinated Universal Time-11 */
00970     {
00971         "Venezuela Standard Time", "Venezuela Daylight Time",
00972         "America/Caracas",
00973     },                          /* (GMT-04:30) Caracas */
00974     {
00975         "Vladivostok Standard Time", "Vladivostok Daylight Time",
00976         "Asia/Vladivostok"
00977     },                          /* (GMT+10:00) Vladivostok */
00978     {
00979         "W. Australia Standard Time", "W. Australia Daylight Time",
00980         "Australia/Perth"
00981     },                          /* (GMT+08:00) Perth */
00982 #ifdef NOT_USED
00983     /* Could not find a match for this one (just a guess). Excluded for now. */
00984     {
00985         "W. Central Africa Standard Time", "W. Central Africa Daylight Time",
00986         "WAT"
00987     },                          /* (GMT+01:00) West Central Africa */
00988 #endif
00989     {
00990         "W. Europe Standard Time", "W. Europe Daylight Time",
00991         "CET"
00992     },                          /* (GMT+01:00) Amsterdam, Berlin, Bern, Rome,
00993                                  * Stockholm, Vienna */
00994     {
00995         "West Asia Standard Time", "West Asia Daylight Time",
00996         "Asia/Karachi"
00997     },                          /* (GMT+05:00) Islamabad, Karachi, Tashkent */
00998     {
00999         "West Pacific Standard Time", "West Pacific Daylight Time",
01000         "Pacific/Guam"
01001     },                          /* (GMT+10:00) Guam, Port Moresby */
01002     {
01003         "Yakutsk Standard Time", "Yakutsk Daylight Time",
01004         "Asia/Yakutsk"
01005     },                          /* (GMT+09:00) Yakutsk */
01006     {
01007         NULL, NULL, NULL
01008     }
01009 };
01010 
01011 static const char *
01012 identify_system_timezone(void)
01013 {
01014     int         i;
01015     char        tzname[128];
01016     char        localtzname[256];
01017     time_t      t = time(NULL);
01018     struct tm  *tm = localtime(&t);
01019     HKEY        rootKey;
01020     int         idx;
01021 
01022     if (!tm)
01023     {
01024 #ifdef DEBUG_IDENTIFY_TIMEZONE
01025         fprintf(stderr, "could not identify system time zone: localtime() failed\n");
01026 #endif
01027         return NULL;            /* go to GMT */
01028     }
01029 
01030     memset(tzname, 0, sizeof(tzname));
01031     strftime(tzname, sizeof(tzname) - 1, "%Z", tm);
01032 
01033     for (i = 0; win32_tzmap[i].stdname != NULL; i++)
01034     {
01035         if (strcmp(tzname, win32_tzmap[i].stdname) == 0 ||
01036             strcmp(tzname, win32_tzmap[i].dstname) == 0)
01037         {
01038 #ifdef DEBUG_IDENTIFY_TIMEZONE
01039             fprintf(stderr, "TZ \"%s\" matches system time zone \"%s\"\n",
01040                     win32_tzmap[i].pgtzname, tzname);
01041 #endif
01042             return win32_tzmap[i].pgtzname;
01043         }
01044     }
01045 
01046     /*
01047      * Localized Windows versions return localized names for the timezone.
01048      * Scan the registry to find the English name, and then try matching
01049      * against our table again.
01050      */
01051     memset(localtzname, 0, sizeof(localtzname));
01052     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
01053                "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones",
01054                      0,
01055                      KEY_READ,
01056                      &rootKey) != ERROR_SUCCESS)
01057     {
01058 #ifdef DEBUG_IDENTIFY_TIMEZONE
01059         fprintf(stderr, "could not open registry key to identify system time zone: error code %lu\n",
01060                 GetLastError());
01061 #endif
01062         return NULL;            /* go to GMT */
01063     }
01064 
01065     for (idx = 0;; idx++)
01066     {
01067         char        keyname[256];
01068         char        zonename[256];
01069         DWORD       namesize;
01070         FILETIME    lastwrite;
01071         HKEY        key;
01072         LONG        r;
01073 
01074         memset(keyname, 0, sizeof(keyname));
01075         namesize = sizeof(keyname);
01076         if ((r = RegEnumKeyEx(rootKey,
01077                               idx,
01078                               keyname,
01079                               &namesize,
01080                               NULL,
01081                               NULL,
01082                               NULL,
01083                               &lastwrite)) != ERROR_SUCCESS)
01084         {
01085             if (r == ERROR_NO_MORE_ITEMS)
01086                 break;
01087 #ifdef DEBUG_IDENTIFY_TIMEZONE
01088             fprintf(stderr, "could not enumerate registry subkeys to identify system time zone: %d\n",
01089                     (int) r);
01090 #endif
01091             break;
01092         }
01093 
01094         if ((r = RegOpenKeyEx(rootKey, keyname, 0, KEY_READ, &key)) != ERROR_SUCCESS)
01095         {
01096 #ifdef DEBUG_IDENTIFY_TIMEZONE
01097             fprintf(stderr, "could not open registry subkey to identify system time zone: %d\n",
01098                     (int) r);
01099 #endif
01100             break;
01101         }
01102 
01103         memset(zonename, 0, sizeof(zonename));
01104         namesize = sizeof(zonename);
01105         if ((r = RegQueryValueEx(key, "Std", NULL, NULL, (unsigned char *) zonename, &namesize)) != ERROR_SUCCESS)
01106         {
01107 #ifdef DEBUG_IDENTIFY_TIMEZONE
01108             fprintf(stderr, "could not query value for key \"std\" to identify system time zone \"%s\": %d\n",
01109                     keyname, (int) r);
01110 #endif
01111             RegCloseKey(key);
01112             continue;           /* Proceed to look at the next timezone */
01113         }
01114         if (strcmp(tzname, zonename) == 0)
01115         {
01116             /* Matched zone */
01117             strcpy(localtzname, keyname);
01118             RegCloseKey(key);
01119             break;
01120         }
01121         memset(zonename, 0, sizeof(zonename));
01122         namesize = sizeof(zonename);
01123         if ((r = RegQueryValueEx(key, "Dlt", NULL, NULL, (unsigned char *) zonename, &namesize)) != ERROR_SUCCESS)
01124         {
01125 #ifdef DEBUG_IDENTIFY_TIMEZONE
01126             fprintf(stderr, "could not query value for key \"dlt\" to identify system time zone \"%s\": %d\n",
01127                     keyname, (int) r);
01128 #endif
01129             RegCloseKey(key);
01130             continue;           /* Proceed to look at the next timezone */
01131         }
01132         if (strcmp(tzname, zonename) == 0)
01133         {
01134             /* Matched DST zone */
01135             strcpy(localtzname, keyname);
01136             RegCloseKey(key);
01137             break;
01138         }
01139 
01140         RegCloseKey(key);
01141     }
01142 
01143     RegCloseKey(rootKey);
01144 
01145     if (localtzname[0])
01146     {
01147         /* Found a localized name, so scan for that one too */
01148         for (i = 0; win32_tzmap[i].stdname != NULL; i++)
01149         {
01150             if (strcmp(localtzname, win32_tzmap[i].stdname) == 0 ||
01151                 strcmp(localtzname, win32_tzmap[i].dstname) == 0)
01152             {
01153 #ifdef DEBUG_IDENTIFY_TIMEZONE
01154                 fprintf(stderr, "TZ \"%s\" matches localized system time zone \"%s\" (\"%s\")\n",
01155                         win32_tzmap[i].pgtzname, tzname, localtzname);
01156 #endif
01157                 return win32_tzmap[i].pgtzname;
01158             }
01159         }
01160     }
01161 
01162 #ifdef DEBUG_IDENTIFY_TIMEZONE
01163     fprintf(stderr, "could not find a match for system time zone \"%s\"\n",
01164             tzname);
01165 #endif
01166     return NULL;                /* go to GMT */
01167 }
01168 #endif   /* WIN32 */
01169 
01170 
01171 /*
01172  * Return true if the given zone name is valid and is an "acceptable" zone.
01173  */
01174 static bool
01175 validate_zone(const char *tzname)
01176 {
01177     pg_tz      *tz;
01178 
01179     if (!tzname || !tzname[0])
01180         return false;
01181 
01182     tz = pg_load_tz(tzname);
01183     if (!tz)
01184         return false;
01185 
01186     if (!pg_tz_acceptable(tz))
01187         return false;
01188 
01189     return true;
01190 }
01191 
01192 /*
01193  * Identify a suitable default timezone setting based on the environment.
01194  *
01195  * The installation share_path must be passed in, as that is the default
01196  * location for the timezone database directory.
01197  *
01198  * We first look to the TZ environment variable.  If not found or not
01199  * recognized by our own code, we see if we can identify the timezone
01200  * from the behavior of the system timezone library.  When all else fails,
01201  * return NULL, indicating that we should default to GMT.
01202  */
01203 const char *
01204 select_default_timezone(const char *share_path)
01205 {
01206     const char *tzname;
01207 
01208     /* Initialize timezone directory path, if needed */
01209 #ifndef SYSTEMTZDIR
01210     snprintf(tzdirpath, sizeof(tzdirpath), "%s/timezone", share_path);
01211 #endif
01212 
01213     /* Check TZ environment variable */
01214     tzname = getenv("TZ");
01215     if (validate_zone(tzname))
01216         return tzname;
01217 
01218     /* Nope, so try to identify the system timezone */
01219     tzname = identify_system_timezone();
01220     if (validate_zone(tzname))
01221         return tzname;
01222 
01223     return NULL;
01224 }