Line data Source code
1 : /* Convert timestamp from pg_time_t to struct pg_tm. */
2 :
3 : /*
4 : * This file is in the public domain, so clarified as of
5 : * 1996-06-05 by Arthur David Olson.
6 : *
7 : * IDENTIFICATION
8 : * src/timezone/localtime.c
9 : */
10 :
11 : /*
12 : * Leap second handling from Bradley White.
13 : * POSIX-style TZ environment variable handling from Guy Harris.
14 : */
15 :
16 : /* this file needs to build in both frontend and backend contexts */
17 : #include "c.h"
18 :
19 : #include <fcntl.h>
20 :
21 : #include "datatype/timestamp.h"
22 : #include "pgtz.h"
23 :
24 : #include "private.h"
25 : #include "tzfile.h"
26 :
27 :
28 : #ifndef WILDABBR
29 : /*
30 : * Someone might make incorrect use of a time zone abbreviation:
31 : * 1. They might reference tzname[0] before calling tzset (explicitly
32 : * or implicitly).
33 : * 2. They might reference tzname[1] before calling tzset (explicitly
34 : * or implicitly).
35 : * 3. They might reference tzname[1] after setting to a time zone
36 : * in which Daylight Saving Time is never observed.
37 : * 4. They might reference tzname[0] after setting to a time zone
38 : * in which Standard Time is never observed.
39 : * 5. They might reference tm.tm_zone after calling offtime.
40 : * What's best to do in the above cases is open to debate;
41 : * for now, we just set things up so that in any of the five cases
42 : * WILDABBR is used. Another possibility: initialize tzname[0] to the
43 : * string "tzname[0] used before set", and similarly for the other cases.
44 : * And another: initialize tzname[0] to "ERA", with an explanation in the
45 : * manual page of what this "time zone abbreviation" means (doing this so
46 : * that tzname[0] has the "normal" length of three characters).
47 : */
48 : #define WILDABBR " "
49 : #endif /* !defined WILDABBR */
50 :
51 : static const char wildabbr[] = WILDABBR;
52 :
53 : static const char gmt[] = "GMT";
54 :
55 : /*
56 : * The DST rules to use if a POSIX TZ string has no rules.
57 : * Default to US rules as of 2017-05-07.
58 : * POSIX does not specify the default DST rules;
59 : * for historical reasons, US rules are a common default.
60 : */
61 : #define TZDEFRULESTRING ",M3.2.0,M11.1.0"
62 :
63 : /* structs ttinfo, lsinfo, state have been moved to pgtz.h */
64 :
65 : enum r_type
66 : {
67 : JULIAN_DAY, /* Jn = Julian day */
68 : DAY_OF_YEAR, /* n = day of year */
69 : MONTH_NTH_DAY_OF_WEEK, /* Mm.n.d = month, week, day of week */
70 : };
71 :
72 : struct rule
73 : {
74 : enum r_type r_type; /* type of rule */
75 : int r_day; /* day number of rule */
76 : int r_week; /* week number of rule */
77 : int r_mon; /* month number of rule */
78 : int32 r_time; /* transition time of rule */
79 : };
80 :
81 : /*
82 : * Prototypes for static functions.
83 : */
84 :
85 : static struct pg_tm *gmtsub(pg_time_t const *timep, int32 offset,
86 : struct pg_tm *tmp);
87 : static bool increment_overflow(int *ip, int j);
88 : static bool increment_overflow_time(pg_time_t *tp, int32 j);
89 : static int64 leapcorr(struct state const *sp, pg_time_t t);
90 : static struct pg_tm *timesub(pg_time_t const *timep,
91 : int32 offset, struct state const *sp,
92 : struct pg_tm *tmp);
93 : static bool typesequiv(struct state const *sp, int a, int b);
94 :
95 :
96 : /*
97 : * Section 4.12.3 of X3.159-1989 requires that
98 : * Except for the strftime function, these functions [asctime,
99 : * ctime, gmtime, localtime] return values in one of two static
100 : * objects: a broken-down time structure and an array of char.
101 : * Thanks to Paul Eggert for noting this.
102 : */
103 :
104 : static struct pg_tm tm;
105 :
106 : /* Initialize *S to a value based on UTOFF, ISDST, and DESIGIDX. */
107 : static void
108 31620 : init_ttinfo(struct ttinfo *s, int32 utoff, bool isdst, int desigidx)
109 : {
110 31620 : s->tt_utoff = utoff;
111 31620 : s->tt_isdst = isdst;
112 31620 : s->tt_desigidx = desigidx;
113 31620 : s->tt_ttisstd = false;
114 31620 : s->tt_ttisut = false;
115 31620 : }
116 :
117 : static int32
118 328176 : detzcode(const char *const codep)
119 : {
120 : int32 result;
121 : int i;
122 328176 : int32 one = 1;
123 328176 : int32 halfmaxval = one << (32 - 2);
124 328176 : int32 maxval = halfmaxval - 1 + halfmaxval;
125 328176 : int32 minval = -1 - maxval;
126 :
127 328176 : result = codep[0] & 0x7f;
128 1312704 : for (i = 1; i < 4; ++i)
129 984528 : result = (result << 8) | (codep[i] & 0xff);
130 :
131 328176 : if (codep[0] & 0x80)
132 : {
133 : /*
134 : * Do two's-complement negation even on non-two's-complement machines.
135 : * If the result would be minval - 1, return minval.
136 : */
137 54852 : result -= !TWOS_COMPLEMENT(int32) && result != 0;
138 54852 : result += minval;
139 : }
140 328176 : return result;
141 : }
142 :
143 : static int64
144 1339632 : detzcode64(const char *const codep)
145 : {
146 : uint64 result;
147 : int i;
148 1339632 : int64 one = 1;
149 1339632 : int64 halfmaxval = one << (64 - 2);
150 1339632 : int64 maxval = halfmaxval - 1 + halfmaxval;
151 1339632 : int64 minval = -TWOS_COMPLEMENT(int64) - maxval;
152 :
153 1339632 : result = codep[0] & 0x7f;
154 10717056 : for (i = 1; i < 8; ++i)
155 9377424 : result = (result << 8) | (codep[i] & 0xff);
156 :
157 1339632 : if (codep[0] & 0x80)
158 : {
159 : /*
160 : * Do two's-complement negation even on non-two's-complement machines.
161 : * If the result would be minval - 1, return minval.
162 : */
163 515202 : result -= !TWOS_COMPLEMENT(int64) && result != 0;
164 515202 : result += minval;
165 : }
166 1339632 : return result;
167 : }
168 :
169 : static bool
170 11108526 : differ_by_repeat(const pg_time_t t1, const pg_time_t t0)
171 : {
172 : if (TYPE_BIT(pg_time_t) - TYPE_SIGNED(pg_time_t) < SECSPERREPEAT_BITS)
173 : return 0;
174 11108526 : return t1 - t0 == SECSPERREPEAT;
175 : }
176 :
177 : /* Input buffer for data read from a compiled tz file. */
178 : union input_buffer
179 : {
180 : /* The first part of the buffer, interpreted as a header. */
181 : struct tzhead tzhead;
182 :
183 : /* The entire buffer. */
184 : char buf[2 * sizeof(struct tzhead) + 2 * sizeof(struct state)
185 : + 4 * TZ_MAX_TIMES];
186 : };
187 :
188 : /* Local storage needed for 'tzloadbody'. */
189 : union local_storage
190 : {
191 : /* The results of analyzing the file's contents after it is opened. */
192 : struct file_analysis
193 : {
194 : /* The input buffer. */
195 : union input_buffer u;
196 :
197 : /* A temporary state used for parsing a TZ string in the file. */
198 : struct state st;
199 : } u;
200 :
201 : /* We don't need the "fullname" member */
202 : };
203 :
204 : /* Load tz data from the file named NAME into *SP. Read extended
205 : * format if DOEXTEND. Use *LSP for temporary storage. Return 0 on
206 : * success, an errno value on failure.
207 : * PG: If "canonname" is not NULL, then on success the canonical spelling of
208 : * given name is stored there (the buffer must be > TZ_STRLEN_MAX bytes!).
209 : */
210 : static int
211 19452 : tzloadbody(char const *name, char *canonname, struct state *sp, bool doextend,
212 : union local_storage *lsp)
213 : {
214 : int i;
215 : int fid;
216 : int stored;
217 : ssize_t nread;
218 19452 : union input_buffer *up = &lsp->u.u;
219 19452 : int tzheadsize = sizeof(struct tzhead);
220 :
221 19452 : sp->goback = sp->goahead = false;
222 :
223 19452 : if (!name)
224 : {
225 0 : name = TZDEFAULT;
226 0 : if (!name)
227 0 : return EINVAL;
228 : }
229 :
230 19452 : if (name[0] == ':')
231 0 : ++name;
232 :
233 19452 : fid = pg_open_tzfile(name, canonname);
234 19452 : if (fid < 0)
235 460 : return ENOENT; /* pg_open_tzfile may not set errno */
236 :
237 18992 : nread = read(fid, up->buf, sizeof up->buf);
238 18992 : if (nread < tzheadsize)
239 : {
240 0 : int err = nread < 0 ? errno : EINVAL;
241 :
242 0 : close(fid);
243 0 : return err;
244 : }
245 18992 : if (close(fid) < 0)
246 0 : return errno;
247 56976 : for (stored = 4; stored <= 8; stored *= 2)
248 : {
249 37984 : int32 ttisstdcnt = detzcode(up->tzhead.tzh_ttisstdcnt);
250 37984 : int32 ttisutcnt = detzcode(up->tzhead.tzh_ttisutcnt);
251 37984 : int64 prevtr = 0;
252 37984 : int32 prevcorr = 0;
253 37984 : int32 leapcnt = detzcode(up->tzhead.tzh_leapcnt);
254 37984 : int32 timecnt = detzcode(up->tzhead.tzh_timecnt);
255 37984 : int32 typecnt = detzcode(up->tzhead.tzh_typecnt);
256 37984 : int32 charcnt = detzcode(up->tzhead.tzh_charcnt);
257 37984 : char const *p = up->buf + tzheadsize;
258 :
259 : /*
260 : * Although tzfile(5) currently requires typecnt to be nonzero,
261 : * support future formats that may allow zero typecnt in files that
262 : * have a TZ string and no transitions.
263 : */
264 75968 : if (!(0 <= leapcnt && leapcnt < TZ_MAX_LEAPS
265 37984 : && 0 <= typecnt && typecnt < TZ_MAX_TYPES
266 37984 : && 0 <= timecnt && timecnt < TZ_MAX_TIMES
267 37984 : && 0 <= charcnt && charcnt < TZ_MAX_CHARS
268 37984 : && (ttisstdcnt == typecnt || ttisstdcnt == 0)
269 37984 : && (ttisutcnt == typecnt || ttisutcnt == 0)))
270 0 : return EINVAL;
271 37984 : if (nread
272 : < (tzheadsize /* struct tzhead */
273 37984 : + timecnt * stored /* ats */
274 37984 : + timecnt /* types */
275 37984 : + typecnt * 6 /* ttinfos */
276 37984 : + charcnt /* chars */
277 37984 : + leapcnt * (stored + 4) /* lsinfos */
278 37984 : + ttisstdcnt /* ttisstds */
279 37984 : + ttisutcnt)) /* ttisuts */
280 0 : return EINVAL;
281 37984 : sp->leapcnt = leapcnt;
282 37984 : sp->timecnt = timecnt;
283 37984 : sp->typecnt = typecnt;
284 37984 : sp->charcnt = charcnt;
285 :
286 : /*
287 : * Read transitions, discarding those out of pg_time_t range. But
288 : * pretend the last transition before TIME_T_MIN occurred at
289 : * TIME_T_MIN.
290 : */
291 37984 : timecnt = 0;
292 1377616 : for (i = 0; i < sp->timecnt; ++i)
293 : {
294 1339632 : int64 at
295 1339632 : = stored == 4 ? detzcode(p) : detzcode64(p);
296 :
297 1339632 : sp->types[i] = at <= TIME_T_MAX;
298 1339632 : if (sp->types[i])
299 : {
300 1339632 : pg_time_t attime
301 : = ((TYPE_SIGNED(pg_time_t) ? at < TIME_T_MIN : at < 0)
302 : ? TIME_T_MIN : at);
303 :
304 1339632 : if (timecnt && attime <= sp->ats[timecnt - 1])
305 : {
306 0 : if (attime < sp->ats[timecnt - 1])
307 0 : return EINVAL;
308 0 : sp->types[i - 1] = 0;
309 0 : timecnt--;
310 : }
311 1339632 : sp->ats[timecnt++] = attime;
312 : }
313 1339632 : p += stored;
314 : }
315 :
316 37984 : timecnt = 0;
317 1377616 : for (i = 0; i < sp->timecnt; ++i)
318 : {
319 1339632 : unsigned char typ = *p++;
320 :
321 1339632 : if (sp->typecnt <= typ)
322 0 : return EINVAL;
323 1339632 : if (sp->types[i])
324 1339632 : sp->types[timecnt++] = typ;
325 : }
326 37984 : sp->timecnt = timecnt;
327 138256 : for (i = 0; i < sp->typecnt; ++i)
328 : {
329 : struct ttinfo *ttisp;
330 : unsigned char isdst,
331 : desigidx;
332 :
333 100272 : ttisp = &sp->ttis[i];
334 100272 : ttisp->tt_utoff = detzcode(p);
335 100272 : p += 4;
336 100272 : isdst = *p++;
337 100272 : if (!(isdst < 2))
338 0 : return EINVAL;
339 100272 : ttisp->tt_isdst = isdst;
340 100272 : desigidx = *p++;
341 100272 : if (!(desigidx < sp->charcnt))
342 0 : return EINVAL;
343 100272 : ttisp->tt_desigidx = desigidx;
344 : }
345 376384 : for (i = 0; i < sp->charcnt; ++i)
346 338400 : sp->chars[i] = *p++;
347 37984 : sp->chars[i] = '\0'; /* ensure '\0' at end */
348 :
349 : /* Read leap seconds, discarding those out of pg_time_t range. */
350 37984 : leapcnt = 0;
351 37984 : for (i = 0; i < sp->leapcnt; ++i)
352 : {
353 0 : int64 tr = stored == 4 ? detzcode(p) : detzcode64(p);
354 0 : int32 corr = detzcode(p + stored);
355 :
356 0 : p += stored + 4;
357 : /* Leap seconds cannot occur before the Epoch. */
358 0 : if (tr < 0)
359 0 : return EINVAL;
360 : if (tr <= TIME_T_MAX)
361 : {
362 : /*
363 : * Leap seconds cannot occur more than once per UTC month, and
364 : * UTC months are at least 28 days long (minus 1 second for a
365 : * negative leap second). Each leap second's correction must
366 : * differ from the previous one's by 1 second.
367 : */
368 0 : if (tr - prevtr < 28 * SECSPERDAY - 1
369 0 : || (corr != prevcorr - 1 && corr != prevcorr + 1))
370 0 : return EINVAL;
371 0 : sp->lsis[leapcnt].ls_trans = prevtr = tr;
372 0 : sp->lsis[leapcnt].ls_corr = prevcorr = corr;
373 0 : leapcnt++;
374 : }
375 : }
376 37984 : sp->leapcnt = leapcnt;
377 :
378 138256 : for (i = 0; i < sp->typecnt; ++i)
379 : {
380 : struct ttinfo *ttisp;
381 :
382 100272 : ttisp = &sp->ttis[i];
383 100272 : if (ttisstdcnt == 0)
384 100272 : ttisp->tt_ttisstd = false;
385 : else
386 : {
387 0 : if (*p != true && *p != false)
388 0 : return EINVAL;
389 0 : ttisp->tt_ttisstd = *p++;
390 : }
391 : }
392 138256 : for (i = 0; i < sp->typecnt; ++i)
393 : {
394 : struct ttinfo *ttisp;
395 :
396 100272 : ttisp = &sp->ttis[i];
397 100272 : if (ttisutcnt == 0)
398 100272 : ttisp->tt_ttisut = false;
399 : else
400 : {
401 0 : if (*p != true && *p != false)
402 0 : return EINVAL;
403 0 : ttisp->tt_ttisut = *p++;
404 : }
405 : }
406 :
407 : /*
408 : * If this is an old file, we're done.
409 : */
410 37984 : if (up->tzhead.tzh_version[0] == '\0')
411 0 : break;
412 37984 : nread -= p - up->buf;
413 37984 : memmove(up->buf, p, nread);
414 : }
415 18992 : if (doextend && nread > 2 &&
416 18992 : up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
417 18992 : sp->typecnt + 2 <= TZ_MAX_TYPES)
418 : {
419 18992 : struct state *ts = &lsp->u.st;
420 :
421 18992 : up->buf[nread - 1] = '\0';
422 18992 : if (tzparse(&up->buf[1], ts, false))
423 : {
424 : /*
425 : * Attempt to reuse existing abbreviations. Without this,
426 : * America/Anchorage would be right on the edge after 2037 when
427 : * TZ_MAX_CHARS is 50, as sp->charcnt equals 40 (for LMT AST AWT
428 : * APT AHST AHDT YST AKDT AKST) and ts->charcnt equals 10 (for
429 : * AKST AKDT). Reusing means sp->charcnt can stay 40 in this
430 : * example.
431 : */
432 18992 : int gotabbr = 0;
433 18992 : int charcnt = sp->charcnt;
434 :
435 48252 : for (i = 0; i < ts->typecnt; i++)
436 : {
437 29260 : char *tsabbr = ts->chars + ts->ttis[i].tt_desigidx;
438 : int j;
439 :
440 244448 : for (j = 0; j < charcnt; j++)
441 244384 : if (strcmp(sp->chars + j, tsabbr) == 0)
442 : {
443 29196 : ts->ttis[i].tt_desigidx = j;
444 29196 : gotabbr++;
445 29196 : break;
446 : }
447 29260 : if (!(j < charcnt))
448 : {
449 64 : int tsabbrlen = strlen(tsabbr);
450 :
451 64 : if (j + tsabbrlen < TZ_MAX_CHARS)
452 : {
453 64 : strcpy(sp->chars + j, tsabbr);
454 64 : charcnt = j + tsabbrlen + 1;
455 64 : ts->ttis[i].tt_desigidx = j;
456 64 : gotabbr++;
457 : }
458 : }
459 : }
460 18992 : if (gotabbr == ts->typecnt)
461 : {
462 18992 : sp->charcnt = charcnt;
463 :
464 : /*
465 : * Ignore any trailing, no-op transitions generated by zic as
466 : * they don't help here and can run afoul of bugs in zic 2016j
467 : * or earlier.
468 : */
469 19024 : while (1 < sp->timecnt
470 15442 : && (sp->types[sp->timecnt - 1]
471 15442 : == sp->types[sp->timecnt - 2]))
472 32 : sp->timecnt--;
473 :
474 4885978 : for (i = 0; i < ts->timecnt; i++)
475 4877254 : if (sp->timecnt == 0
476 9754508 : || (sp->ats[sp->timecnt - 1]
477 4877254 : < ts->ats[i] + leapcorr(sp, ts->ats[i])))
478 : break;
479 15688006 : while (i < ts->timecnt
480 15669014 : && sp->timecnt < TZ_MAX_TIMES)
481 : {
482 15669014 : sp->ats[sp->timecnt]
483 15669014 : = ts->ats[i] + leapcorr(sp, ts->ats[i]);
484 15669014 : sp->types[sp->timecnt] = (sp->typecnt
485 15669014 : + ts->types[i]);
486 15669014 : sp->timecnt++;
487 15669014 : i++;
488 : }
489 48252 : for (i = 0; i < ts->typecnt; i++)
490 29260 : sp->ttis[sp->typecnt++] = ts->ttis[i];
491 : }
492 : }
493 : }
494 18992 : if (sp->typecnt == 0)
495 0 : return EINVAL;
496 18992 : if (sp->timecnt > 1)
497 : {
498 17007894 : for (i = 1; i < sp->timecnt; ++i)
499 23945410 : if (typesequiv(sp, sp->types[i], sp->types[0]) &&
500 6952926 : differ_by_repeat(sp->ats[i], sp->ats[0]))
501 : {
502 0 : sp->goback = true;
503 0 : break;
504 : }
505 8379384 : for (i = sp->timecnt - 2; i >= 0; --i)
506 8374242 : if (typesequiv(sp, sp->types[sp->timecnt - 1],
507 12529842 : sp->types[i]) &&
508 4155600 : differ_by_repeat(sp->ats[sp->timecnt - 1],
509 : sp->ats[i]))
510 : {
511 10268 : sp->goahead = true;
512 10268 : break;
513 : }
514 : }
515 :
516 : /*
517 : * Infer sp->defaulttype from the data. Although this default type is
518 : * always zero for data from recent tzdb releases, things are trickier for
519 : * data from tzdb 2018e or earlier.
520 : *
521 : * The first set of heuristics work around bugs in 32-bit data generated
522 : * by tzdb 2013c or earlier. The workaround is for zones like
523 : * Australia/Macquarie where timestamps before the first transition have a
524 : * time type that is not the earliest standard-time type. See:
525 : * https://mm.icann.org/pipermail/tz/2013-May/019368.html
526 : */
527 :
528 : /*
529 : * If type 0 is unused in transitions, it's the type to use for early
530 : * times.
531 : */
532 16920194 : for (i = 0; i < sp->timecnt; ++i)
533 16901464 : if (sp->types[i] == 0)
534 262 : break;
535 18992 : i = i < sp->timecnt ? -1 : 0;
536 :
537 : /*
538 : * Absent the above, if there are transition times and the first
539 : * transition is to a daylight time find the standard type less than and
540 : * closest to the type of the first transition.
541 : */
542 18992 : if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst)
543 : {
544 0 : i = sp->types[0];
545 0 : while (--i >= 0)
546 0 : if (!sp->ttis[i].tt_isdst)
547 0 : break;
548 : }
549 :
550 : /*
551 : * The next heuristics are for data generated by tzdb 2018e or earlier,
552 : * for zones like EST5EDT where the first transition is to DST.
553 : */
554 :
555 : /*
556 : * If no result yet, find the first standard type. If there is none, punt
557 : * to type zero.
558 : */
559 18992 : if (i < 0)
560 : {
561 262 : i = 0;
562 262 : while (sp->ttis[i].tt_isdst)
563 0 : if (++i >= sp->typecnt)
564 : {
565 0 : i = 0;
566 0 : break;
567 : }
568 : }
569 :
570 : /*
571 : * A simple 'sp->defaulttype = 0;' would suffice here if we didn't have to
572 : * worry about 2018e-or-earlier data. Even simpler would be to remove the
573 : * defaulttype member and just use 0 in its place.
574 : */
575 18992 : sp->defaulttype = i;
576 :
577 18992 : return 0;
578 : }
579 :
580 : /* Load tz data from the file named NAME into *SP. Read extended
581 : * format if DOEXTEND. Return 0 on success, an errno value on failure.
582 : * PG: If "canonname" is not NULL, then on success the canonical spelling of
583 : * given name is stored there (the buffer must be > TZ_STRLEN_MAX bytes!).
584 : */
585 : int
586 19452 : tzload(const char *name, char *canonname, struct state *sp, bool doextend)
587 : {
588 19452 : union local_storage *lsp = malloc(sizeof *lsp);
589 :
590 19452 : if (!lsp)
591 0 : return errno;
592 : else
593 : {
594 19452 : int err = tzloadbody(name, canonname, sp, doextend, lsp);
595 :
596 19452 : free(lsp);
597 19452 : return err;
598 : }
599 : }
600 :
601 : static bool
602 25366726 : typesequiv(const struct state *sp, int a, int b)
603 : {
604 : bool result;
605 :
606 25366726 : if (sp == NULL ||
607 25366726 : a < 0 || a >= sp->typecnt ||
608 25366726 : b < 0 || b >= sp->typecnt)
609 0 : result = false;
610 : else
611 : {
612 25366726 : const struct ttinfo *ap = &sp->ttis[a];
613 25366726 : const struct ttinfo *bp = &sp->ttis[b];
614 :
615 25366726 : result = (ap->tt_utoff == bp->tt_utoff
616 11214148 : && ap->tt_isdst == bp->tt_isdst
617 11136148 : && ap->tt_ttisstd == bp->tt_ttisstd
618 11136148 : && ap->tt_ttisut == bp->tt_ttisut
619 36580874 : && (strcmp(&sp->chars[ap->tt_desigidx],
620 11136148 : &sp->chars[bp->tt_desigidx])
621 : == 0));
622 : }
623 25366726 : return result;
624 : }
625 :
626 : static const int mon_lengths[2][MONSPERYEAR] = {
627 : {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
628 : {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
629 : };
630 :
631 : static const int year_lengths[2] = {
632 : DAYSPERNYEAR, DAYSPERLYEAR
633 : };
634 :
635 : /*
636 : * Given a pointer into a timezone string, scan until a character that is not
637 : * a valid character in a time zone abbreviation is found.
638 : * Return a pointer to that character.
639 : */
640 :
641 : static const char *
642 25486 : getzname(const char *strp)
643 : {
644 : char c;
645 :
646 104786 : while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
647 : c != '+')
648 79300 : ++strp;
649 25486 : return strp;
650 : }
651 :
652 : /*
653 : * Given a pointer into an extended timezone string, scan until the ending
654 : * delimiter of the time zone abbreviation is located.
655 : * Return a pointer to the delimiter.
656 : *
657 : * As with getzname above, the legal character set is actually quite
658 : * restricted, with other characters producing undefined results.
659 : * We don't do any checking here; checking is done later in common-case code.
660 : */
661 :
662 : static const char *
663 4254 : getqzname(const char *strp, const int delim)
664 : {
665 : int c;
666 :
667 17638 : while ((c = *strp) != '\0' && c != delim)
668 13384 : ++strp;
669 4254 : return strp;
670 : }
671 :
672 : /*
673 : * Given a pointer into a timezone string, extract a number from that string.
674 : * Check that the number is within a specified range; if it is not, return
675 : * NULL.
676 : * Otherwise, return a pointer to the first character not part of the number.
677 : */
678 :
679 : static const char *
680 84182 : getnum(const char *strp, int *const nump, const int min, const int max)
681 : {
682 : char c;
683 : int num;
684 :
685 84182 : if (strp == NULL || !is_digit(c = *strp))
686 0 : return NULL;
687 84182 : num = 0;
688 : do
689 : {
690 96742 : num = num * 10 + (c - '0');
691 96742 : if (num > max)
692 0 : return NULL; /* illegal value */
693 96742 : c = *++strp;
694 96742 : } while (is_digit(c));
695 84182 : if (num < min)
696 0 : return NULL; /* illegal value */
697 84182 : *nump = num;
698 84182 : return strp;
699 : }
700 :
701 : /*
702 : * Given a pointer into a timezone string, extract a number of seconds,
703 : * in hh[:mm[:ss]] form, from the string.
704 : * If any error occurs, return NULL.
705 : * Otherwise, return a pointer to the first character not part of the number
706 : * of seconds.
707 : */
708 :
709 : static const char *
710 21876 : getsecs(const char *strp, int32 *const secsp)
711 : {
712 : int num;
713 :
714 : /*
715 : * 'HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
716 : * "M10.4.6/26", which does not conform to Posix, but which specifies the
717 : * equivalent of "02:00 on the first Sunday on or after 23 Oct".
718 : */
719 21876 : strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
720 21876 : if (strp == NULL)
721 0 : return NULL;
722 21876 : *secsp = num * (int32) SECSPERHOUR;
723 21876 : if (*strp == ':')
724 : {
725 578 : ++strp;
726 578 : strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
727 578 : if (strp == NULL)
728 0 : return NULL;
729 578 : *secsp += num * SECSPERMIN;
730 578 : if (*strp == ':')
731 : {
732 0 : ++strp;
733 : /* 'SECSPERMIN' allows for leap seconds. */
734 0 : strp = getnum(strp, &num, 0, SECSPERMIN);
735 0 : if (strp == NULL)
736 0 : return NULL;
737 0 : *secsp += num;
738 : }
739 : }
740 21876 : return strp;
741 : }
742 :
743 : /*
744 : * Given a pointer into a timezone string, extract an offset, in
745 : * [+-]hh[:mm[:ss]] form, from the string.
746 : * If any error occurs, return NULL.
747 : * Otherwise, return a pointer to the first character not part of the time.
748 : */
749 :
750 : static const char *
751 21876 : getoffset(const char *strp, int32 *const offsetp)
752 : {
753 21876 : bool neg = false;
754 :
755 21876 : if (*strp == '-')
756 : {
757 5408 : neg = true;
758 5408 : ++strp;
759 : }
760 16468 : else if (*strp == '+')
761 140 : ++strp;
762 21876 : strp = getsecs(strp, offsetp);
763 21876 : if (strp == NULL)
764 0 : return NULL; /* illegal time */
765 21876 : if (neg)
766 5408 : *offsetp = -*offsetp;
767 21876 : return strp;
768 : }
769 :
770 : /*
771 : * Given a pointer into a timezone string, extract a rule in the form
772 : * date[/time]. See POSIX section 8 for the format of "date" and "time".
773 : * If a valid rule is not found, return NULL.
774 : * Otherwise, return a pointer to the first character not part of the rule.
775 : */
776 :
777 : static const char *
778 20576 : getrule(const char *strp, struct rule *const rulep)
779 : {
780 20576 : if (*strp == 'J')
781 : {
782 : /*
783 : * Julian day.
784 : */
785 0 : rulep->r_type = JULIAN_DAY;
786 0 : ++strp;
787 0 : strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
788 : }
789 20576 : else if (*strp == 'M')
790 : {
791 : /*
792 : * Month, week, day.
793 : */
794 20576 : rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
795 20576 : ++strp;
796 20576 : strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
797 20576 : if (strp == NULL)
798 0 : return NULL;
799 20576 : if (*strp++ != '.')
800 0 : return NULL;
801 20576 : strp = getnum(strp, &rulep->r_week, 1, 5);
802 20576 : if (strp == NULL)
803 0 : return NULL;
804 20576 : if (*strp++ != '.')
805 0 : return NULL;
806 20576 : strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
807 : }
808 0 : else if (is_digit(*strp))
809 : {
810 : /*
811 : * Day of year.
812 : */
813 0 : rulep->r_type = DAY_OF_YEAR;
814 0 : strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
815 : }
816 : else
817 0 : return NULL; /* invalid format */
818 20576 : if (strp == NULL)
819 0 : return NULL;
820 20576 : if (*strp == '/')
821 : {
822 : /*
823 : * Time specified.
824 : */
825 2440 : ++strp;
826 2440 : strp = getoffset(strp, &rulep->r_time);
827 : }
828 : else
829 18136 : rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
830 20576 : return strp;
831 : }
832 :
833 : /*
834 : * Given a year, a rule, and the offset from UT at the time that rule takes
835 : * effect, calculate the year-relative time that rule takes effect.
836 : */
837 :
838 : static int32
839 20596576 : transtime(const int year, const struct rule *const rulep,
840 : const int32 offset)
841 : {
842 : bool leapyear;
843 : int32 value;
844 : int i;
845 : int d,
846 : m1,
847 : yy0,
848 : yy1,
849 : yy2,
850 : dow;
851 :
852 20596576 : INITIALIZE(value);
853 20596576 : leapyear = isleap(year);
854 20596576 : switch (rulep->r_type)
855 : {
856 :
857 0 : case JULIAN_DAY:
858 :
859 : /*
860 : * Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
861 : * years. In non-leap years, or if the day number is 59 or less,
862 : * just add SECSPERDAY times the day number-1 to the time of
863 : * January 1, midnight, to get the day.
864 : */
865 0 : value = (rulep->r_day - 1) * SECSPERDAY;
866 0 : if (leapyear && rulep->r_day >= 60)
867 0 : value += SECSPERDAY;
868 0 : break;
869 :
870 0 : case DAY_OF_YEAR:
871 :
872 : /*
873 : * n - day of year. Just add SECSPERDAY times the day number to
874 : * the time of January 1, midnight, to get the day.
875 : */
876 0 : value = rulep->r_day * SECSPERDAY;
877 0 : break;
878 :
879 20596576 : case MONTH_NTH_DAY_OF_WEEK:
880 :
881 : /*
882 : * Mm.n.d - nth "dth day" of month m.
883 : */
884 :
885 : /*
886 : * Use Zeller's Congruence to get day-of-week of first day of
887 : * month.
888 : */
889 20596576 : m1 = (rulep->r_mon + 9) % 12 + 1;
890 20596576 : yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
891 20596576 : yy1 = yy0 / 100;
892 20596576 : yy2 = yy0 % 100;
893 20596576 : dow = ((26 * m1 - 2) / 10 +
894 20596576 : 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
895 20596576 : if (dow < 0)
896 3873732 : dow += DAYSPERWEEK;
897 :
898 : /*
899 : * "dow" is the day-of-week of the first day of the month. Get the
900 : * day-of-month (zero-origin) of the first "dow" day of the month.
901 : */
902 20596576 : d = rulep->r_day - dow;
903 20596576 : if (d < 0)
904 17367948 : d += DAYSPERWEEK;
905 38865186 : for (i = 1; i < rulep->r_week; ++i)
906 : {
907 19865846 : if (d + DAYSPERWEEK >=
908 19865846 : mon_lengths[(int) leapyear][rulep->r_mon - 1])
909 1597236 : break;
910 18268610 : d += DAYSPERWEEK;
911 : }
912 :
913 : /*
914 : * "d" is the day-of-month (zero-origin) of the day we want.
915 : */
916 20596576 : value = d * SECSPERDAY;
917 142630488 : for (i = 0; i < rulep->r_mon - 1; ++i)
918 122033912 : value += mon_lengths[(int) leapyear][i] * SECSPERDAY;
919 20596576 : break;
920 : }
921 :
922 : /*
923 : * "value" is the year-relative time of 00:00:00 UT on the day in
924 : * question. To get the year-relative time of the specified local time on
925 : * that day, add the transition time and the current offset from UT.
926 : */
927 20596576 : return value + rulep->r_time + offset;
928 : }
929 :
930 : /*
931 : * Given a POSIX section 8-style TZ string, fill in the rule tables as
932 : * appropriate.
933 : * Returns true on success, false on failure.
934 : */
935 : bool
936 21434 : tzparse(const char *name, struct state *sp, bool lastditch)
937 : {
938 : const char *stdname;
939 21434 : const char *dstname = NULL;
940 : size_t stdlen;
941 : size_t dstlen;
942 : size_t charcnt;
943 : int32 stdoffset;
944 : int32 dstoffset;
945 : char *cp;
946 : bool load_ok;
947 :
948 21434 : stdname = name;
949 21434 : if (lastditch)
950 : {
951 : /* Unlike IANA, don't assume name is exactly "GMT" */
952 1982 : stdlen = strlen(name); /* length of standard zone name */
953 1982 : name += stdlen;
954 1982 : stdoffset = 0;
955 : }
956 : else
957 : {
958 19452 : if (*name == '<')
959 : {
960 3974 : name++;
961 3974 : stdname = name;
962 3974 : name = getqzname(name, '>');
963 3974 : if (*name != '>')
964 0 : return false;
965 3974 : stdlen = name - stdname;
966 3974 : name++;
967 : }
968 : else
969 : {
970 15478 : name = getzname(name);
971 15478 : stdlen = name - stdname;
972 : }
973 19452 : if (*name == '\0') /* we allow empty STD abbrev, unlike IANA */
974 102 : return false;
975 19350 : name = getoffset(name, &stdoffset);
976 19350 : if (name == NULL)
977 0 : return false;
978 : }
979 21332 : charcnt = stdlen + 1;
980 21332 : if (sizeof sp->chars < charcnt)
981 0 : return false;
982 :
983 : /*
984 : * The IANA code always tries to tzload(TZDEFRULES) here. We do not want
985 : * to do that; it would be bad news in the lastditch case, where we can't
986 : * assume pg_open_tzfile() is sane yet. Moreover, if we did load it and
987 : * it contains leap-second-dependent info, that would cause problems too.
988 : * Finally, IANA has deprecated the TZDEFRULES feature, so it presumably
989 : * will die at some point. Desupporting it now seems like good
990 : * future-proofing.
991 : */
992 21332 : load_ok = false;
993 21332 : sp->goback = sp->goahead = false; /* simulate failed tzload() */
994 21332 : sp->leapcnt = 0; /* intentionally assume no leap seconds */
995 :
996 21332 : if (*name != '\0')
997 : {
998 10288 : if (*name == '<')
999 : {
1000 280 : dstname = ++name;
1001 280 : name = getqzname(name, '>');
1002 280 : if (*name != '>')
1003 0 : return false;
1004 280 : dstlen = name - dstname;
1005 280 : name++;
1006 : }
1007 : else
1008 : {
1009 10008 : dstname = name;
1010 10008 : name = getzname(name);
1011 10008 : dstlen = name - dstname; /* length of DST abbr. */
1012 : }
1013 10288 : if (!dstlen)
1014 0 : return false;
1015 10288 : charcnt += dstlen + 1;
1016 10288 : if (sizeof sp->chars < charcnt)
1017 0 : return false;
1018 10288 : if (*name != '\0' && *name != ',' && *name != ';')
1019 : {
1020 86 : name = getoffset(name, &dstoffset);
1021 86 : if (name == NULL)
1022 0 : return false;
1023 : }
1024 : else
1025 10202 : dstoffset = stdoffset - SECSPERHOUR;
1026 10288 : if (*name == '\0' && !load_ok)
1027 6 : name = TZDEFRULESTRING;
1028 10288 : if (*name == ',' || *name == ';')
1029 10288 : {
1030 : struct rule start;
1031 : struct rule end;
1032 : int year;
1033 : int yearlim;
1034 : int timecnt;
1035 : pg_time_t janfirst;
1036 10288 : int32 janoffset = 0;
1037 : int yearbeg;
1038 :
1039 10288 : ++name;
1040 10288 : if ((name = getrule(name, &start)) == NULL)
1041 0 : return false;
1042 10288 : if (*name++ != ',')
1043 0 : return false;
1044 10288 : if ((name = getrule(name, &end)) == NULL)
1045 0 : return false;
1046 10288 : if (*name != '\0')
1047 0 : return false;
1048 10288 : sp->typecnt = 2; /* standard time and DST */
1049 :
1050 : /*
1051 : * Two transitions per year, from EPOCH_YEAR forward.
1052 : */
1053 10288 : init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1054 10288 : init_ttinfo(&sp->ttis[1], -dstoffset, true, stdlen + 1);
1055 10288 : sp->defaulttype = 0;
1056 10288 : timecnt = 0;
1057 10288 : janfirst = 0;
1058 10288 : yearbeg = EPOCH_YEAR;
1059 :
1060 : do
1061 : {
1062 2057600 : int32 yearsecs
1063 2057600 : = year_lengths[isleap(yearbeg - 1)] * SECSPERDAY;
1064 :
1065 2057600 : yearbeg--;
1066 2057600 : if (increment_overflow_time(&janfirst, -yearsecs))
1067 : {
1068 0 : janoffset = -yearsecs;
1069 0 : break;
1070 : }
1071 2057600 : } while (EPOCH_YEAR - YEARSPERREPEAT / 2 < yearbeg);
1072 :
1073 10288 : yearlim = yearbeg + YEARSPERREPEAT + 1;
1074 10298288 : for (year = yearbeg; year < yearlim; year++)
1075 : {
1076 : int32
1077 10298288 : starttime = transtime(year, &start, stdoffset),
1078 10298288 : endtime = transtime(year, &end, dstoffset);
1079 : int32
1080 10298288 : yearsecs = (year_lengths[isleap(year)]
1081 : * SECSPERDAY);
1082 10298288 : bool reversed = endtime < starttime;
1083 :
1084 10298288 : if (reversed)
1085 : {
1086 510510 : int32 swap = starttime;
1087 :
1088 510510 : starttime = endtime;
1089 510510 : endtime = swap;
1090 : }
1091 10298288 : if (reversed
1092 9787778 : || (starttime < endtime
1093 9787778 : && (endtime - starttime
1094 : < (yearsecs
1095 9787778 : + (stdoffset - dstoffset)))))
1096 : {
1097 10298288 : if (TZ_MAX_TIMES - 2 < timecnt)
1098 10288 : break;
1099 10288000 : sp->ats[timecnt] = janfirst;
1100 10288000 : if (!increment_overflow_time
1101 : (&sp->ats[timecnt],
1102 : janoffset + starttime))
1103 10288000 : sp->types[timecnt++] = !reversed;
1104 10288000 : sp->ats[timecnt] = janfirst;
1105 10288000 : if (!increment_overflow_time
1106 : (&sp->ats[timecnt],
1107 : janoffset + endtime))
1108 : {
1109 10288000 : sp->types[timecnt++] = reversed;
1110 10288000 : yearlim = year + YEARSPERREPEAT + 1;
1111 : }
1112 : }
1113 10288000 : if (increment_overflow_time
1114 : (&janfirst, janoffset + yearsecs))
1115 0 : break;
1116 10288000 : janoffset = 0;
1117 : }
1118 10288 : sp->timecnt = timecnt;
1119 10288 : if (!timecnt)
1120 : {
1121 0 : sp->ttis[0] = sp->ttis[1];
1122 0 : sp->typecnt = 1; /* Perpetual DST. */
1123 : }
1124 10288 : else if (YEARSPERREPEAT < year - yearbeg)
1125 10288 : sp->goback = sp->goahead = true;
1126 : }
1127 : else
1128 : {
1129 : int32 theirstdoffset;
1130 : int32 theirdstoffset;
1131 : int32 theiroffset;
1132 : bool isdst;
1133 : int i;
1134 : int j;
1135 :
1136 0 : if (*name != '\0')
1137 0 : return false;
1138 :
1139 : /*
1140 : * Initial values of theirstdoffset and theirdstoffset.
1141 : */
1142 0 : theirstdoffset = 0;
1143 0 : for (i = 0; i < sp->timecnt; ++i)
1144 : {
1145 0 : j = sp->types[i];
1146 0 : if (!sp->ttis[j].tt_isdst)
1147 : {
1148 0 : theirstdoffset =
1149 0 : -sp->ttis[j].tt_utoff;
1150 0 : break;
1151 : }
1152 : }
1153 0 : theirdstoffset = 0;
1154 0 : for (i = 0; i < sp->timecnt; ++i)
1155 : {
1156 0 : j = sp->types[i];
1157 0 : if (sp->ttis[j].tt_isdst)
1158 : {
1159 0 : theirdstoffset =
1160 0 : -sp->ttis[j].tt_utoff;
1161 0 : break;
1162 : }
1163 : }
1164 :
1165 : /*
1166 : * Initially we're assumed to be in standard time.
1167 : */
1168 0 : isdst = false;
1169 0 : theiroffset = theirstdoffset;
1170 :
1171 : /*
1172 : * Now juggle transition times and types tracking offsets as you
1173 : * do.
1174 : */
1175 0 : for (i = 0; i < sp->timecnt; ++i)
1176 : {
1177 0 : j = sp->types[i];
1178 0 : sp->types[i] = sp->ttis[j].tt_isdst;
1179 0 : if (sp->ttis[j].tt_ttisut)
1180 : {
1181 : /* No adjustment to transition time */
1182 : }
1183 : else
1184 : {
1185 : /*
1186 : * If daylight saving time is in effect, and the
1187 : * transition time was not specified as standard time, add
1188 : * the daylight saving time offset to the transition time;
1189 : * otherwise, add the standard time offset to the
1190 : * transition time.
1191 : */
1192 : /*
1193 : * Transitions from DST to DDST will effectively disappear
1194 : * since POSIX provides for only one DST offset.
1195 : */
1196 0 : if (isdst && !sp->ttis[j].tt_ttisstd)
1197 : {
1198 0 : sp->ats[i] += dstoffset -
1199 : theirdstoffset;
1200 : }
1201 : else
1202 : {
1203 0 : sp->ats[i] += stdoffset -
1204 : theirstdoffset;
1205 : }
1206 : }
1207 0 : theiroffset = -sp->ttis[j].tt_utoff;
1208 0 : if (sp->ttis[j].tt_isdst)
1209 0 : theirdstoffset = theiroffset;
1210 : else
1211 0 : theirstdoffset = theiroffset;
1212 : }
1213 :
1214 : /*
1215 : * Finally, fill in ttis.
1216 : */
1217 0 : init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1218 0 : init_ttinfo(&sp->ttis[1], -dstoffset, true, stdlen + 1);
1219 0 : sp->typecnt = 2;
1220 0 : sp->defaulttype = 0;
1221 : }
1222 : }
1223 : else
1224 : {
1225 11044 : dstlen = 0;
1226 11044 : sp->typecnt = 1; /* only standard time */
1227 11044 : sp->timecnt = 0;
1228 11044 : init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1229 11044 : sp->defaulttype = 0;
1230 : }
1231 21332 : sp->charcnt = charcnt;
1232 21332 : cp = sp->chars;
1233 21332 : memcpy(cp, stdname, stdlen);
1234 21332 : cp += stdlen;
1235 21332 : *cp++ = '\0';
1236 21332 : if (dstlen != 0)
1237 : {
1238 10288 : memcpy(cp, dstname, dstlen);
1239 10288 : *(cp + dstlen) = '\0';
1240 : }
1241 21332 : return true;
1242 : }
1243 :
1244 : static void
1245 356 : gmtload(struct state *const sp)
1246 : {
1247 356 : if (tzload(gmt, NULL, sp, true) != 0)
1248 0 : tzparse(gmt, sp, true);
1249 356 : }
1250 :
1251 :
1252 : /*
1253 : * The easy way to behave "as if no library function calls" localtime
1254 : * is to not call it, so we drop its guts into "localsub", which can be
1255 : * freely called. (And no, the PANS doesn't require the above behavior,
1256 : * but it *is* desirable.)
1257 : */
1258 : static struct pg_tm *
1259 1286736 : localsub(struct state const *sp, pg_time_t const *timep,
1260 : struct pg_tm *const tmp)
1261 : {
1262 : const struct ttinfo *ttisp;
1263 : int i;
1264 : struct pg_tm *result;
1265 1286736 : const pg_time_t t = *timep;
1266 :
1267 1286736 : if (sp == NULL)
1268 0 : return gmtsub(timep, 0, tmp);
1269 1286736 : if ((sp->goback && t < sp->ats[0]) ||
1270 1286736 : (sp->goahead && t > sp->ats[sp->timecnt - 1]))
1271 : {
1272 60 : pg_time_t newt = t;
1273 : pg_time_t seconds;
1274 : pg_time_t years;
1275 :
1276 60 : if (t < sp->ats[0])
1277 0 : seconds = sp->ats[0] - t;
1278 : else
1279 60 : seconds = t - sp->ats[sp->timecnt - 1];
1280 60 : --seconds;
1281 60 : years = (seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT;
1282 60 : seconds = years * AVGSECSPERYEAR;
1283 60 : if (t < sp->ats[0])
1284 0 : newt += seconds;
1285 : else
1286 60 : newt -= seconds;
1287 60 : if (newt < sp->ats[0] ||
1288 60 : newt > sp->ats[sp->timecnt - 1])
1289 0 : return NULL; /* "cannot happen" */
1290 60 : result = localsub(sp, &newt, tmp);
1291 60 : if (result)
1292 : {
1293 : int64 newy;
1294 :
1295 60 : newy = result->tm_year;
1296 60 : if (t < sp->ats[0])
1297 0 : newy -= years;
1298 : else
1299 60 : newy += years;
1300 60 : if (!(INT_MIN <= newy && newy <= INT_MAX))
1301 0 : return NULL;
1302 60 : result->tm_year = newy;
1303 : }
1304 60 : return result;
1305 : }
1306 1286676 : if (sp->timecnt == 0 || t < sp->ats[0])
1307 : {
1308 1216788 : i = sp->defaulttype;
1309 : }
1310 : else
1311 : {
1312 69888 : int lo = 1;
1313 69888 : int hi = sp->timecnt;
1314 :
1315 728180 : while (lo < hi)
1316 : {
1317 658292 : int mid = (lo + hi) >> 1;
1318 :
1319 658292 : if (t < sp->ats[mid])
1320 391314 : hi = mid;
1321 : else
1322 266978 : lo = mid + 1;
1323 : }
1324 69888 : i = (int) sp->types[lo - 1];
1325 : }
1326 1286676 : ttisp = &sp->ttis[i];
1327 :
1328 : /*
1329 : * To get (wrong) behavior that's compatible with System V Release 2.0
1330 : * you'd replace the statement below with t += ttisp->tt_utoff;
1331 : * timesub(&t, 0L, sp, tmp);
1332 : */
1333 1286676 : result = timesub(&t, ttisp->tt_utoff, sp, tmp);
1334 1286676 : if (result)
1335 : {
1336 1286676 : result->tm_isdst = ttisp->tt_isdst;
1337 1286676 : result->tm_zone = unconstify(char *, &sp->chars[ttisp->tt_desigidx]);
1338 : }
1339 1286676 : return result;
1340 : }
1341 :
1342 :
1343 : struct pg_tm *
1344 1286676 : pg_localtime(const pg_time_t *timep, const pg_tz *tz)
1345 : {
1346 1286676 : return localsub(&tz->state, timep, &tm);
1347 : }
1348 :
1349 :
1350 : /*
1351 : * gmtsub is to gmtime as localsub is to localtime.
1352 : *
1353 : * Except we have a private "struct state" for GMT, so no sp is passed in.
1354 : */
1355 :
1356 : static struct pg_tm *
1357 330372 : gmtsub(pg_time_t const *timep, int32 offset,
1358 : struct pg_tm *tmp)
1359 : {
1360 : struct pg_tm *result;
1361 :
1362 : /* GMT timezone state data is kept here */
1363 : static struct state *gmtptr = NULL;
1364 :
1365 330372 : if (gmtptr == NULL)
1366 : {
1367 : /* Allocate on first use */
1368 356 : gmtptr = (struct state *) malloc(sizeof(struct state));
1369 356 : if (gmtptr == NULL)
1370 0 : return NULL; /* errno should be set by malloc */
1371 356 : gmtload(gmtptr);
1372 : }
1373 :
1374 330372 : result = timesub(timep, offset, gmtptr, tmp);
1375 :
1376 : /*
1377 : * Could get fancy here and deliver something such as "+xx" or "-xx" if
1378 : * offset is non-zero, but this is no time for a treasure hunt.
1379 : */
1380 330372 : if (offset != 0)
1381 0 : tmp->tm_zone = wildabbr;
1382 : else
1383 330372 : tmp->tm_zone = gmtptr->chars;
1384 :
1385 330372 : return result;
1386 : }
1387 :
1388 : struct pg_tm *
1389 330372 : pg_gmtime(const pg_time_t *timep)
1390 : {
1391 330372 : return gmtsub(timep, 0, &tm);
1392 : }
1393 :
1394 : /*
1395 : * Return the number of leap years through the end of the given year
1396 : * where, to make the math easy, the answer for year zero is defined as zero.
1397 : */
1398 :
1399 : static int
1400 9245240 : leaps_thru_end_of_nonneg(int y)
1401 : {
1402 9245240 : return y / 4 - y / 100 + y / 400;
1403 : }
1404 :
1405 : static int
1406 9245240 : leaps_thru_end_of(const int y)
1407 : {
1408 : return (y < 0
1409 2304 : ? -1 - leaps_thru_end_of_nonneg(-1 - y)
1410 9247544 : : leaps_thru_end_of_nonneg(y));
1411 : }
1412 :
1413 : static struct pg_tm *
1414 1617048 : timesub(const pg_time_t *timep, int32 offset,
1415 : const struct state *sp, struct pg_tm *tmp)
1416 : {
1417 : const struct lsinfo *lp;
1418 : pg_time_t tdays;
1419 : int idays; /* unsigned would be so 2003 */
1420 : int64 rem;
1421 : int y;
1422 : const int *ip;
1423 : int64 corr;
1424 : bool hit;
1425 : int i;
1426 :
1427 1617048 : corr = 0;
1428 1617048 : hit = false;
1429 1617048 : i = (sp == NULL) ? 0 : sp->leapcnt;
1430 1617048 : while (--i >= 0)
1431 : {
1432 0 : lp = &sp->lsis[i];
1433 0 : if (*timep >= lp->ls_trans)
1434 : {
1435 0 : corr = lp->ls_corr;
1436 0 : hit = (*timep == lp->ls_trans
1437 0 : && (i == 0 ? 0 : lp[-1].ls_corr) < corr);
1438 0 : break;
1439 : }
1440 : }
1441 1617048 : y = EPOCH_YEAR;
1442 1617048 : tdays = *timep / SECSPERDAY;
1443 1617048 : rem = *timep % SECSPERDAY;
1444 4622620 : while (tdays < 0 || tdays >= year_lengths[isleap(y)])
1445 : {
1446 : int newy;
1447 : pg_time_t tdelta;
1448 : int idelta;
1449 : int leapdays;
1450 :
1451 3005572 : tdelta = tdays / DAYSPERLYEAR;
1452 3005572 : if (!((!TYPE_SIGNED(pg_time_t) || INT_MIN <= tdelta)
1453 : && tdelta <= INT_MAX))
1454 0 : goto out_of_range;
1455 3005572 : idelta = tdelta;
1456 3005572 : if (idelta == 0)
1457 38154 : idelta = (tdays < 0) ? -1 : 1;
1458 3005572 : newy = y;
1459 3005572 : if (increment_overflow(&newy, idelta))
1460 0 : goto out_of_range;
1461 3005572 : leapdays = leaps_thru_end_of(newy - 1) -
1462 3005572 : leaps_thru_end_of(y - 1);
1463 3005572 : tdays -= ((pg_time_t) newy - y) * DAYSPERNYEAR;
1464 3005572 : tdays -= leapdays;
1465 3005572 : y = newy;
1466 : }
1467 :
1468 : /*
1469 : * Given the range, we can now fearlessly cast...
1470 : */
1471 1617048 : idays = tdays;
1472 1617048 : rem += offset - corr;
1473 1657442 : while (rem < 0)
1474 : {
1475 40394 : rem += SECSPERDAY;
1476 40394 : --idays;
1477 : }
1478 1617372 : while (rem >= SECSPERDAY)
1479 : {
1480 324 : rem -= SECSPERDAY;
1481 324 : ++idays;
1482 : }
1483 1630992 : while (idays < 0)
1484 : {
1485 13944 : if (increment_overflow(&y, -1))
1486 0 : goto out_of_range;
1487 13944 : idays += year_lengths[isleap(y)];
1488 : }
1489 1617060 : while (idays >= year_lengths[isleap(y)])
1490 : {
1491 12 : idays -= year_lengths[isleap(y)];
1492 12 : if (increment_overflow(&y, 1))
1493 0 : goto out_of_range;
1494 : }
1495 1617048 : tmp->tm_year = y;
1496 1617048 : if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1497 0 : goto out_of_range;
1498 1617048 : tmp->tm_yday = idays;
1499 :
1500 : /*
1501 : * The "extra" mods below avoid overflow problems.
1502 : */
1503 1617048 : tmp->tm_wday = EPOCH_WDAY +
1504 1617048 : ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1505 1617048 : (DAYSPERNYEAR % DAYSPERWEEK) +
1506 1617048 : leaps_thru_end_of(y - 1) -
1507 1617048 : leaps_thru_end_of(EPOCH_YEAR - 1) +
1508 : idays;
1509 1617048 : tmp->tm_wday %= DAYSPERWEEK;
1510 1617048 : if (tmp->tm_wday < 0)
1511 1668 : tmp->tm_wday += DAYSPERWEEK;
1512 1617048 : tmp->tm_hour = (int) (rem / SECSPERHOUR);
1513 1617048 : rem %= SECSPERHOUR;
1514 1617048 : tmp->tm_min = (int) (rem / SECSPERMIN);
1515 :
1516 : /*
1517 : * A positive leap second requires a special representation. This uses
1518 : * "... ??:59:60" et seq.
1519 : */
1520 1617048 : tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1521 1617048 : ip = mon_lengths[isleap(y)];
1522 2093702 : for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1523 476654 : idays -= ip[tmp->tm_mon];
1524 1617048 : tmp->tm_mday = (int) (idays + 1);
1525 1617048 : tmp->tm_isdst = 0;
1526 1617048 : tmp->tm_gmtoff = offset;
1527 1617048 : return tmp;
1528 :
1529 0 : out_of_range:
1530 0 : errno = EOVERFLOW;
1531 0 : return NULL;
1532 : }
1533 :
1534 : /*
1535 : * Normalize logic courtesy Paul Eggert.
1536 : */
1537 :
1538 : static bool
1539 4636576 : increment_overflow(int *ip, int j)
1540 : {
1541 4636576 : int const i = *ip;
1542 :
1543 : /*----------
1544 : * If i >= 0 there can only be overflow if i + j > INT_MAX
1545 : * or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
1546 : * If i < 0 there can only be overflow if i + j < INT_MIN
1547 : * or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
1548 : *----------
1549 : */
1550 4636576 : if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
1551 0 : return true;
1552 4636576 : *ip += j;
1553 4636576 : return false;
1554 : }
1555 :
1556 : static bool
1557 32921600 : increment_overflow_time(pg_time_t *tp, int32 j)
1558 : {
1559 : /*----------
1560 : * This is like
1561 : * 'if (! (TIME_T_MIN <= *tp + j && *tp + j <= TIME_T_MAX)) ...',
1562 : * except that it does the right thing even if *tp + j would overflow.
1563 : *----------
1564 : */
1565 65843200 : if (!(j < 0
1566 2057600 : ? (TYPE_SIGNED(pg_time_t) ? TIME_T_MIN - j <= *tp : -1 - j < *tp)
1567 30864000 : : *tp <= TIME_T_MAX - j))
1568 0 : return true;
1569 32921600 : *tp += j;
1570 32921600 : return false;
1571 : }
1572 :
1573 : static int64
1574 20546268 : leapcorr(struct state const *sp, pg_time_t t)
1575 : {
1576 : struct lsinfo const *lp;
1577 : int i;
1578 :
1579 20546268 : i = sp->leapcnt;
1580 20546268 : while (--i >= 0)
1581 : {
1582 0 : lp = &sp->lsis[i];
1583 0 : if (t >= lp->ls_trans)
1584 0 : return lp->ls_corr;
1585 : }
1586 20546268 : return 0;
1587 : }
1588 :
1589 : /*
1590 : * Find the next DST transition time in the given zone after the given time
1591 : *
1592 : * *timep and *tz are input arguments, the other parameters are output values.
1593 : *
1594 : * When the function result is 1, *boundary is set to the pg_time_t
1595 : * representation of the next DST transition time after *timep,
1596 : * *before_gmtoff and *before_isdst are set to the GMT offset and isdst
1597 : * state prevailing just before that boundary (in particular, the state
1598 : * prevailing at *timep), and *after_gmtoff and *after_isdst are set to
1599 : * the state prevailing just after that boundary.
1600 : *
1601 : * When the function result is 0, there is no known DST transition
1602 : * after *timep, but *before_gmtoff and *before_isdst indicate the GMT
1603 : * offset and isdst state prevailing at *timep. (This would occur in
1604 : * DST-less time zones, or if a zone has permanently ceased using DST.)
1605 : *
1606 : * A function result of -1 indicates failure (this case does not actually
1607 : * occur in our current implementation).
1608 : */
1609 : int
1610 58188 : pg_next_dst_boundary(const pg_time_t *timep,
1611 : long int *before_gmtoff,
1612 : int *before_isdst,
1613 : pg_time_t *boundary,
1614 : long int *after_gmtoff,
1615 : int *after_isdst,
1616 : const pg_tz *tz)
1617 : {
1618 : const struct state *sp;
1619 : const struct ttinfo *ttisp;
1620 : int i;
1621 : int j;
1622 58188 : const pg_time_t t = *timep;
1623 :
1624 58188 : sp = &tz->state;
1625 58188 : if (sp->timecnt == 0)
1626 : {
1627 : /* non-DST zone, use the defaulttype */
1628 3230 : ttisp = &sp->ttis[sp->defaulttype];
1629 3230 : *before_gmtoff = ttisp->tt_utoff;
1630 3230 : *before_isdst = ttisp->tt_isdst;
1631 3230 : return 0;
1632 : }
1633 54958 : if ((sp->goback && t < sp->ats[0]) ||
1634 54958 : (sp->goahead && t > sp->ats[sp->timecnt - 1]))
1635 : {
1636 : /* For values outside the transition table, extrapolate */
1637 186 : pg_time_t newt = t;
1638 : pg_time_t seconds;
1639 : pg_time_t tcycles;
1640 : int64 icycles;
1641 : int result;
1642 :
1643 186 : if (t < sp->ats[0])
1644 0 : seconds = sp->ats[0] - t;
1645 : else
1646 186 : seconds = t - sp->ats[sp->timecnt - 1];
1647 186 : --seconds;
1648 186 : tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR;
1649 186 : ++tcycles;
1650 186 : icycles = tcycles;
1651 186 : if (tcycles - icycles >= 1 || icycles - tcycles >= 1)
1652 0 : return -1;
1653 186 : seconds = icycles;
1654 186 : seconds *= YEARSPERREPEAT;
1655 186 : seconds *= AVGSECSPERYEAR;
1656 186 : if (t < sp->ats[0])
1657 0 : newt += seconds;
1658 : else
1659 186 : newt -= seconds;
1660 186 : if (newt < sp->ats[0] ||
1661 186 : newt > sp->ats[sp->timecnt - 1])
1662 0 : return -1; /* "cannot happen" */
1663 :
1664 186 : result = pg_next_dst_boundary(&newt, before_gmtoff,
1665 : before_isdst,
1666 : boundary,
1667 : after_gmtoff,
1668 : after_isdst,
1669 : tz);
1670 186 : if (t < sp->ats[0])
1671 0 : *boundary -= seconds;
1672 : else
1673 186 : *boundary += seconds;
1674 186 : return result;
1675 : }
1676 :
1677 54772 : if (t >= sp->ats[sp->timecnt - 1])
1678 : {
1679 : /* No known transition > t, so use last known segment's type */
1680 1056 : i = sp->types[sp->timecnt - 1];
1681 1056 : ttisp = &sp->ttis[i];
1682 1056 : *before_gmtoff = ttisp->tt_utoff;
1683 1056 : *before_isdst = ttisp->tt_isdst;
1684 1056 : return 0;
1685 : }
1686 53716 : if (t < sp->ats[0])
1687 : {
1688 : /* For "before", use the defaulttype */
1689 546 : ttisp = &sp->ttis[sp->defaulttype];
1690 546 : *before_gmtoff = ttisp->tt_utoff;
1691 546 : *before_isdst = ttisp->tt_isdst;
1692 546 : *boundary = sp->ats[0];
1693 : /* And for "after", use the first segment's type */
1694 546 : i = sp->types[0];
1695 546 : ttisp = &sp->ttis[i];
1696 546 : *after_gmtoff = ttisp->tt_utoff;
1697 546 : *after_isdst = ttisp->tt_isdst;
1698 546 : return 1;
1699 : }
1700 : /* Else search to find the boundary following t */
1701 : {
1702 53170 : int lo = 1;
1703 53170 : int hi = sp->timecnt - 1;
1704 :
1705 629806 : while (lo < hi)
1706 : {
1707 576636 : int mid = (lo + hi) >> 1;
1708 :
1709 576636 : if (t < sp->ats[mid])
1710 384200 : hi = mid;
1711 : else
1712 192436 : lo = mid + 1;
1713 : }
1714 53170 : i = lo;
1715 : }
1716 53170 : j = sp->types[i - 1];
1717 53170 : ttisp = &sp->ttis[j];
1718 53170 : *before_gmtoff = ttisp->tt_utoff;
1719 53170 : *before_isdst = ttisp->tt_isdst;
1720 53170 : *boundary = sp->ats[i];
1721 53170 : j = sp->types[i];
1722 53170 : ttisp = &sp->ttis[j];
1723 53170 : *after_gmtoff = ttisp->tt_utoff;
1724 53170 : *after_isdst = ttisp->tt_isdst;
1725 53170 : return 1;
1726 : }
1727 :
1728 : /*
1729 : * Identify a timezone abbreviation's meaning in the given zone
1730 : *
1731 : * Determine the GMT offset and DST flag associated with the abbreviation.
1732 : * This is generally used only when the abbreviation has actually changed
1733 : * meaning over time; therefore, we also take a UTC cutoff time, and return
1734 : * the meaning in use at or most recently before that time, or the meaning
1735 : * in first use after that time if the abbrev was never used before that.
1736 : *
1737 : * On success, returns true and sets *gmtoff and *isdst. If the abbreviation
1738 : * was never used at all in this zone, returns false.
1739 : *
1740 : * Note: abbrev is matched case-sensitively; it should be all-upper-case.
1741 : */
1742 : bool
1743 1662 : pg_interpret_timezone_abbrev(const char *abbrev,
1744 : const pg_time_t *timep,
1745 : long int *gmtoff,
1746 : int *isdst,
1747 : const pg_tz *tz)
1748 : {
1749 : const struct state *sp;
1750 : const char *abbrs;
1751 : const struct ttinfo *ttisp;
1752 : int abbrind;
1753 : int cutoff;
1754 : int i;
1755 1662 : const pg_time_t t = *timep;
1756 :
1757 1662 : sp = &tz->state;
1758 :
1759 : /*
1760 : * Locate the abbreviation in the zone's abbreviation list. We assume
1761 : * there are not duplicates in the list.
1762 : */
1763 1662 : abbrs = sp->chars;
1764 1662 : abbrind = 0;
1765 8574 : while (abbrind < sp->charcnt)
1766 : {
1767 7404 : if (strcmp(abbrev, abbrs + abbrind) == 0)
1768 492 : break;
1769 28578 : while (abbrs[abbrind] != '\0')
1770 21666 : abbrind++;
1771 6912 : abbrind++;
1772 : }
1773 1662 : if (abbrind >= sp->charcnt)
1774 1170 : return false; /* not there! */
1775 :
1776 : /*
1777 : * Unlike pg_next_dst_boundary, we needn't sweat about extrapolation
1778 : * (goback/goahead zones). Finding the newest or oldest meaning of the
1779 : * abbreviation should get us what we want, since extrapolation would just
1780 : * be repeating the newest or oldest meanings.
1781 : *
1782 : * Use binary search to locate the first transition > cutoff time. (Note
1783 : * that sp->timecnt could be zero, in which case this loop does nothing
1784 : * and only the defaulttype entry will be checked.)
1785 : */
1786 : {
1787 492 : int lo = 0;
1788 492 : int hi = sp->timecnt;
1789 :
1790 4494 : while (lo < hi)
1791 : {
1792 4002 : int mid = (lo + hi) >> 1;
1793 :
1794 4002 : if (t < sp->ats[mid])
1795 1878 : hi = mid;
1796 : else
1797 2124 : lo = mid + 1;
1798 : }
1799 492 : cutoff = lo;
1800 : }
1801 :
1802 : /*
1803 : * Scan backwards to find the latest interval using the given abbrev
1804 : * before the cutoff time.
1805 : */
1806 20148 : for (i = cutoff - 1; i >= 0; i--)
1807 : {
1808 20106 : ttisp = &sp->ttis[sp->types[i]];
1809 20106 : if (ttisp->tt_desigidx == abbrind)
1810 : {
1811 450 : *gmtoff = ttisp->tt_utoff;
1812 450 : *isdst = ttisp->tt_isdst;
1813 450 : return true;
1814 : }
1815 : }
1816 :
1817 : /*
1818 : * Not found yet; check the defaulttype, which is notionally the era
1819 : * before any of the entries in sp->types[].
1820 : */
1821 42 : ttisp = &sp->ttis[sp->defaulttype];
1822 42 : if (ttisp->tt_desigidx == abbrind)
1823 : {
1824 42 : *gmtoff = ttisp->tt_utoff;
1825 42 : *isdst = ttisp->tt_isdst;
1826 42 : return true;
1827 : }
1828 :
1829 : /*
1830 : * Not there, so scan forwards to find the first one after the cutoff.
1831 : */
1832 0 : for (i = cutoff; i < sp->timecnt; i++)
1833 : {
1834 0 : ttisp = &sp->ttis[sp->types[i]];
1835 0 : if (ttisp->tt_desigidx == abbrind)
1836 : {
1837 0 : *gmtoff = ttisp->tt_utoff;
1838 0 : *isdst = ttisp->tt_isdst;
1839 0 : return true;
1840 : }
1841 : }
1842 :
1843 0 : return false; /* hm, not actually used in any interval? */
1844 : }
1845 :
1846 : /*
1847 : * Detect whether a timezone abbreviation is defined within the given zone.
1848 : *
1849 : * This is similar to pg_interpret_timezone_abbrev() but is not concerned
1850 : * with a specific point in time. We want to know if the abbreviation is
1851 : * known at all, and if so whether it has one meaning or several.
1852 : *
1853 : * Returns true if the abbreviation is known, false if not.
1854 : * If the abbreviation is known and has a single meaning (only one value
1855 : * of gmtoff/isdst), sets *isfixed = true and sets *gmtoff and *isdst.
1856 : * If there are multiple meanings, sets *isfixed = false.
1857 : *
1858 : * Note: abbrev is matched case-sensitively; it should be all-upper-case.
1859 : */
1860 : bool
1861 7986 : pg_timezone_abbrev_is_known(const char *abbrev,
1862 : bool *isfixed,
1863 : long int *gmtoff,
1864 : int *isdst,
1865 : const pg_tz *tz)
1866 : {
1867 7986 : bool result = false;
1868 7986 : const struct state *sp = &tz->state;
1869 : const char *abbrs;
1870 : int abbrind;
1871 :
1872 : /*
1873 : * Locate the abbreviation in the zone's abbreviation list. We assume
1874 : * there are not duplicates in the list.
1875 : */
1876 7986 : abbrs = sp->chars;
1877 7986 : abbrind = 0;
1878 45906 : while (abbrind < sp->charcnt)
1879 : {
1880 38160 : if (strcmp(abbrev, abbrs + abbrind) == 0)
1881 240 : break;
1882 151740 : while (abbrs[abbrind] != '\0')
1883 113820 : abbrind++;
1884 37920 : abbrind++;
1885 : }
1886 7986 : if (abbrind >= sp->charcnt)
1887 7746 : return false; /* definitely not there */
1888 :
1889 : /*
1890 : * Scan the ttinfo array to find uses of the abbreviation.
1891 : */
1892 1898 : for (int i = 0; i < sp->typecnt; i++)
1893 : {
1894 1658 : const struct ttinfo *ttisp = &sp->ttis[i];
1895 :
1896 1658 : if (ttisp->tt_desigidx == abbrind)
1897 : {
1898 456 : if (!result)
1899 : {
1900 : /* First usage */
1901 240 : *isfixed = true; /* for the moment */
1902 240 : *gmtoff = ttisp->tt_utoff;
1903 240 : *isdst = ttisp->tt_isdst;
1904 240 : result = true;
1905 : }
1906 : else
1907 : {
1908 : /* Second or later usage, does it match? */
1909 216 : if (*gmtoff != ttisp->tt_utoff ||
1910 216 : *isdst != ttisp->tt_isdst)
1911 : {
1912 0 : *isfixed = false;
1913 0 : break; /* no point in looking further */
1914 : }
1915 : }
1916 : }
1917 : }
1918 :
1919 240 : return result;
1920 : }
1921 :
1922 : /*
1923 : * Iteratively fetch all the abbreviations used in the given time zone.
1924 : *
1925 : * *indx is a state counter that the caller must initialize to zero
1926 : * before the first call, and not touch between calls.
1927 : *
1928 : * Returns the next known abbreviation, or NULL if there are no more.
1929 : *
1930 : * Note: the caller typically applies pg_interpret_timezone_abbrev()
1931 : * to each result. While that nominally results in O(N^2) time spent
1932 : * searching the sp->chars[] array, we don't expect any zone to have
1933 : * enough abbreviations to make that meaningful.
1934 : */
1935 : const char *
1936 252 : pg_get_next_timezone_abbrev(int *indx,
1937 : const pg_tz *tz)
1938 : {
1939 : const char *result;
1940 252 : const struct state *sp = &tz->state;
1941 : const char *abbrs;
1942 : int abbrind;
1943 :
1944 : /* If we're still in range, the result is the current abbrev. */
1945 252 : abbrs = sp->chars;
1946 252 : abbrind = *indx;
1947 252 : if (abbrind < 0 || abbrind >= sp->charcnt)
1948 42 : return NULL;
1949 210 : result = abbrs + abbrind;
1950 :
1951 : /* Advance *indx past this abbrev and its trailing null. */
1952 840 : while (abbrs[abbrind] != '\0')
1953 630 : abbrind++;
1954 210 : abbrind++;
1955 210 : *indx = abbrind;
1956 :
1957 210 : return result;
1958 : }
1959 :
1960 : /*
1961 : * If the given timezone uses only one GMT offset, store that offset
1962 : * into *gmtoff and return true, else return false.
1963 : */
1964 : bool
1965 1210 : pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff)
1966 : {
1967 : /*
1968 : * The zone could have more than one ttinfo, if it's historically used
1969 : * more than one abbreviation. We return true as long as they all have
1970 : * the same gmtoff.
1971 : */
1972 : const struct state *sp;
1973 : int i;
1974 :
1975 1210 : sp = &tz->state;
1976 1244 : for (i = 1; i < sp->typecnt; i++)
1977 : {
1978 118 : if (sp->ttis[i].tt_utoff != sp->ttis[0].tt_utoff)
1979 84 : return false;
1980 : }
1981 1126 : *gmtoff = sp->ttis[0].tt_utoff;
1982 1126 : return true;
1983 : }
1984 :
1985 : /*
1986 : * Return the name of the current timezone
1987 : */
1988 : const char *
1989 62788 : pg_get_timezone_name(pg_tz *tz)
1990 : {
1991 62788 : if (tz)
1992 62788 : return tz->TZname;
1993 0 : return NULL;
1994 : }
1995 :
1996 : /*
1997 : * Check whether timezone is acceptable.
1998 : *
1999 : * What we are doing here is checking for leap-second-aware timekeeping.
2000 : * We need to reject such TZ settings because they'll wreak havoc with our
2001 : * date/time arithmetic.
2002 : */
2003 : bool
2004 34146 : pg_tz_acceptable(pg_tz *tz)
2005 : {
2006 : struct pg_tm *tt;
2007 : pg_time_t time2000;
2008 :
2009 : /*
2010 : * To detect leap-second timekeeping, run pg_localtime for what should be
2011 : * GMT midnight, 2000-01-01. Insist that the tm_sec value be zero; any
2012 : * other result has to be due to leap seconds.
2013 : */
2014 34146 : time2000 = (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY;
2015 34146 : tt = pg_localtime(&time2000, tz);
2016 34146 : if (!tt || tt->tm_sec != 0)
2017 0 : return false;
2018 :
2019 34146 : return true;
2020 : }
|