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