Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * findtimezone.c
4 : : * Functions for determining the default timezone to use.
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : *
8 : : * IDENTIFICATION
9 : : * src/bin/initdb/findtimezone.c
10 : : *
11 : : *-------------------------------------------------------------------------
12 : : */
13 : : #include "postgres_fe.h"
14 : :
15 : : #include <fcntl.h>
16 : : #include <sys/stat.h>
17 : : #include <time.h>
18 : : #include <unistd.h>
19 : :
20 : : #include "pgtz.h"
21 : :
22 : : /* Ideally this would be in a .h file, but it hardly seems worth the trouble */
23 : : extern const char *select_default_timezone(const char *share_path);
24 : :
25 : :
26 : : #ifndef SYSTEMTZDIR
27 : : static char tzdirpath[MAXPGPATH];
28 : : #endif
29 : :
30 : :
31 : : /*
32 : : * Return full pathname of timezone data directory
33 : : *
34 : : * In this file, tzdirpath is assumed to be set up by select_default_timezone.
35 : : */
36 : : static const char *
37 : 232 : pg_TZDIR(void)
38 : : {
39 : : #ifndef SYSTEMTZDIR
40 : : /* normal case: timezone stuff is under our share dir */
41 : 232 : return tzdirpath;
42 : : #else
43 : : /* we're configured to use system's timezone database */
44 : : return SYSTEMTZDIR;
45 : : #endif
46 : : }
47 : :
48 : :
49 : : /*
50 : : * Given a timezone name, open() the timezone data file. Return the
51 : : * file descriptor if successful, -1 if not.
52 : : *
53 : : * This is simpler than the backend function of the same name because
54 : : * we assume that the input string has the correct case already, so there
55 : : * is no need for case-folding. (This is obviously true if we got the file
56 : : * name from the filesystem to start with. The only other place it can come
57 : : * from is the environment variable TZ, and there seems no need to allow
58 : : * case variation in that; other programs aren't likely to.)
59 : : *
60 : : * If "canonname" is not NULL, then on success the canonical spelling of the
61 : : * given name is stored there (the buffer must be > TZ_STRLEN_MAX bytes!).
62 : : * This is redundant but kept for compatibility with the backend code.
63 : : */
64 : : int
65 : 232 : pg_open_tzfile(const char *name, char *canonname)
66 : : {
67 : : char fullname[MAXPGPATH];
68 : :
69 [ - + ]: 232 : if (canonname)
70 : 0 : strlcpy(canonname, name, TZ_STRLEN_MAX + 1);
71 : :
72 : 232 : strlcpy(fullname, pg_TZDIR(), sizeof(fullname));
73 [ - + ]: 232 : if (strlen(fullname) + 1 + strlen(name) >= MAXPGPATH)
74 : 0 : return -1; /* not gonna fit */
75 : 232 : strcat(fullname, "/");
76 : 232 : strcat(fullname, name);
77 : :
78 : 232 : return open(fullname, O_RDONLY | PG_BINARY, 0);
79 : : }
80 : :
81 : :
82 : :
83 : : /*
84 : : * Load a timezone definition.
85 : : * Does not verify that the timezone is acceptable!
86 : : *
87 : : * This corresponds to the backend's pg_tzset(), except that we only support
88 : : * one loaded timezone at a time.
89 : : */
90 : : static pg_tz *
91 : 232 : pg_load_tz(const char *name)
92 : : {
93 : : static pg_tz tz;
94 : :
95 [ - + ]: 232 : if (strlen(name) > TZ_STRLEN_MAX)
96 : 0 : return NULL; /* not going to fit */
97 : :
98 : : /*
99 : : * Let the IANA tzdb code interpret the time zone name.
100 : : */
101 [ + + ]: 232 : if (!pg_tzload(name, NULL, &tz.state))
102 : 116 : return NULL; /* unknown timezone */
103 : :
104 : 116 : strcpy(tz.TZname, name);
105 : :
106 : 116 : return &tz;
107 : : }
108 : :
109 : :
110 : : /*
111 : : * The following block of code attempts to determine which timezone in our
112 : : * timezone database is the best match for the active system timezone.
113 : : *
114 : : * On most systems, we rely on trying to match the observable behavior of
115 : : * the C library's localtime() function. The database zone that matches
116 : : * furthest into the past is the one to use. Often there will be several
117 : : * zones with identical rankings (since the IANA database assigns multiple
118 : : * names to many zones). We break ties by first checking for "preferred"
119 : : * names (such as "UTC"), and then arbitrarily by preferring shorter, then
120 : : * alphabetically earlier zone names. (If we did not explicitly prefer
121 : : * "UTC", we would get the alias name "UCT" instead due to alphabetic
122 : : * ordering.)
123 : : *
124 : : * Many modern systems use the IANA database, so if we can determine the
125 : : * system's idea of which zone it is using and its behavior matches our zone
126 : : * of the same name, we can skip the rather-expensive search through all the
127 : : * zones in our database. This short-circuit path also ensures that we spell
128 : : * the zone name the same way the system setting does, even in the presence
129 : : * of multiple aliases for the same zone.
130 : : *
131 : : * Win32's native knowledge about timezones appears to be too incomplete
132 : : * and too different from the IANA database for the above matching strategy
133 : : * to be of any use. But there is just a limited number of timezones
134 : : * available, so we can rely on a handmade mapping table instead.
135 : : */
136 : :
137 : : #ifndef WIN32
138 : :
139 : : #define T_DAY ((time_t) (60*60*24))
140 : : #define T_WEEK ((time_t) (60*60*24*7))
141 : : #define T_MONTH ((time_t) (60*60*24*31))
142 : :
143 : : #define MAX_TEST_TIMES (52*100) /* 100 years */
144 : :
145 : : struct tztry
146 : : {
147 : : int n_test_times;
148 : : time_t test_times[MAX_TEST_TIMES];
149 : : };
150 : :
151 : : static bool check_system_link_file(const char *linkname, struct tztry *tt,
152 : : char *bestzonename);
153 : : static void scan_available_timezones(char *tzdir, char *tzdirsub,
154 : : struct tztry *tt,
155 : : int *bestscore, char *bestzonename);
156 : :
157 : :
158 : : /*
159 : : * Get GMT offset from a system struct tm
160 : : */
161 : : static int
162 : 0 : get_timezone_offset(struct tm *tm)
163 : : {
164 : : #if defined(HAVE_STRUCT_TM_TM_ZONE)
165 : 0 : return tm->tm_gmtoff;
166 : : #elif defined(HAVE_INT_TIMEZONE)
167 : : return -TIMEZONE_GLOBAL;
168 : : #else
169 : : #error No way to determine TZ? Can this happen?
170 : : #endif
171 : : }
172 : :
173 : : /*
174 : : * Convenience subroutine to convert y/m/d to time_t (NOT pg_time_t)
175 : : */
176 : : static time_t
177 : 116 : build_time_t(int year, int month, int day)
178 : : {
179 : : struct tm tm;
180 : :
181 : 116 : memset(&tm, 0, sizeof(tm));
182 : 116 : tm.tm_mday = day;
183 : 116 : tm.tm_mon = month - 1;
184 : 116 : tm.tm_year = year - 1900;
185 : 116 : tm.tm_isdst = -1;
186 : :
187 : 116 : return mktime(&tm);
188 : : }
189 : :
190 : : /*
191 : : * Does a system tm value match one we computed ourselves?
192 : : */
193 : : static bool
194 : 301600 : compare_tm(struct tm *s, struct pg_tm *p)
195 : : {
196 [ + - ]: 301600 : if (s->tm_sec != p->tm_sec ||
197 [ + - ]: 301600 : s->tm_min != p->tm_min ||
198 [ + - ]: 301600 : s->tm_hour != p->tm_hour ||
199 [ + - ]: 301600 : s->tm_mday != p->tm_mday ||
200 [ + - ]: 301600 : s->tm_mon != p->tm_mon ||
201 [ + - ]: 301600 : s->tm_year != p->tm_year ||
202 [ + - ]: 301600 : s->tm_wday != p->tm_wday ||
203 [ + - ]: 301600 : s->tm_yday != p->tm_yday ||
204 [ - + ]: 301600 : s->tm_isdst != p->tm_isdst)
205 : 0 : return false;
206 : 301600 : return true;
207 : : }
208 : :
209 : : /*
210 : : * See how well a specific timezone setting matches the system behavior
211 : : *
212 : : * We score a timezone setting according to the number of test times it
213 : : * matches. (The test times are ordered later-to-earlier, but this routine
214 : : * doesn't actually know that; it just scans until the first non-match.)
215 : : *
216 : : * We return -1 for a completely unusable setting; this is worse than the
217 : : * score of zero for a setting that works but matches not even the first
218 : : * test time.
219 : : */
220 : : static int
221 : 174 : score_timezone(const char *tzname, struct tztry *tt)
222 : : {
223 : : int i;
224 : : pg_time_t pgtt;
225 : : struct tm *systm;
226 : : struct pg_tm *pgtm;
227 : : char cbuf[TZ_STRLEN_MAX + 1];
228 : : pg_tz *tz;
229 : :
230 : : /* Load timezone definition */
231 : 174 : tz = pg_load_tz(tzname);
232 [ + + ]: 174 : if (!tz)
233 : 116 : return -1; /* unrecognized zone name */
234 : :
235 : : /* Reject if leap seconds involved */
236 [ - + ]: 58 : if (!pg_tz_acceptable(tz))
237 : : {
238 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
239 : : fprintf(stderr, "Reject TZ \"%s\": uses leap seconds\n", tzname);
240 : : #endif
241 : 0 : return -1;
242 : : }
243 : :
244 : : /* Check for match at all the test times */
245 [ + + ]: 301658 : for (i = 0; i < tt->n_test_times; i++)
246 : : {
247 : 301600 : pgtt = (pg_time_t) (tt->test_times[i]);
248 : 301600 : pgtm = pg_localtime(&pgtt, tz);
249 [ - + ]: 301600 : if (!pgtm)
250 : 0 : return -1; /* probably shouldn't happen */
251 : 301600 : systm = localtime(&(tt->test_times[i]));
252 [ - + ]: 301600 : if (!systm)
253 : : {
254 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
255 : : fprintf(stderr, "TZ \"%s\" scores %d: at %ld %04d-%02d-%02d %02d:%02d:%02d %s, system had no data\n",
256 : : tzname, i, (long) pgtt,
257 : : pgtm->tm_year + 1900, pgtm->tm_mon + 1, pgtm->tm_mday,
258 : : pgtm->tm_hour, pgtm->tm_min, pgtm->tm_sec,
259 : : pgtm->tm_isdst ? "dst" : "std");
260 : : #endif
261 : 0 : return i;
262 : : }
263 [ - + ]: 301600 : if (!compare_tm(systm, pgtm))
264 : : {
265 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
266 : : fprintf(stderr, "TZ \"%s\" scores %d: at %ld %04d-%02d-%02d %02d:%02d:%02d %s versus %04d-%02d-%02d %02d:%02d:%02d %s\n",
267 : : tzname, i, (long) pgtt,
268 : : pgtm->tm_year + 1900, pgtm->tm_mon + 1, pgtm->tm_mday,
269 : : pgtm->tm_hour, pgtm->tm_min, pgtm->tm_sec,
270 : : pgtm->tm_isdst ? "dst" : "std",
271 : : systm->tm_year + 1900, systm->tm_mon + 1, systm->tm_mday,
272 : : systm->tm_hour, systm->tm_min, systm->tm_sec,
273 : : systm->tm_isdst ? "dst" : "std");
274 : : #endif
275 : 0 : return i;
276 : : }
277 [ + - ]: 301600 : if (systm->tm_isdst >= 0)
278 : : {
279 : : /* Check match of zone names, too */
280 [ - + ]: 301600 : if (pgtm->tm_zone == NULL)
281 : 0 : return -1; /* probably shouldn't happen */
282 : 301600 : memset(cbuf, 0, sizeof(cbuf));
283 : 301600 : strftime(cbuf, sizeof(cbuf) - 1, "%Z", systm); /* zone abbr */
284 [ - + ]: 301600 : if (strcmp(cbuf, pgtm->tm_zone) != 0)
285 : : {
286 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
287 : : fprintf(stderr, "TZ \"%s\" scores %d: at %ld \"%s\" versus \"%s\"\n",
288 : : tzname, i, (long) pgtt,
289 : : pgtm->tm_zone, cbuf);
290 : : #endif
291 : 0 : return i;
292 : : }
293 : : }
294 : : }
295 : :
296 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
297 : : fprintf(stderr, "TZ \"%s\" gets max score %d\n", tzname, i);
298 : : #endif
299 : :
300 : 58 : return i;
301 : : }
302 : :
303 : : /*
304 : : * Test whether given zone name is a perfect match to localtime() behavior
305 : : */
306 : : static bool
307 : 174 : perfect_timezone_match(const char *tzname, struct tztry *tt)
308 : : {
309 : 174 : return (score_timezone(tzname, tt) == tt->n_test_times);
310 : : }
311 : :
312 : :
313 : : /*
314 : : * Try to identify a timezone name (in our terminology) that best matches the
315 : : * observed behavior of the system localtime() function.
316 : : */
317 : : static const char *
318 : 58 : identify_system_timezone(void)
319 : : {
320 : : static char resultbuf[TZ_STRLEN_MAX + 1];
321 : : time_t tnow;
322 : : time_t t;
323 : : struct tztry tt;
324 : : struct tm *tm;
325 : : int thisyear;
326 : : int bestscore;
327 : : char tmptzdir[MAXPGPATH];
328 : : int std_ofs;
329 : : char std_zone_name[TZ_STRLEN_MAX + 1],
330 : : dst_zone_name[TZ_STRLEN_MAX + 1];
331 : : char cbuf[TZ_STRLEN_MAX + 1];
332 : :
333 : : /* Initialize OS timezone library */
334 : 58 : tzset();
335 : :
336 : : /*
337 : : * Set up the list of dates to be probed to see how well our timezone
338 : : * matches the system zone. We first probe January and July of the
339 : : * current year; this serves to quickly eliminate the vast majority of the
340 : : * TZ database entries. If those dates match, we probe every week for 100
341 : : * years backwards from the current July. (Weekly resolution is good
342 : : * enough to identify DST transition rules, since everybody switches on
343 : : * Sundays.) This is sufficient to cover most of the Unix time_t range,
344 : : * and we don't want to look further than that since many systems won't
345 : : * have sane TZ behavior further back anyway. The further back the zone
346 : : * matches, the better we score it. This may seem like a rather random
347 : : * way of doing things, but experience has shown that system-supplied
348 : : * timezone definitions are likely to have DST behavior that is right for
349 : : * the recent past and not so accurate further back. Scoring in this way
350 : : * allows us to recognize zones that have some commonality with the IANA
351 : : * database, without insisting on exact match. (Note: we probe Thursdays,
352 : : * not Sundays, to avoid triggering DST-transition bugs in localtime
353 : : * itself.)
354 : : */
355 : 58 : tnow = time(NULL);
356 : 58 : tm = localtime(&tnow);
357 [ - + ]: 58 : if (!tm)
358 : 0 : return NULL; /* give up if localtime is broken... */
359 : 58 : thisyear = tm->tm_year + 1900;
360 : :
361 : 58 : t = build_time_t(thisyear, 1, 15);
362 : :
363 : : /*
364 : : * Round back to GMT midnight Thursday. This depends on the knowledge
365 : : * that the time_t origin is Thu Jan 01 1970. (With a different origin
366 : : * we'd be probing some other day of the week, but it wouldn't matter
367 : : * anyway unless localtime() had DST-transition bugs.)
368 : : */
369 : 58 : t -= (t % T_WEEK);
370 : :
371 : 58 : tt.n_test_times = 0;
372 : 58 : tt.test_times[tt.n_test_times++] = t;
373 : :
374 : 58 : t = build_time_t(thisyear, 7, 15);
375 : 58 : t -= (t % T_WEEK);
376 : :
377 : 58 : tt.test_times[tt.n_test_times++] = t;
378 : :
379 [ + + ]: 301542 : while (tt.n_test_times < MAX_TEST_TIMES)
380 : : {
381 : 301484 : t -= T_WEEK;
382 : 301484 : tt.test_times[tt.n_test_times++] = t;
383 : : }
384 : :
385 : : /*
386 : : * Try to avoid the brute-force search by seeing if we can recognize the
387 : : * system's timezone setting directly.
388 : : *
389 : : * Currently we just check /etc/localtime; there are other conventions for
390 : : * this, but that seems to be the only one used on enough platforms to be
391 : : * worth troubling over.
392 : : */
393 [ + - ]: 58 : if (check_system_link_file("/etc/localtime", &tt, resultbuf))
394 : 58 : return resultbuf;
395 : :
396 : : /* No luck, so search for the best-matching timezone file */
397 : 0 : strlcpy(tmptzdir, pg_TZDIR(), sizeof(tmptzdir));
398 : 0 : bestscore = -1;
399 : 0 : resultbuf[0] = '\0';
400 : 0 : scan_available_timezones(tmptzdir, tmptzdir + strlen(tmptzdir) + 1,
401 : : &tt,
402 : : &bestscore, resultbuf);
403 [ # # ]: 0 : if (bestscore > 0)
404 : : {
405 : : /* Ignore IANA's rather silly "Factory" zone; use GMT instead */
406 [ # # ]: 0 : if (strcmp(resultbuf, "Factory") == 0)
407 : 0 : return NULL;
408 : 0 : return resultbuf;
409 : : }
410 : :
411 : : /*
412 : : * Couldn't find a match in the database, so next we try constructed zone
413 : : * names (like "PST8PDT").
414 : : *
415 : : * First we need to determine the names of the local standard and daylight
416 : : * zones. The idea here is to scan forward from today until we have seen
417 : : * both zones, if both are in use.
418 : : */
419 : 0 : memset(std_zone_name, 0, sizeof(std_zone_name));
420 : 0 : memset(dst_zone_name, 0, sizeof(dst_zone_name));
421 : 0 : std_ofs = 0;
422 : :
423 : 0 : tnow = time(NULL);
424 : :
425 : : /*
426 : : * Round back to a GMT midnight so results don't depend on local time of
427 : : * day
428 : : */
429 : 0 : tnow -= (tnow % T_DAY);
430 : :
431 : : /*
432 : : * We have to look a little further ahead than one year, in case today is
433 : : * just past a DST boundary that falls earlier in the year than the next
434 : : * similar boundary. Arbitrarily scan up to 14 months.
435 : : */
436 [ # # ]: 0 : for (t = tnow; t <= tnow + T_MONTH * 14; t += T_MONTH)
437 : : {
438 : 0 : tm = localtime(&t);
439 [ # # ]: 0 : if (!tm)
440 : 0 : continue;
441 [ # # ]: 0 : if (tm->tm_isdst < 0)
442 : 0 : continue;
443 [ # # # # ]: 0 : if (tm->tm_isdst == 0 && std_zone_name[0] == '\0')
444 : : {
445 : : /* found STD zone */
446 : 0 : memset(cbuf, 0, sizeof(cbuf));
447 : 0 : strftime(cbuf, sizeof(cbuf) - 1, "%Z", tm); /* zone abbr */
448 : 0 : strcpy(std_zone_name, cbuf);
449 : 0 : std_ofs = get_timezone_offset(tm);
450 : : }
451 [ # # # # ]: 0 : if (tm->tm_isdst > 0 && dst_zone_name[0] == '\0')
452 : : {
453 : : /* found DST zone */
454 : 0 : memset(cbuf, 0, sizeof(cbuf));
455 : 0 : strftime(cbuf, sizeof(cbuf) - 1, "%Z", tm); /* zone abbr */
456 : 0 : strcpy(dst_zone_name, cbuf);
457 : : }
458 : : /* Done if found both */
459 [ # # # # ]: 0 : if (std_zone_name[0] && dst_zone_name[0])
460 : 0 : break;
461 : : }
462 : :
463 : : /* We should have found a STD zone name by now... */
464 [ # # ]: 0 : if (std_zone_name[0] == '\0')
465 : : {
466 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
467 : : fprintf(stderr, "could not determine system time zone\n");
468 : : #endif
469 : 0 : return NULL; /* go to GMT */
470 : : }
471 : :
472 : : /* If we found DST then try STD<ofs>DST */
473 [ # # ]: 0 : if (dst_zone_name[0] != '\0')
474 : : {
475 : 0 : snprintf(resultbuf, sizeof(resultbuf), "%s%d%s",
476 : 0 : std_zone_name, -std_ofs / 3600, dst_zone_name);
477 [ # # ]: 0 : if (score_timezone(resultbuf, &tt) > 0)
478 : 0 : return resultbuf;
479 : : }
480 : :
481 : : /* Try just the STD timezone (works for GMT at least) */
482 : 0 : strcpy(resultbuf, std_zone_name);
483 [ # # ]: 0 : if (score_timezone(resultbuf, &tt) > 0)
484 : 0 : return resultbuf;
485 : :
486 : : /* Try STD<ofs> */
487 : 0 : snprintf(resultbuf, sizeof(resultbuf), "%s%d",
488 : 0 : std_zone_name, -std_ofs / 3600);
489 [ # # ]: 0 : if (score_timezone(resultbuf, &tt) > 0)
490 : 0 : return resultbuf;
491 : :
492 : : /*
493 : : * Did not find the timezone. Fallback to use a GMT zone. Note that the
494 : : * IANA timezone database names the GMT-offset zones in POSIX style: plus
495 : : * is west of Greenwich. It's unfortunate that this is opposite of SQL
496 : : * conventions. Should we therefore change the names? Probably not...
497 : : */
498 : 0 : snprintf(resultbuf, sizeof(resultbuf), "Etc/GMT%s%d",
499 [ # # ]: 0 : (-std_ofs > 0) ? "+" : "", -std_ofs / 3600);
500 : :
501 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
502 : : fprintf(stderr, "could not recognize system time zone, using \"%s\"\n",
503 : : resultbuf);
504 : : #endif
505 : 0 : return resultbuf;
506 : : }
507 : :
508 : : /*
509 : : * Examine a system-provided symlink file to see if it tells us the timezone.
510 : : *
511 : : * Unfortunately, there is little standardization of how the system default
512 : : * timezone is determined in the absence of a TZ environment setting.
513 : : * But a common strategy is to create a symlink at a well-known place.
514 : : * If "linkname" identifies a readable symlink, and the tail of its contents
515 : : * matches a zone name we know, and the actual behavior of localtime() agrees
516 : : * with what we think that zone means, then we may use that zone name.
517 : : *
518 : : * We insist on a perfect behavioral match, which might not happen if the
519 : : * system has a different IANA database version than we do; but in that case
520 : : * it seems best to fall back to the brute-force search.
521 : : *
522 : : * linkname is the symlink file location to probe.
523 : : *
524 : : * tt tells about the system timezone behavior we need to match.
525 : : *
526 : : * If we successfully identify a zone name, store it in *bestzonename and
527 : : * return true; else return false. bestzonename must be a buffer of length
528 : : * TZ_STRLEN_MAX + 1.
529 : : */
530 : : static bool
531 : 58 : check_system_link_file(const char *linkname, struct tztry *tt,
532 : : char *bestzonename)
533 : : {
534 : : #ifdef HAVE_READLINK
535 : : char link_target[MAXPGPATH];
536 : : ssize_t len;
537 : : const char *cur_name;
538 : :
539 : : /*
540 : : * Try to read the symlink. If not there, not a symlink, etc etc, just
541 : : * quietly fail; the precise reason needn't concern us.
542 : : */
543 : 58 : len = readlink(linkname, link_target, sizeof(link_target));
544 [ + - - + ]: 58 : if (len < 0 || len >= sizeof(link_target))
545 : 0 : return false;
546 : 58 : link_target[len] = '\0';
547 : :
548 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
549 : : fprintf(stderr, "symbolic link \"%s\" contains \"%s\"\n",
550 : : linkname, link_target);
551 : : #endif
552 : :
553 : : /*
554 : : * The symlink is probably of the form "/path/to/zones/zone/name", or
555 : : * possibly it is a relative path. Nobody puts their zone DB directly in
556 : : * the root directory, so we can definitely skip the first component; but
557 : : * after that it's trial-and-error to identify which path component begins
558 : : * the zone name.
559 : : */
560 : 58 : cur_name = link_target;
561 [ + - ]: 174 : while (*cur_name)
562 : : {
563 : : /* Advance to next segment of path */
564 : 174 : cur_name = strchr(cur_name + 1, '/');
565 [ - + ]: 174 : if (cur_name == NULL)
566 : 0 : break;
567 : : /* If there are consecutive slashes, skip all, as the kernel would */
568 : : do
569 : : {
570 : 174 : cur_name++;
571 [ - + ]: 174 : } while (*cur_name == '/');
572 : :
573 : : /*
574 : : * Test remainder of path to see if it is a matching zone name.
575 : : * Relative paths might contain ".."; we needn't bother testing if the
576 : : * first component is that. Also defend against overlength names.
577 : : */
578 [ + - + - ]: 174 : if (*cur_name && *cur_name != '.' &&
579 [ + - + + ]: 348 : strlen(cur_name) <= TZ_STRLEN_MAX &&
580 : 174 : perfect_timezone_match(cur_name, tt))
581 : : {
582 : : /* Success! */
583 : 58 : strcpy(bestzonename, cur_name);
584 : 58 : return true;
585 : : }
586 : : }
587 : :
588 : : /* Couldn't extract a matching zone name */
589 : 0 : return false;
590 : : #else
591 : : /* No symlinks? Forget it */
592 : : return false;
593 : : #endif
594 : : }
595 : :
596 : : /*
597 : : * Given a timezone name, determine whether it should be preferred over other
598 : : * names which are equally good matches. The output is arbitrary but we will
599 : : * use 0 for "neutral" default preference; larger values are more preferred.
600 : : */
601 : : static int
602 : 0 : zone_name_pref(const char *zonename)
603 : : {
604 : : /*
605 : : * Prefer UTC over alternatives such as UCT. Also prefer Etc/UTC over
606 : : * Etc/UCT; but UTC is preferred to Etc/UTC.
607 : : */
608 [ # # ]: 0 : if (strcmp(zonename, "UTC") == 0)
609 : 0 : return 50;
610 [ # # ]: 0 : if (strcmp(zonename, "Etc/UTC") == 0)
611 : 0 : return 40;
612 : :
613 : : /*
614 : : * We don't want to pick "localtime" or "posixrules", unless we can find
615 : : * no other name for the prevailing zone. Those aren't real zone names.
616 : : */
617 [ # # ]: 0 : if (strcmp(zonename, "localtime") == 0 ||
618 [ # # ]: 0 : strcmp(zonename, "posixrules") == 0)
619 : 0 : return -50;
620 : :
621 : 0 : return 0;
622 : : }
623 : :
624 : : /*
625 : : * Recursively scan the timezone database looking for the best match to
626 : : * the system timezone behavior.
627 : : *
628 : : * tzdir points to a buffer of size MAXPGPATH. On entry, it holds the
629 : : * pathname of a directory containing TZ files. We internally modify it
630 : : * to hold pathnames of sub-directories and files, but must restore it
631 : : * to its original contents before exit.
632 : : *
633 : : * tzdirsub points to the part of tzdir that represents the subfile name
634 : : * (ie, tzdir + the original directory name length, plus one for the
635 : : * first added '/').
636 : : *
637 : : * tt tells about the system timezone behavior we need to match.
638 : : *
639 : : * *bestscore and *bestzonename on entry hold the best score found so far
640 : : * and the name of the best zone. We overwrite them if we find a better
641 : : * score. bestzonename must be a buffer of length TZ_STRLEN_MAX + 1.
642 : : */
643 : : static void
644 : 0 : scan_available_timezones(char *tzdir, char *tzdirsub, struct tztry *tt,
645 : : int *bestscore, char *bestzonename)
646 : : {
647 : 0 : int tzdir_orig_len = strlen(tzdir);
648 : : char **names;
649 : : char **namep;
650 : :
651 : 0 : names = pgfnames(tzdir);
652 [ # # ]: 0 : if (!names)
653 : 0 : return;
654 : :
655 [ # # ]: 0 : for (namep = names; *namep; namep++)
656 : : {
657 : 0 : char *name = *namep;
658 : : struct stat statbuf;
659 : :
660 : : /* Ignore . and .., plus any other "hidden" files */
661 [ # # ]: 0 : if (name[0] == '.')
662 : 0 : continue;
663 : :
664 : 0 : snprintf(tzdir + tzdir_orig_len, MAXPGPATH - tzdir_orig_len,
665 : : "/%s", name);
666 : :
667 [ # # ]: 0 : if (stat(tzdir, &statbuf) != 0)
668 : : {
669 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
670 : : fprintf(stderr, "could not stat \"%s\": %m\n",
671 : : tzdir);
672 : : #endif
673 : 0 : tzdir[tzdir_orig_len] = '\0';
674 : 0 : continue;
675 : : }
676 : :
677 [ # # ]: 0 : if (S_ISDIR(statbuf.st_mode))
678 : : {
679 : : /* Recurse into subdirectory */
680 : 0 : scan_available_timezones(tzdir, tzdirsub, tt,
681 : : bestscore, bestzonename);
682 : : }
683 : : else
684 : : {
685 : : /* Load and test this file */
686 : 0 : int score = score_timezone(tzdirsub, tt);
687 : :
688 [ # # ]: 0 : if (score > *bestscore)
689 : : {
690 : 0 : *bestscore = score;
691 : 0 : strlcpy(bestzonename, tzdirsub, TZ_STRLEN_MAX + 1);
692 : : }
693 [ # # ]: 0 : else if (score == *bestscore)
694 : : {
695 : : /* Consider how to break a tie */
696 : 0 : int namepref = (zone_name_pref(tzdirsub) -
697 : 0 : zone_name_pref(bestzonename));
698 : :
699 [ # # # # ]: 0 : if (namepref > 0 ||
700 : 0 : (namepref == 0 &&
701 [ # # ]: 0 : (strlen(tzdirsub) < strlen(bestzonename) ||
702 [ # # ]: 0 : (strlen(tzdirsub) == strlen(bestzonename) &&
703 [ # # ]: 0 : strcmp(tzdirsub, bestzonename) < 0))))
704 : 0 : strlcpy(bestzonename, tzdirsub, TZ_STRLEN_MAX + 1);
705 : : }
706 : : }
707 : :
708 : : /* Restore tzdir */
709 : 0 : tzdir[tzdir_orig_len] = '\0';
710 : : }
711 : :
712 : 0 : pgfnames_cleanup(names);
713 : : }
714 : : #else /* WIN32 */
715 : :
716 : : static const struct
717 : : {
718 : : const char *stdname; /* Windows name of standard timezone */
719 : : const char *dstname; /* Windows name of daylight timezone */
720 : : const char *pgtzname; /* Name of pgsql timezone to map to */
721 : : } win32_tzmap[] =
722 : :
723 : : {
724 : : /*
725 : : * This list was built from the contents of the registry at
726 : : * HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time
727 : : * Zones on Windows 7, Windows 10, and Windows Server 2019. Some recent
728 : : * additions have been made by comparing to the CLDR project's
729 : : * windowsZones.xml file.
730 : : *
731 : : * The zones have been matched to IANA timezones based on CLDR's mapping
732 : : * for "territory 001".
733 : : */
734 : : {
735 : : /* (UTC+04:30) Kabul */
736 : : "Afghanistan Standard Time", "Afghanistan Daylight Time",
737 : : "Asia/Kabul"
738 : : },
739 : : {
740 : : /* (UTC-09:00) Alaska */
741 : : "Alaskan Standard Time", "Alaskan Daylight Time",
742 : : "America/Anchorage"
743 : : },
744 : : {
745 : : /* (UTC-10:00) Aleutian Islands */
746 : : "Aleutian Standard Time", "Aleutian Daylight Time",
747 : : "America/Adak"
748 : : },
749 : : {
750 : : /* (UTC+07:00) Barnaul, Gorno-Altaysk */
751 : : "Altai Standard Time", "Altai Daylight Time",
752 : : "Asia/Barnaul"
753 : : },
754 : : {
755 : : /* (UTC+03:00) Kuwait, Riyadh */
756 : : "Arab Standard Time", "Arab Daylight Time",
757 : : "Asia/Riyadh"
758 : : },
759 : : {
760 : : /* (UTC+04:00) Abu Dhabi, Muscat */
761 : : "Arabian Standard Time", "Arabian Daylight Time",
762 : : "Asia/Dubai"
763 : : },
764 : : {
765 : : /* (UTC+03:00) Baghdad */
766 : : "Arabic Standard Time", "Arabic Daylight Time",
767 : : "Asia/Baghdad"
768 : : },
769 : : {
770 : : /* (UTC-03:00) City of Buenos Aires */
771 : : "Argentina Standard Time", "Argentina Daylight Time",
772 : : "America/Buenos_Aires"
773 : : },
774 : : {
775 : : /* (UTC+04:00) Baku, Tbilisi, Yerevan */
776 : : "Armenian Standard Time", "Armenian Daylight Time",
777 : : "Asia/Yerevan"
778 : : },
779 : : {
780 : : /* (UTC+04:00) Astrakhan, Ulyanovsk */
781 : : "Astrakhan Standard Time", "Astrakhan Daylight Time",
782 : : "Europe/Astrakhan"
783 : : },
784 : : {
785 : : /* (UTC-04:00) Atlantic Time (Canada) */
786 : : "Atlantic Standard Time", "Atlantic Daylight Time",
787 : : "America/Halifax"
788 : : },
789 : : {
790 : : /* (UTC+09:30) Darwin */
791 : : "AUS Central Standard Time", "AUS Central Daylight Time",
792 : : "Australia/Darwin"
793 : : },
794 : : {
795 : : /* (UTC+08:45) Eucla */
796 : : "Aus Central W. Standard Time", "Aus Central W. Daylight Time",
797 : : "Australia/Eucla"
798 : : },
799 : : {
800 : : /* (UTC+10:00) Canberra, Melbourne, Sydney */
801 : : "AUS Eastern Standard Time", "AUS Eastern Daylight Time",
802 : : "Australia/Sydney"
803 : : },
804 : : {
805 : : /* (UTC+04:00) Baku */
806 : : "Azerbaijan Standard Time", "Azerbaijan Daylight Time",
807 : : "Asia/Baku"
808 : : },
809 : : {
810 : : /* (UTC-01:00) Azores */
811 : : "Azores Standard Time", "Azores Daylight Time",
812 : : "Atlantic/Azores"
813 : : },
814 : : {
815 : : /* (UTC-03:00) Salvador */
816 : : "Bahia Standard Time", "Bahia Daylight Time",
817 : : "America/Bahia"
818 : : },
819 : : {
820 : : /* (UTC+06:00) Dhaka */
821 : : "Bangladesh Standard Time", "Bangladesh Daylight Time",
822 : : "Asia/Dhaka"
823 : : },
824 : : {
825 : : /* (UTC+03:00) Minsk */
826 : : "Belarus Standard Time", "Belarus Daylight Time",
827 : : "Europe/Minsk"
828 : : },
829 : : {
830 : : /* (UTC+11:00) Bougainville Island */
831 : : "Bougainville Standard Time", "Bougainville Daylight Time",
832 : : "Pacific/Bougainville"
833 : : },
834 : : {
835 : : /* (UTC-01:00) Cabo Verde Is. */
836 : : "Cabo Verde Standard Time", "Cabo Verde Daylight Time",
837 : : "Atlantic/Cape_Verde"
838 : : },
839 : : {
840 : : /* (UTC-06:00) Saskatchewan */
841 : : "Canada Central Standard Time", "Canada Central Daylight Time",
842 : : "America/Regina"
843 : : },
844 : : {
845 : : /* (UTC-01:00) Cape Verde Is. */
846 : : "Cape Verde Standard Time", "Cape Verde Daylight Time",
847 : : "Atlantic/Cape_Verde"
848 : : },
849 : : {
850 : : /* (UTC+04:00) Yerevan */
851 : : "Caucasus Standard Time", "Caucasus Daylight Time",
852 : : "Asia/Yerevan"
853 : : },
854 : : {
855 : : /* (UTC+09:30) Adelaide */
856 : : "Cen. Australia Standard Time", "Cen. Australia Daylight Time",
857 : : "Australia/Adelaide"
858 : : },
859 : : {
860 : : /* (UTC-06:00) Central America */
861 : : "Central America Standard Time", "Central America Daylight Time",
862 : : "America/Guatemala"
863 : : },
864 : : {
865 : : /* (UTC+06:00) Astana */
866 : : "Central Asia Standard Time", "Central Asia Daylight Time",
867 : : "Asia/Almaty"
868 : : },
869 : : {
870 : : /* (UTC-04:00) Cuiaba */
871 : : "Central Brazilian Standard Time", "Central Brazilian Daylight Time",
872 : : "America/Cuiaba"
873 : : },
874 : : {
875 : : /* (UTC+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague */
876 : : "Central Europe Standard Time", "Central Europe Daylight Time",
877 : : "Europe/Budapest"
878 : : },
879 : : {
880 : : /* (UTC+01:00) Sarajevo, Skopje, Warsaw, Zagreb */
881 : : "Central European Standard Time", "Central European Daylight Time",
882 : : "Europe/Warsaw"
883 : : },
884 : : {
885 : : /* (UTC+11:00) Solomon Is., New Caledonia */
886 : : "Central Pacific Standard Time", "Central Pacific Daylight Time",
887 : : "Pacific/Guadalcanal"
888 : : },
889 : : {
890 : : /* (UTC-06:00) Central Time (US & Canada) */
891 : : "Central Standard Time", "Central Daylight Time",
892 : : "America/Chicago"
893 : : },
894 : : {
895 : : /* (UTC-06:00) Guadalajara, Mexico City, Monterrey */
896 : : "Central Standard Time (Mexico)", "Central Daylight Time (Mexico)",
897 : : "America/Mexico_City"
898 : : },
899 : : {
900 : : /* (UTC+12:45) Chatham Islands */
901 : : "Chatham Islands Standard Time", "Chatham Islands Daylight Time",
902 : : "Pacific/Chatham"
903 : : },
904 : : {
905 : : /* (UTC+08:00) Beijing, Chongqing, Hong Kong, Urumqi */
906 : : "China Standard Time", "China Daylight Time",
907 : : "Asia/Shanghai"
908 : : },
909 : : {
910 : : /* (UTC) Coordinated Universal Time */
911 : : "Coordinated Universal Time", "Coordinated Universal Time",
912 : : "UTC"
913 : : },
914 : : {
915 : : /* (UTC-05:00) Havana */
916 : : "Cuba Standard Time", "Cuba Daylight Time",
917 : : "America/Havana"
918 : : },
919 : : {
920 : : /* (UTC-12:00) International Date Line West */
921 : : "Dateline Standard Time", "Dateline Daylight Time",
922 : : "Etc/GMT+12"
923 : : },
924 : : {
925 : : /* (UTC+03:00) Nairobi */
926 : : "E. Africa Standard Time", "E. Africa Daylight Time",
927 : : "Africa/Nairobi"
928 : : },
929 : : {
930 : : /* (UTC+10:00) Brisbane */
931 : : "E. Australia Standard Time", "E. Australia Daylight Time",
932 : : "Australia/Brisbane"
933 : : },
934 : : {
935 : : /* (UTC+02:00) Chisinau */
936 : : "E. Europe Standard Time", "E. Europe Daylight Time",
937 : : "Europe/Chisinau"
938 : : },
939 : : {
940 : : /* (UTC-03:00) Brasilia */
941 : : "E. South America Standard Time", "E. South America Daylight Time",
942 : : "America/Sao_Paulo"
943 : : },
944 : : {
945 : : /* (UTC-06:00) Easter Island */
946 : : "Easter Island Standard Time", "Easter Island Daylight Time",
947 : : "Pacific/Easter"
948 : : },
949 : : {
950 : : /* (UTC-05:00) Eastern Time (US & Canada) */
951 : : "Eastern Standard Time", "Eastern Daylight Time",
952 : : "America/New_York"
953 : : },
954 : : {
955 : : /* (UTC-05:00) Chetumal */
956 : : "Eastern Standard Time (Mexico)", "Eastern Daylight Time (Mexico)",
957 : : "America/Cancun"
958 : : },
959 : : {
960 : : /* (UTC+02:00) Cairo */
961 : : "Egypt Standard Time", "Egypt Daylight Time",
962 : : "Africa/Cairo"
963 : : },
964 : : {
965 : : /* (UTC+05:00) Ekaterinburg */
966 : : "Ekaterinburg Standard Time", "Ekaterinburg Daylight Time",
967 : : "Asia/Yekaterinburg"
968 : : },
969 : : {
970 : : /* (UTC+12:00) Fiji */
971 : : "Fiji Standard Time", "Fiji Daylight Time",
972 : : "Pacific/Fiji"
973 : : },
974 : : {
975 : : /* (UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius */
976 : : "FLE Standard Time", "FLE Daylight Time",
977 : : "Europe/Kiev"
978 : : },
979 : : {
980 : : /* (UTC+04:00) Tbilisi */
981 : : "Georgian Standard Time", "Georgian Daylight Time",
982 : : "Asia/Tbilisi"
983 : : },
984 : : {
985 : : /* (UTC+00:00) Dublin, Edinburgh, Lisbon, London */
986 : : "GMT Standard Time", "GMT Daylight Time",
987 : : "Europe/London"
988 : : },
989 : : {
990 : : /* (UTC-03:00) Greenland */
991 : : "Greenland Standard Time", "Greenland Daylight Time",
992 : : "America/Godthab"
993 : : },
994 : : {
995 : : /*
996 : : * Windows uses this zone name in various places that lie near the
997 : : * prime meridian, but are not in the UK. However, most people
998 : : * probably think that "Greenwich" means UK civil time, or maybe even
999 : : * straight-up UTC. Atlantic/Reykjavik is a decent match for that
1000 : : * interpretation because Iceland hasn't observed DST since 1968.
1001 : : */
1002 : : /* (UTC+00:00) Monrovia, Reykjavik */
1003 : : "Greenwich Standard Time", "Greenwich Daylight Time",
1004 : : "Atlantic/Reykjavik"
1005 : : },
1006 : : {
1007 : : /* (UTC+02:00) Athens, Bucharest */
1008 : : "GTB Standard Time", "GTB Daylight Time",
1009 : : "Europe/Bucharest"
1010 : : },
1011 : : {
1012 : : /* (UTC-05:00) Haiti */
1013 : : "Haiti Standard Time", "Haiti Daylight Time",
1014 : : "America/Port-au-Prince"
1015 : : },
1016 : : {
1017 : : /* (UTC-10:00) Hawaii */
1018 : : "Hawaiian Standard Time", "Hawaiian Daylight Time",
1019 : : "Pacific/Honolulu"
1020 : : },
1021 : : {
1022 : : /* (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi */
1023 : : "India Standard Time", "India Daylight Time",
1024 : : "Asia/Calcutta"
1025 : : },
1026 : : {
1027 : : /* (UTC+03:30) Tehran */
1028 : : "Iran Standard Time", "Iran Daylight Time",
1029 : : "Asia/Tehran"
1030 : : },
1031 : : {
1032 : : /* (UTC+02:00) Jerusalem */
1033 : : "Israel Standard Time", "Israel Daylight Time",
1034 : : "Asia/Jerusalem"
1035 : : },
1036 : : {
1037 : : /* (UTC+02:00) Jerusalem (old spelling of zone name) */
1038 : : "Jerusalem Standard Time", "Jerusalem Daylight Time",
1039 : : "Asia/Jerusalem"
1040 : : },
1041 : : {
1042 : : /* (UTC+02:00) Amman */
1043 : : "Jordan Standard Time", "Jordan Daylight Time",
1044 : : "Asia/Amman"
1045 : : },
1046 : : {
1047 : : /* (UTC+02:00) Kaliningrad */
1048 : : "Kaliningrad Standard Time", "Kaliningrad Daylight Time",
1049 : : "Europe/Kaliningrad"
1050 : : },
1051 : : {
1052 : : /* (UTC+12:00) Petropavlovsk-Kamchatsky - Old */
1053 : : "Kamchatka Standard Time", "Kamchatka Daylight Time",
1054 : : "Asia/Kamchatka"
1055 : : },
1056 : : {
1057 : : /* (UTC+09:00) Seoul */
1058 : : "Korea Standard Time", "Korea Daylight Time",
1059 : : "Asia/Seoul"
1060 : : },
1061 : : {
1062 : : /* (UTC+02:00) Tripoli */
1063 : : "Libya Standard Time", "Libya Daylight Time",
1064 : : "Africa/Tripoli"
1065 : : },
1066 : : {
1067 : : /* (UTC+14:00) Kiritimati Island */
1068 : : "Line Islands Standard Time", "Line Islands Daylight Time",
1069 : : "Pacific/Kiritimati"
1070 : : },
1071 : : {
1072 : : /* (UTC+10:30) Lord Howe Island */
1073 : : "Lord Howe Standard Time", "Lord Howe Daylight Time",
1074 : : "Australia/Lord_Howe"
1075 : : },
1076 : : {
1077 : : /* (UTC+11:00) Magadan */
1078 : : "Magadan Standard Time", "Magadan Daylight Time",
1079 : : "Asia/Magadan"
1080 : : },
1081 : : {
1082 : : /* (UTC-03:00) Punta Arenas */
1083 : : "Magallanes Standard Time", "Magallanes Daylight Time",
1084 : : "America/Punta_Arenas"
1085 : : },
1086 : : {
1087 : : /* (UTC+08:00) Kuala Lumpur, Singapore */
1088 : : "Malay Peninsula Standard Time", "Malay Peninsula Daylight Time",
1089 : : "Asia/Kuala_Lumpur"
1090 : : },
1091 : : {
1092 : : /* (UTC-09:30) Marquesas Islands */
1093 : : "Marquesas Standard Time", "Marquesas Daylight Time",
1094 : : "Pacific/Marquesas"
1095 : : },
1096 : : {
1097 : : /* (UTC+04:00) Port Louis */
1098 : : "Mauritius Standard Time", "Mauritius Daylight Time",
1099 : : "Indian/Mauritius"
1100 : : },
1101 : : {
1102 : : /* (UTC-06:00) Guadalajara, Mexico City, Monterrey */
1103 : : "Mexico Standard Time", "Mexico Daylight Time",
1104 : : "America/Mexico_City"
1105 : : },
1106 : : {
1107 : : /* (UTC-07:00) Chihuahua, La Paz, Mazatlan */
1108 : : "Mexico Standard Time 2", "Mexico Daylight Time 2",
1109 : : "America/Chihuahua"
1110 : : },
1111 : : {
1112 : : /* (UTC-02:00) Mid-Atlantic - Old */
1113 : : "Mid-Atlantic Standard Time", "Mid-Atlantic Daylight Time",
1114 : : "Atlantic/South_Georgia"
1115 : : },
1116 : : {
1117 : : /* (UTC+02:00) Beirut */
1118 : : "Middle East Standard Time", "Middle East Daylight Time",
1119 : : "Asia/Beirut"
1120 : : },
1121 : : {
1122 : : /* (UTC-03:00) Montevideo */
1123 : : "Montevideo Standard Time", "Montevideo Daylight Time",
1124 : : "America/Montevideo"
1125 : : },
1126 : : {
1127 : : /* (UTC+01:00) Casablanca */
1128 : : "Morocco Standard Time", "Morocco Daylight Time",
1129 : : "Africa/Casablanca"
1130 : : },
1131 : : {
1132 : : /* (UTC-07:00) Mountain Time (US & Canada) */
1133 : : "Mountain Standard Time", "Mountain Daylight Time",
1134 : : "America/Denver"
1135 : : },
1136 : : {
1137 : : /* (UTC-07:00) Chihuahua, La Paz, Mazatlan */
1138 : : "Mountain Standard Time (Mexico)", "Mountain Daylight Time (Mexico)",
1139 : : "America/Chihuahua"
1140 : : },
1141 : : {
1142 : : /* (UTC+06:30) Yangon (Rangoon) */
1143 : : "Myanmar Standard Time", "Myanmar Daylight Time",
1144 : : "Asia/Rangoon"
1145 : : },
1146 : : {
1147 : : /* (UTC+07:00) Novosibirsk */
1148 : : "N. Central Asia Standard Time", "N. Central Asia Daylight Time",
1149 : : "Asia/Novosibirsk"
1150 : : },
1151 : : {
1152 : : /* (UTC+02:00) Windhoek */
1153 : : "Namibia Standard Time", "Namibia Daylight Time",
1154 : : "Africa/Windhoek"
1155 : : },
1156 : : {
1157 : : /* (UTC+05:45) Kathmandu */
1158 : : "Nepal Standard Time", "Nepal Daylight Time",
1159 : : "Asia/Katmandu"
1160 : : },
1161 : : {
1162 : : /* (UTC+12:00) Auckland, Wellington */
1163 : : "New Zealand Standard Time", "New Zealand Daylight Time",
1164 : : "Pacific/Auckland"
1165 : : },
1166 : : {
1167 : : /* (UTC-03:30) Newfoundland */
1168 : : "Newfoundland Standard Time", "Newfoundland Daylight Time",
1169 : : "America/St_Johns"
1170 : : },
1171 : : {
1172 : : /* (UTC+11:00) Norfolk Island */
1173 : : "Norfolk Standard Time", "Norfolk Daylight Time",
1174 : : "Pacific/Norfolk"
1175 : : },
1176 : : {
1177 : : /* (UTC+08:00) Irkutsk */
1178 : : "North Asia East Standard Time", "North Asia East Daylight Time",
1179 : : "Asia/Irkutsk"
1180 : : },
1181 : : {
1182 : : /* (UTC+07:00) Krasnoyarsk */
1183 : : "North Asia Standard Time", "North Asia Daylight Time",
1184 : : "Asia/Krasnoyarsk"
1185 : : },
1186 : : {
1187 : : /* (UTC+09:00) Pyongyang */
1188 : : "North Korea Standard Time", "North Korea Daylight Time",
1189 : : "Asia/Pyongyang"
1190 : : },
1191 : : {
1192 : : /* (UTC+07:00) Novosibirsk */
1193 : : "Novosibirsk Standard Time", "Novosibirsk Daylight Time",
1194 : : "Asia/Novosibirsk"
1195 : : },
1196 : : {
1197 : : /* (UTC+06:00) Omsk */
1198 : : "Omsk Standard Time", "Omsk Daylight Time",
1199 : : "Asia/Omsk"
1200 : : },
1201 : : {
1202 : : /* (UTC-04:00) Santiago */
1203 : : "Pacific SA Standard Time", "Pacific SA Daylight Time",
1204 : : "America/Santiago"
1205 : : },
1206 : : {
1207 : : /* (UTC-08:00) Pacific Time (US & Canada) */
1208 : : "Pacific Standard Time", "Pacific Daylight Time",
1209 : : "America/Los_Angeles"
1210 : : },
1211 : : {
1212 : : /* (UTC-08:00) Baja California */
1213 : : "Pacific Standard Time (Mexico)", "Pacific Daylight Time (Mexico)",
1214 : : "America/Tijuana"
1215 : : },
1216 : : {
1217 : : /* (UTC+05:00) Islamabad, Karachi */
1218 : : "Pakistan Standard Time", "Pakistan Daylight Time",
1219 : : "Asia/Karachi"
1220 : : },
1221 : : {
1222 : : /* (UTC-04:00) Asuncion */
1223 : : "Paraguay Standard Time", "Paraguay Daylight Time",
1224 : : "America/Asuncion"
1225 : : },
1226 : : {
1227 : : /* (UTC+05:00) Qyzylorda */
1228 : : "Qyzylorda Standard Time", "Qyzylorda Daylight Time",
1229 : : "Asia/Qyzylorda"
1230 : : },
1231 : : {
1232 : : /* (UTC+01:00) Brussels, Copenhagen, Madrid, Paris */
1233 : : "Romance Standard Time", "Romance Daylight Time",
1234 : : "Europe/Paris"
1235 : : },
1236 : : {
1237 : : /* (UTC+04:00) Izhevsk, Samara */
1238 : : "Russia Time Zone 3", "Russia Time Zone 3",
1239 : : "Europe/Samara"
1240 : : },
1241 : : {
1242 : : /* (UTC+11:00) Chokurdakh */
1243 : : "Russia Time Zone 10", "Russia Time Zone 10",
1244 : : "Asia/Srednekolymsk"
1245 : : },
1246 : : {
1247 : : /* (UTC+12:00) Anadyr, Petropavlovsk-Kamchatsky */
1248 : : "Russia Time Zone 11", "Russia Time Zone 11",
1249 : : "Asia/Kamchatka"
1250 : : },
1251 : : {
1252 : : /* (UTC+02:00) Kaliningrad */
1253 : : "Russia TZ 1 Standard Time", "Russia TZ 1 Daylight Time",
1254 : : "Europe/Kaliningrad"
1255 : : },
1256 : : {
1257 : : /* (UTC+03:00) Moscow, St. Petersburg */
1258 : : "Russia TZ 2 Standard Time", "Russia TZ 2 Daylight Time",
1259 : : "Europe/Moscow"
1260 : : },
1261 : : {
1262 : : /* (UTC+04:00) Izhevsk, Samara */
1263 : : "Russia TZ 3 Standard Time", "Russia TZ 3 Daylight Time",
1264 : : "Europe/Samara"
1265 : : },
1266 : : {
1267 : : /* (UTC+05:00) Ekaterinburg */
1268 : : "Russia TZ 4 Standard Time", "Russia TZ 4 Daylight Time",
1269 : : "Asia/Yekaterinburg"
1270 : : },
1271 : : {
1272 : : /* (UTC+06:00) Novosibirsk (RTZ 5) */
1273 : : "Russia TZ 5 Standard Time", "Russia TZ 5 Daylight Time",
1274 : : "Asia/Novosibirsk"
1275 : : },
1276 : : {
1277 : : /* (UTC+07:00) Krasnoyarsk */
1278 : : "Russia TZ 6 Standard Time", "Russia TZ 6 Daylight Time",
1279 : : "Asia/Krasnoyarsk"
1280 : : },
1281 : : {
1282 : : /* (UTC+08:00) Irkutsk */
1283 : : "Russia TZ 7 Standard Time", "Russia TZ 7 Daylight Time",
1284 : : "Asia/Irkutsk"
1285 : : },
1286 : : {
1287 : : /* (UTC+09:00) Yakutsk */
1288 : : "Russia TZ 8 Standard Time", "Russia TZ 8 Daylight Time",
1289 : : "Asia/Yakutsk"
1290 : : },
1291 : : {
1292 : : /* (UTC+10:00) Vladivostok */
1293 : : "Russia TZ 9 Standard Time", "Russia TZ 9 Daylight Time",
1294 : : "Asia/Vladivostok"
1295 : : },
1296 : : {
1297 : : /* (UTC+11:00) Chokurdakh */
1298 : : "Russia TZ 10 Standard Time", "Russia TZ 10 Daylight Time",
1299 : : "Asia/Magadan"
1300 : : },
1301 : : {
1302 : : /* (UTC+12:00) Anadyr, Petropavlovsk-Kamchatsky */
1303 : : "Russia TZ 11 Standard Time", "Russia TZ 11 Daylight Time",
1304 : : "Asia/Anadyr"
1305 : : },
1306 : : {
1307 : : /* (UTC+03:00) Moscow, St. Petersburg */
1308 : : "Russian Standard Time", "Russian Daylight Time",
1309 : : "Europe/Moscow"
1310 : : },
1311 : : {
1312 : : /* (UTC-03:00) Cayenne, Fortaleza */
1313 : : "SA Eastern Standard Time", "SA Eastern Daylight Time",
1314 : : "America/Cayenne"
1315 : : },
1316 : : {
1317 : : /* (UTC-05:00) Bogota, Lima, Quito, Rio Branco */
1318 : : "SA Pacific Standard Time", "SA Pacific Daylight Time",
1319 : : "America/Bogota"
1320 : : },
1321 : : {
1322 : : /* (UTC-04:00) Georgetown, La Paz, Manaus, San Juan */
1323 : : "SA Western Standard Time", "SA Western Daylight Time",
1324 : : "America/La_Paz"
1325 : : },
1326 : : {
1327 : : /* (UTC-03:00) Saint Pierre and Miquelon */
1328 : : "Saint Pierre Standard Time", "Saint Pierre Daylight Time",
1329 : : "America/Miquelon"
1330 : : },
1331 : : {
1332 : : /* (UTC+11:00) Sakhalin */
1333 : : "Sakhalin Standard Time", "Sakhalin Daylight Time",
1334 : : "Asia/Sakhalin"
1335 : : },
1336 : : {
1337 : : /* (UTC+13:00) Samoa */
1338 : : "Samoa Standard Time", "Samoa Daylight Time",
1339 : : "Pacific/Apia"
1340 : : },
1341 : : {
1342 : : /* (UTC+00:00) Sao Tome */
1343 : : "Sao Tome Standard Time", "Sao Tome Daylight Time",
1344 : : "Africa/Sao_Tome"
1345 : : },
1346 : : {
1347 : : /* (UTC+04:00) Saratov */
1348 : : "Saratov Standard Time", "Saratov Daylight Time",
1349 : : "Europe/Saratov"
1350 : : },
1351 : : {
1352 : : /* (UTC+07:00) Bangkok, Hanoi, Jakarta */
1353 : : "SE Asia Standard Time", "SE Asia Daylight Time",
1354 : : "Asia/Bangkok"
1355 : : },
1356 : : {
1357 : : /* (UTC+08:00) Kuala Lumpur, Singapore */
1358 : : "Singapore Standard Time", "Singapore Daylight Time",
1359 : : "Asia/Singapore"
1360 : : },
1361 : : {
1362 : : /* (UTC+02:00) Harare, Pretoria */
1363 : : "South Africa Standard Time", "South Africa Daylight Time",
1364 : : "Africa/Johannesburg"
1365 : : },
1366 : : {
1367 : : /* (UTC+02:00) Juba */
1368 : : "South Sudan Standard Time", "South Sudan Daylight Time",
1369 : : "Africa/Juba"
1370 : : },
1371 : : {
1372 : : /* (UTC+05:30) Sri Jayawardenepura */
1373 : : "Sri Lanka Standard Time", "Sri Lanka Daylight Time",
1374 : : "Asia/Colombo"
1375 : : },
1376 : : {
1377 : : /* (UTC+02:00) Khartoum */
1378 : : "Sudan Standard Time", "Sudan Daylight Time",
1379 : : "Africa/Khartoum"
1380 : : },
1381 : : {
1382 : : /* (UTC+02:00) Damascus */
1383 : : "Syria Standard Time", "Syria Daylight Time",
1384 : : "Asia/Damascus"
1385 : : },
1386 : : {
1387 : : /* (UTC+08:00) Taipei */
1388 : : "Taipei Standard Time", "Taipei Daylight Time",
1389 : : "Asia/Taipei"
1390 : : },
1391 : : {
1392 : : /* (UTC+10:00) Hobart */
1393 : : "Tasmania Standard Time", "Tasmania Daylight Time",
1394 : : "Australia/Hobart"
1395 : : },
1396 : : {
1397 : : /* (UTC-03:00) Araguaina */
1398 : : "Tocantins Standard Time", "Tocantins Daylight Time",
1399 : : "America/Araguaina"
1400 : : },
1401 : : {
1402 : : /* (UTC+09:00) Osaka, Sapporo, Tokyo */
1403 : : "Tokyo Standard Time", "Tokyo Daylight Time",
1404 : : "Asia/Tokyo"
1405 : : },
1406 : : {
1407 : : /* (UTC+07:00) Tomsk */
1408 : : "Tomsk Standard Time", "Tomsk Daylight Time",
1409 : : "Asia/Tomsk"
1410 : : },
1411 : : {
1412 : : /* (UTC+13:00) Nuku'alofa */
1413 : : "Tonga Standard Time", "Tonga Daylight Time",
1414 : : "Pacific/Tongatapu"
1415 : : },
1416 : : {
1417 : : /* (UTC+09:00) Chita */
1418 : : "Transbaikal Standard Time", "Transbaikal Daylight Time",
1419 : : "Asia/Chita"
1420 : : },
1421 : : {
1422 : : /* (UTC+03:00) Istanbul */
1423 : : "Turkey Standard Time", "Turkey Daylight Time",
1424 : : "Europe/Istanbul"
1425 : : },
1426 : : {
1427 : : /* (UTC-05:00) Turks and Caicos */
1428 : : "Turks And Caicos Standard Time", "Turks And Caicos Daylight Time",
1429 : : "America/Grand_Turk"
1430 : : },
1431 : : {
1432 : : /* (UTC+08:00) Ulaanbaatar */
1433 : : "Ulaanbaatar Standard Time", "Ulaanbaatar Daylight Time",
1434 : : "Asia/Ulaanbaatar"
1435 : : },
1436 : : {
1437 : : /* (UTC-05:00) Indiana (East) */
1438 : : "US Eastern Standard Time", "US Eastern Daylight Time",
1439 : : "America/Indianapolis"
1440 : : },
1441 : : {
1442 : : /* (UTC-07:00) Arizona */
1443 : : "US Mountain Standard Time", "US Mountain Daylight Time",
1444 : : "America/Phoenix"
1445 : : },
1446 : : {
1447 : : /* (UTC) Coordinated Universal Time */
1448 : : "UTC", "UTC",
1449 : : "UTC"
1450 : : },
1451 : : {
1452 : : /* (UTC+12:00) Coordinated Universal Time+12 */
1453 : : "UTC+12", "UTC+12",
1454 : : "Etc/GMT-12"
1455 : : },
1456 : : {
1457 : : /* (UTC+13:00) Coordinated Universal Time+13 */
1458 : : "UTC+13", "UTC+13",
1459 : : "Etc/GMT-13"
1460 : : },
1461 : : {
1462 : : /* (UTC-02:00) Coordinated Universal Time-02 */
1463 : : "UTC-02", "UTC-02",
1464 : : "Etc/GMT+2"
1465 : : },
1466 : : {
1467 : : /* (UTC-08:00) Coordinated Universal Time-08 */
1468 : : "UTC-08", "UTC-08",
1469 : : "Etc/GMT+8"
1470 : : },
1471 : : {
1472 : : /* (UTC-09:00) Coordinated Universal Time-09 */
1473 : : "UTC-09", "UTC-09",
1474 : : "Etc/GMT+9"
1475 : : },
1476 : : {
1477 : : /* (UTC-11:00) Coordinated Universal Time-11 */
1478 : : "UTC-11", "UTC-11",
1479 : : "Etc/GMT+11"
1480 : : },
1481 : : {
1482 : : /* (UTC-04:00) Caracas */
1483 : : "Venezuela Standard Time", "Venezuela Daylight Time",
1484 : : "America/Caracas"
1485 : : },
1486 : : {
1487 : : /* (UTC+10:00) Vladivostok */
1488 : : "Vladivostok Standard Time", "Vladivostok Daylight Time",
1489 : : "Asia/Vladivostok"
1490 : : },
1491 : : {
1492 : : /* (UTC+04:00) Volgograd */
1493 : : "Volgograd Standard Time", "Volgograd Daylight Time",
1494 : : "Europe/Volgograd"
1495 : : },
1496 : : {
1497 : : /* (UTC+08:00) Perth */
1498 : : "W. Australia Standard Time", "W. Australia Daylight Time",
1499 : : "Australia/Perth"
1500 : : },
1501 : : {
1502 : : /* (UTC+01:00) West Central Africa */
1503 : : "W. Central Africa Standard Time", "W. Central Africa Daylight Time",
1504 : : "Africa/Lagos"
1505 : : },
1506 : : {
1507 : : /* (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna */
1508 : : "W. Europe Standard Time", "W. Europe Daylight Time",
1509 : : "Europe/Berlin"
1510 : : },
1511 : : {
1512 : : /* (UTC+07:00) Hovd */
1513 : : "W. Mongolia Standard Time", "W. Mongolia Daylight Time",
1514 : : "Asia/Hovd"
1515 : : },
1516 : : {
1517 : : /* (UTC+05:00) Ashgabat, Tashkent */
1518 : : "West Asia Standard Time", "West Asia Daylight Time",
1519 : : "Asia/Tashkent"
1520 : : },
1521 : : {
1522 : : /* (UTC+02:00) Gaza, Hebron */
1523 : : "West Bank Gaza Standard Time", "West Bank Gaza Daylight Time",
1524 : : "Asia/Gaza"
1525 : : },
1526 : : {
1527 : : /* (UTC+02:00) Gaza, Hebron */
1528 : : "West Bank Standard Time", "West Bank Daylight Time",
1529 : : "Asia/Hebron"
1530 : : },
1531 : : {
1532 : : /* (UTC+10:00) Guam, Port Moresby */
1533 : : "West Pacific Standard Time", "West Pacific Daylight Time",
1534 : : "Pacific/Port_Moresby"
1535 : : },
1536 : : {
1537 : : /* (UTC+09:00) Yakutsk */
1538 : : "Yakutsk Standard Time", "Yakutsk Daylight Time",
1539 : : "Asia/Yakutsk"
1540 : : },
1541 : : {
1542 : : /* (UTC-07:00) Yukon */
1543 : : "Yukon Standard Time", "Yukon Daylight Time",
1544 : : "America/Whitehorse"
1545 : : },
1546 : : {
1547 : : NULL, NULL, NULL
1548 : : }
1549 : : };
1550 : :
1551 : : static const char *
1552 : : identify_system_timezone(void)
1553 : : {
1554 : : int i;
1555 : : char tzname[128];
1556 : : char localtzname[256];
1557 : : time_t t = time(NULL);
1558 : : struct tm *tm = localtime(&t);
1559 : : HKEY rootKey;
1560 : : int idx;
1561 : :
1562 : : if (!tm)
1563 : : {
1564 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1565 : : fprintf(stderr, "could not identify system time zone: localtime() failed\n");
1566 : : #endif
1567 : : return NULL; /* go to GMT */
1568 : : }
1569 : :
1570 : : memset(tzname, 0, sizeof(tzname));
1571 : : strftime(tzname, sizeof(tzname) - 1, "%Z", tm);
1572 : :
1573 : : for (i = 0; win32_tzmap[i].stdname != NULL; i++)
1574 : : {
1575 : : if (strcmp(tzname, win32_tzmap[i].stdname) == 0 ||
1576 : : strcmp(tzname, win32_tzmap[i].dstname) == 0)
1577 : : {
1578 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1579 : : fprintf(stderr, "TZ \"%s\" matches system time zone \"%s\"\n",
1580 : : win32_tzmap[i].pgtzname, tzname);
1581 : : #endif
1582 : : return win32_tzmap[i].pgtzname;
1583 : : }
1584 : : }
1585 : :
1586 : : /*
1587 : : * Localized Windows versions return localized names for the timezone.
1588 : : * Scan the registry to find the English name, and then try matching
1589 : : * against our table again.
1590 : : */
1591 : : memset(localtzname, 0, sizeof(localtzname));
1592 : : if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
1593 : : "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones",
1594 : : 0,
1595 : : KEY_READ,
1596 : : &rootKey) != ERROR_SUCCESS)
1597 : : {
1598 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1599 : : fprintf(stderr, "could not open registry key to identify system time zone: error code %lu\n",
1600 : : GetLastError());
1601 : : #endif
1602 : : return NULL; /* go to GMT */
1603 : : }
1604 : :
1605 : : for (idx = 0;; idx++)
1606 : : {
1607 : : char keyname[256];
1608 : : char zonename[256];
1609 : : DWORD namesize;
1610 : : FILETIME lastwrite;
1611 : : HKEY key;
1612 : : LONG r;
1613 : :
1614 : : memset(keyname, 0, sizeof(keyname));
1615 : : namesize = sizeof(keyname);
1616 : : if ((r = RegEnumKeyEx(rootKey,
1617 : : idx,
1618 : : keyname,
1619 : : &namesize,
1620 : : NULL,
1621 : : NULL,
1622 : : NULL,
1623 : : &lastwrite)) != ERROR_SUCCESS)
1624 : : {
1625 : : if (r == ERROR_NO_MORE_ITEMS)
1626 : : break;
1627 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1628 : : fprintf(stderr, "could not enumerate registry subkeys to identify system time zone: %d\n",
1629 : : (int) r);
1630 : : #endif
1631 : : break;
1632 : : }
1633 : :
1634 : : if ((r = RegOpenKeyEx(rootKey, keyname, 0, KEY_READ, &key)) != ERROR_SUCCESS)
1635 : : {
1636 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1637 : : fprintf(stderr, "could not open registry subkey to identify system time zone: %d\n",
1638 : : (int) r);
1639 : : #endif
1640 : : break;
1641 : : }
1642 : :
1643 : : memset(zonename, 0, sizeof(zonename));
1644 : : namesize = sizeof(zonename);
1645 : : if ((r = RegQueryValueEx(key, "Std", NULL, NULL, (unsigned char *) zonename, &namesize)) != ERROR_SUCCESS)
1646 : : {
1647 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1648 : : fprintf(stderr, "could not query value for key \"std\" to identify system time zone \"%s\": %d\n",
1649 : : keyname, (int) r);
1650 : : #endif
1651 : : RegCloseKey(key);
1652 : : continue; /* Proceed to look at the next timezone */
1653 : : }
1654 : : if (strcmp(tzname, zonename) == 0)
1655 : : {
1656 : : /* Matched zone */
1657 : : strcpy(localtzname, keyname);
1658 : : RegCloseKey(key);
1659 : : break;
1660 : : }
1661 : : memset(zonename, 0, sizeof(zonename));
1662 : : namesize = sizeof(zonename);
1663 : : if ((r = RegQueryValueEx(key, "Dlt", NULL, NULL, (unsigned char *) zonename, &namesize)) != ERROR_SUCCESS)
1664 : : {
1665 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1666 : : fprintf(stderr, "could not query value for key \"dlt\" to identify system time zone \"%s\": %d\n",
1667 : : keyname, (int) r);
1668 : : #endif
1669 : : RegCloseKey(key);
1670 : : continue; /* Proceed to look at the next timezone */
1671 : : }
1672 : : if (strcmp(tzname, zonename) == 0)
1673 : : {
1674 : : /* Matched DST zone */
1675 : : strcpy(localtzname, keyname);
1676 : : RegCloseKey(key);
1677 : : break;
1678 : : }
1679 : :
1680 : : RegCloseKey(key);
1681 : : }
1682 : :
1683 : : RegCloseKey(rootKey);
1684 : :
1685 : : if (localtzname[0])
1686 : : {
1687 : : /* Found a localized name, so scan for that one too */
1688 : : for (i = 0; win32_tzmap[i].stdname != NULL; i++)
1689 : : {
1690 : : if (strcmp(localtzname, win32_tzmap[i].stdname) == 0 ||
1691 : : strcmp(localtzname, win32_tzmap[i].dstname) == 0)
1692 : : {
1693 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1694 : : fprintf(stderr, "TZ \"%s\" matches localized system time zone \"%s\" (\"%s\")\n",
1695 : : win32_tzmap[i].pgtzname, tzname, localtzname);
1696 : : #endif
1697 : : return win32_tzmap[i].pgtzname;
1698 : : }
1699 : : }
1700 : : }
1701 : :
1702 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1703 : : fprintf(stderr, "could not find a match for system time zone \"%s\"\n",
1704 : : tzname);
1705 : : #endif
1706 : : return NULL; /* go to GMT */
1707 : : }
1708 : : #endif /* WIN32 */
1709 : :
1710 : :
1711 : : /*
1712 : : * Return true if the given zone name is valid and is an "acceptable" zone.
1713 : : */
1714 : : static bool
1715 : 116 : validate_zone(const char *tzname)
1716 : : {
1717 : : pg_tz *tz;
1718 : :
1719 [ + + - + ]: 116 : if (!tzname || !tzname[0])
1720 : 58 : return false;
1721 : :
1722 : 58 : tz = pg_load_tz(tzname);
1723 [ - + ]: 58 : if (!tz)
1724 : 0 : return false;
1725 : :
1726 [ - + ]: 58 : if (!pg_tz_acceptable(tz))
1727 : 0 : return false;
1728 : :
1729 : 58 : return true;
1730 : : }
1731 : :
1732 : : /*
1733 : : * Identify a suitable default timezone setting based on the environment.
1734 : : *
1735 : : * The installation share_path must be passed in, as that is the default
1736 : : * location for the timezone database directory.
1737 : : *
1738 : : * We first look to the TZ environment variable. If not found or not
1739 : : * recognized by our own code, we see if we can identify the timezone
1740 : : * from the behavior of the system timezone library. When all else fails,
1741 : : * return NULL, indicating that we should default to GMT.
1742 : : */
1743 : : const char *
1744 : 58 : select_default_timezone(const char *share_path)
1745 : : {
1746 : : const char *tzname;
1747 : :
1748 : : /* Initialize timezone directory path, if needed */
1749 : : #ifndef SYSTEMTZDIR
1750 : 58 : snprintf(tzdirpath, sizeof(tzdirpath), "%s/timezone", share_path);
1751 : : #endif
1752 : :
1753 : : /* Check TZ environment variable */
1754 : 58 : tzname = getenv("TZ");
1755 [ - + ]: 58 : if (validate_zone(tzname))
1756 : 0 : return tzname;
1757 : :
1758 : : /* Nope, so try to identify the system timezone */
1759 : 58 : tzname = identify_system_timezone();
1760 [ + - ]: 58 : if (validate_zone(tzname))
1761 : 58 : return tzname;
1762 : :
1763 : 0 : return NULL;
1764 : : }
|