Skip to content

Commit f309c81

Browse files
committed
Fix an oversight in commit 4c70098.
I had supposed that the from_char_seq_search() call sites were all passing the constant arrays you'd expect them to pass ... but on looking closer, the one for DY format was passing the days[] array not days_short[]. This accidentally worked because the day abbreviations in English are all the same as the first three letters of the full day names. However, once we took out the "maximum comparison length" logic, it stopped working. As penance for that oversight, add regression test cases covering this, as well as every other switch case in DCH_from_char() that was not reached according to the code coverage report. Also, fold the DCH_RM and DCH_rm cases into one --- now that seq_search is case independent, there's no need to pass different comparison arrays for those cases. Back-patch, as the previous commit was.
1 parent be13f22 commit f309c81

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

src/backend/utils/adt/formatting.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,7 +3248,7 @@ DCH_from_char(FormatNode *node, const char *in, TmFromChar *out)
32483248
case DCH_DY:
32493249
case DCH_Dy:
32503250
case DCH_dy:
3251-
from_char_seq_search(&value, &s, days,
3251+
from_char_seq_search(&value, &s, days_short,
32523252
n);
32533253
from_char_set_int(&out->d, value, n);
32543254
out->d++;
@@ -3347,10 +3347,6 @@ DCH_from_char(FormatNode *node, const char *in, TmFromChar *out)
33473347
SKIP_THth(s, n->suffix);
33483348
break;
33493349
case DCH_RM:
3350-
from_char_seq_search(&value, &s, rm_months_upper,
3351-
n);
3352-
from_char_set_int(&out->mm, MONTHS_PER_YEAR - value, n);
3353-
break;
33543350
case DCH_rm:
33553351
from_char_seq_search(&value, &s, rm_months_lower,
33563352
n);

src/test/regress/expected/horology.out

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,18 @@ SELECT to_timestamp('1997 BC 11 16', 'YYYY BC MM DD');
26602660
Tue Nov 16 00:00:00 1997 PST BC
26612661
(1 row)
26622662

2663+
SELECT to_timestamp('1997 A.D. 11 16', 'YYYY B.C. MM DD');
2664+
to_timestamp
2665+
------------------------------
2666+
Sun Nov 16 00:00:00 1997 PST
2667+
(1 row)
2668+
2669+
SELECT to_timestamp('1997 B.C. 11 16', 'YYYY B.C. MM DD');
2670+
to_timestamp
2671+
---------------------------------
2672+
Tue Nov 16 00:00:00 1997 PST BC
2673+
(1 row)
2674+
26632675
SELECT to_timestamp('9-1116', 'Y-MMDD');
26642676
to_timestamp
26652677
------------------------------
@@ -2756,6 +2768,18 @@ SELECT to_timestamp('2011-12-18 11:38 PM', 'YYYY-MM-DD HH12:MI PM');
27562768
Sun Dec 18 23:38:00 2011 PST
27572769
(1 row)
27582770

2771+
SELECT to_timestamp('2011-12-18 11:38 A.M.', 'YYYY-MM-DD HH12:MI P.M.');
2772+
to_timestamp
2773+
------------------------------
2774+
Sun Dec 18 11:38:00 2011 PST
2775+
(1 row)
2776+
2777+
SELECT to_timestamp('2011-12-18 11:38 P.M.', 'YYYY-MM-DD HH12:MI P.M.');
2778+
to_timestamp
2779+
------------------------------
2780+
Sun Dec 18 23:38:00 2011 PST
2781+
(1 row)
2782+
27592783
SELECT to_timestamp('2011-12-18 11:38 +05', 'YYYY-MM-DD HH12:MI TZH');
27602784
to_timestamp
27612785
------------------------------
@@ -2786,6 +2810,32 @@ SELECT to_timestamp('2011-12-18 11:38 20', 'YYYY-MM-DD HH12:MI TZM');
27862810
Sun Dec 18 03:18:00 2011 PST
27872811
(1 row)
27882812

2813+
SELECT to_timestamp('2011-12-18 11:38 PST', 'YYYY-MM-DD HH12:MI TZ'); -- NYI
2814+
ERROR: formatting field "TZ" is only supported in to_char
2815+
SELECT to_timestamp('2018-11-02 12:34:56.025', 'YYYY-MM-DD HH24:MI:SS.MS');
2816+
to_timestamp
2817+
----------------------------------
2818+
Fri Nov 02 12:34:56.025 2018 PDT
2819+
(1 row)
2820+
2821+
SELECT to_date('1 4 1902', 'Q MM YYYY'); -- Q is ignored
2822+
to_date
2823+
------------
2824+
04-01-1902
2825+
(1 row)
2826+
2827+
SELECT to_date('3 4 21 01', 'W MM CC YY');
2828+
to_date
2829+
------------
2830+
04-15-2001
2831+
(1 row)
2832+
2833+
SELECT to_date('2458872', 'J');
2834+
to_date
2835+
------------
2836+
01-23-2020
2837+
(1 row)
2838+
27892839
--
27902840
-- Check handling of multiple spaces in format and/or input
27912841
--
@@ -2950,6 +3000,19 @@ SELECT to_timestamp('19971)24', 'YYYYMMDD');
29503000
ERROR: invalid value "1)" for "MM"
29513001
DETAIL: Field requires 2 characters, but only 1 could be parsed.
29523002
HINT: If your source string is not fixed-width, try using the "FM" modifier.
3003+
-- We don't accept full-length day or month names if short form is specified:
3004+
SELECT to_timestamp('Friday 1-January-1999', 'DY DD MON YYYY');
3005+
ERROR: invalid value "da" for "DD"
3006+
DETAIL: Value must be an integer.
3007+
SELECT to_timestamp('Fri 1-January-1999', 'DY DD MON YYYY');
3008+
ERROR: invalid value "uary" for "YYYY"
3009+
DETAIL: Value must be an integer.
3010+
SELECT to_timestamp('Fri 1-Jan-1999', 'DY DD MON YYYY'); -- ok
3011+
to_timestamp
3012+
------------------------------
3013+
Fri Jan 01 00:00:00 1999 PST
3014+
(1 row)
3015+
29533016
-- Value clobbering:
29543017
SELECT to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
29553018
ERROR: conflicting values for "Mon" field in formatting string

src/test/regress/sql/horology.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ SELECT to_timestamp('20000-1116', 'YYYY-MMDD');
365365
SELECT to_timestamp('1997 AD 11 16', 'YYYY BC MM DD');
366366
SELECT to_timestamp('1997 BC 11 16', 'YYYY BC MM DD');
367367

368+
SELECT to_timestamp('1997 A.D. 11 16', 'YYYY B.C. MM DD');
369+
SELECT to_timestamp('1997 B.C. 11 16', 'YYYY B.C. MM DD');
370+
368371
SELECT to_timestamp('9-1116', 'Y-MMDD');
369372

370373
SELECT to_timestamp('95-1116', 'YY-MMDD');
@@ -396,12 +399,23 @@ SELECT to_timestamp(' 20050302', 'YYYYMMDD');
396399
SELECT to_timestamp('2011-12-18 11:38 AM', 'YYYY-MM-DD HH12:MI PM');
397400
SELECT to_timestamp('2011-12-18 11:38 PM', 'YYYY-MM-DD HH12:MI PM');
398401

402+
SELECT to_timestamp('2011-12-18 11:38 A.M.', 'YYYY-MM-DD HH12:MI P.M.');
403+
SELECT to_timestamp('2011-12-18 11:38 P.M.', 'YYYY-MM-DD HH12:MI P.M.');
404+
399405
SELECT to_timestamp('2011-12-18 11:38 +05', 'YYYY-MM-DD HH12:MI TZH');
400406
SELECT to_timestamp('2011-12-18 11:38 -05', 'YYYY-MM-DD HH12:MI TZH');
401407
SELECT to_timestamp('2011-12-18 11:38 +05:20', 'YYYY-MM-DD HH12:MI TZH:TZM');
402408
SELECT to_timestamp('2011-12-18 11:38 -05:20', 'YYYY-MM-DD HH12:MI TZH:TZM');
403409
SELECT to_timestamp('2011-12-18 11:38 20', 'YYYY-MM-DD HH12:MI TZM');
404410

411+
SELECT to_timestamp('2011-12-18 11:38 PST', 'YYYY-MM-DD HH12:MI TZ'); -- NYI
412+
413+
SELECT to_timestamp('2018-11-02 12:34:56.025', 'YYYY-MM-DD HH24:MI:SS.MS');
414+
415+
SELECT to_date('1 4 1902', 'Q MM YYYY'); -- Q is ignored
416+
SELECT to_date('3 4 21 01', 'W MM CC YY');
417+
SELECT to_date('2458872', 'J');
418+
405419
--
406420
-- Check handling of multiple spaces in format and/or input
407421
--
@@ -450,6 +464,11 @@ SELECT to_timestamp('19971', 'YYYYMMDD');
450464
-- Insufficient digit characters for a single node:
451465
SELECT to_timestamp('19971)24', 'YYYYMMDD');
452466

467+
-- We don't accept full-length day or month names if short form is specified:
468+
SELECT to_timestamp('Friday 1-January-1999', 'DY DD MON YYYY');
469+
SELECT to_timestamp('Fri 1-January-1999', 'DY DD MON YYYY');
470+
SELECT to_timestamp('Fri 1-Jan-1999', 'DY DD MON YYYY'); -- ok
471+
453472
-- Value clobbering:
454473
SELECT to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
455474

0 commit comments

Comments
 (0)