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.

◆ T_DAY

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

Definition at line 152 of file findtimezone.c.

◆ T_MONTH

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

Definition at line 154 of file findtimezone.c.

◆ T_WEEK

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

Definition at line 153 of file findtimezone.c.

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.

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  tm.tm_isdst = -1;
199 
200  return mktime(&tm);
201 }
static struct pg_tm tm
Definition: localtime.c:104
int tm_mday
Definition: pgtime.h:39
int tm_mon
Definition: pgtime.h:40
int tm_isdst
Definition: pgtime.h:44
int tm_year
Definition: pgtime.h:41

References tm, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

Referenced by identify_system_timezone().

◆ check_system_link_file()

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

Definition at line 544 of file findtimezone.c.

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

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

Referenced by identify_system_timezone().

◆ compare_tm()

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

Definition at line 207 of file findtimezone.c.

208 {
209  if (s->tm_sec != p->tm_sec ||
210  s->tm_min != p->tm_min ||
211  s->tm_hour != p->tm_hour ||
212  s->tm_mday != p->tm_mday ||
213  s->tm_mon != p->tm_mon ||
214  s->tm_year != p->tm_year ||
215  s->tm_wday != p->tm_wday ||
216  s->tm_yday != p->tm_yday ||
217  s->tm_isdst != p->tm_isdst)
218  return false;
219  return true;
220 }
int tm_hour
Definition: pgtime.h:38
int tm_min
Definition: pgtime.h:37
int tm_yday
Definition: pgtime.h:43
int tm_wday
Definition: pgtime.h:42
int tm_sec
Definition: pgtime.h:36

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().

◆ get_timezone_offset()

static int get_timezone_offset ( struct tm tm)
static

Definition at line 175 of file findtimezone.c.

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:268
long int tm_gmtoff
Definition: pgtime.h:45

References TIMEZONE_GLOBAL, tm, and pg_tm::tm_gmtoff.

Referenced by identify_system_timezone().

◆ identify_system_timezone()

static const char* identify_system_timezone ( void  )
static

Definition at line 331 of file findtimezone.c.

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

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, pg_tm::tm_isdst, pg_tm::tm_year, and TZ_STRLEN_MAX.

Referenced by select_default_timezone().

◆ perfect_timezone_match()

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

Definition at line 320 of file findtimezone.c.

321 {
322  return (score_timezone(tzname, tt) == tt->n_test_times);
323 }
int n_test_times
Definition: findtimezone.c:160

References tztry::n_test_times, and score_timezone().

Referenced by check_system_link_file().

◆ pg_load_tz()

static pg_tz* pg_load_tz ( const char *  name)
static

Definition at line 91 of file findtimezone.c.

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:586
bool tzparse(const char *name, struct state *sp, bool lastditch)
Definition: localtime.c:936
Definition: pgtz.h:66
char TZname[TZ_STRLEN_MAX+1]
Definition: pgtz.h:68
struct state state
Definition: pgtz.h:69
const char * name

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

Referenced by score_timezone(), and validate_zone().

◆ pg_open_tzfile()

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

Definition at line 65 of file findtimezone.c.

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 }
#define PG_BINARY
Definition: c.h:1273

References MAXPGPATH, name, PG_BINARY, pg_TZDIR(), strlcpy(), and TZ_STRLEN_MAX.

Referenced by tzloadbody().

◆ pg_TZDIR()

static const char* pg_TZDIR ( void  )
static

Definition at line 37 of file findtimezone.c.

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

References tzdirpath.

Referenced by identify_system_timezone(), and pg_open_tzfile().

◆ scan_available_timezones()

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

Definition at line 657 of file findtimezone.c.

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

References fprintf, MAXPGPATH, name, pgfnames(), pgfnames_cleanup(), S_ISDIR, score_timezone(), snprintf, stat::st_mode, stat, strlcpy(), TZ_STRLEN_MAX, and zone_name_pref().

Referenced by identify_system_timezone().

◆ score_timezone()

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

Definition at line 234 of file findtimezone.c.

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

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().

◆ select_default_timezone()

const char * select_default_timezone ( const char *  share_path)

Definition at line 1757 of file findtimezone.c.

1758 {
1759  const char *tzname;
1760 
1761  /* Initialize timezone directory path, if needed */
1762 #ifndef SYSTEMTZDIR
1763  snprintf(tzdirpath, sizeof(tzdirpath), "%s/timezone", share_path);
1764 #endif
1765 
1766  /* Check TZ environment variable */
1767  tzname = getenv("TZ");
1768  if (validate_zone(tzname))
1769  return tzname;
1770 
1771  /* Nope, so try to identify the system timezone */
1772  tzname = identify_system_timezone();
1773  if (validate_zone(tzname))
1774  return tzname;
1775 
1776  return NULL;
1777 }
static const char * identify_system_timezone(void)
Definition: findtimezone.c:331
static bool validate_zone(const char *tzname)
static char * share_path
Definition: initdb.c:135

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

Referenced by test_config_settings().

◆ validate_zone()

static bool validate_zone ( const char *  tzname)
static

Definition at line 1728 of file findtimezone.c.

1729 {
1730  pg_tz *tz;
1731 
1732  if (!tzname || !tzname[0])
1733  return false;
1734 
1735  tz = pg_load_tz(tzname);
1736  if (!tz)
1737  return false;
1738 
1739  if (!pg_tz_acceptable(tz))
1740  return false;
1741 
1742  return true;
1743 }

References pg_load_tz(), and pg_tz_acceptable().

Referenced by select_default_timezone().

◆ zone_name_pref()

static int zone_name_pref ( const char *  zonename)
static

Definition at line 615 of file findtimezone.c.

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

Referenced by scan_available_timezones().

Variable Documentation

◆ tzdirpath

char tzdirpath[MAXPGPATH]
static

Definition at line 27 of file findtimezone.c.

Referenced by pg_TZDIR(), and select_default_timezone().