Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * datetime.c
4 : * Support functions for date/time types.
5 : *
6 : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : *
10 : * IDENTIFICATION
11 : * src/backend/utils/adt/datetime.c
12 : *
13 : *-------------------------------------------------------------------------
14 : */
15 : #include "postgres.h"
16 :
17 : #include <ctype.h>
18 : #include <limits.h>
19 : #include <math.h>
20 :
21 : #include "access/htup_details.h"
22 : #include "access/xact.h"
23 : #include "common/int.h"
24 : #include "common/string.h"
25 : #include "funcapi.h"
26 : #include "miscadmin.h"
27 : #include "nodes/nodeFuncs.h"
28 : #include "parser/scansup.h"
29 : #include "utils/builtins.h"
30 : #include "utils/date.h"
31 : #include "utils/datetime.h"
32 : #include "utils/guc.h"
33 : #include "utils/tuplestore.h"
34 : #include "utils/tzparser.h"
35 :
36 : static int DecodeNumber(int flen, char *str, bool haveTextMonth,
37 : int fmask, int *tmask,
38 : struct pg_tm *tm, fsec_t *fsec, bool *is2digits);
39 : static int DecodeNumberField(int len, char *str,
40 : int fmask, int *tmask,
41 : struct pg_tm *tm, fsec_t *fsec, bool *is2digits);
42 : static int DecodeTimeCommon(char *str, int fmask, int range,
43 : int *tmask, struct pg_itm *itm);
44 : static int DecodeTime(char *str, int fmask, int range,
45 : int *tmask, struct pg_tm *tm, fsec_t *fsec);
46 : static int DecodeTimeForInterval(char *str, int fmask, int range,
47 : int *tmask, struct pg_itm_in *itm_in);
48 : static const datetkn *datebsearch(const char *key, const datetkn *base, int nel);
49 : static int DecodeDate(char *str, int fmask, int *tmask, bool *is2digits,
50 : struct pg_tm *tm);
51 : static char *AppendSeconds(char *cp, int sec, fsec_t fsec,
52 : int precision, bool fillzeros);
53 : static bool int64_multiply_add(int64 val, int64 multiplier, int64 *sum);
54 : static bool AdjustFractMicroseconds(double frac, int64 scale,
55 : struct pg_itm_in *itm_in);
56 : static bool AdjustFractDays(double frac, int scale,
57 : struct pg_itm_in *itm_in);
58 : static bool AdjustFractYears(double frac, int scale,
59 : struct pg_itm_in *itm_in);
60 : static bool AdjustMicroseconds(int64 val, double fval, int64 scale,
61 : struct pg_itm_in *itm_in);
62 : static bool AdjustDays(int64 val, int scale,
63 : struct pg_itm_in *itm_in);
64 : static bool AdjustMonths(int64 val, struct pg_itm_in *itm_in);
65 : static bool AdjustYears(int64 val, int scale,
66 : struct pg_itm_in *itm_in);
67 : static int DetermineTimeZoneOffsetInternal(struct pg_tm *tm, pg_tz *tzp,
68 : pg_time_t *tp);
69 : static bool DetermineTimeZoneAbbrevOffsetInternal(pg_time_t t,
70 : const char *abbr, pg_tz *tzp,
71 : int *offset, int *isdst);
72 : static pg_tz *FetchDynamicTimeZone(TimeZoneAbbrevTable *tbl, const datetkn *tp,
73 : DateTimeErrorExtra *extra);
74 :
75 :
76 : const int day_tab[2][13] =
77 : {
78 : {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0},
79 : {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}
80 : };
81 :
82 : const char *const months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
83 : "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL};
84 :
85 : const char *const days[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
86 : "Thursday", "Friday", "Saturday", NULL};
87 :
88 :
89 : /*****************************************************************************
90 : * PRIVATE ROUTINES *
91 : *****************************************************************************/
92 :
93 : /*
94 : * datetktbl holds date/time keywords.
95 : *
96 : * Note that this table must be strictly alphabetically ordered to allow an
97 : * O(ln(N)) search algorithm to be used.
98 : *
99 : * The token field must be NUL-terminated; we truncate entries to TOKMAXLEN
100 : * characters to fit.
101 : *
102 : * The static table contains no TZ, DTZ, or DYNTZ entries; rather those
103 : * are loaded from configuration files and stored in zoneabbrevtbl, whose
104 : * abbrevs[] field has the same format as the static datetktbl.
105 : */
106 : static const datetkn datetktbl[] = {
107 : /* token, type, value */
108 : {"+infinity", RESERV, DTK_LATE}, /* same as "infinity" */
109 : {EARLY, RESERV, DTK_EARLY}, /* "-infinity" reserved for "early time" */
110 : {DA_D, ADBC, AD}, /* "ad" for years > 0 */
111 : {"allballs", RESERV, DTK_ZULU}, /* 00:00:00 */
112 : {"am", AMPM, AM},
113 : {"apr", MONTH, 4},
114 : {"april", MONTH, 4},
115 : {"at", IGNORE_DTF, 0}, /* "at" (throwaway) */
116 : {"aug", MONTH, 8},
117 : {"august", MONTH, 8},
118 : {DB_C, ADBC, BC}, /* "bc" for years <= 0 */
119 : {"d", UNITS, DTK_DAY}, /* "day of month" for ISO input */
120 : {"dec", MONTH, 12},
121 : {"december", MONTH, 12},
122 : {"dow", UNITS, DTK_DOW}, /* day of week */
123 : {"doy", UNITS, DTK_DOY}, /* day of year */
124 : {"dst", DTZMOD, SECS_PER_HOUR},
125 : {EPOCH, RESERV, DTK_EPOCH}, /* "epoch" reserved for system epoch time */
126 : {"feb", MONTH, 2},
127 : {"february", MONTH, 2},
128 : {"fri", DOW, 5},
129 : {"friday", DOW, 5},
130 : {"h", UNITS, DTK_HOUR}, /* "hour" */
131 : {LATE, RESERV, DTK_LATE}, /* "infinity" reserved for "late time" */
132 : {"isodow", UNITS, DTK_ISODOW}, /* ISO day of week, Sunday == 7 */
133 : {"isoyear", UNITS, DTK_ISOYEAR}, /* year in terms of the ISO week date */
134 : {"j", UNITS, DTK_JULIAN},
135 : {"jan", MONTH, 1},
136 : {"january", MONTH, 1},
137 : {"jd", UNITS, DTK_JULIAN},
138 : {"jul", MONTH, 7},
139 : {"julian", UNITS, DTK_JULIAN},
140 : {"july", MONTH, 7},
141 : {"jun", MONTH, 6},
142 : {"june", MONTH, 6},
143 : {"m", UNITS, DTK_MONTH}, /* "month" for ISO input */
144 : {"mar", MONTH, 3},
145 : {"march", MONTH, 3},
146 : {"may", MONTH, 5},
147 : {"mm", UNITS, DTK_MINUTE}, /* "minute" for ISO input */
148 : {"mon", DOW, 1},
149 : {"monday", DOW, 1},
150 : {"nov", MONTH, 11},
151 : {"november", MONTH, 11},
152 : {NOW, RESERV, DTK_NOW}, /* current transaction time */
153 : {"oct", MONTH, 10},
154 : {"october", MONTH, 10},
155 : {"on", IGNORE_DTF, 0}, /* "on" (throwaway) */
156 : {"pm", AMPM, PM},
157 : {"s", UNITS, DTK_SECOND}, /* "seconds" for ISO input */
158 : {"sat", DOW, 6},
159 : {"saturday", DOW, 6},
160 : {"sep", MONTH, 9},
161 : {"sept", MONTH, 9},
162 : {"september", MONTH, 9},
163 : {"sun", DOW, 0},
164 : {"sunday", DOW, 0},
165 : {"t", ISOTIME, DTK_TIME}, /* Filler for ISO time fields */
166 : {"thu", DOW, 4},
167 : {"thur", DOW, 4},
168 : {"thurs", DOW, 4},
169 : {"thursday", DOW, 4},
170 : {TODAY, RESERV, DTK_TODAY}, /* midnight */
171 : {TOMORROW, RESERV, DTK_TOMORROW}, /* tomorrow midnight */
172 : {"tue", DOW, 2},
173 : {"tues", DOW, 2},
174 : {"tuesday", DOW, 2},
175 : {"wed", DOW, 3},
176 : {"wednesday", DOW, 3},
177 : {"weds", DOW, 3},
178 : {"y", UNITS, DTK_YEAR}, /* "year" for ISO input */
179 : {YESTERDAY, RESERV, DTK_YESTERDAY} /* yesterday midnight */
180 : };
181 :
182 : static const int szdatetktbl = sizeof datetktbl / sizeof datetktbl[0];
183 :
184 : /*
185 : * deltatktbl: same format as datetktbl, but holds keywords used to represent
186 : * time units (eg, for intervals, and for EXTRACT).
187 : */
188 : static const datetkn deltatktbl[] = {
189 : /* token, type, value */
190 : {"@", IGNORE_DTF, 0}, /* postgres relative prefix */
191 : {DAGO, AGO, 0}, /* "ago" indicates negative time offset */
192 : {"c", UNITS, DTK_CENTURY}, /* "century" relative */
193 : {"cent", UNITS, DTK_CENTURY}, /* "century" relative */
194 : {"centuries", UNITS, DTK_CENTURY}, /* "centuries" relative */
195 : {DCENTURY, UNITS, DTK_CENTURY}, /* "century" relative */
196 : {"d", UNITS, DTK_DAY}, /* "day" relative */
197 : {DDAY, UNITS, DTK_DAY}, /* "day" relative */
198 : {"days", UNITS, DTK_DAY}, /* "days" relative */
199 : {"dec", UNITS, DTK_DECADE}, /* "decade" relative */
200 : {DDECADE, UNITS, DTK_DECADE}, /* "decade" relative */
201 : {"decades", UNITS, DTK_DECADE}, /* "decades" relative */
202 : {"decs", UNITS, DTK_DECADE}, /* "decades" relative */
203 : {"h", UNITS, DTK_HOUR}, /* "hour" relative */
204 : {DHOUR, UNITS, DTK_HOUR}, /* "hour" relative */
205 : {"hours", UNITS, DTK_HOUR}, /* "hours" relative */
206 : {"hr", UNITS, DTK_HOUR}, /* "hour" relative */
207 : {"hrs", UNITS, DTK_HOUR}, /* "hours" relative */
208 : {"m", UNITS, DTK_MINUTE}, /* "minute" relative */
209 : {"microsecon", UNITS, DTK_MICROSEC}, /* "microsecond" relative */
210 : {"mil", UNITS, DTK_MILLENNIUM}, /* "millennium" relative */
211 : {"millennia", UNITS, DTK_MILLENNIUM}, /* "millennia" relative */
212 : {DMILLENNIUM, UNITS, DTK_MILLENNIUM}, /* "millennium" relative */
213 : {"millisecon", UNITS, DTK_MILLISEC}, /* relative */
214 : {"mils", UNITS, DTK_MILLENNIUM}, /* "millennia" relative */
215 : {"min", UNITS, DTK_MINUTE}, /* "minute" relative */
216 : {"mins", UNITS, DTK_MINUTE}, /* "minutes" relative */
217 : {DMINUTE, UNITS, DTK_MINUTE}, /* "minute" relative */
218 : {"minutes", UNITS, DTK_MINUTE}, /* "minutes" relative */
219 : {"mon", UNITS, DTK_MONTH}, /* "months" relative */
220 : {"mons", UNITS, DTK_MONTH}, /* "months" relative */
221 : {DMONTH, UNITS, DTK_MONTH}, /* "month" relative */
222 : {"months", UNITS, DTK_MONTH},
223 : {"ms", UNITS, DTK_MILLISEC},
224 : {"msec", UNITS, DTK_MILLISEC},
225 : {DMILLISEC, UNITS, DTK_MILLISEC},
226 : {"mseconds", UNITS, DTK_MILLISEC},
227 : {"msecs", UNITS, DTK_MILLISEC},
228 : {"qtr", UNITS, DTK_QUARTER}, /* "quarter" relative */
229 : {DQUARTER, UNITS, DTK_QUARTER}, /* "quarter" relative */
230 : {"s", UNITS, DTK_SECOND},
231 : {"sec", UNITS, DTK_SECOND},
232 : {DSECOND, UNITS, DTK_SECOND},
233 : {"seconds", UNITS, DTK_SECOND},
234 : {"secs", UNITS, DTK_SECOND},
235 : {DTIMEZONE, UNITS, DTK_TZ}, /* "timezone" time offset */
236 : {"timezone_h", UNITS, DTK_TZ_HOUR}, /* timezone hour units */
237 : {"timezone_m", UNITS, DTK_TZ_MINUTE}, /* timezone minutes units */
238 : {"us", UNITS, DTK_MICROSEC}, /* "microsecond" relative */
239 : {"usec", UNITS, DTK_MICROSEC}, /* "microsecond" relative */
240 : {DMICROSEC, UNITS, DTK_MICROSEC}, /* "microsecond" relative */
241 : {"useconds", UNITS, DTK_MICROSEC}, /* "microseconds" relative */
242 : {"usecs", UNITS, DTK_MICROSEC}, /* "microseconds" relative */
243 : {"w", UNITS, DTK_WEEK}, /* "week" relative */
244 : {DWEEK, UNITS, DTK_WEEK}, /* "week" relative */
245 : {"weeks", UNITS, DTK_WEEK}, /* "weeks" relative */
246 : {"y", UNITS, DTK_YEAR}, /* "year" relative */
247 : {DYEAR, UNITS, DTK_YEAR}, /* "year" relative */
248 : {"years", UNITS, DTK_YEAR}, /* "years" relative */
249 : {"yr", UNITS, DTK_YEAR}, /* "year" relative */
250 : {"yrs", UNITS, DTK_YEAR} /* "years" relative */
251 : };
252 :
253 : static const int szdeltatktbl = sizeof deltatktbl / sizeof deltatktbl[0];
254 :
255 : static TimeZoneAbbrevTable *zoneabbrevtbl = NULL;
256 :
257 : /* Caches of recent lookup results in the above tables */
258 :
259 : static const datetkn *datecache[MAXDATEFIELDS] = {NULL};
260 :
261 : static const datetkn *deltacache[MAXDATEFIELDS] = {NULL};
262 :
263 : /* Cache for results of timezone abbreviation lookups */
264 :
265 : typedef struct TzAbbrevCache
266 : {
267 : char abbrev[TOKMAXLEN + 1]; /* always NUL-terminated */
268 : char ftype; /* TZ, DTZ, or DYNTZ */
269 : int offset; /* GMT offset, if fixed-offset */
270 : pg_tz *tz; /* relevant zone, if variable-offset */
271 : } TzAbbrevCache;
272 :
273 : static TzAbbrevCache tzabbrevcache[MAXDATEFIELDS];
274 :
275 :
276 : /*
277 : * Calendar time to Julian date conversions.
278 : * Julian date is commonly used in astronomical applications,
279 : * since it is numerically accurate and computationally simple.
280 : * The algorithms here will accurately convert between Julian day
281 : * and calendar date for all non-negative Julian days
282 : * (i.e. from Nov 24, -4713 on).
283 : *
284 : * Rewritten to eliminate overflow problems. This now allows the
285 : * routines to work correctly for all Julian day counts from
286 : * 0 to 2147483647 (Nov 24, -4713 to Jun 3, 5874898) assuming
287 : * a 32-bit integer. Longer types should also work to the limits
288 : * of their precision.
289 : *
290 : * Actually, date2j() will work sanely, in the sense of producing
291 : * valid negative Julian dates, significantly before Nov 24, -4713.
292 : * We rely on it to do so back to Nov 1, -4713; see IS_VALID_JULIAN()
293 : * and associated commentary in timestamp.h.
294 : */
295 :
296 : int
297 281812 : date2j(int year, int month, int day)
298 : {
299 : int julian;
300 : int century;
301 :
302 281812 : if (month > 2)
303 : {
304 181036 : month += 1;
305 181036 : year += 4800;
306 : }
307 : else
308 : {
309 100776 : month += 13;
310 100776 : year += 4799;
311 : }
312 :
313 281812 : century = year / 100;
314 281812 : julian = year * 365 - 32167;
315 281812 : julian += year / 4 - century + century / 4;
316 281812 : julian += 7834 * month / 256 + day;
317 :
318 281812 : return julian;
319 : } /* date2j() */
320 :
321 : void
322 199854 : j2date(int jd, int *year, int *month, int *day)
323 : {
324 : unsigned int julian;
325 : unsigned int quad;
326 : unsigned int extra;
327 : int y;
328 :
329 199854 : julian = jd;
330 199854 : julian += 32044;
331 199854 : quad = julian / 146097;
332 199854 : extra = (julian - quad * 146097) * 4 + 3;
333 199854 : julian += 60 + quad * 3 + extra / 146097;
334 199854 : quad = julian / 1461;
335 199854 : julian -= quad * 1461;
336 199854 : y = julian * 4 / 1461;
337 399708 : julian = ((y != 0) ? ((julian + 305) % 365) : ((julian + 306) % 366))
338 199854 : + 123;
339 199854 : y += quad * 4;
340 199854 : *year = y - 4800;
341 199854 : quad = julian * 2141 / 65536;
342 199854 : *day = julian - 7834 * quad / 256;
343 199854 : *month = (quad + 10) % MONTHS_PER_YEAR + 1;
344 199854 : } /* j2date() */
345 :
346 :
347 : /*
348 : * j2day - convert Julian date to day-of-week (0..6 == Sun..Sat)
349 : *
350 : * Note: various places use the locution j2day(date - 1) to produce a
351 : * result according to the convention 0..6 = Mon..Sun. This is a bit of
352 : * a crock, but will work as long as the computation here is just a modulo.
353 : */
354 : int
355 34079 : j2day(int date)
356 : {
357 34079 : date += 1;
358 34079 : date %= 7;
359 : /* Cope if division truncates towards zero, as it probably does */
360 34079 : if (date < 0)
361 0 : date += 7;
362 :
363 34079 : return date;
364 : } /* j2day() */
365 :
366 :
367 : /*
368 : * GetCurrentDateTime()
369 : *
370 : * Get the transaction start time ("now()") broken down as a struct pg_tm,
371 : * converted according to the session timezone setting.
372 : *
373 : * This is just a convenience wrapper for GetCurrentTimeUsec, to cover the
374 : * case where caller doesn't need either fractional seconds or tz offset.
375 : */
376 : void
377 1745 : GetCurrentDateTime(struct pg_tm *tm)
378 : {
379 : fsec_t fsec;
380 :
381 1745 : GetCurrentTimeUsec(tm, &fsec, NULL);
382 1745 : }
383 :
384 : /*
385 : * GetCurrentTimeUsec()
386 : *
387 : * Get the transaction start time ("now()") broken down as a struct pg_tm,
388 : * including fractional seconds and timezone offset. The time is converted
389 : * according to the session timezone setting.
390 : *
391 : * Callers may pass tzp = NULL if they don't need the offset, but this does
392 : * not affect the conversion behavior (unlike timestamp2tm()).
393 : *
394 : * Internally, we cache the result, since this could be called many times
395 : * in a transaction, within which now() doesn't change.
396 : */
397 : void
398 1861 : GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
399 : {
400 1861 : TimestampTz cur_ts = GetCurrentTransactionStartTimestamp();
401 :
402 : /*
403 : * The cache key must include both current time and current timezone. By
404 : * representing the timezone by just a pointer, we're assuming that
405 : * distinct timezone settings could never have the same pointer value.
406 : * This is true by virtue of the hashtable used inside pg_tzset();
407 : * however, it might need another look if we ever allow entries in that
408 : * hash to be recycled.
409 : */
410 : static TimestampTz cache_ts = 0;
411 : static pg_tz *cache_timezone = NULL;
412 : static struct pg_tm cache_tm;
413 : static fsec_t cache_fsec;
414 : static int cache_tz;
415 :
416 1861 : if (cur_ts != cache_ts || session_timezone != cache_timezone)
417 : {
418 : /*
419 : * Make sure cache is marked invalid in case of error after partial
420 : * update within timestamp2tm.
421 : */
422 657 : cache_timezone = NULL;
423 :
424 : /*
425 : * Perform the computation, storing results into cache. We do not
426 : * really expect any error here, since current time surely ought to be
427 : * within range, but check just for sanity's sake.
428 : */
429 657 : if (timestamp2tm(cur_ts, &cache_tz, &cache_tm, &cache_fsec,
430 : NULL, session_timezone) != 0)
431 0 : ereport(ERROR,
432 : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
433 : errmsg("timestamp out of range")));
434 :
435 : /* OK, so mark the cache valid. */
436 657 : cache_ts = cur_ts;
437 657 : cache_timezone = session_timezone;
438 : }
439 :
440 1861 : *tm = cache_tm;
441 1861 : *fsec = cache_fsec;
442 1861 : if (tzp != NULL)
443 108 : *tzp = cache_tz;
444 1861 : }
445 :
446 :
447 : /*
448 : * Append seconds and fractional seconds (if any) at *cp.
449 : *
450 : * precision is the max number of fraction digits, fillzeros says to
451 : * pad to two integral-seconds digits.
452 : *
453 : * Returns a pointer to the new end of string. No NUL terminator is put
454 : * there; callers are responsible for NUL terminating str themselves.
455 : *
456 : * Note that any sign is stripped from the input sec and fsec values.
457 : */
458 : static char *
459 76459 : AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
460 : {
461 : Assert(precision >= 0);
462 :
463 76459 : if (fillzeros)
464 74261 : cp = pg_ultostr_zeropad(cp, abs(sec), 2);
465 : else
466 2198 : cp = pg_ultostr(cp, abs(sec));
467 :
468 : /* fsec_t is just an int32 */
469 76459 : if (fsec != 0)
470 : {
471 12228 : int32 value = abs(fsec);
472 12228 : char *end = &cp[precision + 1];
473 12228 : bool gotnonzero = false;
474 :
475 12228 : *cp++ = '.';
476 :
477 : /*
478 : * Append the fractional seconds part. Note that we don't want any
479 : * trailing zeros here, so since we're building the number in reverse
480 : * we'll skip appending zeros until we've output a non-zero digit.
481 : */
482 85596 : while (precision--)
483 : {
484 73368 : int32 oldval = value;
485 : int32 remainder;
486 :
487 73368 : value /= 10;
488 73368 : remainder = oldval - value * 10;
489 :
490 : /* check if we got a non-zero */
491 73368 : if (remainder)
492 53340 : gotnonzero = true;
493 :
494 73368 : if (gotnonzero)
495 62064 : cp[precision] = '0' + remainder;
496 : else
497 11304 : end = &cp[precision];
498 : }
499 :
500 : /*
501 : * If we still have a non-zero value then precision must have not been
502 : * enough to print the number. We punt the problem to pg_ultostr(),
503 : * which will generate a correct answer in the minimum valid width.
504 : */
505 12228 : if (value)
506 0 : return pg_ultostr(cp, abs(fsec));
507 :
508 12228 : return end;
509 : }
510 : else
511 64231 : return cp;
512 : }
513 :
514 :
515 : /*
516 : * Variant of above that's specialized to timestamp case.
517 : *
518 : * Returns a pointer to the new end of string. No NUL terminator is put
519 : * there; callers are responsible for NUL terminating str themselves.
520 : */
521 : static char *
522 63495 : AppendTimestampSeconds(char *cp, struct pg_tm *tm, fsec_t fsec)
523 : {
524 63495 : return AppendSeconds(cp, tm->tm_sec, fsec, MAX_TIMESTAMP_PRECISION, true);
525 : }
526 :
527 :
528 : /*
529 : * Add val * multiplier to *sum.
530 : * Returns true if successful, false on overflow.
531 : */
532 : static bool
533 5194 : int64_multiply_add(int64 val, int64 multiplier, int64 *sum)
534 : {
535 : int64 product;
536 :
537 10348 : if (pg_mul_s64_overflow(val, multiplier, &product) ||
538 5154 : pg_add_s64_overflow(*sum, product, sum))
539 104 : return false;
540 5090 : return true;
541 : }
542 :
543 : /*
544 : * Multiply frac by scale (to produce microseconds) and add to itm_in->tm_usec.
545 : * Returns true if successful, false if itm_in overflows.
546 : */
547 : static bool
548 5705 : AdjustFractMicroseconds(double frac, int64 scale,
549 : struct pg_itm_in *itm_in)
550 : {
551 : int64 usec;
552 :
553 : /* Fast path for common case */
554 5705 : if (frac == 0)
555 5397 : return true;
556 :
557 : /*
558 : * We assume the input frac has abs value less than 1, so overflow of frac
559 : * or usec is not an issue for interesting values of scale.
560 : */
561 308 : frac *= scale;
562 308 : usec = (int64) frac;
563 :
564 : /* Round off any fractional microsecond */
565 308 : frac -= usec;
566 308 : if (frac > 0.5)
567 16 : usec++;
568 292 : else if (frac < -0.5)
569 20 : usec--;
570 :
571 308 : return !pg_add_s64_overflow(itm_in->tm_usec, usec, &itm_in->tm_usec);
572 : }
573 :
574 : /*
575 : * Multiply frac by scale (to produce days). Add the integral part of the
576 : * result to itm_in->tm_mday, the fractional part to itm_in->tm_usec.
577 : * Returns true if successful, false if itm_in overflows.
578 : */
579 : static bool
580 736 : AdjustFractDays(double frac, int scale,
581 : struct pg_itm_in *itm_in)
582 : {
583 : int extra_days;
584 :
585 : /* Fast path for common case */
586 736 : if (frac == 0)
587 612 : return true;
588 :
589 : /*
590 : * We assume the input frac has abs value less than 1, so overflow of frac
591 : * or extra_days is not an issue.
592 : */
593 124 : frac *= scale;
594 124 : extra_days = (int) frac;
595 :
596 : /* ... but this could overflow, if tm_mday is already nonzero */
597 124 : if (pg_add_s32_overflow(itm_in->tm_mday, extra_days, &itm_in->tm_mday))
598 32 : return false;
599 :
600 : /* Handle any fractional day */
601 92 : frac -= extra_days;
602 92 : return AdjustFractMicroseconds(frac, USECS_PER_DAY, itm_in);
603 : }
604 :
605 : /*
606 : * Multiply frac by scale (to produce years), then further scale up to months.
607 : * Add the integral part of the result to itm_in->tm_mon, discarding any
608 : * fractional part.
609 : * Returns true if successful, false if itm_in overflows.
610 : */
611 : static bool
612 36484 : AdjustFractYears(double frac, int scale,
613 : struct pg_itm_in *itm_in)
614 : {
615 : /*
616 : * As above, we assume abs(frac) < 1, so this can't overflow for any
617 : * interesting value of scale.
618 : */
619 36484 : int extra_months = (int) rint(frac * scale * MONTHS_PER_YEAR);
620 :
621 36484 : return !pg_add_s32_overflow(itm_in->tm_mon, extra_months, &itm_in->tm_mon);
622 : }
623 :
624 : /*
625 : * Add (val + fval) * scale to itm_in->tm_usec.
626 : * Returns true if successful, false if itm_in overflows.
627 : */
628 : static bool
629 1843 : AdjustMicroseconds(int64 val, double fval, int64 scale,
630 : struct pg_itm_in *itm_in)
631 : {
632 : /* Handle the integer part */
633 1843 : if (!int64_multiply_add(val, scale, &itm_in->tm_usec))
634 100 : return false;
635 : /* Handle the float part */
636 1743 : return AdjustFractMicroseconds(fval, scale, itm_in);
637 : }
638 :
639 : /*
640 : * Multiply val by scale (to produce days) and add to itm_in->tm_mday.
641 : * Returns true if successful, false if itm_in overflows.
642 : */
643 : static bool
644 4042 : AdjustDays(int64 val, int scale, struct pg_itm_in *itm_in)
645 : {
646 : int days;
647 :
648 4042 : if (val < INT_MIN || val > INT_MAX)
649 24 : return false;
650 8028 : return !pg_mul_s32_overflow((int32) val, scale, &days) &&
651 4010 : !pg_add_s32_overflow(itm_in->tm_mday, days, &itm_in->tm_mday);
652 : }
653 :
654 : /*
655 : * Add val to itm_in->tm_mon (no need for scale here, as val is always
656 : * in months already).
657 : * Returns true if successful, false if itm_in overflows.
658 : */
659 : static bool
660 716 : AdjustMonths(int64 val, struct pg_itm_in *itm_in)
661 : {
662 716 : if (val < INT_MIN || val > INT_MAX)
663 8 : return false;
664 708 : return !pg_add_s32_overflow(itm_in->tm_mon, (int32) val, &itm_in->tm_mon);
665 : }
666 :
667 : /*
668 : * Multiply val by scale (to produce years) and add to itm_in->tm_year.
669 : * Returns true if successful, false if itm_in overflows.
670 : */
671 : static bool
672 36576 : AdjustYears(int64 val, int scale,
673 : struct pg_itm_in *itm_in)
674 : {
675 : int years;
676 :
677 36576 : if (val < INT_MIN || val > INT_MAX)
678 16 : return false;
679 73096 : return !pg_mul_s32_overflow((int32) val, scale, &years) &&
680 36536 : !pg_add_s32_overflow(itm_in->tm_year, years, &itm_in->tm_year);
681 : }
682 :
683 :
684 : /*
685 : * Parse the fractional part of a number (decimal point and optional digits,
686 : * followed by end of string). Returns the fractional value into *frac.
687 : *
688 : * Returns 0 if successful, DTERR code if bogus input detected.
689 : */
690 : static int
691 12002 : ParseFraction(char *cp, double *frac)
692 : {
693 : /* Caller should always pass the start of the fraction part */
694 : Assert(*cp == '.');
695 :
696 : /*
697 : * We want to allow just "." with no digits, but some versions of strtod
698 : * will report EINVAL for that, so special-case it.
699 : */
700 12002 : if (cp[1] == '\0')
701 : {
702 0 : *frac = 0;
703 : }
704 : else
705 : {
706 : /*
707 : * On the other hand, let's reject anything that's not digits after
708 : * the ".". strtod is happy with input like ".123e9", but that'd
709 : * break callers' expectation that the result is in 0..1. (It's quite
710 : * difficult to get here with such input, but not impossible.)
711 : */
712 12002 : if (strspn(cp + 1, "0123456789") != strlen(cp + 1))
713 8 : return DTERR_BAD_FORMAT;
714 :
715 11994 : errno = 0;
716 11994 : *frac = strtod(cp, &cp);
717 : /* check for parse failure (probably redundant given prior check) */
718 11994 : if (*cp != '\0' || errno != 0)
719 0 : return DTERR_BAD_FORMAT;
720 : }
721 11994 : return 0;
722 : }
723 :
724 : /*
725 : * Fetch a fractional-second value with suitable error checking.
726 : * Same as ParseFraction except we convert the result to integer microseconds.
727 : */
728 : static int
729 11670 : ParseFractionalSecond(char *cp, fsec_t *fsec)
730 : {
731 : double frac;
732 : int dterr;
733 :
734 11670 : dterr = ParseFraction(cp, &frac);
735 11670 : if (dterr)
736 8 : return dterr;
737 11662 : *fsec = rint(frac * 1000000);
738 11662 : return 0;
739 : }
740 :
741 :
742 : /* ParseDateTime()
743 : * Break string into tokens based on a date/time context.
744 : * Returns 0 if successful, DTERR code if bogus input detected.
745 : *
746 : * timestr - the input string
747 : * workbuf - workspace for field string storage. This must be
748 : * larger than the largest legal input for this datetime type --
749 : * some additional space will be needed to NUL terminate fields.
750 : * buflen - the size of workbuf
751 : * field[] - pointers to field strings are returned in this array
752 : * ftype[] - field type indicators are returned in this array
753 : * maxfields - dimensions of the above two arrays
754 : * *numfields - set to the actual number of fields detected
755 : *
756 : * The fields extracted from the input are stored as separate,
757 : * null-terminated strings in the workspace at workbuf. Any text is
758 : * converted to lower case.
759 : *
760 : * Several field types are assigned:
761 : * DTK_NUMBER - digits and (possibly) a decimal point
762 : * DTK_DATE - digits and two delimiters, or digits and text
763 : * DTK_TIME - digits, colon delimiters, and possibly a decimal point
764 : * DTK_STRING - text (no digits or punctuation)
765 : * DTK_SPECIAL - leading "+" or "-" followed by text
766 : * DTK_TZ - leading "+" or "-" followed by digits (also eats ':', '.', '-')
767 : *
768 : * Note that some field types can hold unexpected items:
769 : * DTK_NUMBER can hold date fields (yy.ddd)
770 : * DTK_STRING can hold months (January) and time zones (PST)
771 : * DTK_DATE can hold time zone names (America/New_York, GMT-8)
772 : */
773 : int
774 87414 : ParseDateTime(const char *timestr, char *workbuf, size_t buflen,
775 : char **field, int *ftype, int maxfields, int *numfields)
776 : {
777 87414 : int nf = 0;
778 87414 : const char *cp = timestr;
779 87414 : char *bufp = workbuf;
780 87414 : const char *bufend = workbuf + buflen;
781 :
782 : /*
783 : * Set the character pointed-to by "bufptr" to "newchar", and increment
784 : * "bufptr". "end" gives the end of the buffer -- we return an error if
785 : * there is no space left to append a character to the buffer. Note that
786 : * "bufptr" is evaluated twice.
787 : */
788 : #define APPEND_CHAR(bufptr, end, newchar) \
789 : do \
790 : { \
791 : if (((bufptr) + 1) >= (end)) \
792 : return DTERR_BAD_FORMAT; \
793 : *(bufptr)++ = newchar; \
794 : } while (0)
795 :
796 : /* outer loop through fields */
797 355739 : while (*cp != '\0')
798 : {
799 : /* Ignore spaces between fields */
800 268325 : if (isspace((unsigned char) *cp))
801 : {
802 80738 : cp++;
803 80738 : continue;
804 : }
805 :
806 : /* Record start of current field */
807 187587 : if (nf >= maxfields)
808 0 : return DTERR_BAD_FORMAT;
809 187587 : field[nf] = bufp;
810 :
811 : /* leading digit? then date or time */
812 187587 : if (isdigit((unsigned char) *cp))
813 : {
814 115741 : APPEND_CHAR(bufp, bufend, *cp++);
815 382606 : while (isdigit((unsigned char) *cp))
816 266865 : APPEND_CHAR(bufp, bufend, *cp++);
817 :
818 : /* time field? */
819 115741 : if (*cp == ':')
820 : {
821 31338 : ftype[nf] = DTK_TIME;
822 31338 : APPEND_CHAR(bufp, bufend, *cp++);
823 31338 : while (isdigit((unsigned char) *cp) ||
824 264695 : (*cp == ':') || (*cp == '.'))
825 233357 : APPEND_CHAR(bufp, bufend, *cp++);
826 : }
827 : /* date field? allow embedded text month */
828 84403 : else if (*cp == '-' || *cp == '/' || *cp == '.')
829 39748 : {
830 : /* save delimiting character to use later */
831 39748 : char delim = *cp;
832 :
833 39748 : APPEND_CHAR(bufp, bufend, *cp++);
834 : /* second field is all digits? then no embedded text month */
835 39748 : if (isdigit((unsigned char) *cp))
836 : {
837 39684 : ftype[nf] = ((delim == '.') ? DTK_NUMBER : DTK_DATE);
838 119076 : while (isdigit((unsigned char) *cp))
839 79392 : APPEND_CHAR(bufp, bufend, *cp++);
840 :
841 : /*
842 : * insist that the delimiters match to get a three-field
843 : * date.
844 : */
845 39684 : if (*cp == delim)
846 : {
847 39280 : ftype[nf] = DTK_DATE;
848 39280 : APPEND_CHAR(bufp, bufend, *cp++);
849 119796 : while (isdigit((unsigned char) *cp) || *cp == delim)
850 80516 : APPEND_CHAR(bufp, bufend, *cp++);
851 : }
852 : }
853 : else
854 : {
855 64 : ftype[nf] = DTK_DATE;
856 504 : while (isalnum((unsigned char) *cp) || *cp == delim)
857 440 : APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
858 : }
859 : }
860 :
861 : /*
862 : * otherwise, number only and will determine year, month, day, or
863 : * concatenated fields later...
864 : */
865 : else
866 44655 : ftype[nf] = DTK_NUMBER;
867 : }
868 : /* Leading decimal point? Then fractional seconds... */
869 71846 : else if (*cp == '.')
870 : {
871 0 : APPEND_CHAR(bufp, bufend, *cp++);
872 0 : while (isdigit((unsigned char) *cp))
873 0 : APPEND_CHAR(bufp, bufend, *cp++);
874 :
875 0 : ftype[nf] = DTK_NUMBER;
876 : }
877 :
878 : /*
879 : * text? then date string, month, day of week, special, or timezone
880 : */
881 71846 : else if (isalpha((unsigned char) *cp))
882 : {
883 : bool is_date;
884 :
885 50007 : ftype[nf] = DTK_STRING;
886 50007 : APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
887 236914 : while (isalpha((unsigned char) *cp))
888 186907 : APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
889 :
890 : /*
891 : * Dates can have embedded '-', '/', or '.' separators. It could
892 : * also be a timezone name containing embedded '/', '+', '-', '_',
893 : * or ':' (but '_' or ':' can't be the first punctuation). If the
894 : * next character is a digit or '+', we need to check whether what
895 : * we have so far is a recognized non-timezone keyword --- if so,
896 : * don't believe that this is the start of a timezone.
897 : */
898 50007 : is_date = false;
899 50007 : if (*cp == '-' || *cp == '/' || *cp == '.')
900 836 : is_date = true;
901 49171 : else if (*cp == '+' || isdigit((unsigned char) *cp))
902 : {
903 1089 : *bufp = '\0'; /* null-terminate current field value */
904 : /* we need search only the core token table, not TZ names */
905 1089 : if (datebsearch(field[nf], datetktbl, szdatetktbl) == NULL)
906 821 : is_date = true;
907 : }
908 50007 : if (is_date)
909 : {
910 1657 : ftype[nf] = DTK_DATE;
911 : do
912 : {
913 8744 : APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
914 8744 : } while (*cp == '+' || *cp == '-' ||
915 8548 : *cp == '/' || *cp == '_' ||
916 17188 : *cp == '.' || *cp == ':' ||
917 8164 : isalnum((unsigned char) *cp));
918 : }
919 : }
920 : /* sign? then special or numeric timezone */
921 21839 : else if (*cp == '+' || *cp == '-')
922 : {
923 21463 : APPEND_CHAR(bufp, bufend, *cp++);
924 : /* soak up leading whitespace */
925 21479 : while (isspace((unsigned char) *cp))
926 16 : cp++;
927 : /* numeric timezone? */
928 : /* note that "DTK_TZ" could also be a signed float or yyyy-mm */
929 42926 : if (isdigit((unsigned char) *cp))
930 : {
931 20858 : ftype[nf] = DTK_TZ;
932 20858 : APPEND_CHAR(bufp, bufend, *cp++);
933 20858 : while (isdigit((unsigned char) *cp) ||
934 49999 : *cp == ':' || *cp == '.' || *cp == '-')
935 29141 : APPEND_CHAR(bufp, bufend, *cp++);
936 : }
937 : /* special? */
938 605 : else if (isalpha((unsigned char) *cp))
939 : {
940 605 : ftype[nf] = DTK_SPECIAL;
941 605 : APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
942 4840 : while (isalpha((unsigned char) *cp))
943 4235 : APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
944 : }
945 : /* otherwise something wrong... */
946 : else
947 0 : return DTERR_BAD_FORMAT;
948 : }
949 : /* ignore other punctuation but use as delimiter */
950 376 : else if (ispunct((unsigned char) *cp))
951 : {
952 376 : cp++;
953 376 : continue;
954 : }
955 : /* otherwise, something is not right... */
956 : else
957 0 : return DTERR_BAD_FORMAT;
958 :
959 : /* force in a delimiter after each field */
960 187211 : *bufp++ = '\0';
961 187211 : nf++;
962 : }
963 :
964 87414 : *numfields = nf;
965 :
966 87414 : return 0;
967 : }
968 :
969 :
970 : /* DecodeDateTime()
971 : * Interpret previously parsed fields for general date and time.
972 : * Return 0 if full date, 1 if only time, and negative DTERR code if problems.
973 : * (Currently, all callers treat 1 as an error return too.)
974 : *
975 : * Inputs are field[] and ftype[] arrays, of length nf.
976 : * Other arguments are outputs.
977 : *
978 : * External format(s):
979 : * "<weekday> <month>-<day>-<year> <hour>:<minute>:<second>"
980 : * "Fri Feb-7-1997 15:23:27"
981 : * "Feb-7-1997 15:23:27"
982 : * "2-7-1997 15:23:27"
983 : * "1997-2-7 15:23:27"
984 : * "1997.038 15:23:27" (day of year 1-366)
985 : * Also supports input in compact time:
986 : * "970207 152327"
987 : * "97038 152327"
988 : * "20011225T040506.789-07"
989 : *
990 : * Use the system-provided functions to get the current time zone
991 : * if not specified in the input string.
992 : *
993 : * If the date is outside the range of pg_time_t (in practice that could only
994 : * happen if pg_time_t is just 32 bits), then assume UTC time zone - thomas
995 : * 1997-05-27
996 : */
997 : int
998 42301 : DecodeDateTime(char **field, int *ftype, int nf,
999 : int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp,
1000 : DateTimeErrorExtra *extra)
1001 : {
1002 42301 : int fmask = 0,
1003 : tmask,
1004 : type;
1005 42301 : int ptype = 0; /* "prefix type" for ISO and Julian formats */
1006 : int i;
1007 : int val;
1008 : int dterr;
1009 42301 : int mer = HR24;
1010 42301 : bool haveTextMonth = false;
1011 42301 : bool isjulian = false;
1012 42301 : bool is2digits = false;
1013 42301 : bool bc = false;
1014 42301 : pg_tz *namedTz = NULL;
1015 42301 : pg_tz *abbrevTz = NULL;
1016 : pg_tz *valtz;
1017 42301 : char *abbrev = NULL;
1018 : struct pg_tm cur_tm;
1019 :
1020 : /*
1021 : * We'll insist on at least all of the date fields, but initialize the
1022 : * remaining fields in case they are not set later...
1023 : */
1024 42301 : *dtype = DTK_DATE;
1025 42301 : tm->tm_hour = 0;
1026 42301 : tm->tm_min = 0;
1027 42301 : tm->tm_sec = 0;
1028 42301 : *fsec = 0;
1029 : /* don't know daylight savings time status apriori */
1030 42301 : tm->tm_isdst = -1;
1031 42301 : if (tzp != NULL)
1032 42301 : *tzp = 0;
1033 :
1034 138276 : for (i = 0; i < nf; i++)
1035 : {
1036 96195 : switch (ftype[i])
1037 : {
1038 40012 : case DTK_DATE:
1039 :
1040 : /*
1041 : * Integral julian day with attached time zone? All other
1042 : * forms with JD will be separated into distinct fields, so we
1043 : * handle just this case here.
1044 : */
1045 40012 : if (ptype == DTK_JULIAN)
1046 : {
1047 : char *cp;
1048 : int jday;
1049 :
1050 4 : if (tzp == NULL)
1051 0 : return DTERR_BAD_FORMAT;
1052 :
1053 4 : errno = 0;
1054 4 : jday = strtoint(field[i], &cp, 10);
1055 4 : if (errno == ERANGE || jday < 0)
1056 0 : return DTERR_FIELD_OVERFLOW;
1057 :
1058 4 : j2date(jday, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1059 4 : isjulian = true;
1060 :
1061 : /* Get the time zone from the end of the string */
1062 4 : dterr = DecodeTimezone(cp, tzp);
1063 4 : if (dterr)
1064 0 : return dterr;
1065 :
1066 4 : tmask = DTK_DATE_M | DTK_TIME_M | DTK_M(TZ);
1067 4 : ptype = 0;
1068 4 : break;
1069 : }
1070 :
1071 : /*
1072 : * Already have a date? Then this might be a time zone name
1073 : * with embedded punctuation (e.g. "America/New_York") or a
1074 : * run-together time with trailing time zone (e.g. hhmmss-zz).
1075 : * - thomas 2001-12-25
1076 : *
1077 : * We consider it a time zone if we already have month & day.
1078 : * This is to allow the form "mmm dd hhmmss tz year", which
1079 : * we've historically accepted.
1080 : */
1081 40008 : else if (ptype != 0 ||
1082 39992 : ((fmask & (DTK_M(MONTH) | DTK_M(DAY))) ==
1083 : (DTK_M(MONTH) | DTK_M(DAY))))
1084 : {
1085 : /* No time zone accepted? Then quit... */
1086 772 : if (tzp == NULL)
1087 0 : return DTERR_BAD_FORMAT;
1088 :
1089 1512 : if (isdigit((unsigned char) *field[i]) || ptype != 0)
1090 12 : {
1091 : char *cp;
1092 :
1093 : /*
1094 : * Allow a preceding "t" field, but no other units.
1095 : */
1096 20 : if (ptype != 0)
1097 : {
1098 : /* Sanity check; should not fail this test */
1099 16 : if (ptype != DTK_TIME)
1100 0 : return DTERR_BAD_FORMAT;
1101 16 : ptype = 0;
1102 : }
1103 :
1104 : /*
1105 : * Starts with a digit but we already have a time
1106 : * field? Then we are in trouble with a date and time
1107 : * already...
1108 : */
1109 20 : if ((fmask & DTK_TIME_M) == DTK_TIME_M)
1110 0 : return DTERR_BAD_FORMAT;
1111 :
1112 20 : if ((cp = strchr(field[i], '-')) == NULL)
1113 0 : return DTERR_BAD_FORMAT;
1114 :
1115 : /* Get the time zone from the end of the string */
1116 20 : dterr = DecodeTimezone(cp, tzp);
1117 20 : if (dterr)
1118 0 : return dterr;
1119 20 : *cp = '\0';
1120 :
1121 : /*
1122 : * Then read the rest of the field as a concatenated
1123 : * time
1124 : */
1125 20 : dterr = DecodeNumberField(strlen(field[i]), field[i],
1126 : fmask,
1127 : &tmask, tm,
1128 : fsec, &is2digits);
1129 20 : if (dterr < 0)
1130 8 : return dterr;
1131 :
1132 : /*
1133 : * modify tmask after returning from
1134 : * DecodeNumberField()
1135 : */
1136 12 : tmask |= DTK_M(TZ);
1137 : }
1138 : else
1139 : {
1140 752 : namedTz = pg_tzset(field[i]);
1141 752 : if (!namedTz)
1142 : {
1143 24 : extra->dtee_timezone = field[i];
1144 24 : return DTERR_BAD_TIMEZONE;
1145 : }
1146 : /* we'll apply the zone setting below */
1147 728 : tmask = DTK_M(TZ);
1148 : }
1149 : }
1150 : else
1151 : {
1152 39236 : dterr = DecodeDate(field[i], fmask,
1153 : &tmask, &is2digits, tm);
1154 39236 : if (dterr)
1155 24 : return dterr;
1156 : }
1157 39952 : break;
1158 :
1159 28272 : case DTK_TIME:
1160 :
1161 : /*
1162 : * This might be an ISO time following a "t" field.
1163 : */
1164 28272 : if (ptype != 0)
1165 : {
1166 : /* Sanity check; should not fail this test */
1167 8 : if (ptype != DTK_TIME)
1168 0 : return DTERR_BAD_FORMAT;
1169 8 : ptype = 0;
1170 : }
1171 28272 : dterr = DecodeTime(field[i], fmask, INTERVAL_FULL_RANGE,
1172 : &tmask, tm, fsec);
1173 28272 : if (dterr)
1174 0 : return dterr;
1175 :
1176 : /* check for time overflow */
1177 28272 : if (time_overflows(tm->tm_hour, tm->tm_min, tm->tm_sec,
1178 : *fsec))
1179 0 : return DTERR_FIELD_OVERFLOW;
1180 28272 : break;
1181 :
1182 18598 : case DTK_TZ:
1183 : {
1184 : int tz;
1185 :
1186 18598 : if (tzp == NULL)
1187 8 : return DTERR_BAD_FORMAT;
1188 :
1189 18598 : dterr = DecodeTimezone(field[i], &tz);
1190 18598 : if (dterr)
1191 8 : return dterr;
1192 18590 : *tzp = tz;
1193 18590 : tmask = DTK_M(TZ);
1194 : }
1195 18590 : break;
1196 :
1197 3725 : case DTK_NUMBER:
1198 :
1199 : /*
1200 : * Deal with cases where previous field labeled this one
1201 : */
1202 3725 : if (ptype != 0)
1203 : {
1204 : char *cp;
1205 : int value;
1206 :
1207 96 : errno = 0;
1208 96 : value = strtoint(field[i], &cp, 10);
1209 96 : if (errno == ERANGE)
1210 8 : return DTERR_FIELD_OVERFLOW;
1211 96 : if (*cp != '.' && *cp != '\0')
1212 0 : return DTERR_BAD_FORMAT;
1213 :
1214 96 : switch (ptype)
1215 : {
1216 64 : case DTK_JULIAN:
1217 : /* previous field was a label for "julian date" */
1218 64 : if (value < 0)
1219 0 : return DTERR_FIELD_OVERFLOW;
1220 64 : tmask = DTK_DATE_M;
1221 64 : j2date(value, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1222 64 : isjulian = true;
1223 :
1224 : /* fractional Julian Day? */
1225 64 : if (*cp == '.')
1226 : {
1227 : double time;
1228 :
1229 8 : dterr = ParseFraction(cp, &time);
1230 8 : if (dterr)
1231 0 : return dterr;
1232 8 : time *= USECS_PER_DAY;
1233 8 : dt2time(time,
1234 : &tm->tm_hour, &tm->tm_min,
1235 : &tm->tm_sec, fsec);
1236 8 : tmask |= DTK_TIME_M;
1237 : }
1238 64 : break;
1239 :
1240 24 : case DTK_TIME:
1241 : /* previous field was "t" for ISO time */
1242 24 : dterr = DecodeNumberField(strlen(field[i]), field[i],
1243 : (fmask | DTK_DATE_M),
1244 : &tmask, tm,
1245 : fsec, &is2digits);
1246 24 : if (dterr < 0)
1247 0 : return dterr;
1248 24 : if (tmask != DTK_TIME_M)
1249 0 : return DTERR_BAD_FORMAT;
1250 24 : break;
1251 :
1252 8 : default:
1253 8 : return DTERR_BAD_FORMAT;
1254 : break;
1255 : }
1256 :
1257 88 : ptype = 0;
1258 88 : *dtype = DTK_DATE;
1259 : }
1260 : else
1261 : {
1262 : char *cp;
1263 : int flen;
1264 :
1265 3629 : flen = strlen(field[i]);
1266 3629 : cp = strchr(field[i], '.');
1267 :
1268 : /* Embedded decimal and no date yet? */
1269 3629 : if (cp != NULL && !(fmask & DTK_DATE_M))
1270 : {
1271 20 : dterr = DecodeDate(field[i], fmask,
1272 : &tmask, &is2digits, tm);
1273 20 : if (dterr)
1274 0 : return dterr;
1275 : }
1276 : /* embedded decimal and several digits before? */
1277 3609 : else if (cp != NULL && flen - strlen(cp) > 2)
1278 : {
1279 : /*
1280 : * Interpret as a concatenated date or time Set the
1281 : * type field to allow decoding other fields later.
1282 : * Example: 20011223 or 040506
1283 : */
1284 8 : dterr = DecodeNumberField(flen, field[i], fmask,
1285 : &tmask, tm,
1286 : fsec, &is2digits);
1287 8 : if (dterr < 0)
1288 0 : return dterr;
1289 : }
1290 :
1291 : /*
1292 : * Is this a YMD or HMS specification, or a year number?
1293 : * YMD and HMS are required to be six digits or more, so
1294 : * if it is 5 digits, it is a year. If it is six or more
1295 : * digits, we assume it is YMD or HMS unless no date and
1296 : * no time values have been specified. This forces 6+
1297 : * digit years to be at the end of the string, or to use
1298 : * the ISO date specification.
1299 : */
1300 3601 : else if (flen >= 6 && (!(fmask & DTK_DATE_M) ||
1301 64 : !(fmask & DTK_TIME_M)))
1302 : {
1303 224 : dterr = DecodeNumberField(flen, field[i], fmask,
1304 : &tmask, tm,
1305 : fsec, &is2digits);
1306 224 : if (dterr < 0)
1307 0 : return dterr;
1308 : }
1309 : /* otherwise it is a single date/time field... */
1310 : else
1311 : {
1312 3377 : dterr = DecodeNumber(flen, field[i],
1313 : haveTextMonth, fmask,
1314 : &tmask, tm,
1315 : fsec, &is2digits);
1316 3377 : if (dterr)
1317 8 : return dterr;
1318 : }
1319 : }
1320 3709 : break;
1321 :
1322 5588 : case DTK_STRING:
1323 : case DTK_SPECIAL:
1324 : /* timezone abbrevs take precedence over built-in tokens */
1325 5588 : dterr = DecodeTimezoneAbbrev(i, field[i],
1326 : &type, &val, &valtz, extra);
1327 5588 : if (dterr)
1328 0 : return dterr;
1329 5588 : if (type == UNKNOWN_FIELD)
1330 4204 : type = DecodeSpecial(i, field[i], &val);
1331 5588 : if (type == IGNORE_DTF)
1332 0 : continue;
1333 :
1334 5588 : tmask = DTK_M(type);
1335 5588 : switch (type)
1336 : {
1337 1195 : case RESERV:
1338 1195 : switch (val)
1339 : {
1340 76 : case DTK_NOW:
1341 76 : tmask = (DTK_DATE_M | DTK_TIME_M | DTK_M(TZ));
1342 76 : *dtype = DTK_DATE;
1343 76 : GetCurrentTimeUsec(tm, fsec, tzp);
1344 76 : break;
1345 :
1346 68 : case DTK_YESTERDAY:
1347 68 : tmask = DTK_DATE_M;
1348 68 : *dtype = DTK_DATE;
1349 68 : GetCurrentDateTime(&cur_tm);
1350 68 : j2date(date2j(cur_tm.tm_year, cur_tm.tm_mon, cur_tm.tm_mday) - 1,
1351 : &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1352 68 : break;
1353 :
1354 96 : case DTK_TODAY:
1355 96 : tmask = DTK_DATE_M;
1356 96 : *dtype = DTK_DATE;
1357 96 : GetCurrentDateTime(&cur_tm);
1358 96 : tm->tm_year = cur_tm.tm_year;
1359 96 : tm->tm_mon = cur_tm.tm_mon;
1360 96 : tm->tm_mday = cur_tm.tm_mday;
1361 96 : break;
1362 :
1363 100 : case DTK_TOMORROW:
1364 100 : tmask = DTK_DATE_M;
1365 100 : *dtype = DTK_DATE;
1366 100 : GetCurrentDateTime(&cur_tm);
1367 100 : j2date(date2j(cur_tm.tm_year, cur_tm.tm_mon, cur_tm.tm_mday) + 1,
1368 : &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1369 100 : break;
1370 :
1371 4 : case DTK_ZULU:
1372 4 : tmask = (DTK_TIME_M | DTK_M(TZ));
1373 4 : *dtype = DTK_DATE;
1374 4 : tm->tm_hour = 0;
1375 4 : tm->tm_min = 0;
1376 4 : tm->tm_sec = 0;
1377 4 : if (tzp != NULL)
1378 4 : *tzp = 0;
1379 4 : break;
1380 :
1381 851 : case DTK_EPOCH:
1382 : case DTK_LATE:
1383 : case DTK_EARLY:
1384 851 : tmask = (DTK_DATE_M | DTK_TIME_M | DTK_M(TZ));
1385 851 : *dtype = val;
1386 : /* caller ignores tm for these dtype codes */
1387 851 : break;
1388 :
1389 0 : default:
1390 0 : elog(ERROR, "unrecognized RESERV datetime token: %d",
1391 : val);
1392 : }
1393 :
1394 1195 : break;
1395 :
1396 1529 : case MONTH:
1397 :
1398 : /*
1399 : * already have a (numeric) month? then see if we can
1400 : * substitute...
1401 : */
1402 1529 : if ((fmask & DTK_M(MONTH)) && !haveTextMonth &&
1403 55 : !(fmask & DTK_M(DAY)) && tm->tm_mon >= 1 &&
1404 47 : tm->tm_mon <= 31)
1405 : {
1406 43 : tm->tm_mday = tm->tm_mon;
1407 43 : tmask = DTK_M(DAY);
1408 : }
1409 1529 : haveTextMonth = true;
1410 1529 : tm->tm_mon = val;
1411 1529 : break;
1412 :
1413 4 : case DTZMOD:
1414 :
1415 : /*
1416 : * daylight savings time modifier (solves "MET DST"
1417 : * syntax)
1418 : */
1419 4 : tmask |= DTK_M(DTZ);
1420 4 : tm->tm_isdst = 1;
1421 4 : if (tzp == NULL)
1422 0 : return DTERR_BAD_FORMAT;
1423 4 : *tzp -= val;
1424 4 : break;
1425 :
1426 936 : case DTZ:
1427 :
1428 : /*
1429 : * set mask for TZ here _or_ check for DTZ later when
1430 : * getting default timezone
1431 : */
1432 936 : tmask |= DTK_M(TZ);
1433 936 : tm->tm_isdst = 1;
1434 936 : if (tzp == NULL)
1435 0 : return DTERR_BAD_FORMAT;
1436 936 : *tzp = -val;
1437 936 : break;
1438 :
1439 392 : case TZ:
1440 392 : tm->tm_isdst = 0;
1441 392 : if (tzp == NULL)
1442 0 : return DTERR_BAD_FORMAT;
1443 392 : *tzp = -val;
1444 392 : break;
1445 :
1446 56 : case DYNTZ:
1447 56 : tmask |= DTK_M(TZ);
1448 56 : if (tzp == NULL)
1449 0 : return DTERR_BAD_FORMAT;
1450 : /* we'll determine the actual offset later */
1451 56 : abbrevTz = valtz;
1452 56 : abbrev = field[i];
1453 56 : break;
1454 :
1455 24 : case AMPM:
1456 24 : mer = val;
1457 24 : break;
1458 :
1459 222 : case ADBC:
1460 222 : bc = (val == BC);
1461 222 : break;
1462 :
1463 1054 : case DOW:
1464 1054 : tm->tm_wday = val;
1465 1054 : break;
1466 :
1467 92 : case UNITS:
1468 92 : tmask = 0;
1469 : /* reject consecutive unhandled units */
1470 92 : if (ptype != 0)
1471 8 : return DTERR_BAD_FORMAT;
1472 84 : ptype = val;
1473 84 : break;
1474 :
1475 48 : case ISOTIME:
1476 :
1477 : /*
1478 : * This is a filler field "t" indicating that the next
1479 : * field is time. Try to verify that this is sensible.
1480 : */
1481 48 : tmask = 0;
1482 :
1483 : /* No preceding date? Then quit... */
1484 48 : if ((fmask & DTK_DATE_M) != DTK_DATE_M)
1485 0 : return DTERR_BAD_FORMAT;
1486 :
1487 : /* reject consecutive unhandled units */
1488 48 : if (ptype != 0)
1489 0 : return DTERR_BAD_FORMAT;
1490 48 : ptype = val;
1491 48 : break;
1492 :
1493 36 : case UNKNOWN_FIELD:
1494 :
1495 : /*
1496 : * Before giving up and declaring error, check to see
1497 : * if it is an all-alpha timezone name.
1498 : */
1499 36 : namedTz = pg_tzset(field[i]);
1500 36 : if (!namedTz)
1501 36 : return DTERR_BAD_FORMAT;
1502 : /* we'll apply the zone setting below */
1503 0 : tmask = DTK_M(TZ);
1504 0 : break;
1505 :
1506 0 : default:
1507 0 : return DTERR_BAD_FORMAT;
1508 : }
1509 5544 : break;
1510 :
1511 0 : default:
1512 0 : return DTERR_BAD_FORMAT;
1513 : }
1514 :
1515 96071 : if (tmask & fmask)
1516 96 : return DTERR_BAD_FORMAT;
1517 95975 : fmask |= tmask;
1518 : } /* end loop over fields */
1519 :
1520 : /* reject if prefix type appeared and was never handled */
1521 42081 : if (ptype != 0)
1522 0 : return DTERR_BAD_FORMAT;
1523 :
1524 : /* do additional checking for normal date specs (but not "infinity" etc) */
1525 42081 : if (*dtype == DTK_DATE)
1526 : {
1527 : /* do final checking/adjustment of Y/M/D fields */
1528 41326 : dterr = ValidateDate(fmask, isjulian, is2digits, bc, tm);
1529 41326 : if (dterr)
1530 132 : return dterr;
1531 :
1532 : /* handle AM/PM */
1533 41194 : if (mer != HR24 && tm->tm_hour > HOURS_PER_DAY / 2)
1534 0 : return DTERR_FIELD_OVERFLOW;
1535 41194 : if (mer == AM && tm->tm_hour == HOURS_PER_DAY / 2)
1536 0 : tm->tm_hour = 0;
1537 41194 : else if (mer == PM && tm->tm_hour != HOURS_PER_DAY / 2)
1538 24 : tm->tm_hour += HOURS_PER_DAY / 2;
1539 :
1540 : /* check for incomplete input */
1541 41194 : if ((fmask & DTK_DATE_M) != DTK_DATE_M)
1542 : {
1543 4 : if ((fmask & DTK_TIME_M) == DTK_TIME_M)
1544 0 : return 1;
1545 4 : return DTERR_BAD_FORMAT;
1546 : }
1547 :
1548 : /*
1549 : * If we had a full timezone spec, compute the offset (we could not do
1550 : * it before, because we need the date to resolve DST status).
1551 : */
1552 41190 : if (namedTz != NULL)
1553 : {
1554 : /* daylight savings time modifier disallowed with full TZ */
1555 728 : if (fmask & DTK_M(DTZMOD))
1556 0 : return DTERR_BAD_FORMAT;
1557 :
1558 728 : *tzp = DetermineTimeZoneOffset(tm, namedTz);
1559 : }
1560 :
1561 : /*
1562 : * Likewise, if we had a dynamic timezone abbreviation, resolve it
1563 : * now.
1564 : */
1565 41190 : if (abbrevTz != NULL)
1566 : {
1567 : /* daylight savings time modifier disallowed with dynamic TZ */
1568 56 : if (fmask & DTK_M(DTZMOD))
1569 0 : return DTERR_BAD_FORMAT;
1570 :
1571 56 : *tzp = DetermineTimeZoneAbbrevOffset(tm, abbrev, abbrevTz);
1572 : }
1573 :
1574 : /* timezone not specified? then use session timezone */
1575 41190 : if (tzp != NULL && !(fmask & DTK_M(TZ)))
1576 : {
1577 : /*
1578 : * daylight savings time modifier but no standard timezone? then
1579 : * error
1580 : */
1581 20408 : if (fmask & DTK_M(DTZMOD))
1582 0 : return DTERR_BAD_FORMAT;
1583 :
1584 20408 : *tzp = DetermineTimeZoneOffset(tm, session_timezone);
1585 : }
1586 : }
1587 :
1588 41945 : return 0;
1589 : }
1590 :
1591 :
1592 : /* DetermineTimeZoneOffset()
1593 : *
1594 : * Given a struct pg_tm in which tm_year, tm_mon, tm_mday, tm_hour, tm_min,
1595 : * and tm_sec fields are set, and a zic-style time zone definition, determine
1596 : * the applicable GMT offset and daylight-savings status at that time.
1597 : * Set the struct pg_tm's tm_isdst field accordingly, and return the GMT
1598 : * offset as the function result.
1599 : *
1600 : * Note: if the date is out of the range we can deal with, we return zero
1601 : * as the GMT offset and set tm_isdst = 0. We don't throw an error here,
1602 : * though probably some higher-level code will.
1603 : */
1604 : int
1605 75348 : DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
1606 : {
1607 : pg_time_t t;
1608 :
1609 75348 : return DetermineTimeZoneOffsetInternal(tm, tzp, &t);
1610 : }
1611 :
1612 :
1613 : /* DetermineTimeZoneOffsetInternal()
1614 : *
1615 : * As above, but also return the actual UTC time imputed to the date/time
1616 : * into *tp.
1617 : *
1618 : * In event of an out-of-range date, we punt by returning zero into *tp.
1619 : * This is okay for the immediate callers but is a good reason for not
1620 : * exposing this worker function globally.
1621 : *
1622 : * Note: it might seem that we should use mktime() for this, but bitter
1623 : * experience teaches otherwise. This code is much faster than most versions
1624 : * of mktime(), anyway.
1625 : */
1626 : static int
1627 75486 : DetermineTimeZoneOffsetInternal(struct pg_tm *tm, pg_tz *tzp, pg_time_t *tp)
1628 : {
1629 : int date,
1630 : sec;
1631 : pg_time_t day,
1632 : mytime,
1633 : prevtime,
1634 : boundary,
1635 : beforetime,
1636 : aftertime;
1637 : long int before_gmtoff,
1638 : after_gmtoff;
1639 : int before_isdst,
1640 : after_isdst;
1641 : int res;
1642 :
1643 : /*
1644 : * First, generate the pg_time_t value corresponding to the given
1645 : * y/m/d/h/m/s taken as GMT time. If this overflows, punt and decide the
1646 : * timezone is GMT. (For a valid Julian date, integer overflow should be
1647 : * impossible with 64-bit pg_time_t, but let's check for safety.)
1648 : */
1649 75486 : if (!IS_VALID_JULIAN(tm->tm_year, tm->tm_mon, tm->tm_mday))
1650 16 : goto overflow;
1651 75470 : date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - UNIX_EPOCH_JDATE;
1652 :
1653 75470 : day = ((pg_time_t) date) * SECS_PER_DAY;
1654 75470 : if (day / SECS_PER_DAY != date)
1655 0 : goto overflow;
1656 75470 : sec = tm->tm_sec + (tm->tm_min + tm->tm_hour * MINS_PER_HOUR) * SECS_PER_MINUTE;
1657 75470 : mytime = day + sec;
1658 : /* since sec >= 0, overflow could only be from +day to -mytime */
1659 75470 : if (mytime < 0 && day > 0)
1660 0 : goto overflow;
1661 :
1662 : /*
1663 : * Find the DST time boundary just before or following the target time. We
1664 : * assume that all zones have GMT offsets less than 24 hours, and that DST
1665 : * boundaries can't be closer together than 48 hours, so backing up 24
1666 : * hours and finding the "next" boundary will work.
1667 : */
1668 75470 : prevtime = mytime - SECS_PER_DAY;
1669 75470 : if (mytime < 0 && prevtime > 0)
1670 0 : goto overflow;
1671 :
1672 75470 : res = pg_next_dst_boundary(&prevtime,
1673 : &before_gmtoff, &before_isdst,
1674 : &boundary,
1675 : &after_gmtoff, &after_isdst,
1676 : tzp);
1677 75470 : if (res < 0)
1678 0 : goto overflow; /* failure? */
1679 :
1680 75470 : if (res == 0)
1681 : {
1682 : /* Non-DST zone, life is simple */
1683 2709 : tm->tm_isdst = before_isdst;
1684 2709 : *tp = mytime - before_gmtoff;
1685 2709 : return -(int) before_gmtoff;
1686 : }
1687 :
1688 : /*
1689 : * Form the candidate pg_time_t values with local-time adjustment
1690 : */
1691 72761 : beforetime = mytime - before_gmtoff;
1692 72761 : if ((before_gmtoff > 0 &&
1693 8 : mytime < 0 && beforetime > 0) ||
1694 72761 : (before_gmtoff <= 0 &&
1695 64189 : mytime > 0 && beforetime < 0))
1696 0 : goto overflow;
1697 72761 : aftertime = mytime - after_gmtoff;
1698 72761 : if ((after_gmtoff > 0 &&
1699 8 : mytime < 0 && aftertime > 0) ||
1700 72761 : (after_gmtoff <= 0 &&
1701 64189 : mytime > 0 && aftertime < 0))
1702 0 : goto overflow;
1703 :
1704 : /*
1705 : * If both before or both after the boundary time, we know what to do. The
1706 : * boundary time itself is considered to be after the transition, which
1707 : * means we can accept aftertime == boundary in the second case.
1708 : */
1709 72761 : if (beforetime < boundary && aftertime < boundary)
1710 : {
1711 72140 : tm->tm_isdst = before_isdst;
1712 72140 : *tp = beforetime;
1713 72140 : return -(int) before_gmtoff;
1714 : }
1715 621 : if (beforetime > boundary && aftertime >= boundary)
1716 : {
1717 509 : tm->tm_isdst = after_isdst;
1718 509 : *tp = aftertime;
1719 509 : return -(int) after_gmtoff;
1720 : }
1721 :
1722 : /*
1723 : * It's an invalid or ambiguous time due to timezone transition. In a
1724 : * spring-forward transition, prefer the "before" interpretation; in a
1725 : * fall-back transition, prefer "after". (We used to define and implement
1726 : * this test as "prefer the standard-time interpretation", but that rule
1727 : * does not help to resolve the behavior when both times are reported as
1728 : * standard time; which does happen, eg Europe/Moscow in Oct 2014. Also,
1729 : * in some zones such as Europe/Dublin, there is widespread confusion
1730 : * about which time offset is "standard" time, so it's fortunate that our
1731 : * behavior doesn't depend on that.)
1732 : */
1733 112 : if (beforetime > aftertime)
1734 : {
1735 54 : tm->tm_isdst = before_isdst;
1736 54 : *tp = beforetime;
1737 54 : return -(int) before_gmtoff;
1738 : }
1739 58 : tm->tm_isdst = after_isdst;
1740 58 : *tp = aftertime;
1741 58 : return -(int) after_gmtoff;
1742 :
1743 16 : overflow:
1744 : /* Given date is out of range, so assume UTC */
1745 16 : tm->tm_isdst = 0;
1746 16 : *tp = 0;
1747 16 : return 0;
1748 : }
1749 :
1750 :
1751 : /* DetermineTimeZoneAbbrevOffset()
1752 : *
1753 : * Determine the GMT offset and DST flag to be attributed to a dynamic
1754 : * time zone abbreviation, that is one whose meaning has changed over time.
1755 : * *tm contains the local time at which the meaning should be determined,
1756 : * and tm->tm_isdst receives the DST flag.
1757 : *
1758 : * This differs from the behavior of DetermineTimeZoneOffset() in that a
1759 : * standard-time or daylight-time abbreviation forces use of the corresponding
1760 : * GMT offset even when the zone was then in DS or standard time respectively.
1761 : * (However, that happens only if we can match the given abbreviation to some
1762 : * abbreviation that appears in the IANA timezone data. Otherwise, we fall
1763 : * back to doing DetermineTimeZoneOffset().)
1764 : */
1765 : int
1766 138 : DetermineTimeZoneAbbrevOffset(struct pg_tm *tm, const char *abbr, pg_tz *tzp)
1767 : {
1768 : pg_time_t t;
1769 : int zone_offset;
1770 : int abbr_offset;
1771 : int abbr_isdst;
1772 :
1773 : /*
1774 : * Compute the UTC time we want to probe at. (In event of overflow, we'll
1775 : * probe at the epoch, which is a bit random but probably doesn't matter.)
1776 : */
1777 138 : zone_offset = DetermineTimeZoneOffsetInternal(tm, tzp, &t);
1778 :
1779 : /*
1780 : * Try to match the abbreviation to something in the zone definition.
1781 : */
1782 138 : if (DetermineTimeZoneAbbrevOffsetInternal(t, abbr, tzp,
1783 : &abbr_offset, &abbr_isdst))
1784 : {
1785 : /* Success, so use the abbrev-specific answers. */
1786 138 : tm->tm_isdst = abbr_isdst;
1787 138 : return abbr_offset;
1788 : }
1789 :
1790 : /*
1791 : * No match, so use the answers we already got from
1792 : * DetermineTimeZoneOffsetInternal.
1793 : */
1794 0 : return zone_offset;
1795 : }
1796 :
1797 :
1798 : /* DetermineTimeZoneAbbrevOffsetTS()
1799 : *
1800 : * As above but the probe time is specified as a TimestampTz (hence, UTC time),
1801 : * and DST status is returned into *isdst rather than into tm->tm_isdst.
1802 : */
1803 : int
1804 856 : DetermineTimeZoneAbbrevOffsetTS(TimestampTz ts, const char *abbr,
1805 : pg_tz *tzp, int *isdst)
1806 : {
1807 856 : pg_time_t t = timestamptz_to_time_t(ts);
1808 : int zone_offset;
1809 : int abbr_offset;
1810 : int tz;
1811 : struct pg_tm tm;
1812 : fsec_t fsec;
1813 :
1814 : /*
1815 : * If the abbrev matches anything in the zone data, this is pretty easy.
1816 : */
1817 856 : if (DetermineTimeZoneAbbrevOffsetInternal(t, abbr, tzp,
1818 : &abbr_offset, isdst))
1819 76 : return abbr_offset;
1820 :
1821 : /*
1822 : * Else, break down the timestamp so we can use DetermineTimeZoneOffset.
1823 : */
1824 780 : if (timestamp2tm(ts, &tz, &tm, &fsec, NULL, tzp) != 0)
1825 0 : ereport(ERROR,
1826 : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1827 : errmsg("timestamp out of range")));
1828 :
1829 780 : zone_offset = DetermineTimeZoneOffset(&tm, tzp);
1830 780 : *isdst = tm.tm_isdst;
1831 780 : return zone_offset;
1832 : }
1833 :
1834 :
1835 : /* DetermineTimeZoneAbbrevOffsetInternal()
1836 : *
1837 : * Workhorse for above two functions: work from a pg_time_t probe instant.
1838 : * On success, return GMT offset and DST status into *offset and *isdst.
1839 : */
1840 : static bool
1841 994 : DetermineTimeZoneAbbrevOffsetInternal(pg_time_t t, const char *abbr, pg_tz *tzp,
1842 : int *offset, int *isdst)
1843 : {
1844 : char upabbr[TZ_STRLEN_MAX + 1];
1845 : unsigned char *p;
1846 : long int gmtoff;
1847 :
1848 : /* We need to force the abbrev to upper case */
1849 994 : strlcpy(upabbr, abbr, sizeof(upabbr));
1850 4660 : for (p = (unsigned char *) upabbr; *p; p++)
1851 3666 : *p = pg_toupper(*p);
1852 :
1853 : /* Look up the abbrev's meaning at this time in this zone */
1854 994 : if (pg_interpret_timezone_abbrev(upabbr,
1855 : &t,
1856 : &gmtoff,
1857 : isdst,
1858 : tzp))
1859 : {
1860 : /* Change sign to agree with DetermineTimeZoneOffset() */
1861 214 : *offset = (int) -gmtoff;
1862 214 : return true;
1863 : }
1864 780 : return false;
1865 : }
1866 :
1867 :
1868 : /* TimeZoneAbbrevIsKnown()
1869 : *
1870 : * Detect whether the given string is a time zone abbreviation that's known
1871 : * in the specified TZDB timezone, and if so whether it's fixed or varying
1872 : * meaning. The match is not case-sensitive.
1873 : */
1874 : static bool
1875 4947 : TimeZoneAbbrevIsKnown(const char *abbr, pg_tz *tzp,
1876 : bool *isfixed, int *offset, int *isdst)
1877 : {
1878 : char upabbr[TZ_STRLEN_MAX + 1];
1879 : unsigned char *p;
1880 : long int gmtoff;
1881 :
1882 : /* We need to force the abbrev to upper case */
1883 4947 : strlcpy(upabbr, abbr, sizeof(upabbr));
1884 27463 : for (p = (unsigned char *) upabbr; *p; p++)
1885 22516 : *p = pg_toupper(*p);
1886 :
1887 : /* Look up the abbrev's meaning in this zone */
1888 4947 : if (pg_timezone_abbrev_is_known(upabbr,
1889 : isfixed,
1890 : &gmtoff,
1891 : isdst,
1892 : tzp))
1893 : {
1894 : /* Change sign to agree with DetermineTimeZoneOffset() */
1895 153 : *offset = (int) -gmtoff;
1896 153 : return true;
1897 : }
1898 4794 : return false;
1899 : }
1900 :
1901 :
1902 : /* DecodeTimeOnly()
1903 : * Interpret parsed string as time fields only.
1904 : * Returns 0 if successful, DTERR code if bogus input detected.
1905 : *
1906 : * Inputs are field[] and ftype[] arrays, of length nf.
1907 : * Other arguments are outputs.
1908 : *
1909 : * Note that support for time zone is here for
1910 : * SQL TIME WITH TIME ZONE, but it reveals
1911 : * bogosity with SQL date/time standards, since
1912 : * we must infer a time zone from current time.
1913 : * - thomas 2000-03-10
1914 : * Allow specifying date to get a better time zone,
1915 : * if time zones are allowed. - thomas 2001-12-26
1916 : */
1917 : int
1918 2462 : DecodeTimeOnly(char **field, int *ftype, int nf,
1919 : int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp,
1920 : DateTimeErrorExtra *extra)
1921 : {
1922 2462 : int fmask = 0,
1923 : tmask,
1924 : type;
1925 2462 : int ptype = 0; /* "prefix type" for ISO and Julian formats */
1926 : int i;
1927 : int val;
1928 : int dterr;
1929 2462 : bool isjulian = false;
1930 2462 : bool is2digits = false;
1931 2462 : bool bc = false;
1932 2462 : int mer = HR24;
1933 2462 : pg_tz *namedTz = NULL;
1934 2462 : pg_tz *abbrevTz = NULL;
1935 2462 : char *abbrev = NULL;
1936 : pg_tz *valtz;
1937 :
1938 2462 : *dtype = DTK_TIME;
1939 2462 : tm->tm_hour = 0;
1940 2462 : tm->tm_min = 0;
1941 2462 : tm->tm_sec = 0;
1942 2462 : *fsec = 0;
1943 : /* don't know daylight savings time status apriori */
1944 2462 : tm->tm_isdst = -1;
1945 :
1946 2462 : if (tzp != NULL)
1947 2462 : *tzp = 0;
1948 :
1949 6372 : for (i = 0; i < nf; i++)
1950 : {
1951 3926 : switch (ftype[i])
1952 : {
1953 749 : case DTK_DATE:
1954 :
1955 : /*
1956 : * Time zone not allowed? Then should not accept dates or time
1957 : * zones no matter what else!
1958 : */
1959 749 : if (tzp == NULL)
1960 0 : return DTERR_BAD_FORMAT;
1961 :
1962 : /* Under limited circumstances, we will accept a date... */
1963 749 : if (i == 0 && nf >= 2 &&
1964 140 : (ftype[nf - 1] == DTK_DATE || ftype[1] == DTK_TIME))
1965 : {
1966 140 : dterr = DecodeDate(field[i], fmask,
1967 : &tmask, &is2digits, tm);
1968 140 : if (dterr)
1969 0 : return dterr;
1970 : }
1971 : /* otherwise, this is a time and/or time zone */
1972 : else
1973 : {
1974 609 : if (isdigit((unsigned char) *field[i]))
1975 : {
1976 : char *cp;
1977 :
1978 : /*
1979 : * Starts with a digit but we already have a time
1980 : * field? Then we are in trouble with time already...
1981 : */
1982 0 : if ((fmask & DTK_TIME_M) == DTK_TIME_M)
1983 0 : return DTERR_BAD_FORMAT;
1984 :
1985 : /*
1986 : * Should not get here and fail. Sanity check only...
1987 : */
1988 0 : if ((cp = strchr(field[i], '-')) == NULL)
1989 0 : return DTERR_BAD_FORMAT;
1990 :
1991 : /* Get the time zone from the end of the string */
1992 0 : dterr = DecodeTimezone(cp, tzp);
1993 0 : if (dterr)
1994 0 : return dterr;
1995 0 : *cp = '\0';
1996 :
1997 : /*
1998 : * Then read the rest of the field as a concatenated
1999 : * time
2000 : */
2001 0 : dterr = DecodeNumberField(strlen(field[i]), field[i],
2002 : (fmask | DTK_DATE_M),
2003 : &tmask, tm,
2004 : fsec, &is2digits);
2005 0 : if (dterr < 0)
2006 0 : return dterr;
2007 0 : ftype[i] = dterr;
2008 :
2009 0 : tmask |= DTK_M(TZ);
2010 : }
2011 : else
2012 : {
2013 609 : namedTz = pg_tzset(field[i]);
2014 609 : if (!namedTz)
2015 : {
2016 0 : extra->dtee_timezone = field[i];
2017 0 : return DTERR_BAD_TIMEZONE;
2018 : }
2019 : /* we'll apply the zone setting below */
2020 609 : ftype[i] = DTK_TZ;
2021 609 : tmask = DTK_M(TZ);
2022 : }
2023 : }
2024 749 : break;
2025 :
2026 2346 : case DTK_TIME:
2027 :
2028 : /*
2029 : * This might be an ISO time following a "t" field.
2030 : */
2031 2346 : if (ptype != 0)
2032 : {
2033 24 : if (ptype != DTK_TIME)
2034 0 : return DTERR_BAD_FORMAT;
2035 24 : ptype = 0;
2036 : }
2037 :
2038 2346 : dterr = DecodeTime(field[i], (fmask | DTK_DATE_M),
2039 : INTERVAL_FULL_RANGE,
2040 : &tmask, tm, fsec);
2041 2346 : if (dterr)
2042 0 : return dterr;
2043 2346 : break;
2044 :
2045 420 : case DTK_TZ:
2046 : {
2047 : int tz;
2048 :
2049 420 : if (tzp == NULL)
2050 0 : return DTERR_BAD_FORMAT;
2051 :
2052 420 : dterr = DecodeTimezone(field[i], &tz);
2053 420 : if (dterr)
2054 0 : return dterr;
2055 420 : *tzp = tz;
2056 420 : tmask = DTK_M(TZ);
2057 : }
2058 420 : break;
2059 :
2060 120 : case DTK_NUMBER:
2061 :
2062 : /*
2063 : * Deal with cases where previous field labeled this one
2064 : */
2065 120 : if (ptype != 0)
2066 : {
2067 : char *cp;
2068 : int value;
2069 :
2070 80 : errno = 0;
2071 80 : value = strtoint(field[i], &cp, 10);
2072 80 : if (errno == ERANGE)
2073 16 : return DTERR_FIELD_OVERFLOW;
2074 80 : if (*cp != '.' && *cp != '\0')
2075 0 : return DTERR_BAD_FORMAT;
2076 :
2077 80 : switch (ptype)
2078 : {
2079 4 : case DTK_JULIAN:
2080 : /* previous field was a label for "julian date" */
2081 4 : if (tzp == NULL)
2082 0 : return DTERR_BAD_FORMAT;
2083 4 : if (value < 0)
2084 0 : return DTERR_FIELD_OVERFLOW;
2085 4 : tmask = DTK_DATE_M;
2086 4 : j2date(value, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2087 4 : isjulian = true;
2088 :
2089 4 : if (*cp == '.')
2090 : {
2091 : double time;
2092 :
2093 0 : dterr = ParseFraction(cp, &time);
2094 0 : if (dterr)
2095 0 : return dterr;
2096 0 : time *= USECS_PER_DAY;
2097 0 : dt2time(time,
2098 : &tm->tm_hour, &tm->tm_min,
2099 : &tm->tm_sec, fsec);
2100 0 : tmask |= DTK_TIME_M;
2101 : }
2102 4 : break;
2103 :
2104 68 : case DTK_TIME:
2105 : /* previous field was "t" for ISO time */
2106 68 : dterr = DecodeNumberField(strlen(field[i]), field[i],
2107 : (fmask | DTK_DATE_M),
2108 : &tmask, tm,
2109 : fsec, &is2digits);
2110 68 : if (dterr < 0)
2111 8 : return dterr;
2112 60 : ftype[i] = dterr;
2113 :
2114 60 : if (tmask != DTK_TIME_M)
2115 0 : return DTERR_BAD_FORMAT;
2116 60 : break;
2117 :
2118 8 : default:
2119 8 : return DTERR_BAD_FORMAT;
2120 : break;
2121 : }
2122 :
2123 64 : ptype = 0;
2124 64 : *dtype = DTK_DATE;
2125 : }
2126 : else
2127 : {
2128 : char *cp;
2129 : int flen;
2130 :
2131 40 : flen = strlen(field[i]);
2132 40 : cp = strchr(field[i], '.');
2133 :
2134 : /* Embedded decimal? */
2135 40 : if (cp != NULL)
2136 : {
2137 : /*
2138 : * Under limited circumstances, we will accept a
2139 : * date...
2140 : */
2141 24 : if (i == 0 && nf >= 2 && ftype[nf - 1] == DTK_DATE)
2142 : {
2143 0 : dterr = DecodeDate(field[i], fmask,
2144 : &tmask, &is2digits, tm);
2145 0 : if (dterr)
2146 0 : return dterr;
2147 : }
2148 : /* embedded decimal and several digits before? */
2149 24 : else if (flen - strlen(cp) > 2)
2150 : {
2151 : /*
2152 : * Interpret as a concatenated date or time Set
2153 : * the type field to allow decoding other fields
2154 : * later. Example: 20011223 or 040506
2155 : */
2156 24 : dterr = DecodeNumberField(flen, field[i],
2157 : (fmask | DTK_DATE_M),
2158 : &tmask, tm,
2159 : fsec, &is2digits);
2160 24 : if (dterr < 0)
2161 0 : return dterr;
2162 24 : ftype[i] = dterr;
2163 : }
2164 : else
2165 0 : return DTERR_BAD_FORMAT;
2166 : }
2167 16 : else if (flen > 4)
2168 : {
2169 8 : dterr = DecodeNumberField(flen, field[i],
2170 : (fmask | DTK_DATE_M),
2171 : &tmask, tm,
2172 : fsec, &is2digits);
2173 8 : if (dterr < 0)
2174 0 : return dterr;
2175 8 : ftype[i] = dterr;
2176 : }
2177 : /* otherwise it is a single date/time field... */
2178 : else
2179 : {
2180 8 : dterr = DecodeNumber(flen, field[i],
2181 : false,
2182 : (fmask | DTK_DATE_M),
2183 : &tmask, tm,
2184 : fsec, &is2digits);
2185 8 : if (dterr)
2186 0 : return dterr;
2187 : }
2188 : }
2189 104 : break;
2190 :
2191 291 : case DTK_STRING:
2192 : case DTK_SPECIAL:
2193 : /* timezone abbrevs take precedence over built-in tokens */
2194 291 : dterr = DecodeTimezoneAbbrev(i, field[i],
2195 : &type, &val, &valtz, extra);
2196 291 : if (dterr)
2197 0 : return dterr;
2198 291 : if (type == UNKNOWN_FIELD)
2199 120 : type = DecodeSpecial(i, field[i], &val);
2200 291 : if (type == IGNORE_DTF)
2201 0 : continue;
2202 :
2203 291 : tmask = DTK_M(type);
2204 291 : switch (type)
2205 : {
2206 8 : case RESERV:
2207 8 : switch (val)
2208 : {
2209 8 : case DTK_NOW:
2210 8 : tmask = DTK_TIME_M;
2211 8 : *dtype = DTK_TIME;
2212 8 : GetCurrentTimeUsec(tm, fsec, NULL);
2213 8 : break;
2214 :
2215 0 : case DTK_ZULU:
2216 0 : tmask = (DTK_TIME_M | DTK_M(TZ));
2217 0 : *dtype = DTK_TIME;
2218 0 : tm->tm_hour = 0;
2219 0 : tm->tm_min = 0;
2220 0 : tm->tm_sec = 0;
2221 0 : tm->tm_isdst = 0;
2222 0 : break;
2223 :
2224 0 : default:
2225 0 : return DTERR_BAD_FORMAT;
2226 : }
2227 :
2228 8 : break;
2229 :
2230 0 : case DTZMOD:
2231 :
2232 : /*
2233 : * daylight savings time modifier (solves "MET DST"
2234 : * syntax)
2235 : */
2236 0 : tmask |= DTK_M(DTZ);
2237 0 : tm->tm_isdst = 1;
2238 0 : if (tzp == NULL)
2239 0 : return DTERR_BAD_FORMAT;
2240 0 : *tzp -= val;
2241 0 : break;
2242 :
2243 132 : case DTZ:
2244 :
2245 : /*
2246 : * set mask for TZ here _or_ check for DTZ later when
2247 : * getting default timezone
2248 : */
2249 132 : tmask |= DTK_M(TZ);
2250 132 : tm->tm_isdst = 1;
2251 132 : if (tzp == NULL)
2252 0 : return DTERR_BAD_FORMAT;
2253 132 : *tzp = -val;
2254 132 : ftype[i] = DTK_TZ;
2255 132 : break;
2256 :
2257 35 : case TZ:
2258 35 : tm->tm_isdst = 0;
2259 35 : if (tzp == NULL)
2260 0 : return DTERR_BAD_FORMAT;
2261 35 : *tzp = -val;
2262 35 : ftype[i] = DTK_TZ;
2263 35 : break;
2264 :
2265 4 : case DYNTZ:
2266 4 : tmask |= DTK_M(TZ);
2267 4 : if (tzp == NULL)
2268 0 : return DTERR_BAD_FORMAT;
2269 : /* we'll determine the actual offset later */
2270 4 : abbrevTz = valtz;
2271 4 : abbrev = field[i];
2272 4 : ftype[i] = DTK_TZ;
2273 4 : break;
2274 :
2275 8 : case AMPM:
2276 8 : mer = val;
2277 8 : break;
2278 :
2279 0 : case ADBC:
2280 0 : bc = (val == BC);
2281 0 : break;
2282 :
2283 12 : case UNITS:
2284 12 : tmask = 0;
2285 : /* reject consecutive unhandled units */
2286 12 : if (ptype != 0)
2287 0 : return DTERR_BAD_FORMAT;
2288 12 : ptype = val;
2289 12 : break;
2290 :
2291 92 : case ISOTIME:
2292 92 : tmask = 0;
2293 : /* reject consecutive unhandled units */
2294 92 : if (ptype != 0)
2295 0 : return DTERR_BAD_FORMAT;
2296 92 : ptype = val;
2297 92 : break;
2298 :
2299 0 : case UNKNOWN_FIELD:
2300 :
2301 : /*
2302 : * Before giving up and declaring error, check to see
2303 : * if it is an all-alpha timezone name.
2304 : */
2305 0 : namedTz = pg_tzset(field[i]);
2306 0 : if (!namedTz)
2307 0 : return DTERR_BAD_FORMAT;
2308 : /* we'll apply the zone setting below */
2309 0 : tmask = DTK_M(TZ);
2310 0 : break;
2311 :
2312 0 : default:
2313 0 : return DTERR_BAD_FORMAT;
2314 : }
2315 291 : break;
2316 :
2317 0 : default:
2318 0 : return DTERR_BAD_FORMAT;
2319 : }
2320 :
2321 3910 : if (tmask & fmask)
2322 0 : return DTERR_BAD_FORMAT;
2323 3910 : fmask |= tmask;
2324 : } /* end loop over fields */
2325 :
2326 : /* reject if prefix type appeared and was never handled */
2327 2446 : if (ptype != 0)
2328 0 : return DTERR_BAD_FORMAT;
2329 :
2330 : /* do final checking/adjustment of Y/M/D fields */
2331 2446 : dterr = ValidateDate(fmask, isjulian, is2digits, bc, tm);
2332 2446 : if (dterr)
2333 0 : return dterr;
2334 :
2335 : /* handle AM/PM */
2336 2446 : if (mer != HR24 && tm->tm_hour > HOURS_PER_DAY / 2)
2337 0 : return DTERR_FIELD_OVERFLOW;
2338 2446 : if (mer == AM && tm->tm_hour == HOURS_PER_DAY / 2)
2339 0 : tm->tm_hour = 0;
2340 2446 : else if (mer == PM && tm->tm_hour != HOURS_PER_DAY / 2)
2341 8 : tm->tm_hour += HOURS_PER_DAY / 2;
2342 :
2343 : /* check for time overflow */
2344 2446 : if (time_overflows(tm->tm_hour, tm->tm_min, tm->tm_sec, *fsec))
2345 48 : return DTERR_FIELD_OVERFLOW;
2346 :
2347 2398 : if ((fmask & DTK_TIME_M) != DTK_TIME_M)
2348 0 : return DTERR_BAD_FORMAT;
2349 :
2350 : /*
2351 : * If we had a full timezone spec, compute the offset (we could not do it
2352 : * before, because we may need the date to resolve DST status).
2353 : */
2354 2398 : if (namedTz != NULL)
2355 : {
2356 : long int gmtoff;
2357 :
2358 : /* daylight savings time modifier disallowed with full TZ */
2359 609 : if (fmask & DTK_M(DTZMOD))
2360 28 : return DTERR_BAD_FORMAT;
2361 :
2362 : /* if non-DST zone, we do not need to know the date */
2363 609 : if (pg_get_timezone_offset(namedTz, &gmtoff))
2364 : {
2365 557 : *tzp = -(int) gmtoff;
2366 : }
2367 : else
2368 : {
2369 : /* a date has to be specified */
2370 52 : if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2371 28 : return DTERR_BAD_FORMAT;
2372 24 : *tzp = DetermineTimeZoneOffset(tm, namedTz);
2373 : }
2374 : }
2375 :
2376 : /*
2377 : * Likewise, if we had a dynamic timezone abbreviation, resolve it now.
2378 : */
2379 2370 : if (abbrevTz != NULL)
2380 : {
2381 : struct pg_tm tt,
2382 0 : *tmp = &tt;
2383 :
2384 : /*
2385 : * daylight savings time modifier but no standard timezone? then error
2386 : */
2387 0 : if (fmask & DTK_M(DTZMOD))
2388 0 : return DTERR_BAD_FORMAT;
2389 :
2390 0 : if ((fmask & DTK_DATE_M) == 0)
2391 0 : GetCurrentDateTime(tmp);
2392 : else
2393 : {
2394 : /* a date has to be specified */
2395 0 : if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2396 0 : return DTERR_BAD_FORMAT;
2397 0 : tmp->tm_year = tm->tm_year;
2398 0 : tmp->tm_mon = tm->tm_mon;
2399 0 : tmp->tm_mday = tm->tm_mday;
2400 : }
2401 0 : tmp->tm_hour = tm->tm_hour;
2402 0 : tmp->tm_min = tm->tm_min;
2403 0 : tmp->tm_sec = tm->tm_sec;
2404 0 : *tzp = DetermineTimeZoneAbbrevOffset(tmp, abbrev, abbrevTz);
2405 0 : tm->tm_isdst = tmp->tm_isdst;
2406 : }
2407 :
2408 : /* timezone not specified? then use session timezone */
2409 2370 : if (tzp != NULL && !(fmask & DTK_M(TZ)))
2410 : {
2411 : struct pg_tm tt,
2412 1226 : *tmp = &tt;
2413 :
2414 : /*
2415 : * daylight savings time modifier but no standard timezone? then error
2416 : */
2417 1226 : if (fmask & DTK_M(DTZMOD))
2418 0 : return DTERR_BAD_FORMAT;
2419 :
2420 1226 : if ((fmask & DTK_DATE_M) == 0)
2421 1174 : GetCurrentDateTime(tmp);
2422 : else
2423 : {
2424 : /* a date has to be specified */
2425 52 : if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2426 0 : return DTERR_BAD_FORMAT;
2427 52 : tmp->tm_year = tm->tm_year;
2428 52 : tmp->tm_mon = tm->tm_mon;
2429 52 : tmp->tm_mday = tm->tm_mday;
2430 : }
2431 1226 : tmp->tm_hour = tm->tm_hour;
2432 1226 : tmp->tm_min = tm->tm_min;
2433 1226 : tmp->tm_sec = tm->tm_sec;
2434 1226 : *tzp = DetermineTimeZoneOffset(tmp, session_timezone);
2435 1226 : tm->tm_isdst = tmp->tm_isdst;
2436 : }
2437 :
2438 2370 : return 0;
2439 : }
2440 :
2441 : /* DecodeDate()
2442 : * Decode date string which includes delimiters.
2443 : * Return 0 if okay, a DTERR code if not.
2444 : *
2445 : * str: field to be parsed
2446 : * fmask: bitmask for field types already seen
2447 : * *tmask: receives bitmask for fields found here
2448 : * *is2digits: set to true if we find 2-digit year
2449 : * *tm: field values are stored into appropriate members of this struct
2450 : */
2451 : static int
2452 39396 : DecodeDate(char *str, int fmask, int *tmask, bool *is2digits,
2453 : struct pg_tm *tm)
2454 : {
2455 : fsec_t fsec;
2456 39396 : int nf = 0;
2457 : int i,
2458 : len;
2459 : int dterr;
2460 39396 : bool haveTextMonth = false;
2461 : int type,
2462 : val,
2463 39396 : dmask = 0;
2464 : char *field[MAXDATEFIELDS];
2465 :
2466 39396 : *tmask = 0;
2467 :
2468 : /* parse this string... */
2469 157540 : while (*str != '\0' && nf < MAXDATEFIELDS)
2470 : {
2471 : /* skip field separators */
2472 118144 : while (*str != '\0' && !isalnum((unsigned char) *str))
2473 0 : str++;
2474 :
2475 118144 : if (*str == '\0')
2476 0 : return DTERR_BAD_FORMAT; /* end of string after separator */
2477 :
2478 118144 : field[nf] = str;
2479 118144 : if (isdigit((unsigned char) *str))
2480 : {
2481 432906 : while (isdigit((unsigned char) *str))
2482 314858 : str++;
2483 : }
2484 96 : else if (isalpha((unsigned char) *str))
2485 : {
2486 384 : while (isalpha((unsigned char) *str))
2487 288 : str++;
2488 : }
2489 :
2490 : /* Just get rid of any non-digit, non-alpha characters... */
2491 118144 : if (*str != '\0')
2492 78772 : *str++ = '\0';
2493 118144 : nf++;
2494 : }
2495 :
2496 : /* look first for text fields, since that will be unambiguous month */
2497 157540 : for (i = 0; i < nf; i++)
2498 : {
2499 118144 : if (isalpha((unsigned char) *field[i]))
2500 : {
2501 96 : type = DecodeSpecial(i, field[i], &val);
2502 96 : if (type == IGNORE_DTF)
2503 0 : continue;
2504 :
2505 96 : dmask = DTK_M(type);
2506 96 : switch (type)
2507 : {
2508 96 : case MONTH:
2509 96 : tm->tm_mon = val;
2510 96 : haveTextMonth = true;
2511 96 : break;
2512 :
2513 0 : default:
2514 0 : return DTERR_BAD_FORMAT;
2515 : }
2516 96 : if (fmask & dmask)
2517 0 : return DTERR_BAD_FORMAT;
2518 :
2519 96 : fmask |= dmask;
2520 96 : *tmask |= dmask;
2521 :
2522 : /* mark this field as being completed */
2523 96 : field[i] = NULL;
2524 : }
2525 : }
2526 :
2527 : /* now pick up remaining numeric fields */
2528 157540 : for (i = 0; i < nf; i++)
2529 : {
2530 118144 : if (field[i] == NULL)
2531 96 : continue;
2532 :
2533 118048 : if ((len = strlen(field[i])) <= 0)
2534 0 : return DTERR_BAD_FORMAT;
2535 :
2536 118048 : dterr = DecodeNumber(len, field[i], haveTextMonth, fmask,
2537 : &dmask, tm,
2538 : &fsec, is2digits);
2539 118048 : if (dterr)
2540 0 : return dterr;
2541 :
2542 118048 : if (fmask & dmask)
2543 0 : return DTERR_BAD_FORMAT;
2544 :
2545 118048 : fmask |= dmask;
2546 118048 : *tmask |= dmask;
2547 : }
2548 :
2549 39396 : if ((fmask & ~(DTK_M(DOY) | DTK_M(TZ))) != DTK_DATE_M)
2550 24 : return DTERR_BAD_FORMAT;
2551 :
2552 : /* validation of the field values must wait until ValidateDate() */
2553 :
2554 39372 : return 0;
2555 : }
2556 :
2557 : /* ValidateDate()
2558 : * Check valid year/month/day values, handle BC and DOY cases
2559 : * Return 0 if okay, a DTERR code if not.
2560 : */
2561 : int
2562 47973 : ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc,
2563 : struct pg_tm *tm)
2564 : {
2565 47973 : if (fmask & DTK_M(YEAR))
2566 : {
2567 45663 : if (isjulian)
2568 : {
2569 : /* tm_year is correct and should not be touched */
2570 : }
2571 41587 : else if (bc)
2572 : {
2573 : /* there is no year zero in AD/BC notation */
2574 220 : if (tm->tm_year <= 0)
2575 0 : return DTERR_FIELD_OVERFLOW;
2576 : /* internally, we represent 1 BC as year zero, 2 BC as -1, etc */
2577 220 : tm->tm_year = -(tm->tm_year - 1);
2578 : }
2579 41367 : else if (is2digits)
2580 : {
2581 : /* process 1 or 2-digit input as 1970-2069 AD, allow '0' and '00' */
2582 236 : if (tm->tm_year < 0) /* just paranoia */
2583 0 : return DTERR_FIELD_OVERFLOW;
2584 236 : if (tm->tm_year < 70)
2585 116 : tm->tm_year += 2000;
2586 120 : else if (tm->tm_year < 100)
2587 120 : tm->tm_year += 1900;
2588 : }
2589 : else
2590 : {
2591 : /* there is no year zero in AD/BC notation */
2592 41131 : if (tm->tm_year <= 0)
2593 8 : return DTERR_FIELD_OVERFLOW;
2594 : }
2595 : }
2596 :
2597 : /* now that we have correct year, decode DOY */
2598 47965 : if (fmask & DTK_M(DOY))
2599 : {
2600 20 : j2date(date2j(tm->tm_year, 1, 1) + tm->tm_yday - 1,
2601 : &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2602 : }
2603 :
2604 : /* check for valid month */
2605 47965 : if (fmask & DTK_M(MONTH))
2606 : {
2607 45639 : if (tm->tm_mon < 1 || tm->tm_mon > MONTHS_PER_YEAR)
2608 52 : return DTERR_MD_FIELD_OVERFLOW;
2609 : }
2610 :
2611 : /* minimal check for valid day */
2612 47913 : if (fmask & DTK_M(DAY))
2613 : {
2614 45563 : if (tm->tm_mday < 1 || tm->tm_mday > 31)
2615 92 : return DTERR_MD_FIELD_OVERFLOW;
2616 : }
2617 :
2618 47821 : if ((fmask & DTK_DATE_M) == DTK_DATE_M)
2619 : {
2620 : /*
2621 : * Check for valid day of month, now that we know for sure the month
2622 : * and year. Note we don't use MD_FIELD_OVERFLOW here, since it seems
2623 : * unlikely that "Feb 29" is a YMD-order error.
2624 : */
2625 45459 : if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
2626 32 : return DTERR_FIELD_OVERFLOW;
2627 : }
2628 :
2629 47789 : return 0;
2630 : }
2631 :
2632 :
2633 : /* DecodeTimeCommon()
2634 : * Decode time string which includes delimiters.
2635 : * Return 0 if okay, a DTERR code if not.
2636 : * tmask and itm are output parameters.
2637 : *
2638 : * This code is shared between the timestamp and interval cases.
2639 : * We return a struct pg_itm (of which only the tm_usec, tm_sec, tm_min,
2640 : * and tm_hour fields are used) and let the wrapper functions below
2641 : * convert and range-check as necessary.
2642 : */
2643 : static int
2644 31743 : DecodeTimeCommon(char *str, int fmask, int range,
2645 : int *tmask, struct pg_itm *itm)
2646 : {
2647 : char *cp;
2648 : int dterr;
2649 31743 : fsec_t fsec = 0;
2650 :
2651 31743 : *tmask = DTK_TIME_M;
2652 :
2653 31743 : errno = 0;
2654 31743 : itm->tm_hour = strtoi64(str, &cp, 10);
2655 31743 : if (errno == ERANGE)
2656 0 : return DTERR_FIELD_OVERFLOW;
2657 31743 : if (*cp != ':')
2658 0 : return DTERR_BAD_FORMAT;
2659 31743 : errno = 0;
2660 31743 : itm->tm_min = strtoint(cp + 1, &cp, 10);
2661 31743 : if (errno == ERANGE)
2662 0 : return DTERR_FIELD_OVERFLOW;
2663 31743 : if (*cp == '\0')
2664 : {
2665 1076 : itm->tm_sec = 0;
2666 : /* If it's a MINUTE TO SECOND interval, take 2 fields as being mm:ss */
2667 1076 : if (range == (INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND)))
2668 : {
2669 12 : if (itm->tm_hour > INT_MAX || itm->tm_hour < INT_MIN)
2670 0 : return DTERR_FIELD_OVERFLOW;
2671 12 : itm->tm_sec = itm->tm_min;
2672 12 : itm->tm_min = (int) itm->tm_hour;
2673 12 : itm->tm_hour = 0;
2674 : }
2675 : }
2676 30667 : else if (*cp == '.')
2677 : {
2678 : /* always assume mm:ss.sss is MINUTE TO SECOND */
2679 32 : dterr = ParseFractionalSecond(cp, &fsec);
2680 32 : if (dterr)
2681 8 : return dterr;
2682 24 : if (itm->tm_hour > INT_MAX || itm->tm_hour < INT_MIN)
2683 0 : return DTERR_FIELD_OVERFLOW;
2684 24 : itm->tm_sec = itm->tm_min;
2685 24 : itm->tm_min = (int) itm->tm_hour;
2686 24 : itm->tm_hour = 0;
2687 : }
2688 30635 : else if (*cp == ':')
2689 : {
2690 30635 : errno = 0;
2691 30635 : itm->tm_sec = strtoint(cp + 1, &cp, 10);
2692 30635 : if (errno == ERANGE)
2693 0 : return DTERR_FIELD_OVERFLOW;
2694 30635 : if (*cp == '.')
2695 : {
2696 11546 : dterr = ParseFractionalSecond(cp, &fsec);
2697 11546 : if (dterr)
2698 0 : return dterr;
2699 : }
2700 19089 : else if (*cp != '\0')
2701 0 : return DTERR_BAD_FORMAT;
2702 : }
2703 : else
2704 0 : return DTERR_BAD_FORMAT;
2705 :
2706 : /* do a sanity check; but caller must check the range of tm_hour */
2707 31735 : if (itm->tm_hour < 0 ||
2708 31735 : itm->tm_min < 0 || itm->tm_min > MINS_PER_HOUR - 1 ||
2709 31735 : itm->tm_sec < 0 || itm->tm_sec > SECS_PER_MINUTE ||
2710 31735 : fsec < 0 || fsec > USECS_PER_SEC)
2711 0 : return DTERR_FIELD_OVERFLOW;
2712 :
2713 31735 : itm->tm_usec = (int) fsec;
2714 :
2715 31735 : return 0;
2716 : }
2717 :
2718 : /* DecodeTime()
2719 : * Decode time string which includes delimiters.
2720 : * Return 0 if okay, a DTERR code if not.
2721 : *
2722 : * This version is used for timestamps. The results are returned into
2723 : * the tm_hour/tm_min/tm_sec fields of *tm, and microseconds into *fsec.
2724 : */
2725 : static int
2726 30618 : DecodeTime(char *str, int fmask, int range,
2727 : int *tmask, struct pg_tm *tm, fsec_t *fsec)
2728 : {
2729 : struct pg_itm itm;
2730 : int dterr;
2731 :
2732 30618 : dterr = DecodeTimeCommon(str, fmask, range,
2733 : tmask, &itm);
2734 30618 : if (dterr)
2735 0 : return dterr;
2736 :
2737 30618 : if (itm.tm_hour > INT_MAX)
2738 0 : return DTERR_FIELD_OVERFLOW;
2739 30618 : tm->tm_hour = (int) itm.tm_hour;
2740 30618 : tm->tm_min = itm.tm_min;
2741 30618 : tm->tm_sec = itm.tm_sec;
2742 30618 : *fsec = itm.tm_usec;
2743 :
2744 30618 : return 0;
2745 : }
2746 :
2747 : /* DecodeTimeForInterval()
2748 : * Decode time string which includes delimiters.
2749 : * Return 0 if okay, a DTERR code if not.
2750 : *
2751 : * This version is used for intervals. The results are returned into
2752 : * itm_in->tm_usec.
2753 : */
2754 : static int
2755 1125 : DecodeTimeForInterval(char *str, int fmask, int range,
2756 : int *tmask, struct pg_itm_in *itm_in)
2757 : {
2758 : struct pg_itm itm;
2759 : int dterr;
2760 :
2761 1125 : dterr = DecodeTimeCommon(str, fmask, range,
2762 : tmask, &itm);
2763 1125 : if (dterr)
2764 8 : return dterr;
2765 :
2766 1117 : itm_in->tm_usec = itm.tm_usec;
2767 1117 : if (!int64_multiply_add(itm.tm_hour, USECS_PER_HOUR, &itm_in->tm_usec) ||
2768 1117 : !int64_multiply_add(itm.tm_min, USECS_PER_MINUTE, &itm_in->tm_usec) ||
2769 1117 : !int64_multiply_add(itm.tm_sec, USECS_PER_SEC, &itm_in->tm_usec))
2770 4 : return DTERR_FIELD_OVERFLOW;
2771 :
2772 1113 : return 0;
2773 : }
2774 :
2775 :
2776 : /* DecodeNumber()
2777 : * Interpret plain numeric field as a date value in context.
2778 : * Return 0 if okay, a DTERR code if not.
2779 : */
2780 : static int
2781 121433 : DecodeNumber(int flen, char *str, bool haveTextMonth, int fmask,
2782 : int *tmask, struct pg_tm *tm, fsec_t *fsec, bool *is2digits)
2783 : {
2784 : int val;
2785 : char *cp;
2786 : int dterr;
2787 :
2788 121433 : *tmask = 0;
2789 :
2790 121433 : errno = 0;
2791 121433 : val = strtoint(str, &cp, 10);
2792 121433 : if (errno == ERANGE)
2793 0 : return DTERR_FIELD_OVERFLOW;
2794 121433 : if (cp == str)
2795 0 : return DTERR_BAD_FORMAT;
2796 :
2797 121433 : if (*cp == '.')
2798 : {
2799 : /*
2800 : * More than two digits before decimal point? Then could be a date or
2801 : * a run-together time: 2001.360 20011225 040506.789
2802 : */
2803 0 : if (cp - str > 2)
2804 : {
2805 0 : dterr = DecodeNumberField(flen, str,
2806 : (fmask | DTK_DATE_M),
2807 : tmask, tm,
2808 : fsec, is2digits);
2809 0 : if (dterr < 0)
2810 0 : return dterr;
2811 0 : return 0;
2812 : }
2813 :
2814 0 : dterr = ParseFractionalSecond(cp, fsec);
2815 0 : if (dterr)
2816 0 : return dterr;
2817 : }
2818 121433 : else if (*cp != '\0')
2819 0 : return DTERR_BAD_FORMAT;
2820 :
2821 : /* Special case for day of year */
2822 121433 : if (flen == 3 && (fmask & DTK_DATE_M) == DTK_M(YEAR) && val >= 1 &&
2823 : val <= 366)
2824 : {
2825 36 : *tmask = (DTK_M(DOY) | DTK_M(MONTH) | DTK_M(DAY));
2826 36 : tm->tm_yday = val;
2827 : /* tm_mon and tm_mday can't actually be set yet ... */
2828 36 : return 0;
2829 : }
2830 :
2831 : /* Switch based on what we have so far */
2832 121397 : switch (fmask & DTK_DATE_M)
2833 : {
2834 39516 : case 0:
2835 :
2836 : /*
2837 : * Nothing so far; make a decision about what we think the input
2838 : * is. There used to be lots of heuristics here, but the
2839 : * consensus now is to be paranoid. It *must* be either
2840 : * YYYY-MM-DD (with a more-than-two-digit year field), or the
2841 : * field order defined by DateOrder.
2842 : */
2843 39516 : if (flen >= 3 || DateOrder == DATEORDER_YMD)
2844 : {
2845 38336 : *tmask = DTK_M(YEAR);
2846 38336 : tm->tm_year = val;
2847 : }
2848 1180 : else if (DateOrder == DATEORDER_DMY)
2849 : {
2850 107 : *tmask = DTK_M(DAY);
2851 107 : tm->tm_mday = val;
2852 : }
2853 : else
2854 : {
2855 1073 : *tmask = DTK_M(MONTH);
2856 1073 : tm->tm_mon = val;
2857 : }
2858 39516 : break;
2859 :
2860 38264 : case (DTK_M(YEAR)):
2861 : /* Must be at second field of YY-MM-DD */
2862 38264 : *tmask = DTK_M(MONTH);
2863 38264 : tm->tm_mon = val;
2864 38264 : break;
2865 :
2866 2564 : case (DTK_M(MONTH)):
2867 2564 : if (haveTextMonth)
2868 : {
2869 : /*
2870 : * We are at the first numeric field of a date that included a
2871 : * textual month name. We want to support the variants
2872 : * MON-DD-YYYY, DD-MON-YYYY, and YYYY-MON-DD as unambiguous
2873 : * inputs. We will also accept MON-DD-YY or DD-MON-YY in
2874 : * either DMY or MDY modes, as well as YY-MON-DD in YMD mode.
2875 : */
2876 1522 : if (flen >= 3 || DateOrder == DATEORDER_YMD)
2877 : {
2878 48 : *tmask = DTK_M(YEAR);
2879 48 : tm->tm_year = val;
2880 : }
2881 : else
2882 : {
2883 1474 : *tmask = DTK_M(DAY);
2884 1474 : tm->tm_mday = val;
2885 : }
2886 : }
2887 : else
2888 : {
2889 : /* Must be at second field of MM-DD-YY */
2890 1042 : *tmask = DTK_M(DAY);
2891 1042 : tm->tm_mday = val;
2892 : }
2893 2564 : break;
2894 :
2895 38316 : case (DTK_M(YEAR) | DTK_M(MONTH)):
2896 38316 : if (haveTextMonth)
2897 : {
2898 : /* Need to accept DD-MON-YYYY even in YMD mode */
2899 84 : if (flen >= 3 && *is2digits)
2900 : {
2901 : /* Guess that first numeric field is day was wrong */
2902 20 : *tmask = DTK_M(DAY); /* YEAR is already set */
2903 20 : tm->tm_mday = tm->tm_year;
2904 20 : tm->tm_year = val;
2905 20 : *is2digits = false;
2906 : }
2907 : else
2908 : {
2909 64 : *tmask = DTK_M(DAY);
2910 64 : tm->tm_mday = val;
2911 : }
2912 : }
2913 : else
2914 : {
2915 : /* Must be at third field of YY-MM-DD */
2916 38232 : *tmask = DTK_M(DAY);
2917 38232 : tm->tm_mday = val;
2918 : }
2919 38316 : break;
2920 :
2921 95 : case (DTK_M(DAY)):
2922 : /* Must be at second field of DD-MM-YY */
2923 95 : *tmask = DTK_M(MONTH);
2924 95 : tm->tm_mon = val;
2925 95 : break;
2926 :
2927 2626 : case (DTK_M(MONTH) | DTK_M(DAY)):
2928 : /* Must be at third field of DD-MM-YY or MM-DD-YY */
2929 2626 : *tmask = DTK_M(YEAR);
2930 2626 : tm->tm_year = val;
2931 2626 : break;
2932 :
2933 16 : case (DTK_M(YEAR) | DTK_M(MONTH) | DTK_M(DAY)):
2934 : /* we have all the date, so it must be a time field */
2935 16 : dterr = DecodeNumberField(flen, str, fmask,
2936 : tmask, tm,
2937 : fsec, is2digits);
2938 16 : if (dterr < 0)
2939 8 : return dterr;
2940 8 : return 0;
2941 :
2942 0 : default:
2943 : /* Anything else is bogus input */
2944 0 : return DTERR_BAD_FORMAT;
2945 : }
2946 :
2947 : /*
2948 : * When processing a year field, mark it for adjustment if it's only one
2949 : * or two digits.
2950 : */
2951 121381 : if (*tmask == DTK_M(YEAR))
2952 41010 : *is2digits = (flen <= 2);
2953 :
2954 121381 : return 0;
2955 : }
2956 :
2957 :
2958 : /* DecodeNumberField()
2959 : * Interpret numeric string as a concatenated date or time field.
2960 : * Return a DTK token (>= 0) if successful, a DTERR code (< 0) if not.
2961 : *
2962 : * Use the context of previously decoded fields to help with
2963 : * the interpretation.
2964 : */
2965 : static int
2966 392 : DecodeNumberField(int len, char *str, int fmask,
2967 : int *tmask, struct pg_tm *tm, fsec_t *fsec, bool *is2digits)
2968 : {
2969 : char *cp;
2970 :
2971 : /*
2972 : * This function was originally meant to cope only with DTK_NUMBER fields,
2973 : * but we now sometimes abuse it to parse (parts of) DTK_DATE fields,
2974 : * which can contain letters and other punctuation. Reject if it's not a
2975 : * valid DTK_NUMBER, that is digits and decimal point(s). (ParseFraction
2976 : * will reject if there's more than one decimal point.)
2977 : */
2978 392 : if (strspn(str, "0123456789.") != len)
2979 8 : return DTERR_BAD_FORMAT;
2980 :
2981 : /*
2982 : * Have a decimal point? Then this is a date or something with a seconds
2983 : * field...
2984 : */
2985 384 : if ((cp = strchr(str, '.')) != NULL)
2986 : {
2987 : int dterr;
2988 :
2989 : /* Convert the fraction and store at *fsec */
2990 92 : dterr = ParseFractionalSecond(cp, fsec);
2991 92 : if (dterr)
2992 0 : return dterr;
2993 : /* Now truncate off the fraction for further processing */
2994 92 : *cp = '\0';
2995 92 : len = strlen(str);
2996 : }
2997 : /* No decimal point and no complete date yet? */
2998 292 : else if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2999 : {
3000 164 : if (len >= 6)
3001 : {
3002 164 : *tmask = DTK_DATE_M;
3003 :
3004 : /*
3005 : * Start from end and consider first 2 as Day, next 2 as Month,
3006 : * and the rest as Year.
3007 : */
3008 164 : tm->tm_mday = atoi(str + (len - 2));
3009 164 : *(str + (len - 2)) = '\0';
3010 164 : tm->tm_mon = atoi(str + (len - 4));
3011 164 : *(str + (len - 4)) = '\0';
3012 164 : tm->tm_year = atoi(str);
3013 164 : if ((len - 4) == 2)
3014 12 : *is2digits = true;
3015 :
3016 164 : return DTK_DATE;
3017 : }
3018 : }
3019 :
3020 : /* not all time fields are specified? */
3021 220 : if ((fmask & DTK_TIME_M) != DTK_TIME_M)
3022 : {
3023 : /* hhmmss */
3024 220 : if (len == 6)
3025 : {
3026 188 : *tmask = DTK_TIME_M;
3027 188 : tm->tm_sec = atoi(str + 4);
3028 188 : *(str + 4) = '\0';
3029 188 : tm->tm_min = atoi(str + 2);
3030 188 : *(str + 2) = '\0';
3031 188 : tm->tm_hour = atoi(str);
3032 :
3033 188 : return DTK_TIME;
3034 : }
3035 : /* hhmm? */
3036 32 : else if (len == 4)
3037 : {
3038 16 : *tmask = DTK_TIME_M;
3039 16 : tm->tm_sec = 0;
3040 16 : tm->tm_min = atoi(str + 2);
3041 16 : *(str + 2) = '\0';
3042 16 : tm->tm_hour = atoi(str);
3043 :
3044 16 : return DTK_TIME;
3045 : }
3046 : }
3047 :
3048 16 : return DTERR_BAD_FORMAT;
3049 : }
3050 :
3051 :
3052 : /* DecodeTimezone()
3053 : * Interpret string as a numeric timezone.
3054 : *
3055 : * Return 0 if okay (and set *tzp), a DTERR code if not okay.
3056 : */
3057 : int
3058 19170 : DecodeTimezone(const char *str, int *tzp)
3059 : {
3060 : int tz;
3061 : int hr,
3062 : min,
3063 19170 : sec = 0;
3064 : char *cp;
3065 :
3066 : /* leading character must be "+" or "-" */
3067 19170 : if (*str != '+' && *str != '-')
3068 48 : return DTERR_BAD_FORMAT;
3069 :
3070 19122 : errno = 0;
3071 19122 : hr = strtoint(str + 1, &cp, 10);
3072 19122 : if (errno == ERANGE)
3073 0 : return DTERR_TZDISP_OVERFLOW;
3074 :
3075 : /* explicit delimiter? */
3076 19122 : if (*cp == ':')
3077 : {
3078 72 : errno = 0;
3079 72 : min = strtoint(cp + 1, &cp, 10);
3080 72 : if (errno == ERANGE)
3081 0 : return DTERR_TZDISP_OVERFLOW;
3082 72 : if (*cp == ':')
3083 : {
3084 16 : errno = 0;
3085 16 : sec = strtoint(cp + 1, &cp, 10);
3086 16 : if (errno == ERANGE)
3087 0 : return DTERR_TZDISP_OVERFLOW;
3088 : }
3089 : }
3090 : /* otherwise, might have run things together... */
3091 19050 : else if (*cp == '\0' && strlen(str) > 3)
3092 : {
3093 48 : min = hr % 100;
3094 48 : hr = hr / 100;
3095 : /* we could, but don't, support a run-together hhmmss format */
3096 : }
3097 : else
3098 19002 : min = 0;
3099 :
3100 : /* Range-check the values; see notes in datatype/timestamp.h */
3101 19122 : if (hr < 0 || hr > MAX_TZDISP_HOUR)
3102 8 : return DTERR_TZDISP_OVERFLOW;
3103 19114 : if (min < 0 || min >= MINS_PER_HOUR)
3104 8 : return DTERR_TZDISP_OVERFLOW;
3105 19106 : if (sec < 0 || sec >= SECS_PER_MINUTE)
3106 0 : return DTERR_TZDISP_OVERFLOW;
3107 :
3108 19106 : tz = (hr * MINS_PER_HOUR + min) * SECS_PER_MINUTE + sec;
3109 19106 : if (*str == '-')
3110 555 : tz = -tz;
3111 :
3112 19106 : *tzp = -tz;
3113 :
3114 19106 : if (*cp != '\0')
3115 0 : return DTERR_BAD_FORMAT;
3116 :
3117 19106 : return 0;
3118 : }
3119 :
3120 :
3121 : /* DecodeTimezoneAbbrev()
3122 : * Interpret string as a timezone abbreviation, if possible.
3123 : *
3124 : * Sets *ftype to an abbreviation type (TZ, DTZ, or DYNTZ), or UNKNOWN_FIELD if
3125 : * string is not any known abbreviation. On success, set *offset and *tz to
3126 : * represent the UTC offset (for TZ or DTZ) or underlying zone (for DYNTZ).
3127 : * Note that full timezone names (such as America/New_York) are not handled
3128 : * here, mostly for historical reasons.
3129 : *
3130 : * The function result is 0 or a DTERR code; in the latter case, *extra
3131 : * is filled as needed. Note that unknown-abbreviation is not considered
3132 : * an error case. Also note that many callers assume that the DTERR code
3133 : * is one that DateTimeParseError does not require "str" or "datatype"
3134 : * strings for.
3135 : *
3136 : * Given string must be lowercased already.
3137 : *
3138 : * Implement a cache lookup since it is likely that dates
3139 : * will be related in format.
3140 : */
3141 : int
3142 6496 : DecodeTimezoneAbbrev(int field, const char *lowtoken,
3143 : int *ftype, int *offset, pg_tz **tz,
3144 : DateTimeErrorExtra *extra)
3145 : {
3146 6496 : TzAbbrevCache *tzc = &tzabbrevcache[field];
3147 : bool isfixed;
3148 : int isdst;
3149 : const datetkn *tp;
3150 :
3151 : /*
3152 : * Do we have a cached result? Use strncmp so that we match truncated
3153 : * names, although we shouldn't really see that happen with normal
3154 : * abbreviations.
3155 : */
3156 6496 : if (strncmp(lowtoken, tzc->abbrev, TOKMAXLEN) == 0)
3157 : {
3158 1601 : *ftype = tzc->ftype;
3159 1601 : *offset = tzc->offset;
3160 1601 : *tz = tzc->tz;
3161 1601 : return 0;
3162 : }
3163 :
3164 : /*
3165 : * See if the current session_timezone recognizes it. Checking this
3166 : * before zoneabbrevtbl allows us to correctly handle abbreviations whose
3167 : * meaning varies across zones, such as "LMT".
3168 : */
3169 9790 : if (session_timezone &&
3170 4895 : TimeZoneAbbrevIsKnown(lowtoken, session_timezone,
3171 : &isfixed, offset, &isdst))
3172 : {
3173 149 : *ftype = (isfixed ? (isdst ? DTZ : TZ) : DYNTZ);
3174 149 : *tz = (isfixed ? NULL : session_timezone);
3175 : /* flip sign to agree with the convention used in zoneabbrevtbl */
3176 149 : *offset = -(*offset);
3177 : /* cache result; use strlcpy to truncate name if necessary */
3178 149 : strlcpy(tzc->abbrev, lowtoken, TOKMAXLEN + 1);
3179 149 : tzc->ftype = *ftype;
3180 149 : tzc->offset = *offset;
3181 149 : tzc->tz = *tz;
3182 149 : return 0;
3183 : }
3184 :
3185 : /* Nope, so look in zoneabbrevtbl */
3186 4746 : if (zoneabbrevtbl)
3187 4746 : tp = datebsearch(lowtoken, zoneabbrevtbl->abbrevs,
3188 4746 : zoneabbrevtbl->numabbrevs);
3189 : else
3190 0 : tp = NULL;
3191 4746 : if (tp == NULL)
3192 : {
3193 4591 : *ftype = UNKNOWN_FIELD;
3194 4591 : *offset = 0;
3195 4591 : *tz = NULL;
3196 : /* failure results are not cached */
3197 : }
3198 : else
3199 : {
3200 155 : *ftype = tp->type;
3201 155 : if (tp->type == DYNTZ)
3202 : {
3203 20 : *offset = 0;
3204 20 : *tz = FetchDynamicTimeZone(zoneabbrevtbl, tp, extra);
3205 20 : if (*tz == NULL)
3206 0 : return DTERR_BAD_ZONE_ABBREV;
3207 : }
3208 : else
3209 : {
3210 135 : *offset = tp->value;
3211 135 : *tz = NULL;
3212 : }
3213 :
3214 : /* cache result; use strlcpy to truncate name if necessary */
3215 155 : strlcpy(tzc->abbrev, lowtoken, TOKMAXLEN + 1);
3216 155 : tzc->ftype = *ftype;
3217 155 : tzc->offset = *offset;
3218 155 : tzc->tz = *tz;
3219 : }
3220 :
3221 4746 : return 0;
3222 : }
3223 :
3224 : /*
3225 : * Reset tzabbrevcache after a change in session_timezone.
3226 : */
3227 : void
3228 10360 : ClearTimeZoneAbbrevCache(void)
3229 : {
3230 10360 : memset(tzabbrevcache, 0, sizeof(tzabbrevcache));
3231 10360 : }
3232 :
3233 :
3234 : /* DecodeSpecial()
3235 : * Decode text string using lookup table.
3236 : *
3237 : * Recognizes the keywords listed in datetktbl.
3238 : * Note: at one time this would also recognize timezone abbreviations,
3239 : * but no more; use DecodeTimezoneAbbrev for that.
3240 : *
3241 : * Given string must be lowercased already.
3242 : *
3243 : * Implement a cache lookup since it is likely that dates
3244 : * will be related in format.
3245 : */
3246 : int
3247 23397 : DecodeSpecial(int field, const char *lowtoken, int *val)
3248 : {
3249 : int type;
3250 : const datetkn *tp;
3251 :
3252 23397 : tp = datecache[field];
3253 : /* use strncmp so that we match truncated tokens */
3254 23397 : if (tp == NULL || strncmp(lowtoken, tp->token, TOKMAXLEN) != 0)
3255 : {
3256 6558 : tp = datebsearch(lowtoken, datetktbl, szdatetktbl);
3257 : }
3258 23397 : if (tp == NULL)
3259 : {
3260 76 : type = UNKNOWN_FIELD;
3261 76 : *val = 0;
3262 : }
3263 : else
3264 : {
3265 23321 : datecache[field] = tp;
3266 23321 : type = tp->type;
3267 23321 : *val = tp->value;
3268 : }
3269 :
3270 23397 : return type;
3271 : }
3272 :
3273 :
3274 : /* DecodeTimezoneName()
3275 : * Interpret string as a timezone abbreviation or name.
3276 : * Throw error if the name is not recognized.
3277 : *
3278 : * The return value indicates what kind of zone identifier it is:
3279 : * TZNAME_FIXED_OFFSET: fixed offset from UTC
3280 : * TZNAME_DYNTZ: dynamic timezone abbreviation
3281 : * TZNAME_ZONE: full tzdb zone name
3282 : *
3283 : * For TZNAME_FIXED_OFFSET, *offset receives the UTC offset (in seconds,
3284 : * with ISO sign convention: positive is east of Greenwich).
3285 : * For the other two cases, *tz receives the timezone struct representing
3286 : * the zone name or the abbreviation's underlying zone.
3287 : */
3288 : int
3289 617 : DecodeTimezoneName(const char *tzname, int *offset, pg_tz **tz)
3290 : {
3291 : char *lowzone;
3292 : int dterr,
3293 : type;
3294 : DateTimeErrorExtra extra;
3295 :
3296 : /*
3297 : * First we look in the timezone abbreviation table (to handle cases like
3298 : * "EST"), and if that fails, we look in the timezone database (to handle
3299 : * cases like "America/New_York"). This matches the order in which
3300 : * timestamp input checks the cases; it's important because the timezone
3301 : * database unwisely uses a few zone names that are identical to offset
3302 : * abbreviations.
3303 : */
3304 :
3305 : /* DecodeTimezoneAbbrev requires lowercase input */
3306 617 : lowzone = downcase_truncate_identifier(tzname,
3307 617 : strlen(tzname),
3308 : false);
3309 :
3310 617 : dterr = DecodeTimezoneAbbrev(0, lowzone, &type, offset, tz, &extra);
3311 617 : if (dterr)
3312 0 : DateTimeParseError(dterr, &extra, NULL, NULL, NULL);
3313 :
3314 617 : if (type == TZ || type == DTZ)
3315 : {
3316 : /* fixed-offset abbreviation, return the offset */
3317 207 : return TZNAME_FIXED_OFFSET;
3318 : }
3319 410 : else if (type == DYNTZ)
3320 : {
3321 : /* dynamic-offset abbreviation, return its referenced timezone */
3322 143 : return TZNAME_DYNTZ;
3323 : }
3324 : else
3325 : {
3326 : /* try it as a full zone name */
3327 267 : *tz = pg_tzset(tzname);
3328 267 : if (*tz == NULL)
3329 8 : ereport(ERROR,
3330 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3331 : errmsg("time zone \"%s\" not recognized", tzname)));
3332 259 : return TZNAME_ZONE;
3333 : }
3334 : }
3335 :
3336 : /* DecodeTimezoneNameToTz()
3337 : * Interpret string as a timezone abbreviation or name.
3338 : * Throw error if the name is not recognized.
3339 : *
3340 : * This is a simple wrapper for DecodeTimezoneName that produces a pg_tz *
3341 : * result in all cases.
3342 : */
3343 : pg_tz *
3344 70 : DecodeTimezoneNameToTz(const char *tzname)
3345 : {
3346 : pg_tz *result;
3347 : int offset;
3348 :
3349 70 : if (DecodeTimezoneName(tzname, &offset, &result) == TZNAME_FIXED_OFFSET)
3350 : {
3351 : /* fixed-offset abbreviation, get a pg_tz descriptor for that */
3352 22 : result = pg_tzset_offset(-offset); /* flip to POSIX sign convention */
3353 : }
3354 70 : return result;
3355 : }
3356 :
3357 : /* DecodeTimezoneAbbrevPrefix()
3358 : * Interpret prefix of string as a timezone abbreviation, if possible.
3359 : *
3360 : * This has roughly the same functionality as DecodeTimezoneAbbrev(),
3361 : * but the API is adapted to the needs of formatting.c. Notably,
3362 : * we will match the longest possible prefix of the given string
3363 : * rather than insisting on a complete match, and downcasing is applied
3364 : * here rather than in the caller.
3365 : *
3366 : * Returns the length of the timezone abbreviation, or -1 if not recognized.
3367 : * On success, sets *offset to the GMT offset for the abbreviation if it
3368 : * is a fixed-offset abbreviation, or sets *tz to the pg_tz struct for
3369 : * a dynamic abbreviation.
3370 : */
3371 : int
3372 2374 : DecodeTimezoneAbbrevPrefix(const char *str, int *offset, pg_tz **tz)
3373 : {
3374 : char lowtoken[TOKMAXLEN + 1];
3375 : int len;
3376 :
3377 2374 : *offset = 0; /* avoid uninitialized vars on failure */
3378 2374 : *tz = NULL;
3379 :
3380 : /* Downcase as much of the string as we could need */
3381 2466 : for (len = 0; len < TOKMAXLEN; len++)
3382 : {
3383 2466 : if (*str == '\0' || !isalpha((unsigned char) *str))
3384 : break;
3385 92 : lowtoken[len] = pg_tolower((unsigned char) *str++);
3386 : }
3387 2374 : lowtoken[len] = '\0';
3388 :
3389 : /*
3390 : * We could avoid doing repeated binary searches if we cared to duplicate
3391 : * datebsearch here, but it's not clear that such an optimization would be
3392 : * worth the trouble. In common cases there's probably not anything after
3393 : * the zone abbrev anyway. So just search with successively truncated
3394 : * strings.
3395 : */
3396 2402 : while (len > 0)
3397 : {
3398 : bool isfixed;
3399 : int isdst;
3400 : const datetkn *tp;
3401 :
3402 : /* See if the current session_timezone recognizes it. */
3403 104 : if (session_timezone &&
3404 52 : TimeZoneAbbrevIsKnown(lowtoken, session_timezone,
3405 : &isfixed, offset, &isdst))
3406 : {
3407 4 : if (isfixed)
3408 : {
3409 : /* flip sign to agree with the convention in zoneabbrevtbl */
3410 4 : *offset = -(*offset);
3411 : }
3412 : else
3413 : {
3414 : /* Caller must resolve the abbrev's current meaning */
3415 0 : *tz = session_timezone;
3416 : }
3417 24 : return len;
3418 : }
3419 :
3420 : /* Known in zoneabbrevtbl? */
3421 48 : if (zoneabbrevtbl)
3422 48 : tp = datebsearch(lowtoken, zoneabbrevtbl->abbrevs,
3423 48 : zoneabbrevtbl->numabbrevs);
3424 : else
3425 0 : tp = NULL;
3426 48 : if (tp != NULL)
3427 : {
3428 20 : if (tp->type == DYNTZ)
3429 : {
3430 : DateTimeErrorExtra extra;
3431 4 : pg_tz *tzp = FetchDynamicTimeZone(zoneabbrevtbl, tp,
3432 : &extra);
3433 :
3434 4 : if (tzp != NULL)
3435 : {
3436 : /* Caller must resolve the abbrev's current meaning */
3437 4 : *tz = tzp;
3438 4 : return len;
3439 : }
3440 : }
3441 : else
3442 : {
3443 : /* Fixed-offset zone abbrev, so it's easy */
3444 16 : *offset = tp->value;
3445 16 : return len;
3446 : }
3447 : }
3448 :
3449 : /* Nope, try the next shorter string. */
3450 28 : lowtoken[--len] = '\0';
3451 : }
3452 :
3453 : /* Did not find a match */
3454 2350 : return -1;
3455 : }
3456 :
3457 :
3458 : /* ClearPgItmIn
3459 : *
3460 : * Zero out a pg_itm_in
3461 : */
3462 : static inline void
3463 43059 : ClearPgItmIn(struct pg_itm_in *itm_in)
3464 : {
3465 43059 : itm_in->tm_usec = 0;
3466 43059 : itm_in->tm_mday = 0;
3467 43059 : itm_in->tm_mon = 0;
3468 43059 : itm_in->tm_year = 0;
3469 43059 : }
3470 :
3471 :
3472 : /* DecodeInterval()
3473 : * Interpret previously parsed fields for general time interval.
3474 : * Returns 0 if successful, DTERR code if bogus input detected.
3475 : * dtype and itm_in are output parameters.
3476 : *
3477 : * Allow "date" field DTK_DATE since this could be just
3478 : * an unsigned floating point number. - thomas 1997-11-16
3479 : *
3480 : * Allow ISO-style time span, with implicit units on number of days
3481 : * preceding an hh:mm:ss field. - thomas 1998-04-30
3482 : *
3483 : * itm_in remains undefined for infinite interval values for which dtype alone
3484 : * suffices.
3485 : */
3486 : int
3487 42651 : DecodeInterval(char **field, int *ftype, int nf, int range,
3488 : int *dtype, struct pg_itm_in *itm_in)
3489 : {
3490 42651 : bool force_negative = false;
3491 42651 : bool is_before = false;
3492 42651 : bool parsing_unit_val = false;
3493 : char *cp;
3494 42651 : int fmask = 0,
3495 : tmask,
3496 : type,
3497 : uval;
3498 : int i;
3499 : int dterr;
3500 : int64 val;
3501 : double fval;
3502 :
3503 42651 : *dtype = DTK_DELTA;
3504 42651 : type = IGNORE_DTF;
3505 42651 : ClearPgItmIn(itm_in);
3506 :
3507 : /*----------
3508 : * The SQL standard defines the interval literal
3509 : * '-1 1:00:00'
3510 : * to mean "negative 1 days and negative 1 hours", while Postgres
3511 : * traditionally treats this as meaning "negative 1 days and positive
3512 : * 1 hours". In SQL_STANDARD intervalstyle, we apply the leading sign
3513 : * to all fields if there are no other explicit signs.
3514 : *
3515 : * We leave the signs alone if there are additional explicit signs.
3516 : * This protects us against misinterpreting postgres-style dump output,
3517 : * since the postgres-style output code has always put an explicit sign on
3518 : * all fields following a negative field. But note that SQL-spec output
3519 : * is ambiguous and can be misinterpreted on load! (So it's best practice
3520 : * to dump in postgres style, not SQL style.)
3521 : *----------
3522 : */
3523 42651 : if (IntervalStyle == INTSTYLE_SQL_STANDARD && nf > 0 && *field[0] == '-')
3524 : {
3525 25 : force_negative = true;
3526 : /* Check for additional explicit signs */
3527 162 : for (i = 1; i < nf; i++)
3528 : {
3529 149 : if (*field[i] == '-' || *field[i] == '+')
3530 : {
3531 12 : force_negative = false;
3532 12 : break;
3533 : }
3534 : }
3535 : }
3536 :
3537 : /* read through list backwards to pick up units before values */
3538 128825 : for (i = nf - 1; i >= 0; i--)
3539 : {
3540 86902 : switch (ftype[i])
3541 : {
3542 720 : case DTK_TIME:
3543 720 : dterr = DecodeTimeForInterval(field[i], fmask, range,
3544 : &tmask, itm_in);
3545 720 : if (dterr)
3546 12 : return dterr;
3547 708 : if (force_negative &&
3548 1 : itm_in->tm_usec > 0)
3549 1 : itm_in->tm_usec = -itm_in->tm_usec;
3550 708 : type = DTK_DAY;
3551 708 : parsing_unit_val = false;
3552 708 : break;
3553 :
3554 1828 : case DTK_TZ:
3555 :
3556 : /*
3557 : * Timezone means a token with a leading sign character and at
3558 : * least one digit; there could be ':', '.', '-' embedded in
3559 : * it as well.
3560 : */
3561 : Assert(*field[i] == '-' || *field[i] == '+');
3562 :
3563 : /*
3564 : * Check for signed hh:mm or hh:mm:ss. If so, process exactly
3565 : * like DTK_TIME case above, plus handling the sign.
3566 : */
3567 2233 : if (strchr(field[i] + 1, ':') != NULL &&
3568 405 : DecodeTimeForInterval(field[i] + 1, fmask, range,
3569 : &tmask, itm_in) == 0)
3570 : {
3571 405 : if (*field[i] == '-')
3572 : {
3573 : /* flip the sign on time field */
3574 365 : if (itm_in->tm_usec == PG_INT64_MIN)
3575 0 : return DTERR_FIELD_OVERFLOW;
3576 365 : itm_in->tm_usec = -itm_in->tm_usec;
3577 : }
3578 :
3579 405 : if (force_negative &&
3580 0 : itm_in->tm_usec > 0)
3581 0 : itm_in->tm_usec = -itm_in->tm_usec;
3582 :
3583 : /*
3584 : * Set the next type to be a day, if units are not
3585 : * specified. This handles the case of '1 +02:03' since we
3586 : * are reading right to left.
3587 : */
3588 405 : type = DTK_DAY;
3589 405 : parsing_unit_val = false;
3590 405 : break;
3591 : }
3592 :
3593 : /*
3594 : * Otherwise, fall through to DTK_NUMBER case, which can
3595 : * handle signed float numbers and signed year-month values.
3596 : */
3597 :
3598 : pg_fallthrough;
3599 :
3600 : case DTK_DATE:
3601 : case DTK_NUMBER:
3602 42793 : if (type == IGNORE_DTF)
3603 : {
3604 : /* use typmod to decide what rightmost field is */
3605 400 : switch (range)
3606 : {
3607 4 : case INTERVAL_MASK(YEAR):
3608 4 : type = DTK_YEAR;
3609 4 : break;
3610 20 : case INTERVAL_MASK(MONTH):
3611 : case INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH):
3612 20 : type = DTK_MONTH;
3613 20 : break;
3614 12 : case INTERVAL_MASK(DAY):
3615 12 : type = DTK_DAY;
3616 12 : break;
3617 16 : case INTERVAL_MASK(HOUR):
3618 : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR):
3619 16 : type = DTK_HOUR;
3620 16 : break;
3621 16 : case INTERVAL_MASK(MINUTE):
3622 : case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
3623 : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
3624 16 : type = DTK_MINUTE;
3625 16 : break;
3626 40 : case INTERVAL_MASK(SECOND):
3627 : case INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
3628 : case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
3629 : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
3630 40 : type = DTK_SECOND;
3631 40 : break;
3632 292 : default:
3633 292 : type = DTK_SECOND;
3634 292 : break;
3635 : }
3636 : }
3637 :
3638 42793 : errno = 0;
3639 42793 : val = strtoi64(field[i], &cp, 10);
3640 42793 : if (errno == ERANGE)
3641 8 : return DTERR_FIELD_OVERFLOW;
3642 :
3643 42785 : if (*cp == '-')
3644 : {
3645 : /* SQL "years-months" syntax */
3646 : int val2;
3647 :
3648 40 : val2 = strtoint(cp + 1, &cp, 10);
3649 40 : if (errno == ERANGE || val2 < 0 || val2 >= MONTHS_PER_YEAR)
3650 0 : return DTERR_FIELD_OVERFLOW;
3651 40 : if (*cp != '\0')
3652 0 : return DTERR_BAD_FORMAT;
3653 40 : type = DTK_MONTH;
3654 40 : if (*field[i] == '-')
3655 4 : val2 = -val2;
3656 40 : if (pg_mul_s64_overflow(val, MONTHS_PER_YEAR, &val))
3657 0 : return DTERR_FIELD_OVERFLOW;
3658 40 : if (pg_add_s64_overflow(val, val2, &val))
3659 0 : return DTERR_FIELD_OVERFLOW;
3660 40 : fval = 0;
3661 : }
3662 42745 : else if (*cp == '.')
3663 : {
3664 324 : dterr = ParseFraction(cp, &fval);
3665 324 : if (dterr)
3666 0 : return dterr;
3667 324 : if (*field[i] == '-')
3668 92 : fval = -fval;
3669 : }
3670 42421 : else if (*cp == '\0')
3671 42165 : fval = 0;
3672 : else
3673 256 : return DTERR_BAD_FORMAT;
3674 :
3675 42529 : tmask = 0; /* DTK_M(type); */
3676 :
3677 42529 : if (force_negative)
3678 : {
3679 : /* val and fval should be of same sign, but test anyway */
3680 53 : if (val > 0)
3681 40 : val = -val;
3682 53 : if (fval > 0)
3683 12 : fval = -fval;
3684 : }
3685 :
3686 42529 : switch (type)
3687 : {
3688 220 : case DTK_MICROSEC:
3689 220 : if (!AdjustMicroseconds(val, fval, 1, itm_in))
3690 24 : return DTERR_FIELD_OVERFLOW;
3691 196 : tmask = DTK_M(MICROSECOND);
3692 196 : break;
3693 :
3694 71 : case DTK_MILLISEC:
3695 71 : if (!AdjustMicroseconds(val, fval, 1000, itm_in))
3696 8 : return DTERR_FIELD_OVERFLOW;
3697 63 : tmask = DTK_M(MILLISECOND);
3698 63 : break;
3699 :
3700 608 : case DTK_SECOND:
3701 608 : if (!AdjustMicroseconds(val, fval, USECS_PER_SEC, itm_in))
3702 8 : return DTERR_FIELD_OVERFLOW;
3703 :
3704 : /*
3705 : * If any subseconds were specified, consider this
3706 : * microsecond and millisecond input as well.
3707 : */
3708 600 : if (fval == 0)
3709 492 : tmask = DTK_M(SECOND);
3710 : else
3711 108 : tmask = DTK_ALL_SECS_M;
3712 600 : break;
3713 :
3714 245 : case DTK_MINUTE:
3715 245 : if (!AdjustMicroseconds(val, fval, USECS_PER_MINUTE, itm_in))
3716 8 : return DTERR_FIELD_OVERFLOW;
3717 237 : tmask = DTK_M(MINUTE);
3718 237 : break;
3719 :
3720 439 : case DTK_HOUR:
3721 439 : if (!AdjustMicroseconds(val, fval, USECS_PER_HOUR, itm_in))
3722 8 : return DTERR_FIELD_OVERFLOW;
3723 431 : tmask = DTK_M(HOUR);
3724 431 : type = DTK_DAY; /* set for next field */
3725 431 : break;
3726 :
3727 3822 : case DTK_DAY:
3728 3822 : if (!AdjustDays(val, 1, itm_in) ||
3729 3774 : !AdjustFractMicroseconds(fval, USECS_PER_DAY, itm_in))
3730 60 : return DTERR_FIELD_OVERFLOW;
3731 3762 : tmask = DTK_M(DAY);
3732 3762 : break;
3733 :
3734 64 : case DTK_WEEK:
3735 64 : if (!AdjustDays(val, 7, itm_in) ||
3736 48 : !AdjustFractDays(fval, 7, itm_in))
3737 32 : return DTERR_FIELD_OVERFLOW;
3738 32 : tmask = DTK_M(WEEK);
3739 32 : break;
3740 :
3741 600 : case DTK_MONTH:
3742 600 : if (!AdjustMonths(val, itm_in) ||
3743 560 : !AdjustFractDays(fval, DAYS_PER_MONTH, itm_in))
3744 56 : return DTERR_FIELD_OVERFLOW;
3745 544 : tmask = DTK_M(MONTH);
3746 544 : break;
3747 :
3748 36328 : case DTK_YEAR:
3749 36328 : if (!AdjustYears(val, 1, itm_in) ||
3750 36296 : !AdjustFractYears(fval, 1, itm_in))
3751 40 : return DTERR_FIELD_OVERFLOW;
3752 36288 : tmask = DTK_M(YEAR);
3753 36288 : break;
3754 :
3755 44 : case DTK_DECADE:
3756 44 : if (!AdjustYears(val, 10, itm_in) ||
3757 28 : !AdjustFractYears(fval, 10, itm_in))
3758 24 : return DTERR_FIELD_OVERFLOW;
3759 20 : tmask = DTK_M(DECADE);
3760 20 : break;
3761 :
3762 44 : case DTK_CENTURY:
3763 44 : if (!AdjustYears(val, 100, itm_in) ||
3764 28 : !AdjustFractYears(fval, 100, itm_in))
3765 24 : return DTERR_FIELD_OVERFLOW;
3766 20 : tmask = DTK_M(CENTURY);
3767 20 : break;
3768 :
3769 44 : case DTK_MILLENNIUM:
3770 44 : if (!AdjustYears(val, 1000, itm_in) ||
3771 28 : !AdjustFractYears(fval, 1000, itm_in))
3772 24 : return DTERR_FIELD_OVERFLOW;
3773 20 : tmask = DTK_M(MILLENNIUM);
3774 20 : break;
3775 :
3776 0 : default:
3777 0 : return DTERR_BAD_FORMAT;
3778 : }
3779 42213 : parsing_unit_val = false;
3780 42213 : break;
3781 :
3782 42984 : case DTK_STRING:
3783 : case DTK_SPECIAL:
3784 : /* reject consecutive unhandled units */
3785 42984 : if (parsing_unit_val)
3786 8 : return DTERR_BAD_FORMAT;
3787 42976 : type = DecodeUnits(i, field[i], &uval);
3788 42976 : if (type == UNKNOWN_FIELD)
3789 708 : type = DecodeSpecial(i, field[i], &uval);
3790 42976 : if (type == IGNORE_DTF)
3791 0 : continue;
3792 :
3793 42976 : tmask = 0; /* DTK_M(type); */
3794 42976 : switch (type)
3795 : {
3796 42200 : case UNITS:
3797 42200 : type = uval;
3798 42200 : parsing_unit_val = true;
3799 42200 : break;
3800 :
3801 68 : case AGO:
3802 :
3803 : /*
3804 : * "ago" is only allowed to appear at the end of the
3805 : * interval.
3806 : */
3807 68 : if (i != nf - 1)
3808 8 : return DTERR_BAD_FORMAT;
3809 60 : is_before = true;
3810 60 : type = uval;
3811 60 : break;
3812 :
3813 684 : case RESERV:
3814 684 : tmask = (DTK_DATE_M | DTK_TIME_M);
3815 :
3816 : /*
3817 : * Only reserved words corresponding to infinite
3818 : * intervals are accepted.
3819 : */
3820 684 : if (uval != DTK_LATE && uval != DTK_EARLY)
3821 24 : return DTERR_BAD_FORMAT;
3822 :
3823 : /*
3824 : * Infinity cannot be followed by anything else. We
3825 : * could allow "ago" to reverse the sign of infinity
3826 : * but using signed infinity is more intuitive.
3827 : */
3828 660 : if (i != nf - 1)
3829 8 : return DTERR_BAD_FORMAT;
3830 :
3831 652 : *dtype = uval;
3832 652 : break;
3833 :
3834 24 : default:
3835 24 : return DTERR_BAD_FORMAT;
3836 : }
3837 42912 : break;
3838 :
3839 0 : default:
3840 0 : return DTERR_BAD_FORMAT;
3841 : }
3842 :
3843 86238 : if (tmask & fmask)
3844 64 : return DTERR_BAD_FORMAT;
3845 86174 : fmask |= tmask;
3846 : }
3847 :
3848 : /* ensure that at least one time field has been found */
3849 41923 : if (fmask == 0)
3850 4 : return DTERR_BAD_FORMAT;
3851 :
3852 : /* reject if unit appeared and was never handled */
3853 41919 : if (parsing_unit_val)
3854 4 : return DTERR_BAD_FORMAT;
3855 :
3856 : /* finally, AGO negates everything */
3857 41915 : if (is_before)
3858 : {
3859 28 : if (itm_in->tm_usec == PG_INT64_MIN ||
3860 20 : itm_in->tm_mday == INT_MIN ||
3861 16 : itm_in->tm_mon == INT_MIN ||
3862 12 : itm_in->tm_year == INT_MIN)
3863 16 : return DTERR_FIELD_OVERFLOW;
3864 :
3865 12 : itm_in->tm_usec = -itm_in->tm_usec;
3866 12 : itm_in->tm_mday = -itm_in->tm_mday;
3867 12 : itm_in->tm_mon = -itm_in->tm_mon;
3868 12 : itm_in->tm_year = -itm_in->tm_year;
3869 : }
3870 :
3871 41899 : return 0;
3872 : }
3873 :
3874 :
3875 : /*
3876 : * Helper functions to avoid duplicated code in DecodeISO8601Interval.
3877 : *
3878 : * Parse a decimal value and break it into integer and fractional parts.
3879 : * Set *endptr to end+1 of the parsed substring.
3880 : * Returns 0 or DTERR code.
3881 : */
3882 : static int
3883 636 : ParseISO8601Number(char *str, char **endptr, int64 *ipart, double *fpart)
3884 : {
3885 : double val;
3886 :
3887 : /*
3888 : * Historically this has accepted anything that strtod() would take,
3889 : * notably including "e" notation, so continue doing that. This is
3890 : * slightly annoying because the precision of double is less than that of
3891 : * int64, so we would lose accuracy for inputs larger than 2^53 or so.
3892 : * However, historically we rejected inputs outside the int32 range,
3893 : * making that concern moot. What we do now is reject abs(val) above
3894 : * 1.0e15 (a round number a bit less than 2^50), so that any accepted
3895 : * value will have an exact integer part, and thereby a fraction part with
3896 : * abs(*fpart) less than 1. In the absence of field complaints it doesn't
3897 : * seem worth working harder.
3898 : */
3899 636 : if (!(isdigit((unsigned char) *str) || *str == '-' || *str == '.'))
3900 0 : return DTERR_BAD_FORMAT;
3901 636 : errno = 0;
3902 636 : val = strtod(str, endptr);
3903 : /* did we not see anything that looks like a double? */
3904 636 : if (*endptr == str || errno != 0)
3905 4 : return DTERR_BAD_FORMAT;
3906 : /* watch out for overflow, including infinities; reject NaN too */
3907 632 : if (isnan(val) || val < -1.0e15 || val > 1.0e15)
3908 0 : return DTERR_FIELD_OVERFLOW;
3909 : /* be very sure we truncate towards zero (cf dtrunc()) */
3910 632 : if (val >= 0)
3911 488 : *ipart = (int64) floor(val);
3912 : else
3913 144 : *ipart = (int64) -floor(-val);
3914 632 : *fpart = val - *ipart;
3915 : /* Callers expect this to hold */
3916 : Assert(*fpart > -1.0 && *fpart < 1.0);
3917 632 : return 0;
3918 : }
3919 :
3920 : /*
3921 : * Determine number of integral digits in a valid ISO 8601 number field
3922 : * (we should ignore sign and any fraction part)
3923 : */
3924 : static int
3925 44 : ISO8601IntegerWidth(char *fieldstart)
3926 : {
3927 : /* We might have had a leading '-' */
3928 44 : if (*fieldstart == '-')
3929 12 : fieldstart++;
3930 44 : return strspn(fieldstart, "0123456789");
3931 : }
3932 :
3933 :
3934 : /* DecodeISO8601Interval()
3935 : * Decode an ISO 8601 time interval of the "format with designators"
3936 : * (section 4.4.3.2) or "alternative format" (section 4.4.3.3)
3937 : * Examples: P1D for 1 day
3938 : * PT1H for 1 hour
3939 : * P2Y6M7DT1H30M for 2 years, 6 months, 7 days 1 hour 30 min
3940 : * P0002-06-07T01:30:00 the same value in alternative format
3941 : *
3942 : * Returns 0 if successful, DTERR code if bogus input detected.
3943 : * Note: error code should be DTERR_BAD_FORMAT if input doesn't look like
3944 : * ISO8601, otherwise this could cause unexpected error messages.
3945 : * dtype and itm_in are output parameters.
3946 : *
3947 : * A couple exceptions from the spec:
3948 : * - a week field ('W') may coexist with other units
3949 : * - allows decimals in fields other than the least significant unit.
3950 : */
3951 : int
3952 408 : DecodeISO8601Interval(char *str,
3953 : int *dtype, struct pg_itm_in *itm_in)
3954 : {
3955 408 : bool datepart = true;
3956 408 : bool havefield = false;
3957 :
3958 408 : *dtype = DTK_DELTA;
3959 408 : ClearPgItmIn(itm_in);
3960 :
3961 408 : if (strlen(str) < 2 || str[0] != 'P')
3962 152 : return DTERR_BAD_FORMAT;
3963 :
3964 256 : str++;
3965 748 : while (*str)
3966 : {
3967 : char *fieldstart;
3968 : int64 val;
3969 : double fval;
3970 : char unit;
3971 : int dterr;
3972 :
3973 676 : if (*str == 'T') /* T indicates the beginning of the time part */
3974 : {
3975 132 : datepart = false;
3976 132 : havefield = false;
3977 132 : str++;
3978 160 : continue;
3979 : }
3980 :
3981 544 : fieldstart = str;
3982 544 : dterr = ParseISO8601Number(str, &str, &val, &fval);
3983 544 : if (dterr)
3984 184 : return dterr;
3985 :
3986 : /*
3987 : * Note: we could step off the end of the string here. Code below
3988 : * *must* exit the loop if unit == '\0'.
3989 : */
3990 540 : unit = *str++;
3991 :
3992 540 : if (datepart)
3993 : {
3994 312 : switch (unit) /* before T: Y M W D */
3995 : {
3996 56 : case 'Y':
3997 56 : if (!AdjustYears(val, 1, itm_in) ||
3998 56 : !AdjustFractYears(fval, 1, itm_in))
3999 8 : return DTERR_FIELD_OVERFLOW;
4000 48 : break;
4001 72 : case 'M':
4002 72 : if (!AdjustMonths(val, itm_in) ||
4003 64 : !AdjustFractDays(fval, DAYS_PER_MONTH, itm_in))
4004 16 : return DTERR_FIELD_OVERFLOW;
4005 56 : break;
4006 36 : case 'W':
4007 36 : if (!AdjustDays(val, 7, itm_in) ||
4008 28 : !AdjustFractDays(fval, 7, itm_in))
4009 16 : return DTERR_FIELD_OVERFLOW;
4010 20 : break;
4011 88 : case 'D':
4012 88 : if (!AdjustDays(val, 1, itm_in) ||
4013 64 : !AdjustFractMicroseconds(fval, USECS_PER_DAY, itm_in))
4014 24 : return DTERR_FIELD_OVERFLOW;
4015 64 : break;
4016 20 : case 'T': /* ISO 8601 4.4.3.3 Alternative Format / Basic */
4017 : case '\0':
4018 20 : if (ISO8601IntegerWidth(fieldstart) == 8 && !havefield)
4019 : {
4020 4 : if (!AdjustYears(val / 10000, 1, itm_in) ||
4021 4 : !AdjustMonths((val / 100) % 100, itm_in) ||
4022 4 : !AdjustDays(val % 100, 1, itm_in) ||
4023 4 : !AdjustFractMicroseconds(fval, USECS_PER_DAY, itm_in))
4024 0 : return DTERR_FIELD_OVERFLOW;
4025 4 : if (unit == '\0')
4026 0 : return 0;
4027 4 : datepart = false;
4028 4 : havefield = false;
4029 4 : continue;
4030 : }
4031 : /* Else fall through to extended alternative format */
4032 : pg_fallthrough;
4033 : case '-': /* ISO 8601 4.4.3.3 Alternative Format,
4034 : * Extended */
4035 56 : if (havefield)
4036 0 : return DTERR_BAD_FORMAT;
4037 :
4038 56 : if (!AdjustYears(val, 1, itm_in) ||
4039 48 : !AdjustFractYears(fval, 1, itm_in))
4040 8 : return DTERR_FIELD_OVERFLOW;
4041 48 : if (unit == '\0')
4042 4 : return 0;
4043 44 : if (unit == 'T')
4044 : {
4045 4 : datepart = false;
4046 4 : havefield = false;
4047 4 : continue;
4048 : }
4049 :
4050 40 : dterr = ParseISO8601Number(str, &str, &val, &fval);
4051 40 : if (dterr)
4052 0 : return dterr;
4053 40 : if (!AdjustMonths(val, itm_in) ||
4054 36 : !AdjustFractDays(fval, DAYS_PER_MONTH, itm_in))
4055 4 : return DTERR_FIELD_OVERFLOW;
4056 36 : if (*str == '\0')
4057 4 : return 0;
4058 32 : if (*str == 'T')
4059 : {
4060 4 : datepart = false;
4061 4 : havefield = false;
4062 4 : continue;
4063 : }
4064 28 : if (*str != '-')
4065 0 : return DTERR_BAD_FORMAT;
4066 28 : str++;
4067 :
4068 28 : dterr = ParseISO8601Number(str, &str, &val, &fval);
4069 28 : if (dterr)
4070 0 : return dterr;
4071 28 : if (!AdjustDays(val, 1, itm_in) ||
4072 24 : !AdjustFractMicroseconds(fval, USECS_PER_DAY, itm_in))
4073 4 : return DTERR_FIELD_OVERFLOW;
4074 24 : if (*str == '\0')
4075 8 : return 0;
4076 16 : if (*str == 'T')
4077 : {
4078 16 : datepart = false;
4079 16 : havefield = false;
4080 16 : continue;
4081 : }
4082 0 : return DTERR_BAD_FORMAT;
4083 0 : default:
4084 : /* not a valid date unit suffix */
4085 0 : return DTERR_BAD_FORMAT;
4086 : }
4087 : }
4088 : else
4089 : {
4090 228 : switch (unit) /* after T: H M S */
4091 : {
4092 72 : case 'H':
4093 72 : if (!AdjustMicroseconds(val, fval, USECS_PER_HOUR, itm_in))
4094 24 : return DTERR_FIELD_OVERFLOW;
4095 48 : break;
4096 40 : case 'M':
4097 40 : if (!AdjustMicroseconds(val, fval, USECS_PER_MINUTE, itm_in))
4098 0 : return DTERR_FIELD_OVERFLOW;
4099 40 : break;
4100 64 : case 'S':
4101 64 : if (!AdjustMicroseconds(val, fval, USECS_PER_SEC, itm_in))
4102 8 : return DTERR_FIELD_OVERFLOW;
4103 56 : break;
4104 24 : case '\0': /* ISO 8601 4.4.3.3 Alternative Format */
4105 24 : if (ISO8601IntegerWidth(fieldstart) == 6 && !havefield)
4106 : {
4107 4 : if (!AdjustMicroseconds(val / 10000, 0, USECS_PER_HOUR, itm_in) ||
4108 4 : !AdjustMicroseconds((val / 100) % 100, 0, USECS_PER_MINUTE, itm_in) ||
4109 4 : !AdjustMicroseconds(val % 100, 0, USECS_PER_SEC, itm_in) ||
4110 4 : !AdjustFractMicroseconds(fval, 1, itm_in))
4111 0 : return DTERR_FIELD_OVERFLOW;
4112 4 : return 0;
4113 : }
4114 : /* Else fall through to extended alternative format */
4115 : pg_fallthrough;
4116 : case ':': /* ISO 8601 4.4.3.3 Alternative Format,
4117 : * Extended */
4118 48 : if (havefield)
4119 0 : return DTERR_BAD_FORMAT;
4120 :
4121 48 : if (!AdjustMicroseconds(val, fval, USECS_PER_HOUR, itm_in))
4122 20 : return DTERR_FIELD_OVERFLOW;
4123 28 : if (unit == '\0')
4124 12 : return 0;
4125 :
4126 16 : dterr = ParseISO8601Number(str, &str, &val, &fval);
4127 16 : if (dterr)
4128 0 : return dterr;
4129 16 : if (!AdjustMicroseconds(val, fval, USECS_PER_MINUTE, itm_in))
4130 4 : return DTERR_FIELD_OVERFLOW;
4131 12 : if (*str == '\0')
4132 4 : return 0;
4133 8 : if (*str != ':')
4134 0 : return DTERR_BAD_FORMAT;
4135 8 : str++;
4136 :
4137 8 : dterr = ParseISO8601Number(str, &str, &val, &fval);
4138 8 : if (dterr)
4139 0 : return dterr;
4140 8 : if (!AdjustMicroseconds(val, fval, USECS_PER_SEC, itm_in))
4141 0 : return DTERR_FIELD_OVERFLOW;
4142 8 : if (*str == '\0')
4143 8 : return 0;
4144 0 : return DTERR_BAD_FORMAT;
4145 :
4146 0 : default:
4147 : /* not a valid time unit suffix */
4148 0 : return DTERR_BAD_FORMAT;
4149 : }
4150 : }
4151 :
4152 332 : havefield = true;
4153 : }
4154 :
4155 72 : return 0;
4156 : }
4157 :
4158 :
4159 : /* DecodeUnits()
4160 : * Decode text string using lookup table.
4161 : *
4162 : * This routine recognizes keywords associated with time interval units.
4163 : *
4164 : * Given string must be lowercased already.
4165 : *
4166 : * Implement a cache lookup since it is likely that dates
4167 : * will be related in format.
4168 : */
4169 : int
4170 74775 : DecodeUnits(int field, const char *lowtoken, int *val)
4171 : {
4172 : int type;
4173 : const datetkn *tp;
4174 :
4175 74775 : tp = deltacache[field];
4176 : /* use strncmp so that we match truncated tokens */
4177 74775 : if (tp == NULL || strncmp(lowtoken, tp->token, TOKMAXLEN) != 0)
4178 : {
4179 31903 : tp = datebsearch(lowtoken, deltatktbl, szdeltatktbl);
4180 : }
4181 74775 : if (tp == NULL)
4182 : {
4183 18977 : type = UNKNOWN_FIELD;
4184 18977 : *val = 0;
4185 : }
4186 : else
4187 : {
4188 55798 : deltacache[field] = tp;
4189 55798 : type = tp->type;
4190 55798 : *val = tp->value;
4191 : }
4192 :
4193 74775 : return type;
4194 : } /* DecodeUnits() */
4195 :
4196 : /*
4197 : * Report an error detected by one of the datetime input processing routines.
4198 : *
4199 : * dterr is the error code, and *extra contains any auxiliary info we need
4200 : * for the error report. extra can be NULL if not needed for the particular
4201 : * dterr value.
4202 : *
4203 : * str is the original input string, and datatype is the name of the datatype
4204 : * we were trying to accept. (For some DTERR codes, these are not used and
4205 : * can be NULL.)
4206 : *
4207 : * If escontext points to an ErrorSaveContext node, that is filled instead
4208 : * of throwing an error.
4209 : *
4210 : * Note: it might seem useless to distinguish DTERR_INTERVAL_OVERFLOW and
4211 : * DTERR_TZDISP_OVERFLOW from DTERR_FIELD_OVERFLOW, but SQL99 mandates three
4212 : * separate SQLSTATE codes, so ...
4213 : */
4214 : void
4215 1168 : DateTimeParseError(int dterr, DateTimeErrorExtra *extra,
4216 : const char *str, const char *datatype,
4217 : Node *escontext)
4218 : {
4219 1168 : switch (dterr)
4220 : {
4221 144 : case DTERR_FIELD_OVERFLOW:
4222 144 : errsave(escontext,
4223 : (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
4224 : errmsg("date/time field value out of range: \"%s\"",
4225 : str)));
4226 16 : break;
4227 120 : case DTERR_MD_FIELD_OVERFLOW:
4228 : /* <nanny>same as above, but add hint about DateStyle</nanny> */
4229 120 : errsave(escontext,
4230 : (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
4231 : errmsg("date/time field value out of range: \"%s\"",
4232 : str),
4233 : errhint("Perhaps you need a different \"DateStyle\" setting.")));
4234 0 : break;
4235 480 : case DTERR_INTERVAL_OVERFLOW:
4236 480 : errsave(escontext,
4237 : (errcode(ERRCODE_INTERVAL_FIELD_OVERFLOW),
4238 : errmsg("interval field value out of range: \"%s\"",
4239 : str)));
4240 0 : break;
4241 8 : case DTERR_TZDISP_OVERFLOW:
4242 8 : errsave(escontext,
4243 : (errcode(ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE),
4244 : errmsg("time zone displacement out of range: \"%s\"",
4245 : str)));
4246 0 : break;
4247 24 : case DTERR_BAD_TIMEZONE:
4248 24 : errsave(escontext,
4249 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4250 : errmsg("time zone \"%s\" not recognized",
4251 : extra->dtee_timezone)));
4252 16 : break;
4253 0 : case DTERR_BAD_ZONE_ABBREV:
4254 0 : errsave(escontext,
4255 : (errcode(ERRCODE_CONFIG_FILE_ERROR),
4256 : errmsg("time zone \"%s\" not recognized",
4257 : extra->dtee_timezone),
4258 : errdetail("This time zone name appears in the configuration file for time zone abbreviation \"%s\".",
4259 : extra->dtee_abbrev)));
4260 0 : break;
4261 392 : case DTERR_BAD_FORMAT:
4262 : default:
4263 392 : errsave(escontext,
4264 : (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
4265 : errmsg("invalid input syntax for type %s: \"%s\"",
4266 : datatype, str)));
4267 56 : break;
4268 : }
4269 88 : }
4270 :
4271 : /* datebsearch()
4272 : * Binary search -- from Knuth (6.2.1) Algorithm B. Special case like this
4273 : * is WAY faster than the generic bsearch().
4274 : */
4275 : static const datetkn *
4276 44344 : datebsearch(const char *key, const datetkn *base, int nel)
4277 : {
4278 44344 : if (nel > 0)
4279 : {
4280 44344 : const datetkn *last = base + nel - 1,
4281 : *position;
4282 : int result;
4283 :
4284 276198 : while (last >= base)
4285 : {
4286 251705 : position = base + ((last - base) >> 1);
4287 : /* precheck the first character for a bit of extra speed */
4288 251705 : result = (int) key[0] - (int) position->token[0];
4289 251705 : if (result == 0)
4290 : {
4291 : /* use strncmp so that we match truncated tokens */
4292 71857 : result = strncmp(key, position->token, TOKMAXLEN);
4293 71857 : if (result == 0)
4294 19851 : return position;
4295 : }
4296 231854 : if (result < 0)
4297 118693 : last = position - 1;
4298 : else
4299 113161 : base = position + 1;
4300 : }
4301 : }
4302 24493 : return NULL;
4303 : }
4304 :
4305 : /* EncodeTimezone()
4306 : * Copies representation of a numeric timezone offset to str.
4307 : *
4308 : * Returns a pointer to the new end of string. No NUL terminator is put
4309 : * there; callers are responsible for NUL terminating str themselves.
4310 : */
4311 : static char *
4312 32092 : EncodeTimezone(char *str, int tz, int style)
4313 : {
4314 : int hour,
4315 : min,
4316 : sec;
4317 :
4318 32092 : sec = abs(tz);
4319 32092 : min = sec / SECS_PER_MINUTE;
4320 32092 : sec -= min * SECS_PER_MINUTE;
4321 32092 : hour = min / MINS_PER_HOUR;
4322 32092 : min -= hour * MINS_PER_HOUR;
4323 :
4324 : /* TZ is negated compared to sign we wish to display ... */
4325 32092 : *str++ = (tz <= 0 ? '+' : '-');
4326 :
4327 32092 : if (sec != 0)
4328 : {
4329 0 : str = pg_ultostr_zeropad(str, hour, 2);
4330 0 : *str++ = ':';
4331 0 : str = pg_ultostr_zeropad(str, min, 2);
4332 0 : *str++ = ':';
4333 0 : str = pg_ultostr_zeropad(str, sec, 2);
4334 : }
4335 32092 : else if (min != 0 || style == USE_XSD_DATES)
4336 : {
4337 588 : str = pg_ultostr_zeropad(str, hour, 2);
4338 588 : *str++ = ':';
4339 588 : str = pg_ultostr_zeropad(str, min, 2);
4340 : }
4341 : else
4342 31504 : str = pg_ultostr_zeropad(str, hour, 2);
4343 32092 : return str;
4344 : }
4345 :
4346 : /* EncodeDateOnly()
4347 : * Encode date as local time.
4348 : */
4349 : void
4350 13690 : EncodeDateOnly(struct pg_tm *tm, int style, char *str)
4351 : {
4352 : Assert(tm->tm_mon >= 1 && tm->tm_mon <= MONTHS_PER_YEAR);
4353 :
4354 13690 : switch (style)
4355 : {
4356 8889 : case USE_ISO_DATES:
4357 : case USE_XSD_DATES:
4358 : /* compatible with ISO date formats */
4359 8889 : str = pg_ultostr_zeropad(str,
4360 8889 : (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4361 8889 : *str++ = '-';
4362 8889 : str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4363 8889 : *str++ = '-';
4364 8889 : str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4365 8889 : break;
4366 :
4367 0 : case USE_SQL_DATES:
4368 : /* compatible with Oracle/Ingres date formats */
4369 0 : if (DateOrder == DATEORDER_DMY)
4370 : {
4371 0 : str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4372 0 : *str++ = '/';
4373 0 : str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4374 : }
4375 : else
4376 : {
4377 0 : str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4378 0 : *str++ = '/';
4379 0 : str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4380 : }
4381 0 : *str++ = '/';
4382 0 : str = pg_ultostr_zeropad(str,
4383 0 : (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4384 0 : break;
4385 :
4386 4 : case USE_GERMAN_DATES:
4387 : /* German-style date format */
4388 4 : str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4389 4 : *str++ = '.';
4390 4 : str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4391 4 : *str++ = '.';
4392 4 : str = pg_ultostr_zeropad(str,
4393 4 : (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4394 4 : break;
4395 :
4396 4797 : case USE_POSTGRES_DATES:
4397 : default:
4398 : /* traditional date-only style for Postgres */
4399 4797 : if (DateOrder == DATEORDER_DMY)
4400 : {
4401 0 : str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4402 0 : *str++ = '-';
4403 0 : str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4404 : }
4405 : else
4406 : {
4407 4797 : str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4408 4797 : *str++ = '-';
4409 4797 : str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4410 : }
4411 4797 : *str++ = '-';
4412 4797 : str = pg_ultostr_zeropad(str,
4413 4797 : (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4414 4797 : break;
4415 : }
4416 :
4417 13690 : if (tm->tm_year <= 0)
4418 : {
4419 52 : memcpy(str, " BC", 3); /* Don't copy NUL */
4420 52 : str += 3;
4421 : }
4422 13690 : *str = '\0';
4423 13690 : }
4424 :
4425 :
4426 : /* EncodeTimeOnly()
4427 : * Encode time fields only.
4428 : *
4429 : * tm and fsec are the value to encode, print_tz determines whether to include
4430 : * a time zone (the difference between time and timetz types), tz is the
4431 : * numeric time zone offset, style is the date style, str is where to write the
4432 : * output.
4433 : */
4434 : void
4435 8164 : EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
4436 : {
4437 8164 : str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
4438 8164 : *str++ = ':';
4439 8164 : str = pg_ultostr_zeropad(str, tm->tm_min, 2);
4440 8164 : *str++ = ':';
4441 8164 : str = AppendSeconds(str, tm->tm_sec, fsec, MAX_TIME_PRECISION, true);
4442 8164 : if (print_tz)
4443 4305 : str = EncodeTimezone(str, tz, style);
4444 8164 : *str = '\0';
4445 8164 : }
4446 :
4447 :
4448 : /* EncodeDateTime()
4449 : * Encode date and time interpreted as local time.
4450 : *
4451 : * tm and fsec are the value to encode, print_tz determines whether to include
4452 : * a time zone (the difference between timestamp and timestamptz types), tz is
4453 : * the numeric time zone offset, tzn is the textual time zone, which if
4454 : * specified will be used instead of tz by some styles, style is the date
4455 : * style, str is where to write the output.
4456 : *
4457 : * Supported date styles:
4458 : * Postgres - day mon hh:mm:ss yyyy tz
4459 : * SQL - mm/dd/yyyy hh:mm:ss.ss tz
4460 : * ISO - yyyy-mm-dd hh:mm:ss+/-tz
4461 : * German - dd.mm.yyyy hh:mm:ss tz
4462 : * XSD - yyyy-mm-ddThh:mm:ss.ss+/-tz
4463 : */
4464 : void
4465 63495 : EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
4466 : {
4467 : int day;
4468 :
4469 : Assert(tm->tm_mon >= 1 && tm->tm_mon <= MONTHS_PER_YEAR);
4470 :
4471 : /*
4472 : * Negative tm_isdst means we have no valid time zone translation.
4473 : */
4474 63495 : if (tm->tm_isdst < 0)
4475 23643 : print_tz = false;
4476 :
4477 63495 : switch (style)
4478 : {
4479 43989 : case USE_ISO_DATES:
4480 : case USE_XSD_DATES:
4481 : /* Compatible with ISO-8601 date formats */
4482 43989 : str = pg_ultostr_zeropad(str,
4483 43989 : (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4484 43989 : *str++ = '-';
4485 43989 : str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4486 43989 : *str++ = '-';
4487 43989 : str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4488 43989 : *str++ = (style == USE_ISO_DATES) ? ' ' : 'T';
4489 43989 : str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
4490 43989 : *str++ = ':';
4491 43989 : str = pg_ultostr_zeropad(str, tm->tm_min, 2);
4492 43989 : *str++ = ':';
4493 43989 : str = AppendTimestampSeconds(str, tm, fsec);
4494 43989 : if (print_tz)
4495 27787 : str = EncodeTimezone(str, tz, style);
4496 43989 : break;
4497 :
4498 520 : case USE_SQL_DATES:
4499 : /* Compatible with Oracle/Ingres date formats */
4500 520 : if (DateOrder == DATEORDER_DMY)
4501 : {
4502 256 : str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4503 256 : *str++ = '/';
4504 256 : str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4505 : }
4506 : else
4507 : {
4508 264 : str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4509 264 : *str++ = '/';
4510 264 : str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4511 : }
4512 520 : *str++ = '/';
4513 520 : str = pg_ultostr_zeropad(str,
4514 520 : (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4515 520 : *str++ = ' ';
4516 520 : str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
4517 520 : *str++ = ':';
4518 520 : str = pg_ultostr_zeropad(str, tm->tm_min, 2);
4519 520 : *str++ = ':';
4520 520 : str = AppendTimestampSeconds(str, tm, fsec);
4521 :
4522 : /*
4523 : * Note: the uses of %.*s in this function would be risky if the
4524 : * timezone names ever contain non-ASCII characters, since we are
4525 : * not being careful to do encoding-aware clipping. However, all
4526 : * TZ abbreviations in the IANA database are plain ASCII.
4527 : */
4528 520 : if (print_tz)
4529 : {
4530 12 : if (tzn)
4531 : {
4532 12 : sprintf(str, " %.*s", MAXTZLEN, tzn);
4533 12 : str += strlen(str);
4534 : }
4535 : else
4536 0 : str = EncodeTimezone(str, tz, style);
4537 : }
4538 520 : break;
4539 :
4540 16 : case USE_GERMAN_DATES:
4541 : /* German variant on European style */
4542 16 : str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4543 16 : *str++ = '.';
4544 16 : str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4545 16 : *str++ = '.';
4546 16 : str = pg_ultostr_zeropad(str,
4547 16 : (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4548 16 : *str++ = ' ';
4549 16 : str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
4550 16 : *str++ = ':';
4551 16 : str = pg_ultostr_zeropad(str, tm->tm_min, 2);
4552 16 : *str++ = ':';
4553 16 : str = AppendTimestampSeconds(str, tm, fsec);
4554 :
4555 16 : if (print_tz)
4556 : {
4557 16 : if (tzn)
4558 : {
4559 16 : sprintf(str, " %.*s", MAXTZLEN, tzn);
4560 16 : str += strlen(str);
4561 : }
4562 : else
4563 0 : str = EncodeTimezone(str, tz, style);
4564 : }
4565 16 : break;
4566 :
4567 18970 : case USE_POSTGRES_DATES:
4568 : default:
4569 : /* Backward-compatible with traditional Postgres abstime dates */
4570 18970 : day = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
4571 18970 : tm->tm_wday = j2day(day);
4572 18970 : memcpy(str, days[tm->tm_wday], 3);
4573 18970 : str += 3;
4574 18970 : *str++ = ' ';
4575 18970 : if (DateOrder == DATEORDER_DMY)
4576 : {
4577 264 : str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4578 264 : *str++ = ' ';
4579 264 : memcpy(str, months[tm->tm_mon - 1], 3);
4580 264 : str += 3;
4581 : }
4582 : else
4583 : {
4584 18706 : memcpy(str, months[tm->tm_mon - 1], 3);
4585 18706 : str += 3;
4586 18706 : *str++ = ' ';
4587 18706 : str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4588 : }
4589 18970 : *str++ = ' ';
4590 18970 : str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
4591 18970 : *str++ = ':';
4592 18970 : str = pg_ultostr_zeropad(str, tm->tm_min, 2);
4593 18970 : *str++ = ':';
4594 18970 : str = AppendTimestampSeconds(str, tm, fsec);
4595 18970 : *str++ = ' ';
4596 18970 : str = pg_ultostr_zeropad(str,
4597 18970 : (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4598 :
4599 18970 : if (print_tz)
4600 : {
4601 12037 : if (tzn)
4602 : {
4603 12037 : sprintf(str, " %.*s", MAXTZLEN, tzn);
4604 12037 : str += strlen(str);
4605 : }
4606 : else
4607 : {
4608 : /*
4609 : * We have a time zone, but no string version. Use the
4610 : * numeric form, but be sure to include a leading space to
4611 : * avoid formatting something which would be rejected by
4612 : * the date/time parser later. - thomas 2001-10-19
4613 : */
4614 0 : *str++ = ' ';
4615 0 : str = EncodeTimezone(str, tz, style);
4616 : }
4617 : }
4618 18970 : break;
4619 : }
4620 :
4621 63495 : if (tm->tm_year <= 0)
4622 : {
4623 184 : memcpy(str, " BC", 3); /* Don't copy NUL */
4624 184 : str += 3;
4625 : }
4626 63495 : *str = '\0';
4627 63495 : }
4628 :
4629 :
4630 : /*
4631 : * Helper functions to avoid duplicated code in EncodeInterval.
4632 : */
4633 :
4634 : /* Append an ISO-8601-style interval field, but only if value isn't zero */
4635 : static char *
4636 140 : AddISO8601IntPart(char *cp, int64 value, char units)
4637 : {
4638 140 : if (value == 0)
4639 36 : return cp;
4640 104 : sprintf(cp, "%" PRId64 "%c", value, units);
4641 104 : return cp + strlen(cp);
4642 : }
4643 :
4644 : /* Append a postgres-style interval field, but only if value isn't zero */
4645 : static char *
4646 9456 : AddPostgresIntPart(char *cp, int64 value, const char *units,
4647 : bool *is_zero, bool *is_before)
4648 : {
4649 9456 : if (value == 0)
4650 5229 : return cp;
4651 12681 : sprintf(cp, "%s%s%" PRId64 " %s%s",
4652 4227 : (!*is_zero) ? " " : "",
4653 4227 : (*is_before && value > 0) ? "+" : "",
4654 : value,
4655 : units,
4656 : (value != 1) ? "s" : "");
4657 :
4658 : /*
4659 : * Each nonzero field sets is_before for (only) the next one. This is a
4660 : * tad bizarre but it's how it worked before...
4661 : */
4662 4227 : *is_before = (value < 0);
4663 4227 : *is_zero = false;
4664 4227 : return cp + strlen(cp);
4665 : }
4666 :
4667 : /* Append a verbose-style interval field, but only if value isn't zero */
4668 : static char *
4669 28215 : AddVerboseIntPart(char *cp, int64 value, const char *units,
4670 : bool *is_zero, bool *is_before)
4671 : {
4672 28215 : if (value == 0)
4673 18730 : return cp;
4674 : /* first nonzero value sets is_before */
4675 9485 : if (*is_zero)
4676 : {
4677 5230 : *is_before = (value < 0);
4678 5230 : value = i64abs(value);
4679 : }
4680 4255 : else if (*is_before)
4681 904 : value = -value;
4682 9485 : sprintf(cp, " %" PRId64 " %s%s", value, units, (value == 1) ? "" : "s");
4683 9485 : *is_zero = false;
4684 9485 : return cp + strlen(cp);
4685 : }
4686 :
4687 :
4688 : /* EncodeInterval()
4689 : * Interpret time structure as a delta time and convert to string.
4690 : *
4691 : * Support "traditional Postgres" and ISO-8601 styles.
4692 : * Actually, afaik ISO does not address time interval formatting,
4693 : * but this looks similar to the spec for absolute date/time.
4694 : * - thomas 1998-04-30
4695 : *
4696 : * Actually, afaik, ISO 8601 does specify formats for "time
4697 : * intervals...[of the]...format with time-unit designators", which
4698 : * are pretty ugly. The format looks something like
4699 : * P1Y1M1DT1H1M1.12345S
4700 : * but useful for exchanging data with computers instead of humans.
4701 : * - ron 2003-07-14
4702 : *
4703 : * And ISO's SQL 2008 standard specifies standards for
4704 : * "year-month literal"s (that look like '2-3') and
4705 : * "day-time literal"s (that look like ('4 5:6:7')
4706 : */
4707 : void
4708 8911 : EncodeInterval(struct pg_itm *itm, int style, char *str)
4709 : {
4710 8911 : char *cp = str;
4711 8911 : int year = itm->tm_year;
4712 8911 : int mon = itm->tm_mon;
4713 8911 : int64 mday = itm->tm_mday; /* tm_mday could be INT_MIN */
4714 8911 : int64 hour = itm->tm_hour;
4715 8911 : int min = itm->tm_min;
4716 8911 : int sec = itm->tm_sec;
4717 8911 : int fsec = itm->tm_usec;
4718 8911 : bool is_before = false;
4719 8911 : bool is_zero = true;
4720 :
4721 : /*
4722 : * The sign of year and month are guaranteed to match, since they are
4723 : * stored internally as "month". But we'll need to check for is_before and
4724 : * is_zero when determining the signs of day and hour/minute/seconds
4725 : * fields.
4726 : */
4727 8911 : switch (style)
4728 : {
4729 : /* SQL Standard interval format */
4730 84 : case INTSTYLE_SQL_STANDARD:
4731 : {
4732 64 : bool has_negative = year < 0 || mon < 0 ||
4733 44 : mday < 0 || hour < 0 ||
4734 148 : min < 0 || sec < 0 || fsec < 0;
4735 68 : bool has_positive = year > 0 || mon > 0 ||
4736 44 : mday > 0 || hour > 0 ||
4737 152 : min > 0 || sec > 0 || fsec > 0;
4738 84 : bool has_year_month = year != 0 || mon != 0;
4739 28 : bool has_day_time = mday != 0 || hour != 0 ||
4740 112 : min != 0 || sec != 0 || fsec != 0;
4741 84 : bool has_day = mday != 0;
4742 148 : bool sql_standard_value = !(has_negative && has_positive) &&
4743 64 : !(has_year_month && has_day_time);
4744 :
4745 : /*
4746 : * SQL Standard wants only 1 "<sign>" preceding the whole
4747 : * interval ... but can't do that if mixed signs.
4748 : */
4749 84 : if (has_negative && sql_standard_value)
4750 : {
4751 20 : *cp++ = '-';
4752 20 : year = -year;
4753 20 : mon = -mon;
4754 20 : mday = -mday;
4755 20 : hour = -hour;
4756 20 : min = -min;
4757 20 : sec = -sec;
4758 20 : fsec = -fsec;
4759 : }
4760 :
4761 84 : if (!has_negative && !has_positive)
4762 : {
4763 8 : sprintf(cp, "0");
4764 : }
4765 76 : else if (!sql_standard_value)
4766 : {
4767 : /*
4768 : * For non sql-standard interval values, force outputting
4769 : * the signs to avoid ambiguities with intervals with
4770 : * mixed sign components.
4771 : */
4772 36 : char year_sign = (year < 0 || mon < 0) ? '-' : '+';
4773 36 : char day_sign = (mday < 0) ? '-' : '+';
4774 52 : char sec_sign = (hour < 0 || min < 0 ||
4775 16 : sec < 0 || fsec < 0) ? '-' : '+';
4776 :
4777 36 : sprintf(cp, "%c%d-%d %c%" PRId64 " %c%" PRId64 ":%02d:",
4778 : year_sign, abs(year), abs(mon),
4779 : day_sign, i64abs(mday),
4780 : sec_sign, i64abs(hour), abs(min));
4781 36 : cp += strlen(cp);
4782 36 : cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4783 36 : *cp = '\0';
4784 : }
4785 40 : else if (has_year_month)
4786 : {
4787 12 : sprintf(cp, "%d-%d", year, mon);
4788 : }
4789 28 : else if (has_day)
4790 : {
4791 20 : sprintf(cp, "%" PRId64 " %" PRId64 ":%02d:",
4792 : mday, hour, min);
4793 20 : cp += strlen(cp);
4794 20 : cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4795 20 : *cp = '\0';
4796 : }
4797 : else
4798 : {
4799 8 : sprintf(cp, "%" PRId64 ":%02d:", hour, min);
4800 8 : cp += strlen(cp);
4801 8 : cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4802 8 : *cp = '\0';
4803 : }
4804 : }
4805 84 : break;
4806 :
4807 : /* ISO 8601 "time-intervals by duration only" */
4808 32 : case INTSTYLE_ISO_8601:
4809 : /* special-case zero to avoid printing nothing */
4810 32 : if (year == 0 && mon == 0 && mday == 0 &&
4811 4 : hour == 0 && min == 0 && sec == 0 && fsec == 0)
4812 : {
4813 4 : sprintf(cp, "PT0S");
4814 4 : break;
4815 : }
4816 28 : *cp++ = 'P';
4817 28 : cp = AddISO8601IntPart(cp, year, 'Y');
4818 28 : cp = AddISO8601IntPart(cp, mon, 'M');
4819 28 : cp = AddISO8601IntPart(cp, mday, 'D');
4820 28 : if (hour != 0 || min != 0 || sec != 0 || fsec != 0)
4821 24 : *cp++ = 'T';
4822 28 : cp = AddISO8601IntPart(cp, hour, 'H');
4823 28 : cp = AddISO8601IntPart(cp, min, 'M');
4824 28 : if (sec != 0 || fsec != 0)
4825 : {
4826 24 : if (sec < 0 || fsec < 0)
4827 8 : *cp++ = '-';
4828 24 : cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, false);
4829 24 : *cp++ = 'S';
4830 24 : *cp++ = '\0';
4831 : }
4832 28 : break;
4833 :
4834 : /* Compatible with postgresql < 8.4 when DateStyle = 'iso' */
4835 3152 : case INTSTYLE_POSTGRES:
4836 3152 : cp = AddPostgresIntPart(cp, year, "year", &is_zero, &is_before);
4837 :
4838 : /*
4839 : * Ideally we should spell out "month" like we do for "year" and
4840 : * "day". However, for backward compatibility, we can't easily
4841 : * fix this. bjm 2011-05-24
4842 : */
4843 3152 : cp = AddPostgresIntPart(cp, mon, "mon", &is_zero, &is_before);
4844 3152 : cp = AddPostgresIntPart(cp, mday, "day", &is_zero, &is_before);
4845 3152 : if (is_zero || hour != 0 || min != 0 || sec != 0 || fsec != 0)
4846 : {
4847 2538 : bool minus = (hour < 0 || min < 0 || sec < 0 || fsec < 0);
4848 :
4849 7473 : sprintf(cp, "%s%s%02" PRId64 ":%02d:",
4850 2538 : is_zero ? "" : " ",
4851 2397 : (minus ? "-" : (is_before ? "+" : "")),
4852 : i64abs(hour), abs(min));
4853 2538 : cp += strlen(cp);
4854 2538 : cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4855 2538 : *cp = '\0';
4856 : }
4857 3152 : break;
4858 :
4859 : /* Compatible with postgresql < 8.4 when DateStyle != 'iso' */
4860 5643 : case INTSTYLE_POSTGRES_VERBOSE:
4861 : default:
4862 5643 : strcpy(cp, "@");
4863 5643 : cp++;
4864 5643 : cp = AddVerboseIntPart(cp, year, "year", &is_zero, &is_before);
4865 5643 : cp = AddVerboseIntPart(cp, mon, "mon", &is_zero, &is_before);
4866 5643 : cp = AddVerboseIntPart(cp, mday, "day", &is_zero, &is_before);
4867 5643 : cp = AddVerboseIntPart(cp, hour, "hour", &is_zero, &is_before);
4868 5643 : cp = AddVerboseIntPart(cp, min, "min", &is_zero, &is_before);
4869 5643 : if (sec != 0 || fsec != 0)
4870 : {
4871 2174 : *cp++ = ' ';
4872 2174 : if (sec < 0 || (sec == 0 && fsec < 0))
4873 : {
4874 679 : if (is_zero)
4875 228 : is_before = true;
4876 451 : else if (!is_before)
4877 3 : *cp++ = '-';
4878 : }
4879 1495 : else if (is_before)
4880 8 : *cp++ = '-';
4881 2174 : cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, false);
4882 : /* We output "ago", not negatives, so use abs(). */
4883 2174 : sprintf(cp, " sec%s",
4884 2174 : (abs(sec) != 1 || fsec != 0) ? "s" : "");
4885 2174 : is_zero = false;
4886 : }
4887 : /* identically zero? then put in a unitless zero... */
4888 5643 : if (is_zero)
4889 151 : strcat(cp, " 0");
4890 5643 : if (is_before)
4891 942 : strcat(cp, " ago");
4892 5643 : break;
4893 : }
4894 8911 : }
4895 :
4896 :
4897 : /*
4898 : * We've been burnt by stupid errors in the ordering of the datetkn tables
4899 : * once too often. Arrange to check them during postmaster start.
4900 : */
4901 : static bool
4902 1990 : CheckDateTokenTable(const char *tablename, const datetkn *base, int nel)
4903 : {
4904 1990 : bool ok = true;
4905 : int i;
4906 :
4907 134325 : for (i = 0; i < nel; i++)
4908 : {
4909 : /* check for token strings that don't fit */
4910 132335 : if (strlen(base[i].token) > TOKMAXLEN)
4911 : {
4912 : /* %.*s is safe since all our tokens are ASCII */
4913 0 : elog(LOG, "token too long in %s table: \"%.*s\"",
4914 : tablename,
4915 : TOKMAXLEN + 1, base[i].token);
4916 0 : ok = false;
4917 0 : break; /* don't risk applying strcmp */
4918 : }
4919 : /* check for out of order */
4920 132335 : if (i > 0 &&
4921 130345 : strcmp(base[i - 1].token, base[i].token) >= 0)
4922 : {
4923 0 : elog(LOG, "ordering error in %s table: \"%s\" >= \"%s\"",
4924 : tablename,
4925 : base[i - 1].token,
4926 : base[i].token);
4927 0 : ok = false;
4928 : }
4929 : }
4930 1990 : return ok;
4931 : }
4932 :
4933 : bool
4934 995 : CheckDateTokenTables(void)
4935 : {
4936 995 : bool ok = true;
4937 :
4938 : Assert(UNIX_EPOCH_JDATE == date2j(1970, 1, 1));
4939 : Assert(POSTGRES_EPOCH_JDATE == date2j(2000, 1, 1));
4940 :
4941 995 : ok &= CheckDateTokenTable("datetktbl", datetktbl, szdatetktbl);
4942 995 : ok &= CheckDateTokenTable("deltatktbl", deltatktbl, szdeltatktbl);
4943 995 : return ok;
4944 : }
4945 :
4946 : /*
4947 : * Common code for temporal prosupport functions: simplify, if possible,
4948 : * a call to a temporal type's length-coercion function.
4949 : *
4950 : * Types time, timetz, timestamp and timestamptz each have a range of allowed
4951 : * precisions. An unspecified precision is rigorously equivalent to the
4952 : * highest specifiable precision. We can replace the function call with a
4953 : * no-op RelabelType if it is coercing to the same or higher precision as the
4954 : * input is known to have.
4955 : *
4956 : * The input Node is always a FuncExpr, but to reduce the #include footprint
4957 : * of datetime.h, we declare it as Node *.
4958 : *
4959 : * Note: timestamp_scale throws an error when the typmod is out of range, but
4960 : * we can't get there from a cast: our typmodin will have caught it already.
4961 : */
4962 : Node *
4963 16 : TemporalSimplify(int32 max_precis, Node *node)
4964 : {
4965 16 : FuncExpr *expr = castNode(FuncExpr, node);
4966 16 : Node *ret = NULL;
4967 : Node *typmod;
4968 :
4969 : Assert(list_length(expr->args) >= 2);
4970 :
4971 16 : typmod = (Node *) lsecond(expr->args);
4972 :
4973 16 : if (IsA(typmod, Const) && !((Const *) typmod)->constisnull)
4974 : {
4975 16 : Node *source = (Node *) linitial(expr->args);
4976 16 : int32 old_precis = exprTypmod(source);
4977 16 : int32 new_precis = DatumGetInt32(((Const *) typmod)->constvalue);
4978 :
4979 16 : if (new_precis < 0 || new_precis == max_precis ||
4980 0 : (old_precis >= 0 && new_precis >= old_precis))
4981 0 : ret = relabel_to_typmod(source, new_precis);
4982 : }
4983 :
4984 16 : return ret;
4985 : }
4986 :
4987 : /*
4988 : * This function gets called during timezone config file load or reload
4989 : * to create the final array of timezone tokens. The argument array
4990 : * is already sorted in name order.
4991 : *
4992 : * The result is a TimeZoneAbbrevTable (which must be a single guc_malloc'd
4993 : * chunk) or NULL on alloc failure. No other error conditions are defined.
4994 : */
4995 : TimeZoneAbbrevTable *
4996 8736 : ConvertTimeZoneAbbrevs(struct tzEntry *abbrevs, int n)
4997 : {
4998 : TimeZoneAbbrevTable *tbl;
4999 : Size tbl_size;
5000 : int i;
5001 :
5002 : /* Space for fixed fields and datetkn array */
5003 8736 : tbl_size = offsetof(TimeZoneAbbrevTable, abbrevs) +
5004 8736 : n * sizeof(datetkn);
5005 8736 : tbl_size = MAXALIGN(tbl_size);
5006 : /* Count up space for dynamic abbreviations */
5007 1712264 : for (i = 0; i < n; i++)
5008 : {
5009 1703528 : struct tzEntry *abbr = abbrevs + i;
5010 :
5011 1703528 : if (abbr->zone != NULL)
5012 : {
5013 : Size dsize;
5014 :
5015 436796 : dsize = offsetof(DynamicZoneAbbrev, zone) +
5016 436796 : strlen(abbr->zone) + 1;
5017 436796 : tbl_size += MAXALIGN(dsize);
5018 : }
5019 : }
5020 :
5021 : /* Alloc the result ... */
5022 8736 : tbl = guc_malloc(LOG, tbl_size);
5023 8736 : if (!tbl)
5024 0 : return NULL;
5025 :
5026 : /* ... and fill it in */
5027 8736 : tbl->tblsize = tbl_size;
5028 8736 : tbl->numabbrevs = n;
5029 : /* in this loop, tbl_size reprises the space calculation above */
5030 8736 : tbl_size = offsetof(TimeZoneAbbrevTable, abbrevs) +
5031 8736 : n * sizeof(datetkn);
5032 8736 : tbl_size = MAXALIGN(tbl_size);
5033 1712264 : for (i = 0; i < n; i++)
5034 : {
5035 1703528 : struct tzEntry *abbr = abbrevs + i;
5036 1703528 : datetkn *dtoken = tbl->abbrevs + i;
5037 :
5038 : /* use strlcpy to truncate name if necessary */
5039 1703528 : strlcpy(dtoken->token, abbr->abbrev, TOKMAXLEN + 1);
5040 1703528 : if (abbr->zone != NULL)
5041 : {
5042 : /* Allocate a DynamicZoneAbbrev for this abbreviation */
5043 : DynamicZoneAbbrev *dtza;
5044 : Size dsize;
5045 :
5046 436796 : dtza = (DynamicZoneAbbrev *) ((char *) tbl + tbl_size);
5047 436796 : dtza->tz = NULL;
5048 436796 : strcpy(dtza->zone, abbr->zone);
5049 :
5050 436796 : dtoken->type = DYNTZ;
5051 : /* value is offset from table start to DynamicZoneAbbrev */
5052 436796 : dtoken->value = (int32) tbl_size;
5053 :
5054 436796 : dsize = offsetof(DynamicZoneAbbrev, zone) +
5055 436796 : strlen(abbr->zone) + 1;
5056 436796 : tbl_size += MAXALIGN(dsize);
5057 : }
5058 : else
5059 : {
5060 1266732 : dtoken->type = abbr->is_dst ? DTZ : TZ;
5061 1266732 : dtoken->value = abbr->offset;
5062 : }
5063 : }
5064 :
5065 : /* Assert the two loops above agreed on size calculations */
5066 : Assert(tbl->tblsize == tbl_size);
5067 :
5068 : /* Check the ordering, if testing */
5069 : Assert(CheckDateTokenTable("timezone abbreviations", tbl->abbrevs, n));
5070 :
5071 8736 : return tbl;
5072 : }
5073 :
5074 : /*
5075 : * Install a TimeZoneAbbrevTable as the active table.
5076 : *
5077 : * Caller is responsible that the passed table doesn't go away while in use.
5078 : */
5079 : void
5080 8626 : InstallTimeZoneAbbrevs(TimeZoneAbbrevTable *tbl)
5081 : {
5082 8626 : zoneabbrevtbl = tbl;
5083 : /* reset tzabbrevcache, which may contain results from old table */
5084 8626 : memset(tzabbrevcache, 0, sizeof(tzabbrevcache));
5085 8626 : }
5086 :
5087 : /*
5088 : * Helper subroutine to locate pg_tz timezone for a dynamic abbreviation.
5089 : *
5090 : * On failure, returns NULL and fills *extra for a DTERR_BAD_ZONE_ABBREV error.
5091 : */
5092 : static pg_tz *
5093 820 : FetchDynamicTimeZone(TimeZoneAbbrevTable *tbl, const datetkn *tp,
5094 : DateTimeErrorExtra *extra)
5095 : {
5096 : DynamicZoneAbbrev *dtza;
5097 :
5098 : /* Just some sanity checks to prevent indexing off into nowhere */
5099 : Assert(tp->type == DYNTZ);
5100 : Assert(tp->value > 0 && tp->value < tbl->tblsize);
5101 :
5102 820 : dtza = (DynamicZoneAbbrev *) ((char *) tbl + tp->value);
5103 :
5104 : /* Look up the underlying zone if we haven't already */
5105 820 : if (dtza->tz == NULL)
5106 : {
5107 612 : dtza->tz = pg_tzset(dtza->zone);
5108 612 : if (dtza->tz == NULL)
5109 : {
5110 : /* Ooops, bogus zone name in config file entry */
5111 0 : extra->dtee_timezone = dtza->zone;
5112 0 : extra->dtee_abbrev = tp->token;
5113 : }
5114 : }
5115 820 : return dtza->tz;
5116 : }
5117 :
5118 :
5119 : /*
5120 : * This set-returning function reads all the time zone abbreviations
5121 : * defined by the IANA data for the current timezone setting,
5122 : * and returns a set of (abbrev, utc_offset, is_dst).
5123 : */
5124 : Datum
5125 168 : pg_timezone_abbrevs_zone(PG_FUNCTION_ARGS)
5126 : {
5127 : FuncCallContext *funcctx;
5128 : int *pindex;
5129 : Datum result;
5130 : HeapTuple tuple;
5131 : Datum values[3];
5132 168 : bool nulls[3] = {0};
5133 168 : TimestampTz now = GetCurrentTransactionStartTimestamp();
5134 168 : pg_time_t t = timestamptz_to_time_t(now);
5135 : const char *abbrev;
5136 : long int gmtoff;
5137 : int isdst;
5138 : struct pg_itm_in itm_in;
5139 : Interval *resInterval;
5140 :
5141 : /* stuff done only on the first call of the function */
5142 168 : if (SRF_IS_FIRSTCALL())
5143 : {
5144 : TupleDesc tupdesc;
5145 : MemoryContext oldcontext;
5146 :
5147 : /* create a function context for cross-call persistence */
5148 28 : funcctx = SRF_FIRSTCALL_INIT();
5149 :
5150 : /*
5151 : * switch to memory context appropriate for multiple function calls
5152 : */
5153 28 : oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5154 :
5155 : /* allocate memory for user context */
5156 28 : pindex = palloc_object(int);
5157 28 : *pindex = 0;
5158 28 : funcctx->user_fctx = pindex;
5159 :
5160 28 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
5161 0 : elog(ERROR, "return type must be a row type");
5162 28 : funcctx->tuple_desc = tupdesc;
5163 :
5164 28 : MemoryContextSwitchTo(oldcontext);
5165 : }
5166 :
5167 : /* stuff done on every call of the function */
5168 168 : funcctx = SRF_PERCALL_SETUP();
5169 168 : pindex = (int *) funcctx->user_fctx;
5170 :
5171 168 : while ((abbrev = pg_get_next_timezone_abbrev(pindex,
5172 168 : session_timezone)) != NULL)
5173 : {
5174 : /* Ignore abbreviations that aren't all-alphabetic */
5175 140 : if (strspn(abbrev, "ABCDEFGHIJKLMNOPQRSTUVWXYZ") != strlen(abbrev))
5176 0 : continue;
5177 :
5178 : /* Determine the current meaning of the abbrev */
5179 140 : if (!pg_interpret_timezone_abbrev(abbrev,
5180 : &t,
5181 : &gmtoff,
5182 : &isdst,
5183 : session_timezone))
5184 0 : continue; /* hm, not actually used in this zone? */
5185 :
5186 140 : values[0] = CStringGetTextDatum(abbrev);
5187 :
5188 : /* Convert offset (in seconds) to an interval; can't overflow */
5189 560 : MemSet(&itm_in, 0, sizeof(struct pg_itm_in));
5190 140 : itm_in.tm_usec = (int64) gmtoff * USECS_PER_SEC;
5191 140 : resInterval = palloc_object(Interval);
5192 140 : (void) itmin2interval(&itm_in, resInterval);
5193 140 : values[1] = IntervalPGetDatum(resInterval);
5194 :
5195 140 : values[2] = BoolGetDatum(isdst);
5196 :
5197 140 : tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
5198 140 : result = HeapTupleGetDatum(tuple);
5199 :
5200 140 : SRF_RETURN_NEXT(funcctx, result);
5201 : }
5202 :
5203 28 : SRF_RETURN_DONE(funcctx);
5204 : }
5205 :
5206 : /*
5207 : * This set-returning function reads all the time zone abbreviations
5208 : * defined by the timezone_abbreviations setting,
5209 : * and returns a set of (abbrev, utc_offset, is_dst).
5210 : */
5211 : Datum
5212 3144 : pg_timezone_abbrevs_abbrevs(PG_FUNCTION_ARGS)
5213 : {
5214 : FuncCallContext *funcctx;
5215 : int *pindex;
5216 : Datum result;
5217 : HeapTuple tuple;
5218 : Datum values[3];
5219 3144 : bool nulls[3] = {0};
5220 : const datetkn *tp;
5221 : char buffer[TOKMAXLEN + 1];
5222 : int gmtoffset;
5223 : bool is_dst;
5224 : unsigned char *p;
5225 : struct pg_itm_in itm_in;
5226 : Interval *resInterval;
5227 :
5228 : /* stuff done only on the first call of the function */
5229 3144 : if (SRF_IS_FIRSTCALL())
5230 : {
5231 : TupleDesc tupdesc;
5232 : MemoryContext oldcontext;
5233 :
5234 : /* create a function context for cross-call persistence */
5235 16 : funcctx = SRF_FIRSTCALL_INIT();
5236 :
5237 : /*
5238 : * switch to memory context appropriate for multiple function calls
5239 : */
5240 16 : oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5241 :
5242 : /* allocate memory for user context */
5243 16 : pindex = palloc_object(int);
5244 16 : *pindex = 0;
5245 16 : funcctx->user_fctx = pindex;
5246 :
5247 16 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
5248 0 : elog(ERROR, "return type must be a row type");
5249 16 : funcctx->tuple_desc = tupdesc;
5250 :
5251 16 : MemoryContextSwitchTo(oldcontext);
5252 : }
5253 :
5254 : /* stuff done on every call of the function */
5255 3144 : funcctx = SRF_PERCALL_SETUP();
5256 3144 : pindex = (int *) funcctx->user_fctx;
5257 :
5258 3144 : if (zoneabbrevtbl == NULL ||
5259 3144 : *pindex >= zoneabbrevtbl->numabbrevs)
5260 16 : SRF_RETURN_DONE(funcctx);
5261 :
5262 3128 : tp = zoneabbrevtbl->abbrevs + *pindex;
5263 :
5264 3128 : switch (tp->type)
5265 : {
5266 1564 : case TZ:
5267 1564 : gmtoffset = tp->value;
5268 1564 : is_dst = false;
5269 1564 : break;
5270 768 : case DTZ:
5271 768 : gmtoffset = tp->value;
5272 768 : is_dst = true;
5273 768 : break;
5274 796 : case DYNTZ:
5275 : {
5276 : /* Determine the current meaning of the abbrev */
5277 : pg_tz *tzp;
5278 : DateTimeErrorExtra extra;
5279 : TimestampTz now;
5280 : int isdst;
5281 :
5282 796 : tzp = FetchDynamicTimeZone(zoneabbrevtbl, tp, &extra);
5283 796 : if (tzp == NULL)
5284 0 : DateTimeParseError(DTERR_BAD_ZONE_ABBREV, &extra,
5285 : NULL, NULL, NULL);
5286 796 : now = GetCurrentTransactionStartTimestamp();
5287 1592 : gmtoffset = -DetermineTimeZoneAbbrevOffsetTS(now,
5288 796 : tp->token,
5289 : tzp,
5290 : &isdst);
5291 796 : is_dst = (bool) isdst;
5292 796 : break;
5293 : }
5294 0 : default:
5295 0 : elog(ERROR, "unrecognized timezone type %d", (int) tp->type);
5296 : gmtoffset = 0; /* keep compiler quiet */
5297 : is_dst = false;
5298 : break;
5299 : }
5300 :
5301 : /*
5302 : * Convert name to text, using upcasing conversion that is the inverse of
5303 : * what ParseDateTime() uses.
5304 : */
5305 3128 : strlcpy(buffer, tp->token, sizeof(buffer));
5306 14496 : for (p = (unsigned char *) buffer; *p; p++)
5307 11368 : *p = pg_toupper(*p);
5308 :
5309 3128 : values[0] = CStringGetTextDatum(buffer);
5310 :
5311 : /* Convert offset (in seconds) to an interval; can't overflow */
5312 12512 : MemSet(&itm_in, 0, sizeof(struct pg_itm_in));
5313 3128 : itm_in.tm_usec = (int64) gmtoffset * USECS_PER_SEC;
5314 3128 : resInterval = palloc_object(Interval);
5315 3128 : (void) itmin2interval(&itm_in, resInterval);
5316 3128 : values[1] = IntervalPGetDatum(resInterval);
5317 :
5318 3128 : values[2] = BoolGetDatum(is_dst);
5319 :
5320 3128 : (*pindex)++;
5321 :
5322 3128 : tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
5323 3128 : result = HeapTupleGetDatum(tuple);
5324 :
5325 3128 : SRF_RETURN_NEXT(funcctx, result);
5326 : }
5327 :
5328 : /*
5329 : * This set-returning function reads all the available full time zones
5330 : * and returns a set of (name, abbrev, utc_offset, is_dst).
5331 : */
5332 : Datum
5333 10 : pg_timezone_names(PG_FUNCTION_ARGS)
5334 : {
5335 10 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
5336 : pg_tzenum *tzenum;
5337 : pg_tz *tz;
5338 : Datum values[4];
5339 10 : bool nulls[4] = {0};
5340 : int tzoff;
5341 : struct pg_tm tm;
5342 : fsec_t fsec;
5343 : const char *tzn;
5344 : Interval *resInterval;
5345 : struct pg_itm_in itm_in;
5346 :
5347 10 : InitMaterializedSRF(fcinfo, 0);
5348 :
5349 : /* initialize timezone scanning code */
5350 10 : tzenum = pg_tzenumerate_start();
5351 :
5352 : /* search for another zone to display */
5353 : for (;;)
5354 : {
5355 5990 : tz = pg_tzenumerate_next(tzenum);
5356 5990 : if (!tz)
5357 10 : break;
5358 :
5359 : /* Convert now() to local time in this zone */
5360 5980 : if (timestamp2tm(GetCurrentTransactionStartTimestamp(),
5361 : &tzoff, &tm, &fsec, &tzn, tz) != 0)
5362 0 : continue; /* ignore if conversion fails */
5363 :
5364 : /*
5365 : * IANA's rather silly "Factory" time zone used to emit ridiculously
5366 : * long "abbreviations" such as "Local time zone must be set--see zic
5367 : * manual page" or "Local time zone must be set--use tzsetup". While
5368 : * modern versions of tzdb emit the much saner "-00", it seems some
5369 : * benighted packagers are hacking the IANA data so that it continues
5370 : * to produce these strings. To prevent producing a weirdly wide
5371 : * abbrev column, reject ridiculously long abbreviations.
5372 : */
5373 5980 : if (tzn && strlen(tzn) > 31)
5374 0 : continue;
5375 :
5376 5980 : values[0] = CStringGetTextDatum(pg_get_timezone_name(tz));
5377 5980 : values[1] = CStringGetTextDatum(tzn ? tzn : "");
5378 :
5379 : /* Convert tzoff to an interval; can't overflow */
5380 23920 : MemSet(&itm_in, 0, sizeof(struct pg_itm_in));
5381 5980 : itm_in.tm_usec = (int64) -tzoff * USECS_PER_SEC;
5382 5980 : resInterval = palloc_object(Interval);
5383 5980 : (void) itmin2interval(&itm_in, resInterval);
5384 5980 : values[2] = IntervalPGetDatum(resInterval);
5385 :
5386 5980 : values[3] = BoolGetDatum(tm.tm_isdst > 0);
5387 :
5388 5980 : tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
5389 : }
5390 :
5391 10 : pg_tzenumerate_end(tzenum);
5392 10 : return (Datum) 0;
5393 : }
|