diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index a588350..96ed2c3 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -6157,9 +6157,12 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}'); to_timestamp and to_date - skip multiple blank spaces in the input string unless the + skip multiple blank spaces and printable non letter and non digit + characters in the input string and in the formatting string unless the FX option is used. For example, - to_timestamp('2000    JUN', 'YYYY MON') works, but + to_timestamp('2000    JUN', 'YYYY MON'), + to_timestamp('2000----JUN', 'YYYY MON') + and to_timestamp('2000 JUN', 'YYYY    MON') work, but to_timestamp('2000    JUN', 'FXYYYY MON') returns an error because to_timestamp expects one space only. FX must be specified as the first item in diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index d4eaa50..7986228 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -169,6 +169,8 @@ struct FormatNode #define NODE_TYPE_END 1 #define NODE_TYPE_ACTION 2 #define NODE_TYPE_CHAR 3 +#define NODE_TYPE_SEPARATOR 4 +#define NODE_TYPE_SPACE 5 #define SUFFTYPE_PREFIX 1 #define SUFFTYPE_POSTFIX 2 @@ -951,6 +953,7 @@ typedef struct NUMProc static const KeyWord *index_seq_search(const char *str, const KeyWord *kw, const int *index); static const KeySuffix *suff_search(const char *str, const KeySuffix *suf, int type); +static bool is_char_separator(const char *str); static void NUMDesc_prepare(NUMDesc *num, FormatNode *n); static void parse_format(FormatNode *node, const char *str, const KeyWord *kw, const KeySuffix *suf, const int *index, int ver, NUMDesc *Num); @@ -967,7 +970,6 @@ static void dump_node(FormatNode *node, int max); static const char *get_th(char *num, int type); static char *str_numth(char *dest, char *num, int type); static int adjust_partial_year_to_2020(int year); -static int strspace_len(char *str); static void from_char_set_mode(TmFromChar *tmfc, const FromCharDateMode mode); static void from_char_set_int(int *dest, const int value, const FormatNode *node); static int from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node); @@ -1040,6 +1042,17 @@ suff_search(const char *str, const KeySuffix *suf, int type) return NULL; } +static bool +is_char_separator(const char *str) +{ + return ((pg_mblen(str) == 1) && + /* printable character, but not letter and digit */ + ((*str >= '!' && *str <= '/') || + (*str >= ':' && *str <= '@') || + (*str >= '[' && *str <= '`') || + (*str >= '{' && *str <= '~'))); +} + /* ---------- * Prepare NUMDesc (number description struct) via FormatNode struct * ---------- @@ -1225,9 +1238,10 @@ parse_format(FormatNode *node, const char *str, const KeyWord *kw, { const KeySuffix *s; FormatNode *n; + bool in_text = false, + in_backslash = false; int node_set = 0, - suffix, - last = 0; + suffix; #ifdef DEBUG_TO_FROM_CHAR elog(DEBUG_elog_output, "to_char/number(): run parser"); @@ -1239,6 +1253,55 @@ parse_format(FormatNode *node, const char *str, const KeyWord *kw, { suffix = 0; + /* Previous character was a backslash */ + if (in_backslash) + { + /* After backslash should go non-space character */ + if (isspace(*str)) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("invalid escape sequence"))); + in_backslash = false; + + n->type = NODE_TYPE_CHAR; + n->character = *str; + n->key = NULL; + n->suffix = 0; + n++; + str++; + continue; + } + /* Previous character was a quote */ + else if (in_text) + { + if (*str == '"') + { + str++; + in_text = false; + } + else if (*str == '\\') + { + str++; + in_backslash = true; + } + else + { + if (ver == DCH_TYPE && is_char_separator(str)) + n->type = NODE_TYPE_SEPARATOR; + else if (isspace(*str)) + n->type = NODE_TYPE_SPACE; + else + n->type = NODE_TYPE_CHAR; + + n->character = *str; + n->key = NULL; + n->suffix = 0; + n++; + str++; + } + continue; + } + /* * Prefix */ @@ -1278,48 +1341,30 @@ parse_format(FormatNode *node, const char *str, const KeyWord *kw, } else if (*str) { - /* - * Special characters '\' and '"' - */ - if (*str == '"' && last != '\\') + if (*str == '"') { - int x = 0; - - while (*(++str)) - { - if (*str == '"' && x != '\\') - { - str++; - break; - } - else if (*str == '\\' && x != '\\') - { - x = '\\'; - continue; - } - n->type = NODE_TYPE_CHAR; - n->character = *str; - n->key = NULL; - n->suffix = 0; - ++n; - x = *str; - } + in_text = true; node_set = 0; - suffix = 0; - last = 0; + str++; } - else if (*str && *str == '\\' && last != '\\' && *(str + 1) == '"') + else if (*str == '\\') { - last = *str; + in_backslash = true; + node_set = 0; str++; } - else if (*str) + else { - n->type = NODE_TYPE_CHAR; + if (ver == DCH_TYPE && is_char_separator(str)) + n->type = NODE_TYPE_SEPARATOR; + else if (isspace(*str)) + n->type = NODE_TYPE_SPACE; + else + n->type = NODE_TYPE_CHAR; + n->character = *str; n->key = NULL; node_set = 1; - last = 0; str++; } } @@ -1336,6 +1381,17 @@ parse_format(FormatNode *node, const char *str, const KeyWord *kw, } } + if (in_backslash) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("invalid escape sequence"))); + + /* If we didn't meet closing quotes */ + if (in_text) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("unexpected end of format string, expected '\"' character"))); + n->type = NODE_TYPE_END; n->suffix = 0; } @@ -2068,20 +2124,6 @@ adjust_partial_year_to_2020(int year) return year; } - -static int -strspace_len(char *str) -{ - int len = 0; - - while (*str && isspace((unsigned char) *str)) - { - str++; - len++; - } - return len; -} - /* * Set the date mode of a from-char conversion. * @@ -2151,11 +2193,6 @@ from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node) char *init = *src; int used; - /* - * Skip any whitespace before parsing the integer. - */ - *src += strspace_len(*src); - Assert(len <= DCH_MAX_ITEM_SIZ); used = (int) strlcpy(copy, *src, len + 1); @@ -2934,19 +2971,56 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out) FormatNode *n; char *s; int len, - value; + value, + prev_type = NODE_TYPE_SPACE; bool fx_mode = false; for (n = node, s = in; n->type != NODE_TYPE_END && *s != '\0'; n++) { - if (n->type != NODE_TYPE_ACTION) + if (n->type == NODE_TYPE_SPACE || n->type == NODE_TYPE_SEPARATOR) { /* - * Separator, so consume one character from input string. Notice - * we don't insist that the consumed character match the format's - * character. + * In non FX (fixed format) mode we skip spaces and separator + * characters. */ + if (!fx_mode) + { + if (isspace(*s) || is_char_separator(s)) + s++; + prev_type = n->type; + continue; + } + + /* Checks for FX mode */ + if (n->type == NODE_TYPE_SPACE && !isspace(*s)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_DATETIME_FORMAT), + errmsg("unexpected character \"%c\", expected \"%c\"", + *s, n->character), + errdetail("The given value did not match any of the allowed " + "values for this field."))); + else if (n->type == NODE_TYPE_SEPARATOR && n->character != *s) + ereport(ERROR, + (errcode(ERRCODE_INVALID_DATETIME_FORMAT), + errmsg("unexpected character \"%c\", expected \"%c\"", + *s, n->character), + errdetail("The given value did not match any of the allowed " + "values for this field."))); + s++; + prev_type = n->type; + continue; + } + else if (n->type == NODE_TYPE_CHAR) + { + /* + * Text character, so consume one character from input string. + * Notice we don't insist that the consumed character match the + * format's character. + * Text field ignores FX mode. + */ + s++; + prev_type = n->type; continue; } @@ -2955,7 +3029,20 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out) { while (*s != '\0' && isspace((unsigned char) *s)) s++; + /* + * Ignore separator characters if previous node was space or + * separator and current is not. + */ + if (prev_type == NODE_TYPE_SPACE || prev_type == NODE_TYPE_SEPARATOR) + while (*s != '\0' && is_char_separator(s)) + s++; } + else if (isspace(*s) || is_char_separator(s)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_DATETIME_FORMAT), + errmsg("unexpected character \"%c\"", *s), + errdetail("The given value did not match any of the allowed " + "values for this field."))); from_char_set_mode(out, n->key->date_mode); @@ -3194,6 +3281,7 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out) SKIP_THth(s, n->suffix); break; } + prev_type = n->type; } } diff --git a/src/test/regress/expected/horology.out b/src/test/regress/expected/horology.out index f9d12e0..8c71c72 100644 --- a/src/test/regress/expected/horology.out +++ b/src/test/regress/expected/horology.out @@ -2769,14 +2769,32 @@ SELECT to_timestamp('97/2/16 8:14:30', 'FMYYYY/FMMM/FMDD FMHH:FMMI:FMSS'); Sat Feb 16 08:14:30 0097 PST (1 row) +SELECT TO_TIMESTAMP('2011$03!18 23_38_15', 'YYYY-MM-DD HH24:MI:SS'); + to_timestamp +------------------------------ + Fri Mar 18 23:38:15 2011 PDT +(1 row) + SELECT to_timestamp('1985 January 12', 'YYYY FMMonth DD'); to_timestamp ------------------------------ Sat Jan 12 00:00:00 1985 PST (1 row) +SELECT to_timestamp('1985 FMMonth 12', 'YYYY "FMMonth" DD'); + to_timestamp +------------------------------ + Sat Jan 12 00:00:00 1985 PST +(1 row) + +SELECT to_timestamp('1985 \ 12', 'YYYY \\ DD'); + to_timestamp +------------------------------ + Sat Jan 12 00:00:00 1985 PST +(1 row) + SELECT to_timestamp('My birthday-> Year: 1976, Month: May, Day: 16', - '"My birthday-> Year" YYYY, "Month:" FMMonth, "Day:" DD'); + '"My birthday-> Year:" YYYY, "Month:" FMMonth, "Day:" DD'); to_timestamp ------------------------------ Sun May 16 00:00:00 1976 PDT @@ -2789,12 +2807,18 @@ SELECT to_timestamp('1,582nd VIII 21', 'Y,YYYth FMRM DD'); (1 row) SELECT to_timestamp('15 "text between quote marks" 98 54 45', - E'HH24 "\\text between quote marks\\"" YY MI SS'); + E'HH24 "\\"text between quote marks\\"" YY MI SS'); to_timestamp ------------------------------ Thu Jan 01 15:54:45 1998 PST (1 row) +SELECT to_timestamp('Year: 1976, Month: May, Day: 16', '" Year:" YYYY, "Month:" FMMonth, "Day:" DD'); + to_timestamp +------------------------------ + Sun May 16 00:00:00 1976 PDT +(1 row) + SELECT to_timestamp('05121445482000', 'MMDDHH24MISSYYYY'); to_timestamp ------------------------------ @@ -2810,6 +2834,21 @@ SELECT to_timestamp('2000January09Sunday', 'YYYYFMMonthDDFMDay'); SELECT to_timestamp('97/Feb/16', 'YYMonDD'); ERROR: invalid value "/Fe" for "Mon" DETAIL: The given value did not match any of the allowed values for this field. +SELECT to_timestamp('97/Feb/16', 'YY:Mon:DD'); + to_timestamp +------------------------------ + Sun Feb 16 00:00:00 1997 PST +(1 row) + +SELECT to_timestamp('97/Feb/16', 'FXYY:Mon:DD'); +ERROR: unexpected character "/", expected ":" +DETAIL: The given value did not match any of the allowed values for this field. +SELECT to_timestamp('97/Feb/16', 'FXYY/Mon/DD'); + to_timestamp +------------------------------ + Sun Feb 16 00:00:00 1997 PST +(1 row) + SELECT to_timestamp('19971116', 'YYYYMMDD'); to_timestamp ------------------------------ @@ -2936,7 +2975,7 @@ SELECT to_timestamp('2011-12-18 11:38 PM', 'YYYY-MM-DD HH12:MI PM'); SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS'); to_timestamp ------------------------------ - Sun Dec 18 03:38:15 2011 PST + Sun Dec 18 23:38:15 2011 PST (1 row) SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS'); @@ -2966,7 +3005,13 @@ SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS'); SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS'); to_timestamp ------------------------------ - Sun Dec 18 03:38:15 2011 PST + Sun Dec 18 23:38:15 2011 PST +(1 row) + +SELECT to_timestamp('2000----JUN', 'YYYY MON'); + to_timestamp +------------------------------ + Thu Jun 01 00:00:00 2000 PDT (1 row) SELECT to_date('2011 12 18', 'YYYY MM DD'); @@ -2984,13 +3029,13 @@ SELECT to_date('2011 12 18', 'YYYY MM DD'); SELECT to_date('2011 12 18', 'YYYY MM DD'); to_date ------------ - 12-08-2011 + 12-18-2011 (1 row) SELECT to_date('2011 12 18', 'YYYY MM DD'); to_date ------------ - 02-18-2011 + 12-18-2011 (1 row) SELECT to_date('2011 12 18', 'YYYY MM DD'); diff --git a/src/test/regress/sql/horology.sql b/src/test/regress/sql/horology.sql index a7bc9dc..6984f26 100644 --- a/src/test/regress/sql/horology.sql +++ b/src/test/regress/sql/horology.sql @@ -392,15 +392,23 @@ SELECT to_timestamp('0097/Feb/16 --> 08:14:30', 'YYYY/Mon/DD --> HH:MI:SS'); SELECT to_timestamp('97/2/16 8:14:30', 'FMYYYY/FMMM/FMDD FMHH:FMMI:FMSS'); +SELECT TO_TIMESTAMP('2011$03!18 23_38_15', 'YYYY-MM-DD HH24:MI:SS'); + SELECT to_timestamp('1985 January 12', 'YYYY FMMonth DD'); +SELECT to_timestamp('1985 FMMonth 12', 'YYYY "FMMonth" DD'); + +SELECT to_timestamp('1985 \ 12', 'YYYY \\ DD'); + SELECT to_timestamp('My birthday-> Year: 1976, Month: May, Day: 16', - '"My birthday-> Year" YYYY, "Month:" FMMonth, "Day:" DD'); + '"My birthday-> Year:" YYYY, "Month:" FMMonth, "Day:" DD'); SELECT to_timestamp('1,582nd VIII 21', 'Y,YYYth FMRM DD'); SELECT to_timestamp('15 "text between quote marks" 98 54 45', - E'HH24 "\\text between quote marks\\"" YY MI SS'); + E'HH24 "\\"text between quote marks\\"" YY MI SS'); + +SELECT to_timestamp('Year: 1976, Month: May, Day: 16', '" Year:" YYYY, "Month:" FMMonth, "Day:" DD'); SELECT to_timestamp('05121445482000', 'MMDDHH24MISSYYYY'); @@ -408,6 +416,12 @@ SELECT to_timestamp('2000January09Sunday', 'YYYYFMMonthDDFMDay'); SELECT to_timestamp('97/Feb/16', 'YYMonDD'); +SELECT to_timestamp('97/Feb/16', 'YY:Mon:DD'); + +SELECT to_timestamp('97/Feb/16', 'FXYY:Mon:DD'); + +SELECT to_timestamp('97/Feb/16', 'FXYY/Mon/DD'); + SELECT to_timestamp('19971116', 'YYYYMMDD'); SELECT to_timestamp('20000-1116', 'YYYY-MMDD'); @@ -458,6 +472,8 @@ SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS'); SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS'); SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS'); +SELECT to_timestamp('2000----JUN', 'YYYY MON'); + SELECT to_date('2011 12 18', 'YYYY MM DD'); SELECT to_date('2011 12 18', 'YYYY MM DD'); SELECT to_date('2011 12 18', 'YYYY MM DD');