PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgtz.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pgtz.c
4 * Timezone Library Integration Functions
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * src/timezone/pgtz.c
10 *
11 *-------------------------------------------------------------------------
12 */
13#include "postgres.h"
14
15#include <ctype.h>
16#include <fcntl.h>
17#include <time.h>
18
19#include "common/file_utils.h"
20#include "datatype/timestamp.h"
21#include "miscadmin.h"
22#include "pgtz.h"
23#include "storage/fd.h"
24#include "utils/hsearch.h"
25
26
27/* Current session timezone (controlled by TimeZone GUC) */
29
30/* Current log timezone (controlled by log_timezone GUC) */
32
33
34static bool scan_directory_ci(const char *dirname,
35 const char *fname, int fnamelen,
36 char *canonname, int canonnamelen);
37
38
39/*
40 * Return full pathname of timezone data directory
41 */
42static const char *
44{
45#ifndef SYSTEMTZDIR
46 /* normal case: timezone stuff is under our share dir */
47 static bool done_tzdir = false;
48 static char tzdir[MAXPGPATH];
49
50 if (done_tzdir)
51 return tzdir;
52
54 strlcpy(tzdir + strlen(tzdir), "/timezone", MAXPGPATH - strlen(tzdir));
55
56 done_tzdir = true;
57 return tzdir;
58#else
59 /* we're configured to use system's timezone database */
60 return SYSTEMTZDIR;
61#endif
62}
63
64
65/*
66 * Given a timezone name, open() the timezone data file. Return the
67 * file descriptor if successful, -1 if not.
68 *
69 * The input name is searched for case-insensitively (we assume that the
70 * timezone database does not contain case-equivalent names).
71 *
72 * If "canonname" is not NULL, then on success the canonical spelling of the
73 * given name is stored there (the buffer must be > TZ_STRLEN_MAX bytes!).
74 */
75int
76pg_open_tzfile(const char *name, char *canonname)
77{
78 const char *fname;
79 char fullname[MAXPGPATH];
80 int fullnamelen;
81 int orignamelen;
82
83 /* Initialize fullname with base name of tzdata directory */
84 strlcpy(fullname, pg_TZDIR(), sizeof(fullname));
86
87 if (fullnamelen + 1 + strlen(name) >= MAXPGPATH)
88 return -1; /* not gonna fit */
89
90 /*
91 * If the caller doesn't need the canonical spelling, first just try to
92 * open the name as-is. This can be expected to succeed if the given name
93 * is already case-correct, or if the filesystem is case-insensitive; and
94 * we don't need to distinguish those situations if we aren't tasked with
95 * reporting the canonical spelling.
96 */
97 if (canonname == NULL)
98 {
99 int result;
100
101 fullname[fullnamelen] = '/';
102 /* test above ensured this will fit: */
105 if (result >= 0)
106 return result;
107 /* If that didn't work, fall through to do it the hard way */
108 fullname[fullnamelen] = '\0';
109 }
110
111 /*
112 * Loop to split the given name into directory levels; for each level,
113 * search using scan_directory_ci().
114 */
115 fname = name;
116 for (;;)
117 {
118 const char *slashptr;
119 int fnamelen;
120
121 slashptr = strchr(fname, '/');
122 if (slashptr)
123 fnamelen = slashptr - fname;
124 else
125 fnamelen = strlen(fname);
127 fullname + fullnamelen + 1,
128 MAXPGPATH - fullnamelen - 1))
129 return -1;
130 fullname[fullnamelen++] = '/';
132 if (slashptr)
133 fname = slashptr + 1;
134 else
135 break;
136 }
137
138 if (canonname)
140
141 return open(fullname, O_RDONLY | PG_BINARY, 0);
142}
143
144
145/*
146 * Scan specified directory for a case-insensitive match to fname
147 * (of length fnamelen --- fname may not be null terminated!). If found,
148 * copy the actual filename into canonname and return true.
149 */
150static bool
151scan_directory_ci(const char *dirname, const char *fname, int fnamelen,
152 char *canonname, int canonnamelen)
153{
154 bool found = false;
155 DIR *dirdesc;
156 struct dirent *direntry;
157
158 dirdesc = AllocateDir(dirname);
159
160 while ((direntry = ReadDirExtended(dirdesc, dirname, LOG)) != NULL)
161 {
162 /*
163 * Ignore . and .., plus any other "hidden" files. This is a security
164 * measure to prevent access to files outside the timezone directory.
165 */
166 if (direntry->d_name[0] == '.')
167 continue;
168
169 if (strlen(direntry->d_name) == fnamelen &&
170 pg_strncasecmp(direntry->d_name, fname, fnamelen) == 0)
171 {
172 /* Found our match */
174 found = true;
175 break;
176 }
177 }
178
179 FreeDir(dirdesc);
180
181 return found;
182}
183
184
185/*
186 * We keep loaded timezones in a hashtable so we don't have to
187 * load and parse the TZ definition file every time one is selected.
188 * Because we want timezone names to be found case-insensitively,
189 * the hash key is the uppercased name of the zone.
190 */
191typedef struct
192{
193 /* tznameupper contains the all-upper-case name of the timezone */
194 char tznameupper[TZ_STRLEN_MAX + 1];
197
199
200
201static bool
203{
205
207 hash_ctl.entrysize = sizeof(pg_tz_cache);
208
209 timezone_cache = hash_create("Timezones",
210 4,
211 &hash_ctl,
213 if (!timezone_cache)
214 return false;
215
216 return true;
217}
218
219/*
220 * Load a timezone from file or from cache.
221 * Does not verify that the timezone is acceptable!
222 */
223pg_tz *
224pg_tzset(const char *tzname)
225{
226 pg_tz_cache *tzp;
227 struct state tzstate;
228 char uppername[TZ_STRLEN_MAX + 1];
229 char canonname[TZ_STRLEN_MAX + 1];
230 char *p;
231
233 return NULL; /* not going to fit */
234
235 if (!timezone_cache)
237 return NULL;
238
239 /*
240 * Upcase the given name to perform a case-insensitive hashtable search.
241 * (We could alternatively downcase it, but we prefer upcase so that we
242 * can get consistently upcased results from pg_tzload() in case the name
243 * is a POSIX-style timezone spec.)
244 */
245 p = uppername;
246 while (*tzname)
247 *p++ = pg_toupper((unsigned char) *tzname++);
248 *p = '\0';
249
251 uppername,
252 HASH_FIND,
253 NULL);
254 if (tzp)
255 {
256 /* Timezone found in cache, nothing more to do */
257 return &tzp->tz;
258 }
259
260 /*
261 * Let the IANA tzdb code interpret the time zone name.
262 */
264 {
265 if (strcmp(uppername, "GMT") == 0)
266 {
267 /* This really, really should not happen ... */
268 elog(ERROR, "could not initialize GMT time zone");
269 }
270 /* Unknown timezone. Fail our call instead of loading GMT! */
271 return NULL;
272 }
273
274 /* Save timezone in the cache */
276 uppername,
278 NULL);
279
280 /* hash_search already copied uppername into the hash key */
281 strcpy(tzp->tz.TZname, canonname);
282 memcpy(&tzp->tz.state, &tzstate, sizeof(tzstate));
283
284 return &tzp->tz;
285}
286
287/*
288 * Load a fixed-GMT-offset timezone.
289 * This is used for SQL-spec SET TIME ZONE INTERVAL 'foo' cases.
290 * It's otherwise equivalent to pg_tzset().
291 *
292 * The GMT offset is specified in seconds, positive values meaning west of
293 * Greenwich (ie, POSIX not ISO sign convention). However, we use ISO
294 * sign convention in the displayable abbreviation for the zone.
295 *
296 * Caution: this can fail (return NULL) if the specified offset is outside
297 * the range allowed by the zic library.
298 */
299pg_tz *
300pg_tzset_offset(long gmtoffset)
301{
302 long absoffset = (gmtoffset < 0) ? -gmtoffset : gmtoffset;
303 char offsetstr[64];
304 char tzname[128];
305
307 "%02ld", absoffset / SECS_PER_HOUR);
309 if (absoffset != 0)
310 {
312 sizeof(offsetstr) - strlen(offsetstr),
313 ":%02ld", absoffset / SECS_PER_MINUTE);
315 if (absoffset != 0)
317 sizeof(offsetstr) - strlen(offsetstr),
318 ":%02ld", absoffset);
319 }
320 if (gmtoffset > 0)
321 snprintf(tzname, sizeof(tzname), "<-%s>+%s",
323 else
324 snprintf(tzname, sizeof(tzname), "<+%s>-%s",
326
327 return pg_tzset(tzname);
328}
329
330
331/*
332 * Initialize timezone library
333 *
334 * This is called before GUC variable initialization begins. Its purpose
335 * is to ensure that log_timezone has a valid value before any logging GUC
336 * variables could become set to values that require elog.c to provide
337 * timestamps (e.g., log_line_prefix). We may as well initialize
338 * session_timezone to something valid, too.
339 */
340void
342{
343 /*
344 * We may not yet know where PGSHAREDIR is (in particular this is true in
345 * an EXEC_BACKEND subprocess). So use "GMT", which pg_tzset forces to be
346 * interpreted without reference to the filesystem. This corresponds to
347 * the bootstrap default for these variables in guc_parameters.dat,
348 * although in principle it could be different.
349 */
350 session_timezone = pg_tzset("GMT");
352}
353
354
355/*
356 * Functions to enumerate available timezones
357 *
358 * Note that pg_tzenumerate_next() will return a pointer into the pg_tzenum
359 * structure, so the data is only valid up to the next call.
360 *
361 * All data is allocated using palloc in the current context.
362 */
363#define MAX_TZDIR_DEPTH 10
364
373
374/* typedef pg_tzenum is declared in pgtime.h */
375
376pg_tzenum *
378{
380 char *startdir = pstrdup(pg_TZDIR());
381
382 ret->baselen = strlen(startdir) + 1;
383 ret->depth = 0;
384 ret->dirname[0] = startdir;
385 ret->dirdesc[0] = AllocateDir(startdir);
386 if (!ret->dirdesc[0])
389 errmsg("could not open directory \"%s\": %m", startdir)));
390 return ret;
391}
392
393void
395{
396 while (dir->depth >= 0)
397 {
398 FreeDir(dir->dirdesc[dir->depth]);
399 pfree(dir->dirname[dir->depth]);
400 dir->depth--;
401 }
402 pfree(dir);
403}
404
405pg_tz *
407{
408 while (dir->depth >= 0)
409 {
410 struct dirent *direntry;
411 char fullname[MAXPGPATH * 2];
412
413 direntry = ReadDir(dir->dirdesc[dir->depth], dir->dirname[dir->depth]);
414
415 if (!direntry)
416 {
417 /* End of this directory */
418 FreeDir(dir->dirdesc[dir->depth]);
419 pfree(dir->dirname[dir->depth]);
420 dir->depth--;
421 continue;
422 }
423
424 if (direntry->d_name[0] == '.')
425 continue;
426
427 snprintf(fullname, sizeof(fullname), "%s/%s",
428 dir->dirname[dir->depth], direntry->d_name);
429
431 {
432 /* Step into the subdirectory */
433 if (dir->depth >= MAX_TZDIR_DEPTH - 1)
435 (errmsg_internal("timezone directory stack overflow")));
436 dir->depth++;
437 dir->dirname[dir->depth] = pstrdup(fullname);
438 dir->dirdesc[dir->depth] = AllocateDir(fullname);
439 if (!dir->dirdesc[dir->depth])
442 errmsg("could not open directory \"%s\": %m",
443 fullname)));
444
445 /* Start over reading in the new directory */
446 continue;
447 }
448
449 /*
450 * Load this timezone using pg_tzload() not pg_tzset(), so we don't
451 * fill the cache. Also, don't ask for the canonical spelling: we
452 * already know it, and pg_open_tzfile's way of finding it out is
453 * pretty inefficient.
454 */
455 if (!pg_tzload(fullname + dir->baselen, NULL, &dir->tz.state))
456 {
457 /* Zone could not be loaded, ignore it */
458 continue;
459 }
460
461 if (!pg_tz_acceptable(&dir->tz))
462 {
463 /* Ignore leap-second zones */
464 continue;
465 }
466
467 /* OK, return the canonical zone name spelling. */
468 strlcpy(dir->tz.TZname, fullname + dir->baselen,
469 sizeof(dir->tz.TZname));
470
471 /* Timezone loaded OK. */
472 return &dir->tz;
473 }
474
475 /* Nothing more found */
476 return NULL;
477}
#define PG_BINARY
Definition c.h:1431
uint32 result
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define SECS_PER_HOUR
Definition timestamp.h:127
#define SECS_PER_MINUTE
Definition timestamp.h:128
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:889
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:360
int errcode_for_file_access(void)
Definition elog.c:898
#define LOG
Definition elog.h:32
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
int FreeDir(DIR *dir)
Definition fd.c:3009
struct dirent * ReadDirExtended(DIR *dir, const char *dirname, int elevel)
Definition fd.c:2972
DIR * AllocateDir(const char *dirname)
Definition fd.c:2891
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition fd.c:2957
#define palloc0_object(type)
Definition fe_memutils.h:90
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Definition file_utils.c:547
@ PGFILETYPE_DIR
Definition file_utils.h:23
char my_exec_path[MAXPGPATH]
Definition globals.c:83
#define HASH_STRINGS
Definition hsearch.h:91
@ HASH_FIND
Definition hsearch.h:108
@ HASH_ENTER
Definition hsearch.h:109
#define HASH_ELEM
Definition hsearch.h:90
bool pg_tzload(const char *name, char *canonname, struct state *sp)
Definition localtime.c:1590
char * pstrdup(const char *in)
Definition mcxt.c:1910
void pfree(void *pointer)
Definition mcxt.c:1619
static char * errmsg
#define MAXPGPATH
bool pg_tz_acceptable(pg_tz *tz)
Definition localtime.c:2028
#define TZ_STRLEN_MAX
Definition pgtime.h:54
pg_tz * pg_tzset_offset(long gmtoffset)
Definition pgtz.c:300
static HTAB * timezone_cache
Definition pgtz.c:198
pg_tz * pg_tzenumerate_next(pg_tzenum *dir)
Definition pgtz.c:406
pg_tz * log_timezone
Definition pgtz.c:31
void pg_timezone_initialize(void)
Definition pgtz.c:341
pg_tz * pg_tzset(const char *tzname)
Definition pgtz.c:224
#define MAX_TZDIR_DEPTH
Definition pgtz.c:363
static const char * pg_TZDIR(void)
Definition pgtz.c:43
int pg_open_tzfile(const char *name, char *canonname)
Definition pgtz.c:76
static bool scan_directory_ci(const char *dirname, const char *fname, int fnamelen, char *canonname, int canonnamelen)
Definition pgtz.c:151
pg_tz * session_timezone
Definition pgtz.c:28
static bool init_timezone_hashtable(void)
Definition pgtz.c:202
void pg_tzenumerate_end(pg_tzenum *dir)
Definition pgtz.c:394
pg_tzenum * pg_tzenumerate_start(void)
Definition pgtz.c:377
void get_share_path(const char *my_exec_path, char *ret_path)
Definition path.c:919
unsigned char pg_toupper(unsigned char ch)
#define snprintf
Definition port.h:261
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
static int fb(int x)
Definition dirent.c:26
Size keysize
Definition hsearch.h:69
pg_tz tz
Definition pgtz.c:195
Definition pgtz.h:105
char TZname[TZ_STRLEN_MAX+1]
Definition pgtz.h:107
struct state state
Definition pgtz.h:108
int baselen
Definition pgtz.c:367
char * dirname[MAX_TZDIR_DEPTH]
Definition pgtz.c:370
struct pg_tz tz
Definition pgtz.c:371
int depth
Definition pgtz.c:368
DIR * dirdesc[MAX_TZDIR_DEPTH]
Definition pgtz.c:369
const char * name