Age Owner Branch data TLA 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 *
5536 tgl@sss.pgh.pa.us 37 :CBC 240 : pg_TZDIR(void)
38 : : {
39 : : #ifndef SYSTEMTZDIR
40 : : /* normal case: timezone stuff is under our share dir */
41 : 240 : 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 : 240 : pg_open_tzfile(const char *name, char *canonname)
66 : : {
67 : : char fullname[MAXPGPATH];
68 : :
69 [ - + ]: 240 : if (canonname)
5536 tgl@sss.pgh.pa.us 70 :UBC 0 : strlcpy(canonname, name, TZ_STRLEN_MAX + 1);
71 : :
4644 tgl@sss.pgh.pa.us 72 :CBC 240 : strlcpy(fullname, pg_TZDIR(), sizeof(fullname));
5536 73 [ - + ]: 240 : if (strlen(fullname) + 1 + strlen(name) >= MAXPGPATH)
5536 tgl@sss.pgh.pa.us 74 :UBC 0 : return -1; /* not gonna fit */
5536 tgl@sss.pgh.pa.us 75 :CBC 240 : strcat(fullname, "/");
76 : 240 : strcat(fullname, name);
77 : :
78 : 240 : 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 : 240 : pg_load_tz(const char *name)
92 : : {
93 : : static pg_tz tz;
94 : :
95 [ - + ]: 240 : if (strlen(name) > TZ_STRLEN_MAX)
5536 tgl@sss.pgh.pa.us 96 :UBC 0 : return NULL; /* not going to fit */
97 : :
98 : : /*
99 : : * Let the IANA tzdb code interpret the time zone name.
100 : : */
127 tgl@sss.pgh.pa.us 101 [ + + ]:GNC 240 : if (!pg_tzload(name, NULL, &tz.state))
102 : 120 : return NULL; /* unknown timezone */
103 : :
5536 tgl@sss.pgh.pa.us 104 :CBC 120 : strcpy(tz.TZname, name);
105 : :
106 : 120 : 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
3424 tgl@sss.pgh.pa.us 162 :UBC 0 : get_timezone_offset(struct tm *tm)
163 : : {
164 : : #if defined(HAVE_STRUCT_TM_TM_ZONE)
5536 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
5536 tgl@sss.pgh.pa.us 177 :CBC 120 : build_time_t(int year, int month, int day)
178 : : {
179 : : struct tm tm;
180 : :
181 : 120 : memset(&tm, 0, sizeof(tm));
182 : 120 : tm.tm_mday = day;
183 : 120 : tm.tm_mon = month - 1;
184 : 120 : tm.tm_year = year - 1900;
1971 185 : 120 : tm.tm_isdst = -1;
186 : :
5536 187 : 120 : return mktime(&tm);
188 : : }
189 : :
190 : : /*
191 : : * Does a system tm value match one we computed ourselves?
192 : : */
193 : : static bool
3424 194 : 312000 : compare_tm(struct tm *s, struct pg_tm *p)
195 : : {
5536 196 [ + - ]: 312000 : if (s->tm_sec != p->tm_sec ||
197 [ + - ]: 312000 : s->tm_min != p->tm_min ||
198 [ + - ]: 312000 : s->tm_hour != p->tm_hour ||
199 [ + - ]: 312000 : s->tm_mday != p->tm_mday ||
200 [ + - ]: 312000 : s->tm_mon != p->tm_mon ||
201 [ + - ]: 312000 : s->tm_year != p->tm_year ||
202 [ + - ]: 312000 : s->tm_wday != p->tm_wday ||
203 [ + - ]: 312000 : s->tm_yday != p->tm_yday ||
204 [ - + ]: 312000 : s->tm_isdst != p->tm_isdst)
5536 tgl@sss.pgh.pa.us 205 :UBC 0 : return false;
5536 tgl@sss.pgh.pa.us 206 :CBC 312000 : 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
3424 221 : 180 : 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 */
5536 231 : 180 : tz = pg_load_tz(tzname);
232 [ + + ]: 180 : if (!tz)
233 : 120 : return -1; /* unrecognized zone name */
234 : :
235 : : /* Reject if leap seconds involved */
236 [ - + ]: 60 : if (!pg_tz_acceptable(tz))
237 : : {
238 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
239 : : fprintf(stderr, "Reject TZ \"%s\": uses leap seconds\n", tzname);
240 : : #endif
5536 tgl@sss.pgh.pa.us 241 :UBC 0 : return -1;
242 : : }
243 : :
244 : : /* Check for match at all the test times */
5536 tgl@sss.pgh.pa.us 245 [ + + ]:CBC 312060 : for (i = 0; i < tt->n_test_times; i++)
246 : : {
247 : 312000 : pgtt = (pg_time_t) (tt->test_times[i]);
248 : 312000 : pgtm = pg_localtime(&pgtt, tz);
249 [ - + ]: 312000 : if (!pgtm)
5536 tgl@sss.pgh.pa.us 250 :UBC 0 : return -1; /* probably shouldn't happen */
5536 tgl@sss.pgh.pa.us 251 :CBC 312000 : systm = localtime(&(tt->test_times[i]));
252 [ - + ]: 312000 : 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
5536 tgl@sss.pgh.pa.us 261 :UBC 0 : return i;
262 : : }
5536 tgl@sss.pgh.pa.us 263 [ - + ]:CBC 312000 : 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
5536 tgl@sss.pgh.pa.us 275 :UBC 0 : return i;
276 : : }
5536 tgl@sss.pgh.pa.us 277 [ + - ]:CBC 312000 : if (systm->tm_isdst >= 0)
278 : : {
279 : : /* Check match of zone names, too */
280 [ - + ]: 312000 : if (pgtm->tm_zone == NULL)
5536 tgl@sss.pgh.pa.us 281 :UBC 0 : return -1; /* probably shouldn't happen */
5536 tgl@sss.pgh.pa.us 282 :CBC 312000 : memset(cbuf, 0, sizeof(cbuf));
3424 283 : 312000 : strftime(cbuf, sizeof(cbuf) - 1, "%Z", systm); /* zone abbr */
5536 284 [ - + ]: 312000 : 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
5536 tgl@sss.pgh.pa.us 291 :UBC 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 : :
5536 tgl@sss.pgh.pa.us 300 :CBC 60 : return i;
301 : : }
302 : :
303 : : /*
304 : : * Test whether given zone name is a perfect match to localtime() behavior
305 : : */
306 : : static bool
2975 307 : 180 : perfect_timezone_match(const char *tzname, struct tztry *tt)
308 : : {
309 : 180 : 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 *
5536 318 : 60 : 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 : 60 : 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 : 60 : tnow = time(NULL);
356 : 60 : tm = localtime(&tnow);
357 [ - + ]: 60 : if (!tm)
5536 tgl@sss.pgh.pa.us 358 :UBC 0 : return NULL; /* give up if localtime is broken... */
5536 tgl@sss.pgh.pa.us 359 :CBC 60 : thisyear = tm->tm_year + 1900;
360 : :
361 : 60 : 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 : 60 : t -= (t % T_WEEK);
370 : :
371 : 60 : tt.n_test_times = 0;
372 : 60 : tt.test_times[tt.n_test_times++] = t;
373 : :
374 : 60 : t = build_time_t(thisyear, 7, 15);
375 : 60 : t -= (t % T_WEEK);
376 : :
377 : 60 : tt.test_times[tt.n_test_times++] = t;
378 : :
379 [ + + ]: 311940 : while (tt.n_test_times < MAX_TEST_TIMES)
380 : : {
381 : 311880 : t -= T_WEEK;
382 : 311880 : 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 : : */
2975 393 [ + - ]: 60 : if (check_system_link_file("/etc/localtime", &tt, resultbuf))
394 : 60 : return resultbuf;
395 : :
396 : : /* No luck, so search for the best-matching timezone file */
4644 tgl@sss.pgh.pa.us 397 :UBC 0 : strlcpy(tmptzdir, pg_TZDIR(), sizeof(tmptzdir));
5536 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
2975 tgl@sss.pgh.pa.us 531 :CBC 60 : 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 : 60 : len = readlink(linkname, link_target, sizeof(link_target));
544 [ + - - + ]: 60 : if (len < 0 || len >= sizeof(link_target))
2975 tgl@sss.pgh.pa.us 545 :UBC 0 : return false;
2975 tgl@sss.pgh.pa.us 546 :CBC 60 : 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 : 60 : cur_name = link_target;
561 [ + - ]: 180 : while (*cur_name)
562 : : {
563 : : /* Advance to next segment of path */
564 : 180 : cur_name = strchr(cur_name + 1, '/');
565 [ - + ]: 180 : if (cur_name == NULL)
2975 tgl@sss.pgh.pa.us 566 :UBC 0 : break;
567 : : /* If there are consecutive slashes, skip all, as the kernel would */
568 : : do
569 : : {
2975 tgl@sss.pgh.pa.us 570 :CBC 180 : cur_name++;
571 [ - + ]: 180 : } 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 [ + - + - ]: 180 : if (*cur_name && *cur_name != '.' &&
579 [ + - + + ]: 360 : strlen(cur_name) <= TZ_STRLEN_MAX &&
580 : 180 : perfect_timezone_match(cur_name, tt))
581 : : {
582 : : /* Success! */
583 : 60 : strcpy(bestzonename, cur_name);
584 : 60 : return true;
585 : : }
586 : : }
587 : :
588 : : /* Couldn't extract a matching zone name */
2975 tgl@sss.pgh.pa.us 589 :UBC 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
2700 rhodiumtoad@postgres 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 : : */
2659 tgl@sss.pgh.pa.us 617 [ # # ]: 0 : if (strcmp(zonename, "localtime") == 0 ||
618 [ # # ]: 0 : strcmp(zonename, "posixrules") == 0)
619 : 0 : return -50;
620 : :
2700 rhodiumtoad@postgres 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
3424 tgl@sss.pgh.pa.us 644 : 0 : scan_available_timezones(char *tzdir, char *tzdirsub, struct tztry *tt,
645 : : int *bestscore, char *bestzonename)
646 : : {
5536 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 */
2684 696 : 0 : int namepref = (zone_name_pref(tzdirsub) -
697 : 0 : zone_name_pref(bestzonename));
698 : :
2700 rhodiumtoad@postgres 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))))
5536 tgl@sss.pgh.pa.us 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 : : * The contents of this array are mostly generated from the CLDR project's
726 : : * windowsZones.xml file. See src/timezone/README for more information.
727 : : */
728 : : {
729 : : /* (UTC+04:30) Kabul */
730 : : "Afghanistan Standard Time", "Afghanistan Daylight Time",
731 : : "Asia/Kabul"
732 : : },
733 : : {
734 : : /* (UTC-09:00) Alaska */
735 : : "Alaskan Standard Time", "Alaskan Daylight Time",
736 : : "America/Anchorage"
737 : : },
738 : : {
739 : : /* (UTC-07:00) Alberta */
740 : : "Alberta Standard Time", "Alberta Daylight Time",
741 : : "America/Edmonton"
742 : : },
743 : : {
744 : : /* (UTC-10:00) Aleutian Islands */
745 : : "Aleutian Standard Time", "Aleutian Daylight Time",
746 : : "America/Adak"
747 : : },
748 : : {
749 : : /* (UTC+07:00) Barnaul, Gorno-Altaysk */
750 : : "Altai Standard Time", "Altai Daylight Time",
751 : : "Asia/Barnaul"
752 : : },
753 : : {
754 : : /* (UTC+03:00) Kuwait, Riyadh */
755 : : "Arab Standard Time", "Arab Daylight Time",
756 : : "Asia/Riyadh"
757 : : },
758 : : {
759 : : /* (UTC+04:00) Abu Dhabi, Muscat */
760 : : "Arabian Standard Time", "Arabian Daylight Time",
761 : : "Asia/Dubai"
762 : : },
763 : : {
764 : : /* (UTC+03:00) Baghdad */
765 : : "Arabic Standard Time", "Arabic Daylight Time",
766 : : "Asia/Baghdad"
767 : : },
768 : : {
769 : : /* (UTC-03:00) City of Buenos Aires */
770 : : "Argentina Standard Time", "Argentina Daylight Time",
771 : : "America/Buenos_Aires"
772 : : },
773 : : {
774 : : /* (UTC+04:00) Astrakhan, Ulyanovsk */
775 : : "Astrakhan Standard Time", "Astrakhan Daylight Time",
776 : : "Europe/Astrakhan"
777 : : },
778 : : {
779 : : /* (UTC-04:00) Atlantic Time (Canada) */
780 : : "Atlantic Standard Time", "Atlantic Daylight Time",
781 : : "America/Halifax"
782 : : },
783 : : {
784 : : /* (UTC+09:30) Darwin */
785 : : "AUS Central Standard Time", "AUS Central Daylight Time",
786 : : "Australia/Darwin"
787 : : },
788 : : {
789 : : /* (UTC+08:45) Eucla */
790 : : "Aus Central W. Standard Time", "Aus Central W. Daylight Time",
791 : : "Australia/Eucla"
792 : : },
793 : : {
794 : : /* (UTC+10:00) Canberra, Melbourne, Sydney */
795 : : "AUS Eastern Standard Time", "AUS Eastern Daylight Time",
796 : : "Australia/Sydney"
797 : : },
798 : : {
799 : : /* (UTC+04:00) Baku */
800 : : "Azerbaijan Standard Time", "Azerbaijan Daylight Time",
801 : : "Asia/Baku"
802 : : },
803 : : {
804 : : /* (UTC-01:00) Azores */
805 : : "Azores Standard Time", "Azores Daylight Time",
806 : : "Atlantic/Azores"
807 : : },
808 : : {
809 : : /* (UTC-03:00) Salvador */
810 : : "Bahia Standard Time", "Bahia Daylight Time",
811 : : "America/Bahia"
812 : : },
813 : : {
814 : : /* (UTC+06:00) Dhaka */
815 : : "Bangladesh Standard Time", "Bangladesh Daylight Time",
816 : : "Asia/Dhaka"
817 : : },
818 : : {
819 : : /* (UTC+03:00) Minsk */
820 : : "Belarus Standard Time", "Belarus Daylight Time",
821 : : "Europe/Minsk"
822 : : },
823 : : {
824 : : /* (UTC+11:00) Bougainville Island */
825 : : "Bougainville Standard Time", "Bougainville Daylight Time",
826 : : "Pacific/Bougainville"
827 : : },
828 : : {
829 : : /* (UTC-07:00) British Columbia */
830 : : "British Columbia Standard Time", "British Columbia Daylight Time",
831 : : "America/Vancouver"
832 : : },
833 : : {
834 : : /* (UTC-06:00) Saskatchewan */
835 : : "Canada Central Standard Time", "Canada Central Daylight Time",
836 : : "America/Regina"
837 : : },
838 : : {
839 : : /* (UTC-01:00) Cabo Verde Is. */
840 : : "Cape Verde Standard Time", "Cape Verde Daylight Time",
841 : : "Atlantic/Cape_Verde"
842 : : },
843 : : {
844 : : /* (UTC+04:00) Yerevan */
845 : : "Caucasus Standard Time", "Caucasus Daylight Time",
846 : : "Asia/Yerevan"
847 : : },
848 : : {
849 : : /* (UTC+09:30) Adelaide */
850 : : "Cen. Australia Standard Time", "Cen. Australia Daylight Time",
851 : : "Australia/Adelaide"
852 : : },
853 : : {
854 : : /* (UTC-06:00) Central America */
855 : : "Central America Standard Time", "Central America Daylight Time",
856 : : "America/Guatemala"
857 : : },
858 : : {
859 : : /* (UTC+06:00) Astana */
860 : : "Central Asia Standard Time", "Central Asia Daylight Time",
861 : : "Asia/Bishkek"
862 : : },
863 : : {
864 : : /* (UTC-04:00) Cuiaba */
865 : : "Central Brazilian Standard Time", "Central Brazilian Daylight Time",
866 : : "America/Cuiaba"
867 : : },
868 : : {
869 : : /* (UTC+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague */
870 : : "Central Europe Standard Time", "Central Europe Daylight Time",
871 : : "Europe/Budapest"
872 : : },
873 : : {
874 : : /* (UTC+01:00) Sarajevo, Skopje, Warsaw, Zagreb */
875 : : "Central European Standard Time", "Central European Daylight Time",
876 : : "Europe/Warsaw"
877 : : },
878 : : {
879 : : /* (UTC+11:00) Solomon Is., New Caledonia */
880 : : "Central Pacific Standard Time", "Central Pacific Daylight Time",
881 : : "Pacific/Guadalcanal"
882 : : },
883 : : {
884 : : /* (UTC-06:00) Central Time (US & Canada) */
885 : : "Central Standard Time", "Central Daylight Time",
886 : : "America/Chicago"
887 : : },
888 : : {
889 : : /* (UTC-06:00) Guadalajara, Mexico City, Monterrey */
890 : : "Central Standard Time (Mexico)", "Central Daylight Time (Mexico)",
891 : : "America/Mexico_City"
892 : : },
893 : : {
894 : : /* (UTC+12:45) Chatham Islands */
895 : : "Chatham Islands Standard Time", "Chatham Islands Daylight Time",
896 : : "Pacific/Chatham"
897 : : },
898 : : {
899 : : /* (UTC+08:00) Beijing, Chongqing, Hong Kong, Urumqi */
900 : : "China Standard Time", "China Daylight Time",
901 : : "Asia/Shanghai"
902 : : },
903 : : {
904 : : /* (UTC-05:00) Havana */
905 : : "Cuba Standard Time", "Cuba Daylight Time",
906 : : "America/Havana"
907 : : },
908 : : {
909 : : /* (UTC-12:00) International Date Line West */
910 : : "Dateline Standard Time", "Dateline Daylight Time",
911 : : "Etc/GMT+12"
912 : : },
913 : : {
914 : : /* (UTC+03:00) Nairobi */
915 : : "E. Africa Standard Time", "E. Africa Daylight Time",
916 : : "Africa/Nairobi"
917 : : },
918 : : {
919 : : /* (UTC+10:00) Brisbane */
920 : : "E. Australia Standard Time", "E. Australia Daylight Time",
921 : : "Australia/Brisbane"
922 : : },
923 : : {
924 : : /* (UTC+02:00) Chisinau */
925 : : "E. Europe Standard Time", "E. Europe Daylight Time",
926 : : "Europe/Chisinau"
927 : : },
928 : : {
929 : : /* (UTC-03:00) Brasilia */
930 : : "E. South America Standard Time", "E. South America Daylight Time",
931 : : "America/Sao_Paulo"
932 : : },
933 : : {
934 : : /* (UTC-06:00) Easter Island */
935 : : "Easter Island Standard Time", "Easter Island Daylight Time",
936 : : "Pacific/Easter"
937 : : },
938 : : {
939 : : /* (UTC-05:00) Eastern Time (US & Canada) */
940 : : "Eastern Standard Time", "Eastern Daylight Time",
941 : : "America/New_York"
942 : : },
943 : : {
944 : : /* (UTC-05:00) Chetumal */
945 : : "Eastern Standard Time (Mexico)", "Eastern Daylight Time (Mexico)",
946 : : "America/Cancun"
947 : : },
948 : : {
949 : : /* (UTC+02:00) Cairo */
950 : : "Egypt Standard Time", "Egypt Daylight Time",
951 : : "Africa/Cairo"
952 : : },
953 : : {
954 : : /* (UTC+05:00) Ekaterinburg */
955 : : "Ekaterinburg Standard Time", "Ekaterinburg Daylight Time",
956 : : "Asia/Yekaterinburg"
957 : : },
958 : : {
959 : : /* (UTC+12:00) Fiji */
960 : : "Fiji Standard Time", "Fiji Daylight Time",
961 : : "Pacific/Fiji"
962 : : },
963 : : {
964 : : /* (UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius */
965 : : "FLE Standard Time", "FLE Daylight Time",
966 : : "Europe/Kiev"
967 : : },
968 : : {
969 : : /* (UTC+04:00) Tbilisi */
970 : : "Georgian Standard Time", "Georgian Daylight Time",
971 : : "Asia/Tbilisi"
972 : : },
973 : : {
974 : : /* (UTC+00:00) Dublin, Edinburgh, Lisbon, London */
975 : : "GMT Standard Time", "GMT Daylight Time",
976 : : "Europe/London"
977 : : },
978 : : {
979 : : /* (UTC-03:00) Greenland */
980 : : "Greenland Standard Time", "Greenland Daylight Time",
981 : : "America/Godthab"
982 : : },
983 : : {
984 : : /* (UTC+00:00) Monrovia, Reykjavik */
985 : : "Greenwich Standard Time", "Greenwich Daylight Time",
986 : : "Atlantic/Reykjavik"
987 : : },
988 : : {
989 : : /* (UTC+02:00) Athens, Bucharest */
990 : : "GTB Standard Time", "GTB Daylight Time",
991 : : "Europe/Bucharest"
992 : : },
993 : : {
994 : : /* (UTC-05:00) Haiti */
995 : : "Haiti Standard Time", "Haiti Daylight Time",
996 : : "America/Port-au-Prince"
997 : : },
998 : : {
999 : : /* (UTC-10:00) Hawaii */
1000 : : "Hawaiian Standard Time", "Hawaiian Daylight Time",
1001 : : "Pacific/Honolulu"
1002 : : },
1003 : : {
1004 : : /* (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi */
1005 : : "India Standard Time", "India Daylight Time",
1006 : : "Asia/Calcutta"
1007 : : },
1008 : : {
1009 : : /* (UTC+03:30) Tehran */
1010 : : "Iran Standard Time", "Iran Daylight Time",
1011 : : "Asia/Tehran"
1012 : : },
1013 : : {
1014 : : /* (UTC+02:00) Jerusalem */
1015 : : "Israel Standard Time", "Israel Daylight Time",
1016 : : "Asia/Jerusalem"
1017 : : },
1018 : : {
1019 : : /* (UTC+02:00) Amman */
1020 : : "Jordan Standard Time", "Jordan Daylight Time",
1021 : : "Asia/Amman"
1022 : : },
1023 : : {
1024 : : /* (UTC+02:00) Kaliningrad */
1025 : : "Kaliningrad Standard Time", "Kaliningrad Daylight Time",
1026 : : "Europe/Kaliningrad"
1027 : : },
1028 : : {
1029 : : /* (UTC+09:00) Seoul */
1030 : : "Korea Standard Time", "Korea Daylight Time",
1031 : : "Asia/Seoul"
1032 : : },
1033 : : {
1034 : : /* (UTC+02:00) Tripoli */
1035 : : "Libya Standard Time", "Libya Daylight Time",
1036 : : "Africa/Tripoli"
1037 : : },
1038 : : {
1039 : : /* (UTC+14:00) Kiritimati Island */
1040 : : "Line Islands Standard Time", "Line Islands Daylight Time",
1041 : : "Pacific/Kiritimati"
1042 : : },
1043 : : {
1044 : : /* (UTC+10:30) Lord Howe Island */
1045 : : "Lord Howe Standard Time", "Lord Howe Daylight Time",
1046 : : "Australia/Lord_Howe"
1047 : : },
1048 : : {
1049 : : /* (UTC+11:00) Magadan */
1050 : : "Magadan Standard Time", "Magadan Daylight Time",
1051 : : "Asia/Magadan"
1052 : : },
1053 : : {
1054 : : /* (UTC-03:00) Punta Arenas */
1055 : : "Magallanes Standard Time", "Magallanes Daylight Time",
1056 : : "America/Punta_Arenas"
1057 : : },
1058 : : {
1059 : : /* (UTC-05:00) Manitoba */
1060 : : "Manitoba Standard Time", "Manitoba Daylight Time",
1061 : : "America/Winnipeg"
1062 : : },
1063 : : {
1064 : : /* (UTC-09:30) Marquesas Islands */
1065 : : "Marquesas Standard Time", "Marquesas Daylight Time",
1066 : : "Pacific/Marquesas"
1067 : : },
1068 : : {
1069 : : /* (UTC+04:00) Port Louis */
1070 : : "Mauritius Standard Time", "Mauritius Daylight Time",
1071 : : "Indian/Mauritius"
1072 : : },
1073 : : {
1074 : : /* (UTC+02:00) Beirut */
1075 : : "Middle East Standard Time", "Middle East Daylight Time",
1076 : : "Asia/Beirut"
1077 : : },
1078 : : {
1079 : : /* (UTC-03:00) Montevideo */
1080 : : "Montevideo Standard Time", "Montevideo Daylight Time",
1081 : : "America/Montevideo"
1082 : : },
1083 : : {
1084 : : /* (UTC+01:00) Casablanca */
1085 : : "Morocco Standard Time", "Morocco Daylight Time",
1086 : : "Africa/Casablanca"
1087 : : },
1088 : : {
1089 : : /* (UTC-07:00) Mountain Time (US & Canada) */
1090 : : "Mountain Standard Time", "Mountain Daylight Time",
1091 : : "America/Denver"
1092 : : },
1093 : : {
1094 : : /* (UTC-07:00) Chihuahua, La Paz, Mazatlan */
1095 : : "Mountain Standard Time (Mexico)", "Mountain Daylight Time (Mexico)",
1096 : : "America/Mazatlan"
1097 : : },
1098 : : {
1099 : : /* (UTC+06:30) Yangon (Rangoon) */
1100 : : "Myanmar Standard Time", "Myanmar Daylight Time",
1101 : : "Asia/Rangoon"
1102 : : },
1103 : : {
1104 : : /* (UTC+07:00) Novosibirsk */
1105 : : "N. Central Asia Standard Time", "N. Central Asia Daylight Time",
1106 : : "Asia/Novosibirsk"
1107 : : },
1108 : : {
1109 : : /* (UTC+02:00) Windhoek */
1110 : : "Namibia Standard Time", "Namibia Daylight Time",
1111 : : "Africa/Windhoek"
1112 : : },
1113 : : {
1114 : : /* (UTC+05:45) Kathmandu */
1115 : : "Nepal Standard Time", "Nepal Daylight Time",
1116 : : "Asia/Katmandu"
1117 : : },
1118 : : {
1119 : : /* (UTC+12:00) Auckland, Wellington */
1120 : : "New Zealand Standard Time", "New Zealand Daylight Time",
1121 : : "Pacific/Auckland"
1122 : : },
1123 : : {
1124 : : /* (UTC-03:30) Newfoundland */
1125 : : "Newfoundland Standard Time", "Newfoundland Daylight Time",
1126 : : "America/St_Johns"
1127 : : },
1128 : : {
1129 : : /* (UTC+11:00) Norfolk Island */
1130 : : "Norfolk Standard Time", "Norfolk Daylight Time",
1131 : : "Pacific/Norfolk"
1132 : : },
1133 : : {
1134 : : /* (UTC+08:00) Irkutsk */
1135 : : "North Asia East Standard Time", "North Asia East Daylight Time",
1136 : : "Asia/Irkutsk"
1137 : : },
1138 : : {
1139 : : /* (UTC+07:00) Krasnoyarsk */
1140 : : "North Asia Standard Time", "North Asia Daylight Time",
1141 : : "Asia/Krasnoyarsk"
1142 : : },
1143 : : {
1144 : : /* (UTC+09:00) Pyongyang */
1145 : : "North Korea Standard Time", "North Korea Daylight Time",
1146 : : "Asia/Pyongyang"
1147 : : },
1148 : : {
1149 : : /* (UTC+06:00) Omsk */
1150 : : "Omsk Standard Time", "Omsk Daylight Time",
1151 : : "Asia/Omsk"
1152 : : },
1153 : : {
1154 : : /* (UTC-04:00) Santiago */
1155 : : "Pacific SA Standard Time", "Pacific SA Daylight Time",
1156 : : "America/Santiago"
1157 : : },
1158 : : {
1159 : : /* (UTC-08:00) Pacific Time (US & Canada) */
1160 : : "Pacific Standard Time", "Pacific Daylight Time",
1161 : : "America/Los_Angeles"
1162 : : },
1163 : : {
1164 : : /* (UTC-08:00) Baja California */
1165 : : "Pacific Standard Time (Mexico)", "Pacific Daylight Time (Mexico)",
1166 : : "America/Tijuana"
1167 : : },
1168 : : {
1169 : : /* (UTC+05:00) Islamabad, Karachi */
1170 : : "Pakistan Standard Time", "Pakistan Daylight Time",
1171 : : "Asia/Karachi"
1172 : : },
1173 : : {
1174 : : /* (UTC-04:00) Asuncion */
1175 : : "Paraguay Standard Time", "Paraguay Daylight Time",
1176 : : "America/Asuncion"
1177 : : },
1178 : : {
1179 : : /* (UTC+05:00) Qyzylorda */
1180 : : "Qyzylorda Standard Time", "Qyzylorda Daylight Time",
1181 : : "Asia/Qyzylorda"
1182 : : },
1183 : : {
1184 : : /* (UTC+01:00) Brussels, Copenhagen, Madrid, Paris */
1185 : : "Romance Standard Time", "Romance Daylight Time",
1186 : : "Europe/Paris"
1187 : : },
1188 : : {
1189 : : /* (UTC+11:00) Chokurdakh */
1190 : : "Russia Time Zone 10", "Russia Time Zone 10",
1191 : : "Asia/Srednekolymsk"
1192 : : },
1193 : : {
1194 : : /* (UTC+12:00) Anadyr, Petropavlovsk-Kamchatsky */
1195 : : "Russia Time Zone 11", "Russia Time Zone 11",
1196 : : "Asia/Kamchatka"
1197 : : },
1198 : : {
1199 : : /* (UTC+04:00) Izhevsk, Samara */
1200 : : "Russia Time Zone 3", "Russia Time Zone 3",
1201 : : "Europe/Samara"
1202 : : },
1203 : : {
1204 : : /* (UTC+03:00) Moscow, St. Petersburg */
1205 : : "Russian Standard Time", "Russian Daylight Time",
1206 : : "Europe/Moscow"
1207 : : },
1208 : : {
1209 : : /* (UTC-03:00) Cayenne, Fortaleza */
1210 : : "SA Eastern Standard Time", "SA Eastern Daylight Time",
1211 : : "America/Cayenne"
1212 : : },
1213 : : {
1214 : : /* (UTC-05:00) Bogota, Lima, Quito, Rio Branco */
1215 : : "SA Pacific Standard Time", "SA Pacific Daylight Time",
1216 : : "America/Bogota"
1217 : : },
1218 : : {
1219 : : /* (UTC-04:00) Georgetown, La Paz, Manaus, San Juan */
1220 : : "SA Western Standard Time", "SA Western Daylight Time",
1221 : : "America/La_Paz"
1222 : : },
1223 : : {
1224 : : /* (UTC-03:00) Saint Pierre and Miquelon */
1225 : : "Saint Pierre Standard Time", "Saint Pierre Daylight Time",
1226 : : "America/Miquelon"
1227 : : },
1228 : : {
1229 : : /* (UTC+11:00) Sakhalin */
1230 : : "Sakhalin Standard Time", "Sakhalin Daylight Time",
1231 : : "Asia/Sakhalin"
1232 : : },
1233 : : {
1234 : : /* (UTC+13:00) Samoa */
1235 : : "Samoa Standard Time", "Samoa Daylight Time",
1236 : : "Pacific/Apia"
1237 : : },
1238 : : {
1239 : : /* (UTC+00:00) Sao Tome */
1240 : : "Sao Tome Standard Time", "Sao Tome Daylight Time",
1241 : : "Africa/Sao_Tome"
1242 : : },
1243 : : {
1244 : : /* (UTC+04:00) Saratov */
1245 : : "Saratov Standard Time", "Saratov Daylight Time",
1246 : : "Europe/Saratov"
1247 : : },
1248 : : {
1249 : : /* (UTC+07:00) Bangkok, Hanoi, Jakarta */
1250 : : "SE Asia Standard Time", "SE Asia Daylight Time",
1251 : : "Asia/Bangkok"
1252 : : },
1253 : : {
1254 : : /* (UTC+08:00) Kuala Lumpur, Singapore */
1255 : : "Singapore Standard Time", "Singapore Daylight Time",
1256 : : "Asia/Singapore"
1257 : : },
1258 : : {
1259 : : /* (UTC+02:00) Harare, Pretoria */
1260 : : "South Africa Standard Time", "South Africa Daylight Time",
1261 : : "Africa/Johannesburg"
1262 : : },
1263 : : {
1264 : : /* (UTC+02:00) Juba */
1265 : : "South Sudan Standard Time", "South Sudan Daylight Time",
1266 : : "Africa/Juba"
1267 : : },
1268 : : {
1269 : : /* (UTC+05:30) Sri Jayawardenepura */
1270 : : "Sri Lanka Standard Time", "Sri Lanka Daylight Time",
1271 : : "Asia/Colombo"
1272 : : },
1273 : : {
1274 : : /* (UTC+02:00) Khartoum */
1275 : : "Sudan Standard Time", "Sudan Daylight Time",
1276 : : "Africa/Khartoum"
1277 : : },
1278 : : {
1279 : : /* (UTC+02:00) Damascus */
1280 : : "Syria Standard Time", "Syria Daylight Time",
1281 : : "Asia/Damascus"
1282 : : },
1283 : : {
1284 : : /* (UTC+08:00) Taipei */
1285 : : "Taipei Standard Time", "Taipei Daylight Time",
1286 : : "Asia/Taipei"
1287 : : },
1288 : : {
1289 : : /* (UTC+10:00) Hobart */
1290 : : "Tasmania Standard Time", "Tasmania Daylight Time",
1291 : : "Australia/Hobart"
1292 : : },
1293 : : {
1294 : : /* (UTC-03:00) Araguaina */
1295 : : "Tocantins Standard Time", "Tocantins Daylight Time",
1296 : : "America/Araguaina"
1297 : : },
1298 : : {
1299 : : /* (UTC+09:00) Osaka, Sapporo, Tokyo */
1300 : : "Tokyo Standard Time", "Tokyo Daylight Time",
1301 : : "Asia/Tokyo"
1302 : : },
1303 : : {
1304 : : /* (UTC+07:00) Tomsk */
1305 : : "Tomsk Standard Time", "Tomsk Daylight Time",
1306 : : "Asia/Tomsk"
1307 : : },
1308 : : {
1309 : : /* (UTC+13:00) Nuku'alofa */
1310 : : "Tonga Standard Time", "Tonga Daylight Time",
1311 : : "Pacific/Tongatapu"
1312 : : },
1313 : : {
1314 : : /* (UTC+09:00) Chita */
1315 : : "Transbaikal Standard Time", "Transbaikal Daylight Time",
1316 : : "Asia/Chita"
1317 : : },
1318 : : {
1319 : : /* (UTC+03:00) Istanbul */
1320 : : "Turkey Standard Time", "Turkey Daylight Time",
1321 : : "Europe/Istanbul"
1322 : : },
1323 : : {
1324 : : /* (UTC-05:00) Turks and Caicos */
1325 : : "Turks And Caicos Standard Time", "Turks And Caicos Daylight Time",
1326 : : "America/Grand_Turk"
1327 : : },
1328 : : {
1329 : : /* (UTC+08:00) Ulaanbaatar */
1330 : : "Ulaanbaatar Standard Time", "Ulaanbaatar Daylight Time",
1331 : : "Asia/Ulaanbaatar"
1332 : : },
1333 : : {
1334 : : /* (UTC-05:00) Indiana (East) */
1335 : : "US Eastern Standard Time", "US Eastern Daylight Time",
1336 : : "America/Indianapolis"
1337 : : },
1338 : : {
1339 : : /* (UTC-07:00) Arizona */
1340 : : "US Mountain Standard Time", "US Mountain Daylight Time",
1341 : : "America/Phoenix"
1342 : : },
1343 : : {
1344 : : /* (UTC) Coordinated Universal Time */
1345 : : "UTC", "UTC",
1346 : : "Etc/UTC"
1347 : : },
1348 : : {
1349 : : /* (UTC+12:00) Coordinated Universal Time+12 */
1350 : : "UTC+12", "UTC+12",
1351 : : "Etc/GMT-12"
1352 : : },
1353 : : {
1354 : : /* (UTC+13:00) Coordinated Universal Time+13 */
1355 : : "UTC+13", "UTC+13",
1356 : : "Etc/GMT-13"
1357 : : },
1358 : : {
1359 : : /* (UTC-02:00) Coordinated Universal Time-02 */
1360 : : "UTC-02", "UTC-02",
1361 : : "Etc/GMT+2"
1362 : : },
1363 : : {
1364 : : /* (UTC-08:00) Coordinated Universal Time-08 */
1365 : : "UTC-08", "UTC-08",
1366 : : "Etc/GMT+8"
1367 : : },
1368 : : {
1369 : : /* (UTC-09:00) Coordinated Universal Time-09 */
1370 : : "UTC-09", "UTC-09",
1371 : : "Etc/GMT+9"
1372 : : },
1373 : : {
1374 : : /* (UTC-11:00) Coordinated Universal Time-11 */
1375 : : "UTC-11", "UTC-11",
1376 : : "Etc/GMT+11"
1377 : : },
1378 : : {
1379 : : /* (UTC-04:00) Caracas */
1380 : : "Venezuela Standard Time", "Venezuela Daylight Time",
1381 : : "America/Caracas"
1382 : : },
1383 : : {
1384 : : /* (UTC+10:00) Vladivostok */
1385 : : "Vladivostok Standard Time", "Vladivostok Daylight Time",
1386 : : "Asia/Vladivostok"
1387 : : },
1388 : : {
1389 : : /* (UTC+04:00) Volgograd */
1390 : : "Volgograd Standard Time", "Volgograd Daylight Time",
1391 : : "Europe/Volgograd"
1392 : : },
1393 : : {
1394 : : /* (UTC+08:00) Perth */
1395 : : "W. Australia Standard Time", "W. Australia Daylight Time",
1396 : : "Australia/Perth"
1397 : : },
1398 : : {
1399 : : /* (UTC+01:00) West Central Africa */
1400 : : "W. Central Africa Standard Time", "W. Central Africa Daylight Time",
1401 : : "Africa/Lagos"
1402 : : },
1403 : : {
1404 : : /* (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna */
1405 : : "W. Europe Standard Time", "W. Europe Daylight Time",
1406 : : "Europe/Berlin"
1407 : : },
1408 : : {
1409 : : /* (UTC+07:00) Hovd */
1410 : : "W. Mongolia Standard Time", "W. Mongolia Daylight Time",
1411 : : "Asia/Hovd"
1412 : : },
1413 : : {
1414 : : /* (UTC+05:00) Ashgabat, Tashkent */
1415 : : "West Asia Standard Time", "West Asia Daylight Time",
1416 : : "Asia/Tashkent"
1417 : : },
1418 : : {
1419 : : /* (UTC+02:00) Gaza, Hebron */
1420 : : "West Bank Standard Time", "West Bank Daylight Time",
1421 : : "Asia/Hebron"
1422 : : },
1423 : : {
1424 : : /* (UTC+10:00) Guam, Port Moresby */
1425 : : "West Pacific Standard Time", "West Pacific Daylight Time",
1426 : : "Pacific/Port_Moresby"
1427 : : },
1428 : : {
1429 : : /* (UTC+09:00) Yakutsk */
1430 : : "Yakutsk Standard Time", "Yakutsk Daylight Time",
1431 : : "Asia/Yakutsk"
1432 : : },
1433 : : {
1434 : : /* (UTC-07:00) Yukon */
1435 : : "Yukon Standard Time", "Yukon Daylight Time",
1436 : : "America/Whitehorse"
1437 : : },
1438 : :
1439 : : /*
1440 : : * END of CLDR-derived entries
1441 : : */
1442 : :
1443 : : /*
1444 : : * Obsolete zones, or just obsolete spellings of their names. These are no
1445 : : * longer derivable from CLDR, but should be kept to support older Windows
1446 : : * installations.
1447 : : */
1448 : :
1449 : : {
1450 : : /* (UTC+04:00) Baku, Tbilisi, Yerevan */
1451 : : "Armenian Standard Time", "Armenian Daylight Time",
1452 : : "Asia/Yerevan"
1453 : : },
1454 : : {
1455 : : /* (UTC-01:00) Cabo Verde Is. */
1456 : : "Cabo Verde Standard Time", "Cabo Verde Daylight Time",
1457 : : "Atlantic/Cape_Verde"
1458 : : },
1459 : : {
1460 : : /* (UTC) Coordinated Universal Time */
1461 : : "Coordinated Universal Time", "Coordinated Universal Time",
1462 : : "UTC"
1463 : : },
1464 : : {
1465 : : /* (UTC+02:00) Jerusalem */
1466 : : "Jerusalem Standard Time", "Jerusalem Daylight Time",
1467 : : "Asia/Jerusalem"
1468 : : },
1469 : : {
1470 : : /* (UTC+12:00) Petropavlovsk-Kamchatsky */
1471 : : "Kamchatka Standard Time", "Kamchatka Daylight Time",
1472 : : "Asia/Kamchatka"
1473 : : },
1474 : : {
1475 : : /* (UTC+08:00) Kuala Lumpur, Singapore */
1476 : : "Malay Peninsula Standard Time", "Malay Peninsula Daylight Time",
1477 : : "Asia/Kuala_Lumpur"
1478 : : },
1479 : : {
1480 : : /* (UTC-06:00) Guadalajara, Mexico City, Monterrey */
1481 : : "Mexico Standard Time", "Mexico Daylight Time",
1482 : : "America/Mexico_City"
1483 : : },
1484 : : {
1485 : : /* (UTC-07:00) Chihuahua, La Paz, Mazatlan */
1486 : : "Mexico Standard Time 2", "Mexico Daylight Time 2",
1487 : : "America/Chihuahua"
1488 : : },
1489 : : {
1490 : : /* (UTC-02:00) Mid-Atlantic */
1491 : : "Mid-Atlantic Standard Time", "Mid-Atlantic Daylight Time",
1492 : : "Atlantic/South_Georgia"
1493 : : },
1494 : : {
1495 : : /* (UTC+07:00) Novosibirsk */
1496 : : "Novosibirsk Standard Time", "Novosibirsk Daylight Time",
1497 : : "Asia/Novosibirsk"
1498 : : },
1499 : : {
1500 : : /* (UTC+02:00) Kaliningrad */
1501 : : "Russia TZ 1 Standard Time", "Russia TZ 1 Daylight Time",
1502 : : "Europe/Kaliningrad"
1503 : : },
1504 : : {
1505 : : /* (UTC+03:00) Moscow, St. Petersburg */
1506 : : "Russia TZ 2 Standard Time", "Russia TZ 2 Daylight Time",
1507 : : "Europe/Moscow"
1508 : : },
1509 : : {
1510 : : /* (UTC+04:00) Izhevsk, Samara */
1511 : : "Russia TZ 3 Standard Time", "Russia TZ 3 Daylight Time",
1512 : : "Europe/Samara"
1513 : : },
1514 : : {
1515 : : /* (UTC+05:00) Ekaterinburg */
1516 : : "Russia TZ 4 Standard Time", "Russia TZ 4 Daylight Time",
1517 : : "Asia/Yekaterinburg"
1518 : : },
1519 : : {
1520 : : /* (UTC+06:00) Novosibirsk (RTZ 5) */
1521 : : "Russia TZ 5 Standard Time", "Russia TZ 5 Daylight Time",
1522 : : "Asia/Novosibirsk"
1523 : : },
1524 : : {
1525 : : /* (UTC+07:00) Krasnoyarsk */
1526 : : "Russia TZ 6 Standard Time", "Russia TZ 6 Daylight Time",
1527 : : "Asia/Krasnoyarsk"
1528 : : },
1529 : : {
1530 : : /* (UTC+08:00) Irkutsk */
1531 : : "Russia TZ 7 Standard Time", "Russia TZ 7 Daylight Time",
1532 : : "Asia/Irkutsk"
1533 : : },
1534 : : {
1535 : : /* (UTC+09:00) Yakutsk */
1536 : : "Russia TZ 8 Standard Time", "Russia TZ 8 Daylight Time",
1537 : : "Asia/Yakutsk"
1538 : : },
1539 : : {
1540 : : /* (UTC+10:00) Vladivostok */
1541 : : "Russia TZ 9 Standard Time", "Russia TZ 9 Daylight Time",
1542 : : "Asia/Vladivostok"
1543 : : },
1544 : : {
1545 : : /* (UTC+11:00) Chokurdakh */
1546 : : "Russia TZ 10 Standard Time", "Russia TZ 10 Daylight Time",
1547 : : "Asia/Magadan"
1548 : : },
1549 : : {
1550 : : /* (UTC+12:00) Anadyr, Petropavlovsk-Kamchatsky */
1551 : : "Russia TZ 11 Standard Time", "Russia TZ 11 Daylight Time",
1552 : : "Asia/Anadyr"
1553 : : },
1554 : : {
1555 : : /* (UTC+02:00) Gaza, Hebron */
1556 : : "West Bank Gaza Standard Time", "West Bank Gaza Daylight Time",
1557 : : "Asia/Gaza"
1558 : : },
1559 : :
1560 : : {
1561 : : NULL, NULL, NULL
1562 : : }
1563 : : };
1564 : :
1565 : : static const char *
1566 : : identify_system_timezone(void)
1567 : : {
1568 : : int i;
1569 : : char tzname[128];
1570 : : char localtzname[256];
1571 : : time_t t = time(NULL);
1572 : : struct tm *tm = localtime(&t);
1573 : : HKEY rootKey;
1574 : : int idx;
1575 : :
1576 : : if (!tm)
1577 : : {
1578 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1579 : : fprintf(stderr, "could not identify system time zone: localtime() failed\n");
1580 : : #endif
1581 : : return NULL; /* go to GMT */
1582 : : }
1583 : :
1584 : : memset(tzname, 0, sizeof(tzname));
1585 : : strftime(tzname, sizeof(tzname) - 1, "%Z", tm);
1586 : :
1587 : : for (i = 0; win32_tzmap[i].stdname != NULL; i++)
1588 : : {
1589 : : if (strcmp(tzname, win32_tzmap[i].stdname) == 0 ||
1590 : : strcmp(tzname, win32_tzmap[i].dstname) == 0)
1591 : : {
1592 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1593 : : fprintf(stderr, "TZ \"%s\" matches system time zone \"%s\"\n",
1594 : : win32_tzmap[i].pgtzname, tzname);
1595 : : #endif
1596 : : return win32_tzmap[i].pgtzname;
1597 : : }
1598 : : }
1599 : :
1600 : : /*
1601 : : * Localized Windows versions return localized names for the timezone.
1602 : : * Scan the registry to find the English name, and then try matching
1603 : : * against our table again.
1604 : : */
1605 : : memset(localtzname, 0, sizeof(localtzname));
1606 : : if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
1607 : : "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones",
1608 : : 0,
1609 : : KEY_READ,
1610 : : &rootKey) != ERROR_SUCCESS)
1611 : : {
1612 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1613 : : fprintf(stderr, "could not open registry key to identify system time zone: error code %lu\n",
1614 : : GetLastError());
1615 : : #endif
1616 : : return NULL; /* go to GMT */
1617 : : }
1618 : :
1619 : : for (idx = 0;; idx++)
1620 : : {
1621 : : char keyname[256];
1622 : : char zonename[256];
1623 : : DWORD namesize;
1624 : : FILETIME lastwrite;
1625 : : HKEY key;
1626 : : LONG r;
1627 : :
1628 : : memset(keyname, 0, sizeof(keyname));
1629 : : namesize = sizeof(keyname);
1630 : : if ((r = RegEnumKeyEx(rootKey,
1631 : : idx,
1632 : : keyname,
1633 : : &namesize,
1634 : : NULL,
1635 : : NULL,
1636 : : NULL,
1637 : : &lastwrite)) != ERROR_SUCCESS)
1638 : : {
1639 : : if (r == ERROR_NO_MORE_ITEMS)
1640 : : break;
1641 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1642 : : fprintf(stderr, "could not enumerate registry subkeys to identify system time zone: %d\n",
1643 : : (int) r);
1644 : : #endif
1645 : : break;
1646 : : }
1647 : :
1648 : : if ((r = RegOpenKeyEx(rootKey, keyname, 0, KEY_READ, &key)) != ERROR_SUCCESS)
1649 : : {
1650 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1651 : : fprintf(stderr, "could not open registry subkey to identify system time zone: %d\n",
1652 : : (int) r);
1653 : : #endif
1654 : : break;
1655 : : }
1656 : :
1657 : : memset(zonename, 0, sizeof(zonename));
1658 : : namesize = sizeof(zonename);
1659 : : if ((r = RegQueryValueEx(key, "Std", NULL, NULL, (unsigned char *) zonename, &namesize)) != ERROR_SUCCESS)
1660 : : {
1661 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1662 : : fprintf(stderr, "could not query value for key \"std\" to identify system time zone \"%s\": %d\n",
1663 : : keyname, (int) r);
1664 : : #endif
1665 : : RegCloseKey(key);
1666 : : continue; /* Proceed to look at the next timezone */
1667 : : }
1668 : : if (strcmp(tzname, zonename) == 0)
1669 : : {
1670 : : /* Matched zone */
1671 : : strcpy(localtzname, keyname);
1672 : : RegCloseKey(key);
1673 : : break;
1674 : : }
1675 : : memset(zonename, 0, sizeof(zonename));
1676 : : namesize = sizeof(zonename);
1677 : : if ((r = RegQueryValueEx(key, "Dlt", NULL, NULL, (unsigned char *) zonename, &namesize)) != ERROR_SUCCESS)
1678 : : {
1679 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1680 : : fprintf(stderr, "could not query value for key \"dlt\" to identify system time zone \"%s\": %d\n",
1681 : : keyname, (int) r);
1682 : : #endif
1683 : : RegCloseKey(key);
1684 : : continue; /* Proceed to look at the next timezone */
1685 : : }
1686 : : if (strcmp(tzname, zonename) == 0)
1687 : : {
1688 : : /* Matched DST zone */
1689 : : strcpy(localtzname, keyname);
1690 : : RegCloseKey(key);
1691 : : break;
1692 : : }
1693 : :
1694 : : RegCloseKey(key);
1695 : : }
1696 : :
1697 : : RegCloseKey(rootKey);
1698 : :
1699 : : if (localtzname[0])
1700 : : {
1701 : : /* Found a localized name, so scan for that one too */
1702 : : for (i = 0; win32_tzmap[i].stdname != NULL; i++)
1703 : : {
1704 : : if (strcmp(localtzname, win32_tzmap[i].stdname) == 0 ||
1705 : : strcmp(localtzname, win32_tzmap[i].dstname) == 0)
1706 : : {
1707 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1708 : : fprintf(stderr, "TZ \"%s\" matches localized system time zone \"%s\" (\"%s\")\n",
1709 : : win32_tzmap[i].pgtzname, tzname, localtzname);
1710 : : #endif
1711 : : return win32_tzmap[i].pgtzname;
1712 : : }
1713 : : }
1714 : : }
1715 : :
1716 : : #ifdef DEBUG_IDENTIFY_TIMEZONE
1717 : : fprintf(stderr, "could not find a match for system time zone \"%s\"\n",
1718 : : tzname);
1719 : : #endif
1720 : : return NULL; /* go to GMT */
1721 : : }
1722 : : #endif /* WIN32 */
1723 : :
1724 : :
1725 : : /*
1726 : : * Return true if the given zone name is valid and is an "acceptable" zone.
1727 : : */
1728 : : static bool
5536 tgl@sss.pgh.pa.us 1729 :CBC 120 : validate_zone(const char *tzname)
1730 : : {
1731 : : pg_tz *tz;
1732 : :
1733 [ + + - + ]: 120 : if (!tzname || !tzname[0])
1734 : 60 : return false;
1735 : :
1736 : 60 : tz = pg_load_tz(tzname);
1737 [ - + ]: 60 : if (!tz)
5536 tgl@sss.pgh.pa.us 1738 :UBC 0 : return false;
1739 : :
5536 tgl@sss.pgh.pa.us 1740 [ - + ]:CBC 60 : if (!pg_tz_acceptable(tz))
5536 tgl@sss.pgh.pa.us 1741 :UBC 0 : return false;
1742 : :
5536 tgl@sss.pgh.pa.us 1743 :CBC 60 : return true;
1744 : : }
1745 : :
1746 : : /*
1747 : : * Identify a suitable default timezone setting based on the environment.
1748 : : *
1749 : : * The installation share_path must be passed in, as that is the default
1750 : : * location for the timezone database directory.
1751 : : *
1752 : : * We first look to the TZ environment variable. If not found or not
1753 : : * recognized by our own code, we see if we can identify the timezone
1754 : : * from the behavior of the system timezone library. When all else fails,
1755 : : * return NULL, indicating that we should default to GMT.
1756 : : */
1757 : : const char *
1758 : 60 : select_default_timezone(const char *share_path)
1759 : : {
1760 : : const char *tzname;
1761 : :
1762 : : /* Initialize timezone directory path, if needed */
1763 : : #ifndef SYSTEMTZDIR
1764 : 60 : snprintf(tzdirpath, sizeof(tzdirpath), "%s/timezone", share_path);
1765 : : #endif
1766 : :
1767 : : /* Check TZ environment variable */
1768 : 60 : tzname = getenv("TZ");
1769 [ - + ]: 60 : if (validate_zone(tzname))
5536 tgl@sss.pgh.pa.us 1770 :UBC 0 : return tzname;
1771 : :
1772 : : /* Nope, so try to identify the system timezone */
5536 tgl@sss.pgh.pa.us 1773 :CBC 60 : tzname = identify_system_timezone();
1774 [ + - ]: 60 : if (validate_zone(tzname))
1775 : 60 : return tzname;
1776 : :
5536 tgl@sss.pgh.pa.us 1777 :UBC 0 : return NULL;
1778 : : }
|