PostgreSQL Source Code  git master
pgtime.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pg_tm
 

Macros

#define TZ_STRLEN_MAX   255
 

Typedefs

typedef int64 pg_time_t
 
typedef struct pg_tz pg_tz
 
typedef struct pg_tzenum pg_tzenum
 

Functions

struct pg_tmpg_localtime (const pg_time_t *timep, const pg_tz *tz)
 
struct pg_tmpg_gmtime (const pg_time_t *timep)
 
int pg_next_dst_boundary (const pg_time_t *timep, long int *before_gmtoff, int *before_isdst, pg_time_t *boundary, long int *after_gmtoff, int *after_isdst, const pg_tz *tz)
 
bool pg_interpret_timezone_abbrev (const char *abbrev, const pg_time_t *timep, long int *gmtoff, int *isdst, const pg_tz *tz)
 
bool pg_get_timezone_offset (const pg_tz *tz, long int *gmtoff)
 
const char * pg_get_timezone_name (pg_tz *tz)
 
bool pg_tz_acceptable (pg_tz *tz)
 
size_t pg_strftime (char *s, size_t max, const char *format, const struct pg_tm *tm)
 
void pg_timezone_initialize (void)
 
pg_tzpg_tzset (const char *tzname)
 
pg_tzpg_tzset_offset (long gmtoffset)
 
pg_tzenumpg_tzenumerate_start (void)
 
pg_tzpg_tzenumerate_next (pg_tzenum *dir)
 
void pg_tzenumerate_end (pg_tzenum *dir)
 

Variables

PGDLLIMPORT pg_tzsession_timezone
 
pg_tzlog_timezone
 

Macro Definition Documentation

◆ TZ_STRLEN_MAX

Typedef Documentation

◆ pg_time_t

typedef int64 pg_time_t

Definition at line 23 of file pgtime.h.

◆ pg_tz

typedef struct pg_tz pg_tz

Definition at line 47 of file pgtime.h.

◆ pg_tzenum

typedef struct pg_tzenum pg_tzenum

Definition at line 48 of file pgtime.h.

Function Documentation

◆ pg_get_timezone_name()

const char* pg_get_timezone_name ( pg_tz tz)

Definition at line 1873 of file localtime.c.

References pg_tz::TZname.

Referenced by pg_timezone_names(), show_log_timezone(), and show_timezone().

1874 {
1875  if (tz)
1876  return tz->TZname;
1877  return NULL;
1878 }
char TZname[TZ_STRLEN_MAX+1]
Definition: pgtz.h:68

◆ pg_get_timezone_offset()

bool pg_get_timezone_offset ( const pg_tz tz,
long int *  gmtoff 
)

Definition at line 1849 of file localtime.c.

References i, pg_tz::state, ttinfo::tt_utoff, state::ttis, and state::typecnt.

Referenced by DecodeTimeOnly(), and TimestampTimestampTzRequiresRewrite().

1850 {
1851  /*
1852  * The zone could have more than one ttinfo, if it's historically used
1853  * more than one abbreviation. We return true as long as they all have
1854  * the same gmtoff.
1855  */
1856  const struct state *sp;
1857  int i;
1858 
1859  sp = &tz->state;
1860  for (i = 1; i < sp->typecnt; i++)
1861  {
1862  if (sp->ttis[i].tt_utoff != sp->ttis[0].tt_utoff)
1863  return false;
1864  }
1865  *gmtoff = sp->ttis[0].tt_utoff;
1866  return true;
1867 }
int32 tt_utoff
Definition: pgtz.h:28
struct state state
Definition: pgtz.h:69
struct ttinfo ttis[TZ_MAX_TYPES]
Definition: pgtz.h:51
Definition: regguts.h:317
int i
int typecnt
Definition: pgtz.h:45

◆ pg_gmtime()

struct pg_tm* pg_gmtime ( const pg_time_t timep)

Definition at line 1387 of file localtime.c.

References gmtsub(), and tm.

Referenced by AddFileToBackupManifest(), and GetEpochTime().

1388 {
1389  return gmtsub(timep, 0, &tm);
1390 }
static struct pg_tm tm
Definition: localtime.c:102
static struct pg_tm * gmtsub(pg_time_t const *, int32, struct pg_tm *)
Definition: localtime.c:1355

◆ pg_interpret_timezone_abbrev()

bool pg_interpret_timezone_abbrev ( const char *  abbrev,
const pg_time_t timep,
long int *  gmtoff,
int *  isdst,
const pg_tz tz 
)

Definition at line 1755 of file localtime.c.

References state::charcnt, charcnt, state::chars, i, pg_tz::state, state::timecnt, ttinfo::tt_desigidx, ttinfo::tt_isdst, ttinfo::tt_utoff, state::ttis, and state::types.

Referenced by DetermineTimeZoneAbbrevOffsetInternal().

1760 {
1761  const struct state *sp;
1762  const char *abbrs;
1763  const struct ttinfo *ttisp;
1764  int abbrind;
1765  int cutoff;
1766  int i;
1767  const pg_time_t t = *timep;
1768 
1769  sp = &tz->state;
1770 
1771  /*
1772  * Locate the abbreviation in the zone's abbreviation list. We assume
1773  * there are not duplicates in the list.
1774  */
1775  abbrs = sp->chars;
1776  abbrind = 0;
1777  while (abbrind < sp->charcnt)
1778  {
1779  if (strcmp(abbrev, abbrs + abbrind) == 0)
1780  break;
1781  while (abbrs[abbrind] != '\0')
1782  abbrind++;
1783  abbrind++;
1784  }
1785  if (abbrind >= sp->charcnt)
1786  return false; /* not there! */
1787 
1788  /*
1789  * Unlike pg_next_dst_boundary, we needn't sweat about extrapolation
1790  * (goback/goahead zones). Finding the newest or oldest meaning of the
1791  * abbreviation should get us what we want, since extrapolation would just
1792  * be repeating the newest or oldest meanings.
1793  *
1794  * Use binary search to locate the first transition > cutoff time.
1795  */
1796  {
1797  int lo = 0;
1798  int hi = sp->timecnt;
1799 
1800  while (lo < hi)
1801  {
1802  int mid = (lo + hi) >> 1;
1803 
1804  if (t < sp->ats[mid])
1805  hi = mid;
1806  else
1807  lo = mid + 1;
1808  }
1809  cutoff = lo;
1810  }
1811 
1812  /*
1813  * Scan backwards to find the latest interval using the given abbrev
1814  * before the cutoff time.
1815  */
1816  for (i = cutoff - 1; i >= 0; i--)
1817  {
1818  ttisp = &sp->ttis[sp->types[i]];
1819  if (ttisp->tt_desigidx == abbrind)
1820  {
1821  *gmtoff = ttisp->tt_utoff;
1822  *isdst = ttisp->tt_isdst;
1823  return true;
1824  }
1825  }
1826 
1827  /*
1828  * Not there, so scan forwards to find the first one after.
1829  */
1830  for (i = cutoff; i < sp->timecnt; i++)
1831  {
1832  ttisp = &sp->ttis[sp->types[i]];
1833  if (ttisp->tt_desigidx == abbrind)
1834  {
1835  *gmtoff = ttisp->tt_utoff;
1836  *isdst = ttisp->tt_isdst;
1837  return true;
1838  }
1839  }
1840 
1841  return false; /* hm, not actually used in any interval? */
1842 }
int64 pg_time_t
Definition: pgtime.h:23
int charcnt
Definition: pgtz.h:46
Definition: pgtz.h:26
int32 tt_utoff
Definition: pgtz.h:28
char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS+1, 4),(2 *(TZ_STRLEN_MAX+1)))]
Definition: pgtz.h:53
bool tt_isdst
Definition: pgtz.h:29
int tt_desigidx
Definition: pgtz.h:30
struct state state
Definition: pgtz.h:69
struct ttinfo ttis[TZ_MAX_TYPES]
Definition: pgtz.h:51
unsigned char types[TZ_MAX_TIMES]
Definition: pgtz.h:50
Definition: regguts.h:317
int timecnt
Definition: pgtz.h:44
static int charcnt
Definition: zic.c:173
int i

◆ pg_localtime()

struct pg_tm* pg_localtime ( const pg_time_t timep,
const pg_tz tz 
)

Definition at line 1342 of file localtime.c.

References localsub(), pg_tz::state, and tm.

Referenced by do_pg_start_backup(), do_pg_stop_backup(), log_line_prefix(), logfile_getname(), pg_tz_acceptable(), score_timezone(), set_next_rotation_time(), setup_formatted_log_time(), setup_formatted_start_time(), str_time(), timeofday(), and timestamp2tm().

1343 {
1344  return localsub(&tz->state, timep, &tm);
1345 }
static struct pg_tm * localsub(struct state const *sp, pg_time_t const *timep, struct pg_tm *const tmp)
Definition: localtime.c:1257
static struct pg_tm tm
Definition: localtime.c:102
struct state state
Definition: pgtz.h:69

◆ pg_next_dst_boundary()

int pg_next_dst_boundary ( const pg_time_t timep,
long int *  before_gmtoff,
int *  before_isdst,
pg_time_t boundary,
long int *  after_gmtoff,
int *  after_isdst,
const pg_tz tz 
)

Definition at line 1608 of file localtime.c.

References state::ats, AVGSECSPERYEAR, state::goahead, state::goback, i, pg_tz::state, state::timecnt, ttinfo::tt_isdst, ttinfo::tt_utoff, state::ttis, state::typecnt, state::types, and YEARSPERREPEAT.

Referenced by DetermineTimeZoneOffsetInternal().

1615 {
1616  const struct state *sp;
1617  const struct ttinfo *ttisp;
1618  int i;
1619  int j;
1620  const pg_time_t t = *timep;
1621 
1622  sp = &tz->state;
1623  if (sp->timecnt == 0)
1624  {
1625  /* non-DST zone, use lowest-numbered standard type */
1626  i = 0;
1627  while (sp->ttis[i].tt_isdst)
1628  if (++i >= sp->typecnt)
1629  {
1630  i = 0;
1631  break;
1632  }
1633  ttisp = &sp->ttis[i];
1634  *before_gmtoff = ttisp->tt_utoff;
1635  *before_isdst = ttisp->tt_isdst;
1636  return 0;
1637  }
1638  if ((sp->goback && t < sp->ats[0]) ||
1639  (sp->goahead && t > sp->ats[sp->timecnt - 1]))
1640  {
1641  /* For values outside the transition table, extrapolate */
1642  pg_time_t newt = t;
1643  pg_time_t seconds;
1644  pg_time_t tcycles;
1645  int64 icycles;
1646  int result;
1647 
1648  if (t < sp->ats[0])
1649  seconds = sp->ats[0] - t;
1650  else
1651  seconds = t - sp->ats[sp->timecnt - 1];
1652  --seconds;
1653  tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR;
1654  ++tcycles;
1655  icycles = tcycles;
1656  if (tcycles - icycles >= 1 || icycles - tcycles >= 1)
1657  return -1;
1658  seconds = icycles;
1659  seconds *= YEARSPERREPEAT;
1660  seconds *= AVGSECSPERYEAR;
1661  if (t < sp->ats[0])
1662  newt += seconds;
1663  else
1664  newt -= seconds;
1665  if (newt < sp->ats[0] ||
1666  newt > sp->ats[sp->timecnt - 1])
1667  return -1; /* "cannot happen" */
1668 
1669  result = pg_next_dst_boundary(&newt, before_gmtoff,
1670  before_isdst,
1671  boundary,
1672  after_gmtoff,
1673  after_isdst,
1674  tz);
1675  if (t < sp->ats[0])
1676  *boundary -= seconds;
1677  else
1678  *boundary += seconds;
1679  return result;
1680  }
1681 
1682  if (t >= sp->ats[sp->timecnt - 1])
1683  {
1684  /* No known transition > t, so use last known segment's type */
1685  i = sp->types[sp->timecnt - 1];
1686  ttisp = &sp->ttis[i];
1687  *before_gmtoff = ttisp->tt_utoff;
1688  *before_isdst = ttisp->tt_isdst;
1689  return 0;
1690  }
1691  if (t < sp->ats[0])
1692  {
1693  /* For "before", use lowest-numbered standard type */
1694  i = 0;
1695  while (sp->ttis[i].tt_isdst)
1696  if (++i >= sp->typecnt)
1697  {
1698  i = 0;
1699  break;
1700  }
1701  ttisp = &sp->ttis[i];
1702  *before_gmtoff = ttisp->tt_utoff;
1703  *before_isdst = ttisp->tt_isdst;
1704  *boundary = sp->ats[0];
1705  /* And for "after", use the first segment's type */
1706  i = sp->types[0];
1707  ttisp = &sp->ttis[i];
1708  *after_gmtoff = ttisp->tt_utoff;
1709  *after_isdst = ttisp->tt_isdst;
1710  return 1;
1711  }
1712  /* Else search to find the boundary following t */
1713  {
1714  int lo = 1;
1715  int hi = sp->timecnt - 1;
1716 
1717  while (lo < hi)
1718  {
1719  int mid = (lo + hi) >> 1;
1720 
1721  if (t < sp->ats[mid])
1722  hi = mid;
1723  else
1724  lo = mid + 1;
1725  }
1726  i = lo;
1727  }
1728  j = sp->types[i - 1];
1729  ttisp = &sp->ttis[j];
1730  *before_gmtoff = ttisp->tt_utoff;
1731  *before_isdst = ttisp->tt_isdst;
1732  *boundary = sp->ats[i];
1733  j = sp->types[i];
1734  ttisp = &sp->ttis[j];
1735  *after_gmtoff = ttisp->tt_utoff;
1736  *after_isdst = ttisp->tt_isdst;
1737  return 1;
1738 }
#define AVGSECSPERYEAR
Definition: private.h:158
int64 pg_time_t
Definition: pgtime.h:23
bool goback
Definition: pgtz.h:47
Definition: pgtz.h:26
int32 tt_utoff
Definition: pgtz.h:28
pg_time_t ats[TZ_MAX_TIMES]
Definition: pgtz.h:49
bool tt_isdst
Definition: pgtz.h:29
struct state state
Definition: pgtz.h:69
struct ttinfo ttis[TZ_MAX_TYPES]
Definition: pgtz.h:51
unsigned char types[TZ_MAX_TIMES]
Definition: pgtz.h:50
int pg_next_dst_boundary(const pg_time_t *timep, long int *before_gmtoff, int *before_isdst, pg_time_t *boundary, long int *after_gmtoff, int *after_isdst, const pg_tz *tz)
Definition: localtime.c:1608
Definition: regguts.h:317
int timecnt
Definition: pgtz.h:44
#define YEARSPERREPEAT
Definition: private.h:99
int i
bool goahead
Definition: pgtz.h:48
int typecnt
Definition: pgtz.h:45

◆ pg_strftime()

size_t pg_strftime ( char *  s,
size_t  max,
const char *  format,
const struct pg_tm tm 
)

Definition at line 128 of file strftime.c.

References _fmt(), EOVERFLOW, and IN_NONE.

Referenced by AddFileToBackupManifest(), do_pg_start_backup(), do_pg_stop_backup(), log_line_prefix(), logfile_getname(), setup_formatted_log_time(), setup_formatted_start_time(), str_time(), and timeofday().

129 {
130  char *p;
131  int saved_errno = errno;
132  enum warn warn = IN_NONE;
133 
134  p = _fmt(format, t, s, s + maxsize, &warn);
135  if (!p)
136  {
137  errno = EOVERFLOW;
138  return 0;
139  }
140  if (p == s + maxsize)
141  {
142  errno = ERANGE;
143  return 0;
144  }
145  *p = '\0';
146  errno = saved_errno;
147  return p - s;
148 }
static char * _fmt(const char *, const struct pg_tm *, char *, const char *, enum warn *)
Definition: strftime.c:151
warn
Definition: strftime.c:109
static char format
#define EOVERFLOW
Definition: private.h:41

◆ pg_timezone_initialize()

void pg_timezone_initialize ( void  )

Definition at line 361 of file pgtz.c.

References pg_tzset(), and session_timezone.

Referenced by InitializeGUCOptions().

362 {
363  /*
364  * We may not yet know where PGSHAREDIR is (in particular this is true in
365  * an EXEC_BACKEND subprocess). So use "GMT", which pg_tzset forces to be
366  * interpreted without reference to the filesystem. This corresponds to
367  * the bootstrap default for these variables in guc.c, although in
368  * principle it could be different.
369  */
370  session_timezone = pg_tzset("GMT");
372 }
pg_tz * log_timezone
Definition: pgtz.c:31
pg_tz * pg_tzset(const char *name)
Definition: pgtz.c:234
pg_tz * session_timezone
Definition: pgtz.c:28

◆ pg_tz_acceptable()

bool pg_tz_acceptable ( pg_tz tz)

Definition at line 1888 of file localtime.c.

References pg_localtime(), POSTGRES_EPOCH_JDATE, SECS_PER_DAY, pg_tm::tm_sec, and UNIX_EPOCH_JDATE.

Referenced by check_log_timezone(), check_timezone(), pg_tzenumerate_next(), score_timezone(), and validate_zone().

1889 {
1890  struct pg_tm *tt;
1891  pg_time_t time2000;
1892 
1893  /*
1894  * To detect leap-second timekeeping, run pg_localtime for what should be
1895  * GMT midnight, 2000-01-01. Insist that the tm_sec value be zero; any
1896  * other result has to be due to leap seconds.
1897  */
1899  tt = pg_localtime(&time2000, tz);
1900  if (!tt || tt->tm_sec != 0)
1901  return false;
1902 
1903  return true;
1904 }
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1342
int64 pg_time_t
Definition: pgtime.h:23
Definition: pgtime.h:32
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
int tm_sec
Definition: pgtime.h:34

◆ pg_tzenumerate_end()

void pg_tzenumerate_end ( pg_tzenum dir)

Definition at line 414 of file pgtz.c.

References pg_tzenum::depth, pg_tzenum::dirdesc, pg_tzenum::dirname, FreeDir(), and pfree().

Referenced by pg_timezone_names().

415 {
416  while (dir->depth >= 0)
417  {
418  FreeDir(dir->dirdesc[dir->depth]);
419  pfree(dir->dirname[dir->depth]);
420  dir->depth--;
421  }
422  pfree(dir);
423 }
DIR * dirdesc[MAX_TZDIR_DEPTH]
Definition: pgtz.c:389
void pfree(void *pointer)
Definition: mcxt.c:1169
char * dirname[MAX_TZDIR_DEPTH]
Definition: pgtz.c:390
int depth
Definition: pgtz.c:388
int FreeDir(DIR *dir)
Definition: fd.c:2838

◆ pg_tzenumerate_next()

pg_tz* pg_tzenumerate_next ( pg_tzenum dir)

Definition at line 426 of file pgtz.c.

References AllocateDir(), pg_tzenum::baselen, pg_tzenum::depth, pg_tzenum::dirdesc, pg_tzenum::dirname, ereport, errcode_for_file_access(), errmsg(), errmsg_internal(), ERROR, FreeDir(), MAX_TZDIR_DEPTH, MAXPGPATH, pfree(), pg_tz_acceptable(), pstrdup(), ReadDir(), S_ISDIR, snprintf, stat::st_mode, stat, pg_tz::state, strlcpy(), pg_tzenum::tz, tzload(), and pg_tz::TZname.

Referenced by pg_timezone_names().

427 {
428  while (dir->depth >= 0)
429  {
430  struct dirent *direntry;
431  char fullname[MAXPGPATH * 2];
432  struct stat statbuf;
433 
434  direntry = ReadDir(dir->dirdesc[dir->depth], dir->dirname[dir->depth]);
435 
436  if (!direntry)
437  {
438  /* End of this directory */
439  FreeDir(dir->dirdesc[dir->depth]);
440  pfree(dir->dirname[dir->depth]);
441  dir->depth--;
442  continue;
443  }
444 
445  if (direntry->d_name[0] == '.')
446  continue;
447 
448  snprintf(fullname, sizeof(fullname), "%s/%s",
449  dir->dirname[dir->depth], direntry->d_name);
450  if (stat(fullname, &statbuf) != 0)
451  ereport(ERROR,
453  errmsg("could not stat \"%s\": %m", fullname)));
454 
455  if (S_ISDIR(statbuf.st_mode))
456  {
457  /* Step into the subdirectory */
458  if (dir->depth >= MAX_TZDIR_DEPTH - 1)
459  ereport(ERROR,
460  (errmsg_internal("timezone directory stack overflow")));
461  dir->depth++;
462  dir->dirname[dir->depth] = pstrdup(fullname);
463  dir->dirdesc[dir->depth] = AllocateDir(fullname);
464  if (!dir->dirdesc[dir->depth])
465  ereport(ERROR,
467  errmsg("could not open directory \"%s\": %m",
468  fullname)));
469 
470  /* Start over reading in the new directory */
471  continue;
472  }
473 
474  /*
475  * Load this timezone using tzload() not pg_tzset(), so we don't fill
476  * the cache. Also, don't ask for the canonical spelling: we already
477  * know it, and pg_open_tzfile's way of finding it out is pretty
478  * inefficient.
479  */
480  if (tzload(fullname + dir->baselen, NULL, &dir->tz.state, true) != 0)
481  {
482  /* Zone could not be loaded, ignore it */
483  continue;
484  }
485 
486  if (!pg_tz_acceptable(&dir->tz))
487  {
488  /* Ignore leap-second zones */
489  continue;
490  }
491 
492  /* OK, return the canonical zone name spelling. */
493  strlcpy(dir->tz.TZname, fullname + dir->baselen,
494  sizeof(dir->tz.TZname));
495 
496  /* Timezone loaded OK. */
497  return &dir->tz;
498  }
499 
500  /* Nothing more found */
501  return NULL;
502 }
struct pg_tz tz
Definition: pgtz.c:391
int tzload(const char *name, char *canonname, struct state *sp, bool doextend)
Definition: localtime.c:584
char * pstrdup(const char *in)
Definition: mcxt.c:1299
Definition: dirent.h:9
DIR * dirdesc[MAX_TZDIR_DEPTH]
Definition: pgtz.c:389
void pfree(void *pointer)
Definition: mcxt.c:1169
char * dirname[MAX_TZDIR_DEPTH]
Definition: pgtz.c:390
#define ERROR
Definition: elog.h:46
#define MAXPGPATH
#define MAX_TZDIR_DEPTH
Definition: pgtz.c:383
int errcode_for_file_access(void)
Definition: elog.c:721
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2720
bool pg_tz_acceptable(pg_tz *tz)
Definition: localtime.c:1888
struct state state
Definition: pgtz.h:69
#define ereport(elevel,...)
Definition: elog.h:157
int baselen
Definition: pgtz.c:387
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2786
char TZname[TZ_STRLEN_MAX+1]
Definition: pgtz.h:68
#define S_ISDIR(m)
Definition: win32_port.h:316
int errmsg(const char *fmt,...)
Definition: elog.c:909
int depth
Definition: pgtz.c:388
#define snprintf
Definition: port.h:216
int FreeDir(DIR *dir)
Definition: fd.c:2838
#define stat
Definition: win32_port.h:275

◆ pg_tzenumerate_start()

pg_tzenum* pg_tzenumerate_start ( void  )

Definition at line 397 of file pgtz.c.

References AllocateDir(), pg_tzenum::baselen, pg_tzenum::depth, pg_tzenum::dirdesc, pg_tzenum::dirname, ereport, errcode_for_file_access(), errmsg(), ERROR, palloc0(), pg_TZDIR(), and pstrdup().

Referenced by pg_timezone_names().

398 {
399  pg_tzenum *ret = (pg_tzenum *) palloc0(sizeof(pg_tzenum));
400  char *startdir = pstrdup(pg_TZDIR());
401 
402  ret->baselen = strlen(startdir) + 1;
403  ret->depth = 0;
404  ret->dirname[0] = startdir;
405  ret->dirdesc[0] = AllocateDir(startdir);
406  if (!ret->dirdesc[0])
407  ereport(ERROR,
409  errmsg("could not open directory \"%s\": %m", startdir)));
410  return ret;
411 }
char * pstrdup(const char *in)
Definition: mcxt.c:1299
DIR * dirdesc[MAX_TZDIR_DEPTH]
Definition: pgtz.c:389
static const char * pg_TZDIR(void)
Definition: pgtz.c:43
char * dirname[MAX_TZDIR_DEPTH]
Definition: pgtz.c:390
#define ERROR
Definition: elog.h:46
int errcode_for_file_access(void)
Definition: elog.c:721
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2720
void * palloc0(Size size)
Definition: mcxt.c:1093
#define ereport(elevel,...)
Definition: elog.h:157
int baselen
Definition: pgtz.c:387
int errmsg(const char *fmt,...)
Definition: elog.c:909
int depth
Definition: pgtz.c:388

◆ pg_tzset()

pg_tz* pg_tzset ( const char *  tzname)

Definition at line 234 of file pgtz.c.

References elog, ERROR, HASH_ENTER, HASH_FIND, hash_search(), init_timezone_hashtable(), pg_toupper(), pg_tz::state, pg_tz_cache::tz, TZ_STRLEN_MAX, tzload(), pg_tz::TZname, and tzparse().

Referenced by check_log_timezone(), check_timezone(), DecodeDateTime(), DecodeTimeOnly(), FetchDynamicTimeZone(), parse_sane_timezone(), pg_timezone_initialize(), pg_tzset_offset(), timestamp_zone(), timestamptz_trunc_zone(), timestamptz_zone(), and timetz_zone().

235 {
236  pg_tz_cache *tzp;
237  struct state tzstate;
238  char uppername[TZ_STRLEN_MAX + 1];
239  char canonname[TZ_STRLEN_MAX + 1];
240  char *p;
241 
242  if (strlen(name) > TZ_STRLEN_MAX)
243  return NULL; /* not going to fit */
244 
245  if (!timezone_cache)
247  return NULL;
248 
249  /*
250  * Upcase the given name to perform a case-insensitive hashtable search.
251  * (We could alternatively downcase it, but we prefer upcase so that we
252  * can get consistently upcased results from tzparse() in case the name is
253  * a POSIX-style timezone spec.)
254  */
255  p = uppername;
256  while (*name)
257  *p++ = pg_toupper((unsigned char) *name++);
258  *p = '\0';
259 
261  uppername,
262  HASH_FIND,
263  NULL);
264  if (tzp)
265  {
266  /* Timezone found in cache, nothing more to do */
267  return &tzp->tz;
268  }
269 
270  /*
271  * "GMT" is always sent to tzparse(), as per discussion above.
272  */
273  if (strcmp(uppername, "GMT") == 0)
274  {
275  if (!tzparse(uppername, &tzstate, true))
276  {
277  /* This really, really should not happen ... */
278  elog(ERROR, "could not initialize GMT time zone");
279  }
280  /* Use uppercase name as canonical */
281  strcpy(canonname, uppername);
282  }
283  else if (tzload(uppername, canonname, &tzstate, true) != 0)
284  {
285  if (uppername[0] == ':' || !tzparse(uppername, &tzstate, false))
286  {
287  /* Unknown timezone. Fail our call instead of loading GMT! */
288  return NULL;
289  }
290  /* For POSIX timezone specs, use uppercase name as canonical */
291  strcpy(canonname, uppername);
292  }
293 
294  /* Save timezone in the cache */
296  uppername,
297  HASH_ENTER,
298  NULL);
299 
300  /* hash_search already copied uppername into the hash key */
301  strcpy(tzp->tz.TZname, canonname);
302  memcpy(&tzp->tz.state, &tzstate, sizeof(tzstate));
303 
304  return &tzp->tz;
305 }
int tzload(const char *name, char *canonname, struct state *sp, bool doextend)
Definition: localtime.c:584
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
#define TZ_STRLEN_MAX
Definition: pgtime.h:51
#define ERROR
Definition: elog.h:46
static bool init_timezone_hashtable(void)
Definition: pgtz.c:202
bool tzparse(const char *name, struct state *sp, bool lastditch)
Definition: localtime.c:934
pg_tz tz
Definition: pgtz.c:195
struct state state
Definition: pgtz.h:69
static HTAB * timezone_cache
Definition: pgtz.c:198
Definition: regguts.h:317
char TZname[TZ_STRLEN_MAX+1]
Definition: pgtz.h:68
const char * name
Definition: encode.c:561
#define elog(elevel,...)
Definition: elog.h:232
unsigned char pg_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:105

◆ pg_tzset_offset()

pg_tz* pg_tzset_offset ( long  gmtoffset)

Definition at line 320 of file pgtz.c.

References pg_tzset(), SECS_PER_HOUR, SECS_PER_MINUTE, and snprintf.

Referenced by check_timezone(), and timestamptz_trunc_zone().

321 {
322  long absoffset = (gmtoffset < 0) ? -gmtoffset : gmtoffset;
323  char offsetstr[64];
324  char tzname[128];
325 
326  snprintf(offsetstr, sizeof(offsetstr),
327  "%02ld", absoffset / SECS_PER_HOUR);
328  absoffset %= SECS_PER_HOUR;
329  if (absoffset != 0)
330  {
331  snprintf(offsetstr + strlen(offsetstr),
332  sizeof(offsetstr) - strlen(offsetstr),
333  ":%02ld", absoffset / SECS_PER_MINUTE);
334  absoffset %= SECS_PER_MINUTE;
335  if (absoffset != 0)
336  snprintf(offsetstr + strlen(offsetstr),
337  sizeof(offsetstr) - strlen(offsetstr),
338  ":%02ld", absoffset);
339  }
340  if (gmtoffset > 0)
341  snprintf(tzname, sizeof(tzname), "<-%s>+%s",
342  offsetstr, offsetstr);
343  else
344  snprintf(tzname, sizeof(tzname), "<+%s>-%s",
345  offsetstr, offsetstr);
346 
347  return pg_tzset(tzname);
348 }
#define SECS_PER_MINUTE
Definition: timestamp.h:88
pg_tz * pg_tzset(const char *name)
Definition: pgtz.c:234
#define SECS_PER_HOUR
Definition: timestamp.h:87
#define snprintf
Definition: port.h:216

Variable Documentation

◆ log_timezone

◆ session_timezone