PostgreSQL Source Code  git master
findtimezone.c File Reference
#include "postgres_fe.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "pgtz.h"
Include dependency graph for findtimezone.c:

Go to the source code of this file.

Data Structures

struct  tztry
 

Macros

#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) /* 100 years */
 

Functions

const char * select_default_timezone (const char *share_path)
 
static const char * pg_TZDIR (void)
 
int pg_open_tzfile (const char *name, char *canonname)
 
static pg_tzpg_load_tz (const char *name)
 
static bool check_system_link_file (const char *linkname, struct tztry *tt, char *bestzonename)
 
static void scan_available_timezones (char *tzdir, char *tzdirsub, struct tztry *tt, int *bestscore, char *bestzonename)
 
static int get_timezone_offset (struct tm *tm)
 
static time_t build_time_t (int year, int month, int day)
 
static bool compare_tm (struct tm *s, struct pg_tm *p)
 
static int score_timezone (const char *tzname, struct tztry *tt)
 
static bool perfect_timezone_match (const char *tzname, struct tztry *tt)
 
static const char * identify_system_timezone (void)
 
static int zone_name_pref (const char *zonename)
 
static bool validate_zone (const char *tzname)
 

Variables

static char tzdirpath [MAXPGPATH]
 

Macro Definition Documentation

◆ MAX_TEST_TIMES

#define MAX_TEST_TIMES   (52*100) /* 100 years */

Definition at line 156 of file findtimezone.c.

Referenced by identify_system_timezone().

◆ T_DAY

#define T_DAY   ((time_t) (60*60*24))

Definition at line 152 of file findtimezone.c.

Referenced by identify_system_timezone().

◆ T_MONTH

#define T_MONTH   ((time_t) (60*60*24*31))

Definition at line 154 of file findtimezone.c.

Referenced by identify_system_timezone().

◆ T_WEEK

#define T_WEEK   ((time_t) (60*60*24*7))

Definition at line 153 of file findtimezone.c.

Referenced by identify_system_timezone().

Function Documentation

◆ build_time_t()

static time_t build_time_t ( int  year,
int  month,
int  day 
)
static

Definition at line 190 of file findtimezone.c.

References tm.

Referenced by identify_system_timezone().

191 {
192  struct tm tm;
193 
194  memset(&tm, 0, sizeof(tm));
195  tm.tm_mday = day;
196  tm.tm_mon = month - 1;
197  tm.tm_year = year - 1900;
198 
199  return mktime(&tm);
200 }
static struct pg_tm tm
Definition: localtime.c:108
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int tm_year
Definition: pgtime.h:32

◆ check_system_link_file()

static bool check_system_link_file ( const char *  linkname,
struct tztry tt,
char *  bestzonename 
)
static

Definition at line 543 of file findtimezone.c.

References fprintf, MAXPGPATH, perfect_timezone_match(), readlink, and TZ_STRLEN_MAX.

Referenced by identify_system_timezone().

545 {
546 #ifdef HAVE_READLINK
547  char link_target[MAXPGPATH];
548  int len;
549  const char *cur_name;
550 
551  /*
552  * Try to read the symlink. If not there, not a symlink, etc etc, just
553  * quietly fail; the precise reason needn't concern us.
554  */
555  len = readlink(linkname, link_target, sizeof(link_target));
556  if (len < 0 || len >= sizeof(link_target))
557  return false;
558  link_target[len] = '\0';
559 
560 #ifdef DEBUG_IDENTIFY_TIMEZONE
561  fprintf(stderr, "symbolic link \"%s\" contains \"%s\"\n",
562  linkname, link_target);
563 #endif
564 
565  /*
566  * The symlink is probably of the form "/path/to/zones/zone/name", or
567  * possibly it is a relative path. Nobody puts their zone DB directly in
568  * the root directory, so we can definitely skip the first component; but
569  * after that it's trial-and-error to identify which path component begins
570  * the zone name.
571  */
572  cur_name = link_target;
573  while (*cur_name)
574  {
575  /* Advance to next segment of path */
576  cur_name = strchr(cur_name + 1, '/');
577  if (cur_name == NULL)
578  break;
579  /* If there are consecutive slashes, skip all, as the kernel would */
580  do
581  {
582  cur_name++;
583  } while (*cur_name == '/');
584 
585  /*
586  * Test remainder of path to see if it is a matching zone name.
587  * Relative paths might contain ".."; we needn't bother testing if the
588  * first component is that. Also defend against overlength names.
589  */
590  if (*cur_name && *cur_name != '.' &&
591  strlen(cur_name) <= TZ_STRLEN_MAX &&
592  perfect_timezone_match(cur_name, tt))
593  {
594  /* Success! */
595  strcpy(bestzonename, cur_name);
596  return true;
597  }
598  }
599 
600  /* Couldn't extract a matching zone name */
601  return false;
602 #else
603  /* No symlinks? Forget it */
604  return false;
605 #endif
606 }
#define fprintf
Definition: port.h:196
#define TZ_STRLEN_MAX
Definition: pgtime.h:44
#define MAXPGPATH
static bool perfect_timezone_match(const char *tzname, struct tztry *tt)
Definition: findtimezone.c:319
#define readlink(path, buf, size)
Definition: win32_port.h:222

◆ compare_tm()

static bool compare_tm ( struct tm s,
struct pg_tm p 
)
static

Definition at line 206 of file findtimezone.c.

References pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_wday, pg_tm::tm_yday, and pg_tm::tm_year.

Referenced by score_timezone().

207 {
208  if (s->tm_sec != p->tm_sec ||
209  s->tm_min != p->tm_min ||
210  s->tm_hour != p->tm_hour ||
211  s->tm_mday != p->tm_mday ||
212  s->tm_mon != p->tm_mon ||
213  s->tm_year != p->tm_year ||
214  s->tm_wday != p->tm_wday ||
215  s->tm_yday != p->tm_yday ||
216  s->tm_isdst != p->tm_isdst)
217  return false;
218  return true;
219 }
int tm_wday
Definition: pgtime.h:33
int tm_isdst
Definition: pgtime.h:35
int tm_hour
Definition: pgtime.h:29
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int tm_year
Definition: pgtime.h:32
int tm_yday
Definition: pgtime.h:34
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ get_timezone_offset()

static int get_timezone_offset ( struct tm tm)
static

Definition at line 175 of file findtimezone.c.

References TIMEZONE_GLOBAL.

Referenced by identify_system_timezone().

176 {
177 #if defined(HAVE_STRUCT_TM_TM_ZONE)
178  return tm->tm_gmtoff;
179 #elif defined(HAVE_INT_TIMEZONE)
180  return -TIMEZONE_GLOBAL;
181 #else
182 #error No way to determine TZ? Can this happen?
183 #endif
184 }
#define TIMEZONE_GLOBAL
Definition: port.h:226
long int tm_gmtoff
Definition: pgtime.h:36
static struct pg_tm tm
Definition: localtime.c:108

◆ identify_system_timezone()

static const char* identify_system_timezone ( void  )
static

Definition at line 330 of file findtimezone.c.

References build_time_t(), check_system_link_file(), fprintf, get_timezone_offset(), MAX_TEST_TIMES, MAXPGPATH, tztry::n_test_times, pg_TZDIR(), scan_available_timezones(), score_timezone(), snprintf, strlcpy(), T_DAY, T_MONTH, T_WEEK, tztry::test_times, tm, and TZ_STRLEN_MAX.

Referenced by scan_available_timezones(), and select_default_timezone().

331 {
332  static char resultbuf[TZ_STRLEN_MAX + 1];
333  time_t tnow;
334  time_t t;
335  struct tztry tt;
336  struct tm *tm;
337  int thisyear;
338  int bestscore;
339  char tmptzdir[MAXPGPATH];
340  int std_ofs;
341  char std_zone_name[TZ_STRLEN_MAX + 1],
342  dst_zone_name[TZ_STRLEN_MAX + 1];
343  char cbuf[TZ_STRLEN_MAX + 1];
344 
345  /* Initialize OS timezone library */
346  tzset();
347 
348  /*
349  * Set up the list of dates to be probed to see how well our timezone
350  * matches the system zone. We first probe January and July of the
351  * current year; this serves to quickly eliminate the vast majority of the
352  * TZ database entries. If those dates match, we probe every week for 100
353  * years backwards from the current July. (Weekly resolution is good
354  * enough to identify DST transition rules, since everybody switches on
355  * Sundays.) This is sufficient to cover most of the Unix time_t range,
356  * and we don't want to look further than that since many systems won't
357  * have sane TZ behavior further back anyway. The further back the zone
358  * matches, the better we score it. This may seem like a rather random
359  * way of doing things, but experience has shown that system-supplied
360  * timezone definitions are likely to have DST behavior that is right for
361  * the recent past and not so accurate further back. Scoring in this way
362  * allows us to recognize zones that have some commonality with the IANA
363  * database, without insisting on exact match. (Note: we probe Thursdays,
364  * not Sundays, to avoid triggering DST-transition bugs in localtime
365  * itself.)
366  */
367  tnow = time(NULL);
368  tm = localtime(&tnow);
369  if (!tm)
370  return NULL; /* give up if localtime is broken... */
371  thisyear = tm->tm_year + 1900;
372 
373  t = build_time_t(thisyear, 1, 15);
374 
375  /*
376  * Round back to GMT midnight Thursday. This depends on the knowledge
377  * that the time_t origin is Thu Jan 01 1970. (With a different origin
378  * we'd be probing some other day of the week, but it wouldn't matter
379  * anyway unless localtime() had DST-transition bugs.)
380  */
381  t -= (t % T_WEEK);
382 
383  tt.n_test_times = 0;
384  tt.test_times[tt.n_test_times++] = t;
385 
386  t = build_time_t(thisyear, 7, 15);
387  t -= (t % T_WEEK);
388 
389  tt.test_times[tt.n_test_times++] = t;
390 
391  while (tt.n_test_times < MAX_TEST_TIMES)
392  {
393  t -= T_WEEK;
394  tt.test_times[tt.n_test_times++] = t;
395  }
396 
397  /*
398  * Try to avoid the brute-force search by seeing if we can recognize the
399  * system's timezone setting directly.
400  *
401  * Currently we just check /etc/localtime; there are other conventions for
402  * this, but that seems to be the only one used on enough platforms to be
403  * worth troubling over.
404  */
405  if (check_system_link_file("/etc/localtime", &tt, resultbuf))
406  return resultbuf;
407 
408  /* No luck, so search for the best-matching timezone file */
409  strlcpy(tmptzdir, pg_TZDIR(), sizeof(tmptzdir));
410  bestscore = -1;
411  resultbuf[0] = '\0';
412  scan_available_timezones(tmptzdir, tmptzdir + strlen(tmptzdir) + 1,
413  &tt,
414  &bestscore, resultbuf);
415  if (bestscore > 0)
416  {
417  /* Ignore IANA's rather silly "Factory" zone; use GMT instead */
418  if (strcmp(resultbuf, "Factory") == 0)
419  return NULL;
420  return resultbuf;
421  }
422 
423  /*
424  * Couldn't find a match in the database, so next we try constructed zone
425  * names (like "PST8PDT").
426  *
427  * First we need to determine the names of the local standard and daylight
428  * zones. The idea here is to scan forward from today until we have seen
429  * both zones, if both are in use.
430  */
431  memset(std_zone_name, 0, sizeof(std_zone_name));
432  memset(dst_zone_name, 0, sizeof(dst_zone_name));
433  std_ofs = 0;
434 
435  tnow = time(NULL);
436 
437  /*
438  * Round back to a GMT midnight so results don't depend on local time of
439  * day
440  */
441  tnow -= (tnow % T_DAY);
442 
443  /*
444  * We have to look a little further ahead than one year, in case today is
445  * just past a DST boundary that falls earlier in the year than the next
446  * similar boundary. Arbitrarily scan up to 14 months.
447  */
448  for (t = tnow; t <= tnow + T_MONTH * 14; t += T_MONTH)
449  {
450  tm = localtime(&t);
451  if (!tm)
452  continue;
453  if (tm->tm_isdst < 0)
454  continue;
455  if (tm->tm_isdst == 0 && std_zone_name[0] == '\0')
456  {
457  /* found STD zone */
458  memset(cbuf, 0, sizeof(cbuf));
459  strftime(cbuf, sizeof(cbuf) - 1, "%Z", tm); /* zone abbr */
460  strcpy(std_zone_name, cbuf);
461  std_ofs = get_timezone_offset(tm);
462  }
463  if (tm->tm_isdst > 0 && dst_zone_name[0] == '\0')
464  {
465  /* found DST zone */
466  memset(cbuf, 0, sizeof(cbuf));
467  strftime(cbuf, sizeof(cbuf) - 1, "%Z", tm); /* zone abbr */
468  strcpy(dst_zone_name, cbuf);
469  }
470  /* Done if found both */
471  if (std_zone_name[0] && dst_zone_name[0])
472  break;
473  }
474 
475  /* We should have found a STD zone name by now... */
476  if (std_zone_name[0] == '\0')
477  {
478 #ifdef DEBUG_IDENTIFY_TIMEZONE
479  fprintf(stderr, "could not determine system time zone\n");
480 #endif
481  return NULL; /* go to GMT */
482  }
483 
484  /* If we found DST then try STD<ofs>DST */
485  if (dst_zone_name[0] != '\0')
486  {
487  snprintf(resultbuf, sizeof(resultbuf), "%s%d%s",
488  std_zone_name, -std_ofs / 3600, dst_zone_name);
489  if (score_timezone(resultbuf, &tt) > 0)
490  return resultbuf;
491  }
492 
493  /* Try just the STD timezone (works for GMT at least) */
494  strcpy(resultbuf, std_zone_name);
495  if (score_timezone(resultbuf, &tt) > 0)
496  return resultbuf;
497 
498  /* Try STD<ofs> */
499  snprintf(resultbuf, sizeof(resultbuf), "%s%d",
500  std_zone_name, -std_ofs / 3600);
501  if (score_timezone(resultbuf, &tt) > 0)
502  return resultbuf;
503 
504  /*
505  * Did not find the timezone. Fallback to use a GMT zone. Note that the
506  * IANA timezone database names the GMT-offset zones in POSIX style: plus
507  * is west of Greenwich. It's unfortunate that this is opposite of SQL
508  * conventions. Should we therefore change the names? Probably not...
509  */
510  snprintf(resultbuf, sizeof(resultbuf), "Etc/GMT%s%d",
511  (-std_ofs > 0) ? "+" : "", -std_ofs / 3600);
512 
513 #ifdef DEBUG_IDENTIFY_TIMEZONE
514  fprintf(stderr, "could not recognize system time zone, using \"%s\"\n",
515  resultbuf);
516 #endif
517  return resultbuf;
518 }
#define T_DAY
Definition: findtimezone.c:152
static void scan_available_timezones(char *tzdir, char *tzdirsub, struct tztry *tt, int *bestscore, char *bestzonename)
Definition: findtimezone.c:656
#define T_MONTH
Definition: findtimezone.c:154
#define fprintf
Definition: port.h:196
static const char * pg_TZDIR(void)
Definition: findtimezone.c:37
static struct pg_tm tm
Definition: localtime.c:108
#define TZ_STRLEN_MAX
Definition: pgtime.h:44
#define MAXPGPATH
static int get_timezone_offset(struct tm *tm)
Definition: findtimezone.c:175
static time_t build_time_t(int year, int month, int day)
Definition: findtimezone.c:190
#define T_WEEK
Definition: findtimezone.c:153
static int score_timezone(const char *tzname, struct tztry *tt)
Definition: findtimezone.c:233
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static bool check_system_link_file(const char *linkname, struct tztry *tt, char *bestzonename)
Definition: findtimezone.c:543
#define snprintf
Definition: port.h:192
#define MAX_TEST_TIMES
Definition: findtimezone.c:156

◆ perfect_timezone_match()

static bool perfect_timezone_match ( const char *  tzname,
struct tztry tt 
)
static

Definition at line 319 of file findtimezone.c.

References tztry::n_test_times, and score_timezone().

Referenced by check_system_link_file().

320 {
321  return (score_timezone(tzname, tt) == tt->n_test_times);
322 }
int n_test_times
Definition: findtimezone.c:160
static int score_timezone(const char *tzname, struct tztry *tt)
Definition: findtimezone.c:233

◆ pg_load_tz()

static pg_tz* pg_load_tz ( const char *  name)
static

Definition at line 91 of file findtimezone.c.

References pg_tz::state, TZ_STRLEN_MAX, tzload(), pg_tz::TZname, and tzparse().

Referenced by score_timezone(), and validate_zone().

92 {
93  static pg_tz tz;
94 
95  if (strlen(name) > TZ_STRLEN_MAX)
96  return NULL; /* not going to fit */
97 
98  /*
99  * "GMT" is always sent to tzparse(); see comments for pg_tzset().
100  */
101  if (strcmp(name, "GMT") == 0)
102  {
103  if (!tzparse(name, &tz.state, true))
104  {
105  /* This really, really should not happen ... */
106  return NULL;
107  }
108  }
109  else if (tzload(name, NULL, &tz.state, true) != 0)
110  {
111  if (name[0] == ':' || !tzparse(name, &tz.state, false))
112  {
113  return NULL; /* unknown timezone */
114  }
115  }
116 
117  strcpy(tz.TZname, name);
118 
119  return &tz;
120 }
int tzload(const char *name, char *canonname, struct state *sp, bool doextend)
Definition: localtime.c:588
#define TZ_STRLEN_MAX
Definition: pgtime.h:44
bool tzparse(const char *name, struct state *sp, bool lastditch)
Definition: localtime.c:938
struct state state
Definition: pgtz.h:69
Definition: pgtz.h:65
char TZname[TZ_STRLEN_MAX+1]
Definition: pgtz.h:68
const char * name
Definition: encode.c:521

◆ pg_open_tzfile()

int pg_open_tzfile ( const char *  name,
char *  canonname 
)

Definition at line 65 of file findtimezone.c.

Referenced by tzloadbody().

66 {
67  char fullname[MAXPGPATH];
68 
69  if (canonname)
70  strlcpy(canonname, name, TZ_STRLEN_MAX + 1);
71 
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);
77 
78  return open(fullname, O_RDONLY | PG_BINARY, 0);
79 }
static const char * pg_TZDIR(void)
Definition: findtimezone.c:37
#define PG_BINARY
Definition: c.h:1222
#define TZ_STRLEN_MAX
Definition: pgtime.h:44
#define MAXPGPATH
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
const char * name
Definition: encode.c:521

◆ pg_TZDIR()

static const char* pg_TZDIR ( void  )
static

Definition at line 37 of file findtimezone.c.

References tzdirpath.

Referenced by identify_system_timezone(), and pg_open_tzfile().

38 {
39 #ifndef SYSTEMTZDIR
40  /* normal case: timezone stuff is under our share dir */
41  return tzdirpath;
42 #else
43  /* we're configured to use system's timezone database */
44  return SYSTEMTZDIR;
45 #endif
46 }
static char tzdirpath[MAXPGPATH]
Definition: findtimezone.c:27

◆ scan_available_timezones()

static void scan_available_timezones ( char *  tzdir,
char *  tzdirsub,
struct tztry tt,
int *  bestscore,
char *  bestzonename 
)
static

Definition at line 656 of file findtimezone.c.

References fprintf, i, identify_system_timezone(), idx(), sort-test::key, MAXPGPATH, name, pgfnames(), pgfnames_cleanup(), S_ISDIR, score_timezone(), snprintf, stat, strerror, strlcpy(), tm, TZ_STRLEN_MAX, and zone_name_pref().

Referenced by identify_system_timezone().

658 {
659  int tzdir_orig_len = strlen(tzdir);
660  char **names;
661  char **namep;
662 
663  names = pgfnames(tzdir);
664  if (!names)
665  return;
666 
667  for (namep = names; *namep; namep++)
668  {
669  char *name = *namep;
670  struct stat statbuf;
671 
672  /* Ignore . and .., plus any other "hidden" files */
673  if (name[0] == '.')
674  continue;
675 
676  snprintf(tzdir + tzdir_orig_len, MAXPGPATH - tzdir_orig_len,
677  "/%s", name);
678 
679  if (stat(tzdir, &statbuf) != 0)
680  {
681 #ifdef DEBUG_IDENTIFY_TIMEZONE
682  fprintf(stderr, "could not stat \"%s\": %s\n",
683  tzdir, strerror(errno));
684 #endif
685  tzdir[tzdir_orig_len] = '\0';
686  continue;
687  }
688 
689  if (S_ISDIR(statbuf.st_mode))
690  {
691  /* Recurse into subdirectory */
692  scan_available_timezones(tzdir, tzdirsub, tt,
693  bestscore, bestzonename);
694  }
695  else
696  {
697  /* Load and test this file */
698  int score = score_timezone(tzdirsub, tt);
699 
700  if (score > *bestscore)
701  {
702  *bestscore = score;
703  strlcpy(bestzonename, tzdirsub, TZ_STRLEN_MAX + 1);
704  }
705  else if (score == *bestscore)
706  {
707  /* Consider how to break a tie */
708  int namepref = (zone_name_pref(tzdirsub) -
709  zone_name_pref(bestzonename));
710 
711  if (namepref > 0 ||
712  (namepref == 0 &&
713  (strlen(tzdirsub) < strlen(bestzonename) ||
714  (strlen(tzdirsub) == strlen(bestzonename) &&
715  strcmp(tzdirsub, bestzonename) < 0))))
716  strlcpy(bestzonename, tzdirsub, TZ_STRLEN_MAX + 1);
717  }
718  }
719 
720  /* Restore tzdir */
721  tzdir[tzdir_orig_len] = '\0';
722  }
723 
724  pgfnames_cleanup(names);
725 }
char ** pgfnames(const char *path)
Definition: pgfnames.c:37
static void scan_available_timezones(char *tzdir, char *tzdirsub, struct tztry *tt, int *bestscore, char *bestzonename)
Definition: findtimezone.c:656
#define fprintf
Definition: port.h:196
static int zone_name_pref(const char *zonename)
Definition: findtimezone.c:614
#define TZ_STRLEN_MAX
Definition: pgtime.h:44
#define MAXPGPATH
#define stat(a, b)
Definition: win32_port.h:255
static int score_timezone(const char *tzname, struct tztry *tt)
Definition: findtimezone.c:233
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
void pgfnames_cleanup(char **filenames)
Definition: pgfnames.c:86
#define strerror
Definition: port.h:205
const char * name
Definition: encode.c:521
#define S_ISDIR(m)
Definition: win32_port.h:296
#define snprintf
Definition: port.h:192

◆ score_timezone()

static int score_timezone ( const char *  tzname,
struct tztry tt 
)
static

Definition at line 233 of file findtimezone.c.

References compare_tm(), fprintf, i, tztry::n_test_times, pg_load_tz(), pg_localtime(), pg_tz_acceptable(), tztry::test_times, tm, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, pg_tm::tm_zone, and TZ_STRLEN_MAX.

Referenced by identify_system_timezone(), perfect_timezone_match(), and scan_available_timezones().

234 {
235  int i;
236  pg_time_t pgtt;
237  struct tm *systm;
238  struct pg_tm *pgtm;
239  char cbuf[TZ_STRLEN_MAX + 1];
240  pg_tz *tz;
241 
242  /* Load timezone definition */
243  tz = pg_load_tz(tzname);
244  if (!tz)
245  return -1; /* unrecognized zone name */
246 
247  /* Reject if leap seconds involved */
248  if (!pg_tz_acceptable(tz))
249  {
250 #ifdef DEBUG_IDENTIFY_TIMEZONE
251  fprintf(stderr, "Reject TZ \"%s\": uses leap seconds\n", tzname);
252 #endif
253  return -1;
254  }
255 
256  /* Check for match at all the test times */
257  for (i = 0; i < tt->n_test_times; i++)
258  {
259  pgtt = (pg_time_t) (tt->test_times[i]);
260  pgtm = pg_localtime(&pgtt, tz);
261  if (!pgtm)
262  return -1; /* probably shouldn't happen */
263  systm = localtime(&(tt->test_times[i]));
264  if (!systm)
265  {
266 #ifdef DEBUG_IDENTIFY_TIMEZONE
267  fprintf(stderr, "TZ \"%s\" scores %d: at %ld %04d-%02d-%02d %02d:%02d:%02d %s, system had no data\n",
268  tzname, i, (long) pgtt,
269  pgtm->tm_year + 1900, pgtm->tm_mon + 1, pgtm->tm_mday,
270  pgtm->tm_hour, pgtm->tm_min, pgtm->tm_sec,
271  pgtm->tm_isdst ? "dst" : "std");
272 #endif
273  return i;
274  }
275  if (!compare_tm(systm, pgtm))
276  {
277 #ifdef DEBUG_IDENTIFY_TIMEZONE
278  fprintf(stderr, "TZ \"%s\" scores %d: at %ld %04d-%02d-%02d %02d:%02d:%02d %s versus %04d-%02d-%02d %02d:%02d:%02d %s\n",
279  tzname, i, (long) pgtt,
280  pgtm->tm_year + 1900, pgtm->tm_mon + 1, pgtm->tm_mday,
281  pgtm->tm_hour, pgtm->tm_min, pgtm->tm_sec,
282  pgtm->tm_isdst ? "dst" : "std",
283  systm->tm_year + 1900, systm->tm_mon + 1, systm->tm_mday,
284  systm->tm_hour, systm->tm_min, systm->tm_sec,
285  systm->tm_isdst ? "dst" : "std");
286 #endif
287  return i;
288  }
289  if (systm->tm_isdst >= 0)
290  {
291  /* Check match of zone names, too */
292  if (pgtm->tm_zone == NULL)
293  return -1; /* probably shouldn't happen */
294  memset(cbuf, 0, sizeof(cbuf));
295  strftime(cbuf, sizeof(cbuf) - 1, "%Z", systm); /* zone abbr */
296  if (strcmp(cbuf, pgtm->tm_zone) != 0)
297  {
298 #ifdef DEBUG_IDENTIFY_TIMEZONE
299  fprintf(stderr, "TZ \"%s\" scores %d: at %ld \"%s\" versus \"%s\"\n",
300  tzname, i, (long) pgtt,
301  pgtm->tm_zone, cbuf);
302 #endif
303  return i;
304  }
305  }
306  }
307 
308 #ifdef DEBUG_IDENTIFY_TIMEZONE
309  fprintf(stderr, "TZ \"%s\" gets max score %d\n", tzname, i);
310 #endif
311 
312  return i;
313 }
int64 pg_time_t
Definition: pgtime.h:23
int tm_isdst
Definition: pgtime.h:35
int tm_hour
Definition: pgtime.h:29
time_t test_times[MAX_TEST_TIMES]
Definition: findtimezone.c:161
Definition: pgtime.h:25
static pg_tz * pg_load_tz(const char *name)
Definition: findtimezone.c:91
#define fprintf
Definition: port.h:196
static struct pg_tm tm
Definition: localtime.c:108
#define TZ_STRLEN_MAX
Definition: pgtime.h:44
static bool compare_tm(struct tm *s, struct pg_tm *p)
Definition: findtimezone.c:206
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int n_test_times
Definition: findtimezone.c:160
const char * tm_zone
Definition: pgtime.h:37
bool pg_tz_acceptable(pg_tz *tz)
Definition: localtime.c:1905
Definition: pgtz.h:65
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1375
int tm_year
Definition: pgtime.h:32
int i
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ select_default_timezone()

const char * select_default_timezone ( const char *  share_path)

Definition at line 1527 of file findtimezone.c.

References identify_system_timezone(), snprintf, tzdirpath, and validate_zone().

Referenced by test_config_settings().

1528 {
1529  const char *tzname;
1530 
1531  /* Initialize timezone directory path, if needed */
1532 #ifndef SYSTEMTZDIR
1533  snprintf(tzdirpath, sizeof(tzdirpath), "%s/timezone", share_path);
1534 #endif
1535 
1536  /* Check TZ environment variable */
1537  tzname = getenv("TZ");
1538  if (validate_zone(tzname))
1539  return tzname;
1540 
1541  /* Nope, so try to identify the system timezone */
1542  tzname = identify_system_timezone();
1543  if (validate_zone(tzname))
1544  return tzname;
1545 
1546  return NULL;
1547 }
static bool validate_zone(const char *tzname)
static char * share_path
Definition: initdb.c:120
static char tzdirpath[MAXPGPATH]
Definition: findtimezone.c:27
static const char * identify_system_timezone(void)
Definition: findtimezone.c:330
#define snprintf
Definition: port.h:192

◆ validate_zone()

static bool validate_zone ( const char *  tzname)
static

Definition at line 1498 of file findtimezone.c.

References pg_load_tz(), and pg_tz_acceptable().

Referenced by select_default_timezone().

1499 {
1500  pg_tz *tz;
1501 
1502  if (!tzname || !tzname[0])
1503  return false;
1504 
1505  tz = pg_load_tz(tzname);
1506  if (!tz)
1507  return false;
1508 
1509  if (!pg_tz_acceptable(tz))
1510  return false;
1511 
1512  return true;
1513 }
static pg_tz * pg_load_tz(const char *name)
Definition: findtimezone.c:91
bool pg_tz_acceptable(pg_tz *tz)
Definition: localtime.c:1905
Definition: pgtz.h:65

◆ zone_name_pref()

static int zone_name_pref ( const char *  zonename)
static

Definition at line 614 of file findtimezone.c.

Referenced by scan_available_timezones().

615 {
616  /*
617  * Prefer UTC over alternatives such as UCT. Also prefer Etc/UTC over
618  * Etc/UCT; but UTC is preferred to Etc/UTC.
619  */
620  if (strcmp(zonename, "UTC") == 0)
621  return 50;
622  if (strcmp(zonename, "Etc/UTC") == 0)
623  return 40;
624 
625  /*
626  * We don't want to pick "localtime" or "posixrules", unless we can find
627  * no other name for the prevailing zone. Those aren't real zone names.
628  */
629  if (strcmp(zonename, "localtime") == 0 ||
630  strcmp(zonename, "posixrules") == 0)
631  return -50;
632 
633  return 0;
634 }

Variable Documentation

◆ tzdirpath

char tzdirpath[MAXPGPATH]
static

Definition at line 27 of file findtimezone.c.

Referenced by pg_TZDIR(), and select_default_timezone().