Age Owner Branch data TLA 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
57 tgl@sss.pgh.pa.us 73 :GNC 6316292 : leapcount(ATTRIBUTE_MAYBE_UNUSED struct state const *sp)
74 : : {
75 : : #if TZ_RUNTIME_LEAPS
76 : 6316292 : return sp->leapcnt;
77 : : #else
78 : : return 0;
79 : : #endif
80 : : }
81 : : static void
82 : 37726 : set_leapcount(ATTRIBUTE_MAYBE_UNUSED struct state *sp,
83 : : ATTRIBUTE_MAYBE_UNUSED int leapcnt)
84 : : {
85 : : #if TZ_RUNTIME_LEAPS
86 : 37726 : sp->leapcnt = leapcnt;
87 : : #endif
88 : 37726 : }
89 : : static struct lsinfo
57 tgl@sss.pgh.pa.us 90 :UNC 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
57 tgl@sss.pgh.pa.us 152 :GNC 20275 : init_ttinfo(struct ttinfo *s, int_fast32_t utoff, bool isdst,
153 : : desigidx_type desigidx)
154 : : {
2598 tgl@sss.pgh.pa.us 155 :CBC 20275 : s->tt_utoff = utoff;
3804 156 : 20275 : s->tt_isdst = isdst;
2598 157 : 20275 : s->tt_desigidx = desigidx;
3804 158 : 20275 : s->tt_ttisstd = false;
2598 159 : 20275 : s->tt_ttisut = false;
3804 160 : 20275 : }
161 : :
162 : : static int_fast32_2s
2869 163 : 195829 : detzcode(const char *const codep)
164 : : {
165 : : int i;
166 : : int_fast32_2s
57 tgl@sss.pgh.pa.us 167 :GNC 195829 : maxval = TWO_31_MINUS_1,
168 : 195829 : minval = -1 - maxval,
169 : : result;
170 : :
3804 tgl@sss.pgh.pa.us 171 :CBC 195829 : result = codep[0] & 0x7f;
172 [ + + ]: 783316 : for (i = 1; i < 4; ++i)
8154 bruce@momjian.us 173 : 587487 : result = (result << 8) | (codep[i] & 0xff);
174 : :
3804 tgl@sss.pgh.pa.us 175 [ + + ]: 195829 : 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 : 35339 : result += minval;
182 : : }
8154 bruce@momjian.us 183 : 195829 : return result;
184 : : }
185 : :
186 : : static int_fast64_t
2869 tgl@sss.pgh.pa.us 187 : 865041 : detzcode64(const char *const codep)
188 : : {
189 : : int_fast64_t result;
190 : : int i;
279 peter@eisentraut.org 191 : 865041 : int_fast64_t one = 1;
192 : 865041 : int_fast64_t halfmaxval = one << (64 - 2);
193 : 865041 : int_fast64_t maxval = halfmaxval - 1 + halfmaxval;
194 : 865041 : int_fast64_t minval = -TWOS_COMPLEMENT(int_fast64_t) - maxval;
195 : :
3804 tgl@sss.pgh.pa.us 196 : 865041 : result = codep[0] & 0x7f;
197 [ + + ]: 6920328 : for (i = 1; i < 8; ++i)
198 : 6055287 : result = (result << 8) | (codep[i] & 0xff);
199 : :
200 [ + + ]: 865041 : 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 : : */
279 peter@eisentraut.org 206 : 332289 : result -= !TWOS_COMPLEMENT(int_fast64_t) && result != 0;
3804 tgl@sss.pgh.pa.us 207 : 332289 : result += minval;
208 : : }
6767 209 : 865041 : 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
57 tgl@sss.pgh.pa.us 261 :GNC 12401 : 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 : 12401 : union local_storage *lsp = *lspp;
270 : : union input_buffer *up;
3804 tgl@sss.pgh.pa.us 271 :CBC 12401 : int tzheadsize = sizeof(struct tzhead);
272 : :
273 : 12401 : sp->goback = sp->goahead = false;
274 : :
275 [ - + ]: 12401 : if (!name)
276 : : {
3804 tgl@sss.pgh.pa.us 277 :UBC 0 : name = TZDEFAULT;
278 [ # # ]: 0 : if (!name)
279 : 0 : return EINVAL;
280 : : }
281 : :
7255 tgl@sss.pgh.pa.us 282 [ - + ]:CBC 12401 : if (name[0] == ':')
7255 tgl@sss.pgh.pa.us 283 :UBC 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 : : */
7255 tgl@sss.pgh.pa.us 291 :CBC 12401 : fid = pg_open_tzfile(name, canonname);
292 [ + + ]: 12401 : if (fid < 0)
3804 293 : 401 : return ENOENT; /* pg_open_tzfile may not set errno */
294 : :
57 tgl@sss.pgh.pa.us 295 :GNC 12000 : up = &lsp->u.u;
3804 tgl@sss.pgh.pa.us 296 :CBC 12000 : nread = read(fid, up->buf, sizeof up->buf);
297 [ - + ]: 12000 : if (nread < tzheadsize)
298 : : {
3804 tgl@sss.pgh.pa.us 299 [ # # ]:UBC 0 : int err = nread < 0 ? errno : EINVAL;
300 : :
301 : 0 : close(fid);
302 : 0 : return err;
303 : : }
3804 tgl@sss.pgh.pa.us 304 [ - + ]:CBC 12000 : if (close(fid) < 0)
3804 tgl@sss.pgh.pa.us 305 :UBC 0 : return errno;
306 : :
6767 tgl@sss.pgh.pa.us 307 [ + + ]:CBC 36000 : for (stored = 4; stored <= 8; stored *= 2)
308 : : {
57 tgl@sss.pgh.pa.us 309 :GNC 24000 : char version = up->tzhead.tzh_version[0];
310 [ + + + - ]: 24000 : bool skip_datablock = stored == 4 && version;
311 : : int_fast32_t datablock_size;
312 : : int_fast32_2s
313 : 24000 : ttisstdcnt = detzcode(up->tzhead.tzh_ttisstdcnt),
314 : 24000 : ttisutcnt = detzcode(up->tzhead.tzh_ttisutcnt),
315 : 24000 : leapcnt = detzcode(up->tzhead.tzh_leapcnt),
316 : 24000 : timecnt = detzcode(up->tzhead.tzh_timecnt),
317 : 24000 : typecnt = detzcode(up->tzhead.tzh_typecnt),
318 : 24000 : charcnt = detzcode(up->tzhead.tzh_charcnt);
3804 tgl@sss.pgh.pa.us 319 :CBC 24000 : 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 : : */
57 tgl@sss.pgh.pa.us 326 [ + - - + ]:GNC 48000 : if (!(0 <= leapcnt
327 [ + - ]: 24000 : && leapcnt <= (TZ_RUNTIME_LEAPS ? TZ_MAX_LEAPS : 0)
328 [ + - + - ]: 24000 : && 0 <= typecnt && typecnt <= TZ_MAX_TYPES
329 [ + - + - ]: 24000 : && 0 <= timecnt && timecnt <= TZ_MAX_TIMES
330 [ + - + - ]: 24000 : && 0 <= charcnt && charcnt <= TZ_MAX_CHARS
331 [ + - + - ]: 24000 : && 0 <= ttisstdcnt && ttisstdcnt <= TZ_MAX_TYPES
332 [ + - ]: 24000 : && 0 <= ttisutcnt && ttisutcnt <= TZ_MAX_TYPES))
3804 tgl@sss.pgh.pa.us 333 :UBC 0 : return EINVAL;
334 : : datablock_size
57 tgl@sss.pgh.pa.us 335 :GNC 24000 : = (timecnt * stored /* ats */
3804 tgl@sss.pgh.pa.us 336 :CBC 24000 : + timecnt /* types */
337 : 24000 : + typecnt * 6 /* ttinfos */
338 : 24000 : + charcnt /* chars */
339 : 24000 : + leapcnt * (stored + 4) /* lsinfos */
340 : 24000 : + ttisstdcnt /* ttisstds */
341 : : + ttisutcnt); /* ttisuts */
57 tgl@sss.pgh.pa.us 342 [ - + ]:GNC 24000 : if (nread < tzheadsize + datablock_size)
3804 tgl@sss.pgh.pa.us 343 :UBC 0 : return EINVAL;
57 tgl@sss.pgh.pa.us 344 [ + + ]:GNC 24000 : if (skip_datablock)
345 : 12000 : p += datablock_size;
346 [ + - + - : 12000 : else if (!((ttisstdcnt == typecnt || ttisstdcnt == 0)
+ - ]
347 [ - + ]: 12000 : && (ttisutcnt == typecnt || ttisutcnt == 0)))
57 tgl@sss.pgh.pa.us 348 :UNC 0 : return EINVAL;
349 : : else
350 : : {
57 tgl@sss.pgh.pa.us 351 :GNC 12000 : int_fast64_t prevtr = -1;
352 : 12000 : int_fast32_2s prevcorr = -1;
353 : :
354 : 12000 : set_leapcount(sp, leapcnt);
355 : 12000 : sp->timecnt = timecnt;
356 : 12000 : sp->typecnt = typecnt;
357 : 12000 : 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 : 12000 : timecnt = 0;
365 [ + + ]: 877041 : for (i = 0; i < sp->timecnt; ++i)
366 : : {
367 : 865041 : int_fast64_t at
368 [ - + ]: 865041 : = stored == 4 ? detzcode(p) : detzcode64(p);
369 : :
370 : 865041 : sp->types[i] = at <= TIME_T_MAX;
371 [ + - ]: 865041 : if (sp->types[i])
372 : : {
373 : 865041 : pg_time_t attime
374 : : = ((TYPE_SIGNED(pg_time_t) ? at < TIME_T_MIN : at < 0)
375 : : ? TIME_T_MIN : at);
376 : :
377 [ + + - + ]: 865041 : if (timecnt && attime <= sp->ats[timecnt - 1])
378 : : {
57 tgl@sss.pgh.pa.us 379 [ # # ]:UNC 0 : if (attime < sp->ats[timecnt - 1])
380 : 0 : return EINVAL;
381 : 0 : sp->types[i - 1] = 0;
382 : 0 : timecnt--;
383 : : }
57 tgl@sss.pgh.pa.us 384 :GNC 865041 : sp->ats[timecnt++] = attime;
385 : : }
386 : 865041 : p += stored;
387 : : }
388 : :
389 : 12000 : timecnt = 0;
390 [ + + ]: 877041 : for (i = 0; i < sp->timecnt; ++i)
391 : : {
392 : 865041 : unsigned char typ = *p++;
393 : :
394 [ - + ]: 865041 : if (sp->typecnt <= typ)
57 tgl@sss.pgh.pa.us 395 :UNC 0 : return EINVAL;
57 tgl@sss.pgh.pa.us 396 [ + - ]:GNC 865041 : if (sp->types[i])
397 : 865041 : sp->types[timecnt++] = typ;
398 : : }
399 : 12000 : sp->timecnt = timecnt;
400 [ + + ]: 63829 : for (i = 0; i < sp->typecnt; ++i)
401 : : {
402 : : struct ttinfo *ttisp;
403 : : unsigned char isdst,
404 : : desigidx;
405 : 51829 : 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 [ - + ]: 51829 : if (utoff < -TWO_31_MINUS_1)
3261 tgl@sss.pgh.pa.us 413 :UBC 0 : return EINVAL;
414 : :
57 tgl@sss.pgh.pa.us 415 :GNC 51829 : ttisp = &sp->ttis[i];
416 : 51829 : ttisp->tt_utoff = utoff;
417 : 51829 : p += 4;
418 : 51829 : isdst = *p++;
419 [ - + ]: 51829 : if (!(isdst < 2))
57 tgl@sss.pgh.pa.us 420 :UNC 0 : return EINVAL;
57 tgl@sss.pgh.pa.us 421 :GNC 51829 : ttisp->tt_isdst = isdst;
422 : 51829 : desigidx = *p++;
423 [ - + ]: 51829 : if (!(desigidx < sp->charcnt))
57 tgl@sss.pgh.pa.us 424 :UNC 0 : return EINVAL;
57 tgl@sss.pgh.pa.us 425 :GNC 51829 : ttisp->tt_desigidx = desigidx;
426 : : }
427 [ + + ]: 215553 : for (i = 0; i < sp->charcnt; ++i)
428 : 203553 : sp->chars[i] = *p++;
429 : :
430 : : /*
431 : : * Ensure '\0'-terminated, and make it safe to call ttunspecified
432 : : * later.
433 : : */
434 : 12000 : memset(&sp->chars[i], 0, CHARS_EXTRA);
435 : :
436 : : /* Read leap seconds, discarding those out of pg_time_t range. */
437 : 12000 : leapcnt = 0;
438 [ - + ]: 12000 : for (i = 0; i < leapcount(sp); i++)
439 : : {
57 tgl@sss.pgh.pa.us 440 [ # # ]:UNC 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))))
3804 tgl@sss.pgh.pa.us 464 :UBC 0 : return EINVAL;
57 tgl@sss.pgh.pa.us 465 :UNC 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 : : }
57 tgl@sss.pgh.pa.us 478 :GNC 12000 : set_leapcount(sp, leapcnt);
479 : :
480 [ + + ]: 63829 : for (i = 0; i < sp->typecnt; ++i)
481 : : {
482 : : struct ttinfo *ttisp;
483 : :
484 : 51829 : ttisp = &sp->ttis[i];
485 [ + - ]: 51829 : if (ttisstdcnt == 0)
486 : 51829 : ttisp->tt_ttisstd = false;
487 : : else
488 : : {
57 tgl@sss.pgh.pa.us 489 [ # # # # ]:UNC 0 : if (*p != true && *p != false)
490 : 0 : return EINVAL;
491 : 0 : ttisp->tt_ttisstd = *p++;
492 : : }
493 : : }
57 tgl@sss.pgh.pa.us 494 [ + + ]:GNC 63829 : for (i = 0; i < sp->typecnt; ++i)
495 : : {
496 : : struct ttinfo *ttisp;
497 : :
498 : 51829 : ttisp = &sp->ttis[i];
499 [ + - ]: 51829 : if (ttisutcnt == 0)
500 : 51829 : ttisp->tt_ttisut = false;
501 : : else
502 : : {
57 tgl@sss.pgh.pa.us 503 [ # # # # ]:UNC 0 : if (*p != true && *p != false)
504 : 0 : return EINVAL;
505 : 0 : ttisp->tt_ttisut = *p++;
506 : : }
507 : : }
508 : : }
509 : :
3804 tgl@sss.pgh.pa.us 510 :CBC 24000 : nread -= p - up->buf;
511 : 24000 : memmove(up->buf, p, nread);
512 : :
513 : : /* If this is an old file, we're done. */
57 tgl@sss.pgh.pa.us 514 [ - + ]:GNC 24000 : if (!version)
57 tgl@sss.pgh.pa.us 515 :UNC 0 : break;
516 : : }
57 tgl@sss.pgh.pa.us 517 [ + - + - ]:GNC 12000 : if ((tzloadflags & TZLOAD_TZSTRING) && nread > 2 &&
3804 tgl@sss.pgh.pa.us 518 [ + - + - ]:CBC 12000 : up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
6767 519 [ + - ]: 12000 : sp->typecnt + 2 <= TZ_MAX_TYPES)
520 : : {
3804 521 : 12000 : struct state *ts = &lsp->u.st;
522 : :
523 : 12000 : up->buf[nread - 1] = '\0';
57 tgl@sss.pgh.pa.us 524 [ + - ]:GNC 12000 : 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 : : */
3804 tgl@sss.pgh.pa.us 534 :CBC 12000 : int gotabbr = 0;
535 : 12000 : int charcnt = sp->charcnt;
536 : :
2869 537 [ + + ]: 30536 : for (i = 0; i < ts->typecnt; i++)
538 : : {
2598 539 : 18536 : char *tsabbr = ts->chars + ts->ttis[i].tt_desigidx;
540 : : int j;
541 : :
3804 542 [ + + ]: 155907 : for (j = 0; j < charcnt; j++)
543 [ + + ]: 155847 : if (strcmp(sp->chars + j, tsabbr) == 0)
544 : : {
2598 545 : 18476 : ts->ttis[i].tt_desigidx = j;
3804 546 : 18476 : gotabbr++;
547 : 18476 : break;
548 : : }
549 [ + + ]: 18536 : if (!(j < charcnt))
550 : : {
57 tgl@sss.pgh.pa.us 551 :GNC 60 : int tsabbrlen = strnlen(tsabbr, TZ_MAX_CHARS - j);
552 : :
3804 tgl@sss.pgh.pa.us 553 [ + - ]:CBC 60 : if (j + tsabbrlen < TZ_MAX_CHARS)
554 : : {
57 tgl@sss.pgh.pa.us 555 :GNC 60 : char *cp = sp->chars + j;
556 : :
557 : 60 : memcpy(cp, tsabbr, tsabbrlen);
558 : 60 : cp += tsabbrlen;
559 : 60 : *cp = '\0';
3804 tgl@sss.pgh.pa.us 560 :CBC 60 : charcnt = j + tsabbrlen + 1;
2598 561 : 60 : ts->ttis[i].tt_desigidx = j;
3804 562 : 60 : gotabbr++;
563 : : }
564 : : }
565 : : }
2869 566 [ + - ]: 12000 : if (gotabbr == ts->typecnt)
567 : : {
3804 568 : 12000 : 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 : : */
3406 575 : 12000 : while (1 < sp->timecnt
576 [ + + ]: 12348 : && (sp->types[sp->timecnt - 1]
577 [ + + ]: 10175 : == sp->types[sp->timecnt - 2]))
578 : 348 : sp->timecnt--;
579 : :
57 tgl@sss.pgh.pa.us 580 :GNC 12000 : sp->goahead = ts->goahead;
581 : :
3804 tgl@sss.pgh.pa.us 582 [ + + ]:CBC 5266298 : for (i = 0; i < ts->timecnt; i++)
583 : : {
57 tgl@sss.pgh.pa.us 584 :GNC 5254298 : pg_time_t t = ts->ats[i];
585 : :
586 [ + - ]: 5254298 : if (increment_overflow_time(&t, leapcorr(sp, t))
587 [ + - ]: 5254298 : || (0 < sp->timecnt
588 [ + + ]: 5254298 : && t <= sp->ats[sp->timecnt - 1]))
589 : 6118 : continue;
590 [ - + ]: 5248180 : if (TZ_MAX_TIMES <= sp->timecnt)
591 : : {
57 tgl@sss.pgh.pa.us 592 :UNC 0 : sp->goahead = false;
593 : 0 : break;
594 : : }
57 tgl@sss.pgh.pa.us 595 :GNC 5248180 : sp->ats[sp->timecnt] = t;
3804 tgl@sss.pgh.pa.us 596 :CBC 5248180 : sp->types[sp->timecnt] = (sp->typecnt
597 : 5248180 : + ts->types[i]);
598 : 5248180 : sp->timecnt++;
599 : : }
2869 600 [ + + ]: 30536 : for (i = 0; i < ts->typecnt; i++)
601 : 18536 : sp->ttis[sp->typecnt++] = ts->ttis[i];
602 : : }
603 : : }
604 : : }
605 [ - + ]: 12000 : if (sp->typecnt == 0)
2869 tgl@sss.pgh.pa.us 606 :UBC 0 : return EINVAL;
607 : :
8154 bruce@momjian.us 608 :CBC 12000 : 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
57 tgl@sss.pgh.pa.us 618 :GNC 12401 : 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
16 tgl@sss.pgh.pa.us 637 :CBC 12401 : lsp = malloc(sizeof *lsp);
638 [ - + ]: 12401 : if (!lsp)
16 tgl@sss.pgh.pa.us 639 :UBC 0 : return errno;
640 : : #endif
16 tgl@sss.pgh.pa.us 641 :GNC 12401 : r = tzloadbody(name, canonname, sp, tzloadflags, &lsp);
642 : : #ifndef USE_VALGRIND
16 tgl@sss.pgh.pa.us 643 :CBC 12401 : free(lsp);
644 : : #endif
645 : 12401 : 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
57 tgl@sss.pgh.pa.us 659 :GNC 192635 : is_digit(char c)
660 : : {
661 [ + + + + ]: 192635 : 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 *
7738 neilc@samurai.com 671 :CBC 17784 : getzname(const char *strp)
672 : : {
673 : : char c;
674 : :
8154 bruce@momjian.us 675 [ + + + + : 74735 : while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
+ + + + +
+ ]
676 : : c != '+')
8133 677 : 56951 : ++strp;
8154 678 : 17784 : 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 *
2869 tgl@sss.pgh.pa.us 692 : 2671 : getqzname(const char *strp, const int delim)
693 : : {
694 : : int c;
695 : :
6767 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 *
2869 709 : 55140 : getnum(const char *strp, int *const nump, const int min, const int max)
710 : : {
711 : : char c;
712 : : int num;
713 : :
8154 bruce@momjian.us 714 [ + - + + ]: 55140 : if (strp == NULL || !is_digit(c = *strp))
8154 bruce@momjian.us 715 :GBC 180 : return NULL;
8154 bruce@momjian.us 716 :CBC 54960 : num = 0;
717 : : do
718 : : {
719 : 62944 : num = num * 10 + (c - '0');
720 [ - + ]: 62944 : if (num > max)
8133 bruce@momjian.us 721 :UBC 0 : return NULL; /* illegal value */
8154 bruce@momjian.us 722 :CBC 62944 : c = *++strp;
723 [ + + ]: 62944 : } while (is_digit(c));
724 [ - + ]: 54960 : if (num < min)
8133 bruce@momjian.us 725 :UBC 0 : return NULL; /* illegal value */
8154 bruce@momjian.us 726 :CBC 54960 : *nump = num;
727 : 54960 : 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 *
279 peter@eisentraut.org 739 : 15480 : getsecs(const char *strp, int_fast32_t *const secsp)
740 : : {
741 : : int num;
57 tgl@sss.pgh.pa.us 742 :GNC 15480 : 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 : : */
8154 bruce@momjian.us 749 :CBC 15480 : strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
750 [ + + ]: 15480 : if (strp == NULL)
8154 bruce@momjian.us 751 :GBC 180 : return NULL;
57 tgl@sss.pgh.pa.us 752 :GNC 15300 : *secsp = num * secsperhour;
8133 bruce@momjian.us 753 [ + + ]:CBC 15300 : if (*strp == ':')
754 : : {
8154 755 : 366 : ++strp;
756 : 366 : strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
757 [ - + ]: 366 : if (strp == NULL)
8154 bruce@momjian.us 758 :UBC 0 : return NULL;
8154 bruce@momjian.us 759 :CBC 366 : *secsp += num * SECSPERMIN;
8133 760 [ - + ]: 366 : if (*strp == ':')
761 : : {
8154 bruce@momjian.us 762 :UBC 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 : : }
8154 bruce@momjian.us 770 :CBC 15300 : 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 *
279 peter@eisentraut.org 781 : 15480 : getoffset(const char *strp, int_fast32_t *const offsetp)
782 : : {
3804 tgl@sss.pgh.pa.us 783 : 15480 : bool neg = false;
784 : :
8133 bruce@momjian.us 785 [ + + ]: 15480 : if (*strp == '-')
786 : : {
3804 tgl@sss.pgh.pa.us 787 : 3367 : neg = true;
8154 bruce@momjian.us 788 : 3367 : ++strp;
789 : : }
8133 790 [ + + ]: 12113 : else if (*strp == '+')
8154 791 : 85 : ++strp;
792 : 15480 : strp = getsecs(strp, offsetp);
793 [ + + ]: 15480 : if (strp == NULL)
8133 bruce@momjian.us 794 :GBC 180 : return NULL; /* illegal time */
8154 bruce@momjian.us 795 [ + + ]:CBC 15300 : if (neg)
796 : 3367 : *offsetp = -*offsetp;
797 : 15300 : 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 *
2869 tgl@sss.pgh.pa.us 809 : 13098 : getrule(const char *strp, struct rule *const rulep)
810 : : {
8133 bruce@momjian.us 811 [ - + ]: 13098 : if (*strp == 'J')
812 : : {
813 : : /*
814 : : * Julian day.
815 : : */
8154 bruce@momjian.us 816 :UBC 0 : rulep->r_type = JULIAN_DAY;
817 : 0 : ++strp;
818 : 0 : strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
819 : : }
8133 bruce@momjian.us 820 [ + - ]:CBC 13098 : else if (*strp == 'M')
821 : : {
822 : : /*
823 : : * Month, week, day.
824 : : */
8154 825 : 13098 : rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
826 : 13098 : ++strp;
827 : 13098 : strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
828 [ - + ]: 13098 : if (strp == NULL)
8154 bruce@momjian.us 829 :UBC 0 : return NULL;
8154 bruce@momjian.us 830 [ - + ]:CBC 13098 : if (*strp++ != '.')
8154 bruce@momjian.us 831 :UBC 0 : return NULL;
8154 bruce@momjian.us 832 :CBC 13098 : strp = getnum(strp, &rulep->r_week, 1, 5);
833 [ - + ]: 13098 : if (strp == NULL)
8154 bruce@momjian.us 834 :UBC 0 : return NULL;
8154 bruce@momjian.us 835 [ - + ]:CBC 13098 : if (*strp++ != '.')
8154 bruce@momjian.us 836 :UBC 0 : return NULL;
8154 bruce@momjian.us 837 :CBC 13098 : strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
838 : : }
8133 bruce@momjian.us 839 [ # # ]:UBC 0 : else if (is_digit(*strp))
840 : : {
841 : : /*
842 : : * Day of year.
843 : : */
8154 844 : 0 : rulep->r_type = DAY_OF_YEAR;
845 : 0 : strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
846 : : }
847 : : else
8133 848 : 0 : return NULL; /* invalid format */
8154 bruce@momjian.us 849 [ - + ]:CBC 13098 : if (strp == NULL)
8154 bruce@momjian.us 850 :UBC 0 : return NULL;
8133 bruce@momjian.us 851 [ + + ]:CBC 13098 : if (*strp == '/')
852 : : {
853 : : /*
854 : : * Time specified.
855 : : */
8154 856 : 1520 : ++strp;
3804 tgl@sss.pgh.pa.us 857 : 1520 : strp = getoffset(strp, &rulep->r_time);
858 : : }
859 : : else
3354 860 : 11578 : rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
8154 bruce@momjian.us 861 : 13098 : 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
2869 tgl@sss.pgh.pa.us 870 : 5265396 : 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 : :
8154 bruce@momjian.us 883 [ + + + + : 5265396 : leapyear = isleap(year);
+ + ]
8133 884 [ - - + - ]: 5265396 : switch (rulep->r_type)
885 : : {
886 : :
8133 bruce@momjian.us 887 :UBC 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 : : */
3804 tgl@sss.pgh.pa.us 895 : 0 : value = (rulep->r_day - 1) * SECSPERDAY;
8133 bruce@momjian.us 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 : : */
3804 tgl@sss.pgh.pa.us 906 : 0 : value = rulep->r_day * SECSPERDAY;
8133 bruce@momjian.us 907 : 0 : break;
908 : :
8133 bruce@momjian.us 909 :CBC 5265396 : 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 : 5265396 : m1 = (rulep->r_mon + 9) % 12 + 1;
920 [ - + ]: 5265396 : yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
921 : 5265396 : yy1 = yy0 / 100;
922 : 5265396 : yy2 = yy0 % 100;
923 : 5265396 : dow = ((26 * m1 - 2) / 10 +
924 : 5265396 : 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
925 [ + + ]: 5265396 : if (dow < 0)
926 : 948811 : 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 : 5265396 : d = rulep->r_day - dow;
933 [ + + ]: 5265396 : if (d < 0)
934 : 4448161 : d += DAYSPERWEEK;
935 [ + + ]: 9890262 : for (i = 1; i < rulep->r_week; ++i)
936 : : {
937 : 5026206 : if (d + DAYSPERWEEK >=
280 peter@eisentraut.org 938 [ + + ]: 5026206 : mon_lengths[leapyear][rulep->r_mon - 1])
8154 bruce@momjian.us 939 : 401340 : break;
8133 940 : 4624866 : d += DAYSPERWEEK;
941 : : }
942 : :
943 : : /*
944 : : * "d" is the day-of-month (zero-origin) of the day we want.
945 : : */
3804 tgl@sss.pgh.pa.us 946 : 5265396 : value = d * SECSPERDAY;
947 [ + + ]: 36474666 : for (i = 0; i < rulep->r_mon - 1; ++i)
280 peter@eisentraut.org 948 : 31209270 : value += mon_lengths[leapyear][i] * SECSPERDAY;
8133 bruce@momjian.us 949 : 5265396 : break;
950 : :
57 tgl@sss.pgh.pa.us 951 :UNC 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 : : */
8154 bruce@momjian.us 960 :CBC 5265396 : 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
57 tgl@sss.pgh.pa.us 969 :GNC 13906 : tzparse(const char *name, struct state *sp, struct state const *basep)
970 : : {
971 : : const char *stdname;
8133 bruce@momjian.us 972 :CBC 13906 : 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;
57 tgl@sss.pgh.pa.us 979 :GNC 13906 : pg_time_t atlo = TIME_T_MIN,
980 : 13906 : leaplo = TIME_T_MIN;
981 : :
8154 bruce@momjian.us 982 :CBC 13906 : stdname = name;
57 tgl@sss.pgh.pa.us 983 [ + + ]:GNC 13906 : if (*name == '<')
984 : : {
985 : 2509 : name++;
986 : 2509 : stdname = name;
987 : 2509 : name = getqzname(name, '>');
988 [ - + ]: 2509 : if (*name != '>')
57 tgl@sss.pgh.pa.us 989 :UNC 0 : return false;
57 tgl@sss.pgh.pa.us 990 :GNC 2509 : stdlen = name - stdname;
991 : 2509 : name++;
992 : : }
993 : : else
994 : : {
995 : 11397 : name = getzname(name);
996 : 11397 : stdlen = name - stdname;
997 : : }
998 [ - + ]: 13906 : if (stdlen > TZNAME_MAXIMUM) /* allow empty STD abbrev, unlike IANA */
2871 tgl@sss.pgh.pa.us 999 :UBC 0 : return false;
57 tgl@sss.pgh.pa.us 1000 :GNC 13906 : name = getoffset(name, &stdoffset);
1001 [ + + ]: 13906 : if (name == NULL)
1002 : 180 : return false;
1003 : 13726 : charcnt = stdlen + 1;
1004 [ + + ]: 13726 : if (basep)
1005 : : {
1006 [ + + ]: 12000 : if (0 < basep->timecnt)
1007 : 10287 : atlo = basep->ats[basep->timecnt - 1];
1008 : 12000 : set_leapcount(sp, leapcount(basep));
1009 [ - + ]: 12000 : if (0 < leapcount(sp))
1010 : : {
1011 : : int i;
1012 : :
57 tgl@sss.pgh.pa.us 1013 [ # # ]:UNC 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
57 tgl@sss.pgh.pa.us 1019 :GNC 1726 : set_leapcount(sp, 0); /* So, we're off a little. */
1020 : 13726 : sp->goback = sp->goahead = false;
8133 bruce@momjian.us 1021 [ + + ]:CBC 13726 : if (*name != '\0')
1022 : : {
1023 : : struct rule start,
1024 : : end;
1025 : : int year,
1026 : : yearbeg,
1027 : : yearlim,
1028 : : timecnt;
1029 : : pg_time_t janfirst;
57 tgl@sss.pgh.pa.us 1030 :GNC 6549 : int_fast32_t janoffset = 0;
1031 : :
6767 tgl@sss.pgh.pa.us 1032 [ + + ]:CBC 6549 : if (*name == '<')
1033 : : {
1034 : 162 : dstname = ++name;
1035 : 162 : name = getqzname(name, '>');
1036 [ - + ]: 162 : if (*name != '>')
3804 tgl@sss.pgh.pa.us 1037 :UBC 0 : return false;
6767 tgl@sss.pgh.pa.us 1038 :CBC 162 : dstlen = name - dstname;
1039 : 162 : name++;
1040 : : }
1041 : : else
1042 : : {
1043 : 6387 : dstname = name;
1044 : 6387 : name = getzname(name);
2869 1045 : 6387 : dstlen = name - dstname; /* length of DST abbr. */
1046 : : }
57 tgl@sss.pgh.pa.us 1047 [ + - - + ]:GNC 6549 : if (!(0 < dstlen && dstlen <= TZNAME_MAXIMUM))
3804 tgl@sss.pgh.pa.us 1048 :UBC 0 : return false;
3804 tgl@sss.pgh.pa.us 1049 :CBC 6549 : charcnt += dstlen + 1;
8133 bruce@momjian.us 1050 [ + + + + : 6549 : if (*name != '\0' && *name != ',' && *name != ';')
+ - ]
1051 : : {
8154 1052 : 54 : name = getoffset(name, &dstoffset);
1053 [ - + ]: 54 : if (name == NULL)
3804 tgl@sss.pgh.pa.us 1054 :UBC 0 : return false;
1055 : : }
1056 : : else
8133 bruce@momjian.us 1057 :CBC 6495 : dstoffset = stdoffset - SECSPERHOUR;
1058 : :
57 tgl@sss.pgh.pa.us 1059 [ + + ]:GNC 6549 : if (*name == '\0')
1060 : 4 : name = TZDEFRULESTRING;
1061 [ - + - - ]: 6549 : if (!(*name == ',' || *name == ';'))
57 tgl@sss.pgh.pa.us 1062 :UNC 0 : return false;
1063 : :
57 tgl@sss.pgh.pa.us 1064 :GNC 6549 : name = getrule(name + 1, &start);
1065 [ - + ]: 6549 : if (!name)
57 tgl@sss.pgh.pa.us 1066 :UNC 0 : return false;
57 tgl@sss.pgh.pa.us 1067 [ - + ]:GNC 6549 : if (*name++ != ',')
57 tgl@sss.pgh.pa.us 1068 :UNC 0 : return false;
57 tgl@sss.pgh.pa.us 1069 :GNC 6549 : name = getrule(name, &end);
1070 [ + - - + ]: 6549 : if (!name || *name)
57 tgl@sss.pgh.pa.us 1071 :UNC 0 : return false;
57 tgl@sss.pgh.pa.us 1072 :GNC 6549 : sp->typecnt = 2; /* standard time and DST */
1073 : :
1074 : : /*
1075 : : * Two transitions per year, from EPOCH_YEAR forward.
1076 : : */
1077 : 6549 : init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1078 : 6549 : init_ttinfo(&sp->ttis[1], -dstoffset, true, stdlen + 1);
1079 : 6549 : timecnt = 0;
1080 : 6549 : janfirst = 0;
1081 : 6549 : yearbeg = EPOCH_YEAR;
1082 : :
1083 : : do
1084 : : {
1085 : 9136 : int_fast32_t yearsecs
1086 [ + + + + : 9136 : = year_lengths[isleap(yearbeg - 1)] * SECSPERDAY;
- + ]
1087 : 9136 : pg_time_t janfirst1 = janfirst;
1088 : :
1089 : 9136 : yearbeg--;
1090 [ - + ]: 9136 : if (increment_overflow_time(&janfirst1, -yearsecs))
1091 : : {
57 tgl@sss.pgh.pa.us 1092 :UNC 0 : janoffset = -yearsecs;
1093 : 0 : break;
1094 : : }
57 tgl@sss.pgh.pa.us 1095 :GNC 9136 : janfirst = janfirst1;
1096 : 9136 : } while (atlo < janfirst
1097 [ + + + + ]: 9136 : && EPOCH_YEAR - YEARSPERREPEAT / 2 < yearbeg);
1098 : :
1099 : : while (true)
8133 bruce@momjian.us 1100 :GIC 245561 : {
57 tgl@sss.pgh.pa.us 1101 :GNC 252110 : int_fast32_t yearsecs
1102 [ + + + + : 252110 : = year_lengths[isleap(yearbeg)] * SECSPERDAY;
+ - ]
1103 : 252110 : int yearbeg1 = yearbeg;
1104 : 252110 : pg_time_t janfirst1 = janfirst;
1105 : :
1106 [ + - ]: 252110 : if (increment_overflow_time(&janfirst1, yearsecs)
1107 [ + - ]: 252110 : || increment_overflow(&yearbeg1, 1)
1108 [ + + ]: 252110 : || atlo <= janfirst1)
1109 : : break;
1110 : 245561 : yearbeg = yearbeg1;
1111 : 245561 : janfirst = janfirst1;
1112 : : }
1113 : :
1114 : 6549 : yearlim = yearbeg;
1115 [ - + ]: 6549 : if (increment_overflow(&yearlim, years_of_observations))
57 tgl@sss.pgh.pa.us 1116 :UNC 0 : yearlim = INT_MAX;
57 tgl@sss.pgh.pa.us 1117 [ + + ]:GNC 2639247 : for (year = yearbeg; year < yearlim; year++)
1118 : : {
1119 : : int_fast32_t
1120 : 2632698 : starttime = transtime(year, &start, stdoffset),
1121 : 2632698 : endtime = transtime(year, &end, dstoffset),
1122 [ + + + + : 2632698 : yearsecs = year_lengths[isleap(year)] * SECSPERDAY;
+ + ]
1123 : 2632698 : bool reversed = endtime < starttime;
1124 : :
1125 [ + + ]: 2632698 : if (reversed)
1126 : : {
1127 : 123012 : int_fast32_t swap = starttime;
1128 : :
1129 : 123012 : starttime = endtime;
1130 : 123012 : endtime = swap;
1131 : : }
1132 [ + + ]: 2632698 : if (reversed
1133 [ + - ]: 2509686 : || (starttime < endtime
1134 [ + - ]: 2509686 : && endtime - starttime < yearsecs))
1135 : : {
1136 [ - + ]: 2632698 : if (TZ_MAX_TIMES - 2 < timecnt)
8154 bruce@momjian.us 1137 :UBC 0 : break;
57 tgl@sss.pgh.pa.us 1138 :GNC 2632698 : sp->ats[timecnt] = janfirst;
1139 [ + - ]: 2632698 : if (!increment_overflow_time(&sp->ats[timecnt],
1140 : : janoffset + starttime)
1141 [ + + ]: 2632698 : && atlo <= sp->ats[timecnt])
1142 : 2632240 : sp->types[timecnt++] = !reversed;
1143 : 2632698 : sp->ats[timecnt] = janfirst;
1144 [ + - ]: 2632698 : if (!increment_overflow_time(&sp->ats[timecnt],
1145 : : janoffset + endtime)
1146 [ + + ]: 2632698 : && atlo <= sp->ats[timecnt])
1147 : : {
1148 : 2632510 : sp->types[timecnt++] = reversed;
1149 : : }
1150 : : }
1151 [ - + ]: 2632698 : if (endtime < leaplo)
1152 : : {
57 tgl@sss.pgh.pa.us 1153 :UNC 0 : yearlim = year;
1154 [ # # ]: 0 : if (increment_overflow(&yearlim, years_of_observations))
1155 : 0 : yearlim = INT_MAX;
1156 : : }
57 tgl@sss.pgh.pa.us 1157 [ - + ]:GNC 2632698 : if (increment_overflow_time(&janfirst, janoffset + yearsecs))
57 tgl@sss.pgh.pa.us 1158 :UNC 0 : break;
57 tgl@sss.pgh.pa.us 1159 :GNC 2632698 : janoffset = 0;
1160 : : }
1161 : 6549 : sp->timecnt = timecnt;
1162 [ - + ]: 6549 : if (!timecnt)
1163 : : {
57 tgl@sss.pgh.pa.us 1164 :UNC 0 : sp->ttis[0] = sp->ttis[1];
1165 : 0 : sp->typecnt = 1; /* Perpetual DST. */
1166 : : }
57 tgl@sss.pgh.pa.us 1167 [ + - ]:GNC 6549 : else if (years_of_observations <= year - yearbeg)
1168 : 6549 : sp->goback = sp->goahead = true;
1169 : : }
1170 : : else
1171 : : {
8154 bruce@momjian.us 1172 :CBC 7177 : dstlen = 0;
1173 : 7177 : sp->typecnt = 1; /* only standard time */
1174 : 7177 : sp->timecnt = 0;
3804 tgl@sss.pgh.pa.us 1175 : 7177 : init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1176 : : }
1177 : 13726 : sp->charcnt = charcnt;
8154 bruce@momjian.us 1178 : 13726 : cp = sp->chars;
3804 tgl@sss.pgh.pa.us 1179 : 13726 : memcpy(cp, stdname, stdlen);
8154 bruce@momjian.us 1180 : 13726 : cp += stdlen;
1181 : 13726 : *cp++ = '\0';
8133 1182 [ + + ]: 13726 : if (dstlen != 0)
1183 : : {
3804 tgl@sss.pgh.pa.us 1184 : 6549 : memcpy(cp, dstname, dstlen);
57 tgl@sss.pgh.pa.us 1185 :GNC 6549 : cp += dstlen;
1186 : 6549 : *cp = '\0';
1187 : : }
3804 tgl@sss.pgh.pa.us 1188 :CBC 13726 : return true;
1189 : : }
1190 : :
1191 : : static void
2869 1192 : 1505 : gmtload(struct state *const sp)
1193 : : {
1194 : : /* PG: for historical compatibility, use "GMT" not "UTC" as TZ abbrev */
57 tgl@sss.pgh.pa.us 1195 :GNC 1505 : tzparse("GMT0", sp, NULL);
8154 bruce@momjian.us 1196 :CBC 1505 : }
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 *
3354 tgl@sss.pgh.pa.us 1206 : 817970 : 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;
8120 1212 : 817970 : const pg_time_t t = *timep;
1213 : :
3804 1214 [ - + ]: 817970 : if (sp == NULL)
3804 tgl@sss.pgh.pa.us 1215 :UBC 0 : return gmtsub(timep, 0, tmp);
6767 tgl@sss.pgh.pa.us 1216 [ + + + - ]:CBC 817970 : if ((sp->goback && t < sp->ats[0]) ||
1217 [ + + + + ]: 817970 : (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])
6767 tgl@sss.pgh.pa.us 1224 :UBC 0 : seconds = sp->ats[0] - t;
1225 : : else
6286 bruce@momjian.us 1226 :CBC 44 : seconds = t - sp->ats[sp->timecnt - 1];
6767 tgl@sss.pgh.pa.us 1227 : 44 : --seconds;
1228 : :
1229 : : /*
1230 : : * Beware integer overflow, as SECONDS might be close to the maximum
1231 : : * pg_time_t.
1232 : : */
57 tgl@sss.pgh.pa.us 1233 :GNC 44 : years = seconds / SECSPERREPEAT * YEARSPERREPEAT;
3804 tgl@sss.pgh.pa.us 1234 :CBC 44 : seconds = years * AVGSECSPERYEAR;
57 tgl@sss.pgh.pa.us 1235 :GNC 44 : years += YEARSPERREPEAT;
6767 tgl@sss.pgh.pa.us 1236 [ - + ]:CBC 44 : if (t < sp->ats[0])
57 tgl@sss.pgh.pa.us 1237 :UNC 0 : newt = t + seconds + SECSPERREPEAT;
1238 : : else
57 tgl@sss.pgh.pa.us 1239 :GNC 44 : newt = t - seconds - SECSPERREPEAT;
1240 : :
6767 tgl@sss.pgh.pa.us 1241 [ + - ]:CBC 44 : if (newt < sp->ats[0] ||
1242 [ - + ]: 44 : newt > sp->ats[sp->timecnt - 1])
6286 bruce@momjian.us 1243 :UBC 0 : return NULL; /* "cannot happen" */
3804 tgl@sss.pgh.pa.us 1244 :CBC 44 : result = localsub(sp, &newt, tmp);
1245 [ + - ]: 44 : if (result)
1246 : : {
1247 : : #if defined ckd_add && defined ckd_sub
57 tgl@sss.pgh.pa.us 1248 [ - + - + ]:GNC 88 : if (t < sp->ats[0]
57 tgl@sss.pgh.pa.us 1249 :UNC 0 : ? ckd_sub(&result->tm_year,
1250 : : result->tm_year, years)
57 tgl@sss.pgh.pa.us 1251 :GNC 44 : : ckd_add(&result->tm_year,
1252 : : result->tm_year, years))
57 tgl@sss.pgh.pa.us 1253 :UNC 0 : return NULL;
1254 : : #else
1255 : : int_fast64_t newy;
1256 : :
3804 tgl@sss.pgh.pa.us 1257 :ECB (44) : newy = result->tm_year;
6767 1258 [ - + ]: (44) : if (t < sp->ats[0])
3804 tgl@sss.pgh.pa.us 1259 :EUB : newy -= years;
1260 : : else
3804 tgl@sss.pgh.pa.us 1261 :ECB (44) : newy += years;
1262 [ + - - + ]: (44) : if (!(INT_MIN <= newy && newy <= INT_MAX))
6767 tgl@sss.pgh.pa.us 1263 :EUB : return NULL;
3804 tgl@sss.pgh.pa.us 1264 :ECB (44) : result->tm_year = newy;
1265 : : #endif
1266 : : }
6767 tgl@sss.pgh.pa.us 1267 :CBC 44 : return result;
1268 : : }
8133 bruce@momjian.us 1269 [ + + + + ]: 817926 : if (sp->timecnt == 0 || t < sp->ats[0])
1270 : : {
57 tgl@sss.pgh.pa.us 1271 :GNC 738395 : i = 0;
1272 : : }
1273 : : else
1274 : : {
6286 bruce@momjian.us 1275 :CBC 79531 : int lo = 1;
1276 : 79531 : int hi = sp->timecnt;
1277 : :
6767 tgl@sss.pgh.pa.us 1278 [ + + ]: 820078 : while (lo < hi)
1279 : : {
6286 bruce@momjian.us 1280 : 740547 : int mid = (lo + hi) >> 1;
1281 : :
6767 tgl@sss.pgh.pa.us 1282 [ + + ]: 740547 : if (t < sp->ats[mid])
1283 : 428872 : hi = mid;
1284 : : else
6286 bruce@momjian.us 1285 : 311675 : lo = mid + 1;
1286 : : }
57 tgl@sss.pgh.pa.us 1287 :GNC 79531 : i = sp->types[lo - 1];
1288 : : }
8154 bruce@momjian.us 1289 :CBC 817926 : 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 : : */
2598 tgl@sss.pgh.pa.us 1296 : 817926 : result = timesub(&t, ttisp->tt_utoff, sp, tmp);
3804 1297 [ + - ]: 817926 : if (result)
1298 : : {
1299 : 817926 : result->tm_isdst = ttisp->tt_isdst;
1300 : : #ifdef TM_ZONE
57 tgl@sss.pgh.pa.us 1301 :GNC 817926 : result->TM_ZONE = UNCONST(&sp->chars[ttisp->tt_desigidx]);
1302 : : #endif
1303 : : }
6767 tgl@sss.pgh.pa.us 1304 :CBC 817926 : return result;
1305 : : }
1306 : :
1307 : :
1308 : : struct pg_tm *
7800 bruce@momjian.us 1309 : 817926 : pg_localtime(const pg_time_t *timep, const pg_tz *tz)
1310 : : {
3804 tgl@sss.pgh.pa.us 1311 : 817926 : 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 *
57 tgl@sss.pgh.pa.us 1322 :GNC 208068 : 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 : :
2872 tgl@sss.pgh.pa.us 1330 [ + + ]:CBC 208068 : if (gmtptr == NULL)
1331 : : {
1332 : : /* Allocate on first use */
1333 : 216 : gmtptr = (struct state *) malloc(sizeof(struct state));
1334 [ - + ]: 216 : if (gmtptr == NULL)
2872 tgl@sss.pgh.pa.us 1335 :UBC 0 : return NULL; /* errno should be set by malloc */
8133 tgl@sss.pgh.pa.us 1336 :CBC 216 : gmtload(gmtptr);
1337 : : }
1338 : :
6767 1339 : 208068 : 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 : : */
57 tgl@sss.pgh.pa.us 1346 [ + - ]:GNC 208068 : tmp->TM_ZONE = UNCONST(offset ? wildabbr
1347 : : : gmtptr->chars);
1348 : : #endif /* defined TM_ZONE */
6767 tgl@sss.pgh.pa.us 1349 :CBC 208068 : return result;
1350 : : }
1351 : :
1352 : : struct pg_tm *
8120 1353 : 208068 : pg_gmtime(const pg_time_t *timep)
1354 : : {
3804 1355 : 208068 : 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
57 tgl@sss.pgh.pa.us 1364 :GNC 4283478 : leaps_thru_end_of_nonneg(pg_time_t y)
1365 : : {
3261 tgl@sss.pgh.pa.us 1366 :CBC 4283478 : return y / 4 - y / 100 + y / 400;
1367 : : }
1368 : :
1369 : : static pg_time_t
57 tgl@sss.pgh.pa.us 1370 :GNC 4283478 : leaps_thru_end_of(pg_time_t y)
1371 : : {
1372 : : return (y < 0
3261 tgl@sss.pgh.pa.us 1373 :CBC 1496 : ? -1 - leaps_thru_end_of_nonneg(-1 - y)
1374 [ + + ]: 4284974 : : leaps_thru_end_of_nonneg(y));
1375 : : }
1376 : :
1377 : : static struct pg_tm *
279 peter@eisentraut.org 1378 : 1025994 : 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 : : */
57 tgl@sss.pgh.pa.us 1396 :GNC 1025994 : pg_time_t secs_since_posleap = SECSPERMIN;
1397 : :
8154 bruce@momjian.us 1398 :CBC 1025994 : corr = 0;
57 tgl@sss.pgh.pa.us 1399 [ + - ]:GNC 1025994 : i = sp ? leapcount(sp) : 0;
8133 bruce@momjian.us 1400 [ - + ]:CBC 1025994 : while (--i >= 0)
1401 : : {
57 tgl@sss.pgh.pa.us 1402 :UNC 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;
8154 bruce@momjian.us 1409 :UBC 0 : break;
1410 : : }
1411 : : }
1412 : :
1413 : : /*
1414 : : * Calculate the year, avoiding integer overflow even if pg_time_t is
1415 : : * unsigned.
1416 : : */
6767 tgl@sss.pgh.pa.us 1417 :CBC 1025994 : tdays = *timep / SECSPERDAY;
3804 1418 : 1025994 : rem = *timep % SECSPERDAY;
57 tgl@sss.pgh.pa.us 1419 :GNC 1025994 : rem += offset % SECSPERDAY - corr % SECSPERDAY + 3 * SECSPERDAY;
1420 : 1025994 : dayoff = offset / SECSPERDAY - corr / SECSPERDAY + rem / SECSPERDAY - 3;
1421 : 1025994 : 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 : 1025994 : dayrem = tdays % DAYSPERREPEAT;
1430 : 1025994 : dayrem += dayoff % DAYSPERREPEAT;
1431 : 1025994 : y = (EPOCH_YEAR - YEARSPERREPEAT
1432 : 1025994 : + ((1 + dayoff / DAYSPERREPEAT + dayrem / DAYSPERREPEAT
1433 : 1025994 : - ((dayrem % DAYSPERREPEAT) < 0)
1434 : 1025994 : + tdays / DAYSPERREPEAT)
1435 : : * YEARSPERREPEAT));
1436 : : /* idays = (tdays + dayoff) mod DAYSPERREPEAT, sans overflow. */
1437 : 1025994 : idays = tdays % DAYSPERREPEAT;
1438 : 1025994 : idays += dayoff % DAYSPERREPEAT + 2 * DAYSPERREPEAT;
1439 : 1025994 : idays %= DAYSPERREPEAT;
1440 : : /* Increase Y and decrease IDAYS until IDAYS is in range for Y. */
1441 [ + + + + : 2141739 : while (year_lengths[isleap(y)] <= idays)
+ + + + ]
1442 : : {
1443 : 1115745 : int tdelta = idays / DAYSPERLYEAR;
1444 : 1115745 : int_fast32_t ydelta = tdelta + !tdelta;
1445 : 1115745 : pg_time_t newy = y + ydelta;
1446 : : int leapdays;
1447 : :
6767 tgl@sss.pgh.pa.us 1448 :CBC 1115745 : leapdays = leaps_thru_end_of(newy - 1) -
1449 : 1115745 : leaps_thru_end_of(y - 1);
57 tgl@sss.pgh.pa.us 1450 :GNC 1115745 : idays -= ydelta * DAYSPERNYEAR;
1451 : 1115745 : idays -= leapdays;
6767 tgl@sss.pgh.pa.us 1452 :CBC 1115745 : y = newy;
1453 : : }
1454 : :
1455 : : #ifdef ckd_add
57 tgl@sss.pgh.pa.us 1456 [ - + ]:GNC 1025994 : if (ckd_add(&tmp->tm_year, y, -TM_YEAR_BASE))
1457 : : {
57 tgl@sss.pgh.pa.us 1458 :UNC 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
6767 tgl@sss.pgh.pa.us 1477 :CBC 1025994 : tmp->tm_yday = idays;
1478 : :
1479 : : /*
1480 : : * The "extra" mods below avoid overflow problems.
1481 : : */
57 tgl@sss.pgh.pa.us 1482 :GNC 1025994 : tmp->tm_wday = (TM_WDAY_BASE
1483 : 1025994 : + ((tmp->tm_year % DAYSPERWEEK)
1484 : 1025994 : * (DAYSPERNYEAR % DAYSPERWEEK))
1485 : 1025994 : + leaps_thru_end_of(y - 1)
1486 : 1025994 : - leaps_thru_end_of(TM_YEAR_BASE - 1)
1487 : 1025994 : + idays);
6767 tgl@sss.pgh.pa.us 1488 :CBC 1025994 : tmp->tm_wday %= DAYSPERWEEK;
8154 bruce@momjian.us 1489 [ + + ]: 1025994 : if (tmp->tm_wday < 0)
1490 : 1093 : tmp->tm_wday += DAYSPERWEEK;
57 tgl@sss.pgh.pa.us 1491 :GNC 1025994 : tmp->tm_hour = rem / SECSPERHOUR;
6767 tgl@sss.pgh.pa.us 1492 :CBC 1025994 : rem %= SECSPERHOUR;
57 tgl@sss.pgh.pa.us 1493 :GNC 1025994 : tmp->tm_min = rem / SECSPERMIN;
1494 : 1025994 : 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 : 1025994 : tmp->tm_sec += secs_since_posleap <= tmp->tm_sec;
1501 : :
6767 tgl@sss.pgh.pa.us 1502 [ + + + + :CBC 1025994 : ip = mon_lengths[isleap(y)];
+ + ]
1503 [ + + ]: 7245408 : for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1504 : 6219414 : idays -= ip[tmp->tm_mon];
57 tgl@sss.pgh.pa.us 1505 :GNC 1025994 : tmp->tm_mday = idays + 1;
8154 bruce@momjian.us 1506 :CBC 1025994 : tmp->tm_isdst = 0;
1507 : : #ifdef TM_GMTOFF
57 tgl@sss.pgh.pa.us 1508 :GNC 1025994 : tmp->TM_GMTOFF = offset;
1509 : : #endif /* defined TM_GMTOFF */
6767 tgl@sss.pgh.pa.us 1510 :CBC 1025994 : 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
3804 1531 : 258659 : increment_overflow(int *ip, int j)
1532 : : {
1533 : : #ifdef ckd_add
57 tgl@sss.pgh.pa.us 1534 :GNC 258659 : return ckd_add(ip, *ip, j);
1535 : : #else
3804 tgl@sss.pgh.pa.us 1536 :ECB (2183073) : 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 [ + + - + ]: (2183073) : if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
3804 tgl@sss.pgh.pa.us 1546 :EUB : return true;
3804 tgl@sss.pgh.pa.us 1547 :ECB (2183073) : *ip += j;
1548 : (2183073) : return false;
1549 : : #endif
1550 : : }
1551 : :
1552 : : static bool
57 tgl@sss.pgh.pa.us 1553 :GNC 13413638 : increment_overflow_time(pg_time_t *tp, int_fast32_2s j)
1554 : : {
1555 : : #ifdef ckd_add
1556 : 13413638 : 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 : : */
3804 tgl@sss.pgh.pa.us 1564 [ + + - + ]:ECB (41900800) : if (!(j < 0
3261 1565 : (1309400) : ? (TYPE_SIGNED(pg_time_t) ? TIME_T_MIN - j <= *tp : -1 - j < *tp)
1566 : (19641000) : : *tp <= TIME_T_MAX - j))
3804 tgl@sss.pgh.pa.us 1567 :EUB : return true;
3804 tgl@sss.pgh.pa.us 1568 :ECB (20950400) : *tp += j;
1569 : (20950400) : return false;
1570 : : #endif
1571 : : }
1572 : :
1573 : : static int_fast32_2s
2262 tgl@sss.pgh.pa.us 1574 :CBC 5254298 : leapcorr(struct state const *sp, pg_time_t t)
1575 : : {
1576 : : int i;
1577 : :
57 tgl@sss.pgh.pa.us 1578 :GNC 5254298 : i = leapcount(sp);
2262 tgl@sss.pgh.pa.us 1579 [ - + ]:CBC 5254298 : while (--i >= 0)
1580 : : {
57 tgl@sss.pgh.pa.us 1581 :UNC 0 : struct lsinfo ls = lsinfo(sp, i);
1582 : :
1583 [ # # ]: 0 : if (ls.ls_trans <= t)
1584 : 0 : return ls.ls_corr;
1585 : : }
2262 tgl@sss.pgh.pa.us 1586 :CBC 5254298 : 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
57 tgl@sss.pgh.pa.us 1610 :GNC 13690 : pg_tzload(const char *name, char *canonname, struct state *sp)
1611 : : {
1612 [ + + ]: 13690 : if (strcmp(name, "GMT") == 0)
1613 : : {
1614 : 1289 : gmtload(sp);
1615 : : /* Use given name as canonical */
1616 [ + + ]: 1289 : if (canonname)
1617 : 1279 : strcpy(canonname, name);
1618 : : }
1619 [ + + ]: 12401 : else if (tzload(name, canonname, sp, TZLOAD_TZSTRING) != 0)
1620 : : {
1621 [ + - + + ]: 401 : if (name[0] == ':' || !tzparse(name, sp, NULL))
1622 : : {
1623 : : /* Unknown timezone. Fail our call instead of loading GMT! */
1624 : 180 : return false;
1625 : : }
1626 : : /* For POSIX timezone specs, use given name as canonical */
1627 [ + - ]: 221 : if (canonname)
1628 : 221 : strcpy(canonname, name);
1629 : : }
1630 : 13510 : 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
7969 tgl@sss.pgh.pa.us 1654 :CBC 109788 : 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 : 109788 : const pg_time_t t = *timep;
1667 : :
7800 bruce@momjian.us 1668 : 109788 : sp = &tz->state;
7969 tgl@sss.pgh.pa.us 1669 [ + + ]: 109788 : if (sp->timecnt == 0)
1670 : : {
1671 : : /* non-DST zone, use the defaulttype (now always 0) */
57 tgl@sss.pgh.pa.us 1672 :GNC 2635 : ttisp = &sp->ttis[0];
2598 tgl@sss.pgh.pa.us 1673 :CBC 2635 : *before_gmtoff = ttisp->tt_utoff;
7969 1674 : 2635 : *before_isdst = ttisp->tt_isdst;
1675 : 2635 : return 0;
1676 : : }
6767 1677 [ + + + - ]: 107153 : if ((sp->goback && t < sp->ats[0]) ||
1678 [ + + + + ]: 107153 : (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])
6767 tgl@sss.pgh.pa.us 1688 :UBC 0 : seconds = sp->ats[0] - t;
1689 : : else
6286 bruce@momjian.us 1690 :CBC 34064 : seconds = t - sp->ats[sp->timecnt - 1];
6767 tgl@sss.pgh.pa.us 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)
6767 tgl@sss.pgh.pa.us 1696 :UBC 0 : return -1;
6767 tgl@sss.pgh.pa.us 1697 :CBC 34064 : seconds = icycles;
1698 : 34064 : seconds *= YEARSPERREPEAT;
1699 : 34064 : seconds *= AVGSECSPERYEAR;
1700 [ - + ]: 34064 : if (t < sp->ats[0])
6767 tgl@sss.pgh.pa.us 1701 :UBC 0 : newt += seconds;
1702 : : else
6286 bruce@momjian.us 1703 :CBC 34064 : newt -= seconds;
6767 tgl@sss.pgh.pa.us 1704 [ + - ]: 34064 : if (newt < sp->ats[0] ||
1705 [ - + ]: 34064 : newt > sp->ats[sp->timecnt - 1])
6286 bruce@momjian.us 1706 :UBC 0 : return -1; /* "cannot happen" */
1707 : :
6767 tgl@sss.pgh.pa.us 1708 :CBC 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])
6767 tgl@sss.pgh.pa.us 1715 :UBC 0 : *boundary -= seconds;
1716 : : else
6767 tgl@sss.pgh.pa.us 1717 :CBC 34064 : *boundary += seconds;
1718 : 34064 : return result;
1719 : : }
1720 : :
5237 1721 [ + + ]: 73089 : if (t >= sp->ats[sp->timecnt - 1])
1722 : : {
1723 : : /* No known transition > t, so use last known segment's type */
7969 1724 : 720 : i = sp->types[sp->timecnt - 1];
1725 : 720 : ttisp = &sp->ttis[i];
2598 1726 : 720 : *before_gmtoff = ttisp->tt_utoff;
7969 1727 : 720 : *before_isdst = ttisp->tt_isdst;
1728 : 720 : return 0;
1729 : : }
5237 1730 [ + + ]: 72369 : if (t < sp->ats[0])
1731 : : {
1732 : : /* For "before", use the defaulttype (now always 0) */
57 tgl@sss.pgh.pa.us 1733 :GNC 384 : ttisp = &sp->ttis[0];
2598 tgl@sss.pgh.pa.us 1734 :CBC 384 : *before_gmtoff = ttisp->tt_utoff;
7969 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];
2598 1740 : 384 : *after_gmtoff = ttisp->tt_utoff;
7969 1741 : 384 : *after_isdst = ttisp->tt_isdst;
1742 : 384 : return 1;
1743 : : }
1744 : : /* Else search to find the boundary following t */
1745 : : {
6286 bruce@momjian.us 1746 : 71985 : int lo = 1;
5237 tgl@sss.pgh.pa.us 1747 : 71985 : int hi = sp->timecnt - 1;
1748 : :
6767 1749 [ + + ]: 783578 : while (lo < hi)
1750 : : {
6286 bruce@momjian.us 1751 : 711593 : int mid = (lo + hi) >> 1;
1752 : :
6767 tgl@sss.pgh.pa.us 1753 [ + + ]: 711593 : if (t < sp->ats[mid])
1754 : 401995 : hi = mid;
1755 : : else
6286 bruce@momjian.us 1756 : 309598 : lo = mid + 1;
1757 : : }
6767 tgl@sss.pgh.pa.us 1758 : 71985 : i = lo;
1759 : : }
7969 1760 : 71985 : j = sp->types[i - 1];
1761 : 71985 : ttisp = &sp->ttis[j];
2598 1762 : 71985 : *before_gmtoff = ttisp->tt_utoff;
7969 1763 : 71985 : *before_isdst = ttisp->tt_isdst;
1764 : 71985 : *boundary = sp->ats[i];
1765 : 71985 : j = sp->types[i];
1766 : 71985 : ttisp = &sp->ttis[j];
2598 1767 : 71985 : *after_gmtoff = ttisp->tt_utoff;
7969 1768 : 71985 : *after_isdst = ttisp->tt_isdst;
1769 : 71985 : 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
4333 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)
3804 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 : : {
4333 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]];
2598 1853 [ + + ]: 13682 : if (ttisp->tt_desigidx == abbrind)
1854 : : {
1855 : 326 : *gmtoff = ttisp->tt_utoff;
4333 1856 : 326 : *isdst = ttisp->tt_isdst;
3804 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 : : */
57 tgl@sss.pgh.pa.us 1865 :GNC 28 : ttisp = &sp->ttis[0];
588 tgl@sss.pgh.pa.us 1866 [ + - ]:CBC 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 : : */
4333 tgl@sss.pgh.pa.us 1876 [ # # ]:UBC 0 : for (i = cutoff; i < sp->timecnt; i++)
1877 : : {
1878 : 0 : ttisp = &sp->ttis[sp->types[i]];
2598 1879 [ # # ]: 0 : if (ttisp->tt_desigidx == abbrind)
1880 : : {
1881 : 0 : *gmtoff = ttisp->tt_utoff;
4333 1882 : 0 : *isdst = ttisp->tt_isdst;
3804 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
588 tgl@sss.pgh.pa.us 1905 :CBC 4151 : 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 : 4151 : bool result = false;
1912 : 4151 : 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 : 4151 : abbrs = sp->chars;
1921 : 4151 : abbrind = 0;
1922 [ + + ]: 23573 : while (abbrind < sp->charcnt)
1923 : : {
1924 [ + + ]: 19554 : if (strcmp(abbrev, abbrs + abbrind) == 0)
1925 : 132 : break;
1926 [ + + ]: 77728 : while (abbrs[abbrind] != '\0')
1927 : 58306 : abbrind++;
1928 : 19422 : abbrind++;
1929 : : }
1930 [ + + ]: 4151 : if (abbrind >= sp->charcnt)
1931 : 4019 : return false; /* definitely not there */
1932 : :
1933 : : /*
1934 : : * Scan the ttinfo array to find uses of the abbreviation.
1935 : : */
1936 [ + + ]: 1043 : for (int i = 0; i < sp->typecnt; i++)
1937 : : {
1938 : 911 : const struct ttinfo *ttisp = &sp->ttis[i];
1939 : :
1940 [ + + ]: 911 : if (ttisp->tt_desigidx == abbrind)
1941 : : {
1942 [ + + ]: 248 : if (!result)
1943 : : {
1944 : : /* First usage */
1945 : 132 : *isfixed = true; /* for the moment */
1946 : 132 : *gmtoff = ttisp->tt_utoff;
1947 : 132 : *isdst = ttisp->tt_isdst;
1948 : 132 : result = true;
1949 : : }
1950 : : else
1951 : : {
1952 : : /* Second or later usage, does it match? */
1953 [ + - ]: 116 : if (*gmtoff != ttisp->tt_utoff ||
1954 [ - + ]: 116 : *isdst != ttisp->tt_isdst)
1955 : : {
588 tgl@sss.pgh.pa.us 1956 :UBC 0 : *isfixed = false;
1957 : 0 : break; /* no point in looking further */
1958 : : }
1959 : : }
1960 : : }
1961 : : }
1962 : :
588 tgl@sss.pgh.pa.us 1963 :CBC 132 : 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
7253 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 : : {
2598 2022 [ + + ]: 74 : if (sp->ttis[i].tt_utoff != sp->ttis[0].tt_utoff)
7253 2023 : 56 : return false;
2024 : : }
2598 2025 : 565 : *gmtoff = sp->ttis[0].tt_utoff;
7253 2026 : 565 : return true;
2027 : : }
2028 : :
2029 : : /*
2030 : : * Return the name of the current timezone
2031 : : */
2032 : : const char *
7800 bruce@momjian.us 2033 : 40522 : pg_get_timezone_name(pg_tz *tz)
2034 : : {
2035 [ + - ]: 40522 : if (tz)
2036 : 40522 : return tz->TZname;
8133 tgl@sss.pgh.pa.us 2037 :UBC 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
5466 tgl@sss.pgh.pa.us 2048 :CBC 23509 : 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 : 23509 : time2000 = (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY;
2059 : 23509 : tt = pg_localtime(&time2000, tz);
2060 [ + - - + ]: 23509 : if (!tt || tt->tm_sec != 0)
5466 tgl@sss.pgh.pa.us 2061 :UBC 0 : return false;
2062 : :
5466 tgl@sss.pgh.pa.us 2063 :CBC 23509 : return true;
2064 : : }
|