LCOV - code coverage report
Current view: top level - src/backend/utils/adt - formatting.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 85.9 % 2415 2074
Test Date: 2026-02-17 17:20:33 Functions: 94.4 % 71 67
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  * formatting.c
       3              :  *
       4              :  * src/backend/utils/adt/formatting.c
       5              :  *
       6              :  *
       7              :  *   Portions Copyright (c) 1999-2026, PostgreSQL Global Development Group
       8              :  *
       9              :  *
      10              :  *   TO_CHAR(); TO_TIMESTAMP(); TO_DATE(); TO_NUMBER();
      11              :  *
      12              :  *   The PostgreSQL routines for a timestamp/int/float/numeric formatting,
      13              :  *   inspired by the Oracle TO_CHAR() / TO_DATE() / TO_NUMBER() routines.
      14              :  *
      15              :  *
      16              :  *   Cache & Memory:
      17              :  *  Routines use (itself) internal cache for format pictures.
      18              :  *
      19              :  *  The cache uses a static buffer and is persistent across transactions.  If
      20              :  *  the format-picture is bigger than the cache buffer, the parser is called
      21              :  *  always.
      22              :  *
      23              :  *   NOTE for Number version:
      24              :  *  All in this version is implemented as keywords ( => not used
      25              :  *  suffixes), because a format picture is for *one* item (number)
      26              :  *  only. It not is as a timestamp version, where each keyword (can)
      27              :  *  has suffix.
      28              :  *
      29              :  *   NOTE for Timestamp routines:
      30              :  *  In this module the POSIX 'struct tm' type is *not* used, but rather
      31              :  *  PgSQL type, which has tm_mon based on one (*non* zero) and
      32              :  *  year *not* based on 1900, but is used full year number.
      33              :  *  Module supports AD / BC / AM / PM.
      34              :  *
      35              :  *  Supported types for to_char():
      36              :  *
      37              :  *      Timestamp, Numeric, int4, int8, float4, float8
      38              :  *
      39              :  *  Supported types for reverse conversion:
      40              :  *
      41              :  *      Timestamp   - to_timestamp()
      42              :  *      Date        - to_date()
      43              :  *      Numeric     - to_number()
      44              :  *
      45              :  *
      46              :  *  Karel Zak
      47              :  *
      48              :  * TODO
      49              :  *  - better number building (formatting) / parsing, now it isn't
      50              :  *        ideal code
      51              :  *  - use Assert()
      52              :  *  - add support for number spelling
      53              :  *  - add support for string to string formatting (we must be better
      54              :  *    than Oracle :-),
      55              :  *      to_char('Hello', 'X X X X X') -> 'H e l l o'
      56              :  *
      57              :  *-------------------------------------------------------------------------
      58              :  */
      59              : 
      60              : #ifdef DEBUG_TO_FROM_CHAR
      61              : #define DEBUG_elog_output   DEBUG3
      62              : #endif
      63              : 
      64              : #include "postgres.h"
      65              : 
      66              : #include <ctype.h>
      67              : #include <unistd.h>
      68              : #include <math.h>
      69              : #include <float.h>
      70              : #include <limits.h>
      71              : 
      72              : #include "catalog/pg_type.h"
      73              : #include "common/int.h"
      74              : #include "mb/pg_wchar.h"
      75              : #include "nodes/miscnodes.h"
      76              : #include "parser/scansup.h"
      77              : #include "utils/builtins.h"
      78              : #include "utils/date.h"
      79              : #include "utils/datetime.h"
      80              : #include "utils/formatting.h"
      81              : #include "utils/memutils.h"
      82              : #include "utils/numeric.h"
      83              : #include "utils/pg_locale.h"
      84              : #include "varatt.h"
      85              : 
      86              : 
      87              : /*
      88              :  * Routines flags
      89              :  */
      90              : #define DCH_FLAG        0x1     /* DATE-TIME flag   */
      91              : #define NUM_FLAG        0x2     /* NUMBER flag  */
      92              : #define STD_FLAG        0x4     /* STANDARD flag    */
      93              : 
      94              : /*
      95              :  * KeyWord Index (ascii from position 32 (' ') to 126 (~))
      96              :  */
      97              : #define KeyWord_INDEX_SIZE      ('~' - ' ')
      98              : #define KeyWord_INDEX_FILTER(_c)    ((_c) <= ' ' || (_c) >= '~' ? 0 : 1)
      99              : 
     100              : /*
     101              :  * Maximal length of one node
     102              :  */
     103              : #define DCH_MAX_ITEM_SIZ       12   /* max localized day name       */
     104              : #define NUM_MAX_ITEM_SIZ        8   /* roman number (RN has 15 chars)   */
     105              : 
     106              : 
     107              : /*
     108              :  * Format parser structs
     109              :  */
     110              : 
     111              : enum KeySuffixType
     112              : {
     113              :     SUFFTYPE_PREFIX = 1,
     114              :     SUFFTYPE_POSTFIX = 2,
     115              : };
     116              : 
     117              : typedef struct
     118              : {
     119              :     const char *name;           /* suffix string        */
     120              :     size_t      len;            /* suffix length        */
     121              :     int         id;             /* used in node->suffix */
     122              :     enum KeySuffixType type;    /* prefix / postfix     */
     123              : } KeySuffix;
     124              : 
     125              : /*
     126              :  * FromCharDateMode
     127              :  *
     128              :  * This value is used to nominate one of several distinct (and mutually
     129              :  * exclusive) date conventions that a keyword can belong to.
     130              :  */
     131              : typedef enum
     132              : {
     133              :     FROM_CHAR_DATE_NONE = 0,    /* Value does not affect date mode. */
     134              :     FROM_CHAR_DATE_GREGORIAN,   /* Gregorian (day, month, year) style date */
     135              :     FROM_CHAR_DATE_ISOWEEK,     /* ISO 8601 week date */
     136              : } FromCharDateMode;
     137              : 
     138              : typedef struct
     139              : {
     140              :     const char *name;
     141              :     size_t      len;
     142              :     int         id;
     143              :     bool        is_digit;
     144              :     FromCharDateMode date_mode;
     145              : } KeyWord;
     146              : 
     147              : enum FormatNodeType
     148              : {
     149              :     NODE_TYPE_END = 1,
     150              :     NODE_TYPE_ACTION = 2,
     151              :     NODE_TYPE_CHAR = 3,
     152              :     NODE_TYPE_SEPARATOR = 4,
     153              :     NODE_TYPE_SPACE = 5,
     154              : };
     155              : 
     156              : typedef struct
     157              : {
     158              :     enum FormatNodeType type;
     159              :     char        character[MAX_MULTIBYTE_CHAR_LEN + 1];  /* if type is CHAR */
     160              :     uint8       suffix;         /* keyword prefix/suffix code, if any
     161              :                                  * (DCH_SUFFIX_*) */
     162              :     const KeyWord *key;         /* if type is ACTION */
     163              : } FormatNode;
     164              : 
     165              : 
     166              : /*
     167              :  * Full months
     168              :  */
     169              : static const char *const months_full[] = {
     170              :     "January", "February", "March", "April", "May", "June", "July",
     171              :     "August", "September", "October", "November", "December", NULL
     172              : };
     173              : 
     174              : static const char *const days_short[] = {
     175              :     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
     176              : };
     177              : 
     178              : /*
     179              :  * AD / BC
     180              :  *
     181              :  *  There is no 0 AD.  Years go from 1 BC to 1 AD, so we make it
     182              :  *  positive and map year == -1 to year zero, and shift all negative
     183              :  *  years up one.  For interval years, we just return the year.
     184              :  */
     185              : #define ADJUST_YEAR(year, is_interval)  ((is_interval) ? (year) : ((year) <= 0 ? -((year) - 1) : (year)))
     186              : 
     187              : #define A_D_STR     "A.D."
     188              : #define a_d_STR     "a.d."
     189              : #define AD_STR      "AD"
     190              : #define ad_STR      "ad"
     191              : 
     192              : #define B_C_STR     "B.C."
     193              : #define b_c_STR     "b.c."
     194              : #define BC_STR      "BC"
     195              : #define bc_STR      "bc"
     196              : 
     197              : /*
     198              :  * AD / BC strings for seq_search.
     199              :  *
     200              :  * These are given in two variants, a long form with periods and a standard
     201              :  * form without.
     202              :  *
     203              :  * The array is laid out such that matches for AD have an even index, and
     204              :  * matches for BC have an odd index.  So the boolean value for BC is given by
     205              :  * taking the array index of the match, modulo 2.
     206              :  */
     207              : static const char *const adbc_strings[] = {ad_STR, bc_STR, AD_STR, BC_STR, NULL};
     208              : static const char *const adbc_strings_long[] = {a_d_STR, b_c_STR, A_D_STR, B_C_STR, NULL};
     209              : 
     210              : /*
     211              :  * AM / PM
     212              :  */
     213              : #define A_M_STR     "A.M."
     214              : #define a_m_STR     "a.m."
     215              : #define AM_STR      "AM"
     216              : #define am_STR      "am"
     217              : 
     218              : #define P_M_STR     "P.M."
     219              : #define p_m_STR     "p.m."
     220              : #define PM_STR      "PM"
     221              : #define pm_STR      "pm"
     222              : 
     223              : /*
     224              :  * AM / PM strings for seq_search.
     225              :  *
     226              :  * These are given in two variants, a long form with periods and a standard
     227              :  * form without.
     228              :  *
     229              :  * The array is laid out such that matches for AM have an even index, and
     230              :  * matches for PM have an odd index.  So the boolean value for PM is given by
     231              :  * taking the array index of the match, modulo 2.
     232              :  */
     233              : static const char *const ampm_strings[] = {am_STR, pm_STR, AM_STR, PM_STR, NULL};
     234              : static const char *const ampm_strings_long[] = {a_m_STR, p_m_STR, A_M_STR, P_M_STR, NULL};
     235              : 
     236              : /*
     237              :  * Months in roman-numeral
     238              :  * (Must be in reverse order for seq_search (in FROM_CHAR), because
     239              :  *  'VIII' must have higher precedence than 'V')
     240              :  */
     241              : static const char *const rm_months_upper[] =
     242              : {"XII", "XI", "X", "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I", NULL};
     243              : 
     244              : static const char *const rm_months_lower[] =
     245              : {"xii", "xi", "x", "ix", "viii", "vii", "vi", "v", "iv", "iii", "ii", "i", NULL};
     246              : 
     247              : /*
     248              :  * Roman numerals
     249              :  */
     250              : static const char *const rm1[] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", NULL};
     251              : static const char *const rm10[] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", NULL};
     252              : static const char *const rm100[] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", NULL};
     253              : 
     254              : /*
     255              :  * MACRO: Check if the current and next characters form a valid subtraction
     256              :  * combination for roman numerals.
     257              :  */
     258              : #define IS_VALID_SUB_COMB(curr, next) \
     259              :     (((curr) == 'I' && ((next) == 'V' || (next) == 'X')) || \
     260              :      ((curr) == 'X' && ((next) == 'L' || (next) == 'C')) || \
     261              :      ((curr) == 'C' && ((next) == 'D' || (next) == 'M')))
     262              : 
     263              : /*
     264              :  * MACRO: Roman numeral value, or 0 if character isn't a roman numeral.
     265              :  */
     266              : #define ROMAN_VAL(r) \
     267              :     ((r) == 'I' ? 1 : \
     268              :      (r) == 'V' ? 5 : \
     269              :      (r) == 'X' ? 10 : \
     270              :      (r) == 'L' ? 50 : \
     271              :      (r) == 'C' ? 100 : \
     272              :      (r) == 'D' ? 500 : \
     273              :      (r) == 'M' ? 1000 : 0)
     274              : 
     275              : /*
     276              :  * 'MMMDCCCLXXXVIII' (3888) is the longest valid roman numeral (15 characters).
     277              :  */
     278              : #define MAX_ROMAN_LEN   15
     279              : 
     280              : /*
     281              :  * Ordinal postfixes
     282              :  */
     283              : static const char *const numTH[] = {"ST", "ND", "RD", "TH", NULL};
     284              : static const char *const numth[] = {"st", "nd", "rd", "th", NULL};
     285              : 
     286              : /*
     287              :  * Flags & Options:
     288              :  */
     289              : enum TH_Case
     290              : {
     291              :     TH_UPPER = 1,
     292              :     TH_LOWER = 2,
     293              : };
     294              : 
     295              : enum NUMDesc_lsign
     296              : {
     297              :     NUM_LSIGN_PRE = -1,
     298              :     NUM_LSIGN_POST = 1,
     299              :     NUM_LSIGN_NONE = 0,
     300              : };
     301              : 
     302              : /*
     303              :  * Number description struct
     304              :  */
     305              : typedef struct
     306              : {
     307              :     int         pre;            /* (count) numbers before decimal */
     308              :     int         post;           /* (count) numbers after decimal */
     309              :     enum NUMDesc_lsign lsign;   /* want locales sign */
     310              :     int         flag;           /* number parameters (NUM_F_*) */
     311              :     int         pre_lsign_num;  /* tmp value for lsign */
     312              :     int         multi;          /* multiplier for 'V' */
     313              :     int         zero_start;     /* position of first zero */
     314              :     int         zero_end;       /* position of last zero */
     315              :     bool        need_locale;    /* needs it locale */
     316              : } NUMDesc;
     317              : 
     318              : /*
     319              :  * Flags for NUMBER version
     320              :  */
     321              : #define NUM_F_DECIMAL       (1 << 1)
     322              : #define NUM_F_LDECIMAL      (1 << 2)
     323              : #define NUM_F_ZERO          (1 << 3)
     324              : #define NUM_F_BLANK         (1 << 4)
     325              : #define NUM_F_FILLMODE      (1 << 5)
     326              : #define NUM_F_LSIGN         (1 << 6)
     327              : #define NUM_F_BRACKET       (1 << 7)
     328              : #define NUM_F_MINUS         (1 << 8)
     329              : #define NUM_F_PLUS          (1 << 9)
     330              : #define NUM_F_ROMAN         (1 << 10)
     331              : #define NUM_F_MULTI         (1 << 11)
     332              : #define NUM_F_PLUS_POST     (1 << 12)
     333              : #define NUM_F_MINUS_POST    (1 << 13)
     334              : #define NUM_F_EEEE          (1 << 14)
     335              : 
     336              : /*
     337              :  * Tests
     338              :  */
     339              : #define IS_DECIMAL(_f)  ((_f)->flag & NUM_F_DECIMAL)
     340              : #define IS_LDECIMAL(_f) ((_f)->flag & NUM_F_LDECIMAL)
     341              : #define IS_ZERO(_f) ((_f)->flag & NUM_F_ZERO)
     342              : #define IS_BLANK(_f)    ((_f)->flag & NUM_F_BLANK)
     343              : #define IS_FILLMODE(_f) ((_f)->flag & NUM_F_FILLMODE)
     344              : #define IS_BRACKET(_f)  ((_f)->flag & NUM_F_BRACKET)
     345              : #define IS_MINUS(_f)    ((_f)->flag & NUM_F_MINUS)
     346              : #define IS_LSIGN(_f)    ((_f)->flag & NUM_F_LSIGN)
     347              : #define IS_PLUS(_f) ((_f)->flag & NUM_F_PLUS)
     348              : #define IS_ROMAN(_f)    ((_f)->flag & NUM_F_ROMAN)
     349              : #define IS_MULTI(_f)    ((_f)->flag & NUM_F_MULTI)
     350              : #define IS_EEEE(_f)     ((_f)->flag & NUM_F_EEEE)
     351              : 
     352              : /*
     353              :  * Format picture cache
     354              :  *
     355              :  * We will cache datetime format pictures up to DCH_CACHE_SIZE bytes long;
     356              :  * likewise number format pictures up to NUM_CACHE_SIZE bytes long.
     357              :  *
     358              :  * For simplicity, the cache entries are fixed-size, so they allow for the
     359              :  * worst case of a FormatNode for each byte in the picture string.
     360              :  *
     361              :  * The CACHE_SIZE constants are computed to make sizeof(DCHCacheEntry) and
     362              :  * sizeof(NUMCacheEntry) be powers of 2, or just less than that, so that
     363              :  * we don't waste too much space by palloc'ing them individually.  Be sure
     364              :  * to adjust those macros if you add fields to those structs.
     365              :  *
     366              :  * The max number of entries in each cache is DCH_CACHE_ENTRIES
     367              :  * resp. NUM_CACHE_ENTRIES.
     368              :  */
     369              : #define DCH_CACHE_OVERHEAD \
     370              :     MAXALIGN(sizeof(bool) + sizeof(int))
     371              : #define NUM_CACHE_OVERHEAD \
     372              :     MAXALIGN(sizeof(bool) + sizeof(int) + sizeof(NUMDesc))
     373              : 
     374              : #define DCH_CACHE_SIZE \
     375              :     ((2048 - DCH_CACHE_OVERHEAD) / (sizeof(FormatNode) + sizeof(char)) - 1)
     376              : #define NUM_CACHE_SIZE \
     377              :     ((1024 - NUM_CACHE_OVERHEAD) / (sizeof(FormatNode) + sizeof(char)) - 1)
     378              : 
     379              : #define DCH_CACHE_ENTRIES   20
     380              : #define NUM_CACHE_ENTRIES   20
     381              : 
     382              : typedef struct
     383              : {
     384              :     FormatNode  format[DCH_CACHE_SIZE + 1];
     385              :     char        str[DCH_CACHE_SIZE + 1];
     386              :     bool        std;
     387              :     bool        valid;
     388              :     int         age;
     389              : } DCHCacheEntry;
     390              : 
     391              : typedef struct
     392              : {
     393              :     FormatNode  format[NUM_CACHE_SIZE + 1];
     394              :     char        str[NUM_CACHE_SIZE + 1];
     395              :     bool        valid;
     396              :     int         age;
     397              :     NUMDesc     Num;
     398              : } NUMCacheEntry;
     399              : 
     400              : /* global cache for date/time format pictures */
     401              : static DCHCacheEntry *DCHCache[DCH_CACHE_ENTRIES];
     402              : static int  n_DCHCache = 0;     /* current number of entries */
     403              : static int  DCHCounter = 0;     /* aging-event counter */
     404              : 
     405              : /* global cache for number format pictures */
     406              : static NUMCacheEntry *NUMCache[NUM_CACHE_ENTRIES];
     407              : static int  n_NUMCache = 0;     /* current number of entries */
     408              : static int  NUMCounter = 0;     /* aging-event counter */
     409              : 
     410              : /*
     411              :  * For char->date/time conversion
     412              :  */
     413              : typedef struct
     414              : {
     415              :     FromCharDateMode mode;
     416              :     int         hh;
     417              :     int         pm;
     418              :     int         mi;
     419              :     int         ss;
     420              :     int         ssss;
     421              :     int         d;              /* stored as 1-7, Sunday = 1, 0 means missing */
     422              :     int         dd;
     423              :     int         ddd;
     424              :     int         mm;
     425              :     int         ms;
     426              :     int         year;
     427              :     int         bc;
     428              :     int         ww;
     429              :     int         w;
     430              :     int         cc;
     431              :     int         j;
     432              :     int         us;
     433              :     int         yysz;           /* is it YY or YYYY ? */
     434              :     bool        clock_12_hour;  /* 12 or 24 hour clock? */
     435              :     int         tzsign;         /* +1, -1, or 0 if no TZH/TZM fields */
     436              :     int         tzh;
     437              :     int         tzm;
     438              :     int         ff;             /* fractional precision */
     439              :     bool        has_tz;         /* was there a TZ field? */
     440              :     int         gmtoffset;      /* GMT offset of fixed-offset zone abbrev */
     441              :     pg_tz      *tzp;            /* pg_tz for dynamic abbrev */
     442              :     const char *abbrev;         /* dynamic abbrev */
     443              : } TmFromChar;
     444              : 
     445              : struct fmt_tz                   /* do_to_timestamp's timezone info output */
     446              : {
     447              :     bool        has_tz;         /* was there any TZ/TZH/TZM field? */
     448              :     int         gmtoffset;      /* GMT offset in seconds */
     449              : };
     450              : 
     451              : /*
     452              :  * Debug
     453              :  */
     454              : #ifdef DEBUG_TO_FROM_CHAR
     455              : #define DEBUG_TMFC(_X) \
     456              :         elog(DEBUG_elog_output, "TMFC:\nmode %d\nhh %d\npm %d\nmi %d\nss %d\nssss %d\nd %d\ndd %d\nddd %d\nmm %d\nms: %d\nyear %d\nbc %d\nww %d\nw %d\ncc %d\nj %d\nus: %d\nyysz: %d\nclock: %d", \
     457              :             (_X)->mode, (_X)->hh, (_X)->pm, (_X)->mi, (_X)->ss, (_X)->ssss, \
     458              :             (_X)->d, (_X)->dd, (_X)->ddd, (_X)->mm, (_X)->ms, (_X)->year, \
     459              :             (_X)->bc, (_X)->ww, (_X)->w, (_X)->cc, (_X)->j, (_X)->us, \
     460              :             (_X)->yysz, (_X)->clock_12_hour)
     461              : #define DEBUG_TM(_X) \
     462              :         elog(DEBUG_elog_output, "TM:\nsec %d\nyear %d\nmin %d\nwday %d\nhour %d\nyday %d\nmday %d\nnisdst %d\nmon %d\n",\
     463              :             (_X)->tm_sec, (_X)->tm_year,\
     464              :             (_X)->tm_min, (_X)->tm_wday, (_X)->tm_hour, (_X)->tm_yday,\
     465              :             (_X)->tm_mday, (_X)->tm_isdst, (_X)->tm_mon)
     466              : #else
     467              : #define DEBUG_TMFC(_X)
     468              : #define DEBUG_TM(_X)
     469              : #endif
     470              : 
     471              : /*
     472              :  * Datetime to char conversion
     473              :  *
     474              :  * To support intervals as well as timestamps, we use a custom "tm" struct
     475              :  * that is almost like struct pg_tm, but has a 64-bit tm_hour field.
     476              :  * We omit the tm_isdst and tm_zone fields, which are not used here.
     477              :  */
     478              : struct fmt_tm
     479              : {
     480              :     int         tm_sec;
     481              :     int         tm_min;
     482              :     int64       tm_hour;
     483              :     int         tm_mday;
     484              :     int         tm_mon;
     485              :     int         tm_year;
     486              :     int         tm_wday;
     487              :     int         tm_yday;
     488              :     long int    tm_gmtoff;
     489              : };
     490              : 
     491              : typedef struct TmToChar
     492              : {
     493              :     struct fmt_tm tm;           /* almost the classic 'tm' struct */
     494              :     fsec_t      fsec;           /* fractional seconds */
     495              :     const char *tzn;            /* timezone */
     496              : } TmToChar;
     497              : 
     498              : #define tmtcTm(_X)  (&(_X)->tm)
     499              : #define tmtcTzn(_X) ((_X)->tzn)
     500              : #define tmtcFsec(_X)    ((_X)->fsec)
     501              : 
     502              : /* Note: this is used to copy pg_tm to fmt_tm, so not quite a bitwise copy */
     503              : #define COPY_tm(_DST, _SRC) \
     504              : do {    \
     505              :     (_DST)->tm_sec = (_SRC)->tm_sec; \
     506              :     (_DST)->tm_min = (_SRC)->tm_min; \
     507              :     (_DST)->tm_hour = (_SRC)->tm_hour; \
     508              :     (_DST)->tm_mday = (_SRC)->tm_mday; \
     509              :     (_DST)->tm_mon = (_SRC)->tm_mon; \
     510              :     (_DST)->tm_year = (_SRC)->tm_year; \
     511              :     (_DST)->tm_wday = (_SRC)->tm_wday; \
     512              :     (_DST)->tm_yday = (_SRC)->tm_yday; \
     513              :     (_DST)->tm_gmtoff = (_SRC)->tm_gmtoff; \
     514              : } while(0)
     515              : 
     516              : /* Caution: this is used to zero both pg_tm and fmt_tm structs */
     517              : #define ZERO_tm(_X) \
     518              : do {    \
     519              :     memset(_X, 0, sizeof(*(_X))); \
     520              :     (_X)->tm_mday = (_X)->tm_mon = 1; \
     521              : } while(0)
     522              : 
     523              : #define ZERO_tmtc(_X) \
     524              : do { \
     525              :     ZERO_tm( tmtcTm(_X) ); \
     526              :     tmtcFsec(_X) = 0; \
     527              :     tmtcTzn(_X) = NULL; \
     528              : } while(0)
     529              : 
     530              : /*
     531              :  *  to_char(time) appears to to_char() as an interval, so this check
     532              :  *  is really for interval and time data types.
     533              :  */
     534              : #define INVALID_FOR_INTERVAL  \
     535              : do { \
     536              :     if (is_interval) \
     537              :         ereport(ERROR, \
     538              :                 (errcode(ERRCODE_INVALID_DATETIME_FORMAT), \
     539              :                  errmsg("invalid format specification for an interval value"), \
     540              :                  errhint("Intervals are not tied to specific calendar dates."))); \
     541              : } while(0)
     542              : 
     543              : /*****************************************************************************
     544              :  *          KeyWord definitions
     545              :  *****************************************************************************/
     546              : 
     547              : /*
     548              :  * Suffixes (FormatNode.suffix is an OR of these codes)
     549              :  */
     550              : #define DCH_SUFFIX_FM   0x01
     551              : #define DCH_SUFFIX_TH   0x02
     552              : #define DCH_SUFFIX_th   0x04
     553              : #define DCH_SUFFIX_SP   0x08
     554              : #define DCH_SUFFIX_TM   0x10
     555              : 
     556              : /*
     557              :  * Suffix tests
     558              :  */
     559              : static inline bool
     560       147594 : IS_SUFFIX_TH(uint8 _s)
     561              : {
     562       147594 :     return (_s & DCH_SUFFIX_TH);
     563              : }
     564              : 
     565              : static inline bool
     566       147213 : IS_SUFFIX_th(uint8 _s)
     567              : {
     568       147213 :     return (_s & DCH_SUFFIX_th);
     569              : }
     570              : 
     571              : static inline bool
     572       147594 : IS_SUFFIX_THth(uint8 _s)
     573              : {
     574       147594 :     return IS_SUFFIX_TH(_s) || IS_SUFFIX_th(_s);
     575              : }
     576              : 
     577              : static inline enum TH_Case
     578         1143 : SUFFIX_TH_TYPE(uint8 _s)
     579              : {
     580         1143 :     return _s & DCH_SUFFIX_TH ? TH_UPPER : TH_LOWER;
     581              : }
     582              : 
     583              : /* Oracle toggles FM behavior, we don't; see docs. */
     584              : static inline bool
     585        87450 : IS_SUFFIX_FM(uint8 _s)
     586              : {
     587        87450 :     return (_s & DCH_SUFFIX_FM);
     588              : }
     589              : 
     590              : static inline bool
     591         6981 : IS_SUFFIX_TM(uint8 _s)
     592              : {
     593         6981 :     return (_s & DCH_SUFFIX_TM);
     594              : }
     595              : 
     596              : /*
     597              :  * Suffixes definition for DATE-TIME TO/FROM CHAR
     598              :  */
     599              : #define TM_SUFFIX_LEN   2
     600              : 
     601              : static const KeySuffix DCH_suff[] = {
     602              :     {"FM", 2, DCH_SUFFIX_FM, SUFFTYPE_PREFIX},
     603              :     {"fm", 2, DCH_SUFFIX_FM, SUFFTYPE_PREFIX},
     604              :     {"TM", TM_SUFFIX_LEN, DCH_SUFFIX_TM, SUFFTYPE_PREFIX},
     605              :     {"tm", 2, DCH_SUFFIX_TM, SUFFTYPE_PREFIX},
     606              :     {"TH", 2, DCH_SUFFIX_TH, SUFFTYPE_POSTFIX},
     607              :     {"th", 2, DCH_SUFFIX_th, SUFFTYPE_POSTFIX},
     608              :     {"SP", 2, DCH_SUFFIX_SP, SUFFTYPE_POSTFIX},
     609              :     /* last */
     610              :     {NULL, 0, 0, 0}
     611              : };
     612              : 
     613              : 
     614              : /*
     615              :  * Format-pictures (KeyWord).
     616              :  *
     617              :  * The KeyWord field; alphabetic sorted, *BUT* strings alike is sorted
     618              :  *        complicated -to-> easy:
     619              :  *
     620              :  *  (example: "DDD","DD","Day","D" )
     621              :  *
     622              :  * (this specific sort needs the algorithm for sequential search for strings,
     623              :  * which not has exact end; -> How keyword is in "HH12blabla" ? - "HH"
     624              :  * or "HH12"? You must first try "HH12", because "HH" is in string, but
     625              :  * it is not good.
     626              :  *
     627              :  * (!)
     628              :  *   - Position for the keyword is similar as position in the enum DCH/NUM_poz.
     629              :  * (!)
     630              :  *
     631              :  * For fast search is used the 'int index[]', index is ascii table from position
     632              :  * 32 (' ') to 126 (~), in this index is DCH_ / NUM_ enums for each ASCII
     633              :  * position or -1 if char is not used in the KeyWord. Search example for
     634              :  * string "MM":
     635              :  *  1)  see in index to index['M' - 32],
     636              :  *  2)  take keywords position (enum DCH_MI) from index
     637              :  *  3)  run sequential search in keywords[] from this position
     638              :  */
     639              : 
     640              : typedef enum
     641              : {
     642              :     DCH_A_D,
     643              :     DCH_A_M,
     644              :     DCH_AD,
     645              :     DCH_AM,
     646              :     DCH_B_C,
     647              :     DCH_BC,
     648              :     DCH_CC,
     649              :     DCH_DAY,
     650              :     DCH_DDD,
     651              :     DCH_DD,
     652              :     DCH_DY,
     653              :     DCH_Day,
     654              :     DCH_Dy,
     655              :     DCH_D,
     656              :     DCH_FF1,                    /* FFn codes must be consecutive */
     657              :     DCH_FF2,
     658              :     DCH_FF3,
     659              :     DCH_FF4,
     660              :     DCH_FF5,
     661              :     DCH_FF6,
     662              :     DCH_FX,                     /* global suffix */
     663              :     DCH_HH24,
     664              :     DCH_HH12,
     665              :     DCH_HH,
     666              :     DCH_IDDD,
     667              :     DCH_ID,
     668              :     DCH_IW,
     669              :     DCH_IYYY,
     670              :     DCH_IYY,
     671              :     DCH_IY,
     672              :     DCH_I,
     673              :     DCH_J,
     674              :     DCH_MI,
     675              :     DCH_MM,
     676              :     DCH_MONTH,
     677              :     DCH_MON,
     678              :     DCH_MS,
     679              :     DCH_Month,
     680              :     DCH_Mon,
     681              :     DCH_OF,
     682              :     DCH_P_M,
     683              :     DCH_PM,
     684              :     DCH_Q,
     685              :     DCH_RM,
     686              :     DCH_SSSSS,
     687              :     DCH_SSSS,
     688              :     DCH_SS,
     689              :     DCH_TZH,
     690              :     DCH_TZM,
     691              :     DCH_TZ,
     692              :     DCH_US,
     693              :     DCH_WW,
     694              :     DCH_W,
     695              :     DCH_Y_YYY,
     696              :     DCH_YYYY,
     697              :     DCH_YYY,
     698              :     DCH_YY,
     699              :     DCH_Y,
     700              :     DCH_a_d,
     701              :     DCH_a_m,
     702              :     DCH_ad,
     703              :     DCH_am,
     704              :     DCH_b_c,
     705              :     DCH_bc,
     706              :     DCH_cc,
     707              :     DCH_day,
     708              :     DCH_ddd,
     709              :     DCH_dd,
     710              :     DCH_dy,
     711              :     DCH_d,
     712              :     DCH_ff1,
     713              :     DCH_ff2,
     714              :     DCH_ff3,
     715              :     DCH_ff4,
     716              :     DCH_ff5,
     717              :     DCH_ff6,
     718              :     DCH_fx,
     719              :     DCH_hh24,
     720              :     DCH_hh12,
     721              :     DCH_hh,
     722              :     DCH_iddd,
     723              :     DCH_id,
     724              :     DCH_iw,
     725              :     DCH_iyyy,
     726              :     DCH_iyy,
     727              :     DCH_iy,
     728              :     DCH_i,
     729              :     DCH_j,
     730              :     DCH_mi,
     731              :     DCH_mm,
     732              :     DCH_month,
     733              :     DCH_mon,
     734              :     DCH_ms,
     735              :     DCH_of,
     736              :     DCH_p_m,
     737              :     DCH_pm,
     738              :     DCH_q,
     739              :     DCH_rm,
     740              :     DCH_sssss,
     741              :     DCH_ssss,
     742              :     DCH_ss,
     743              :     DCH_tzh,
     744              :     DCH_tzm,
     745              :     DCH_tz,
     746              :     DCH_us,
     747              :     DCH_ww,
     748              :     DCH_w,
     749              :     DCH_y_yyy,
     750              :     DCH_yyyy,
     751              :     DCH_yyy,
     752              :     DCH_yy,
     753              :     DCH_y,
     754              : 
     755              :     /* last */
     756              :     _DCH_last_
     757              : }           DCH_poz;
     758              : 
     759              : typedef enum
     760              : {
     761              :     NUM_COMMA,
     762              :     NUM_DEC,
     763              :     NUM_0,
     764              :     NUM_9,
     765              :     NUM_B,
     766              :     NUM_C,
     767              :     NUM_D,
     768              :     NUM_E,
     769              :     NUM_FM,
     770              :     NUM_G,
     771              :     NUM_L,
     772              :     NUM_MI,
     773              :     NUM_PL,
     774              :     NUM_PR,
     775              :     NUM_RN,
     776              :     NUM_SG,
     777              :     NUM_SP,
     778              :     NUM_S,
     779              :     NUM_TH,
     780              :     NUM_V,
     781              :     NUM_b,
     782              :     NUM_c,
     783              :     NUM_d,
     784              :     NUM_e,
     785              :     NUM_fm,
     786              :     NUM_g,
     787              :     NUM_l,
     788              :     NUM_mi,
     789              :     NUM_pl,
     790              :     NUM_pr,
     791              :     NUM_rn,
     792              :     NUM_sg,
     793              :     NUM_sp,
     794              :     NUM_s,
     795              :     NUM_th,
     796              :     NUM_v,
     797              : 
     798              :     /* last */
     799              :     _NUM_last_
     800              : }           NUM_poz;
     801              : 
     802              : /*
     803              :  * KeyWords for DATE-TIME version
     804              :  */
     805              : static const KeyWord DCH_keywords[] = {
     806              : /*  name, len, id, is_digit, date_mode */
     807              :     {"A.D.", 4, DCH_A_D, false, FROM_CHAR_DATE_NONE}, /* A */
     808              :     {"A.M.", 4, DCH_A_M, false, FROM_CHAR_DATE_NONE},
     809              :     {"AD", 2, DCH_AD, false, FROM_CHAR_DATE_NONE},
     810              :     {"AM", 2, DCH_AM, false, FROM_CHAR_DATE_NONE},
     811              :     {"B.C.", 4, DCH_B_C, false, FROM_CHAR_DATE_NONE}, /* B */
     812              :     {"BC", 2, DCH_BC, false, FROM_CHAR_DATE_NONE},
     813              :     {"CC", 2, DCH_CC, true, FROM_CHAR_DATE_NONE}, /* C */
     814              :     {"DAY", 3, DCH_DAY, false, FROM_CHAR_DATE_NONE},  /* D */
     815              :     {"DDD", 3, DCH_DDD, true, FROM_CHAR_DATE_GREGORIAN},
     816              :     {"DD", 2, DCH_DD, true, FROM_CHAR_DATE_GREGORIAN},
     817              :     {"DY", 2, DCH_DY, false, FROM_CHAR_DATE_NONE},
     818              :     {"Day", 3, DCH_Day, false, FROM_CHAR_DATE_NONE},
     819              :     {"Dy", 2, DCH_Dy, false, FROM_CHAR_DATE_NONE},
     820              :     {"D", 1, DCH_D, true, FROM_CHAR_DATE_GREGORIAN},
     821              :     {"FF1", 3, DCH_FF1, true, FROM_CHAR_DATE_NONE}, /* F */
     822              :     {"FF2", 3, DCH_FF2, true, FROM_CHAR_DATE_NONE},
     823              :     {"FF3", 3, DCH_FF3, true, FROM_CHAR_DATE_NONE},
     824              :     {"FF4", 3, DCH_FF4, true, FROM_CHAR_DATE_NONE},
     825              :     {"FF5", 3, DCH_FF5, true, FROM_CHAR_DATE_NONE},
     826              :     {"FF6", 3, DCH_FF6, true, FROM_CHAR_DATE_NONE},
     827              :     {"FX", 2, DCH_FX, false, FROM_CHAR_DATE_NONE},
     828              :     {"HH24", 4, DCH_HH24, true, FROM_CHAR_DATE_NONE}, /* H */
     829              :     {"HH12", 4, DCH_HH12, true, FROM_CHAR_DATE_NONE},
     830              :     {"HH", 2, DCH_HH, true, FROM_CHAR_DATE_NONE},
     831              :     {"IDDD", 4, DCH_IDDD, true, FROM_CHAR_DATE_ISOWEEK},  /* I */
     832              :     {"ID", 2, DCH_ID, true, FROM_CHAR_DATE_ISOWEEK},
     833              :     {"IW", 2, DCH_IW, true, FROM_CHAR_DATE_ISOWEEK},
     834              :     {"IYYY", 4, DCH_IYYY, true, FROM_CHAR_DATE_ISOWEEK},
     835              :     {"IYY", 3, DCH_IYY, true, FROM_CHAR_DATE_ISOWEEK},
     836              :     {"IY", 2, DCH_IY, true, FROM_CHAR_DATE_ISOWEEK},
     837              :     {"I", 1, DCH_I, true, FROM_CHAR_DATE_ISOWEEK},
     838              :     {"J", 1, DCH_J, true, FROM_CHAR_DATE_NONE}, /* J */
     839              :     {"MI", 2, DCH_MI, true, FROM_CHAR_DATE_NONE}, /* M */
     840              :     {"MM", 2, DCH_MM, true, FROM_CHAR_DATE_GREGORIAN},
     841              :     {"MONTH", 5, DCH_MONTH, false, FROM_CHAR_DATE_GREGORIAN},
     842              :     {"MON", 3, DCH_MON, false, FROM_CHAR_DATE_GREGORIAN},
     843              :     {"MS", 2, DCH_MS, true, FROM_CHAR_DATE_NONE},
     844              :     {"Month", 5, DCH_Month, false, FROM_CHAR_DATE_GREGORIAN},
     845              :     {"Mon", 3, DCH_Mon, false, FROM_CHAR_DATE_GREGORIAN},
     846              :     {"OF", 2, DCH_OF, false, FROM_CHAR_DATE_NONE},    /* O */
     847              :     {"P.M.", 4, DCH_P_M, false, FROM_CHAR_DATE_NONE}, /* P */
     848              :     {"PM", 2, DCH_PM, false, FROM_CHAR_DATE_NONE},
     849              :     {"Q", 1, DCH_Q, true, FROM_CHAR_DATE_NONE}, /* Q */
     850              :     {"RM", 2, DCH_RM, false, FROM_CHAR_DATE_GREGORIAN}, /* R */
     851              :     {"SSSSS", 5, DCH_SSSS, true, FROM_CHAR_DATE_NONE},    /* S */
     852              :     {"SSSS", 4, DCH_SSSS, true, FROM_CHAR_DATE_NONE},
     853              :     {"SS", 2, DCH_SS, true, FROM_CHAR_DATE_NONE},
     854              :     {"TZH", 3, DCH_TZH, false, FROM_CHAR_DATE_NONE},  /* T */
     855              :     {"TZM", 3, DCH_TZM, true, FROM_CHAR_DATE_NONE},
     856              :     {"TZ", 2, DCH_TZ, false, FROM_CHAR_DATE_NONE},
     857              :     {"US", 2, DCH_US, true, FROM_CHAR_DATE_NONE}, /* U */
     858              :     {"WW", 2, DCH_WW, true, FROM_CHAR_DATE_GREGORIAN},    /* W */
     859              :     {"W", 1, DCH_W, true, FROM_CHAR_DATE_GREGORIAN},
     860              :     {"Y,YYY", 5, DCH_Y_YYY, true, FROM_CHAR_DATE_GREGORIAN},  /* Y */
     861              :     {"YYYY", 4, DCH_YYYY, true, FROM_CHAR_DATE_GREGORIAN},
     862              :     {"YYY", 3, DCH_YYY, true, FROM_CHAR_DATE_GREGORIAN},
     863              :     {"YY", 2, DCH_YY, true, FROM_CHAR_DATE_GREGORIAN},
     864              :     {"Y", 1, DCH_Y, true, FROM_CHAR_DATE_GREGORIAN},
     865              :     {"a.d.", 4, DCH_a_d, false, FROM_CHAR_DATE_NONE}, /* a */
     866              :     {"a.m.", 4, DCH_a_m, false, FROM_CHAR_DATE_NONE},
     867              :     {"ad", 2, DCH_ad, false, FROM_CHAR_DATE_NONE},
     868              :     {"am", 2, DCH_am, false, FROM_CHAR_DATE_NONE},
     869              :     {"b.c.", 4, DCH_b_c, false, FROM_CHAR_DATE_NONE}, /* b */
     870              :     {"bc", 2, DCH_bc, false, FROM_CHAR_DATE_NONE},
     871              :     {"cc", 2, DCH_CC, true, FROM_CHAR_DATE_NONE}, /* c */
     872              :     {"day", 3, DCH_day, false, FROM_CHAR_DATE_NONE},  /* d */
     873              :     {"ddd", 3, DCH_DDD, true, FROM_CHAR_DATE_GREGORIAN},
     874              :     {"dd", 2, DCH_DD, true, FROM_CHAR_DATE_GREGORIAN},
     875              :     {"dy", 2, DCH_dy, false, FROM_CHAR_DATE_NONE},
     876              :     {"d", 1, DCH_D, true, FROM_CHAR_DATE_GREGORIAN},
     877              :     {"ff1", 3, DCH_FF1, true, FROM_CHAR_DATE_NONE}, /* f */
     878              :     {"ff2", 3, DCH_FF2, true, FROM_CHAR_DATE_NONE},
     879              :     {"ff3", 3, DCH_FF3, true, FROM_CHAR_DATE_NONE},
     880              :     {"ff4", 3, DCH_FF4, true, FROM_CHAR_DATE_NONE},
     881              :     {"ff5", 3, DCH_FF5, true, FROM_CHAR_DATE_NONE},
     882              :     {"ff6", 3, DCH_FF6, true, FROM_CHAR_DATE_NONE},
     883              :     {"fx", 2, DCH_FX, false, FROM_CHAR_DATE_NONE},
     884              :     {"hh24", 4, DCH_HH24, true, FROM_CHAR_DATE_NONE}, /* h */
     885              :     {"hh12", 4, DCH_HH12, true, FROM_CHAR_DATE_NONE},
     886              :     {"hh", 2, DCH_HH, true, FROM_CHAR_DATE_NONE},
     887              :     {"iddd", 4, DCH_IDDD, true, FROM_CHAR_DATE_ISOWEEK},  /* i */
     888              :     {"id", 2, DCH_ID, true, FROM_CHAR_DATE_ISOWEEK},
     889              :     {"iw", 2, DCH_IW, true, FROM_CHAR_DATE_ISOWEEK},
     890              :     {"iyyy", 4, DCH_IYYY, true, FROM_CHAR_DATE_ISOWEEK},
     891              :     {"iyy", 3, DCH_IYY, true, FROM_CHAR_DATE_ISOWEEK},
     892              :     {"iy", 2, DCH_IY, true, FROM_CHAR_DATE_ISOWEEK},
     893              :     {"i", 1, DCH_I, true, FROM_CHAR_DATE_ISOWEEK},
     894              :     {"j", 1, DCH_J, true, FROM_CHAR_DATE_NONE}, /* j */
     895              :     {"mi", 2, DCH_MI, true, FROM_CHAR_DATE_NONE}, /* m */
     896              :     {"mm", 2, DCH_MM, true, FROM_CHAR_DATE_GREGORIAN},
     897              :     {"month", 5, DCH_month, false, FROM_CHAR_DATE_GREGORIAN},
     898              :     {"mon", 3, DCH_mon, false, FROM_CHAR_DATE_GREGORIAN},
     899              :     {"ms", 2, DCH_MS, true, FROM_CHAR_DATE_NONE},
     900              :     {"of", 2, DCH_OF, false, FROM_CHAR_DATE_NONE},    /* o */
     901              :     {"p.m.", 4, DCH_p_m, false, FROM_CHAR_DATE_NONE}, /* p */
     902              :     {"pm", 2, DCH_pm, false, FROM_CHAR_DATE_NONE},
     903              :     {"q", 1, DCH_Q, true, FROM_CHAR_DATE_NONE}, /* q */
     904              :     {"rm", 2, DCH_rm, false, FROM_CHAR_DATE_GREGORIAN}, /* r */
     905              :     {"sssss", 5, DCH_SSSS, true, FROM_CHAR_DATE_NONE},    /* s */
     906              :     {"ssss", 4, DCH_SSSS, true, FROM_CHAR_DATE_NONE},
     907              :     {"ss", 2, DCH_SS, true, FROM_CHAR_DATE_NONE},
     908              :     {"tzh", 3, DCH_TZH, false, FROM_CHAR_DATE_NONE},  /* t */
     909              :     {"tzm", 3, DCH_TZM, true, FROM_CHAR_DATE_NONE},
     910              :     {"tz", 2, DCH_tz, false, FROM_CHAR_DATE_NONE},
     911              :     {"us", 2, DCH_US, true, FROM_CHAR_DATE_NONE}, /* u */
     912              :     {"ww", 2, DCH_WW, true, FROM_CHAR_DATE_GREGORIAN},    /* w */
     913              :     {"w", 1, DCH_W, true, FROM_CHAR_DATE_GREGORIAN},
     914              :     {"y,yyy", 5, DCH_Y_YYY, true, FROM_CHAR_DATE_GREGORIAN},  /* y */
     915              :     {"yyyy", 4, DCH_YYYY, true, FROM_CHAR_DATE_GREGORIAN},
     916              :     {"yyy", 3, DCH_YYY, true, FROM_CHAR_DATE_GREGORIAN},
     917              :     {"yy", 2, DCH_YY, true, FROM_CHAR_DATE_GREGORIAN},
     918              :     {"y", 1, DCH_Y, true, FROM_CHAR_DATE_GREGORIAN},
     919              : 
     920              :     /* last */
     921              :     {NULL, 0, 0, 0, 0}
     922              : };
     923              : 
     924              : /*
     925              :  * KeyWords for NUMBER version
     926              :  *
     927              :  * The is_digit and date_mode fields are not relevant here.
     928              :  */
     929              : static const KeyWord NUM_keywords[] = {
     930              : /*  name, len, id           is in Index */
     931              :     {",", 1, NUM_COMMA},      /* , */
     932              :     {".", 1, NUM_DEC},            /* . */
     933              :     {"0", 1, NUM_0},          /* 0 */
     934              :     {"9", 1, NUM_9},          /* 9 */
     935              :     {"B", 1, NUM_B},          /* B */
     936              :     {"C", 1, NUM_C},          /* C */
     937              :     {"D", 1, NUM_D},          /* D */
     938              :     {"EEEE", 4, NUM_E},           /* E */
     939              :     {"FM", 2, NUM_FM},            /* F */
     940              :     {"G", 1, NUM_G},          /* G */
     941              :     {"L", 1, NUM_L},          /* L */
     942              :     {"MI", 2, NUM_MI},            /* M */
     943              :     {"PL", 2, NUM_PL},            /* P */
     944              :     {"PR", 2, NUM_PR},
     945              :     {"RN", 2, NUM_RN},            /* R */
     946              :     {"SG", 2, NUM_SG},            /* S */
     947              :     {"SP", 2, NUM_SP},
     948              :     {"S", 1, NUM_S},
     949              :     {"TH", 2, NUM_TH},            /* T */
     950              :     {"V", 1, NUM_V},          /* V */
     951              :     {"b", 1, NUM_B},          /* b */
     952              :     {"c", 1, NUM_C},          /* c */
     953              :     {"d", 1, NUM_D},          /* d */
     954              :     {"eeee", 4, NUM_E},           /* e */
     955              :     {"fm", 2, NUM_FM},            /* f */
     956              :     {"g", 1, NUM_G},          /* g */
     957              :     {"l", 1, NUM_L},          /* l */
     958              :     {"mi", 2, NUM_MI},            /* m */
     959              :     {"pl", 2, NUM_PL},            /* p */
     960              :     {"pr", 2, NUM_PR},
     961              :     {"rn", 2, NUM_rn},            /* r */
     962              :     {"sg", 2, NUM_SG},            /* s */
     963              :     {"sp", 2, NUM_SP},
     964              :     {"s", 1, NUM_S},
     965              :     {"th", 2, NUM_th},            /* t */
     966              :     {"v", 1, NUM_V},          /* v */
     967              : 
     968              :     /* last */
     969              :     {NULL, 0, 0}
     970              : };
     971              : 
     972              : 
     973              : /*
     974              :  * KeyWords index for DATE-TIME version
     975              :  */
     976              : static const int DCH_index[KeyWord_INDEX_SIZE] = {
     977              : /*
     978              : 0   1   2   3   4   5   6   7   8   9
     979              : */
     980              :     /*---- first 0..31 chars are skipped ----*/
     981              : 
     982              :     -1, -1, -1, -1, -1, -1, -1, -1,
     983              :     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     984              :     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     985              :     -1, -1, -1, -1, -1, DCH_A_D, DCH_B_C, DCH_CC, DCH_DAY, -1,
     986              :     DCH_FF1, -1, DCH_HH24, DCH_IDDD, DCH_J, -1, -1, DCH_MI, -1, DCH_OF,
     987              :     DCH_P_M, DCH_Q, DCH_RM, DCH_SSSSS, DCH_TZH, DCH_US, -1, DCH_WW, -1, DCH_Y_YYY,
     988              :     -1, -1, -1, -1, -1, -1, -1, DCH_a_d, DCH_b_c, DCH_cc,
     989              :     DCH_day, -1, DCH_ff1, -1, DCH_hh24, DCH_iddd, DCH_j, -1, -1, DCH_mi,
     990              :     -1, DCH_of, DCH_p_m, DCH_q, DCH_rm, DCH_sssss, DCH_tzh, DCH_us, -1, DCH_ww,
     991              :     -1, DCH_y_yyy, -1, -1, -1, -1
     992              : 
     993              :     /*---- chars over 126 are skipped ----*/
     994              : };
     995              : 
     996              : /*
     997              :  * KeyWords index for NUMBER version
     998              :  */
     999              : static const int NUM_index[KeyWord_INDEX_SIZE] = {
    1000              : /*
    1001              : 0   1   2   3   4   5   6   7   8   9
    1002              : */
    1003              :     /*---- first 0..31 chars are skipped ----*/
    1004              : 
    1005              :     -1, -1, -1, -1, -1, -1, -1, -1,
    1006              :     -1, -1, -1, -1, NUM_COMMA, -1, NUM_DEC, -1, NUM_0, -1,
    1007              :     -1, -1, -1, -1, -1, -1, -1, NUM_9, -1, -1,
    1008              :     -1, -1, -1, -1, -1, -1, NUM_B, NUM_C, NUM_D, NUM_E,
    1009              :     NUM_FM, NUM_G, -1, -1, -1, -1, NUM_L, NUM_MI, -1, -1,
    1010              :     NUM_PL, -1, NUM_RN, NUM_SG, NUM_TH, -1, NUM_V, -1, -1, -1,
    1011              :     -1, -1, -1, -1, -1, -1, -1, -1, NUM_b, NUM_c,
    1012              :     NUM_d, NUM_e, NUM_fm, NUM_g, -1, -1, -1, -1, NUM_l, NUM_mi,
    1013              :     -1, -1, NUM_pl, -1, NUM_rn, NUM_sg, NUM_th, -1, NUM_v, -1,
    1014              :     -1, -1, -1, -1, -1, -1
    1015              : 
    1016              :     /*---- chars over 126 are skipped ----*/
    1017              : };
    1018              : 
    1019              : /*
    1020              :  * Number processor struct
    1021              :  */
    1022              : typedef struct NUMProc
    1023              : {
    1024              :     bool        is_to_char;
    1025              :     NUMDesc    *Num;            /* number description       */
    1026              : 
    1027              :     int         sign,           /* '-' or '+'           */
    1028              :                 sign_wrote,     /* was sign write       */
    1029              :                 num_count,      /* number of write digits   */
    1030              :                 num_in,         /* is inside number     */
    1031              :                 num_curr,       /* current position in number   */
    1032              :                 out_pre_spaces, /* spaces before first digit    */
    1033              : 
    1034              :                 read_dec,       /* to_number - was read dec. point  */
    1035              :                 read_post,      /* to_number - number of dec. digit */
    1036              :                 read_pre;       /* to_number - number non-dec. digit */
    1037              : 
    1038              :     char       *number,         /* string with number   */
    1039              :                *number_p,       /* pointer to current number position */
    1040              :                *inout,          /* in / out buffer  */
    1041              :                *inout_p;        /* pointer to current inout position */
    1042              : 
    1043              :     const char *last_relevant,  /* last relevant number after decimal point */
    1044              : 
    1045              :                *L_negative_sign,    /* Locale */
    1046              :                *L_positive_sign,
    1047              :                *decimal,
    1048              :                *L_thousands_sep,
    1049              :                *L_currency_symbol;
    1050              : } NUMProc;
    1051              : 
    1052              : /* Return flags for DCH_from_char() */
    1053              : #define DCH_DATED   0x01
    1054              : #define DCH_TIMED   0x02
    1055              : #define DCH_ZONED   0x04
    1056              : 
    1057              : /*
    1058              :  * These macros are used in NUM_processor() and its subsidiary routines.
    1059              :  * OVERLOAD_TEST: true if we've reached end of input string
    1060              :  * AMOUNT_TEST(s): true if at least s bytes remain in string
    1061              :  */
    1062              : #define OVERLOAD_TEST   (Np->inout_p >= Np->inout + input_len)
    1063              : #define AMOUNT_TEST(s)  (Np->inout_p <= Np->inout + (input_len - (s)))
    1064              : 
    1065              : 
    1066              : /*
    1067              :  * Functions
    1068              :  */
    1069              : static const KeyWord *index_seq_search(const char *str, const KeyWord *kw,
    1070              :                                        const int *index);
    1071              : static const KeySuffix *suff_search(const char *str, const KeySuffix *suf, enum KeySuffixType type);
    1072              : static bool is_separator_char(const char *str);
    1073              : static void NUMDesc_prepare(NUMDesc *num, FormatNode *n);
    1074              : static void parse_format(FormatNode *node, const char *str, const KeyWord *kw,
    1075              :                          const KeySuffix *suf, const int *index, uint32 flags, NUMDesc *Num);
    1076              : 
    1077              : static void DCH_to_char(FormatNode *node, bool is_interval,
    1078              :                         TmToChar *in, char *out, Oid collid);
    1079              : static void DCH_from_char(FormatNode *node, const char *in, TmFromChar *out,
    1080              :                           Oid collid, bool std, Node *escontext);
    1081              : 
    1082              : #ifdef DEBUG_TO_FROM_CHAR
    1083              : static void dump_index(const KeyWord *k, const int *index);
    1084              : static void dump_node(FormatNode *node, int max);
    1085              : #endif
    1086              : 
    1087              : static const char *get_th(const char *num, enum TH_Case type);
    1088              : static char *str_numth(char *dest, const char *num, enum TH_Case type);
    1089              : static int  adjust_partial_year_to_2020(int year);
    1090              : static size_t strspace_len(const char *str);
    1091              : static bool from_char_set_mode(TmFromChar *tmfc, const FromCharDateMode mode,
    1092              :                                Node *escontext);
    1093              : static bool from_char_set_int(int *dest, const int value, const FormatNode *node,
    1094              :                               Node *escontext);
    1095              : static int  from_char_parse_int_len(int *dest, const char **src, const size_t len,
    1096              :                                     FormatNode *node, Node *escontext);
    1097              : static int  from_char_parse_int(int *dest, const char **src, FormatNode *node,
    1098              :                                 Node *escontext);
    1099              : static int  seq_search_ascii(const char *name, const char *const *array, size_t *len);
    1100              : static int  seq_search_localized(const char *name, char **array, size_t *len,
    1101              :                                  Oid collid);
    1102              : static bool from_char_seq_search(int *dest, const char **src,
    1103              :                                  const char *const *array,
    1104              :                                  char **localized_array, Oid collid,
    1105              :                                  FormatNode *node, Node *escontext);
    1106              : static bool do_to_timestamp(const text *date_txt, const text *fmt, Oid collid, bool std,
    1107              :                             struct pg_tm *tm, fsec_t *fsec, struct fmt_tz *tz,
    1108              :                             int *fprec, uint32 *flags, Node *escontext);
    1109              : static void fill_str(char *str, int c, int max);
    1110              : static FormatNode *NUM_cache(int len, NUMDesc *Num, const text *pars_str, bool *shouldFree);
    1111              : static char *int_to_roman(int number);
    1112              : static int  roman_to_int(NUMProc *Np, size_t input_len);
    1113              : static void NUM_prepare_locale(NUMProc *Np);
    1114              : static const char *get_last_relevant_decnum(const char *num);
    1115              : static void NUM_numpart_from_char(NUMProc *Np, int id, size_t input_len);
    1116              : static void NUM_numpart_to_char(NUMProc *Np, int id);
    1117              : static char *NUM_processor(FormatNode *node, NUMDesc *Num, char *inout,
    1118              :                            char *number, size_t input_len, int to_char_out_pre_spaces,
    1119              :                            int sign, bool is_to_char, Oid collid);
    1120              : static DCHCacheEntry *DCH_cache_getnew(const char *str, bool std);
    1121              : static DCHCacheEntry *DCH_cache_search(const char *str, bool std);
    1122              : static DCHCacheEntry *DCH_cache_fetch(const char *str, bool std);
    1123              : static NUMCacheEntry *NUM_cache_getnew(const char *str);
    1124              : static NUMCacheEntry *NUM_cache_search(const char *str);
    1125              : static NUMCacheEntry *NUM_cache_fetch(const char *str);
    1126              : 
    1127              : 
    1128              : /*
    1129              :  * Fast sequential search, use index for data selection which
    1130              :  * go to seq. cycle (it is very fast for unwanted strings)
    1131              :  * (can't be used binary search in format parsing)
    1132              :  */
    1133              : static const KeyWord *
    1134        15429 : index_seq_search(const char *str, const KeyWord *kw, const int *index)
    1135              : {
    1136              :     int         poz;
    1137              : 
    1138        15429 :     if (!KeyWord_INDEX_FILTER(*str))
    1139         3545 :         return NULL;
    1140              : 
    1141        11884 :     if ((poz = index[*str - ' ']) > -1)
    1142              :     {
    1143        10836 :         const KeyWord *k = kw + poz;
    1144              : 
    1145              :         do
    1146              :         {
    1147        14421 :             if (strncmp(str, k->name, k->len) == 0)
    1148        10770 :                 return k;
    1149         3651 :             k++;
    1150         3651 :             if (!k->name)
    1151            0 :                 return NULL;
    1152         3651 :         } while (*str == *k->name);
    1153              :     }
    1154         1114 :     return NULL;
    1155              : }
    1156              : 
    1157              : static const KeySuffix *
    1158         5836 : suff_search(const char *str, const KeySuffix *suf, enum KeySuffixType type)
    1159              : {
    1160        45254 :     for (const KeySuffix *s = suf; s->name != NULL; s++)
    1161              :     {
    1162        39637 :         if (s->type != type)
    1163        18719 :             continue;
    1164              : 
    1165        20918 :         if (strncmp(str, s->name, s->len) == 0)
    1166          219 :             return s;
    1167              :     }
    1168         5617 :     return NULL;
    1169              : }
    1170              : 
    1171              : static bool
    1172         3276 : is_separator_char(const char *str)
    1173              : {
    1174              :     /* ASCII printable character, but not letter or digit */
    1175         2434 :     return (*str > 0x20 && *str < 0x7F &&
    1176         2434 :             !(*str >= 'A' && *str <= 'Z') &&
    1177         8000 :             !(*str >= 'a' && *str <= 'z') &&
    1178         2290 :             !(*str >= '0' && *str <= '9'));
    1179              : }
    1180              : 
    1181              : /*
    1182              :  * Prepare NUMDesc (number description struct) via FormatNode struct
    1183              :  */
    1184              : static void
    1185         8512 : NUMDesc_prepare(NUMDesc *num, FormatNode *n)
    1186              : {
    1187         8512 :     if (n->type != NODE_TYPE_ACTION)
    1188            0 :         return;
    1189              : 
    1190         8512 :     if (IS_EEEE(num) && n->key->id != NUM_E)
    1191            0 :         ereport(ERROR,
    1192              :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1193              :                  errmsg("\"EEEE\" must be the last pattern used")));
    1194              : 
    1195         8512 :     switch (n->key->id)
    1196              :     {
    1197         7245 :         case NUM_9:
    1198         7245 :             if (IS_BRACKET(num))
    1199            0 :                 ereport(ERROR,
    1200              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1201              :                          errmsg("\"9\" must be ahead of \"PR\"")));
    1202         7245 :             if (IS_MULTI(num))
    1203              :             {
    1204           18 :                 ++num->multi;
    1205           18 :                 break;
    1206              :             }
    1207         7227 :             if (IS_DECIMAL(num))
    1208         2491 :                 ++num->post;
    1209              :             else
    1210         4736 :                 ++num->pre;
    1211         7227 :             break;
    1212              : 
    1213          268 :         case NUM_0:
    1214          268 :             if (IS_BRACKET(num))
    1215            0 :                 ereport(ERROR,
    1216              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1217              :                          errmsg("\"0\" must be ahead of \"PR\"")));
    1218          268 :             if (!IS_ZERO(num) && !IS_DECIMAL(num))
    1219              :             {
    1220           57 :                 num->flag |= NUM_F_ZERO;
    1221           57 :                 num->zero_start = num->pre + 1;
    1222              :             }
    1223          268 :             if (!IS_DECIMAL(num))
    1224          184 :                 ++num->pre;
    1225              :             else
    1226           84 :                 ++num->post;
    1227              : 
    1228          268 :             num->zero_end = num->pre + num->post;
    1229          268 :             break;
    1230              : 
    1231            0 :         case NUM_B:
    1232            0 :             if (num->pre == 0 && num->post == 0 && !IS_ZERO(num))
    1233            0 :                 num->flag |= NUM_F_BLANK;
    1234            0 :             break;
    1235              : 
    1236           45 :         case NUM_D:
    1237           45 :             num->flag |= NUM_F_LDECIMAL;
    1238           45 :             num->need_locale = true;
    1239              :             /* FALLTHROUGH */
    1240          243 :         case NUM_DEC:
    1241          243 :             if (IS_DECIMAL(num))
    1242            0 :                 ereport(ERROR,
    1243              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1244              :                          errmsg("multiple decimal points")));
    1245          243 :             if (IS_MULTI(num))
    1246            0 :                 ereport(ERROR,
    1247              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1248              :                          errmsg("cannot use \"V\" and decimal point together")));
    1249          243 :             num->flag |= NUM_F_DECIMAL;
    1250          243 :             break;
    1251              : 
    1252          132 :         case NUM_FM:
    1253          132 :             num->flag |= NUM_F_FILLMODE;
    1254          132 :             break;
    1255              : 
    1256          113 :         case NUM_S:
    1257          113 :             if (IS_LSIGN(num))
    1258            0 :                 ereport(ERROR,
    1259              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1260              :                          errmsg("cannot use \"S\" twice")));
    1261          113 :             if (IS_PLUS(num) || IS_MINUS(num) || IS_BRACKET(num))
    1262            0 :                 ereport(ERROR,
    1263              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1264              :                          errmsg("cannot use \"S\" and \"PL\"/\"MI\"/\"SG\"/\"PR\" together")));
    1265          113 :             if (!IS_DECIMAL(num))
    1266              :             {
    1267           96 :                 num->lsign = NUM_LSIGN_PRE;
    1268           96 :                 num->pre_lsign_num = num->pre;
    1269           96 :                 num->need_locale = true;
    1270           96 :                 num->flag |= NUM_F_LSIGN;
    1271              :             }
    1272           17 :             else if (num->lsign == NUM_LSIGN_NONE)
    1273              :             {
    1274           17 :                 num->lsign = NUM_LSIGN_POST;
    1275           17 :                 num->need_locale = true;
    1276           17 :                 num->flag |= NUM_F_LSIGN;
    1277              :             }
    1278          113 :             break;
    1279              : 
    1280           18 :         case NUM_MI:
    1281           18 :             if (IS_LSIGN(num))
    1282            0 :                 ereport(ERROR,
    1283              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1284              :                          errmsg("cannot use \"S\" and \"MI\" together")));
    1285           18 :             num->flag |= NUM_F_MINUS;
    1286           18 :             if (IS_DECIMAL(num))
    1287            3 :                 num->flag |= NUM_F_MINUS_POST;
    1288           18 :             break;
    1289              : 
    1290            3 :         case NUM_PL:
    1291            3 :             if (IS_LSIGN(num))
    1292            0 :                 ereport(ERROR,
    1293              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1294              :                          errmsg("cannot use \"S\" and \"PL\" together")));
    1295            3 :             num->flag |= NUM_F_PLUS;
    1296            3 :             if (IS_DECIMAL(num))
    1297            0 :                 num->flag |= NUM_F_PLUS_POST;
    1298            3 :             break;
    1299              : 
    1300           12 :         case NUM_SG:
    1301           12 :             if (IS_LSIGN(num))
    1302            0 :                 ereport(ERROR,
    1303              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1304              :                          errmsg("cannot use \"S\" and \"SG\" together")));
    1305           12 :             num->flag |= NUM_F_MINUS;
    1306           12 :             num->flag |= NUM_F_PLUS;
    1307           12 :             break;
    1308              : 
    1309           18 :         case NUM_PR:
    1310           18 :             if (IS_LSIGN(num) || IS_PLUS(num) || IS_MINUS(num))
    1311            0 :                 ereport(ERROR,
    1312              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1313              :                          errmsg("cannot use \"PR\" and \"S\"/\"PL\"/\"MI\"/\"SG\" together")));
    1314           18 :             num->flag |= NUM_F_BRACKET;
    1315           18 :             break;
    1316              : 
    1317           33 :         case NUM_rn:
    1318              :         case NUM_RN:
    1319           33 :             if (IS_ROMAN(num))
    1320            3 :                 ereport(ERROR,
    1321              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1322              :                          errmsg("cannot use \"RN\" twice")));
    1323           30 :             num->flag |= NUM_F_ROMAN;
    1324           30 :             break;
    1325              : 
    1326          352 :         case NUM_L:
    1327              :         case NUM_G:
    1328          352 :             num->need_locale = true;
    1329          352 :             break;
    1330              : 
    1331            9 :         case NUM_V:
    1332            9 :             if (IS_DECIMAL(num))
    1333            0 :                 ereport(ERROR,
    1334              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1335              :                          errmsg("cannot use \"V\" and decimal point together")));
    1336            9 :             num->flag |= NUM_F_MULTI;
    1337            9 :             break;
    1338              : 
    1339            9 :         case NUM_E:
    1340            9 :             if (IS_EEEE(num))
    1341            0 :                 ereport(ERROR,
    1342              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1343              :                          errmsg("cannot use \"EEEE\" twice")));
    1344            9 :             if (IS_BLANK(num) || IS_FILLMODE(num) || IS_LSIGN(num) ||
    1345            9 :                 IS_BRACKET(num) || IS_MINUS(num) || IS_PLUS(num) ||
    1346            9 :                 IS_ROMAN(num) || IS_MULTI(num))
    1347            0 :                 ereport(ERROR,
    1348              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1349              :                          errmsg("\"EEEE\" is incompatible with other formats"),
    1350              :                          errdetail("\"EEEE\" may only be used together with digit and decimal point patterns.")));
    1351            9 :             num->flag |= NUM_F_EEEE;
    1352            9 :             break;
    1353              :     }
    1354              : 
    1355         8509 :     if (IS_ROMAN(num) &&
    1356           30 :         (num->flag & ~(NUM_F_ROMAN | NUM_F_FILLMODE)) != 0)
    1357            3 :         ereport(ERROR,
    1358              :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1359              :                  errmsg("\"RN\" is incompatible with other formats"),
    1360              :                  errdetail("\"RN\" may only be used together with \"FM\".")));
    1361              : }
    1362              : 
    1363              : /*
    1364              :  * Format parser, search small keywords and keyword's suffixes, and make
    1365              :  * format-node tree.
    1366              :  *
    1367              :  * for DATE-TIME & NUMBER version
    1368              :  */
    1369              : static void
    1370          929 : parse_format(FormatNode *node, const char *str, const KeyWord *kw,
    1371              :              const KeySuffix *suf, const int *index, uint32 flags, NUMDesc *Num)
    1372              : {
    1373              :     FormatNode *n;
    1374              : 
    1375              : #ifdef DEBUG_TO_FROM_CHAR
    1376              :     elog(DEBUG_elog_output, "to_char/number(): run parser");
    1377              : #endif
    1378              : 
    1379          929 :     n = node;
    1380              : 
    1381        16349 :     while (*str)
    1382              :     {
    1383        15429 :         int         suffix = 0;
    1384              :         const KeySuffix *s;
    1385              : 
    1386              :         /*
    1387              :          * Prefix
    1388              :          */
    1389        19460 :         if ((flags & DCH_FLAG) &&
    1390         4031 :             (s = suff_search(str, suf, SUFFTYPE_PREFIX)) != NULL)
    1391              :         {
    1392          198 :             suffix |= s->id;
    1393          198 :             if (s->len)
    1394          198 :                 str += s->len;
    1395              :         }
    1396              : 
    1397              :         /*
    1398              :          * Keyword
    1399              :          */
    1400        15429 :         if (*str && (n->key = index_seq_search(str, kw, index)) != NULL)
    1401              :         {
    1402        10770 :             n->type = NODE_TYPE_ACTION;
    1403        10770 :             n->suffix = suffix;
    1404        10770 :             if (n->key->len)
    1405        10770 :                 str += n->key->len;
    1406              : 
    1407              :             /*
    1408              :              * NUM version: Prepare global NUMDesc struct
    1409              :              */
    1410        10770 :             if (flags & NUM_FLAG)
    1411         8512 :                 NUMDesc_prepare(Num, n);
    1412              : 
    1413              :             /*
    1414              :              * Postfix
    1415              :              */
    1416        12569 :             if ((flags & DCH_FLAG) && *str &&
    1417         1805 :                 (s = suff_search(str, suf, SUFFTYPE_POSTFIX)) != NULL)
    1418              :             {
    1419           21 :                 n->suffix |= s->id;
    1420           21 :                 if (s->len)
    1421           21 :                     str += s->len;
    1422              :             }
    1423              : 
    1424        10764 :             n++;
    1425              :         }
    1426         4659 :         else if (*str)
    1427              :         {
    1428              :             int         chlen;
    1429              : 
    1430         4659 :             if ((flags & STD_FLAG) && *str != '"')
    1431              :             {
    1432              :                 /*
    1433              :                  * Standard mode, allow only following separators: "-./,':; ".
    1434              :                  * However, we support double quotes even in standard mode
    1435              :                  * (see below).  This is our extension of standard mode.
    1436              :                  */
    1437          285 :                 if (strchr("-./,':; ", *str) == NULL)
    1438            3 :                     ereport(ERROR,
    1439              :                             (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    1440              :                              errmsg("invalid datetime format separator: \"%s\"",
    1441              :                                     pnstrdup(str, pg_mblen_cstr(str)))));
    1442              : 
    1443          282 :                 if (*str == ' ')
    1444           45 :                     n->type = NODE_TYPE_SPACE;
    1445              :                 else
    1446          237 :                     n->type = NODE_TYPE_SEPARATOR;
    1447              : 
    1448          282 :                 n->character[0] = *str;
    1449          282 :                 n->character[1] = '\0';
    1450          282 :                 n->key = NULL;
    1451          282 :                 n->suffix = 0;
    1452          282 :                 n++;
    1453          282 :                 str++;
    1454              :             }
    1455         4374 :             else if (*str == '"')
    1456              :             {
    1457              :                 /*
    1458              :                  * Process double-quoted literal string, if any
    1459              :                  */
    1460          192 :                 str++;
    1461         2208 :                 while (*str)
    1462              :                 {
    1463         2205 :                     if (*str == '"')
    1464              :                     {
    1465          189 :                         str++;
    1466          189 :                         break;
    1467              :                     }
    1468              :                     /* backslash quotes the next character, if any */
    1469         2016 :                     if (*str == '\\' && *(str + 1))
    1470          120 :                         str++;
    1471         2016 :                     chlen = pg_mblen_cstr(str);
    1472         2016 :                     n->type = NODE_TYPE_CHAR;
    1473         2016 :                     memcpy(n->character, str, chlen);
    1474         2016 :                     n->character[chlen] = '\0';
    1475         2016 :                     n->key = NULL;
    1476         2016 :                     n->suffix = 0;
    1477         2016 :                     n++;
    1478         2016 :                     str += chlen;
    1479              :                 }
    1480              :             }
    1481              :             else
    1482              :             {
    1483              :                 /*
    1484              :                  * Outside double-quoted strings, backslash is only special if
    1485              :                  * it immediately precedes a double quote.
    1486              :                  */
    1487         4182 :                 if (*str == '\\' && *(str + 1) == '"')
    1488            6 :                     str++;
    1489         4182 :                 chlen = pg_mblen_cstr(str);
    1490              : 
    1491         4182 :                 if ((flags & DCH_FLAG) && is_separator_char(str))
    1492          538 :                     n->type = NODE_TYPE_SEPARATOR;
    1493         3644 :                 else if (isspace((unsigned char) *str))
    1494         3500 :                     n->type = NODE_TYPE_SPACE;
    1495              :                 else
    1496          144 :                     n->type = NODE_TYPE_CHAR;
    1497              : 
    1498         4182 :                 memcpy(n->character, str, chlen);
    1499         4182 :                 n->character[chlen] = '\0';
    1500         4182 :                 n->key = NULL;
    1501         4182 :                 n->suffix = 0;
    1502         4182 :                 n++;
    1503         4182 :                 str += chlen;
    1504              :             }
    1505              :         }
    1506              :     }
    1507              : 
    1508          920 :     n->type = NODE_TYPE_END;
    1509          920 :     n->suffix = 0;
    1510          920 : }
    1511              : 
    1512              : /*
    1513              :  * DEBUG: Dump the FormatNode Tree (debug)
    1514              :  */
    1515              : #ifdef DEBUG_TO_FROM_CHAR
    1516              : 
    1517              : #define DUMP_THth(_suf) (IS_SUFFIX_TH(_suf) ? "TH" : (IS_SUFFIX_th(_suf) ? "th" : " "))
    1518              : #define DUMP_FM(_suf)   (IS_SUFFIX_FM(_suf) ? "FM" : " ")
    1519              : 
    1520              : static void
    1521              : dump_node(FormatNode *node, int max)
    1522              : {
    1523              :     FormatNode *n;
    1524              :     int         a;
    1525              : 
    1526              :     elog(DEBUG_elog_output, "to_from-char(): DUMP FORMAT");
    1527              : 
    1528              :     for (a = 0, n = node; a <= max; n++, a++)
    1529              :     {
    1530              :         if (n->type == NODE_TYPE_ACTION)
    1531              :             elog(DEBUG_elog_output, "%d:\t NODE_TYPE_ACTION '%s'\t(%s,%s)",
    1532              :                  a, n->key->name, DUMP_THth(n->suffix), DUMP_FM(n->suffix));
    1533              :         else if (n->type == NODE_TYPE_CHAR)
    1534              :             elog(DEBUG_elog_output, "%d:\t NODE_TYPE_CHAR '%s'",
    1535              :                  a, n->character);
    1536              :         else if (n->type == NODE_TYPE_END)
    1537              :         {
    1538              :             elog(DEBUG_elog_output, "%d:\t NODE_TYPE_END", a);
    1539              :             return;
    1540              :         }
    1541              :         else
    1542              :             elog(DEBUG_elog_output, "%d:\t unknown NODE!", a);
    1543              :     }
    1544              : }
    1545              : #endif                          /* DEBUG */
    1546              : 
    1547              : /*****************************************************************************
    1548              :  *          Private utils
    1549              :  *****************************************************************************/
    1550              : 
    1551              : /*
    1552              :  * Return ST/ND/RD/TH for simple (1..9) numbers
    1553              :  */
    1554              : static const char *
    1555         1167 : get_th(const char *num, enum TH_Case type)
    1556              : {
    1557         1167 :     size_t      len = strlen(num);
    1558              :     char        last;
    1559              : 
    1560              :     Assert(len > 0);
    1561              : 
    1562         1167 :     last = num[len - 1];
    1563         1167 :     if (!isdigit((unsigned char) last))
    1564            0 :         ereport(ERROR,
    1565              :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
    1566              :                  errmsg("\"%s\" is not a number", num)));
    1567              : 
    1568              :     /*
    1569              :      * All "teens" (<x>1[0-9]) get 'TH/th', while <x>[02-9][123] still get
    1570              :      * 'ST/st', 'ND/nd', 'RD/rd', respectively
    1571              :      */
    1572         1167 :     if (len > 1 && num[len - 2] == '1')
    1573           66 :         last = 0;
    1574              : 
    1575         1167 :     switch (last)
    1576              :     {
    1577           48 :         case '1':
    1578           48 :             if (type == TH_UPPER)
    1579           12 :                 return numTH[0];
    1580           36 :             return numth[0];
    1581           24 :         case '2':
    1582           24 :             if (type == TH_UPPER)
    1583            0 :                 return numTH[1];
    1584           24 :             return numth[1];
    1585           18 :         case '3':
    1586           18 :             if (type == TH_UPPER)
    1587            3 :                 return numTH[2];
    1588           15 :             return numth[2];
    1589         1077 :         default:
    1590         1077 :             if (type == TH_UPPER)
    1591          378 :                 return numTH[3];
    1592          699 :             return numth[3];
    1593              :     }
    1594              : }
    1595              : 
    1596              : /*
    1597              :  * Convert string-number to ordinal string-number
    1598              :  */
    1599              : static char *
    1600         1143 : str_numth(char *dest, const char *num, enum TH_Case type)
    1601              : {
    1602         1143 :     if (dest != num)
    1603            0 :         strcpy(dest, num);
    1604         1143 :     strcat(dest, get_th(num, type));
    1605         1143 :     return dest;
    1606              : }
    1607              : 
    1608              : /*****************************************************************************
    1609              :  *          upper/lower/initcap functions
    1610              :  *****************************************************************************/
    1611              : 
    1612              : /*
    1613              :  * collation-aware, wide-character-aware lower function
    1614              :  *
    1615              :  * We pass the number of bytes so we can pass varlena and char*
    1616              :  * to this function.  The result is a palloc'd, null-terminated string.
    1617              :  */
    1618              : char *
    1619       219028 : str_tolower(const char *buff, size_t nbytes, Oid collid)
    1620              : {
    1621              :     char       *result;
    1622              :     pg_locale_t mylocale;
    1623              : 
    1624       219028 :     if (!buff)
    1625            0 :         return NULL;
    1626              : 
    1627       219028 :     if (!OidIsValid(collid))
    1628              :     {
    1629              :         /*
    1630              :          * This typically means that the parser could not resolve a conflict
    1631              :          * of implicit collations, so report it that way.
    1632              :          */
    1633            0 :         ereport(ERROR,
    1634              :                 (errcode(ERRCODE_INDETERMINATE_COLLATION),
    1635              :                  errmsg("could not determine which collation to use for %s function",
    1636              :                         "lower()"),
    1637              :                  errhint("Use the COLLATE clause to set the collation explicitly.")));
    1638              :     }
    1639              : 
    1640       219028 :     mylocale = pg_newlocale_from_collation(collid);
    1641              : 
    1642              :     /* C/POSIX collations use this path regardless of database encoding */
    1643       219028 :     if (mylocale->ctype_is_c)
    1644              :     {
    1645          138 :         result = asc_tolower(buff, nbytes);
    1646              :     }
    1647              :     else
    1648              :     {
    1649       218890 :         const char *src = buff;
    1650       218890 :         size_t      srclen = nbytes;
    1651              :         size_t      dstsize;
    1652              :         char       *dst;
    1653              :         size_t      needed;
    1654              : 
    1655              :         /* first try buffer of equal size plus terminating NUL */
    1656       218890 :         dstsize = srclen + 1;
    1657       218890 :         dst = palloc(dstsize);
    1658              : 
    1659       218890 :         needed = pg_strlower(dst, dstsize, src, srclen, mylocale);
    1660       218890 :         if (needed + 1 > dstsize)
    1661              :         {
    1662              :             /* grow buffer if needed and retry */
    1663           48 :             dstsize = needed + 1;
    1664           48 :             dst = repalloc(dst, dstsize);
    1665           48 :             needed = pg_strlower(dst, dstsize, src, srclen, mylocale);
    1666              :             Assert(needed + 1 <= dstsize);
    1667              :         }
    1668              : 
    1669              :         Assert(dst[needed] == '\0');
    1670       218890 :         result = dst;
    1671              :     }
    1672              : 
    1673       219028 :     return result;
    1674              : }
    1675              : 
    1676              : /*
    1677              :  * collation-aware, wide-character-aware upper function
    1678              :  *
    1679              :  * We pass the number of bytes so we can pass varlena and char*
    1680              :  * to this function.  The result is a palloc'd, null-terminated string.
    1681              :  */
    1682              : char *
    1683       527304 : str_toupper(const char *buff, size_t nbytes, Oid collid)
    1684              : {
    1685              :     char       *result;
    1686              :     pg_locale_t mylocale;
    1687              : 
    1688       527304 :     if (!buff)
    1689            0 :         return NULL;
    1690              : 
    1691       527304 :     if (!OidIsValid(collid))
    1692              :     {
    1693              :         /*
    1694              :          * This typically means that the parser could not resolve a conflict
    1695              :          * of implicit collations, so report it that way.
    1696              :          */
    1697            0 :         ereport(ERROR,
    1698              :                 (errcode(ERRCODE_INDETERMINATE_COLLATION),
    1699              :                  errmsg("could not determine which collation to use for %s function",
    1700              :                         "upper()"),
    1701              :                  errhint("Use the COLLATE clause to set the collation explicitly.")));
    1702              :     }
    1703              : 
    1704       527304 :     mylocale = pg_newlocale_from_collation(collid);
    1705              : 
    1706              :     /* C/POSIX collations use this path regardless of database encoding */
    1707       527304 :     if (mylocale->ctype_is_c)
    1708              :     {
    1709         7581 :         result = asc_toupper(buff, nbytes);
    1710              :     }
    1711              :     else
    1712              :     {
    1713       519723 :         const char *src = buff;
    1714       519723 :         size_t      srclen = nbytes;
    1715              :         size_t      dstsize;
    1716              :         char       *dst;
    1717              :         size_t      needed;
    1718              : 
    1719              :         /* first try buffer of equal size plus terminating NUL */
    1720       519723 :         dstsize = srclen + 1;
    1721       519723 :         dst = palloc(dstsize);
    1722              : 
    1723       519723 :         needed = pg_strupper(dst, dstsize, src, srclen, mylocale);
    1724       519723 :         if (needed + 1 > dstsize)
    1725              :         {
    1726              :             /* grow buffer if needed and retry */
    1727            3 :             dstsize = needed + 1;
    1728            3 :             dst = repalloc(dst, dstsize);
    1729            3 :             needed = pg_strupper(dst, dstsize, src, srclen, mylocale);
    1730              :             Assert(needed + 1 <= dstsize);
    1731              :         }
    1732              : 
    1733              :         Assert(dst[needed] == '\0');
    1734       519723 :         result = dst;
    1735              :     }
    1736              : 
    1737       527304 :     return result;
    1738              : }
    1739              : 
    1740              : /*
    1741              :  * collation-aware, wide-character-aware initcap function
    1742              :  *
    1743              :  * We pass the number of bytes so we can pass varlena and char*
    1744              :  * to this function.  The result is a palloc'd, null-terminated string.
    1745              :  */
    1746              : char *
    1747          113 : str_initcap(const char *buff, size_t nbytes, Oid collid)
    1748              : {
    1749              :     char       *result;
    1750              :     pg_locale_t mylocale;
    1751              : 
    1752          113 :     if (!buff)
    1753            0 :         return NULL;
    1754              : 
    1755          113 :     if (!OidIsValid(collid))
    1756              :     {
    1757              :         /*
    1758              :          * This typically means that the parser could not resolve a conflict
    1759              :          * of implicit collations, so report it that way.
    1760              :          */
    1761            0 :         ereport(ERROR,
    1762              :                 (errcode(ERRCODE_INDETERMINATE_COLLATION),
    1763              :                  errmsg("could not determine which collation to use for %s function",
    1764              :                         "initcap()"),
    1765              :                  errhint("Use the COLLATE clause to set the collation explicitly.")));
    1766              :     }
    1767              : 
    1768          113 :     mylocale = pg_newlocale_from_collation(collid);
    1769              : 
    1770              :     /* C/POSIX collations use this path regardless of database encoding */
    1771          113 :     if (mylocale->ctype_is_c)
    1772              :     {
    1773           12 :         result = asc_initcap(buff, nbytes);
    1774              :     }
    1775              :     else
    1776              :     {
    1777          101 :         const char *src = buff;
    1778          101 :         size_t      srclen = nbytes;
    1779              :         size_t      dstsize;
    1780              :         char       *dst;
    1781              :         size_t      needed;
    1782              : 
    1783              :         /* first try buffer of equal size plus terminating NUL */
    1784          101 :         dstsize = srclen + 1;
    1785          101 :         dst = palloc(dstsize);
    1786              : 
    1787          101 :         needed = pg_strtitle(dst, dstsize, src, srclen, mylocale);
    1788          101 :         if (needed + 1 > dstsize)
    1789              :         {
    1790              :             /* grow buffer if needed and retry */
    1791           15 :             dstsize = needed + 1;
    1792           15 :             dst = repalloc(dst, dstsize);
    1793           15 :             needed = pg_strtitle(dst, dstsize, src, srclen, mylocale);
    1794              :             Assert(needed + 1 <= dstsize);
    1795              :         }
    1796              : 
    1797              :         Assert(dst[needed] == '\0');
    1798          101 :         result = dst;
    1799              :     }
    1800              : 
    1801          113 :     return result;
    1802              : }
    1803              : 
    1804              : /*
    1805              :  * collation-aware, wide-character-aware case folding
    1806              :  *
    1807              :  * We pass the number of bytes so we can pass varlena and char*
    1808              :  * to this function.  The result is a palloc'd, null-terminated string.
    1809              :  */
    1810              : char *
    1811           12 : str_casefold(const char *buff, size_t nbytes, Oid collid)
    1812              : {
    1813              :     char       *result;
    1814              :     pg_locale_t mylocale;
    1815              : 
    1816           12 :     if (!buff)
    1817            0 :         return NULL;
    1818              : 
    1819           12 :     if (!OidIsValid(collid))
    1820              :     {
    1821              :         /*
    1822              :          * This typically means that the parser could not resolve a conflict
    1823              :          * of implicit collations, so report it that way.
    1824              :          */
    1825            0 :         ereport(ERROR,
    1826              :                 (errcode(ERRCODE_INDETERMINATE_COLLATION),
    1827              :                  errmsg("could not determine which collation to use for %s function",
    1828              :                         "casefold()"),
    1829              :                  errhint("Use the COLLATE clause to set the collation explicitly.")));
    1830              :     }
    1831              : 
    1832           12 :     if (GetDatabaseEncoding() != PG_UTF8)
    1833            0 :         ereport(ERROR,
    1834              :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1835              :                  errmsg("Unicode case folding can only be performed if server encoding is UTF8")));
    1836              : 
    1837           12 :     mylocale = pg_newlocale_from_collation(collid);
    1838              : 
    1839              :     /* C/POSIX collations use this path regardless of database encoding */
    1840           12 :     if (mylocale->ctype_is_c)
    1841              :     {
    1842            0 :         result = asc_tolower(buff, nbytes);
    1843              :     }
    1844              :     else
    1845              :     {
    1846           12 :         const char *src = buff;
    1847           12 :         size_t      srclen = nbytes;
    1848              :         size_t      dstsize;
    1849              :         char       *dst;
    1850              :         size_t      needed;
    1851              : 
    1852              :         /* first try buffer of equal size plus terminating NUL */
    1853           12 :         dstsize = srclen + 1;
    1854           12 :         dst = palloc(dstsize);
    1855              : 
    1856           12 :         needed = pg_strfold(dst, dstsize, src, srclen, mylocale);
    1857           12 :         if (needed + 1 > dstsize)
    1858              :         {
    1859              :             /* grow buffer if needed and retry */
    1860            0 :             dstsize = needed + 1;
    1861            0 :             dst = repalloc(dst, dstsize);
    1862            0 :             needed = pg_strfold(dst, dstsize, src, srclen, mylocale);
    1863              :             Assert(needed + 1 <= dstsize);
    1864              :         }
    1865              : 
    1866              :         Assert(dst[needed] == '\0');
    1867           12 :         result = dst;
    1868              :     }
    1869              : 
    1870           12 :     return result;
    1871              : }
    1872              : 
    1873              : /*
    1874              :  * ASCII-only lower function
    1875              :  *
    1876              :  * We pass the number of bytes so we can pass varlena and char*
    1877              :  * to this function.  The result is a palloc'd, null-terminated string.
    1878              :  */
    1879              : char *
    1880         2442 : asc_tolower(const char *buff, size_t nbytes)
    1881              : {
    1882              :     char       *result;
    1883              : 
    1884         2442 :     if (!buff)
    1885            0 :         return NULL;
    1886              : 
    1887         2442 :     result = pnstrdup(buff, nbytes);
    1888              : 
    1889        16800 :     for (char *p = result; *p; p++)
    1890        14358 :         *p = pg_ascii_tolower((unsigned char) *p);
    1891              : 
    1892         2442 :     return result;
    1893              : }
    1894              : 
    1895              : /*
    1896              :  * ASCII-only upper function
    1897              :  *
    1898              :  * We pass the number of bytes so we can pass varlena and char*
    1899              :  * to this function.  The result is a palloc'd, null-terminated string.
    1900              :  */
    1901              : char *
    1902         9867 : asc_toupper(const char *buff, size_t nbytes)
    1903              : {
    1904              :     char       *result;
    1905              : 
    1906         9867 :     if (!buff)
    1907            0 :         return NULL;
    1908              : 
    1909         9867 :     result = pnstrdup(buff, nbytes);
    1910              : 
    1911        85068 :     for (char *p = result; *p; p++)
    1912        75201 :         *p = pg_ascii_toupper((unsigned char) *p);
    1913              : 
    1914         9867 :     return result;
    1915              : }
    1916              : 
    1917              : /*
    1918              :  * ASCII-only initcap function
    1919              :  *
    1920              :  * We pass the number of bytes so we can pass varlena and char*
    1921              :  * to this function.  The result is a palloc'd, null-terminated string.
    1922              :  */
    1923              : char *
    1924           12 : asc_initcap(const char *buff, size_t nbytes)
    1925              : {
    1926              :     char       *result;
    1927           12 :     int         wasalnum = false;
    1928              : 
    1929           12 :     if (!buff)
    1930            0 :         return NULL;
    1931              : 
    1932           12 :     result = pnstrdup(buff, nbytes);
    1933              : 
    1934           48 :     for (char *p = result; *p; p++)
    1935              :     {
    1936              :         char        c;
    1937              : 
    1938           36 :         if (wasalnum)
    1939           24 :             *p = c = pg_ascii_tolower((unsigned char) *p);
    1940              :         else
    1941           12 :             *p = c = pg_ascii_toupper((unsigned char) *p);
    1942              :         /* we don't trust isalnum() here */
    1943           72 :         wasalnum = ((c >= 'A' && c <= 'Z') ||
    1944           72 :                     (c >= 'a' && c <= 'z') ||
    1945            0 :                     (c >= '0' && c <= '9'));
    1946              :     }
    1947              : 
    1948           12 :     return result;
    1949              : }
    1950              : 
    1951              : /* convenience routines for when the input is null-terminated */
    1952              : 
    1953              : static char *
    1954            0 : str_tolower_z(const char *buff, Oid collid)
    1955              : {
    1956            0 :     return str_tolower(buff, strlen(buff), collid);
    1957              : }
    1958              : 
    1959              : static char *
    1960            0 : str_toupper_z(const char *buff, Oid collid)
    1961              : {
    1962            0 :     return str_toupper(buff, strlen(buff), collid);
    1963              : }
    1964              : 
    1965              : static char *
    1966            0 : str_initcap_z(const char *buff, Oid collid)
    1967              : {
    1968            0 :     return str_initcap(buff, strlen(buff), collid);
    1969              : }
    1970              : 
    1971              : static char *
    1972         2304 : asc_tolower_z(const char *buff)
    1973              : {
    1974         2304 :     return asc_tolower(buff, strlen(buff));
    1975              : }
    1976              : 
    1977              : static char *
    1978         2286 : asc_toupper_z(const char *buff)
    1979              : {
    1980         2286 :     return asc_toupper(buff, strlen(buff));
    1981              : }
    1982              : 
    1983              : /* asc_initcap_z is not currently needed */
    1984              : 
    1985              : 
    1986              : /*
    1987              :  * Skip TM / th in FROM_CHAR
    1988              :  *
    1989              :  * If IS_SUFFIX_THth is on, skip two chars, assuming there are two available
    1990              :  */
    1991              : #define SKIP_THth(ptr, _suf) \
    1992              :     do { \
    1993              :         if (IS_SUFFIX_THth(_suf)) \
    1994              :         { \
    1995              :             if (*(ptr)) (ptr) += pg_mblen_cstr(ptr); \
    1996              :             if (*(ptr)) (ptr) += pg_mblen_cstr(ptr); \
    1997              :         } \
    1998              :     } while (0)
    1999              : 
    2000              : 
    2001              : #ifdef DEBUG_TO_FROM_CHAR
    2002              : /*
    2003              :  * DEBUG: Call for debug and for index checking; (Show ASCII char
    2004              :  * and defined keyword for each used position
    2005              :  */
    2006              : static void
    2007              : dump_index(const KeyWord *k, const int *index)
    2008              : {
    2009              :     int         count = 0,
    2010              :                 free_i = 0;
    2011              : 
    2012              :     elog(DEBUG_elog_output, "TO-FROM_CHAR: Dump KeyWord Index:");
    2013              : 
    2014              :     for (int i = 0; i < KeyWord_INDEX_SIZE; i++)
    2015              :     {
    2016              :         if (index[i] != -1)
    2017              :         {
    2018              :             elog(DEBUG_elog_output, "\t%c: %s, ", i + 32, k[index[i]].name);
    2019              :             count++;
    2020              :         }
    2021              :         else
    2022              :         {
    2023              :             free_i++;
    2024              :             elog(DEBUG_elog_output, "\t(%d) %c %d", i, i + 32, index[i]);
    2025              :         }
    2026              :     }
    2027              :     elog(DEBUG_elog_output, "\n\t\tUsed positions: %d,\n\t\tFree positions: %d",
    2028              :          count, free_i);
    2029              : }
    2030              : #endif                          /* DEBUG */
    2031              : 
    2032              : /*
    2033              :  * Return true if next format picture is not digit value
    2034              :  */
    2035              : static bool
    2036        61867 : is_next_separator(FormatNode *n)
    2037              : {
    2038        61867 :     if (n->type == NODE_TYPE_END)
    2039            0 :         return false;
    2040              : 
    2041        61867 :     if (n->type == NODE_TYPE_ACTION && IS_SUFFIX_THth(n->suffix))
    2042            0 :         return true;
    2043              : 
    2044              :     /*
    2045              :      * Next node
    2046              :      */
    2047        61867 :     n++;
    2048              : 
    2049              :     /* end of format string is treated like a non-digit separator */
    2050        61867 :     if (n->type == NODE_TYPE_END)
    2051         6424 :         return true;
    2052              : 
    2053        55443 :     if (n->type == NODE_TYPE_ACTION)
    2054              :     {
    2055         3225 :         if (n->key->is_digit)
    2056          240 :             return false;
    2057              : 
    2058         2985 :         return true;
    2059              :     }
    2060        52218 :     else if (n->character[1] == '\0' &&
    2061        52218 :              isdigit((unsigned char) n->character[0]))
    2062            0 :         return false;
    2063              : 
    2064        52218 :     return true;                /* some non-digit input (separator) */
    2065              : }
    2066              : 
    2067              : 
    2068              : static int
    2069           42 : adjust_partial_year_to_2020(int year)
    2070              : {
    2071              :     /*
    2072              :      * Adjust all dates toward 2020; this is effectively what happens when we
    2073              :      * assume '70' is 1970 and '69' is 2069.
    2074              :      */
    2075              :     /* Force 0-69 into the 2000's */
    2076           42 :     if (year < 70)
    2077           21 :         return year + 2000;
    2078              :     /* Force 70-99 into the 1900's */
    2079           21 :     else if (year < 100)
    2080           18 :         return year + 1900;
    2081              :     /* Force 100-519 into the 2000's */
    2082            3 :     else if (year < 520)
    2083            0 :         return year + 2000;
    2084              :     /* Force 520-999 into the 1000's */
    2085            3 :     else if (year < 1000)
    2086            3 :         return year + 1000;
    2087              :     else
    2088            0 :         return year;
    2089              : }
    2090              : 
    2091              : 
    2092              : static size_t
    2093        61888 : strspace_len(const char *str)
    2094              : {
    2095        61888 :     size_t      len = 0;
    2096              : 
    2097        61888 :     while (*str && isspace((unsigned char) *str))
    2098              :     {
    2099            0 :         str++;
    2100            0 :         len++;
    2101              :     }
    2102        61888 :     return len;
    2103              : }
    2104              : 
    2105              : /*
    2106              :  * Set the date mode of a from-char conversion.
    2107              :  *
    2108              :  * Puke if the date mode has already been set, and the caller attempts to set
    2109              :  * it to a conflicting mode.
    2110              :  *
    2111              :  * Returns true on success, false on failure (if escontext points to an
    2112              :  * ErrorSaveContext; otherwise errors are thrown).
    2113              :  */
    2114              : static bool
    2115        61879 : from_char_set_mode(TmFromChar *tmfc, const FromCharDateMode mode,
    2116              :                    Node *escontext)
    2117              : {
    2118        61879 :     if (mode != FROM_CHAR_DATE_NONE)
    2119              :     {
    2120        27463 :         if (tmfc->mode == FROM_CHAR_DATE_NONE)
    2121        10144 :             tmfc->mode = mode;
    2122        17319 :         else if (tmfc->mode != mode)
    2123            3 :             ereturn(escontext, false,
    2124              :                     (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    2125              :                      errmsg("invalid combination of date conventions"),
    2126              :                      errhint("Do not mix Gregorian and ISO week date conventions in a formatting template.")));
    2127              :     }
    2128        61876 :     return true;
    2129              : }
    2130              : 
    2131              : /*
    2132              :  * Set the integer pointed to by 'dest' to the given value.
    2133              :  *
    2134              :  * Puke if the destination integer has previously been set to some other
    2135              :  * non-zero value.
    2136              :  *
    2137              :  * Returns true on success, false on failure (if escontext points to an
    2138              :  * ErrorSaveContext; otherwise errors are thrown).
    2139              :  */
    2140              : static bool
    2141        61575 : from_char_set_int(int *dest, const int value, const FormatNode *node,
    2142              :                   Node *escontext)
    2143              : {
    2144        61575 :     if (*dest != 0 && *dest != value)
    2145            3 :         ereturn(escontext, false,
    2146              :                 (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    2147              :                  errmsg("conflicting values for \"%s\" field in formatting string",
    2148              :                         node->key->name),
    2149              :                  errdetail("This value contradicts a previous setting for the same field type.")));
    2150        61572 :     *dest = value;
    2151        61572 :     return true;
    2152              : }
    2153              : 
    2154              : /*
    2155              :  * Read a single integer from the source string, into the int pointed to by
    2156              :  * 'dest'. If 'dest' is NULL, the result is discarded.
    2157              :  *
    2158              :  * In fixed-width mode (the node does not have the FM suffix), consume at most
    2159              :  * 'len' characters.  However, any leading whitespace isn't counted in 'len'.
    2160              :  *
    2161              :  * We use strtol() to recover the integer value from the source string, in
    2162              :  * accordance with the given FormatNode.
    2163              :  *
    2164              :  * If the conversion completes successfully, src will have been advanced to
    2165              :  * point at the character immediately following the last character used in the
    2166              :  * conversion.
    2167              :  *
    2168              :  * Returns the number of characters consumed, or -1 on error (if escontext
    2169              :  * points to an ErrorSaveContext; otherwise errors are thrown).
    2170              :  *
    2171              :  * Note that from_char_parse_int() provides a more convenient wrapper where
    2172              :  * the length of the field is the same as the length of the format keyword (as
    2173              :  * with DD and MI).
    2174              :  */
    2175              : static int
    2176        61888 : from_char_parse_int_len(int *dest, const char **src, const size_t len, FormatNode *node,
    2177              :                         Node *escontext)
    2178              : {
    2179              :     long        result;
    2180              :     char        copy[DCH_MAX_ITEM_SIZ + 1];
    2181        61888 :     const char *init = *src;
    2182              :     size_t      used;
    2183              : 
    2184              :     /*
    2185              :      * Skip any whitespace before parsing the integer.
    2186              :      */
    2187        61888 :     *src += strspace_len(*src);
    2188              : 
    2189              :     Assert(len <= DCH_MAX_ITEM_SIZ);
    2190        61888 :     used = strlcpy(copy, *src, len + 1);
    2191              : 
    2192        61888 :     if (IS_SUFFIX_FM(node->suffix) || is_next_separator(node))
    2193        61648 :     {
    2194              :         /*
    2195              :          * This node is in Fill Mode, or the next node is known to be a
    2196              :          * non-digit value, so we just slurp as many characters as we can get.
    2197              :          */
    2198              :         char       *endptr;
    2199              : 
    2200        61648 :         errno = 0;
    2201        61648 :         result = strtol(init, &endptr, 10);
    2202        61648 :         *src = endptr;
    2203              :     }
    2204              :     else
    2205              :     {
    2206              :         /*
    2207              :          * We need to pull exactly the number of characters given in 'len' out
    2208              :          * of the string, and convert those.
    2209              :          */
    2210              :         char       *last;
    2211              : 
    2212          240 :         if (used < len)
    2213            3 :             ereturn(escontext, -1,
    2214              :                     (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    2215              :                      errmsg("source string too short for \"%s\" formatting field",
    2216              :                             node->key->name),
    2217              :                      errdetail("Field requires %zu characters, but only %zu remain.",
    2218              :                                len, used),
    2219              :                      errhint("If your source string is not fixed-width, try using the \"FM\" modifier.")));
    2220              : 
    2221          237 :         errno = 0;
    2222          237 :         result = strtol(copy, &last, 10);
    2223          237 :         used = last - copy;
    2224              : 
    2225          237 :         if (used > 0 && used < len)
    2226            3 :             ereturn(escontext, -1,
    2227              :                     (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    2228              :                      errmsg("invalid value \"%s\" for \"%s\"",
    2229              :                             copy, node->key->name),
    2230              :                      errdetail("Field requires %zu characters, but only %zu could be parsed.",
    2231              :                                len, used),
    2232              :                      errhint("If your source string is not fixed-width, try using the \"FM\" modifier.")));
    2233              : 
    2234          234 :         *src += used;
    2235              :     }
    2236              : 
    2237        61882 :     if (*src == init)
    2238          418 :         ereturn(escontext, -1,
    2239              :                 (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    2240              :                  errmsg("invalid value \"%s\" for \"%s\"",
    2241              :                         copy, node->key->name),
    2242              :                  errdetail("Value must be an integer.")));
    2243              : 
    2244        61464 :     if (errno == ERANGE || result < INT_MIN || result > INT_MAX)
    2245            3 :         ereturn(escontext, -1,
    2246              :                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2247              :                  errmsg("value for \"%s\" in source string is out of range",
    2248              :                         node->key->name),
    2249              :                  errdetail("Value must be in the range %d to %d.",
    2250              :                            INT_MIN, INT_MAX)));
    2251              : 
    2252        61461 :     if (dest != NULL)
    2253              :     {
    2254        61458 :         if (!from_char_set_int(dest, (int) result, node, escontext))
    2255            0 :             return -1;
    2256              :     }
    2257              : 
    2258        61461 :     return *src - init;
    2259              : }
    2260              : 
    2261              : /*
    2262              :  * Call from_char_parse_int_len(), using the length of the format keyword as
    2263              :  * the expected length of the field.
    2264              :  *
    2265              :  * Don't call this function if the field differs in length from the format
    2266              :  * keyword (as with HH24; the keyword length is 4, but the field length is 2).
    2267              :  * In such cases, call from_char_parse_int_len() instead to specify the
    2268              :  * required length explicitly.
    2269              :  */
    2270              : static int
    2271        43927 : from_char_parse_int(int *dest, const char **src, FormatNode *node,
    2272              :                     Node *escontext)
    2273              : {
    2274        43927 :     return from_char_parse_int_len(dest, src, node->key->len, node, escontext);
    2275              : }
    2276              : 
    2277              : /*
    2278              :  * Sequentially search null-terminated "array" for a case-insensitive match
    2279              :  * to the initial character(s) of "name".
    2280              :  *
    2281              :  * Returns array index of match, or -1 for no match.
    2282              :  *
    2283              :  * *len is set to the length of the match, or 0 for no match.
    2284              :  *
    2285              :  * Case-insensitivity is defined per pg_ascii_tolower, so this is only
    2286              :  * suitable for comparisons to ASCII strings.
    2287              :  */
    2288              : static int
    2289          120 : seq_search_ascii(const char *name, const char *const *array, size_t *len)
    2290              : {
    2291              :     unsigned char firstc;
    2292              : 
    2293          120 :     *len = 0;
    2294              : 
    2295              :     /* empty string can't match anything */
    2296          120 :     if (!*name)
    2297            0 :         return -1;
    2298              : 
    2299              :     /* we handle first char specially to gain some speed */
    2300          120 :     firstc = pg_ascii_tolower((unsigned char) *name);
    2301              : 
    2302          534 :     for (const char *const *a = array; *a != NULL; a++)
    2303              :     {
    2304              :         /* compare first chars */
    2305          528 :         if (pg_ascii_tolower((unsigned char) **a) != firstc)
    2306          390 :             continue;
    2307              : 
    2308              :         /* compare rest of string */
    2309          393 :         for (const char *p = *a + 1, *n = name + 1;; p++, n++)
    2310              :         {
    2311              :             /* return success if we matched whole array entry */
    2312          393 :             if (*p == '\0')
    2313              :             {
    2314          114 :                 *len = n - name;
    2315          114 :                 return a - array;
    2316              :             }
    2317              :             /* else, must have another character in "name" ... */
    2318          279 :             if (*n == '\0')
    2319            0 :                 break;
    2320              :             /* ... and it must match */
    2321          558 :             if (pg_ascii_tolower((unsigned char) *p) !=
    2322          279 :                 pg_ascii_tolower((unsigned char) *n))
    2323           24 :                 break;
    2324              :         }
    2325              :     }
    2326              : 
    2327            6 :     return -1;
    2328              : }
    2329              : 
    2330              : /*
    2331              :  * Sequentially search an array of possibly non-English words for
    2332              :  * a case-insensitive match to the initial character(s) of "name".
    2333              :  *
    2334              :  * This has the same API as seq_search_ascii(), but we use a more general
    2335              :  * case-folding transformation to achieve case-insensitivity.  Case folding
    2336              :  * is done per the rules of the collation identified by "collid".
    2337              :  *
    2338              :  * The array is treated as const, but we don't declare it that way because
    2339              :  * the arrays exported by pg_locale.c aren't const.
    2340              :  */
    2341              : static int
    2342            0 : seq_search_localized(const char *name, char **array, size_t *len, Oid collid)
    2343              : {
    2344              :     char       *upper_name;
    2345              :     char       *lower_name;
    2346              : 
    2347            0 :     *len = 0;
    2348              : 
    2349              :     /* empty string can't match anything */
    2350            0 :     if (!*name)
    2351            0 :         return -1;
    2352              : 
    2353              :     /*
    2354              :      * The case-folding processing done below is fairly expensive, so before
    2355              :      * doing that, make a quick pass to see if there is an exact match.
    2356              :      */
    2357            0 :     for (char **a = array; *a != NULL; a++)
    2358              :     {
    2359            0 :         size_t      element_len = strlen(*a);
    2360              : 
    2361            0 :         if (strncmp(name, *a, element_len) == 0)
    2362              :         {
    2363            0 :             *len = element_len;
    2364            0 :             return a - array;
    2365              :         }
    2366              :     }
    2367              : 
    2368              :     /*
    2369              :      * Fold to upper case, then to lower case, so that we can match reliably
    2370              :      * even in languages in which case conversions are not injective.
    2371              :      */
    2372            0 :     upper_name = str_toupper(name, strlen(name), collid);
    2373            0 :     lower_name = str_tolower(upper_name, strlen(upper_name), collid);
    2374            0 :     pfree(upper_name);
    2375              : 
    2376            0 :     for (char **a = array; *a != NULL; a++)
    2377              :     {
    2378              :         char       *upper_element;
    2379              :         char       *lower_element;
    2380              :         size_t      element_len;
    2381              : 
    2382              :         /* Likewise upper/lower-case array element */
    2383            0 :         upper_element = str_toupper(*a, strlen(*a), collid);
    2384            0 :         lower_element = str_tolower(upper_element, strlen(upper_element),
    2385              :                                     collid);
    2386            0 :         pfree(upper_element);
    2387            0 :         element_len = strlen(lower_element);
    2388              : 
    2389              :         /* Match? */
    2390            0 :         if (strncmp(lower_name, lower_element, element_len) == 0)
    2391              :         {
    2392            0 :             *len = element_len;
    2393            0 :             pfree(lower_element);
    2394            0 :             pfree(lower_name);
    2395            0 :             return a - array;
    2396              :         }
    2397            0 :         pfree(lower_element);
    2398              :     }
    2399              : 
    2400            0 :     pfree(lower_name);
    2401            0 :     return -1;
    2402              : }
    2403              : 
    2404              : /*
    2405              :  * Perform a sequential search in 'array' (or 'localized_array', if that's
    2406              :  * not NULL) for an entry matching the first character(s) of the 'src'
    2407              :  * string case-insensitively.
    2408              :  *
    2409              :  * The 'array' is presumed to be English words (all-ASCII), but
    2410              :  * if 'localized_array' is supplied, that might be non-English
    2411              :  * so we need a more expensive case-folding transformation
    2412              :  * (which will follow the rules of the collation 'collid').
    2413              :  *
    2414              :  * If a match is found, copy the array index of the match into the integer
    2415              :  * pointed to by 'dest' and advance 'src' to the end of the part of the string
    2416              :  * which matched.
    2417              :  *
    2418              :  * Returns true on match, false on failure (if escontext points to an
    2419              :  * ErrorSaveContext; otherwise errors are thrown).
    2420              :  *
    2421              :  * 'node' is used only for error reports: node->key->name identifies the
    2422              :  * field type we were searching for.
    2423              :  */
    2424              : static bool
    2425          120 : from_char_seq_search(int *dest, const char **src, const char *const *array,
    2426              :                      char **localized_array, Oid collid,
    2427              :                      FormatNode *node, Node *escontext)
    2428              : {
    2429              :     size_t      len;
    2430              : 
    2431          120 :     if (localized_array == NULL)
    2432          120 :         *dest = seq_search_ascii(*src, array, &len);
    2433              :     else
    2434            0 :         *dest = seq_search_localized(*src, localized_array, &len, collid);
    2435              : 
    2436          120 :     if (len <= 0)
    2437              :     {
    2438              :         /*
    2439              :          * In the error report, truncate the string at the next whitespace (if
    2440              :          * any) to avoid including irrelevant data.
    2441              :          */
    2442            6 :         char       *copy = pstrdup(*src);
    2443              : 
    2444           30 :         for (char *c = copy; *c; c++)
    2445              :         {
    2446           27 :             if (scanner_isspace(*c))
    2447              :             {
    2448            3 :                 *c = '\0';
    2449            3 :                 break;
    2450              :             }
    2451              :         }
    2452              : 
    2453            6 :         ereturn(escontext, false,
    2454              :                 (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    2455              :                  errmsg("invalid value \"%s\" for \"%s\"",
    2456              :                         copy, node->key->name),
    2457              :                  errdetail("The given value did not match any of the allowed values for this field.")));
    2458              :     }
    2459          114 :     *src += len;
    2460          114 :     return true;
    2461              : }
    2462              : 
    2463              : /*
    2464              :  * Process a TmToChar struct as denoted by a list of FormatNodes.
    2465              :  * The formatted data is written to the string pointed to by 'out'.
    2466              :  */
    2467              : static void
    2468         4793 : DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid collid)
    2469              : {
    2470              :     char       *s;
    2471         4793 :     struct fmt_tm *tm = &in->tm;
    2472              :     int         i;
    2473              : 
    2474              :     /* cache localized days and months */
    2475         4793 :     cache_locale_time();
    2476              : 
    2477         4793 :     s = out;
    2478        93481 :     for (FormatNode *n = node; n->type != NODE_TYPE_END; n++)
    2479              :     {
    2480        88688 :         if (n->type != NODE_TYPE_ACTION)
    2481              :         {
    2482        51948 :             strcpy(s, n->character);
    2483        51948 :             s += strlen(s);
    2484        51948 :             continue;
    2485              :         }
    2486              : 
    2487        36740 :         switch (n->key->id)
    2488              :         {
    2489          381 :             case DCH_A_M:
    2490              :             case DCH_P_M:
    2491          381 :                 strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
    2492              :                        ? P_M_STR : A_M_STR);
    2493          381 :                 s += strlen(s);
    2494          381 :                 break;
    2495            0 :             case DCH_AM:
    2496              :             case DCH_PM:
    2497            0 :                 strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
    2498              :                        ? PM_STR : AM_STR);
    2499            0 :                 s += strlen(s);
    2500            0 :                 break;
    2501          381 :             case DCH_a_m:
    2502              :             case DCH_p_m:
    2503          381 :                 strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
    2504              :                        ? p_m_STR : a_m_STR);
    2505          381 :                 s += strlen(s);
    2506          381 :                 break;
    2507          381 :             case DCH_am:
    2508              :             case DCH_pm:
    2509          381 :                 strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
    2510              :                        ? pm_STR : am_STR);
    2511          381 :                 s += strlen(s);
    2512          381 :                 break;
    2513         2300 :             case DCH_HH:
    2514              :             case DCH_HH12:
    2515              : 
    2516              :                 /*
    2517              :                  * display time as shown on a 12-hour clock, even for
    2518              :                  * intervals
    2519              :                  */
    2520         2300 :                 sprintf(s, "%0*lld", IS_SUFFIX_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3,
    2521         2300 :                         tm->tm_hour % (HOURS_PER_DAY / 2) == 0 ?
    2522              :                         (long long) (HOURS_PER_DAY / 2) :
    2523         2216 :                         (long long) (tm->tm_hour % (HOURS_PER_DAY / 2)));
    2524         2300 :                 if (IS_SUFFIX_THth(n->suffix))
    2525            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2526         2300 :                 s += strlen(s);
    2527         2300 :                 break;
    2528          763 :             case DCH_HH24:
    2529          763 :                 sprintf(s, "%0*lld", IS_SUFFIX_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3,
    2530          763 :                         (long long) tm->tm_hour);
    2531          763 :                 if (IS_SUFFIX_THth(n->suffix))
    2532            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2533          763 :                 s += strlen(s);
    2534          763 :                 break;
    2535         2301 :             case DCH_MI:
    2536         2301 :                 sprintf(s, "%0*d", IS_SUFFIX_FM(n->suffix) ? 0 : (tm->tm_min >= 0) ? 2 : 3,
    2537              :                         tm->tm_min);
    2538         2301 :                 if (IS_SUFFIX_THth(n->suffix))
    2539            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2540         2301 :                 s += strlen(s);
    2541         2301 :                 break;
    2542         2301 :             case DCH_SS:
    2543         2301 :                 sprintf(s, "%0*d", IS_SUFFIX_FM(n->suffix) ? 0 : (tm->tm_sec >= 0) ? 2 : 3,
    2544              :                         tm->tm_sec);
    2545         2301 :                 if (IS_SUFFIX_THth(n->suffix))
    2546            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2547         2301 :                 s += strlen(s);
    2548         2301 :                 break;
    2549              : 
    2550              : #define DCH_to_char_fsec(frac_fmt, frac_val) \
    2551              :                 sprintf(s, frac_fmt, (int) (frac_val)); \
    2552              :                 if (IS_SUFFIX_THth(n->suffix)) \
    2553              :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix)); \
    2554              :                 s += strlen(s)
    2555              : 
    2556           48 :             case DCH_FF1:       /* tenth of second */
    2557           48 :                 DCH_to_char_fsec("%01d", in->fsec / 100000);
    2558           48 :                 break;
    2559           48 :             case DCH_FF2:       /* hundredth of second */
    2560           48 :                 DCH_to_char_fsec("%02d", in->fsec / 10000);
    2561           48 :                 break;
    2562           72 :             case DCH_FF3:
    2563              :             case DCH_MS:        /* millisecond */
    2564           72 :                 DCH_to_char_fsec("%03d", in->fsec / 1000);
    2565           72 :                 break;
    2566           48 :             case DCH_FF4:       /* tenth of a millisecond */
    2567           48 :                 DCH_to_char_fsec("%04d", in->fsec / 100);
    2568           48 :                 break;
    2569           48 :             case DCH_FF5:       /* hundredth of a millisecond */
    2570           48 :                 DCH_to_char_fsec("%05d", in->fsec / 10);
    2571           48 :                 break;
    2572           72 :             case DCH_FF6:
    2573              :             case DCH_US:        /* microsecond */
    2574           72 :                 DCH_to_char_fsec("%06d", in->fsec);
    2575           72 :                 break;
    2576              : #undef DCH_to_char_fsec
    2577          387 :             case DCH_SSSS:
    2578          387 :                 sprintf(s, "%lld",
    2579          387 :                         (long long) (tm->tm_hour * SECS_PER_HOUR +
    2580          387 :                                      tm->tm_min * SECS_PER_MINUTE +
    2581          387 :                                      tm->tm_sec));
    2582          387 :                 if (IS_SUFFIX_THth(n->suffix))
    2583            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2584          387 :                 s += strlen(s);
    2585          387 :                 break;
    2586            3 :             case DCH_tz:
    2587            3 :                 INVALID_FOR_INTERVAL;
    2588            3 :                 if (tmtcTzn(in))
    2589              :                 {
    2590              :                     /* We assume here that timezone names aren't localized */
    2591            3 :                     char       *p = asc_tolower_z(tmtcTzn(in));
    2592              : 
    2593            3 :                     strcpy(s, p);
    2594            3 :                     pfree(p);
    2595            3 :                     s += strlen(s);
    2596              :                 }
    2597            3 :                 break;
    2598            9 :             case DCH_TZ:
    2599            9 :                 INVALID_FOR_INTERVAL;
    2600            9 :                 if (tmtcTzn(in))
    2601              :                 {
    2602            9 :                     strcpy(s, tmtcTzn(in));
    2603            9 :                     s += strlen(s);
    2604              :                 }
    2605            9 :                 break;
    2606           54 :             case DCH_TZH:
    2607           54 :                 INVALID_FOR_INTERVAL;
    2608           54 :                 sprintf(s, "%c%02d",
    2609           54 :                         (tm->tm_gmtoff >= 0) ? '+' : '-',
    2610           54 :                         abs((int) tm->tm_gmtoff) / SECS_PER_HOUR);
    2611           54 :                 s += strlen(s);
    2612           54 :                 break;
    2613           54 :             case DCH_TZM:
    2614           54 :                 INVALID_FOR_INTERVAL;
    2615           54 :                 sprintf(s, "%02d",
    2616           54 :                         (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR) / SECS_PER_MINUTE);
    2617           54 :                 s += strlen(s);
    2618           54 :                 break;
    2619           54 :             case DCH_OF:
    2620           54 :                 INVALID_FOR_INTERVAL;
    2621          108 :                 sprintf(s, "%c%0*d",
    2622           54 :                         (tm->tm_gmtoff >= 0) ? '+' : '-',
    2623           54 :                         IS_SUFFIX_FM(n->suffix) ? 0 : 2,
    2624           54 :                         abs((int) tm->tm_gmtoff) / SECS_PER_HOUR);
    2625           54 :                 s += strlen(s);
    2626           54 :                 if (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR != 0)
    2627              :                 {
    2628           36 :                     sprintf(s, ":%02d",
    2629           36 :                             (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR) / SECS_PER_MINUTE);
    2630           36 :                     s += strlen(s);
    2631              :                 }
    2632           54 :                 break;
    2633          381 :             case DCH_A_D:
    2634              :             case DCH_B_C:
    2635          381 :                 INVALID_FOR_INTERVAL;
    2636          381 :                 strcpy(s, (tm->tm_year <= 0 ? B_C_STR : A_D_STR));
    2637          381 :                 s += strlen(s);
    2638          381 :                 break;
    2639            0 :             case DCH_AD:
    2640              :             case DCH_BC:
    2641            0 :                 INVALID_FOR_INTERVAL;
    2642            0 :                 strcpy(s, (tm->tm_year <= 0 ? BC_STR : AD_STR));
    2643            0 :                 s += strlen(s);
    2644            0 :                 break;
    2645          381 :             case DCH_a_d:
    2646              :             case DCH_b_c:
    2647          381 :                 INVALID_FOR_INTERVAL;
    2648          381 :                 strcpy(s, (tm->tm_year <= 0 ? b_c_STR : a_d_STR));
    2649          381 :                 s += strlen(s);
    2650          381 :                 break;
    2651          381 :             case DCH_ad:
    2652              :             case DCH_bc:
    2653          381 :                 INVALID_FOR_INTERVAL;
    2654          381 :                 strcpy(s, (tm->tm_year <= 0 ? bc_STR : ad_STR));
    2655          381 :                 s += strlen(s);
    2656          381 :                 break;
    2657          762 :             case DCH_MONTH:
    2658          762 :                 INVALID_FOR_INTERVAL;
    2659          762 :                 if (!tm->tm_mon)
    2660            0 :                     break;
    2661          762 :                 if (IS_SUFFIX_TM(n->suffix))
    2662              :                 {
    2663            0 :                     char       *str = str_toupper_z(localized_full_months[tm->tm_mon - 1], collid);
    2664              : 
    2665            0 :                     if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
    2666            0 :                         strcpy(s, str);
    2667              :                     else
    2668            0 :                         ereport(ERROR,
    2669              :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2670              :                                  errmsg("localized string format value too long")));
    2671              :                 }
    2672              :                 else
    2673          762 :                     sprintf(s, "%*s", IS_SUFFIX_FM(n->suffix) ? 0 : -9,
    2674          762 :                             asc_toupper_z(months_full[tm->tm_mon - 1]));
    2675          762 :                 s += strlen(s);
    2676          762 :                 break;
    2677          762 :             case DCH_Month:
    2678          762 :                 INVALID_FOR_INTERVAL;
    2679          762 :                 if (!tm->tm_mon)
    2680            0 :                     break;
    2681          762 :                 if (IS_SUFFIX_TM(n->suffix))
    2682              :                 {
    2683            0 :                     char       *str = str_initcap_z(localized_full_months[tm->tm_mon - 1], collid);
    2684              : 
    2685            0 :                     if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
    2686            0 :                         strcpy(s, str);
    2687              :                     else
    2688            0 :                         ereport(ERROR,
    2689              :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2690              :                                  errmsg("localized string format value too long")));
    2691              :                 }
    2692              :                 else
    2693          762 :                     sprintf(s, "%*s", IS_SUFFIX_FM(n->suffix) ? 0 : -9,
    2694          762 :                             months_full[tm->tm_mon - 1]);
    2695          762 :                 s += strlen(s);
    2696          762 :                 break;
    2697          762 :             case DCH_month:
    2698          762 :                 INVALID_FOR_INTERVAL;
    2699          762 :                 if (!tm->tm_mon)
    2700            0 :                     break;
    2701          762 :                 if (IS_SUFFIX_TM(n->suffix))
    2702              :                 {
    2703            0 :                     char       *str = str_tolower_z(localized_full_months[tm->tm_mon - 1], collid);
    2704              : 
    2705            0 :                     if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
    2706            0 :                         strcpy(s, str);
    2707              :                     else
    2708            0 :                         ereport(ERROR,
    2709              :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2710              :                                  errmsg("localized string format value too long")));
    2711              :                 }
    2712              :                 else
    2713          762 :                     sprintf(s, "%*s", IS_SUFFIX_FM(n->suffix) ? 0 : -9,
    2714          762 :                             asc_tolower_z(months_full[tm->tm_mon - 1]));
    2715          762 :                 s += strlen(s);
    2716          762 :                 break;
    2717          381 :             case DCH_MON:
    2718          381 :                 INVALID_FOR_INTERVAL;
    2719          381 :                 if (!tm->tm_mon)
    2720            0 :                     break;
    2721          381 :                 if (IS_SUFFIX_TM(n->suffix))
    2722              :                 {
    2723            0 :                     char       *str = str_toupper_z(localized_abbrev_months[tm->tm_mon - 1], collid);
    2724              : 
    2725            0 :                     if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
    2726            0 :                         strcpy(s, str);
    2727              :                     else
    2728            0 :                         ereport(ERROR,
    2729              :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2730              :                                  errmsg("localized string format value too long")));
    2731              :                 }
    2732              :                 else
    2733          381 :                     strcpy(s, asc_toupper_z(months[tm->tm_mon - 1]));
    2734          381 :                 s += strlen(s);
    2735          381 :                 break;
    2736          423 :             case DCH_Mon:
    2737          423 :                 INVALID_FOR_INTERVAL;
    2738          423 :                 if (!tm->tm_mon)
    2739            0 :                     break;
    2740          423 :                 if (IS_SUFFIX_TM(n->suffix))
    2741              :                 {
    2742            0 :                     char       *str = str_initcap_z(localized_abbrev_months[tm->tm_mon - 1], collid);
    2743              : 
    2744            0 :                     if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
    2745            0 :                         strcpy(s, str);
    2746              :                     else
    2747            0 :                         ereport(ERROR,
    2748              :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2749              :                                  errmsg("localized string format value too long")));
    2750              :                 }
    2751              :                 else
    2752          423 :                     strcpy(s, months[tm->tm_mon - 1]);
    2753          423 :                 s += strlen(s);
    2754          423 :                 break;
    2755          381 :             case DCH_mon:
    2756          381 :                 INVALID_FOR_INTERVAL;
    2757          381 :                 if (!tm->tm_mon)
    2758            0 :                     break;
    2759          381 :                 if (IS_SUFFIX_TM(n->suffix))
    2760              :                 {
    2761            0 :                     char       *str = str_tolower_z(localized_abbrev_months[tm->tm_mon - 1], collid);
    2762              : 
    2763            0 :                     if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
    2764            0 :                         strcpy(s, str);
    2765              :                     else
    2766            0 :                         ereport(ERROR,
    2767              :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2768              :                                  errmsg("localized string format value too long")));
    2769              :                 }
    2770              :                 else
    2771          381 :                     strcpy(s, asc_tolower_z(months[tm->tm_mon - 1]));
    2772          381 :                 s += strlen(s);
    2773          381 :                 break;
    2774         1024 :             case DCH_MM:
    2775         1024 :                 sprintf(s, "%0*d", IS_SUFFIX_FM(n->suffix) ? 0 : (tm->tm_mon >= 0) ? 2 : 3,
    2776              :                         tm->tm_mon);
    2777         1024 :                 if (IS_SUFFIX_THth(n->suffix))
    2778            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2779         1024 :                 s += strlen(s);
    2780         1024 :                 break;
    2781          762 :             case DCH_DAY:
    2782          762 :                 INVALID_FOR_INTERVAL;
    2783          762 :                 if (IS_SUFFIX_TM(n->suffix))
    2784              :                 {
    2785            0 :                     char       *str = str_toupper_z(localized_full_days[tm->tm_wday], collid);
    2786              : 
    2787            0 :                     if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
    2788            0 :                         strcpy(s, str);
    2789              :                     else
    2790            0 :                         ereport(ERROR,
    2791              :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2792              :                                  errmsg("localized string format value too long")));
    2793              :                 }
    2794              :                 else
    2795          762 :                     sprintf(s, "%*s", IS_SUFFIX_FM(n->suffix) ? 0 : -9,
    2796          762 :                             asc_toupper_z(days[tm->tm_wday]));
    2797          762 :                 s += strlen(s);
    2798          762 :                 break;
    2799          762 :             case DCH_Day:
    2800          762 :                 INVALID_FOR_INTERVAL;
    2801          762 :                 if (IS_SUFFIX_TM(n->suffix))
    2802              :                 {
    2803            0 :                     char       *str = str_initcap_z(localized_full_days[tm->tm_wday], collid);
    2804              : 
    2805            0 :                     if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
    2806            0 :                         strcpy(s, str);
    2807              :                     else
    2808            0 :                         ereport(ERROR,
    2809              :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2810              :                                  errmsg("localized string format value too long")));
    2811              :                 }
    2812              :                 else
    2813          762 :                     sprintf(s, "%*s", IS_SUFFIX_FM(n->suffix) ? 0 : -9,
    2814          762 :                             days[tm->tm_wday]);
    2815          762 :                 s += strlen(s);
    2816          762 :                 break;
    2817          762 :             case DCH_day:
    2818          762 :                 INVALID_FOR_INTERVAL;
    2819          762 :                 if (IS_SUFFIX_TM(n->suffix))
    2820              :                 {
    2821            0 :                     char       *str = str_tolower_z(localized_full_days[tm->tm_wday], collid);
    2822              : 
    2823            0 :                     if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
    2824            0 :                         strcpy(s, str);
    2825              :                     else
    2826            0 :                         ereport(ERROR,
    2827              :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2828              :                                  errmsg("localized string format value too long")));
    2829              :                 }
    2830              :                 else
    2831          762 :                     sprintf(s, "%*s", IS_SUFFIX_FM(n->suffix) ? 0 : -9,
    2832          762 :                             asc_tolower_z(days[tm->tm_wday]));
    2833          762 :                 s += strlen(s);
    2834          762 :                 break;
    2835          381 :             case DCH_DY:
    2836          381 :                 INVALID_FOR_INTERVAL;
    2837          381 :                 if (IS_SUFFIX_TM(n->suffix))
    2838              :                 {
    2839            0 :                     char       *str = str_toupper_z(localized_abbrev_days[tm->tm_wday], collid);
    2840              : 
    2841            0 :                     if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
    2842            0 :                         strcpy(s, str);
    2843              :                     else
    2844            0 :                         ereport(ERROR,
    2845              :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2846              :                                  errmsg("localized string format value too long")));
    2847              :                 }
    2848              :                 else
    2849          381 :                     strcpy(s, asc_toupper_z(days_short[tm->tm_wday]));
    2850          381 :                 s += strlen(s);
    2851          381 :                 break;
    2852          381 :             case DCH_Dy:
    2853          381 :                 INVALID_FOR_INTERVAL;
    2854          381 :                 if (IS_SUFFIX_TM(n->suffix))
    2855              :                 {
    2856            0 :                     char       *str = str_initcap_z(localized_abbrev_days[tm->tm_wday], collid);
    2857              : 
    2858            0 :                     if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
    2859            0 :                         strcpy(s, str);
    2860              :                     else
    2861            0 :                         ereport(ERROR,
    2862              :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2863              :                                  errmsg("localized string format value too long")));
    2864              :                 }
    2865              :                 else
    2866          381 :                     strcpy(s, days_short[tm->tm_wday]);
    2867          381 :                 s += strlen(s);
    2868          381 :                 break;
    2869          381 :             case DCH_dy:
    2870          381 :                 INVALID_FOR_INTERVAL;
    2871          381 :                 if (IS_SUFFIX_TM(n->suffix))
    2872              :                 {
    2873            0 :                     char       *str = str_tolower_z(localized_abbrev_days[tm->tm_wday], collid);
    2874              : 
    2875            0 :                     if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
    2876            0 :                         strcpy(s, str);
    2877              :                     else
    2878            0 :                         ereport(ERROR,
    2879              :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2880              :                                  errmsg("localized string format value too long")));
    2881              :                 }
    2882              :                 else
    2883          381 :                     strcpy(s, asc_tolower_z(days_short[tm->tm_wday]));
    2884          381 :                 s += strlen(s);
    2885          381 :                 break;
    2886         1524 :             case DCH_DDD:
    2887              :             case DCH_IDDD:
    2888         1524 :                 sprintf(s, "%0*d", IS_SUFFIX_FM(n->suffix) ? 0 : 3,
    2889         1524 :                         (n->key->id == DCH_DDD) ?
    2890              :                         tm->tm_yday :
    2891          762 :                         date2isoyearday(tm->tm_year, tm->tm_mon, tm->tm_mday));
    2892         1524 :                 if (IS_SUFFIX_THth(n->suffix))
    2893            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2894         1524 :                 s += strlen(s);
    2895         1524 :                 break;
    2896          780 :             case DCH_DD:
    2897          780 :                 sprintf(s, "%0*d", IS_SUFFIX_FM(n->suffix) ? 0 : 2, tm->tm_mday);
    2898          780 :                 if (IS_SUFFIX_THth(n->suffix))
    2899            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2900          780 :                 s += strlen(s);
    2901          780 :                 break;
    2902          762 :             case DCH_D:
    2903          762 :                 INVALID_FOR_INTERVAL;
    2904          762 :                 sprintf(s, "%d", tm->tm_wday + 1);
    2905          762 :                 if (IS_SUFFIX_THth(n->suffix))
    2906            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2907          762 :                 s += strlen(s);
    2908          762 :                 break;
    2909          762 :             case DCH_ID:
    2910          762 :                 INVALID_FOR_INTERVAL;
    2911          762 :                 sprintf(s, "%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
    2912          762 :                 if (IS_SUFFIX_THth(n->suffix))
    2913            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2914          762 :                 s += strlen(s);
    2915          762 :                 break;
    2916          762 :             case DCH_WW:
    2917          762 :                 sprintf(s, "%0*d", IS_SUFFIX_FM(n->suffix) ? 0 : 2,
    2918          762 :                         (tm->tm_yday - 1) / 7 + 1);
    2919          762 :                 if (IS_SUFFIX_THth(n->suffix))
    2920            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2921          762 :                 s += strlen(s);
    2922          762 :                 break;
    2923          762 :             case DCH_IW:
    2924          762 :                 sprintf(s, "%0*d", IS_SUFFIX_FM(n->suffix) ? 0 : 2,
    2925              :                         date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday));
    2926          762 :                 if (IS_SUFFIX_THth(n->suffix))
    2927            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2928          762 :                 s += strlen(s);
    2929          762 :                 break;
    2930          762 :             case DCH_Q:
    2931          762 :                 if (!tm->tm_mon)
    2932            0 :                     break;
    2933          762 :                 sprintf(s, "%d", (tm->tm_mon - 1) / 3 + 1);
    2934          762 :                 if (IS_SUFFIX_THth(n->suffix))
    2935            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2936          762 :                 s += strlen(s);
    2937          762 :                 break;
    2938          762 :             case DCH_CC:
    2939          762 :                 if (is_interval)    /* straight calculation */
    2940            0 :                     i = tm->tm_year / 100;
    2941              :                 else
    2942              :                 {
    2943          762 :                     if (tm->tm_year > 0)
    2944              :                         /* Century 20 == 1901 - 2000 */
    2945          750 :                         i = (tm->tm_year - 1) / 100 + 1;
    2946              :                     else
    2947              :                         /* Century 6BC == 600BC - 501BC */
    2948           12 :                         i = tm->tm_year / 100 - 1;
    2949              :                 }
    2950          762 :                 if (i <= 99 && i >= -99)
    2951          762 :                     sprintf(s, "%0*d", IS_SUFFIX_FM(n->suffix) ? 0 : (i >= 0) ? 2 : 3, i);
    2952              :                 else
    2953            0 :                     sprintf(s, "%d", i);
    2954          762 :                 if (IS_SUFFIX_THth(n->suffix))
    2955            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2956          762 :                 s += strlen(s);
    2957          762 :                 break;
    2958          762 :             case DCH_Y_YYY:
    2959          762 :                 i = ADJUST_YEAR(tm->tm_year, is_interval) / 1000;
    2960          762 :                 sprintf(s, "%d,%03d", i,
    2961          762 :                         ADJUST_YEAR(tm->tm_year, is_interval) - (i * 1000));
    2962          762 :                 if (IS_SUFFIX_THth(n->suffix))
    2963            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2964          762 :                 s += strlen(s);
    2965          762 :                 break;
    2966         3691 :             case DCH_YYYY:
    2967              :             case DCH_IYYY:
    2968        10311 :                 sprintf(s, "%0*d",
    2969         3691 :                         IS_SUFFIX_FM(n->suffix) ? 0 :
    2970         2929 :                         (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 4 : 5,
    2971         3691 :                         (n->key->id == DCH_YYYY ?
    2972         2929 :                          ADJUST_YEAR(tm->tm_year, is_interval) :
    2973          762 :                          ADJUST_YEAR(date2isoyear(tm->tm_year,
    2974              :                                                   tm->tm_mon,
    2975              :                                                   tm->tm_mday),
    2976              :                                      is_interval)));
    2977         3691 :                 if (IS_SUFFIX_THth(n->suffix))
    2978          762 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2979         3691 :                 s += strlen(s);
    2980         3691 :                 break;
    2981         1524 :             case DCH_YYY:
    2982              :             case DCH_IYY:
    2983         3810 :                 sprintf(s, "%0*d",
    2984         1524 :                         IS_SUFFIX_FM(n->suffix) ? 0 :
    2985          762 :                         (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 3 : 4,
    2986         1524 :                         (n->key->id == DCH_YYY ?
    2987         1524 :                          ADJUST_YEAR(tm->tm_year, is_interval) :
    2988         1524 :                          ADJUST_YEAR(date2isoyear(tm->tm_year,
    2989              :                                                   tm->tm_mon,
    2990              :                                                   tm->tm_mday),
    2991         3048 :                                      is_interval)) % 1000);
    2992         1524 :                 if (IS_SUFFIX_THth(n->suffix))
    2993            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    2994         1524 :                 s += strlen(s);
    2995         1524 :                 break;
    2996         1524 :             case DCH_YY:
    2997              :             case DCH_IY:
    2998         3810 :                 sprintf(s, "%0*d",
    2999         1524 :                         IS_SUFFIX_FM(n->suffix) ? 0 :
    3000          762 :                         (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 2 : 3,
    3001         1524 :                         (n->key->id == DCH_YY ?
    3002         1524 :                          ADJUST_YEAR(tm->tm_year, is_interval) :
    3003         1524 :                          ADJUST_YEAR(date2isoyear(tm->tm_year,
    3004              :                                                   tm->tm_mon,
    3005              :                                                   tm->tm_mday),
    3006         3048 :                                      is_interval)) % 100);
    3007         1524 :                 if (IS_SUFFIX_THth(n->suffix))
    3008            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    3009         1524 :                 s += strlen(s);
    3010         1524 :                 break;
    3011         1524 :             case DCH_Y:
    3012              :             case DCH_I:
    3013         1524 :                 sprintf(s, "%1d",
    3014         1524 :                         (n->key->id == DCH_Y ?
    3015         1524 :                          ADJUST_YEAR(tm->tm_year, is_interval) :
    3016         1524 :                          ADJUST_YEAR(date2isoyear(tm->tm_year,
    3017              :                                                   tm->tm_mon,
    3018              :                                                   tm->tm_mday),
    3019         3048 :                                      is_interval)) % 10);
    3020         1524 :                 if (IS_SUFFIX_THth(n->suffix))
    3021            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    3022         1524 :                 s += strlen(s);
    3023         1524 :                 break;
    3024          924 :             case DCH_RM:
    3025              :                 /* FALLTHROUGH */
    3026              :             case DCH_rm:
    3027              : 
    3028              :                 /*
    3029              :                  * For intervals, values like '12 month' will be reduced to 0
    3030              :                  * month and some years.  These should be processed.
    3031              :                  */
    3032          924 :                 if (!tm->tm_mon && !tm->tm_year)
    3033              :                     break;
    3034              :                 else
    3035              :                 {
    3036          918 :                     int         mon = 0;
    3037              :                     const char *const *months;
    3038              : 
    3039          918 :                     if (n->key->id == DCH_RM)
    3040          840 :                         months = rm_months_upper;
    3041              :                     else
    3042           78 :                         months = rm_months_lower;
    3043              : 
    3044              :                     /*
    3045              :                      * Compute the position in the roman-numeral array.  Note
    3046              :                      * that the contents of the array are reversed, December
    3047              :                      * being first and January last.
    3048              :                      */
    3049          918 :                     if (tm->tm_mon == 0)
    3050              :                     {
    3051              :                         /*
    3052              :                          * This case is special, and tracks the case of full
    3053              :                          * interval years.
    3054              :                          */
    3055           12 :                         mon = tm->tm_year >= 0 ? 0 : MONTHS_PER_YEAR - 1;
    3056              :                     }
    3057          906 :                     else if (tm->tm_mon < 0)
    3058              :                     {
    3059              :                         /*
    3060              :                          * Negative case.  In this case, the calculation is
    3061              :                          * reversed, where -1 means December, -2 November,
    3062              :                          * etc.
    3063              :                          */
    3064           72 :                         mon = -1 * (tm->tm_mon + 1);
    3065              :                     }
    3066              :                     else
    3067              :                     {
    3068              :                         /*
    3069              :                          * Common case, with a strictly positive value.  The
    3070              :                          * position in the array matches with the value of
    3071              :                          * tm_mon.
    3072              :                          */
    3073          834 :                         mon = MONTHS_PER_YEAR - tm->tm_mon;
    3074              :                     }
    3075              : 
    3076          918 :                     sprintf(s, "%*s", IS_SUFFIX_FM(n->suffix) ? 0 : -4,
    3077          918 :                             months[mon]);
    3078          918 :                     s += strlen(s);
    3079              :                 }
    3080          918 :                 break;
    3081            0 :             case DCH_W:
    3082            0 :                 sprintf(s, "%d", (tm->tm_mday - 1) / 7 + 1);
    3083            0 :                 if (IS_SUFFIX_THth(n->suffix))
    3084            0 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    3085            0 :                 s += strlen(s);
    3086            0 :                 break;
    3087         1143 :             case DCH_J:
    3088         1143 :                 sprintf(s, "%d", date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
    3089         1143 :                 if (IS_SUFFIX_THth(n->suffix))
    3090          381 :                     str_numth(s, s, SUFFIX_TH_TYPE(n->suffix));
    3091         1143 :                 s += strlen(s);
    3092         1143 :                 break;
    3093              :         }
    3094              :     }
    3095              : 
    3096         4793 :     *s = '\0';
    3097         4793 : }
    3098              : 
    3099              : /*
    3100              :  * Process the string 'in' as denoted by the array of FormatNodes 'node[]'.
    3101              :  * The TmFromChar struct pointed to by 'out' is populated with the results.
    3102              :  *
    3103              :  * 'collid' identifies the collation to use, if needed.
    3104              :  * 'std' specifies standard parsing mode.
    3105              :  *
    3106              :  * If escontext points to an ErrorSaveContext, data errors will be reported
    3107              :  * by filling that struct; the caller must test SOFT_ERROR_OCCURRED() to see
    3108              :  * whether an error occurred.  Otherwise, errors are thrown.
    3109              :  *
    3110              :  * Note: we currently don't have any to_interval() function, so there
    3111              :  * is no need here for INVALID_FOR_INTERVAL checks.
    3112              :  */
    3113              : static void
    3114        20224 : DCH_from_char(FormatNode *node, const char *in, TmFromChar *out,
    3115              :               Oid collid, bool std, Node *escontext)
    3116              : {
    3117              :     FormatNode *n;
    3118              :     const char *s;
    3119              :     int         len,
    3120              :                 value;
    3121        20224 :     bool        fx_mode = std;
    3122              : 
    3123              :     /* number of extra skipped characters (more than given in format string) */
    3124        20224 :     int         extra_skip = 0;
    3125              : 
    3126              :     /* cache localized days and months */
    3127        20224 :     cache_locale_time();
    3128              : 
    3129       121600 :     for (n = node, s = in; n->type != NODE_TYPE_END && *s != '\0'; n++)
    3130              :     {
    3131              :         /*
    3132              :          * Ignore spaces at the beginning of the string and before fields when
    3133              :          * not in FX (fixed width) mode.
    3134              :          */
    3135       112087 :         if (!fx_mode && (n->type != NODE_TYPE_ACTION || n->key->id != DCH_FX) &&
    3136         4678 :             (n->type == NODE_TYPE_ACTION || n == node))
    3137              :         {
    3138         2641 :             while (*s != '\0' && isspace((unsigned char) *s))
    3139              :             {
    3140           42 :                 s++;
    3141           42 :                 extra_skip++;
    3142              :             }
    3143              :         }
    3144              : 
    3145       112087 :         if (n->type == NODE_TYPE_SPACE || n->type == NODE_TYPE_SEPARATOR)
    3146              :         {
    3147        48615 :             if (std)
    3148              :             {
    3149              :                 /*
    3150              :                  * Standard mode requires strict matching between format
    3151              :                  * string separators/spaces and input string.
    3152              :                  */
    3153              :                 Assert(n->character[0] && !n->character[1]);
    3154              : 
    3155        46740 :                 if (*s == n->character[0])
    3156        37830 :                     s++;
    3157              :                 else
    3158        15546 :                     ereturn(escontext,,
    3159              :                             (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    3160              :                              errmsg("unmatched format separator \"%c\"",
    3161              :                                     n->character[0])));
    3162              :             }
    3163         1875 :             else if (!fx_mode)
    3164              :             {
    3165              :                 /*
    3166              :                  * In non FX (fixed format) mode one format string space or
    3167              :                  * separator match to one space or separator in input string.
    3168              :                  * Or match nothing if there is no space or separator in the
    3169              :                  * current position of input string.
    3170              :                  */
    3171         1863 :                 extra_skip--;
    3172         1863 :                 if (isspace((unsigned char) *s) || is_separator_char(s))
    3173              :                 {
    3174         1329 :                     s++;
    3175         1329 :                     extra_skip++;
    3176              :                 }
    3177              :             }
    3178              :             else
    3179              :             {
    3180              :                 /*
    3181              :                  * In FX mode, on format string space or separator we consume
    3182              :                  * exactly one character from input string.  Notice we don't
    3183              :                  * insist that the consumed character match the format's
    3184              :                  * character.
    3185              :                  */
    3186           12 :                 s += pg_mblen_cstr(s);
    3187              :             }
    3188        39705 :             continue;
    3189              :         }
    3190        63472 :         else if (n->type != NODE_TYPE_ACTION)
    3191              :         {
    3192              :             /*
    3193              :              * Text character, so consume one character from input string.
    3194              :              * Notice we don't insist that the consumed character match the
    3195              :              * format's character.
    3196              :              */
    3197         1593 :             if (!fx_mode)
    3198              :             {
    3199              :                 /*
    3200              :                  * In non FX mode we might have skipped some extra characters
    3201              :                  * (more than specified in format string) before.  In this
    3202              :                  * case we don't skip input string character, because it might
    3203              :                  * be part of field.
    3204              :                  */
    3205          219 :                 if (extra_skip > 0)
    3206           12 :                     extra_skip--;
    3207              :                 else
    3208          207 :                     s += pg_mblen_cstr(s);
    3209              :             }
    3210              :             else
    3211              :             {
    3212         1374 :                 int         chlen = pg_mblen_cstr(s);
    3213              : 
    3214              :                 /*
    3215              :                  * Standard mode requires strict match of format characters.
    3216              :                  */
    3217         1374 :                 if (std && n->type == NODE_TYPE_CHAR &&
    3218         1374 :                     strncmp(s, n->character, chlen) != 0)
    3219         1356 :                     ereturn(escontext,,
    3220              :                             (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    3221              :                              errmsg("unmatched format character \"%s\"",
    3222              :                                     n->character)));
    3223              : 
    3224           18 :                 s += chlen;
    3225              :             }
    3226          237 :             continue;
    3227              :         }
    3228              : 
    3229        61879 :         if (!from_char_set_mode(out, n->key->date_mode, escontext))
    3230            0 :             return;
    3231              : 
    3232        61876 :         switch (n->key->id)
    3233              :         {
    3234            6 :             case DCH_FX:
    3235            6 :                 fx_mode = true;
    3236            6 :                 break;
    3237            6 :             case DCH_A_M:
    3238              :             case DCH_P_M:
    3239              :             case DCH_a_m:
    3240              :             case DCH_p_m:
    3241            6 :                 if (!from_char_seq_search(&value, &s, ampm_strings_long,
    3242              :                                           NULL, InvalidOid,
    3243              :                                           n, escontext))
    3244            0 :                     return;
    3245            6 :                 if (!from_char_set_int(&out->pm, value % 2, n, escontext))
    3246            0 :                     return;
    3247            6 :                 out->clock_12_hour = true;
    3248            6 :                 break;
    3249            6 :             case DCH_AM:
    3250              :             case DCH_PM:
    3251              :             case DCH_am:
    3252              :             case DCH_pm:
    3253            6 :                 if (!from_char_seq_search(&value, &s, ampm_strings,
    3254              :                                           NULL, InvalidOid,
    3255              :                                           n, escontext))
    3256            0 :                     return;
    3257            6 :                 if (!from_char_set_int(&out->pm, value % 2, n, escontext))
    3258            0 :                     return;
    3259            6 :                 out->clock_12_hour = true;
    3260            6 :                 break;
    3261           72 :             case DCH_HH:
    3262              :             case DCH_HH12:
    3263           72 :                 if (from_char_parse_int_len(&out->hh, &s, 2, n, escontext) < 0)
    3264            0 :                     return;
    3265           72 :                 out->clock_12_hour = true;
    3266           72 :                 SKIP_THth(s, n->suffix);
    3267           72 :                 break;
    3268        14841 :             case DCH_HH24:
    3269        14841 :                 if (from_char_parse_int_len(&out->hh, &s, 2, n, escontext) < 0)
    3270           72 :                     return;
    3271        14766 :                 SKIP_THth(s, n->suffix);
    3272        14766 :                 break;
    3273         8610 :             case DCH_MI:
    3274         8610 :                 if (from_char_parse_int(&out->mi, &s, n, escontext) < 0)
    3275            0 :                     return;
    3276         8610 :                 SKIP_THth(s, n->suffix);
    3277         8610 :                 break;
    3278         7917 :             case DCH_SS:
    3279         7917 :                 if (from_char_parse_int(&out->ss, &s, n, escontext) < 0)
    3280            0 :                     return;
    3281         7917 :                 SKIP_THth(s, n->suffix);
    3282         7917 :                 break;
    3283            6 :             case DCH_MS:        /* millisecond */
    3284            6 :                 len = from_char_parse_int_len(&out->ms, &s, 3, n, escontext);
    3285            6 :                 if (len < 0)
    3286            0 :                     return;
    3287              : 
    3288              :                 /*
    3289              :                  * 25 is 0.25 and 250 is 0.25 too; 025 is 0.025 and not 0.25
    3290              :                  */
    3291           12 :                 out->ms *= len == 1 ? 100 :
    3292            6 :                     len == 2 ? 10 : 1;
    3293              : 
    3294            6 :                 SKIP_THth(s, n->suffix);
    3295            6 :                 break;
    3296          129 :             case DCH_FF1:
    3297              :             case DCH_FF2:
    3298              :             case DCH_FF3:
    3299              :             case DCH_FF4:
    3300              :             case DCH_FF5:
    3301              :             case DCH_FF6:
    3302          129 :                 out->ff = n->key->id - DCH_FF1 + 1;
    3303              :                 /* FALLTHROUGH */
    3304          666 :             case DCH_US:        /* microsecond */
    3305          666 :                 len = from_char_parse_int_len(&out->us, &s,
    3306          666 :                                               n->key->id == DCH_US ? 6 :
    3307          129 :                                               out->ff, n, escontext);
    3308          666 :                 if (len < 0)
    3309            0 :                     return;
    3310              : 
    3311         1293 :                 out->us *= len == 1 ? 100000 :
    3312         1236 :                     len == 2 ? 10000 :
    3313          762 :                     len == 3 ? 1000 :
    3314          228 :                     len == 4 ? 100 :
    3315           75 :                     len == 5 ? 10 : 1;
    3316              : 
    3317          666 :                 SKIP_THth(s, n->suffix);
    3318          666 :                 break;
    3319           12 :             case DCH_SSSS:
    3320           12 :                 if (from_char_parse_int(&out->ssss, &s, n, escontext) < 0)
    3321            0 :                     return;
    3322           12 :                 SKIP_THth(s, n->suffix);
    3323           12 :                 break;
    3324         1779 :             case DCH_tz:
    3325              :             case DCH_TZ:
    3326              :                 {
    3327              :                     int         tzlen;
    3328              : 
    3329         1779 :                     tzlen = DecodeTimezoneAbbrevPrefix(s,
    3330              :                                                        &out->gmtoffset,
    3331              :                                                        &out->tzp);
    3332         1779 :                     if (tzlen > 0)
    3333              :                     {
    3334           18 :                         out->has_tz = true;
    3335              :                         /* we only need the zone abbrev for DYNTZ case */
    3336           18 :                         if (out->tzp)
    3337            3 :                             out->abbrev = pnstrdup(s, tzlen);
    3338           18 :                         out->tzsign = 0; /* drop any earlier TZH/TZM info */
    3339           18 :                         s += tzlen;
    3340           18 :                         break;
    3341              :                     }
    3342         1761 :                     else if (isalpha((unsigned char) *s))
    3343              :                     {
    3344              :                         /*
    3345              :                          * It doesn't match any abbreviation, but it starts
    3346              :                          * with a letter.  OF format certainly won't succeed;
    3347              :                          * assume it's a misspelled abbreviation and complain
    3348              :                          * accordingly.
    3349              :                          */
    3350            3 :                         ereturn(escontext,,
    3351              :                                 (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    3352              :                                  errmsg("invalid value \"%s\" for \"%s\"", s, n->key->name),
    3353              :                                  errdetail("Time zone abbreviation is not recognized.")));
    3354              :                     }
    3355              :                     /* otherwise parse it like OF */
    3356              :                 }
    3357              :                 /* FALLTHROUGH */
    3358              :             case DCH_OF:
    3359              :                 /* OF is equivalent to TZH or TZH:TZM */
    3360              :                 /* see TZH comments below */
    3361         1770 :                 if (*s == '+' || *s == '-' || *s == ' ')
    3362              :                 {
    3363         1590 :                     out->tzsign = *s == '-' ? -1 : +1;
    3364         1590 :                     s++;
    3365              :                 }
    3366              :                 else
    3367              :                 {
    3368          180 :                     if (extra_skip > 0 && *(s - 1) == '-')
    3369            6 :                         out->tzsign = -1;
    3370              :                     else
    3371          174 :                         out->tzsign = +1;
    3372              :                 }
    3373         1770 :                 if (from_char_parse_int_len(&out->tzh, &s, 2, n, escontext) < 0)
    3374          159 :                     return;
    3375         1605 :                 if (*s == ':')
    3376              :                 {
    3377          165 :                     s++;
    3378          165 :                     if (from_char_parse_int_len(&out->tzm, &s, 2, n,
    3379              :                                                 escontext) < 0)
    3380            0 :                         return;
    3381              :                 }
    3382         1602 :                 break;
    3383          387 :             case DCH_TZH:
    3384              : 
    3385              :                 /*
    3386              :                  * Value of TZH might be negative.  And the issue is that we
    3387              :                  * might swallow minus sign as the separator.  So, if we have
    3388              :                  * skipped more characters than specified in the format
    3389              :                  * string, then we consider prepending last skipped minus to
    3390              :                  * TZH.
    3391              :                  */
    3392          387 :                 if (*s == '+' || *s == '-' || *s == ' ')
    3393              :                 {
    3394          369 :                     out->tzsign = *s == '-' ? -1 : +1;
    3395          369 :                     s++;
    3396              :                 }
    3397              :                 else
    3398              :                 {
    3399           18 :                     if (extra_skip > 0 && *(s - 1) == '-')
    3400            9 :                         out->tzsign = -1;
    3401              :                     else
    3402            9 :                         out->tzsign = +1;
    3403              :                 }
    3404              : 
    3405          387 :                 if (from_char_parse_int_len(&out->tzh, &s, 2, n, escontext) < 0)
    3406            0 :                     return;
    3407          387 :                 break;
    3408           39 :             case DCH_TZM:
    3409              :                 /* assign positive timezone sign if TZH was not seen before */
    3410           39 :                 if (!out->tzsign)
    3411            3 :                     out->tzsign = +1;
    3412           39 :                 if (from_char_parse_int_len(&out->tzm, &s, 2, n, escontext) < 0)
    3413            0 :                     return;
    3414           39 :                 break;
    3415            6 :             case DCH_A_D:
    3416              :             case DCH_B_C:
    3417              :             case DCH_a_d:
    3418              :             case DCH_b_c:
    3419            6 :                 if (!from_char_seq_search(&value, &s, adbc_strings_long,
    3420              :                                           NULL, InvalidOid,
    3421              :                                           n, escontext))
    3422            0 :                     return;
    3423            6 :                 if (!from_char_set_int(&out->bc, value % 2, n, escontext))
    3424            0 :                     return;
    3425            6 :                 break;
    3426           18 :             case DCH_AD:
    3427              :             case DCH_BC:
    3428              :             case DCH_ad:
    3429              :             case DCH_bc:
    3430           18 :                 if (!from_char_seq_search(&value, &s, adbc_strings,
    3431              :                                           NULL, InvalidOid,
    3432              :                                           n, escontext))
    3433            0 :                     return;
    3434           18 :                 if (!from_char_set_int(&out->bc, value % 2, n, escontext))
    3435            0 :                     return;
    3436           18 :                 break;
    3437            9 :             case DCH_MONTH:
    3438              :             case DCH_Month:
    3439              :             case DCH_month:
    3440            9 :                 if (!from_char_seq_search(&value, &s, months_full,
    3441            9 :                                           IS_SUFFIX_TM(n->suffix) ? localized_full_months : NULL,
    3442              :                                           collid,
    3443              :                                           n, escontext))
    3444            0 :                     return;
    3445            9 :                 if (!from_char_set_int(&out->mm, value + 1, n, escontext))
    3446            0 :                     return;
    3447            9 :                 break;
    3448           60 :             case DCH_MON:
    3449              :             case DCH_Mon:
    3450              :             case DCH_mon:
    3451           60 :                 if (!from_char_seq_search(&value, &s, months,
    3452           60 :                                           IS_SUFFIX_TM(n->suffix) ? localized_abbrev_months : NULL,
    3453              :                                           collid,
    3454              :                                           n, escontext))
    3455            0 :                     return;
    3456           54 :                 if (!from_char_set_int(&out->mm, value + 1, n, escontext))
    3457            0 :                     return;
    3458           51 :                 break;
    3459         8577 :             case DCH_MM:
    3460         8577 :                 if (from_char_parse_int(&out->mm, &s, n, escontext) < 0)
    3461            0 :                     return;
    3462         8568 :                 SKIP_THth(s, n->suffix);
    3463         8568 :                 break;
    3464            3 :             case DCH_DAY:
    3465              :             case DCH_Day:
    3466              :             case DCH_day:
    3467            3 :                 if (!from_char_seq_search(&value, &s, days,
    3468            3 :                                           IS_SUFFIX_TM(n->suffix) ? localized_full_days : NULL,
    3469              :                                           collid,
    3470              :                                           n, escontext))
    3471            0 :                     return;
    3472            3 :                 if (!from_char_set_int(&out->d, value, n, escontext))
    3473            0 :                     return;
    3474            3 :                 out->d++;
    3475            3 :                 break;
    3476            9 :             case DCH_DY:
    3477              :             case DCH_Dy:
    3478              :             case DCH_dy:
    3479            9 :                 if (!from_char_seq_search(&value, &s, days_short,
    3480            9 :                                           IS_SUFFIX_TM(n->suffix) ? localized_abbrev_days : NULL,
    3481              :                                           collid,
    3482              :                                           n, escontext))
    3483            0 :                     return;
    3484            9 :                 if (!from_char_set_int(&out->d, value, n, escontext))
    3485            0 :                     return;
    3486            9 :                 out->d++;
    3487            9 :                 break;
    3488           18 :             case DCH_DDD:
    3489           18 :                 if (from_char_parse_int(&out->ddd, &s, n, escontext) < 0)
    3490            0 :                     return;
    3491           18 :                 SKIP_THth(s, n->suffix);
    3492           18 :                 break;
    3493            3 :             case DCH_IDDD:
    3494            3 :                 if (from_char_parse_int_len(&out->ddd, &s, 3, n, escontext) < 0)
    3495            0 :                     return;
    3496            3 :                 SKIP_THth(s, n->suffix);
    3497            3 :                 break;
    3498         8611 :             case DCH_DD:
    3499         8611 :                 if (from_char_parse_int(&out->dd, &s, n, escontext) < 0)
    3500            0 :                     return;
    3501         8604 :                 SKIP_THth(s, n->suffix);
    3502         8604 :                 break;
    3503            6 :             case DCH_D:
    3504            6 :                 if (from_char_parse_int(&out->d, &s, n, escontext) < 0)
    3505            0 :                     return;
    3506            6 :                 SKIP_THth(s, n->suffix);
    3507            6 :                 break;
    3508           12 :             case DCH_ID:
    3509           12 :                 if (from_char_parse_int_len(&out->d, &s, 1, n, escontext) < 0)
    3510            0 :                     return;
    3511              :                 /* Shift numbering to match Gregorian where Sunday = 1 */
    3512           12 :                 if (++out->d > 7)
    3513           12 :                     out->d = 1;
    3514           12 :                 SKIP_THth(s, n->suffix);
    3515           12 :                 break;
    3516           18 :             case DCH_WW:
    3517              :             case DCH_IW:
    3518           18 :                 if (from_char_parse_int(&out->ww, &s, n, escontext) < 0)
    3519            0 :                     return;
    3520           18 :                 SKIP_THth(s, n->suffix);
    3521           18 :                 break;
    3522            3 :             case DCH_Q:
    3523              : 
    3524              :                 /*
    3525              :                  * We ignore 'Q' when converting to date because it is unclear
    3526              :                  * which date in the quarter to use, and some people specify
    3527              :                  * both quarter and month, so if it was honored it might
    3528              :                  * conflict with the supplied month. That is also why we don't
    3529              :                  * throw an error.
    3530              :                  *
    3531              :                  * We still parse the source string for an integer, but it
    3532              :                  * isn't stored anywhere in 'out'.
    3533              :                  */
    3534            3 :                 if (from_char_parse_int((int *) NULL, &s, n, escontext) < 0)
    3535            0 :                     return;
    3536            3 :                 SKIP_THth(s, n->suffix);
    3537            3 :                 break;
    3538           15 :             case DCH_CC:
    3539           15 :                 if (from_char_parse_int(&out->cc, &s, n, escontext) < 0)
    3540            0 :                     return;
    3541           15 :                 SKIP_THth(s, n->suffix);
    3542           15 :                 break;
    3543            6 :             case DCH_Y_YYY:
    3544              :                 {
    3545              :                     int         matched,
    3546              :                                 years,
    3547              :                                 millennia,
    3548              :                                 nch;
    3549              : 
    3550            6 :                     matched = sscanf(s, "%d,%03d%n", &millennia, &years, &nch);
    3551            6 :                     if (matched < 2)
    3552            0 :                         ereturn(escontext,,
    3553              :                                 (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    3554              :                                  errmsg("invalid value \"%s\" for \"%s\"", s, "Y,YYY")));
    3555              : 
    3556              :                     /* years += (millennia * 1000); */
    3557            9 :                     if (pg_mul_s32_overflow(millennia, 1000, &millennia) ||
    3558            3 :                         pg_add_s32_overflow(years, millennia, &years))
    3559            3 :                         ereturn(escontext,,
    3560              :                                 (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
    3561              :                                  errmsg("value for \"%s\" in source string is out of range", "Y,YYY")));
    3562              : 
    3563            3 :                     if (!from_char_set_int(&out->year, years, n, escontext))
    3564            0 :                         return;
    3565            3 :                     out->yysz = 4;
    3566            3 :                     s += nch;
    3567            3 :                     SKIP_THth(s, n->suffix);
    3568              :                 }
    3569            3 :                 break;
    3570        10089 :             case DCH_YYYY:
    3571              :             case DCH_IYYY:
    3572        10089 :                 if (from_char_parse_int(&out->year, &s, n, escontext) < 0)
    3573          162 :                     return;
    3574         9921 :                 out->yysz = 4;
    3575         9921 :                 SKIP_THth(s, n->suffix);
    3576         9921 :                 break;
    3577            6 :             case DCH_YYY:
    3578              :             case DCH_IYY:
    3579            6 :                 len = from_char_parse_int(&out->year, &s, n, escontext);
    3580            6 :                 if (len < 0)
    3581            0 :                     return;
    3582            6 :                 if (len < 4)
    3583            6 :                     out->year = adjust_partial_year_to_2020(out->year);
    3584            6 :                 out->yysz = 3;
    3585            6 :                 SKIP_THth(s, n->suffix);
    3586            6 :                 break;
    3587           30 :             case DCH_YY:
    3588              :             case DCH_IY:
    3589           30 :                 len = from_char_parse_int(&out->year, &s, n, escontext);
    3590           30 :                 if (len < 0)
    3591            0 :                     return;
    3592           30 :                 if (len < 4)
    3593           30 :                     out->year = adjust_partial_year_to_2020(out->year);
    3594           30 :                 out->yysz = 2;
    3595           30 :                 SKIP_THth(s, n->suffix);
    3596           30 :                 break;
    3597            6 :             case DCH_Y:
    3598              :             case DCH_I:
    3599            6 :                 len = from_char_parse_int(&out->year, &s, n, escontext);
    3600            6 :                 if (len < 0)
    3601            0 :                     return;
    3602            6 :                 if (len < 4)
    3603            6 :                     out->year = adjust_partial_year_to_2020(out->year);
    3604            6 :                 out->yysz = 1;
    3605            6 :                 SKIP_THth(s, n->suffix);
    3606            6 :                 break;
    3607            3 :             case DCH_RM:
    3608              :             case DCH_rm:
    3609            3 :                 if (!from_char_seq_search(&value, &s, rm_months_lower,
    3610              :                                           NULL, InvalidOid,
    3611              :                                           n, escontext))
    3612            0 :                     return;
    3613            3 :                 if (!from_char_set_int(&out->mm, MONTHS_PER_YEAR - value, n,
    3614              :                                        escontext))
    3615            0 :                     return;
    3616            3 :                 break;
    3617            6 :             case DCH_W:
    3618            6 :                 if (from_char_parse_int(&out->w, &s, n, escontext) < 0)
    3619            0 :                     return;
    3620            6 :                 SKIP_THth(s, n->suffix);
    3621            6 :                 break;
    3622            3 :             case DCH_J:
    3623            3 :                 if (from_char_parse_int(&out->j, &s, n, escontext) < 0)
    3624            0 :                     return;
    3625            3 :                 SKIP_THth(s, n->suffix);
    3626            3 :                 break;
    3627              :         }
    3628              : 
    3629              :         /* Ignore all spaces after fields */
    3630        61434 :         if (!fx_mode)
    3631              :         {
    3632         2547 :             extra_skip = 0;
    3633         3150 :             while (*s != '\0' && isspace((unsigned char) *s))
    3634              :             {
    3635          603 :                 s++;
    3636          603 :                 extra_skip++;
    3637              :             }
    3638              :         }
    3639              :     }
    3640              : 
    3641              :     /*
    3642              :      * Standard parsing mode doesn't allow unmatched format patterns or
    3643              :      * trailing characters in the input string.
    3644              :      */
    3645         9513 :     if (std)
    3646              :     {
    3647         8997 :         if (n->type != NODE_TYPE_END)
    3648         3348 :             ereturn(escontext,,
    3649              :                     (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    3650              :                      errmsg("input string is too short for datetime format")));
    3651              : 
    3652         7194 :         while (*s != '\0' && isspace((unsigned char) *s))
    3653         1545 :             s++;
    3654              : 
    3655         5649 :         if (*s != '\0')
    3656         1563 :             ereturn(escontext,,
    3657              :                     (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    3658              :                      errmsg("trailing characters remain in input string after datetime format")));
    3659              :     }
    3660              : }
    3661              : 
    3662              : /*
    3663              :  * The invariant for DCH cache entry management is that DCHCounter is equal
    3664              :  * to the maximum age value among the existing entries, and we increment it
    3665              :  * whenever an access occurs.  If we approach overflow, deal with that by
    3666              :  * halving all the age values, so that we retain a fairly accurate idea of
    3667              :  * which entries are oldest.
    3668              :  */
    3669              : static inline void
    3670        25522 : DCH_prevent_counter_overflow(void)
    3671              : {
    3672        25522 :     if (DCHCounter >= (INT_MAX - 1))
    3673              :     {
    3674            0 :         for (int i = 0; i < n_DCHCache; i++)
    3675            0 :             DCHCache[i]->age >>= 1;
    3676            0 :         DCHCounter >>= 1;
    3677              :     }
    3678        25522 : }
    3679              : 
    3680              : /*
    3681              :  * Get mask of date/time/zone components present in format nodes.
    3682              :  */
    3683              : static int
    3684         4119 : DCH_datetime_type(FormatNode *node)
    3685              : {
    3686         4119 :     int         flags = 0;
    3687              : 
    3688        37893 :     for (FormatNode *n = node; n->type != NODE_TYPE_END; n++)
    3689              :     {
    3690        33774 :         if (n->type != NODE_TYPE_ACTION)
    3691        14031 :             continue;
    3692              : 
    3693        19743 :         switch (n->key->id)
    3694              :         {
    3695            0 :             case DCH_FX:
    3696            0 :                 break;
    3697        10146 :             case DCH_A_M:
    3698              :             case DCH_P_M:
    3699              :             case DCH_a_m:
    3700              :             case DCH_p_m:
    3701              :             case DCH_AM:
    3702              :             case DCH_PM:
    3703              :             case DCH_am:
    3704              :             case DCH_pm:
    3705              :             case DCH_HH:
    3706              :             case DCH_HH12:
    3707              :             case DCH_HH24:
    3708              :             case DCH_MI:
    3709              :             case DCH_SS:
    3710              :             case DCH_MS:        /* millisecond */
    3711              :             case DCH_US:        /* microsecond */
    3712              :             case DCH_FF1:
    3713              :             case DCH_FF2:
    3714              :             case DCH_FF3:
    3715              :             case DCH_FF4:
    3716              :             case DCH_FF5:
    3717              :             case DCH_FF6:
    3718              :             case DCH_SSSS:
    3719        10146 :                 flags |= DCH_TIMED;
    3720        10146 :                 break;
    3721         2010 :             case DCH_tz:
    3722              :             case DCH_TZ:
    3723              :             case DCH_OF:
    3724              :             case DCH_TZH:
    3725              :             case DCH_TZM:
    3726         2010 :                 flags |= DCH_ZONED;
    3727         2010 :                 break;
    3728         7587 :             case DCH_A_D:
    3729              :             case DCH_B_C:
    3730              :             case DCH_a_d:
    3731              :             case DCH_b_c:
    3732              :             case DCH_AD:
    3733              :             case DCH_BC:
    3734              :             case DCH_ad:
    3735              :             case DCH_bc:
    3736              :             case DCH_MONTH:
    3737              :             case DCH_Month:
    3738              :             case DCH_month:
    3739              :             case DCH_MON:
    3740              :             case DCH_Mon:
    3741              :             case DCH_mon:
    3742              :             case DCH_MM:
    3743              :             case DCH_DAY:
    3744              :             case DCH_Day:
    3745              :             case DCH_day:
    3746              :             case DCH_DY:
    3747              :             case DCH_Dy:
    3748              :             case DCH_dy:
    3749              :             case DCH_DDD:
    3750              :             case DCH_IDDD:
    3751              :             case DCH_DD:
    3752              :             case DCH_D:
    3753              :             case DCH_ID:
    3754              :             case DCH_WW:
    3755              :             case DCH_Q:
    3756              :             case DCH_CC:
    3757              :             case DCH_Y_YYY:
    3758              :             case DCH_YYYY:
    3759              :             case DCH_IYYY:
    3760              :             case DCH_YYY:
    3761              :             case DCH_IYY:
    3762              :             case DCH_YY:
    3763              :             case DCH_IY:
    3764              :             case DCH_Y:
    3765              :             case DCH_I:
    3766              :             case DCH_RM:
    3767              :             case DCH_rm:
    3768              :             case DCH_W:
    3769              :             case DCH_J:
    3770         7587 :                 flags |= DCH_DATED;
    3771         7587 :                 break;
    3772              :         }
    3773              :     }
    3774              : 
    3775         4119 :     return flags;
    3776              : }
    3777              : 
    3778              : /* select a DCHCacheEntry to hold the given format picture */
    3779              : static DCHCacheEntry *
    3780          469 : DCH_cache_getnew(const char *str, bool std)
    3781              : {
    3782              :     DCHCacheEntry *ent;
    3783              : 
    3784              :     /* Ensure we can advance DCHCounter below */
    3785          469 :     DCH_prevent_counter_overflow();
    3786              : 
    3787              :     /*
    3788              :      * If cache is full, remove oldest entry (or recycle first not-valid one)
    3789              :      */
    3790          469 :     if (n_DCHCache >= DCH_CACHE_ENTRIES)
    3791              :     {
    3792          231 :         DCHCacheEntry *old = DCHCache[0];
    3793              : 
    3794              : #ifdef DEBUG_TO_FROM_CHAR
    3795              :         elog(DEBUG_elog_output, "cache is full (%d)", n_DCHCache);
    3796              : #endif
    3797          231 :         if (old->valid)
    3798              :         {
    3799         4599 :             for (int i = 1; i < DCH_CACHE_ENTRIES; i++)
    3800              :             {
    3801         4371 :                 ent = DCHCache[i];
    3802         4371 :                 if (!ent->valid)
    3803              :                 {
    3804            3 :                     old = ent;
    3805            3 :                     break;
    3806              :                 }
    3807         4368 :                 if (ent->age < old->age)
    3808          384 :                     old = ent;
    3809              :             }
    3810              :         }
    3811              : #ifdef DEBUG_TO_FROM_CHAR
    3812              :         elog(DEBUG_elog_output, "OLD: '%s' AGE: %d", old->str, old->age);
    3813              : #endif
    3814          231 :         old->valid = false;
    3815          231 :         strlcpy(old->str, str, DCH_CACHE_SIZE + 1);
    3816          231 :         old->age = (++DCHCounter);
    3817              :         /* caller is expected to fill format, then set valid */
    3818          231 :         return old;
    3819              :     }
    3820              :     else
    3821              :     {
    3822              : #ifdef DEBUG_TO_FROM_CHAR
    3823              :         elog(DEBUG_elog_output, "NEW (%d)", n_DCHCache);
    3824              : #endif
    3825              :         Assert(DCHCache[n_DCHCache] == NULL);
    3826          238 :         DCHCache[n_DCHCache] = ent = (DCHCacheEntry *)
    3827          238 :             MemoryContextAllocZero(TopMemoryContext, sizeof(DCHCacheEntry));
    3828          238 :         ent->valid = false;
    3829          238 :         strlcpy(ent->str, str, DCH_CACHE_SIZE + 1);
    3830          238 :         ent->std = std;
    3831          238 :         ent->age = (++DCHCounter);
    3832              :         /* caller is expected to fill format, then set valid */
    3833          238 :         ++n_DCHCache;
    3834          238 :         return ent;
    3835              :     }
    3836              : }
    3837              : 
    3838              : /* look for an existing DCHCacheEntry matching the given format picture */
    3839              : static DCHCacheEntry *
    3840        25053 : DCH_cache_search(const char *str, bool std)
    3841              : {
    3842              :     /* Ensure we can advance DCHCounter below */
    3843        25053 :     DCH_prevent_counter_overflow();
    3844              : 
    3845       132619 :     for (int i = 0; i < n_DCHCache; i++)
    3846              :     {
    3847       132150 :         DCHCacheEntry *ent = DCHCache[i];
    3848              : 
    3849       132150 :         if (ent->valid && strcmp(ent->str, str) == 0 && ent->std == std)
    3850              :         {
    3851        24584 :             ent->age = (++DCHCounter);
    3852        24584 :             return ent;
    3853              :         }
    3854              :     }
    3855              : 
    3856          469 :     return NULL;
    3857              : }
    3858              : 
    3859              : /* Find or create a DCHCacheEntry for the given format picture */
    3860              : static DCHCacheEntry *
    3861        25053 : DCH_cache_fetch(const char *str, bool std)
    3862              : {
    3863              :     DCHCacheEntry *ent;
    3864              : 
    3865        25053 :     if ((ent = DCH_cache_search(str, std)) == NULL)
    3866              :     {
    3867              :         /*
    3868              :          * Not in the cache, must run parser and save a new format-picture to
    3869              :          * the cache.  Do not mark the cache entry valid until parsing
    3870              :          * succeeds.
    3871              :          */
    3872          469 :         ent = DCH_cache_getnew(str, std);
    3873              : 
    3874          469 :         parse_format(ent->format, str, DCH_keywords, DCH_suff, DCH_index,
    3875              :                      DCH_FLAG | (std ? STD_FLAG : 0), NULL);
    3876              : 
    3877          466 :         ent->valid = true;
    3878              :     }
    3879        25050 :     return ent;
    3880              : }
    3881              : 
    3882              : /*
    3883              :  * Format a date/time or interval into a string according to fmt.
    3884              :  * We parse fmt into a list of FormatNodes.  This is then passed to DCH_to_char
    3885              :  * for formatting.
    3886              :  */
    3887              : static text *
    3888         4793 : datetime_to_char_body(TmToChar *tmtc, const text *fmt, bool is_interval, Oid collid)
    3889              : {
    3890              :     FormatNode *format;
    3891              :     char       *fmt_str,
    3892              :                *result;
    3893              :     bool        incache;
    3894              :     size_t      fmt_len;
    3895              :     text       *res;
    3896              : 
    3897              :     /*
    3898              :      * Convert fmt to C string
    3899              :      */
    3900         4793 :     fmt_str = text_to_cstring(fmt);
    3901         4793 :     fmt_len = strlen(fmt_str);
    3902              : 
    3903              :     /*
    3904              :      * Allocate workspace for result as C string
    3905              :      */
    3906         4793 :     result = palloc((fmt_len * DCH_MAX_ITEM_SIZ) + 1);
    3907         4793 :     *result = '\0';
    3908              : 
    3909         4793 :     if (fmt_len > DCH_CACHE_SIZE)
    3910              :     {
    3911              :         /*
    3912              :          * Allocate new memory if format picture is bigger than static cache
    3913              :          * and do not use cache (call parser always)
    3914              :          */
    3915            0 :         incache = false;
    3916              : 
    3917            0 :         format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
    3918              : 
    3919            0 :         parse_format(format, fmt_str, DCH_keywords,
    3920              :                      DCH_suff, DCH_index, DCH_FLAG, NULL);
    3921              :     }
    3922              :     else
    3923              :     {
    3924              :         /*
    3925              :          * Use cache buffers
    3926              :          */
    3927         4793 :         DCHCacheEntry *ent = DCH_cache_fetch(fmt_str, false);
    3928              : 
    3929         4793 :         incache = true;
    3930         4793 :         format = ent->format;
    3931              :     }
    3932              : 
    3933              :     /* The real work is here */
    3934         4793 :     DCH_to_char(format, is_interval, tmtc, result, collid);
    3935              : 
    3936         4793 :     if (!incache)
    3937            0 :         pfree(format);
    3938              : 
    3939         4793 :     pfree(fmt_str);
    3940              : 
    3941              :     /* convert C-string result to TEXT format */
    3942         4793 :     res = cstring_to_text(result);
    3943              : 
    3944         4793 :     pfree(result);
    3945         4793 :     return res;
    3946              : }
    3947              : 
    3948              : /****************************************************************************
    3949              :  *              Public routines
    3950              :  ***************************************************************************/
    3951              : 
    3952              : /*
    3953              :  * TIMESTAMP to_char()
    3954              :  */
    3955              : Datum
    3956         2402 : timestamp_to_char(PG_FUNCTION_ARGS)
    3957              : {
    3958         2402 :     Timestamp   dt = PG_GETARG_TIMESTAMP(0);
    3959         2402 :     text       *fmt = PG_GETARG_TEXT_PP(1),
    3960              :                *res;
    3961              :     TmToChar    tmtc;
    3962              :     struct pg_tm tt;
    3963              :     struct fmt_tm *tm;
    3964              :     int         thisdate;
    3965              : 
    3966         2402 :     if (VARSIZE_ANY_EXHDR(fmt) <= 0 || TIMESTAMP_NOT_FINITE(dt))
    3967           66 :         PG_RETURN_NULL();
    3968              : 
    3969         2336 :     ZERO_tmtc(&tmtc);
    3970         2336 :     tm = tmtcTm(&tmtc);
    3971              : 
    3972         2336 :     if (timestamp2tm(dt, NULL, &tt, &tmtcFsec(&tmtc), NULL, NULL) != 0)
    3973            0 :         ereport(ERROR,
    3974              :                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    3975              :                  errmsg("timestamp out of range")));
    3976              : 
    3977              :     /* calculate wday and yday, because timestamp2tm doesn't */
    3978         2336 :     thisdate = date2j(tt.tm_year, tt.tm_mon, tt.tm_mday);
    3979         2336 :     tt.tm_wday = (thisdate + 1) % 7;
    3980         2336 :     tt.tm_yday = thisdate - date2j(tt.tm_year, 1, 1) + 1;
    3981              : 
    3982         2336 :     COPY_tm(tm, &tt);
    3983              : 
    3984         2336 :     if (!(res = datetime_to_char_body(&tmtc, fmt, false, PG_GET_COLLATION())))
    3985            0 :         PG_RETURN_NULL();
    3986              : 
    3987         2336 :     PG_RETURN_TEXT_P(res);
    3988              : }
    3989              : 
    3990              : Datum
    3991         2360 : timestamptz_to_char(PG_FUNCTION_ARGS)
    3992              : {
    3993         2360 :     TimestampTz dt = PG_GETARG_TIMESTAMP(0);
    3994         2360 :     text       *fmt = PG_GETARG_TEXT_PP(1),
    3995              :                *res;
    3996              :     TmToChar    tmtc;
    3997              :     int         tz;
    3998              :     struct pg_tm tt;
    3999              :     struct fmt_tm *tm;
    4000              :     int         thisdate;
    4001              : 
    4002         2360 :     if (VARSIZE_ANY_EXHDR(fmt) <= 0 || TIMESTAMP_NOT_FINITE(dt))
    4003           66 :         PG_RETURN_NULL();
    4004              : 
    4005         2294 :     ZERO_tmtc(&tmtc);
    4006         2294 :     tm = tmtcTm(&tmtc);
    4007              : 
    4008         2294 :     if (timestamp2tm(dt, &tz, &tt, &tmtcFsec(&tmtc), &tmtcTzn(&tmtc), NULL) != 0)
    4009            0 :         ereport(ERROR,
    4010              :                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    4011              :                  errmsg("timestamp out of range")));
    4012              : 
    4013              :     /* calculate wday and yday, because timestamp2tm doesn't */
    4014         2294 :     thisdate = date2j(tt.tm_year, tt.tm_mon, tt.tm_mday);
    4015         2294 :     tt.tm_wday = (thisdate + 1) % 7;
    4016         2294 :     tt.tm_yday = thisdate - date2j(tt.tm_year, 1, 1) + 1;
    4017              : 
    4018         2294 :     COPY_tm(tm, &tt);
    4019              : 
    4020         2294 :     if (!(res = datetime_to_char_body(&tmtc, fmt, false, PG_GET_COLLATION())))
    4021            0 :         PG_RETURN_NULL();
    4022              : 
    4023         2294 :     PG_RETURN_TEXT_P(res);
    4024              : }
    4025              : 
    4026              : 
    4027              : /*
    4028              :  * INTERVAL to_char()
    4029              :  */
    4030              : Datum
    4031          169 : interval_to_char(PG_FUNCTION_ARGS)
    4032              : {
    4033          169 :     Interval   *it = PG_GETARG_INTERVAL_P(0);
    4034          169 :     text       *fmt = PG_GETARG_TEXT_PP(1),
    4035              :                *res;
    4036              :     TmToChar    tmtc;
    4037              :     struct fmt_tm *tm;
    4038              :     struct pg_itm tt,
    4039          169 :                *itm = &tt;
    4040              : 
    4041          169 :     if (VARSIZE_ANY_EXHDR(fmt) <= 0 || INTERVAL_NOT_FINITE(it))
    4042            6 :         PG_RETURN_NULL();
    4043              : 
    4044          163 :     ZERO_tmtc(&tmtc);
    4045          163 :     tm = tmtcTm(&tmtc);
    4046              : 
    4047          163 :     interval2itm(*it, itm);
    4048          163 :     tmtc.fsec = itm->tm_usec;
    4049          163 :     tm->tm_sec = itm->tm_sec;
    4050          163 :     tm->tm_min = itm->tm_min;
    4051          163 :     tm->tm_hour = itm->tm_hour;
    4052          163 :     tm->tm_mday = itm->tm_mday;
    4053          163 :     tm->tm_mon = itm->tm_mon;
    4054          163 :     tm->tm_year = itm->tm_year;
    4055              : 
    4056              :     /* wday is meaningless, yday approximates the total span in days */
    4057          163 :     tm->tm_yday = (tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon) * DAYS_PER_MONTH + tm->tm_mday;
    4058              : 
    4059          163 :     if (!(res = datetime_to_char_body(&tmtc, fmt, true, PG_GET_COLLATION())))
    4060            0 :         PG_RETURN_NULL();
    4061              : 
    4062          163 :     PG_RETURN_TEXT_P(res);
    4063              : }
    4064              : 
    4065              : /*
    4066              :  * TO_TIMESTAMP()
    4067              :  *
    4068              :  * Make Timestamp from date_str which is formatted at argument 'fmt'
    4069              :  * ( to_timestamp is reverse to_char() )
    4070              :  */
    4071              : Datum
    4072          462 : to_timestamp(PG_FUNCTION_ARGS)
    4073              : {
    4074          462 :     text       *date_txt = PG_GETARG_TEXT_PP(0);
    4075          462 :     text       *fmt = PG_GETARG_TEXT_PP(1);
    4076          462 :     Oid         collid = PG_GET_COLLATION();
    4077              :     Timestamp   result;
    4078              :     int         tz;
    4079              :     struct pg_tm tm;
    4080              :     struct fmt_tz ftz;
    4081              :     fsec_t      fsec;
    4082              :     int         fprec;
    4083              : 
    4084          462 :     do_to_timestamp(date_txt, fmt, collid, false,
    4085              :                     &tm, &fsec, &ftz, &fprec, NULL, NULL);
    4086              : 
    4087              :     /* Use the specified time zone, if any. */
    4088          378 :     if (ftz.has_tz)
    4089           48 :         tz = ftz.gmtoffset;
    4090              :     else
    4091          330 :         tz = DetermineTimeZoneOffset(&tm, session_timezone);
    4092              : 
    4093          378 :     if (tm2timestamp(&tm, fsec, &tz, &result) != 0)
    4094            0 :         ereport(ERROR,
    4095              :                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    4096              :                  errmsg("timestamp out of range")));
    4097              : 
    4098              :     /* Use the specified fractional precision, if any. */
    4099          378 :     if (fprec)
    4100          126 :         AdjustTimestampForTypmod(&result, fprec, NULL);
    4101              : 
    4102          378 :     PG_RETURN_TIMESTAMP(result);
    4103              : }
    4104              : 
    4105              : /*
    4106              :  * TO_DATE
    4107              :  *  Make Date from date_str which is formatted at argument 'fmt'
    4108              :  */
    4109              : Datum
    4110          103 : to_date(PG_FUNCTION_ARGS)
    4111              : {
    4112          103 :     text       *date_txt = PG_GETARG_TEXT_PP(0);
    4113          103 :     text       *fmt = PG_GETARG_TEXT_PP(1);
    4114          103 :     Oid         collid = PG_GET_COLLATION();
    4115              :     DateADT     result;
    4116              :     struct pg_tm tm;
    4117              :     struct fmt_tz ftz;
    4118              :     fsec_t      fsec;
    4119              : 
    4120          103 :     do_to_timestamp(date_txt, fmt, collid, false,
    4121              :                     &tm, &fsec, &ftz, NULL, NULL, NULL);
    4122              : 
    4123              :     /* Prevent overflow in Julian-day routines */
    4124           72 :     if (!IS_VALID_JULIAN(tm.tm_year, tm.tm_mon, tm.tm_mday))
    4125            0 :         ereport(ERROR,
    4126              :                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    4127              :                  errmsg("date out of range: \"%s\"", text_to_cstring(date_txt))));
    4128              : 
    4129           72 :     result = date2j(tm.tm_year, tm.tm_mon, tm.tm_mday) - POSTGRES_EPOCH_JDATE;
    4130              : 
    4131              :     /* Now check for just-out-of-range dates */
    4132           72 :     if (!IS_VALID_DATE(result))
    4133            0 :         ereport(ERROR,
    4134              :                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    4135              :                  errmsg("date out of range: \"%s\"", text_to_cstring(date_txt))));
    4136              : 
    4137           72 :     PG_RETURN_DATEADT(result);
    4138              : }
    4139              : 
    4140              : /*
    4141              :  * Convert the 'date_txt' input to a datetime type using argument 'fmt'
    4142              :  * as a format string.  The collation 'collid' may be used for case-folding
    4143              :  * rules in some cases.  'strict' specifies standard parsing mode.
    4144              :  *
    4145              :  * The actual data type (returned in 'typid', 'typmod') is determined by
    4146              :  * the presence of date/time/zone components in the format string.
    4147              :  *
    4148              :  * When a timezone component is present, the corresponding offset is
    4149              :  * returned in '*tz'.
    4150              :  *
    4151              :  * If escontext points to an ErrorSaveContext, data errors will be reported
    4152              :  * by filling that struct; the caller must test SOFT_ERROR_OCCURRED() to see
    4153              :  * whether an error occurred.  Otherwise, errors are thrown.
    4154              :  */
    4155              : Datum
    4156        19662 : parse_datetime(text *date_txt, text *fmt, Oid collid, bool strict,
    4157              :                Oid *typid, int32 *typmod, int *tz,
    4158              :                Node *escontext)
    4159              : {
    4160              :     struct pg_tm tm;
    4161              :     struct fmt_tz ftz;
    4162              :     fsec_t      fsec;
    4163              :     int         fprec;
    4164              :     uint32      flags;
    4165              : 
    4166        19662 :     if (!do_to_timestamp(date_txt, fmt, collid, strict,
    4167              :                          &tm, &fsec, &ftz, &fprec, &flags, escontext))
    4168        15546 :         return (Datum) 0;
    4169              : 
    4170         4086 :     *typmod = fprec ? fprec : -1;   /* fractional part precision */
    4171              : 
    4172         4086 :     if (flags & DCH_DATED)
    4173              :     {
    4174         2520 :         if (flags & DCH_TIMED)
    4175              :         {
    4176         1878 :             if (flags & DCH_ZONED)
    4177              :             {
    4178              :                 TimestampTz result;
    4179              : 
    4180         1077 :                 if (ftz.has_tz)
    4181              :                 {
    4182         1077 :                     *tz = ftz.gmtoffset;
    4183              :                 }
    4184              :                 else
    4185              :                 {
    4186              :                     /*
    4187              :                      * Time zone is present in format string, but not in input
    4188              :                      * string.  Assuming do_to_timestamp() triggers no error
    4189              :                      * this should be possible only in non-strict case.
    4190              :                      */
    4191              :                     Assert(!strict);
    4192              : 
    4193            0 :                     ereturn(escontext, (Datum) 0,
    4194              :                             (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    4195              :                              errmsg("missing time zone in input string for type timestamptz")));
    4196              :                 }
    4197              : 
    4198         1077 :                 if (tm2timestamp(&tm, fsec, tz, &result) != 0)
    4199            0 :                     ereturn(escontext, (Datum) 0,
    4200              :                             (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    4201              :                              errmsg("timestamptz out of range")));
    4202              : 
    4203         1077 :                 AdjustTimestampForTypmod(&result, *typmod, escontext);
    4204              : 
    4205         1077 :                 *typid = TIMESTAMPTZOID;
    4206         1077 :                 return TimestampTzGetDatum(result);
    4207              :             }
    4208              :             else
    4209              :             {
    4210              :                 Timestamp   result;
    4211              : 
    4212          801 :                 if (tm2timestamp(&tm, fsec, NULL, &result) != 0)
    4213            0 :                     ereturn(escontext, (Datum) 0,
    4214              :                             (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    4215              :                              errmsg("timestamp out of range")));
    4216              : 
    4217          801 :                 AdjustTimestampForTypmod(&result, *typmod, escontext);
    4218              : 
    4219          801 :                 *typid = TIMESTAMPOID;
    4220          801 :                 return TimestampGetDatum(result);
    4221              :             }
    4222              :         }
    4223              :         else
    4224              :         {
    4225          642 :             if (flags & DCH_ZONED)
    4226              :             {
    4227            0 :                 ereturn(escontext, (Datum) 0,
    4228              :                         (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    4229              :                          errmsg("datetime format is zoned but not timed")));
    4230              :             }
    4231              :             else
    4232              :             {
    4233              :                 DateADT     result;
    4234              : 
    4235              :                 /* Prevent overflow in Julian-day routines */
    4236          642 :                 if (!IS_VALID_JULIAN(tm.tm_year, tm.tm_mon, tm.tm_mday))
    4237            0 :                     ereturn(escontext, (Datum) 0,
    4238              :                             (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    4239              :                              errmsg("date out of range: \"%s\"", text_to_cstring(date_txt))));
    4240              : 
    4241          642 :                 result = date2j(tm.tm_year, tm.tm_mon, tm.tm_mday) -
    4242              :                     POSTGRES_EPOCH_JDATE;
    4243              : 
    4244              :                 /* Now check for just-out-of-range dates */
    4245          642 :                 if (!IS_VALID_DATE(result))
    4246            0 :                     ereturn(escontext, (Datum) 0,
    4247              :                             (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    4248              :                              errmsg("date out of range: \"%s\"", text_to_cstring(date_txt))));
    4249              : 
    4250          642 :                 *typid = DATEOID;
    4251          642 :                 return DateADTGetDatum(result);
    4252              :             }
    4253              :         }
    4254              :     }
    4255         1566 :     else if (flags & DCH_TIMED)
    4256              :     {
    4257         1566 :         if (flags & DCH_ZONED)
    4258              :         {
    4259          885 :             TimeTzADT  *result = palloc_object(TimeTzADT);
    4260              : 
    4261          885 :             if (ftz.has_tz)
    4262              :             {
    4263          885 :                 *tz = ftz.gmtoffset;
    4264              :             }
    4265              :             else
    4266              :             {
    4267              :                 /*
    4268              :                  * Time zone is present in format string, but not in input
    4269              :                  * string.  Assuming do_to_timestamp() triggers no error this
    4270              :                  * should be possible only in non-strict case.
    4271              :                  */
    4272              :                 Assert(!strict);
    4273              : 
    4274            0 :                 ereturn(escontext, (Datum) 0,
    4275              :                         (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    4276              :                          errmsg("missing time zone in input string for type timetz")));
    4277              :             }
    4278              : 
    4279          885 :             if (tm2timetz(&tm, fsec, *tz, result) != 0)
    4280            0 :                 ereturn(escontext, (Datum) 0,
    4281              :                         (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    4282              :                          errmsg("timetz out of range")));
    4283              : 
    4284          885 :             AdjustTimeForTypmod(&result->time, *typmod);
    4285              : 
    4286          885 :             *typid = TIMETZOID;
    4287          885 :             return TimeTzADTPGetDatum(result);
    4288              :         }
    4289              :         else
    4290              :         {
    4291              :             TimeADT     result;
    4292              : 
    4293          681 :             if (tm2time(&tm, fsec, &result) != 0)
    4294            0 :                 ereturn(escontext, (Datum) 0,
    4295              :                         (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    4296              :                          errmsg("time out of range")));
    4297              : 
    4298          681 :             AdjustTimeForTypmod(&result, *typmod);
    4299              : 
    4300          681 :             *typid = TIMEOID;
    4301          681 :             return TimeADTGetDatum(result);
    4302              :         }
    4303              :     }
    4304              :     else
    4305              :     {
    4306            0 :         ereturn(escontext, (Datum) 0,
    4307              :                 (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    4308              :                  errmsg("datetime format is not dated and not timed")));
    4309              :     }
    4310              : }
    4311              : 
    4312              : /*
    4313              :  * Parses the datetime format string in 'fmt_str' and returns true if it
    4314              :  * contains a timezone specifier, false if not.
    4315              :  */
    4316              : bool
    4317           33 : datetime_format_has_tz(const char *fmt_str)
    4318              : {
    4319              :     bool        incache;
    4320           33 :     size_t      fmt_len = strlen(fmt_str);
    4321              :     int         result;
    4322              :     FormatNode *format;
    4323              : 
    4324           33 :     if (fmt_len > DCH_CACHE_SIZE)
    4325              :     {
    4326              :         /*
    4327              :          * Allocate new memory if format picture is bigger than static cache
    4328              :          * and do not use cache (call parser always)
    4329              :          */
    4330            0 :         incache = false;
    4331              : 
    4332            0 :         format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
    4333              : 
    4334            0 :         parse_format(format, fmt_str, DCH_keywords,
    4335              :                      DCH_suff, DCH_index, DCH_FLAG, NULL);
    4336              :     }
    4337              :     else
    4338              :     {
    4339              :         /*
    4340              :          * Use cache buffers
    4341              :          */
    4342           33 :         DCHCacheEntry *ent = DCH_cache_fetch(fmt_str, false);
    4343              : 
    4344           33 :         incache = true;
    4345           33 :         format = ent->format;
    4346              :     }
    4347              : 
    4348           33 :     result = DCH_datetime_type(format);
    4349              : 
    4350           33 :     if (!incache)
    4351            0 :         pfree(format);
    4352              : 
    4353           33 :     return result & DCH_ZONED;
    4354              : }
    4355              : 
    4356              : /*
    4357              :  * do_to_timestamp: shared code for to_timestamp and to_date
    4358              :  *
    4359              :  * Parse the 'date_txt' according to 'fmt', return results as a struct pg_tm,
    4360              :  * fractional seconds, struct fmt_tz, and fractional precision.
    4361              :  *
    4362              :  * 'collid' identifies the collation to use, if needed.
    4363              :  * 'std' specifies standard parsing mode.
    4364              :  *
    4365              :  * Bit mask of date/time/zone components found in 'fmt' is returned in 'flags',
    4366              :  * if that is not NULL.
    4367              :  *
    4368              :  * Returns true on success, false on failure (if escontext points to an
    4369              :  * ErrorSaveContext; otherwise errors are thrown).  Note that currently,
    4370              :  * soft-error behavior is provided for bad data but not bad format.
    4371              :  *
    4372              :  * We parse 'fmt' into a list of FormatNodes, which is then passed to
    4373              :  * DCH_from_char to populate a TmFromChar with the parsed contents of
    4374              :  * 'date_txt'.
    4375              :  *
    4376              :  * The TmFromChar is then analysed and converted into the final results in
    4377              :  * struct 'tm', 'fsec', struct 'tz', and 'fprec'.
    4378              :  */
    4379              : static bool
    4380        20227 : do_to_timestamp(const text *date_txt, const text *fmt, Oid collid, bool std,
    4381              :                 struct pg_tm *tm, fsec_t *fsec, struct fmt_tz *tz,
    4382              :                 int *fprec, uint32 *flags, Node *escontext)
    4383              : {
    4384        20227 :     FormatNode *format = NULL;
    4385        20227 :     TmFromChar  tmfc = {0};
    4386              :     int         fmt_len;
    4387              :     char       *date_str;
    4388              :     int         fmask;
    4389        20227 :     bool        incache = false;
    4390              : 
    4391              :     Assert(tm != NULL);
    4392              :     Assert(fsec != NULL);
    4393              : 
    4394        20227 :     date_str = text_to_cstring(date_txt);
    4395              : 
    4396        20227 :     ZERO_tm(tm);
    4397        20227 :     *fsec = 0;
    4398        20227 :     tz->has_tz = false;
    4399        20227 :     if (fprec)
    4400        20124 :         *fprec = 0;
    4401        20227 :     if (flags)
    4402        19662 :         *flags = 0;
    4403        20227 :     fmask = 0;                  /* bit mask for ValidateDate() */
    4404              : 
    4405        20227 :     fmt_len = VARSIZE_ANY_EXHDR(fmt);
    4406              : 
    4407        20227 :     if (fmt_len)
    4408              :     {
    4409              :         char       *fmt_str;
    4410              : 
    4411        20227 :         fmt_str = text_to_cstring(fmt);
    4412              : 
    4413        20227 :         if (fmt_len > DCH_CACHE_SIZE)
    4414              :         {
    4415              :             /*
    4416              :              * Allocate new memory if format picture is bigger than static
    4417              :              * cache and do not use cache (call parser always)
    4418              :              */
    4419            0 :             format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
    4420              : 
    4421            0 :             parse_format(format, fmt_str, DCH_keywords, DCH_suff, DCH_index,
    4422              :                          DCH_FLAG | (std ? STD_FLAG : 0), NULL);
    4423              :         }
    4424              :         else
    4425              :         {
    4426              :             /*
    4427              :              * Use cache buffers
    4428              :              */
    4429        20227 :             DCHCacheEntry *ent = DCH_cache_fetch(fmt_str, std);
    4430              : 
    4431        20224 :             incache = true;
    4432        20224 :             format = ent->format;
    4433              :         }
    4434              : 
    4435              : #ifdef DEBUG_TO_FROM_CHAR
    4436              :         /* dump_node(format, fmt_len); */
    4437              :         /* dump_index(DCH_keywords, DCH_index); */
    4438              : #endif
    4439              : 
    4440        20224 :         DCH_from_char(format, date_str, &tmfc, collid, std, escontext);
    4441        20148 :         pfree(fmt_str);
    4442        20148 :         if (SOFT_ERROR_OCCURRED(escontext))
    4443        15546 :             goto fail;
    4444              : 
    4445         4602 :         if (flags)
    4446         4086 :             *flags = DCH_datetime_type(format);
    4447              : 
    4448         4602 :         if (!incache)
    4449              :         {
    4450            0 :             pfree(format);
    4451            0 :             format = NULL;
    4452              :         }
    4453              :     }
    4454              : 
    4455              :     DEBUG_TMFC(&tmfc);
    4456              : 
    4457              :     /*
    4458              :      * Convert to_date/to_timestamp input fields to standard 'tm'
    4459              :      */
    4460         4602 :     if (tmfc.ssss)
    4461              :     {
    4462           12 :         int         x = tmfc.ssss;
    4463              : 
    4464           12 :         tm->tm_hour = x / SECS_PER_HOUR;
    4465           12 :         x %= SECS_PER_HOUR;
    4466           12 :         tm->tm_min = x / SECS_PER_MINUTE;
    4467           12 :         x %= SECS_PER_MINUTE;
    4468           12 :         tm->tm_sec = x;
    4469              :     }
    4470              : 
    4471         4602 :     if (tmfc.ss)
    4472          678 :         tm->tm_sec = tmfc.ss;
    4473         4602 :     if (tmfc.mi)
    4474         3636 :         tm->tm_min = tmfc.mi;
    4475         4602 :     if (tmfc.hh)
    4476         3669 :         tm->tm_hour = tmfc.hh;
    4477              : 
    4478         4602 :     if (tmfc.clock_12_hour)
    4479              :     {
    4480           60 :         if (tm->tm_hour < 1 || tm->tm_hour > HOURS_PER_DAY / 2)
    4481              :         {
    4482            3 :             errsave(escontext,
    4483              :                     (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    4484              :                      errmsg("hour \"%d\" is invalid for the 12-hour clock", tm->tm_hour),
    4485              :                      errhint("Use the 24-hour clock, or give an hour between 1 and 12.")));
    4486            0 :             goto fail;
    4487              :         }
    4488              : 
    4489           57 :         if (tmfc.pm && tm->tm_hour < HOURS_PER_DAY / 2)
    4490            6 :             tm->tm_hour += HOURS_PER_DAY / 2;
    4491           51 :         else if (!tmfc.pm && tm->tm_hour == HOURS_PER_DAY / 2)
    4492            0 :             tm->tm_hour = 0;
    4493              :     }
    4494              : 
    4495         4599 :     if (tmfc.year)
    4496              :     {
    4497              :         /*
    4498              :          * If CC and YY (or Y) are provided, use YY as 2 low-order digits for
    4499              :          * the year in the given century.  Keep in mind that the 21st century
    4500              :          * AD runs from 2001-2100, not 2000-2099; 6th century BC runs from
    4501              :          * 600BC to 501BC.
    4502              :          */
    4503         3015 :         if (tmfc.cc && tmfc.yysz <= 2)
    4504              :         {
    4505            9 :             if (tmfc.bc)
    4506            0 :                 tmfc.cc = -tmfc.cc;
    4507            9 :             tm->tm_year = tmfc.year % 100;
    4508            9 :             if (tm->tm_year)
    4509              :             {
    4510              :                 int         tmp;
    4511              : 
    4512            9 :                 if (tmfc.cc >= 0)
    4513              :                 {
    4514              :                     /* tm->tm_year += (tmfc.cc - 1) * 100; */
    4515            6 :                     tmp = tmfc.cc - 1;
    4516            9 :                     if (pg_mul_s32_overflow(tmp, 100, &tmp) ||
    4517            3 :                         pg_add_s32_overflow(tm->tm_year, tmp, &tm->tm_year))
    4518              :                     {
    4519            3 :                         DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL,
    4520            3 :                                            text_to_cstring(date_txt), "timestamp",
    4521              :                                            escontext);
    4522            0 :                         goto fail;
    4523              :                     }
    4524              :                 }
    4525              :                 else
    4526              :                 {
    4527              :                     /* tm->tm_year = (tmfc.cc + 1) * 100 - tm->tm_year + 1; */
    4528            3 :                     tmp = tmfc.cc + 1;
    4529            3 :                     if (pg_mul_s32_overflow(tmp, 100, &tmp) ||
    4530            0 :                         pg_sub_s32_overflow(tmp, tm->tm_year, &tmp) ||
    4531            0 :                         pg_add_s32_overflow(tmp, 1, &tm->tm_year))
    4532              :                     {
    4533            3 :                         DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL,
    4534            3 :                                            text_to_cstring(date_txt), "timestamp",
    4535              :                                            escontext);
    4536            0 :                         goto fail;
    4537              :                     }
    4538              :                 }
    4539              :             }
    4540              :             else
    4541              :             {
    4542              :                 /* find century year for dates ending in "00" */
    4543            0 :                 tm->tm_year = tmfc.cc * 100 + ((tmfc.cc >= 0) ? 0 : 1);
    4544              :             }
    4545              :         }
    4546              :         else
    4547              :         {
    4548              :             /* If a 4-digit year is provided, we use that and ignore CC. */
    4549         3006 :             tm->tm_year = tmfc.year;
    4550         3006 :             if (tmfc.bc)
    4551           18 :                 tm->tm_year = -tm->tm_year;
    4552              :             /* correct for our representation of BC years */
    4553         3006 :             if (tm->tm_year < 0)
    4554           18 :                 tm->tm_year++;
    4555              :         }
    4556         3009 :         fmask |= DTK_M(YEAR);
    4557              :     }
    4558         1584 :     else if (tmfc.cc)
    4559              :     {
    4560              :         /* use first year of century */
    4561            6 :         if (tmfc.bc)
    4562            0 :             tmfc.cc = -tmfc.cc;
    4563            6 :         if (tmfc.cc >= 0)
    4564              :         {
    4565              :             /* +1 because 21st century started in 2001 */
    4566              :             /* tm->tm_year = (tmfc.cc - 1) * 100 + 1; */
    4567            3 :             if (pg_mul_s32_overflow(tmfc.cc - 1, 100, &tm->tm_year) ||
    4568            0 :                 pg_add_s32_overflow(tm->tm_year, 1, &tm->tm_year))
    4569              :             {
    4570            3 :                 DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL,
    4571            3 :                                    text_to_cstring(date_txt), "timestamp",
    4572              :                                    escontext);
    4573            0 :                 goto fail;
    4574              :             }
    4575              :         }
    4576              :         else
    4577              :         {
    4578              :             /* +1 because year == 599 is 600 BC */
    4579              :             /* tm->tm_year = tmfc.cc * 100 + 1; */
    4580            3 :             if (pg_mul_s32_overflow(tmfc.cc, 100, &tm->tm_year) ||
    4581            0 :                 pg_add_s32_overflow(tm->tm_year, 1, &tm->tm_year))
    4582              :             {
    4583            3 :                 DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL,
    4584            3 :                                    text_to_cstring(date_txt), "timestamp",
    4585              :                                    escontext);
    4586            0 :                 goto fail;
    4587              :             }
    4588              :         }
    4589            0 :         fmask |= DTK_M(YEAR);
    4590              :     }
    4591              : 
    4592         4587 :     if (tmfc.j)
    4593              :     {
    4594            3 :         j2date(tmfc.j, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
    4595            3 :         fmask |= DTK_DATE_M;
    4596              :     }
    4597              : 
    4598         4587 :     if (tmfc.ww)
    4599              :     {
    4600           18 :         if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK)
    4601              :         {
    4602              :             /*
    4603              :              * If tmfc.d is not set, then the date is left at the beginning of
    4604              :              * the ISO week (Monday).
    4605              :              */
    4606           12 :             if (tmfc.d)
    4607           12 :                 isoweekdate2date(tmfc.ww, tmfc.d, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
    4608              :             else
    4609            0 :                 isoweek2date(tmfc.ww, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
    4610           12 :             fmask |= DTK_DATE_M;
    4611              :         }
    4612              :         else
    4613              :         {
    4614              :             /* tmfc.ddd = (tmfc.ww - 1) * 7 + 1; */
    4615           12 :             if (pg_sub_s32_overflow(tmfc.ww, 1, &tmfc.ddd) ||
    4616            9 :                 pg_mul_s32_overflow(tmfc.ddd, 7, &tmfc.ddd) ||
    4617            3 :                 pg_add_s32_overflow(tmfc.ddd, 1, &tmfc.ddd))
    4618              :             {
    4619            3 :                 DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL,
    4620              :                                    date_str, "timestamp", escontext);
    4621            0 :                 goto fail;
    4622              :             }
    4623              :         }
    4624              :     }
    4625              : 
    4626         4584 :     if (tmfc.w)
    4627              :     {
    4628              :         /* tmfc.dd = (tmfc.w - 1) * 7 + 1; */
    4629           12 :         if (pg_sub_s32_overflow(tmfc.w, 1, &tmfc.dd) ||
    4630            9 :             pg_mul_s32_overflow(tmfc.dd, 7, &tmfc.dd) ||
    4631            3 :             pg_add_s32_overflow(tmfc.dd, 1, &tmfc.dd))
    4632              :         {
    4633            3 :             DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL,
    4634              :                                date_str, "timestamp", escontext);
    4635            0 :             goto fail;
    4636              :         }
    4637              :     }
    4638         4581 :     if (tmfc.dd)
    4639              :     {
    4640         2940 :         tm->tm_mday = tmfc.dd;
    4641         2940 :         fmask |= DTK_M(DAY);
    4642              :     }
    4643         4581 :     if (tmfc.mm)
    4644              :     {
    4645         2958 :         tm->tm_mon = tmfc.mm;
    4646         2958 :         fmask |= DTK_M(MONTH);
    4647              :     }
    4648              : 
    4649         4581 :     if (tmfc.ddd && (tm->tm_mon <= 1 || tm->tm_mday <= 1))
    4650              :     {
    4651              :         /*
    4652              :          * The month and day field have not been set, so we use the
    4653              :          * day-of-year field to populate them.  Depending on the date mode,
    4654              :          * this field may be interpreted as a Gregorian day-of-year, or an ISO
    4655              :          * week date day-of-year.
    4656              :          */
    4657              : 
    4658           24 :         if (!tm->tm_year && !tmfc.bc)
    4659              :         {
    4660            0 :             errsave(escontext,
    4661              :                     (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
    4662              :                      errmsg("cannot calculate day of year without year information")));
    4663            0 :             goto fail;
    4664              :         }
    4665              : 
    4666           24 :         if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK)
    4667              :         {
    4668              :             int         j0;     /* zeroth day of the ISO year, in Julian */
    4669              : 
    4670            3 :             j0 = isoweek2j(tm->tm_year, 1) - 1;
    4671              : 
    4672            3 :             j2date(j0 + tmfc.ddd, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
    4673            3 :             fmask |= DTK_DATE_M;
    4674              :         }
    4675              :         else
    4676              :         {
    4677              :             const int  *y;
    4678              :             int         i;
    4679              : 
    4680              :             static const int ysum[2][13] = {
    4681              :                 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
    4682              :             {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};
    4683              : 
    4684           21 :             y = ysum[isleap(tm->tm_year)];
    4685              : 
    4686          246 :             for (i = 1; i <= MONTHS_PER_YEAR; i++)
    4687              :             {
    4688          240 :                 if (tmfc.ddd <= y[i])
    4689           15 :                     break;
    4690              :             }
    4691           21 :             if (tm->tm_mon <= 1)
    4692           21 :                 tm->tm_mon = i;
    4693              : 
    4694           21 :             if (tm->tm_mday <= 1)
    4695           21 :                 tm->tm_mday = tmfc.ddd - y[i - 1];
    4696              : 
    4697           21 :             fmask |= DTK_M(MONTH) | DTK_M(DAY);
    4698              :         }
    4699              :     }
    4700              : 
    4701         4581 :     if (tmfc.ms)
    4702              :     {
    4703            6 :         int         tmp = 0;
    4704              : 
    4705              :         /* *fsec += tmfc.ms * 1000; */
    4706            9 :         if (pg_mul_s32_overflow(tmfc.ms, 1000, &tmp) ||
    4707            3 :             pg_add_s32_overflow(*fsec, tmp, fsec))
    4708              :         {
    4709            3 :             DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL,
    4710              :                                date_str, "timestamp", escontext);
    4711            0 :             goto fail;
    4712              :         }
    4713              :     }
    4714         4578 :     if (tmfc.us)
    4715          507 :         *fsec += tmfc.us;
    4716         4578 :     if (fprec)
    4717         4491 :         *fprec = tmfc.ff;       /* fractional precision, if specified */
    4718              : 
    4719              :     /* Range-check date fields according to bit mask computed above */
    4720         4578 :     if (fmask != 0)
    4721              :     {
    4722              :         /* We already dealt with AD/BC, so pass isjulian = true */
    4723         3012 :         int         dterr = ValidateDate(fmask, true, false, false, tm);
    4724              : 
    4725         3012 :         if (dterr != 0)
    4726              :         {
    4727              :             /*
    4728              :              * Force the error to be DTERR_FIELD_OVERFLOW even if ValidateDate
    4729              :              * said DTERR_MD_FIELD_OVERFLOW, because we don't want to print an
    4730              :              * irrelevant hint about datestyle.
    4731              :              */
    4732           24 :             DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL,
    4733              :                                date_str, "timestamp", escontext);
    4734            0 :             goto fail;
    4735              :         }
    4736              :     }
    4737              : 
    4738              :     /* Range-check time fields too */
    4739         4554 :     if (tm->tm_hour < 0 || tm->tm_hour >= HOURS_PER_DAY ||
    4740         4545 :         tm->tm_min < 0 || tm->tm_min >= MINS_PER_HOUR ||
    4741         4542 :         tm->tm_sec < 0 || tm->tm_sec >= SECS_PER_MINUTE ||
    4742         4539 :         *fsec < INT64CONST(0) || *fsec >= USECS_PER_SEC)
    4743              :     {
    4744           18 :         DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL,
    4745              :                            date_str, "timestamp", escontext);
    4746            0 :         goto fail;
    4747              :     }
    4748              : 
    4749              :     /*
    4750              :      * If timezone info was present, reduce it to a GMT offset.  (We cannot do
    4751              :      * this until we've filled all of the tm struct, since the zone's offset
    4752              :      * might be time-varying.)
    4753              :      */
    4754         4536 :     if (tmfc.tzsign)
    4755              :     {
    4756              :         /* TZH and/or TZM fields */
    4757         1992 :         if (tmfc.tzh < 0 || tmfc.tzh > MAX_TZDISP_HOUR ||
    4758         1992 :             tmfc.tzm < 0 || tmfc.tzm >= MINS_PER_HOUR)
    4759              :         {
    4760            0 :             DateTimeParseError(DTERR_TZDISP_OVERFLOW, NULL,
    4761              :                                date_str, "timestamp", escontext);
    4762            0 :             goto fail;
    4763              :         }
    4764              : 
    4765         1992 :         tz->has_tz = true;
    4766         1992 :         tz->gmtoffset = (tmfc.tzh * MINS_PER_HOUR + tmfc.tzm) * SECS_PER_MINUTE;
    4767              :         /* note we are flipping the sign convention here */
    4768         1992 :         if (tmfc.tzsign > 0)
    4769         1818 :             tz->gmtoffset = -tz->gmtoffset;
    4770              :     }
    4771         2544 :     else if (tmfc.has_tz)
    4772              :     {
    4773              :         /* TZ field */
    4774           18 :         tz->has_tz = true;
    4775           18 :         if (tmfc.tzp == NULL)
    4776              :         {
    4777              :             /* fixed-offset abbreviation; flip the sign convention */
    4778           15 :             tz->gmtoffset = -tmfc.gmtoffset;
    4779              :         }
    4780              :         else
    4781              :         {
    4782              :             /* dynamic-offset abbreviation, resolve using specified time */
    4783            3 :             tz->gmtoffset = DetermineTimeZoneAbbrevOffset(tm, tmfc.abbrev,
    4784              :                                                           tmfc.tzp);
    4785              :         }
    4786              :     }
    4787              : 
    4788              :     DEBUG_TM(tm);
    4789              : 
    4790         4536 :     if (format && !incache)
    4791            0 :         pfree(format);
    4792         4536 :     pfree(date_str);
    4793              : 
    4794         4536 :     return true;
    4795              : 
    4796        15546 : fail:
    4797        15546 :     if (format && !incache)
    4798            0 :         pfree(format);
    4799        15546 :     pfree(date_str);
    4800              : 
    4801        15546 :     return false;
    4802              : }
    4803              : 
    4804              : 
    4805              : /**********************************************************************
    4806              :  *  the NUMBER version part
    4807              :  *********************************************************************/
    4808              : 
    4809              : 
    4810              : /*
    4811              :  * Fill str with character c max times, and add terminating \0.  (So max+1
    4812              :  * bytes are written altogether!)
    4813              :  */
    4814              : static void
    4815          114 : fill_str(char *str, int c, int max)
    4816              : {
    4817          114 :     memset(str, c, max);
    4818          114 :     str[max] = '\0';
    4819          114 : }
    4820              : 
    4821              : /* This works the same as DCH_prevent_counter_overflow */
    4822              : static inline void
    4823       528301 : NUM_prevent_counter_overflow(void)
    4824              : {
    4825       528301 :     if (NUMCounter >= (INT_MAX - 1))
    4826              :     {
    4827            0 :         for (int i = 0; i < n_NUMCache; i++)
    4828            0 :             NUMCache[i]->age >>= 1;
    4829            0 :         NUMCounter >>= 1;
    4830              :     }
    4831       528301 : }
    4832              : 
    4833              : /* select a NUMCacheEntry to hold the given format picture */
    4834              : static NUMCacheEntry *
    4835          310 : NUM_cache_getnew(const char *str)
    4836              : {
    4837              :     NUMCacheEntry *ent;
    4838              : 
    4839              :     /* Ensure we can advance NUMCounter below */
    4840          310 :     NUM_prevent_counter_overflow();
    4841              : 
    4842              :     /*
    4843              :      * If cache is full, remove oldest entry (or recycle first not-valid one)
    4844              :      */
    4845          310 :     if (n_NUMCache >= NUM_CACHE_ENTRIES)
    4846              :     {
    4847          147 :         NUMCacheEntry *old = NUMCache[0];
    4848              : 
    4849              : #ifdef DEBUG_TO_FROM_CHAR
    4850              :         elog(DEBUG_elog_output, "Cache is full (%d)", n_NUMCache);
    4851              : #endif
    4852          147 :         if (old->valid)
    4853              :         {
    4854         2889 :             for (int i = 1; i < NUM_CACHE_ENTRIES; i++)
    4855              :             {
    4856         2745 :                 ent = NUMCache[i];
    4857         2745 :                 if (!ent->valid)
    4858              :                 {
    4859            3 :                     old = ent;
    4860            3 :                     break;
    4861              :                 }
    4862         2742 :                 if (ent->age < old->age)
    4863          147 :                     old = ent;
    4864              :             }
    4865              :         }
    4866              : #ifdef DEBUG_TO_FROM_CHAR
    4867              :         elog(DEBUG_elog_output, "OLD: \"%s\" AGE: %d", old->str, old->age);
    4868              : #endif
    4869          147 :         old->valid = false;
    4870          147 :         strlcpy(old->str, str, NUM_CACHE_SIZE + 1);
    4871          147 :         old->age = (++NUMCounter);
    4872              :         /* caller is expected to fill format and Num, then set valid */
    4873          147 :         return old;
    4874              :     }
    4875              :     else
    4876              :     {
    4877              : #ifdef DEBUG_TO_FROM_CHAR
    4878              :         elog(DEBUG_elog_output, "NEW (%d)", n_NUMCache);
    4879              : #endif
    4880              :         Assert(NUMCache[n_NUMCache] == NULL);
    4881          163 :         NUMCache[n_NUMCache] = ent = (NUMCacheEntry *)
    4882          163 :             MemoryContextAllocZero(TopMemoryContext, sizeof(NUMCacheEntry));
    4883          163 :         ent->valid = false;
    4884          163 :         strlcpy(ent->str, str, NUM_CACHE_SIZE + 1);
    4885          163 :         ent->age = (++NUMCounter);
    4886              :         /* caller is expected to fill format and Num, then set valid */
    4887          163 :         ++n_NUMCache;
    4888          163 :         return ent;
    4889              :     }
    4890              : }
    4891              : 
    4892              : /* look for an existing NUMCacheEntry matching the given format picture */
    4893              : static NUMCacheEntry *
    4894       527991 : NUM_cache_search(const char *str)
    4895              : {
    4896              :     /* Ensure we can advance NUMCounter below */
    4897       527991 :     NUM_prevent_counter_overflow();
    4898              : 
    4899       662721 :     for (int i = 0; i < n_NUMCache; i++)
    4900              :     {
    4901       662411 :         NUMCacheEntry *ent = NUMCache[i];
    4902              : 
    4903       662411 :         if (ent->valid && strcmp(ent->str, str) == 0)
    4904              :         {
    4905       527681 :             ent->age = (++NUMCounter);
    4906       527681 :             return ent;
    4907              :         }
    4908              :     }
    4909              : 
    4910          310 :     return NULL;
    4911              : }
    4912              : 
    4913              : /* Find or create a NUMCacheEntry for the given format picture */
    4914              : static NUMCacheEntry *
    4915       527991 : NUM_cache_fetch(const char *str)
    4916              : {
    4917              :     NUMCacheEntry *ent;
    4918              : 
    4919       527991 :     if ((ent = NUM_cache_search(str)) == NULL)
    4920              :     {
    4921              :         /*
    4922              :          * Not in the cache, must run parser and save a new format-picture to
    4923              :          * the cache.  Do not mark the cache entry valid until parsing
    4924              :          * succeeds.
    4925              :          */
    4926          310 :         ent = NUM_cache_getnew(str);
    4927              : 
    4928          310 :         memset(&ent->Num, 0, sizeof ent->Num);
    4929              : 
    4930          310 :         parse_format(ent->format, str, NUM_keywords,
    4931              :                      NULL, NUM_index, NUM_FLAG, &ent->Num);
    4932              : 
    4933          304 :         ent->valid = true;
    4934              :     }
    4935       527985 :     return ent;
    4936              : }
    4937              : 
    4938              : /*
    4939              :  * Cache routine for NUM to_char version
    4940              :  */
    4941              : static FormatNode *
    4942       528141 : NUM_cache(int len, NUMDesc *Num, const text *pars_str, bool *shouldFree)
    4943              : {
    4944       528141 :     FormatNode *format = NULL;
    4945              :     char       *str;
    4946              : 
    4947       528141 :     str = text_to_cstring(pars_str);
    4948              : 
    4949       528141 :     if (len > NUM_CACHE_SIZE)
    4950              :     {
    4951              :         /*
    4952              :          * Allocate new memory if format picture is bigger than static cache
    4953              :          * and do not use cache (call parser always)
    4954              :          */
    4955          150 :         format = (FormatNode *) palloc((len + 1) * sizeof(FormatNode));
    4956              : 
    4957          150 :         *shouldFree = true;
    4958              : 
    4959          150 :         memset(Num, 0, sizeof *Num);
    4960              : 
    4961          150 :         parse_format(format, str, NUM_keywords,
    4962              :                      NULL, NUM_index, NUM_FLAG, Num);
    4963              :     }
    4964              :     else
    4965              :     {
    4966              :         /*
    4967              :          * Use cache buffers
    4968              :          */
    4969       527991 :         NUMCacheEntry *ent = NUM_cache_fetch(str);
    4970              : 
    4971       527985 :         *shouldFree = false;
    4972              : 
    4973       527985 :         format = ent->format;
    4974              : 
    4975              :         /*
    4976              :          * Copy cache to used struct
    4977              :          */
    4978       527985 :         Num->flag = ent->Num.flag;
    4979       527985 :         Num->lsign = ent->Num.lsign;
    4980       527985 :         Num->pre = ent->Num.pre;
    4981       527985 :         Num->post = ent->Num.post;
    4982       527985 :         Num->pre_lsign_num = ent->Num.pre_lsign_num;
    4983       527985 :         Num->need_locale = ent->Num.need_locale;
    4984       527985 :         Num->multi = ent->Num.multi;
    4985       527985 :         Num->zero_start = ent->Num.zero_start;
    4986       527985 :         Num->zero_end = ent->Num.zero_end;
    4987              :     }
    4988              : 
    4989              : #ifdef DEBUG_TO_FROM_CHAR
    4990              :     /* dump_node(format, len); */
    4991              :     dump_index(NUM_keywords, NUM_index);
    4992              : #endif
    4993              : 
    4994       528135 :     pfree(str);
    4995       528135 :     return format;
    4996              : }
    4997              : 
    4998              : 
    4999              : /*
    5000              :  * Convert integer to Roman numerals
    5001              :  * Result is upper-case and not blank-padded (NUM_processor converts as needed)
    5002              :  * If input is out-of-range, produce '###############'
    5003              :  */
    5004              : static char *
    5005        12066 : int_to_roman(int number)
    5006              : {
    5007              :     int         len,
    5008              :                 num;
    5009              :     char       *result,
    5010              :                 numstr[12];
    5011              : 
    5012        12066 :     result = (char *) palloc(MAX_ROMAN_LEN + 1);
    5013        12066 :     *result = '\0';
    5014              : 
    5015              :     /*
    5016              :      * This range limit is the same as in Oracle(TM).  The difficulty with
    5017              :      * handling 4000 or more is that we'd need to use more than 3 "M"'s, and
    5018              :      * more than 3 of the same digit isn't considered a valid Roman string.
    5019              :      */
    5020        12066 :     if (number > 3999 || number < 1)
    5021              :     {
    5022           45 :         fill_str(result, '#', MAX_ROMAN_LEN);
    5023           45 :         return result;
    5024              :     }
    5025              : 
    5026              :     /* Convert to decimal, then examine each digit */
    5027        12021 :     len = snprintf(numstr, sizeof(numstr), "%d", number);
    5028              :     Assert(len > 0 && len <= 4);
    5029              : 
    5030        56766 :     for (char *p = numstr; *p != '\0'; p++, --len)
    5031              :     {
    5032        44745 :         num = *p - ('0' + 1);
    5033        44745 :         if (num < 0)
    5034         3273 :             continue;           /* ignore zeroes */
    5035              :         /* switch on current column position */
    5036        41472 :         switch (len)
    5037              :         {
    5038         9012 :             case 4:
    5039        27024 :                 while (num-- >= 0)
    5040        18012 :                     strcat(result, "M");
    5041         9012 :                 break;
    5042        10821 :             case 3:
    5043        10821 :                 strcat(result, rm100[num]);
    5044        10821 :                 break;
    5045        10818 :             case 2:
    5046        10818 :                 strcat(result, rm10[num]);
    5047        10818 :                 break;
    5048        10821 :             case 1:
    5049        10821 :                 strcat(result, rm1[num]);
    5050        10821 :                 break;
    5051              :         }
    5052              :     }
    5053        12021 :     return result;
    5054              : }
    5055              : 
    5056              : /*
    5057              :  * Convert a roman numeral (standard form) to an integer.
    5058              :  * Result is an integer between 1 and 3999.
    5059              :  * Np->inout_p is advanced past the characters consumed.
    5060              :  *
    5061              :  * If input is invalid, return -1.
    5062              :  */
    5063              : static int
    5064        12054 : roman_to_int(NUMProc *Np, size_t input_len)
    5065              : {
    5066        12054 :     int         result = 0;
    5067              :     size_t      len;
    5068              :     char        romanChars[MAX_ROMAN_LEN];
    5069              :     int         romanValues[MAX_ROMAN_LEN];
    5070        12054 :     int         repeatCount = 1;
    5071        12054 :     int         vCount = 0,
    5072        12054 :                 lCount = 0,
    5073        12054 :                 dCount = 0;
    5074        12054 :     bool        subtractionEncountered = false;
    5075        12054 :     int         lastSubtractedValue = 0;
    5076              : 
    5077              :     /*
    5078              :      * Skip any leading whitespace.  Perhaps we should limit the amount of
    5079              :      * space skipped to MAX_ROMAN_LEN, but that seems unnecessarily picky.
    5080              :      */
    5081       102012 :     while (!OVERLOAD_TEST && isspace((unsigned char) *Np->inout_p))
    5082        89958 :         Np->inout_p++;
    5083              : 
    5084              :     /*
    5085              :      * Collect and decode valid roman numerals, consuming at most
    5086              :      * MAX_ROMAN_LEN characters.  We do this in a separate loop to avoid
    5087              :      * repeated decoding and because the main loop needs to know when it's at
    5088              :      * the last numeral.
    5089              :      */
    5090       102231 :     for (len = 0; len < MAX_ROMAN_LEN && !OVERLOAD_TEST; len++)
    5091              :     {
    5092        90189 :         char        currChar = pg_ascii_toupper(*Np->inout_p);
    5093        90189 :         int         currValue = ROMAN_VAL(currChar);
    5094              : 
    5095        90189 :         if (currValue == 0)
    5096           12 :             break;              /* Not a valid roman numeral. */
    5097        90177 :         romanChars[len] = currChar;
    5098        90177 :         romanValues[len] = currValue;
    5099        90177 :         Np->inout_p++;
    5100              :     }
    5101              : 
    5102        12054 :     if (len == 0)
    5103            6 :         return -1;              /* No valid roman numerals. */
    5104              : 
    5105              :     /* Check for valid combinations and compute the represented value. */
    5106        94950 :     for (size_t i = 0; i < len; i++)
    5107              :     {
    5108        82938 :         char        currChar = romanChars[i];
    5109        82938 :         int         currValue = romanValues[i];
    5110              : 
    5111              :         /*
    5112              :          * Ensure no numeral greater than or equal to the subtracted numeral
    5113              :          * appears after a subtraction.
    5114              :          */
    5115        82938 :         if (subtractionEncountered && currValue >= lastSubtractedValue)
    5116            3 :             return -1;
    5117              : 
    5118              :         /*
    5119              :          * V, L, and D should not appear before a larger numeral, nor should
    5120              :          * they be repeated.
    5121              :          */
    5122        82935 :         if ((vCount && currValue >= ROMAN_VAL('V')) ||
    5123        82932 :             (lCount && currValue >= ROMAN_VAL('L')) ||
    5124        28815 :             (dCount && currValue >= ROMAN_VAL('D')))
    5125            3 :             return -1;
    5126        82932 :         if (currChar == 'V')
    5127         4812 :             vCount++;
    5128        78120 :         else if (currChar == 'L')
    5129         4806 :             lCount++;
    5130        73314 :         else if (currChar == 'D')
    5131         4809 :             dCount++;
    5132              : 
    5133        82932 :         if (i < len - 1)
    5134              :         {
    5135              :             /* Compare current numeral to next numeral. */
    5136        73590 :             char        nextChar = romanChars[i + 1];
    5137        73590 :             int         nextValue = romanValues[i + 1];
    5138              : 
    5139              :             /*
    5140              :              * If the current value is less than the next value, handle
    5141              :              * subtraction. Verify valid subtractive combinations and update
    5142              :              * the result accordingly.
    5143              :              */
    5144        73590 :             if (currValue < nextValue)
    5145              :             {
    5146         7236 :                 if (!IS_VALID_SUB_COMB(currChar, nextChar))
    5147            3 :                     return -1;
    5148              : 
    5149              :                 /*
    5150              :                  * Reject cases where same numeral is repeated with
    5151              :                  * subtraction (e.g. 'MCCM' or 'DCCCD').
    5152              :                  */
    5153         7233 :                 if (repeatCount > 1)
    5154            6 :                     return -1;
    5155              : 
    5156              :                 /*
    5157              :                  * We are going to skip nextChar, so first make checks needed
    5158              :                  * for V, L, and D.  These are the same as we'd have applied
    5159              :                  * if we reached nextChar without a subtraction.
    5160              :                  */
    5161         7227 :                 if ((vCount && nextValue >= ROMAN_VAL('V')) ||
    5162         7221 :                     (lCount && nextValue >= ROMAN_VAL('L')) ||
    5163         2406 :                     (dCount && nextValue >= ROMAN_VAL('D')))
    5164           18 :                     return -1;
    5165         7209 :                 if (nextChar == 'V')
    5166         1206 :                     vCount++;
    5167         6003 :                 else if (nextChar == 'L')
    5168         1200 :                     lCount++;
    5169         4803 :                 else if (nextChar == 'D')
    5170         1200 :                     dCount++;
    5171              : 
    5172              :                 /*
    5173              :                  * Skip the next numeral as it is part of the subtractive
    5174              :                  * combination.
    5175              :                  */
    5176         7209 :                 i++;
    5177              : 
    5178              :                 /* Update state. */
    5179         7209 :                 repeatCount = 1;
    5180         7209 :                 subtractionEncountered = true;
    5181         7209 :                 lastSubtractedValue = currValue;
    5182         7209 :                 result += (nextValue - currValue);
    5183              :             }
    5184              :             else
    5185              :             {
    5186              :                 /* For same numerals, check for repetition. */
    5187        66354 :                 if (currChar == nextChar)
    5188              :                 {
    5189        30639 :                     repeatCount++;
    5190        30639 :                     if (repeatCount > 3)
    5191            3 :                         return -1;
    5192              :                 }
    5193              :                 else
    5194        35715 :                     repeatCount = 1;
    5195        66351 :                 result += currValue;
    5196              :             }
    5197              :         }
    5198              :         else
    5199              :         {
    5200              :             /* This is the last numeral; just add it to the result. */
    5201         9342 :             result += currValue;
    5202              :         }
    5203              :     }
    5204              : 
    5205        12012 :     return result;
    5206              : }
    5207              : 
    5208              : 
    5209              : /*
    5210              :  * Locale
    5211              :  */
    5212              : static void
    5213       527967 : NUM_prepare_locale(NUMProc *Np)
    5214              : {
    5215       527967 :     if (Np->Num->need_locale)
    5216              :     {
    5217              :         struct lconv *lconv;
    5218              : 
    5219              :         /*
    5220              :          * Get locales
    5221              :          */
    5222          421 :         lconv = PGLC_localeconv();
    5223              : 
    5224              :         /*
    5225              :          * Positive / Negative number sign
    5226              :          */
    5227          421 :         if (lconv->negative_sign && *lconv->negative_sign)
    5228            0 :             Np->L_negative_sign = lconv->negative_sign;
    5229              :         else
    5230          421 :             Np->L_negative_sign = "-";
    5231              : 
    5232          421 :         if (lconv->positive_sign && *lconv->positive_sign)
    5233            0 :             Np->L_positive_sign = lconv->positive_sign;
    5234              :         else
    5235          421 :             Np->L_positive_sign = "+";
    5236              : 
    5237              :         /*
    5238              :          * Number decimal point
    5239              :          */
    5240          421 :         if (lconv->decimal_point && *lconv->decimal_point)
    5241          421 :             Np->decimal = lconv->decimal_point;
    5242              : 
    5243              :         else
    5244            0 :             Np->decimal = ".";
    5245              : 
    5246          421 :         if (!IS_LDECIMAL(Np->Num))
    5247          354 :             Np->decimal = ".";
    5248              : 
    5249              :         /*
    5250              :          * Number thousands separator
    5251              :          *
    5252              :          * Some locales (e.g. broken glibc pt_BR), have a comma for decimal,
    5253              :          * but "" for thousands_sep, so we set the thousands_sep too.
    5254              :          * http://archives.postgresql.org/pgsql-hackers/2007-11/msg00772.php
    5255              :          */
    5256          421 :         if (lconv->thousands_sep && *lconv->thousands_sep)
    5257            0 :             Np->L_thousands_sep = lconv->thousands_sep;
    5258              :         /* Make sure thousands separator doesn't match decimal point symbol. */
    5259          421 :         else if (strcmp(Np->decimal, ",") != 0)
    5260          421 :             Np->L_thousands_sep = ",";
    5261              :         else
    5262            0 :             Np->L_thousands_sep = ".";
    5263              : 
    5264              :         /*
    5265              :          * Currency symbol
    5266              :          */
    5267          421 :         if (lconv->currency_symbol && *lconv->currency_symbol)
    5268            0 :             Np->L_currency_symbol = lconv->currency_symbol;
    5269              :         else
    5270          421 :             Np->L_currency_symbol = " ";
    5271              :     }
    5272              :     else
    5273              :     {
    5274              :         /*
    5275              :          * Default values
    5276              :          */
    5277       527546 :         Np->L_negative_sign = "-";
    5278       527546 :         Np->L_positive_sign = "+";
    5279       527546 :         Np->decimal = ".";
    5280              : 
    5281       527546 :         Np->L_thousands_sep = ",";
    5282       527546 :         Np->L_currency_symbol = " ";
    5283              :     }
    5284       527967 : }
    5285              : 
    5286              : /*
    5287              :  * Return pointer of last relevant number after decimal point
    5288              :  *  12.0500 --> last relevant is '5'
    5289              :  *  12.0000 --> last relevant is '.'
    5290              :  * If there is no decimal point, return NULL (which will result in same
    5291              :  * behavior as if FM hadn't been specified).
    5292              :  */
    5293              : static const char *
    5294          339 : get_last_relevant_decnum(const char *num)
    5295              : {
    5296              :     const char *result,
    5297          339 :                *p = strchr(num, '.');
    5298              : 
    5299              : #ifdef DEBUG_TO_FROM_CHAR
    5300              :     elog(DEBUG_elog_output, "get_last_relevant_decnum()");
    5301              : #endif
    5302              : 
    5303          339 :     if (!p)
    5304            3 :         return NULL;
    5305              : 
    5306          336 :     result = p;
    5307              : 
    5308         4971 :     while (*(++p))
    5309              :     {
    5310         4635 :         if (*p != '0')
    5311          972 :             result = p;
    5312              :     }
    5313              : 
    5314          336 :     return result;
    5315              : }
    5316              : 
    5317              : /*
    5318              :  * Number extraction for TO_NUMBER()
    5319              :  */
    5320              : static void
    5321          447 : NUM_numpart_from_char(NUMProc *Np, int id, size_t input_len)
    5322              : {
    5323          447 :     bool        isread = false;
    5324              : 
    5325              : #ifdef DEBUG_TO_FROM_CHAR
    5326              :     elog(DEBUG_elog_output, " --- scan start --- id=%s",
    5327              :          (id == NUM_0 || id == NUM_9) ? "NUM_0/9" : id == NUM_DEC ? "NUM_DEC" : "???");
    5328              : #endif
    5329              : 
    5330          447 :     if (OVERLOAD_TEST)
    5331            0 :         return;
    5332              : 
    5333          447 :     if (*Np->inout_p == ' ')
    5334            0 :         Np->inout_p++;
    5335              : 
    5336          447 :     if (OVERLOAD_TEST)
    5337            0 :         return;
    5338              : 
    5339              :     /*
    5340              :      * read sign before number
    5341              :      */
    5342          447 :     if (*Np->number == ' ' && (id == NUM_0 || id == NUM_9) &&
    5343          303 :         (Np->read_pre + Np->read_post) == 0)
    5344              :     {
    5345              : #ifdef DEBUG_TO_FROM_CHAR
    5346              :         elog(DEBUG_elog_output, "Try read sign (%c), locale positive: %s, negative: %s",
    5347              :              *Np->inout_p, Np->L_positive_sign, Np->L_negative_sign);
    5348              : #endif
    5349              : 
    5350              :         /*
    5351              :          * locale sign
    5352              :          */
    5353           87 :         if (IS_LSIGN(Np->Num) && Np->Num->lsign == NUM_LSIGN_PRE)
    5354            6 :         {
    5355            6 :             size_t      x = 0;
    5356              : 
    5357              : #ifdef DEBUG_TO_FROM_CHAR
    5358              :             elog(DEBUG_elog_output, "Try read locale pre-sign (%c)", *Np->inout_p);
    5359              : #endif
    5360            6 :             if ((x = strlen(Np->L_negative_sign)) &&
    5361            6 :                 AMOUNT_TEST(x) &&
    5362            6 :                 strncmp(Np->inout_p, Np->L_negative_sign, x) == 0)
    5363              :             {
    5364            3 :                 Np->inout_p += x;
    5365            3 :                 *Np->number = '-';
    5366              :             }
    5367            3 :             else if ((x = strlen(Np->L_positive_sign)) &&
    5368            3 :                      AMOUNT_TEST(x) &&
    5369            3 :                      strncmp(Np->inout_p, Np->L_positive_sign, x) == 0)
    5370              :             {
    5371            0 :                 Np->inout_p += x;
    5372            0 :                 *Np->number = '+';
    5373              :             }
    5374              :         }
    5375              :         else
    5376              :         {
    5377              : #ifdef DEBUG_TO_FROM_CHAR
    5378              :             elog(DEBUG_elog_output, "Try read simple sign (%c)", *Np->inout_p);
    5379              : #endif
    5380              : 
    5381              :             /*
    5382              :              * simple + - < >
    5383              :              */
    5384           81 :             if (*Np->inout_p == '-' || (IS_BRACKET(Np->Num) &&
    5385            3 :                                         *Np->inout_p == '<'))
    5386              :             {
    5387            9 :                 *Np->number = '-';   /* set - */
    5388            9 :                 Np->inout_p++;
    5389              :             }
    5390           72 :             else if (*Np->inout_p == '+')
    5391              :             {
    5392            0 :                 *Np->number = '+';   /* set + */
    5393            0 :                 Np->inout_p++;
    5394              :             }
    5395              :         }
    5396              :     }
    5397              : 
    5398          447 :     if (OVERLOAD_TEST)
    5399            0 :         return;
    5400              : 
    5401              : #ifdef DEBUG_TO_FROM_CHAR
    5402              :     elog(DEBUG_elog_output, "Scan for numbers (%c), current number: '%s'", *Np->inout_p, Np->number);
    5403              : #endif
    5404              : 
    5405              :     /*
    5406              :      * read digit or decimal point
    5407              :      */
    5408          447 :     if (isdigit((unsigned char) *Np->inout_p))
    5409              :     {
    5410          381 :         if (Np->read_dec && Np->read_post == Np->Num->post)
    5411            0 :             return;
    5412              : 
    5413          381 :         *Np->number_p = *Np->inout_p;
    5414          381 :         Np->number_p++;
    5415              : 
    5416          381 :         if (Np->read_dec)
    5417          132 :             Np->read_post++;
    5418              :         else
    5419          249 :             Np->read_pre++;
    5420              : 
    5421          381 :         isread = true;
    5422              : 
    5423              : #ifdef DEBUG_TO_FROM_CHAR
    5424              :         elog(DEBUG_elog_output, "Read digit (%c)", *Np->inout_p);
    5425              : #endif
    5426              :     }
    5427           66 :     else if (IS_DECIMAL(Np->Num) && Np->read_dec == false)
    5428              :     {
    5429              :         /*
    5430              :          * We need not test IS_LDECIMAL(Np->Num) explicitly here, because
    5431              :          * Np->decimal is always just "." if we don't have a D format token.
    5432              :          * So we just unconditionally match to Np->decimal.
    5433              :          */
    5434           60 :         size_t      x = strlen(Np->decimal);
    5435              : 
    5436              : #ifdef DEBUG_TO_FROM_CHAR
    5437              :         elog(DEBUG_elog_output, "Try read decimal point (%c)",
    5438              :              *Np->inout_p);
    5439              : #endif
    5440           60 :         if (x && AMOUNT_TEST(x) && strncmp(Np->inout_p, Np->decimal, x) == 0)
    5441              :         {
    5442           54 :             Np->inout_p += x - 1;
    5443           54 :             *Np->number_p = '.';
    5444           54 :             Np->number_p++;
    5445           54 :             Np->read_dec = true;
    5446           54 :             isread = true;
    5447              :         }
    5448              :     }
    5449              : 
    5450          447 :     if (OVERLOAD_TEST)
    5451            0 :         return;
    5452              : 
    5453              :     /*
    5454              :      * Read sign behind "last" number
    5455              :      *
    5456              :      * We need sign detection because determine exact position of post-sign is
    5457              :      * difficult:
    5458              :      *
    5459              :      * FM9999.9999999S     -> 123.001- 9.9S             -> .5- FM9.999999MI ->
    5460              :      * 5.01-
    5461              :      */
    5462          447 :     if (*Np->number == ' ' && Np->read_pre + Np->read_post > 0)
    5463              :     {
    5464              :         /*
    5465              :          * locale sign (NUM_S) is always anchored behind a last number, if: -
    5466              :          * locale sign expected - last read char was NUM_0/9 or NUM_DEC - and
    5467              :          * next char is not digit
    5468              :          */
    5469          318 :         if (IS_LSIGN(Np->Num) && isread &&
    5470           87 :             (Np->inout_p + 1) < Np->inout + input_len &&
    5471           87 :             !isdigit((unsigned char) *(Np->inout_p + 1)))
    5472           39 :         {
    5473              :             size_t      x;
    5474           39 :             char       *tmp = Np->inout_p++;
    5475              : 
    5476              : #ifdef DEBUG_TO_FROM_CHAR
    5477              :             elog(DEBUG_elog_output, "Try read locale post-sign (%c)", *Np->inout_p);
    5478              : #endif
    5479           39 :             if ((x = strlen(Np->L_negative_sign)) &&
    5480           39 :                 AMOUNT_TEST(x) &&
    5481           39 :                 strncmp(Np->inout_p, Np->L_negative_sign, x) == 0)
    5482              :             {
    5483           18 :                 Np->inout_p += x - 1;    /* -1 .. NUM_processor() do inout_p++ */
    5484           18 :                 *Np->number = '-';
    5485              :             }
    5486           21 :             else if ((x = strlen(Np->L_positive_sign)) &&
    5487           21 :                      AMOUNT_TEST(x) &&
    5488           21 :                      strncmp(Np->inout_p, Np->L_positive_sign, x) == 0)
    5489              :             {
    5490            0 :                 Np->inout_p += x - 1;    /* -1 .. NUM_processor() do inout_p++ */
    5491            0 :                 *Np->number = '+';
    5492              :             }
    5493           39 :             if (*Np->number == ' ')
    5494              :                 /* no sign read */
    5495           21 :                 Np->inout_p = tmp;
    5496              :         }
    5497              : 
    5498              :         /*
    5499              :          * try read non-locale sign, which happens only if format is not exact
    5500              :          * and we cannot determine sign position of MI/PL/SG, an example:
    5501              :          *
    5502              :          * FM9.999999MI            -> 5.01-
    5503              :          *
    5504              :          * if (.... && IS_LSIGN(Np->Num)==false) prevents read wrong formats
    5505              :          * like to_number('1 -', '9S') where sign is not anchored to last
    5506              :          * number.
    5507              :          */
    5508          279 :         else if (isread == false && IS_LSIGN(Np->Num) == false &&
    5509           12 :                  (IS_PLUS(Np->Num) || IS_MINUS(Np->Num)))
    5510              :         {
    5511              : #ifdef DEBUG_TO_FROM_CHAR
    5512              :             elog(DEBUG_elog_output, "Try read simple post-sign (%c)", *Np->inout_p);
    5513              : #endif
    5514              : 
    5515              :             /*
    5516              :              * simple + -
    5517              :              */
    5518            3 :             if (*Np->inout_p == '-' || *Np->inout_p == '+')
    5519              :                 /* NUM_processor() do inout_p++ */
    5520            3 :                 *Np->number = *Np->inout_p;
    5521              :         }
    5522              :     }
    5523              : }
    5524              : 
    5525              : #define IS_PREDEC_SPACE(_n) \
    5526              :         (IS_ZERO((_n)->Num)==false && \
    5527              :          (_n)->number == (_n)->number_p && \
    5528              :          *(_n)->number == '0' && \
    5529              :                  (_n)->Num->post != 0)
    5530              : 
    5531              : /*
    5532              :  * Add digit or sign to number-string
    5533              :  */
    5534              : static void
    5535      4438330 : NUM_numpart_to_char(NUMProc *Np, int id)
    5536              : {
    5537              :     int         end;
    5538              : 
    5539      4438330 :     if (IS_ROMAN(Np->Num))
    5540            0 :         return;
    5541              : 
    5542              :     /* Note: in this elog() output not set '\0' in 'inout' */
    5543              : 
    5544              : #ifdef DEBUG_TO_FROM_CHAR
    5545              : 
    5546              :     /*
    5547              :      * Np->num_curr is number of current item in format-picture, it is not
    5548              :      * current position in inout!
    5549              :      */
    5550              :     elog(DEBUG_elog_output,
    5551              :          "SIGN_WROTE: %d, CURRENT: %d, NUMBER_P: \"%s\", INOUT: \"%s\"",
    5552              :          Np->sign_wrote,
    5553              :          Np->num_curr,
    5554              :          Np->number_p,
    5555              :          Np->inout);
    5556              : #endif
    5557      4438330 :     Np->num_in = false;
    5558              : 
    5559              :     /*
    5560              :      * Write sign if real number will write to output Note: IS_PREDEC_SPACE()
    5561              :      * handle "9.9" --> " .1"
    5562              :      */
    5563      4438330 :     if (Np->sign_wrote == false &&
    5564         7476 :         (Np->num_curr >= Np->out_pre_spaces || (IS_ZERO(Np->Num) && Np->Num->zero_start == Np->num_curr)) &&
    5565         1521 :         (IS_PREDEC_SPACE(Np) == false || (Np->last_relevant && *Np->last_relevant == '.')))
    5566              :     {
    5567         1449 :         if (IS_LSIGN(Np->Num))
    5568              :         {
    5569          969 :             if (Np->Num->lsign == NUM_LSIGN_PRE)
    5570              :             {
    5571          180 :                 if (Np->sign == '-')
    5572           57 :                     strcpy(Np->inout_p, Np->L_negative_sign);
    5573              :                 else
    5574          123 :                     strcpy(Np->inout_p, Np->L_positive_sign);
    5575          180 :                 Np->inout_p += strlen(Np->inout_p);
    5576          180 :                 Np->sign_wrote = true;
    5577              :             }
    5578              :         }
    5579          480 :         else if (IS_BRACKET(Np->Num))
    5580              :         {
    5581           72 :             *Np->inout_p = Np->sign == '+' ? ' ' : '<';
    5582           72 :             ++Np->inout_p;
    5583           72 :             Np->sign_wrote = true;
    5584              :         }
    5585          408 :         else if (Np->sign == '+')
    5586              :         {
    5587          273 :             if (!IS_FILLMODE(Np->Num))
    5588              :             {
    5589          273 :                 *Np->inout_p = ' '; /* Write + */
    5590          273 :                 ++Np->inout_p;
    5591              :             }
    5592          273 :             Np->sign_wrote = true;
    5593              :         }
    5594          135 :         else if (Np->sign == '-')
    5595              :         {                       /* Write - */
    5596          135 :             *Np->inout_p = '-';
    5597          135 :             ++Np->inout_p;
    5598          135 :             Np->sign_wrote = true;
    5599              :         }
    5600              :     }
    5601              : 
    5602              : 
    5603              :     /*
    5604              :      * digits / FM / Zero / Dec. point
    5605              :      */
    5606      4438330 :     if (id == NUM_9 || id == NUM_0 || id == NUM_D || id == NUM_DEC)
    5607              :     {
    5608      4438330 :         if (Np->num_curr < Np->out_pre_spaces &&
    5609      2680991 :             (Np->Num->zero_start > Np->num_curr || !IS_ZERO(Np->Num)))
    5610              :         {
    5611              :             /*
    5612              :              * Write blank space
    5613              :              */
    5614         7821 :             if (!IS_FILLMODE(Np->Num))
    5615              :             {
    5616         4944 :                 *Np->inout_p = ' '; /* Write ' ' */
    5617         4944 :                 ++Np->inout_p;
    5618              :             }
    5619              :         }
    5620      4430509 :         else if (IS_ZERO(Np->Num) &&
    5621      4417027 :                  Np->num_curr < Np->out_pre_spaces &&
    5622      2673170 :                  Np->Num->zero_start <= Np->num_curr)
    5623              :         {
    5624              :             /*
    5625              :              * Write ZERO
    5626              :              */
    5627      2673170 :             *Np->inout_p = '0'; /* Write '0' */
    5628      2673170 :             ++Np->inout_p;
    5629      2673170 :             Np->num_in = true;
    5630              :         }
    5631              :         else
    5632              :         {
    5633              :             /*
    5634              :              * Write Decimal point
    5635              :              */
    5636      1757339 :             if (*Np->number_p == '.')
    5637              :             {
    5638          784 :                 if (!Np->last_relevant || *Np->last_relevant != '.')
    5639              :                 {
    5640          694 :                     strcpy(Np->inout_p, Np->decimal); /* Write DEC/D */
    5641          694 :                     Np->inout_p += strlen(Np->inout_p);
    5642              :                 }
    5643              : 
    5644              :                 /*
    5645              :                  * Ora 'n' -- FM9.9 --> 'n.'
    5646              :                  */
    5647           90 :                 else if (IS_FILLMODE(Np->Num) &&
    5648           90 :                          Np->last_relevant && *Np->last_relevant == '.')
    5649              :                 {
    5650           90 :                     strcpy(Np->inout_p, Np->decimal); /* Write DEC/D */
    5651           90 :                     Np->inout_p += strlen(Np->inout_p);
    5652              :                 }
    5653              :             }
    5654              :             else
    5655              :             {
    5656              :                 /*
    5657              :                  * Write Digits
    5658              :                  */
    5659      1756555 :                 if (Np->last_relevant && Np->number_p > Np->last_relevant &&
    5660              :                     id != NUM_0)
    5661              :                     ;
    5662              : 
    5663              :                 /*
    5664              :                  * '0.1' -- 9.9 --> '  .1'
    5665              :                  */
    5666      1753219 :                 else if (IS_PREDEC_SPACE(Np))
    5667              :                 {
    5668          114 :                     if (!IS_FILLMODE(Np->Num))
    5669              :                     {
    5670           78 :                         *Np->inout_p = ' ';
    5671           78 :                         ++Np->inout_p;
    5672              :                     }
    5673              : 
    5674              :                     /*
    5675              :                      * '0' -- FM9.9 --> '0.'
    5676              :                      */
    5677           36 :                     else if (Np->last_relevant && *Np->last_relevant == '.')
    5678              :                     {
    5679           30 :                         *Np->inout_p = '0';
    5680           30 :                         ++Np->inout_p;
    5681              :                     }
    5682              :                 }
    5683              :                 else
    5684              :                 {
    5685      1753105 :                     *Np->inout_p = *Np->number_p; /* Write DIGIT */
    5686      1753105 :                     ++Np->inout_p;
    5687      1753105 :                     Np->num_in = true;
    5688              :                 }
    5689              :             }
    5690              :             /* do no exceed string length */
    5691      1757339 :             if (*Np->number_p)
    5692      1757168 :                 ++Np->number_p;
    5693              :         }
    5694              : 
    5695      4438330 :         end = Np->num_count + (Np->out_pre_spaces ? 1 : 0) + (IS_DECIMAL(Np->Num) ? 1 : 0);
    5696              : 
    5697      4438330 :         if (Np->last_relevant && Np->last_relevant == Np->number_p)
    5698          336 :             end = Np->num_curr;
    5699              : 
    5700      4438330 :         if (Np->num_curr + 1 == end)
    5701              :         {
    5702       503757 :             if (Np->sign_wrote == true && IS_BRACKET(Np->Num))
    5703              :             {
    5704           72 :                 *Np->inout_p = Np->sign == '+' ? ' ' : '>';
    5705           72 :                 ++Np->inout_p;
    5706              :             }
    5707       503685 :             else if (IS_LSIGN(Np->Num) && Np->Num->lsign == NUM_LSIGN_POST)
    5708              :             {
    5709           46 :                 if (Np->sign == '-')
    5710           25 :                     strcpy(Np->inout_p, Np->L_negative_sign);
    5711              :                 else
    5712           21 :                     strcpy(Np->inout_p, Np->L_positive_sign);
    5713           46 :                 Np->inout_p += strlen(Np->inout_p);
    5714              :             }
    5715              :         }
    5716              :     }
    5717              : 
    5718      4438330 :     ++Np->num_curr;
    5719              : }
    5720              : 
    5721              : /*
    5722              :  * Skip over "n" input characters, but only if they aren't numeric data
    5723              :  */
    5724              : static void
    5725           18 : NUM_eat_non_data_chars(NUMProc *Np, int n, size_t input_len)
    5726              : {
    5727           18 :     const char *end = Np->inout + input_len;
    5728              : 
    5729           33 :     while (n-- > 0)
    5730              :     {
    5731           21 :         if (OVERLOAD_TEST)
    5732            0 :             break;              /* end of input */
    5733           21 :         if (strchr("0123456789.,+-", *Np->inout_p) != NULL)
    5734            6 :             break;              /* it's a data character */
    5735           15 :         Np->inout_p += pg_mblen_range(Np->inout_p, end);
    5736              :     }
    5737           18 : }
    5738              : 
    5739              : static char *
    5740       528135 : NUM_processor(FormatNode *node, NUMDesc *Num, char *inout,
    5741              :               char *number, size_t input_len, int to_char_out_pre_spaces,
    5742              :               int sign, bool is_to_char, Oid collid)
    5743              : {
    5744              :     FormatNode *n;
    5745              :     NUMProc     _Np,
    5746       528135 :                *Np = &_Np;
    5747              :     const char *pattern;
    5748              :     size_t      pattern_len;
    5749              : 
    5750      9506430 :     MemSet(Np, 0, sizeof(NUMProc));
    5751              : 
    5752       528135 :     Np->Num = Num;
    5753       528135 :     Np->is_to_char = is_to_char;
    5754       528135 :     Np->number = number;
    5755       528135 :     Np->inout = inout;
    5756       528135 :     Np->last_relevant = NULL;
    5757       528135 :     Np->read_post = 0;
    5758       528135 :     Np->read_pre = 0;
    5759       528135 :     Np->read_dec = false;
    5760              : 
    5761       528135 :     if (Np->Num->zero_start)
    5762       502796 :         --Np->Num->zero_start;
    5763              : 
    5764       528135 :     if (IS_EEEE(Np->Num))
    5765              :     {
    5766          168 :         if (!Np->is_to_char)
    5767            0 :             ereport(ERROR,
    5768              :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    5769              :                      errmsg("\"EEEE\" not supported for input")));
    5770          168 :         return strcpy(inout, number);
    5771              :     }
    5772              : 
    5773              :     /*
    5774              :      * Sign
    5775              :      */
    5776       527967 :     if (is_to_char)
    5777              :     {
    5778       515835 :         Np->sign = sign;
    5779              : 
    5780              :         /* MI/PL/SG - write sign itself and not in number */
    5781       515835 :         if (IS_PLUS(Np->Num) || IS_MINUS(Np->Num))
    5782              :         {
    5783          276 :             if (IS_PLUS(Np->Num) && IS_MINUS(Np->Num) == false)
    5784           15 :                 Np->sign_wrote = false; /* need sign */
    5785              :             else
    5786          261 :                 Np->sign_wrote = true;   /* needn't sign */
    5787              :         }
    5788              :         else
    5789              :         {
    5790       515559 :             if (Np->sign != '-')
    5791              :             {
    5792       515297 :                 if (IS_FILLMODE(Np->Num))
    5793       502901 :                     Np->Num->flag &= ~NUM_F_BRACKET;
    5794              :             }
    5795              : 
    5796       515559 :             if (Np->sign == '+' && IS_FILLMODE(Np->Num) && IS_LSIGN(Np->Num) == false)
    5797       502799 :                 Np->sign_wrote = true;   /* needn't sign */
    5798              :             else
    5799        12760 :                 Np->sign_wrote = false; /* need sign */
    5800              : 
    5801       515559 :             if (Np->Num->lsign == NUM_LSIGN_PRE && Np->Num->pre == Np->Num->pre_lsign_num)
    5802           15 :                 Np->Num->lsign = NUM_LSIGN_POST;
    5803              :         }
    5804              :     }
    5805              :     else
    5806        12132 :         Np->sign = false;
    5807              : 
    5808              :     /*
    5809              :      * Count
    5810              :      */
    5811       527967 :     Np->num_count = Np->Num->post + Np->Num->pre - 1;
    5812              : 
    5813       527967 :     if (is_to_char)
    5814              :     {
    5815       515835 :         Np->out_pre_spaces = to_char_out_pre_spaces;
    5816              : 
    5817       515835 :         if (IS_FILLMODE(Np->Num) && IS_DECIMAL(Np->Num))
    5818              :         {
    5819          339 :             Np->last_relevant = get_last_relevant_decnum(Np->number);
    5820              : 
    5821              :             /*
    5822              :              * If any '0' specifiers are present, make sure we don't strip
    5823              :              * those digits.  But don't advance last_relevant beyond the last
    5824              :              * character of the Np->number string, which is a hazard if the
    5825              :              * number got shortened due to precision limitations.
    5826              :              */
    5827          339 :             if (Np->last_relevant && Np->Num->zero_end > Np->out_pre_spaces)
    5828              :             {
    5829              :                 size_t      last_zero_pos;
    5830              :                 char       *last_zero;
    5831              : 
    5832              :                 /* note that Np->number cannot be zero-length here */
    5833          138 :                 last_zero_pos = strlen(Np->number) - 1;
    5834          138 :                 last_zero_pos = Min(last_zero_pos,
    5835              :                                     Np->Num->zero_end - Np->out_pre_spaces);
    5836          138 :                 last_zero = Np->number + last_zero_pos;
    5837          138 :                 if (Np->last_relevant < last_zero)
    5838           72 :                     Np->last_relevant = last_zero;
    5839              :             }
    5840              :         }
    5841              : 
    5842       515835 :         if (Np->sign_wrote == false && Np->out_pre_spaces == 0)
    5843        12265 :             ++Np->num_count;
    5844              :     }
    5845              :     else
    5846              :     {
    5847        12132 :         Np->out_pre_spaces = 0;
    5848        12132 :         *Np->number = ' ';       /* sign space */
    5849        12132 :         *(Np->number + 1) = '\0';
    5850              :     }
    5851              : 
    5852       527967 :     Np->num_in = 0;
    5853       527967 :     Np->num_curr = 0;
    5854              : 
    5855              : #ifdef DEBUG_TO_FROM_CHAR
    5856              :     elog(DEBUG_elog_output,
    5857              :          "\n\tSIGN: '%c'\n\tNUM: '%s'\n\tPRE: %d\n\tPOST: %d\n\tNUM_COUNT: %d\n\tNUM_PRE: %d\n\tSIGN_WROTE: %s\n\tZERO: %s\n\tZERO_START: %d\n\tZERO_END: %d\n\tLAST_RELEVANT: %s\n\tBRACKET: %s\n\tPLUS: %s\n\tMINUS: %s\n\tFILLMODE: %s\n\tROMAN: %s\n\tEEEE: %s",
    5858              :          Np->sign,
    5859              :          Np->number,
    5860              :          Np->Num->pre,
    5861              :          Np->Num->post,
    5862              :          Np->num_count,
    5863              :          Np->out_pre_spaces,
    5864              :          Np->sign_wrote ? "Yes" : "No",
    5865              :          IS_ZERO(Np->Num) ? "Yes" : "No",
    5866              :          Np->Num->zero_start,
    5867              :          Np->Num->zero_end,
    5868              :          Np->last_relevant ? Np->last_relevant : "<not set>",
    5869              :          IS_BRACKET(Np->Num) ? "Yes" : "No",
    5870              :          IS_PLUS(Np->Num) ? "Yes" : "No",
    5871              :          IS_MINUS(Np->Num) ? "Yes" : "No",
    5872              :          IS_FILLMODE(Np->Num) ? "Yes" : "No",
    5873              :          IS_ROMAN(Np->Num) ? "Yes" : "No",
    5874              :          IS_EEEE(Np->Num) ? "Yes" : "No"
    5875              :         );
    5876              : #endif
    5877              : 
    5878              :     /*
    5879              :      * Locale
    5880              :      */
    5881       527967 :     NUM_prepare_locale(Np);
    5882              : 
    5883              :     /*
    5884              :      * Processor direct cycle
    5885              :      */
    5886       527967 :     if (Np->is_to_char)
    5887       515835 :         Np->number_p = Np->number;
    5888              :     else
    5889        12132 :         Np->number_p = Np->number + 1;    /* first char is space for sign */
    5890              : 
    5891      5499742 :     for (n = node, Np->inout_p = Np->inout; n->type != NODE_TYPE_END; n++)
    5892              :     {
    5893      4971862 :         if (!Np->is_to_char)
    5894              :         {
    5895              :             /*
    5896              :              * Check at least one byte remains to be scanned.  (In actions
    5897              :              * below, must use AMOUNT_TEST if we want to read more bytes than
    5898              :              * that.)
    5899              :              */
    5900        12672 :             if (OVERLOAD_TEST)
    5901           45 :                 break;
    5902              :         }
    5903              : 
    5904              :         /*
    5905              :          * Format pictures actions
    5906              :          */
    5907      4971817 :         if (n->type == NODE_TYPE_ACTION)
    5908              :         {
    5909              :             /*
    5910              :              * Create/read digit/zero/blank/sign/special-case
    5911              :              *
    5912              :              * 'NUM_S' note: The locale sign is anchored to number and we
    5913              :              * read/write it when we work with first or last number
    5914              :              * (NUM_0/NUM_9).  This is why NUM_S is missing in switch().
    5915              :              *
    5916              :              * Notice the "Np->inout_p++" at the bottom of the loop.  This is
    5917              :              * why most of the actions advance inout_p one less than you might
    5918              :              * expect.  In cases where we don't want that increment to happen,
    5919              :              * a switch case ends with "continue" not "break".
    5920              :              */
    5921      4967491 :             switch (n->key->id)
    5922              :             {
    5923      4438777 :                 case NUM_9:
    5924              :                 case NUM_0:
    5925              :                 case NUM_DEC:
    5926              :                 case NUM_D:
    5927      4438777 :                     if (Np->is_to_char)
    5928              :                     {
    5929      4438330 :                         NUM_numpart_to_char(Np, n->key->id);
    5930      4438330 :                         continue;   /* for() */
    5931              :                     }
    5932              :                     else
    5933              :                     {
    5934          447 :                         NUM_numpart_from_char(Np, n->key->id, input_len);
    5935          447 :                         break;  /* switch() case: */
    5936              :                     }
    5937              : 
    5938          183 :                 case NUM_COMMA:
    5939          183 :                     if (Np->is_to_char)
    5940              :                     {
    5941          165 :                         if (!Np->num_in)
    5942              :                         {
    5943           60 :                             if (IS_FILLMODE(Np->Num))
    5944            0 :                                 continue;
    5945              :                             else
    5946           60 :                                 *Np->inout_p = ' ';
    5947              :                         }
    5948              :                         else
    5949          105 :                             *Np->inout_p = ',';
    5950              :                     }
    5951              :                     else
    5952              :                     {
    5953           18 :                         if (!Np->num_in)
    5954              :                         {
    5955           18 :                             if (IS_FILLMODE(Np->Num))
    5956            0 :                                 continue;
    5957              :                         }
    5958           18 :                         if (*Np->inout_p != ',')
    5959           18 :                             continue;
    5960              :                     }
    5961          165 :                     break;
    5962              : 
    5963          612 :                 case NUM_G:
    5964          612 :                     pattern = Np->L_thousands_sep;
    5965          612 :                     pattern_len = strlen(pattern);
    5966          612 :                     if (Np->is_to_char)
    5967              :                     {
    5968          585 :                         if (!Np->num_in)
    5969              :                         {
    5970          294 :                             if (IS_FILLMODE(Np->Num))
    5971            0 :                                 continue;
    5972              :                             else
    5973              :                             {
    5974              :                                 /* just in case there are MB chars */
    5975          294 :                                 pattern_len = pg_mbstrlen(pattern);
    5976          294 :                                 memset(Np->inout_p, ' ', pattern_len);
    5977          294 :                                 Np->inout_p += pattern_len - 1;
    5978              :                             }
    5979              :                         }
    5980              :                         else
    5981              :                         {
    5982          291 :                             strcpy(Np->inout_p, pattern);
    5983          291 :                             Np->inout_p += pattern_len - 1;
    5984              :                         }
    5985              :                     }
    5986              :                     else
    5987              :                     {
    5988           27 :                         if (!Np->num_in)
    5989              :                         {
    5990           27 :                             if (IS_FILLMODE(Np->Num))
    5991            0 :                                 continue;
    5992              :                         }
    5993              : 
    5994              :                         /*
    5995              :                          * Because L_thousands_sep typically contains data
    5996              :                          * characters (either '.' or ','), we can't use
    5997              :                          * NUM_eat_non_data_chars here.  Instead skip only if
    5998              :                          * the input matches L_thousands_sep.
    5999              :                          */
    6000           27 :                         if (AMOUNT_TEST(pattern_len) &&
    6001           27 :                             strncmp(Np->inout_p, pattern, pattern_len) == 0)
    6002           24 :                             Np->inout_p += pattern_len - 1;
    6003              :                         else
    6004            3 :                             continue;
    6005              :                     }
    6006          609 :                     break;
    6007              : 
    6008           60 :                 case NUM_L:
    6009           60 :                     pattern = Np->L_currency_symbol;
    6010           60 :                     if (Np->is_to_char)
    6011              :                     {
    6012           45 :                         strcpy(Np->inout_p, pattern);
    6013           45 :                         Np->inout_p += strlen(pattern) - 1;
    6014              :                     }
    6015              :                     else
    6016              :                     {
    6017           15 :                         NUM_eat_non_data_chars(Np, pg_mbstrlen(pattern), input_len);
    6018           15 :                         continue;
    6019              :                     }
    6020           45 :                     break;
    6021              : 
    6022        24120 :                 case NUM_RN:
    6023              :                 case NUM_rn:
    6024        24120 :                     if (Np->is_to_char)
    6025              :                     {
    6026              :                         const char *number_p;
    6027              : 
    6028        12066 :                         if (n->key->id == NUM_rn)
    6029           15 :                             number_p = asc_tolower_z(Np->number_p);
    6030              :                         else
    6031        12051 :                             number_p = Np->number_p;
    6032        12066 :                         if (IS_FILLMODE(Np->Num))
    6033           48 :                             strcpy(Np->inout_p, number_p);
    6034              :                         else
    6035        12018 :                             sprintf(Np->inout_p, "%15s", number_p);
    6036        12066 :                         Np->inout_p += strlen(Np->inout_p) - 1;
    6037              :                     }
    6038              :                     else
    6039        12012 :                     {
    6040        12054 :                         int         roman_result = roman_to_int(Np, input_len);
    6041              :                         int         numlen;
    6042              : 
    6043        12054 :                         if (roman_result < 0)
    6044           42 :                             ereport(ERROR,
    6045              :                                     (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
    6046              :                                      errmsg("invalid Roman numeral")));
    6047        12012 :                         numlen = sprintf(Np->number_p, "%d", roman_result);
    6048        12012 :                         Np->number_p += numlen;
    6049        12012 :                         Np->Num->pre = numlen;
    6050        12012 :                         Np->Num->post = 0;
    6051        12012 :                         continue;   /* roman_to_int ate all the chars */
    6052              :                     }
    6053        12066 :                     break;
    6054              : 
    6055           48 :                 case NUM_th:
    6056           48 :                     if (IS_ROMAN(Np->Num) || *Np->number == '#' ||
    6057           48 :                         Np->sign == '-' || IS_DECIMAL(Np->Num))
    6058           33 :                         continue;
    6059              : 
    6060           15 :                     if (Np->is_to_char)
    6061              :                     {
    6062           12 :                         strcpy(Np->inout_p, get_th(Np->number, TH_LOWER));
    6063           12 :                         Np->inout_p += 1;
    6064              :                     }
    6065              :                     else
    6066              :                     {
    6067              :                         /* All variants of 'th' occupy 2 characters */
    6068            3 :                         NUM_eat_non_data_chars(Np, 2, input_len);
    6069            3 :                         continue;
    6070              :                     }
    6071           12 :                     break;
    6072              : 
    6073           45 :                 case NUM_TH:
    6074           45 :                     if (IS_ROMAN(Np->Num) || *Np->number == '#' ||
    6075           45 :                         Np->sign == '-' || IS_DECIMAL(Np->Num))
    6076           33 :                         continue;
    6077              : 
    6078           12 :                     if (Np->is_to_char)
    6079              :                     {
    6080           12 :                         strcpy(Np->inout_p, get_th(Np->number, TH_UPPER));
    6081           12 :                         Np->inout_p += 1;
    6082              :                     }
    6083              :                     else
    6084              :                     {
    6085              :                         /* All variants of 'TH' occupy 2 characters */
    6086            0 :                         NUM_eat_non_data_chars(Np, 2, input_len);
    6087            0 :                         continue;
    6088              :                     }
    6089           12 :                     break;
    6090              : 
    6091          171 :                 case NUM_MI:
    6092          171 :                     if (Np->is_to_char)
    6093              :                     {
    6094          171 :                         if (Np->sign == '-')
    6095           48 :                             *Np->inout_p = '-';
    6096          123 :                         else if (IS_FILLMODE(Np->Num))
    6097            0 :                             continue;
    6098              :                         else
    6099          123 :                             *Np->inout_p = ' ';
    6100              :                     }
    6101              :                     else
    6102              :                     {
    6103            0 :                         if (*Np->inout_p == '-')
    6104            0 :                             *Np->number = '-';
    6105              :                         else
    6106              :                         {
    6107            0 :                             NUM_eat_non_data_chars(Np, 1, input_len);
    6108            0 :                             continue;
    6109              :                         }
    6110              :                     }
    6111          171 :                     break;
    6112              : 
    6113           15 :                 case NUM_PL:
    6114           15 :                     if (Np->is_to_char)
    6115              :                     {
    6116           15 :                         if (Np->sign == '+')
    6117           12 :                             *Np->inout_p = '+';
    6118            3 :                         else if (IS_FILLMODE(Np->Num))
    6119            0 :                             continue;
    6120              :                         else
    6121            3 :                             *Np->inout_p = ' ';
    6122              :                     }
    6123              :                     else
    6124              :                     {
    6125            0 :                         if (*Np->inout_p == '+')
    6126            0 :                             *Np->number = '+';
    6127              :                         else
    6128              :                         {
    6129            0 :                             NUM_eat_non_data_chars(Np, 1, input_len);
    6130            0 :                             continue;
    6131              :                         }
    6132              :                     }
    6133           15 :                     break;
    6134              : 
    6135           90 :                 case NUM_SG:
    6136           90 :                     if (Np->is_to_char)
    6137           90 :                         *Np->inout_p = Np->sign;
    6138              :                     else
    6139              :                     {
    6140            0 :                         if (*Np->inout_p == '-')
    6141            0 :                             *Np->number = '-';
    6142            0 :                         else if (*Np->inout_p == '+')
    6143            0 :                             *Np->number = '+';
    6144              :                         else
    6145              :                         {
    6146            0 :                             NUM_eat_non_data_chars(Np, 1, input_len);
    6147            0 :                             continue;
    6148              :                         }
    6149              :                     }
    6150           90 :                     break;
    6151              : 
    6152       503370 :                 default:
    6153       503370 :                     continue;
    6154              :                     break;
    6155              :             }
    6156              :         }
    6157              :         else
    6158              :         {
    6159              :             /*
    6160              :              * In TO_CHAR, non-pattern characters in the format are copied to
    6161              :              * the output.  In TO_NUMBER, we skip one input character for each
    6162              :              * non-pattern format character, whether or not it matches the
    6163              :              * format character.
    6164              :              */
    6165         4326 :             if (Np->is_to_char)
    6166              :             {
    6167         4281 :                 strcpy(Np->inout_p, n->character);
    6168         4281 :                 Np->inout_p += strlen(Np->inout_p);
    6169              :             }
    6170              :             else
    6171              :             {
    6172           45 :                 Np->inout_p += pg_mblen_range(Np->inout_p, Np->inout + input_len);
    6173              :             }
    6174         4326 :             continue;
    6175              :         }
    6176        13632 :         Np->inout_p++;
    6177              :     }
    6178              : 
    6179       527925 :     if (Np->is_to_char)
    6180              :     {
    6181       515835 :         *Np->inout_p = '\0';
    6182       515835 :         return Np->inout;
    6183              :     }
    6184              :     else
    6185              :     {
    6186        12090 :         if (*(Np->number_p - 1) == '.')
    6187            0 :             *(Np->number_p - 1) = '\0';
    6188              :         else
    6189        12090 :             *Np->number_p = '\0';
    6190              : 
    6191              :         /*
    6192              :          * Correction - precision of dec. number
    6193              :          */
    6194        12090 :         Np->Num->post = Np->read_post;
    6195              : 
    6196              : #ifdef DEBUG_TO_FROM_CHAR
    6197              :         elog(DEBUG_elog_output, "TO_NUMBER (number): '%s'", Np->number);
    6198              : #endif
    6199        12090 :         return Np->number;
    6200              :     }
    6201              : }
    6202              : 
    6203              : /*
    6204              :  * MACRO: Start part of NUM - for all NUM's to_char variants
    6205              :  *  (sorry, but I hate copy same code - macro is better..)
    6206              :  */
    6207              : #define NUM_TOCHAR_prepare \
    6208              : do { \
    6209              :     int len = VARSIZE_ANY_EXHDR(fmt); \
    6210              :     if (len <= 0 || len >= (INT_MAX-VARHDRSZ)/NUM_MAX_ITEM_SIZ)       \
    6211              :         PG_RETURN_TEXT_P(cstring_to_text("")); \
    6212              :     result  = (text *) palloc0((len * NUM_MAX_ITEM_SIZ) + 1 + VARHDRSZ);    \
    6213              :     format  = NUM_cache(len, &Num, fmt, &shouldFree);       \
    6214              : } while (0)
    6215              : 
    6216              : /*
    6217              :  * MACRO: Finish part of NUM
    6218              :  */
    6219              : #define NUM_TOCHAR_finish \
    6220              : do { \
    6221              :     size_t  len; \
    6222              :                                     \
    6223              :     NUM_processor(format, &Num, VARDATA(result), numstr, 0, out_pre_spaces, sign, true, PG_GET_COLLATION()); \
    6224              :                                     \
    6225              :     if (shouldFree)                 \
    6226              :         pfree(format);              \
    6227              :                                     \
    6228              :     /*                              \
    6229              :      * Convert null-terminated representation of result to standard text. \
    6230              :      * The result is usually much bigger than it needs to be, but there \
    6231              :      * seems little point in realloc'ing it smaller. \
    6232              :      */                             \
    6233              :     len = strlen(VARDATA(result));  \
    6234              :     SET_VARSIZE(result, len + VARHDRSZ); \
    6235              : } while (0)
    6236              : 
    6237              : /*
    6238              :  * NUMERIC to_number() (convert string to numeric)
    6239              :  */
    6240              : Datum
    6241        12138 : numeric_to_number(PG_FUNCTION_ARGS)
    6242              : {
    6243        12138 :     text       *value = PG_GETARG_TEXT_PP(0);
    6244        12138 :     text       *fmt = PG_GETARG_TEXT_PP(1);
    6245              :     NUMDesc     Num;
    6246              :     Datum       result;
    6247              :     FormatNode *format;
    6248              :     char       *numstr;
    6249              :     bool        shouldFree;
    6250        12138 :     int         len = 0;
    6251              :     int         scale,
    6252              :                 precision;
    6253              : 
    6254        12138 :     len = VARSIZE_ANY_EXHDR(fmt);
    6255              : 
    6256        12138 :     if (len <= 0 || len >= INT_MAX / NUM_MAX_ITEM_SIZ)
    6257            0 :         PG_RETURN_NULL();
    6258              : 
    6259        12138 :     format = NUM_cache(len, &Num, fmt, &shouldFree);
    6260              : 
    6261        12132 :     numstr = (char *) palloc((len * NUM_MAX_ITEM_SIZ) + 1);
    6262              : 
    6263        12132 :     NUM_processor(format, &Num, VARDATA_ANY(value), numstr,
    6264              :                   VARSIZE_ANY_EXHDR(value), 0, 0, false, PG_GET_COLLATION());
    6265              : 
    6266        12090 :     scale = Num.post;
    6267        12090 :     precision = Num.pre + Num.multi + scale;
    6268              : 
    6269        12090 :     if (shouldFree)
    6270            0 :         pfree(format);
    6271              : 
    6272        12090 :     result = DirectFunctionCall3(numeric_in,
    6273              :                                  CStringGetDatum(numstr),
    6274              :                                  ObjectIdGetDatum(InvalidOid),
    6275              :                                  Int32GetDatum(((precision << 16) | scale) + VARHDRSZ));
    6276              : 
    6277        12087 :     if (IS_MULTI(&Num))
    6278              :     {
    6279              :         Numeric     x;
    6280            3 :         Numeric     a = int64_to_numeric(10);
    6281            3 :         Numeric     b = int64_to_numeric(-Num.multi);
    6282              : 
    6283            3 :         x = DatumGetNumeric(DirectFunctionCall2(numeric_power,
    6284              :                                                 NumericGetDatum(a),
    6285              :                                                 NumericGetDatum(b)));
    6286            3 :         result = DirectFunctionCall2(numeric_mul,
    6287              :                                      result,
    6288              :                                      NumericGetDatum(x));
    6289              :     }
    6290              : 
    6291        12087 :     pfree(numstr);
    6292        12087 :     return result;
    6293              : }
    6294              : 
    6295              : /*
    6296              :  * NUMERIC to_char()
    6297              :  */
    6298              : Datum
    6299          901 : numeric_to_char(PG_FUNCTION_ARGS)
    6300              : {
    6301          901 :     Numeric     value = PG_GETARG_NUMERIC(0);
    6302          901 :     text       *fmt = PG_GETARG_TEXT_PP(1);
    6303              :     NUMDesc     Num;
    6304              :     FormatNode *format;
    6305              :     text       *result;
    6306              :     bool        shouldFree;
    6307          901 :     int         out_pre_spaces = 0,
    6308          901 :                 sign = 0;
    6309              :     char       *numstr,
    6310              :                *orgnum,
    6311              :                *p;
    6312              : 
    6313          901 :     NUM_TOCHAR_prepare;
    6314              : 
    6315              :     /*
    6316              :      * On DateType depend part (numeric)
    6317              :      */
    6318          901 :     if (IS_ROMAN(&Num))
    6319              :     {
    6320              :         int32       intvalue;
    6321           39 :         ErrorSaveContext escontext = {T_ErrorSaveContext};
    6322              : 
    6323              :         /* Round and convert to int */
    6324           39 :         intvalue = numeric_int4_safe(value, (Node *) &escontext);
    6325              :         /* On overflow, just use PG_INT32_MAX; int_to_roman will cope */
    6326           39 :         if (escontext.error_occurred)
    6327            3 :             intvalue = PG_INT32_MAX;
    6328           39 :         numstr = int_to_roman(intvalue);
    6329              :     }
    6330          862 :     else if (IS_EEEE(&Num))
    6331              :     {
    6332          117 :         orgnum = numeric_out_sci(value, Num.post);
    6333              : 
    6334              :         /*
    6335              :          * numeric_out_sci() does not emit a sign for positive numbers.  We
    6336              :          * need to add a space in this case so that positive and negative
    6337              :          * numbers are aligned.  Also must check for NaN/infinity cases, which
    6338              :          * we handle the same way as in float8_to_char.
    6339              :          */
    6340          117 :         if (strcmp(orgnum, "NaN") == 0 ||
    6341          114 :             strcmp(orgnum, "Infinity") == 0 ||
    6342          111 :             strcmp(orgnum, "-Infinity") == 0)
    6343              :         {
    6344              :             /*
    6345              :              * Allow 6 characters for the leading sign, the decimal point,
    6346              :              * "e", the exponent's sign and two exponent digits.
    6347              :              */
    6348            9 :             numstr = (char *) palloc(Num.pre + Num.post + 7);
    6349            9 :             fill_str(numstr, '#', Num.pre + Num.post + 6);
    6350            9 :             *numstr = ' ';
    6351            9 :             *(numstr + Num.pre + 1) = '.';
    6352              :         }
    6353          108 :         else if (*orgnum != '-')
    6354              :         {
    6355           96 :             numstr = (char *) palloc(strlen(orgnum) + 2);
    6356           96 :             *numstr = ' ';
    6357           96 :             strcpy(numstr + 1, orgnum);
    6358              :         }
    6359              :         else
    6360              :         {
    6361           12 :             numstr = orgnum;
    6362              :         }
    6363              :     }
    6364              :     else
    6365              :     {
    6366              :         size_t      numstr_pre_len;
    6367          745 :         Numeric     val = value;
    6368              :         Numeric     x;
    6369              : 
    6370          745 :         if (IS_MULTI(&Num))
    6371              :         {
    6372            3 :             Numeric     a = int64_to_numeric(10);
    6373            3 :             Numeric     b = int64_to_numeric(Num.multi);
    6374              : 
    6375            3 :             x = DatumGetNumeric(DirectFunctionCall2(numeric_power,
    6376              :                                                     NumericGetDatum(a),
    6377              :                                                     NumericGetDatum(b)));
    6378            3 :             val = DatumGetNumeric(DirectFunctionCall2(numeric_mul,
    6379              :                                                       NumericGetDatum(value),
    6380              :                                                       NumericGetDatum(x)));
    6381            3 :             Num.pre += Num.multi;
    6382              :         }
    6383              : 
    6384          745 :         x = DatumGetNumeric(DirectFunctionCall2(numeric_round,
    6385              :                                                 NumericGetDatum(val),
    6386              :                                                 Int32GetDatum(Num.post)));
    6387          745 :         orgnum = DatumGetCString(DirectFunctionCall1(numeric_out,
    6388              :                                                      NumericGetDatum(x)));
    6389              : 
    6390          745 :         if (*orgnum == '-')
    6391              :         {
    6392          211 :             sign = '-';
    6393          211 :             numstr = orgnum + 1;
    6394              :         }
    6395              :         else
    6396              :         {
    6397          534 :             sign = '+';
    6398          534 :             numstr = orgnum;
    6399              :         }
    6400              : 
    6401          745 :         if ((p = strchr(numstr, '.')))
    6402          598 :             numstr_pre_len = p - numstr;
    6403              :         else
    6404          147 :             numstr_pre_len = strlen(numstr);
    6405              : 
    6406              :         /* needs padding? */
    6407          745 :         if (numstr_pre_len < Num.pre)
    6408          690 :             out_pre_spaces = Num.pre - numstr_pre_len;
    6409              :         /* overflowed prefix digit format? */
    6410           55 :         else if (numstr_pre_len > Num.pre)
    6411              :         {
    6412           15 :             numstr = (char *) palloc(Num.pre + Num.post + 2);
    6413           15 :             fill_str(numstr, '#', Num.pre + Num.post + 1);
    6414           15 :             *(numstr + Num.pre) = '.';
    6415              :         }
    6416              :     }
    6417              : 
    6418          901 :     NUM_TOCHAR_finish;
    6419          901 :     PG_RETURN_TEXT_P(result);
    6420              : }
    6421              : 
    6422              : /*
    6423              :  * INT4 to_char()
    6424              :  */
    6425              : Datum
    6426       514588 : int4_to_char(PG_FUNCTION_ARGS)
    6427              : {
    6428       514588 :     int32       value = PG_GETARG_INT32(0);
    6429       514588 :     text       *fmt = PG_GETARG_TEXT_PP(1);
    6430              :     NUMDesc     Num;
    6431              :     FormatNode *format;
    6432              :     text       *result;
    6433              :     bool        shouldFree;
    6434       514588 :     int         out_pre_spaces = 0,
    6435       514588 :                 sign = 0;
    6436              :     char       *numstr,
    6437              :                *orgnum;
    6438              : 
    6439       514588 :     NUM_TOCHAR_prepare;
    6440              : 
    6441              :     /*
    6442              :      * On DateType depend part (int32)
    6443              :      */
    6444       514588 :     if (IS_ROMAN(&Num))
    6445        11997 :         numstr = int_to_roman(value);
    6446       502591 :     else if (IS_EEEE(&Num))
    6447              :     {
    6448              :         /* we can do it easily because float8 won't lose any precision */
    6449            3 :         float8      val = (float8) value;
    6450              : 
    6451            3 :         orgnum = (char *) psprintf("%+.*e", Num.post, val);
    6452              : 
    6453              :         /*
    6454              :          * Swap a leading positive sign for a space.
    6455              :          */
    6456            3 :         if (*orgnum == '+')
    6457            3 :             *orgnum = ' ';
    6458              : 
    6459            3 :         numstr = orgnum;
    6460              :     }
    6461              :     else
    6462              :     {
    6463              :         size_t      numstr_pre_len;
    6464              : 
    6465       502588 :         if (IS_MULTI(&Num))
    6466              :         {
    6467            3 :             orgnum = DatumGetCString(DirectFunctionCall1(int4out,
    6468              :                                                          Int32GetDatum(value * ((int32) pow((double) 10, (double) Num.multi)))));
    6469            3 :             Num.pre += Num.multi;
    6470              :         }
    6471              :         else
    6472              :         {
    6473       502585 :             orgnum = DatumGetCString(DirectFunctionCall1(int4out,
    6474              :                                                          Int32GetDatum(value)));
    6475              :         }
    6476              : 
    6477       502588 :         if (*orgnum == '-')
    6478              :         {
    6479            0 :             sign = '-';
    6480            0 :             orgnum++;
    6481              :         }
    6482              :         else
    6483       502588 :             sign = '+';
    6484              : 
    6485       502588 :         numstr_pre_len = strlen(orgnum);
    6486              : 
    6487              :         /* post-decimal digits?  Pad out with zeros. */
    6488       502588 :         if (Num.post)
    6489              :         {
    6490            0 :             numstr = (char *) palloc(numstr_pre_len + Num.post + 2);
    6491            0 :             strcpy(numstr, orgnum);
    6492            0 :             *(numstr + numstr_pre_len) = '.';
    6493            0 :             memset(numstr + numstr_pre_len + 1, '0', Num.post);
    6494            0 :             *(numstr + numstr_pre_len + Num.post + 1) = '\0';
    6495              :         }
    6496              :         else
    6497       502588 :             numstr = orgnum;
    6498              : 
    6499              :         /* needs padding? */
    6500       502588 :         if (numstr_pre_len < Num.pre)
    6501       495436 :             out_pre_spaces = Num.pre - numstr_pre_len;
    6502              :         /* overflowed prefix digit format? */
    6503         7152 :         else if (numstr_pre_len > Num.pre)
    6504              :         {
    6505            0 :             numstr = (char *) palloc(Num.pre + Num.post + 2);
    6506            0 :             fill_str(numstr, '#', Num.pre + Num.post + 1);
    6507            0 :             *(numstr + Num.pre) = '.';
    6508              :         }
    6509              :     }
    6510              : 
    6511       514588 :     NUM_TOCHAR_finish;
    6512       514588 :     PG_RETURN_TEXT_P(result);
    6513              : }
    6514              : 
    6515              : /*
    6516              :  * INT8 to_char()
    6517              :  */
    6518              : Datum
    6519          355 : int8_to_char(PG_FUNCTION_ARGS)
    6520              : {
    6521          355 :     int64       value = PG_GETARG_INT64(0);
    6522          355 :     text       *fmt = PG_GETARG_TEXT_PP(1);
    6523              :     NUMDesc     Num;
    6524              :     FormatNode *format;
    6525              :     text       *result;
    6526              :     bool        shouldFree;
    6527          355 :     int         out_pre_spaces = 0,
    6528          355 :                 sign = 0;
    6529              :     char       *numstr,
    6530              :                *orgnum;
    6531              : 
    6532          355 :     NUM_TOCHAR_prepare;
    6533              : 
    6534              :     /*
    6535              :      * On DateType depend part (int64)
    6536              :      */
    6537          355 :     if (IS_ROMAN(&Num))
    6538              :     {
    6539              :         int32       intvalue;
    6540              : 
    6541              :         /* On overflow, just use PG_INT32_MAX; int_to_roman will cope */
    6542           15 :         if (value <= PG_INT32_MAX && value >= PG_INT32_MIN)
    6543            6 :             intvalue = (int32) value;
    6544              :         else
    6545            9 :             intvalue = PG_INT32_MAX;
    6546           15 :         numstr = int_to_roman(intvalue);
    6547              :     }
    6548          340 :     else if (IS_EEEE(&Num))
    6549              :     {
    6550              :         /* to avoid loss of precision, must go via numeric not float8 */
    6551            6 :         orgnum = numeric_out_sci(int64_to_numeric(value),
    6552              :                                  Num.post);
    6553              : 
    6554              :         /*
    6555              :          * numeric_out_sci() does not emit a sign for positive numbers.  We
    6556              :          * need to add a space in this case so that positive and negative
    6557              :          * numbers are aligned.  We don't have to worry about NaN/inf here.
    6558              :          */
    6559            6 :         if (*orgnum != '-')
    6560              :         {
    6561            3 :             numstr = (char *) palloc(strlen(orgnum) + 2);
    6562            3 :             *numstr = ' ';
    6563            3 :             strcpy(numstr + 1, orgnum);
    6564              :         }
    6565              :         else
    6566              :         {
    6567            3 :             numstr = orgnum;
    6568              :         }
    6569              :     }
    6570              :     else
    6571              :     {
    6572              :         size_t      numstr_pre_len;
    6573              : 
    6574          334 :         if (IS_MULTI(&Num))
    6575              :         {
    6576            3 :             double      multi = pow((double) 10, (double) Num.multi);
    6577              : 
    6578            3 :             value = DatumGetInt64(DirectFunctionCall2(int8mul,
    6579              :                                                       Int64GetDatum(value),
    6580              :                                                       DirectFunctionCall1(dtoi8,
    6581              :                                                                           Float8GetDatum(multi))));
    6582            3 :             Num.pre += Num.multi;
    6583              :         }
    6584              : 
    6585          334 :         orgnum = DatumGetCString(DirectFunctionCall1(int8out,
    6586              :                                                      Int64GetDatum(value)));
    6587              : 
    6588          334 :         if (*orgnum == '-')
    6589              :         {
    6590          102 :             sign = '-';
    6591          102 :             orgnum++;
    6592              :         }
    6593              :         else
    6594          232 :             sign = '+';
    6595              : 
    6596          334 :         numstr_pre_len = strlen(orgnum);
    6597              : 
    6598              :         /* post-decimal digits?  Pad out with zeros. */
    6599          334 :         if (Num.post)
    6600              :         {
    6601          105 :             numstr = (char *) palloc(numstr_pre_len + Num.post + 2);
    6602          105 :             strcpy(numstr, orgnum);
    6603          105 :             *(numstr + numstr_pre_len) = '.';
    6604          105 :             memset(numstr + numstr_pre_len + 1, '0', Num.post);
    6605          105 :             *(numstr + numstr_pre_len + Num.post + 1) = '\0';
    6606              :         }
    6607              :         else
    6608          229 :             numstr = orgnum;
    6609              : 
    6610              :         /* needs padding? */
    6611          334 :         if (numstr_pre_len < Num.pre)
    6612          135 :             out_pre_spaces = Num.pre - numstr_pre_len;
    6613              :         /* overflowed prefix digit format? */
    6614          199 :         else if (numstr_pre_len > Num.pre)
    6615              :         {
    6616            0 :             numstr = (char *) palloc(Num.pre + Num.post + 2);
    6617            0 :             fill_str(numstr, '#', Num.pre + Num.post + 1);
    6618            0 :             *(numstr + Num.pre) = '.';
    6619              :         }
    6620              :     }
    6621              : 
    6622          355 :     NUM_TOCHAR_finish;
    6623          355 :     PG_RETURN_TEXT_P(result);
    6624              : }
    6625              : 
    6626              : /*
    6627              :  * FLOAT4 to_char()
    6628              :  */
    6629              : Datum
    6630           74 : float4_to_char(PG_FUNCTION_ARGS)
    6631              : {
    6632           74 :     float4      value = PG_GETARG_FLOAT4(0);
    6633           74 :     text       *fmt = PG_GETARG_TEXT_PP(1);
    6634              :     NUMDesc     Num;
    6635              :     FormatNode *format;
    6636              :     text       *result;
    6637              :     bool        shouldFree;
    6638           74 :     int         out_pre_spaces = 0,
    6639           74 :                 sign = 0;
    6640              :     char       *numstr,
    6641              :                *p;
    6642              : 
    6643           74 :     NUM_TOCHAR_prepare;
    6644              : 
    6645           74 :     if (IS_ROMAN(&Num))
    6646              :     {
    6647              :         int32       intvalue;
    6648              : 
    6649              :         /* See notes in ftoi4() */
    6650            6 :         value = rint(value);
    6651              :         /* On overflow, just use PG_INT32_MAX; int_to_roman will cope */
    6652            6 :         if (!isnan(value) && FLOAT4_FITS_IN_INT32(value))
    6653            3 :             intvalue = (int32) value;
    6654              :         else
    6655            3 :             intvalue = PG_INT32_MAX;
    6656            6 :         numstr = int_to_roman(intvalue);
    6657              :     }
    6658           68 :     else if (IS_EEEE(&Num))
    6659              :     {
    6660           21 :         if (isnan(value) || isinf(value))
    6661              :         {
    6662              :             /*
    6663              :              * Allow 6 characters for the leading sign, the decimal point,
    6664              :              * "e", the exponent's sign and two exponent digits.
    6665              :              */
    6666            9 :             numstr = (char *) palloc(Num.pre + Num.post + 7);
    6667            9 :             fill_str(numstr, '#', Num.pre + Num.post + 6);
    6668            9 :             *numstr = ' ';
    6669            9 :             *(numstr + Num.pre + 1) = '.';
    6670              :         }
    6671              :         else
    6672              :         {
    6673           12 :             numstr = psprintf("%+.*e", Num.post, value);
    6674              : 
    6675              :             /*
    6676              :              * Swap a leading positive sign for a space.
    6677              :              */
    6678           12 :             if (*numstr == '+')
    6679            9 :                 *numstr = ' ';
    6680              :         }
    6681              :     }
    6682              :     else
    6683              :     {
    6684           47 :         float4      val = value;
    6685              :         char       *orgnum;
    6686              :         size_t      numstr_pre_len;
    6687              : 
    6688           47 :         if (IS_MULTI(&Num))
    6689              :         {
    6690            3 :             float       multi = pow((double) 10, (double) Num.multi);
    6691              : 
    6692            3 :             val = value * multi;
    6693            3 :             Num.pre += Num.multi;
    6694              :         }
    6695              : 
    6696           47 :         orgnum = psprintf("%.0f", fabs(val));
    6697           47 :         numstr_pre_len = strlen(orgnum);
    6698              : 
    6699              :         /* adjust post digits to fit max float digits */
    6700           47 :         if (numstr_pre_len >= FLT_DIG)
    6701           21 :             Num.post = 0;
    6702           26 :         else if (numstr_pre_len + Num.post > FLT_DIG)
    6703            0 :             Num.post = FLT_DIG - numstr_pre_len;
    6704           47 :         orgnum = psprintf("%.*f", Num.post, val);
    6705              : 
    6706           47 :         if (*orgnum == '-')
    6707              :         {                       /* < 0 */
    6708           12 :             sign = '-';
    6709           12 :             numstr = orgnum + 1;
    6710              :         }
    6711              :         else
    6712              :         {
    6713           35 :             sign = '+';
    6714           35 :             numstr = orgnum;
    6715              :         }
    6716              : 
    6717           47 :         if ((p = strchr(numstr, '.')))
    6718           20 :             numstr_pre_len = p - numstr;
    6719              :         else
    6720           27 :             numstr_pre_len = strlen(numstr);
    6721              : 
    6722              :         /* needs padding? */
    6723           47 :         if (numstr_pre_len < Num.pre)
    6724           30 :             out_pre_spaces = Num.pre - numstr_pre_len;
    6725              :         /* overflowed prefix digit format? */
    6726           17 :         else if (numstr_pre_len > Num.pre)
    6727              :         {
    6728           12 :             numstr = (char *) palloc(Num.pre + Num.post + 2);
    6729           12 :             fill_str(numstr, '#', Num.pre + Num.post + 1);
    6730           12 :             *(numstr + Num.pre) = '.';
    6731              :         }
    6732              :     }
    6733              : 
    6734           74 :     NUM_TOCHAR_finish;
    6735           74 :     PG_RETURN_TEXT_P(result);
    6736              : }
    6737              : 
    6738              : /*
    6739              :  * FLOAT8 to_char()
    6740              :  */
    6741              : Datum
    6742           85 : float8_to_char(PG_FUNCTION_ARGS)
    6743              : {
    6744           85 :     float8      value = PG_GETARG_FLOAT8(0);
    6745           85 :     text       *fmt = PG_GETARG_TEXT_PP(1);
    6746              :     NUMDesc     Num;
    6747              :     FormatNode *format;
    6748              :     text       *result;
    6749              :     bool        shouldFree;
    6750           85 :     int         out_pre_spaces = 0,
    6751           85 :                 sign = 0;
    6752              :     char       *numstr,
    6753              :                *p;
    6754              : 
    6755           85 :     NUM_TOCHAR_prepare;
    6756              : 
    6757           85 :     if (IS_ROMAN(&Num))
    6758              :     {
    6759              :         int32       intvalue;
    6760              : 
    6761              :         /* See notes in dtoi4() */
    6762            9 :         value = rint(value);
    6763              :         /* On overflow, just use PG_INT32_MAX; int_to_roman will cope */
    6764            9 :         if (!isnan(value) && FLOAT8_FITS_IN_INT32(value))
    6765            6 :             intvalue = (int32) value;
    6766              :         else
    6767            3 :             intvalue = PG_INT32_MAX;
    6768            9 :         numstr = int_to_roman(intvalue);
    6769              :     }
    6770           76 :     else if (IS_EEEE(&Num))
    6771              :     {
    6772           21 :         if (isnan(value) || isinf(value))
    6773              :         {
    6774              :             /*
    6775              :              * Allow 6 characters for the leading sign, the decimal point,
    6776              :              * "e", the exponent's sign and two exponent digits.
    6777              :              */
    6778            9 :             numstr = (char *) palloc(Num.pre + Num.post + 7);
    6779            9 :             fill_str(numstr, '#', Num.pre + Num.post + 6);
    6780            9 :             *numstr = ' ';
    6781            9 :             *(numstr + Num.pre + 1) = '.';
    6782              :         }
    6783              :         else
    6784              :         {
    6785           12 :             numstr = psprintf("%+.*e", Num.post, value);
    6786              : 
    6787              :             /*
    6788              :              * Swap a leading positive sign for a space.
    6789              :              */
    6790           12 :             if (*numstr == '+')
    6791            9 :                 *numstr = ' ';
    6792              :         }
    6793              :     }
    6794              :     else
    6795              :     {
    6796           55 :         float8      val = value;
    6797              :         char       *orgnum;
    6798              :         size_t      numstr_pre_len;
    6799              : 
    6800           55 :         if (IS_MULTI(&Num))
    6801              :         {
    6802            3 :             double      multi = pow((double) 10, (double) Num.multi);
    6803              : 
    6804            3 :             val = value * multi;
    6805            3 :             Num.pre += Num.multi;
    6806              :         }
    6807              : 
    6808           55 :         orgnum = psprintf("%.0f", fabs(val));
    6809           55 :         numstr_pre_len = strlen(orgnum);
    6810              : 
    6811              :         /* adjust post digits to fit max double digits */
    6812           55 :         if (numstr_pre_len >= DBL_DIG)
    6813            3 :             Num.post = 0;
    6814           52 :         else if (numstr_pre_len + Num.post > DBL_DIG)
    6815            3 :             Num.post = DBL_DIG - numstr_pre_len;
    6816           55 :         orgnum = psprintf("%.*f", Num.post, val);
    6817              : 
    6818           55 :         if (*orgnum == '-')
    6819              :         {                       /* < 0 */
    6820           12 :             sign = '-';
    6821           12 :             numstr = orgnum + 1;
    6822              :         }
    6823              :         else
    6824              :         {
    6825           43 :             sign = '+';
    6826           43 :             numstr = orgnum;
    6827              :         }
    6828              : 
    6829           55 :         if ((p = strchr(numstr, '.')))
    6830           31 :             numstr_pre_len = p - numstr;
    6831              :         else
    6832           24 :             numstr_pre_len = strlen(numstr);
    6833              : 
    6834              :         /* needs padding? */
    6835           55 :         if (numstr_pre_len < Num.pre)
    6836           33 :             out_pre_spaces = Num.pre - numstr_pre_len;
    6837              :         /* overflowed prefix digit format? */
    6838           22 :         else if (numstr_pre_len > Num.pre)
    6839              :         {
    6840           15 :             numstr = (char *) palloc(Num.pre + Num.post + 2);
    6841           15 :             fill_str(numstr, '#', Num.pre + Num.post + 1);
    6842           15 :             *(numstr + Num.pre) = '.';
    6843              :         }
    6844              :     }
    6845              : 
    6846           85 :     NUM_TOCHAR_finish;
    6847           85 :     PG_RETURN_TEXT_P(result);
    6848              : }
        

Generated by: LCOV version 2.0-1