Also I removed some dead code from the previous patch. - Joe Koshakow
From 2ff08d729bca87992514d0651fdb62455e43cd8a Mon Sep 17 00:00:00 2001 From: Joseph Koshakow <kosh...@gmail.com> Date: Sat, 10 Dec 2022 18:59:26 -0500 Subject: [PATCH] Remove unknown ISO format, handle dandling units
This commit removes the date format of "y2001m02d04" and the time format of "h04mm05s06". These were never documented and don't seem to be valid ISO formats. Additionally this commit handles repeated and dangling julian units in DecodeDateTime. --- src/backend/utils/adt/datetime.c | 219 ++----------------------- src/test/regress/expected/horology.out | 41 ++--- src/test/regress/sql/horology.sql | 4 + 3 files changed, 36 insertions(+), 228 deletions(-) diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c index d166613895..bf7cb94b52 100644 --- a/src/backend/utils/adt/datetime.c +++ b/src/backend/utils/adt/datetime.c @@ -983,7 +983,7 @@ DecodeDateTime(char **field, int *ftype, int nf, int fmask = 0, tmask, type; - int ptype = 0; /* "prefix type" for ISO y2001m02d04 format */ + int ptype = 0; /* "prefix type" for ISO and Julian formats */ int i; int val; int dterr; @@ -1174,10 +1174,6 @@ DecodeDateTime(char **field, int *ftype, int nf, case DTK_NUMBER: - /* - * Was this an "ISO date" with embedded field labels? An - * example is "y2001m02d04" - thomas 2001-02-04 - */ if (ptype != 0) { char *cp; @@ -1188,84 +1184,11 @@ DecodeDateTime(char **field, int *ftype, int nf, if (errno == ERANGE) return DTERR_FIELD_OVERFLOW; - /* - * only a few kinds are allowed to have an embedded - * decimal - */ - if (*cp == '.') - switch (ptype) - { - case DTK_JULIAN: - case DTK_TIME: - case DTK_SECOND: - break; - default: - return DTERR_BAD_FORMAT; - break; - } - else if (*cp != '\0') + if (*cp != '.' && *cp != '\0') return DTERR_BAD_FORMAT; switch (ptype) { - case DTK_YEAR: - tm->tm_year = value; - tmask = DTK_M(YEAR); - break; - - case DTK_MONTH: - - /* - * already have a month and hour? then assume - * minutes - */ - if ((fmask & DTK_M(MONTH)) != 0 && - (fmask & DTK_M(HOUR)) != 0) - { - tm->tm_min = value; - tmask = DTK_M(MINUTE); - } - else - { - tm->tm_mon = value; - tmask = DTK_M(MONTH); - } - break; - - case DTK_DAY: - tm->tm_mday = value; - tmask = DTK_M(DAY); - break; - - case DTK_HOUR: - tm->tm_hour = value; - tmask = DTK_M(HOUR); - break; - - case DTK_MINUTE: - tm->tm_min = value; - tmask = DTK_M(MINUTE); - break; - - case DTK_SECOND: - tm->tm_sec = value; - tmask = DTK_M(SECOND); - if (*cp == '.') - { - dterr = ParseFractionalSecond(cp, fsec); - if (dterr) - return dterr; - tmask = DTK_ALL_SECS_M; - } - break; - - case DTK_TZ: - tmask = DTK_M(TZ); - dterr = DecodeTimezone(field[i], tzp); - if (dterr) - return dterr; - break; - case DTK_JULIAN: /* previous field was a label for "julian date" */ if (value < 0) @@ -1510,6 +1433,9 @@ DecodeDateTime(char **field, int *ftype, int nf, case UNITS: tmask = 0; + /* prevent consecutive unhandled units */ + if (ptype != 0) + return DTERR_BAD_FORMAT; ptype = val; break; @@ -1536,7 +1462,6 @@ DecodeDateTime(char **field, int *ftype, int nf, ftype[i + 1] != DTK_TIME && ftype[i + 1] != DTK_DATE)) return DTERR_BAD_FORMAT; - ptype = val; break; @@ -1567,6 +1492,10 @@ DecodeDateTime(char **field, int *ftype, int nf, fmask |= tmask; } /* end loop over fields */ + /* prefix type was dangling and never handled */ + if (ptype != 0) + return DTERR_BAD_FORMAT; + /* do final checking/adjustment of Y/M/D fields */ dterr = ValidateDate(fmask, isjulian, is2digits, bc, tm); if (dterr) @@ -1933,7 +1862,7 @@ DecodeTimeOnly(char **field, int *ftype, int nf, int fmask = 0, tmask, type; - int ptype = 0; /* "prefix type" for ISO h04mm05s06 format */ + int ptype = 0; /* "prefix type" for ISO format */ int i; int val; int dterr; @@ -2060,133 +1989,12 @@ DecodeTimeOnly(char **field, int *ftype, int nf, case DTK_NUMBER: /* - * Was this an "ISO time" with embedded field labels? An - * example is "h04mm05s06" - thomas 2001-02-04 + * Was this an "ISO time" An example is "T040506.789" */ if (ptype != 0) { - char *cp; - int value; - - /* Only accept a date under limited circumstances */ switch (ptype) { - case DTK_JULIAN: - case DTK_YEAR: - case DTK_MONTH: - case DTK_DAY: - if (tzp == NULL) - return DTERR_BAD_FORMAT; - default: - break; - } - - errno = 0; - value = strtoint(field[i], &cp, 10); - if (errno == ERANGE) - return DTERR_FIELD_OVERFLOW; - - /* - * only a few kinds are allowed to have an embedded - * decimal - */ - if (*cp == '.') - switch (ptype) - { - case DTK_JULIAN: - case DTK_TIME: - case DTK_SECOND: - break; - default: - return DTERR_BAD_FORMAT; - break; - } - else if (*cp != '\0') - return DTERR_BAD_FORMAT; - - switch (ptype) - { - case DTK_YEAR: - tm->tm_year = value; - tmask = DTK_M(YEAR); - break; - - case DTK_MONTH: - - /* - * already have a month and hour? then assume - * minutes - */ - if ((fmask & DTK_M(MONTH)) != 0 && - (fmask & DTK_M(HOUR)) != 0) - { - tm->tm_min = value; - tmask = DTK_M(MINUTE); - } - else - { - tm->tm_mon = value; - tmask = DTK_M(MONTH); - } - break; - - case DTK_DAY: - tm->tm_mday = value; - tmask = DTK_M(DAY); - break; - - case DTK_HOUR: - tm->tm_hour = value; - tmask = DTK_M(HOUR); - break; - - case DTK_MINUTE: - tm->tm_min = value; - tmask = DTK_M(MINUTE); - break; - - case DTK_SECOND: - tm->tm_sec = value; - tmask = DTK_M(SECOND); - if (*cp == '.') - { - dterr = ParseFractionalSecond(cp, fsec); - if (dterr) - return dterr; - tmask = DTK_ALL_SECS_M; - } - break; - - case DTK_TZ: - tmask = DTK_M(TZ); - dterr = DecodeTimezone(field[i], tzp); - if (dterr) - return dterr; - break; - - case DTK_JULIAN: - /* previous field was a label for "julian date" */ - if (value < 0) - return DTERR_FIELD_OVERFLOW; - tmask = DTK_DATE_M; - j2date(value, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); - isjulian = true; - - if (*cp == '.') - { - double time; - - dterr = ParseFraction(cp, &time); - if (dterr) - return dterr; - time *= USECS_PER_DAY; - dt2time(time, - &tm->tm_hour, &tm->tm_min, - &tm->tm_sec, fsec); - tmask |= DTK_TIME_M; - } - break; - case DTK_TIME: /* previous field was "t" for ISO time */ dterr = DecodeNumberField(strlen(field[i]), field[i], @@ -2366,11 +2174,6 @@ DecodeTimeOnly(char **field, int *ftype, int nf, bc = (val == BC); break; - case UNITS: - tmask = 0; - ptype = val; - break; - case ISOTIME: tmask = 0; diff --git a/src/test/regress/expected/horology.out b/src/test/regress/expected/horology.out index de73683690..10c5a6001a 100644 --- a/src/test/regress/expected/horology.out +++ b/src/test/regress/expected/horology.out @@ -98,29 +98,21 @@ SELECT timestamp with time zone '27/12/2001 04:05:06.789-08'; reset datestyle; SELECT timestamp with time zone 'Y2001M12D27H04M05S06.789+08'; - timestamptz ----------------------------------- - Wed Dec 26 12:05:06.789 2001 PST -(1 row) - +ERROR: invalid input syntax for type timestamp with time zone: "Y2001M12D27H04M05S06.789+08" +LINE 1: SELECT timestamp with time zone 'Y2001M12D27H04M05S06.789+08... + ^ SELECT timestamp with time zone 'Y2001M12D27H04M05S06.789-08'; - timestamptz ----------------------------------- - Thu Dec 27 04:05:06.789 2001 PST -(1 row) - +ERROR: invalid input syntax for type timestamp with time zone: "Y2001M12D27H04M05S06.789-08" +LINE 1: SELECT timestamp with time zone 'Y2001M12D27H04M05S06.789-08... + ^ SELECT timestamp with time zone 'Y2001M12D27H04MM05S06.789+08'; - timestamptz ----------------------------------- - Wed Dec 26 12:05:06.789 2001 PST -(1 row) - +ERROR: invalid input syntax for type timestamp with time zone: "Y2001M12D27H04MM05S06.789+08" +LINE 1: SELECT timestamp with time zone 'Y2001M12D27H04MM05S06.789+0... + ^ SELECT timestamp with time zone 'Y2001M12D27H04MM05S06.789-08'; - timestamptz ----------------------------------- - Thu Dec 27 04:05:06.789 2001 PST -(1 row) - +ERROR: invalid input syntax for type timestamp with time zone: "Y2001M12D27H04MM05S06.789-08" +LINE 1: SELECT timestamp with time zone 'Y2001M12D27H04MM05S06.789-0... + ^ SELECT timestamp with time zone 'J2452271+08'; timestamptz ------------------------------ @@ -3413,3 +3405,12 @@ SELECT to_char('2012-12-12 12:00'::timestamptz, 'YYYY-MM-DD SSSSS'); (1 row) RESET TIME ZONE; +-- test error on dangling julian units +SELECT date '1995-08-6 J J J '; +ERROR: invalid input syntax for type date: "1995-08-6 J J J " +LINE 1: SELECT date '1995-08-6 J J J '; + ^ +SELECT date 'J J 1520447'; +ERROR: invalid input syntax for type date: "J J 1520447" +LINE 1: SELECT date 'J J 1520447'; + ^ diff --git a/src/test/regress/sql/horology.sql b/src/test/regress/sql/horology.sql index 2724a2bbc7..6ad576b2dc 100644 --- a/src/test/regress/sql/horology.sql +++ b/src/test/regress/sql/horology.sql @@ -596,3 +596,7 @@ SELECT to_char('2012-12-12 12:00'::timestamptz, 'YYYY-MM-DD SSSS'); SELECT to_char('2012-12-12 12:00'::timestamptz, 'YYYY-MM-DD SSSSS'); RESET TIME ZONE; + +-- test error on dangling julian units +SELECT date '1995-08-6 J J J '; +SELECT date 'J J 1520447'; -- 2.34.1