LCOV - code coverage report
Current view: top level - src/backend/utils/adt - datetime.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 1856 2143 86.6 %
Date: 2024-11-21 08:14:44 Functions: 62 62 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14