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