PostgreSQL Source Code: src/include/pgtime.h File Reference (original) (raw)

Go to the source code of this file.

Data Structures
struct pg_tm
Typedefs
typedef int64 pg_time_t
typedef struct pg_tz pg_tz
typedef struct pg_tzenum pg_tzenum
Functions
struct pg_tm * pg_localtime (const pg_time_t *timep, const pg_tz *tz)
struct pg_tm * pg_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_timezone_abbrev_is_known (const char *abbrev, bool *isfixed, long int *gmtoff, int *isdst, const pg_tz *tz)
const char * pg_get_next_timezone_abbrev (int *indx, 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 maxsize, const char *format, const struct pg_tm *t)
void pg_timezone_initialize (void)
pg_tz * pg_tzset (const char *tzname)
pg_tz * pg_tzset_offset (long gmtoffset)
pg_tzenum * pg_tzenumerate_start (void)
pg_tz * pg_tzenumerate_next (pg_tzenum *dir)
void pg_tzenumerate_end (pg_tzenum *dir)

TZ_STRLEN_MAX

#define TZ_STRLEN_MAX 255

pg_time_t

pg_tz

pg_tzenum

pg_get_next_timezone_abbrev()

const char * pg_get_next_timezone_abbrev ( int * indx,
const pg_tz * tz
)

Definition at line 1936 of file localtime.c.

1938{

1939 const char *result;

1940 const struct state *sp = &tz->state;

1941 const char *abbrs;

1942 int abbrind;

1943

1944

1945 abbrs = sp->chars;

1946 abbrind = *indx;

1947 if (abbrind < 0 || abbrind >= sp->charcnt)

1948 return NULL;

1949 result = abbrs + abbrind;

1950

1951

1952 while (abbrs[abbrind] != '\0')

1953 abbrind++;

1954 abbrind++;

1955 *indx = abbrind;

1956

1957 return result;

1958}

char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS+1, 4),(2 *(TZ_STRLEN_MAX+1)))]

References state::charcnt, state::chars, and pg_tz::state.

Referenced by pg_timezone_abbrevs_zone().

pg_get_timezone_name()

const char * pg_get_timezone_name ( pg_tz * tz )

pg_get_timezone_offset()

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

pg_gmtime()

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 1743 of file localtime.c.

1748{

1749 const struct state *sp;

1750 const char *abbrs;

1751 const struct ttinfo *ttisp;

1752 int abbrind;

1753 int cutoff;

1754 int i;

1756

1757 sp = &tz->state;

1758

1759

1760

1761

1762

1763 abbrs = sp->chars;

1764 abbrind = 0;

1765 while (abbrind < sp->charcnt)

1766 {

1767 if (strcmp(abbrev, abbrs + abbrind) == 0)

1768 break;

1769 while (abbrs[abbrind] != '\0')

1770 abbrind++;

1771 abbrind++;

1772 }

1773 if (abbrind >= sp->charcnt)

1774 return false;

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786 {

1787 int lo = 0;

1789

1790 while (lo < hi)

1791 {

1792 int mid = (lo + hi) >> 1;

1793

1794 if (t < sp->ats[mid])

1795 hi = mid;

1796 else

1797 lo = mid + 1;

1798 }

1799 cutoff = lo;

1800 }

1801

1802

1803

1804

1805

1806 for (i = cutoff - 1; i >= 0; i--)

1807 {

1810 {

1813 return true;

1814 }

1815 }

1816

1817

1818

1819

1820

1823 {

1826 return true;

1827 }

1828

1829

1830

1831

1832 for (i = cutoff; i < sp->timecnt; i++)

1833 {

1836 {

1839 return true;

1840 }

1841 }

1842

1843 return false;

1844}

unsigned char types[TZ_MAX_TIMES]

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

Referenced by DetermineTimeZoneAbbrevOffsetInternal(), and pg_timezone_abbrevs_zone().

pg_localtime()

Definition at line 1344 of file localtime.c.

1345{

1347}

static struct pg_tm * localsub(struct state const *sp, pg_time_t const *timep, struct pg_tm *const tmp)

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

Referenced by build_backup_content(), get_formatted_log_time(), get_formatted_start_time(), log_status_format(), logfile_getname(), pg_tz_acceptable(), score_timezone(), set_next_rotation_time(), str_time(), timeofday(), and timestamp2tm().

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 1610 of file localtime.c.

1617{

1618 const struct state *sp;

1619 const struct ttinfo *ttisp;

1620 int i;

1621 int j;

1623

1624 sp = &tz->state;

1626 {

1627

1629 *before_gmtoff = ttisp->tt_utoff;

1630 *before_isdst = ttisp->tt_isdst;

1631 return 0;

1632 }

1633 if ((sp->goback && t < sp->ats[0]) ||

1635 {

1636

1641 int result;

1642

1643 if (t < sp->ats[0])

1644 seconds = sp->ats[0] - t;

1645 else

1646 seconds = t - sp->ats[sp->timecnt - 1];

1647 --seconds;

1649 ++tcycles;

1650 icycles = tcycles;

1651 if (tcycles - icycles >= 1 || icycles - tcycles >= 1)

1652 return -1;

1653 seconds = icycles;

1656 if (t < sp->ats[0])

1657 newt += seconds;

1658 else

1659 newt -= seconds;

1660 if (newt < sp->ats[0] ||

1662 return -1;

1663

1665 before_isdst,

1666 boundary,

1667 after_gmtoff,

1668 after_isdst,

1669 tz);

1670 if (t < sp->ats[0])

1671 *boundary -= seconds;

1672 else

1673 *boundary += seconds;

1674 return result;

1675 }

1676

1678 {

1679

1681 ttisp = &sp->ttis[i];

1682 *before_gmtoff = ttisp->tt_utoff;

1683 *before_isdst = ttisp->tt_isdst;

1684 return 0;

1685 }

1686 if (t < sp->ats[0])

1687 {

1688

1690 *before_gmtoff = ttisp->tt_utoff;

1691 *before_isdst = ttisp->tt_isdst;

1692 *boundary = sp->ats[0];

1693

1695 ttisp = &sp->ttis[i];

1696 *after_gmtoff = ttisp->tt_utoff;

1697 *after_isdst = ttisp->tt_isdst;

1698 return 1;

1699 }

1700

1701 {

1702 int lo = 1;

1703 int hi = sp->timecnt - 1;

1704

1705 while (lo < hi)

1706 {

1707 int mid = (lo + hi) >> 1;

1708

1709 if (t < sp->ats[mid])

1710 hi = mid;

1711 else

1712 lo = mid + 1;

1713 }

1714 i = lo;

1715 }

1717 ttisp = &sp->ttis[j];

1718 *before_gmtoff = ttisp->tt_utoff;

1719 *before_isdst = ttisp->tt_isdst;

1720 *boundary = sp->ats[i];

1722 ttisp = &sp->ttis[j];

1723 *after_gmtoff = ttisp->tt_utoff;

1724 *after_isdst = ttisp->tt_isdst;

1725 return 1;

1726}

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)

pg_time_t ats[TZ_MAX_TIMES]

References state::ats, AVGSECSPERYEAR, state::defaulttype, state::goahead, state::goback, i, j, pg_next_dst_boundary(), pg_tz::state, state::timecnt, ttinfo::tt_isdst, ttinfo::tt_utoff, state::ttis, state::types, and YEARSPERREPEAT.

Referenced by DetermineTimeZoneOffsetInternal(), and pg_next_dst_boundary().

pg_strftime()

size_t pg_strftime ( char * s,
size_t maxsize,
const char * format,
const struct pg_tm * t
)

Definition at line 128 of file strftime.c.

129{

130 char *p;

131 int saved_errno = errno;

133

135 if (!p)

136 {

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 *format, const struct pg_tm *t, char *pt, const char *ptlim, enum warn *warnp)

References _fmt(), EOVERFLOW, format, and IN_NONE.

Referenced by AddFileToBackupManifest(), build_backup_content(), get_formatted_log_time(), get_formatted_start_time(), log_status_format(), logfile_getname(), str_time(), and timeofday().

pg_timezone_abbrev_is_known()

bool pg_timezone_abbrev_is_known ( const char * abbrev,
bool * isfixed,
long int * gmtoff,
int * isdst,
const pg_tz * tz
)

Definition at line 1861 of file localtime.c.

1866{

1867 bool result = false;

1868 const struct state *sp = &tz->state;

1869 const char *abbrs;

1870 int abbrind;

1871

1872

1873

1874

1875

1876 abbrs = sp->chars;

1877 abbrind = 0;

1878 while (abbrind < sp->charcnt)

1879 {

1880 if (strcmp(abbrev, abbrs + abbrind) == 0)

1881 break;

1882 while (abbrs[abbrind] != '\0')

1883 abbrind++;

1884 abbrind++;

1885 }

1886 if (abbrind >= sp->charcnt)

1887 return false;

1888

1889

1890

1891

1892 for (int i = 0; i < sp->typecnt; i++)

1893 {

1894 const struct ttinfo *ttisp = &sp->ttis[i];

1895

1897 {

1898 if (!result)

1899 {

1900

1901 *isfixed = true;

1904 result = true;

1905 }

1906 else

1907 {

1908

1909 if (*gmtoff != ttisp->tt_utoff ||

1911 {

1912 *isfixed = false;

1913 break;

1914 }

1915 }

1916 }

1917 }

1918

1919 return result;

1920}

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

Referenced by TimeZoneAbbrevIsKnown().

pg_timezone_initialize()

void pg_timezone_initialize ( void )

pg_tz_acceptable()

bool pg_tz_acceptable ( pg_tz * tz )

pg_tzenumerate_end()

pg_tzenumerate_next()

Definition at line 426 of file pgtz.c.

427{

428 while (dir->depth >= 0)

429 {

430 struct dirent *direntry;

432

434

435 if (!direntry)

436 {

437

441 continue;

442 }

443

444 if (direntry->d_name[0] == '.')

445 continue;

446

447 snprintf(fullname, sizeof(fullname), "%s/%s",

449

451 {

452

462 errmsg("could not open directory \"%s\": %m",

463 fullname)));

464

465

466 continue;

467 }

468

469

470

471

472

473

474

476 {

477

478 continue;

479 }

480

482 {

483

484 continue;

485 }

486

487

490

491

492 return &dir->tz;

493 }

494

495

496 return NULL;

497}

int errmsg_internal(const char *fmt,...)

int errcode_for_file_access(void)

int errmsg(const char *fmt,...)

#define ereport(elevel,...)

DIR * AllocateDir(const char *dirname)

struct dirent * ReadDir(DIR *dir, const char *dirname)

PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)

int tzload(const char *name, char *canonname, struct state *sp, bool doextend)

char * pstrdup(const char *in)

bool pg_tz_acceptable(pg_tz *tz)

size_t strlcpy(char *dst, const char *src, size_t siz)

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

pg_tzenumerate_start()

Definition at line 397 of file pgtz.c.

398{

401

402 ret->baselen = strlen(startdir) + 1;

404 ret->dirname[0] = startdir;

409 errmsg("could not open directory \"%s\": %m", startdir)));

410 return ret;

411}

void * palloc0(Size size)

static const char * pg_TZDIR(void)

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

pg_tzset()

pg_tz * pg_tzset ( const char * tzname )

Definition at line 234 of file pgtz.c.

235{

237 struct state tzstate;

240 char *p;

241

243 return NULL;

244

247 return NULL;

248

249

250

251

252

253

254

255 p = uppername;

256 while (*tzname)

257 *p++ = pg_toupper((unsigned char) *tzname++);

258 *p = '\0';

259

261 uppername,

263 NULL);

264 if (tzp)

265 {

266

267 return &tzp->tz;

268 }

269

270

271

272

273 if (strcmp(uppername, "GMT") == 0)

274 {

275 if (tzparse(uppername, &tzstate, true))

276 {

277

278 elog(ERROR, "could not initialize GMT time zone");

279 }

280

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

288 return NULL;

289 }

290

291 strcpy(canonname, uppername);

292 }

293

294

296 uppername,

298 NULL);

299

300

301 strcpy(tzp->tz.TZname, canonname);

302 memcpy(&tzp->tz.state, &tzstate, sizeof(tzstate));

303

304 return &tzp->tz;

305}

void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)

bool tzparse(const char *name, struct state *sp, bool lastditch)

static HTAB * timezone_cache

static bool init_timezone_hashtable(void)

unsigned char pg_toupper(unsigned char ch)

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

Referenced by check_log_timezone(), check_timezone(), DecodeDateTime(), DecodeTimeOnly(), DecodeTimezoneName(), FetchDynamicTimeZone(), pg_timezone_initialize(), and pg_tzset_offset().

pg_tzset_offset()

pg_tz * pg_tzset_offset ( long gmtoffset )

Definition at line 320 of file pgtz.c.

321{

322 long absoffset = (gmtoffset < 0) ? -gmtoffset : gmtoffset;

323 char offsetstr[64];

324 char tzname[128];

325

326 snprintf(offsetstr, sizeof(offsetstr),

329 if (absoffset != 0)

330 {

331 snprintf(offsetstr + strlen(offsetstr),

332 sizeof(offsetstr) - strlen(offsetstr),

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

348}

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

Referenced by check_timezone(), and DecodeTimezoneNameToTz().

log_timezone

session_timezone

Definition at line 28 of file pgtz.c.

Referenced by assign_timezone(), date2timestamptz_opt_overflow(), DecodeDateTime(), DecodeTimeOnly(), DecodeTimezoneAbbrev(), DecodeTimezoneAbbrevPrefix(), executeDateTimeMethod(), generate_series_timestamptz_internal(), GetCurrentTimeUsec(), pg_timezone_abbrevs_zone(), pg_timezone_initialize(), show_timezone(), time_timetz(), timeofday(), timestamp2timestamptz_opt_overflow(), timestamp2tm(), TimestampTimestampTzRequiresRewrite(), timestamptz_pl_interval_internal(), timestamptz_trunc(), timetz_at_local(), and to_timestamp().