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