Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9a8f572

Browse files
committed
Fix to_timestamp/to_date's handling of consecutive spaces in format string.
When there are consecutive spaces (or other non-format-code characters) in the format, we should advance over exactly that many characters of input. The previous coding mistakenly did a "skip whitespace" action between such characters, possibly allowing more input to be skipped than the user intended. We only need to skip whitespace just before an actual field. This is really a bug fix, but given the minimal number of field complaints and the risk of breaking applications coded to expect the old behavior, let's not back-patch it. Jeevan Chalke
1 parent 5363c7f commit 9a8f572

File tree

3 files changed

+107
-6
lines changed

3 files changed

+107
-6
lines changed

src/backend/utils/adt/formatting.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -2839,16 +2839,22 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
28392839
{
28402840
if (n->type != NODE_TYPE_ACTION)
28412841
{
2842+
/*
2843+
* Separator, so consume one character from input string. Notice
2844+
* we don't insist that the consumed character match the format's
2845+
* character.
2846+
*/
28422847
s++;
2843-
/* Ignore spaces when not in FX (fixed width) mode */
2844-
if (isspace((unsigned char) n->character) && !fx_mode)
2845-
{
2846-
while (*s != '\0' && isspace((unsigned char) *s))
2847-
s++;
2848-
}
28492848
continue;
28502849
}
28512850

2851+
/* Ignore spaces before fields when not in FX (fixed width) mode */
2852+
if (!fx_mode && n->key->id != DCH_FX)
2853+
{
2854+
while (*s != '\0' && isspace((unsigned char) *s))
2855+
s++;
2856+
}
2857+
28522858
from_char_set_mode(out, n->key->date_mode);
28532859

28542860
switch (n->key->id)

src/test/regress/expected/horology.out

+75
Original file line numberDiff line numberDiff line change
@@ -2906,6 +2906,81 @@ SELECT to_timestamp(' 20050302', 'YYYYMMDD');
29062906
Wed Mar 02 00:00:00 2005 PST
29072907
(1 row)
29082908

2909+
--
2910+
-- Check handling of multiple spaces in format and/or input
2911+
--
2912+
SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS');
2913+
to_timestamp
2914+
------------------------------
2915+
Sun Dec 18 03:38:15 2011 PST
2916+
(1 row)
2917+
2918+
SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS');
2919+
to_timestamp
2920+
------------------------------
2921+
Sun Dec 18 23:38:15 2011 PST
2922+
(1 row)
2923+
2924+
SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS');
2925+
to_timestamp
2926+
------------------------------
2927+
Sun Dec 18 23:38:15 2011 PST
2928+
(1 row)
2929+
2930+
SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS');
2931+
to_timestamp
2932+
------------------------------
2933+
Sun Dec 18 23:38:15 2011 PST
2934+
(1 row)
2935+
2936+
SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS');
2937+
to_timestamp
2938+
------------------------------
2939+
Sun Dec 18 23:38:15 2011 PST
2940+
(1 row)
2941+
2942+
SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS');
2943+
to_timestamp
2944+
------------------------------
2945+
Sun Dec 18 03:38:15 2011 PST
2946+
(1 row)
2947+
2948+
SELECT to_date('2011 12 18', 'YYYY MM DD');
2949+
to_date
2950+
------------
2951+
12-18-2011
2952+
(1 row)
2953+
2954+
SELECT to_date('2011 12 18', 'YYYY MM DD');
2955+
to_date
2956+
------------
2957+
12-18-2011
2958+
(1 row)
2959+
2960+
SELECT to_date('2011 12 18', 'YYYY MM DD');
2961+
to_date
2962+
------------
2963+
12-08-2011
2964+
(1 row)
2965+
2966+
SELECT to_date('2011 12 18', 'YYYY MM DD');
2967+
to_date
2968+
------------
2969+
02-18-2011
2970+
(1 row)
2971+
2972+
SELECT to_date('2011 12 18', 'YYYY MM DD');
2973+
to_date
2974+
------------
2975+
12-18-2011
2976+
(1 row)
2977+
2978+
SELECT to_date('2011 12 18', 'YYYY MM DD');
2979+
to_date
2980+
------------
2981+
12-18-2011
2982+
(1 row)
2983+
29092984
--
29102985
-- Check errors for some incorrect usages of to_timestamp()
29112986
--

src/test/regress/sql/horology.sql

+20
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,26 @@ SELECT to_timestamp(' 2005 03 02', 'YYYYMMDD');
440440

441441
SELECT to_timestamp(' 20050302', 'YYYYMMDD');
442442

443+
--
444+
-- Check handling of multiple spaces in format and/or input
445+
--
446+
447+
SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS');
448+
SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS');
449+
SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS');
450+
451+
SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS');
452+
SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS');
453+
SELECT to_timestamp('2011-12-18 23:38:15', 'YYYY-MM-DD HH24:MI:SS');
454+
455+
SELECT to_date('2011 12 18', 'YYYY MM DD');
456+
SELECT to_date('2011 12 18', 'YYYY MM DD');
457+
SELECT to_date('2011 12 18', 'YYYY MM DD');
458+
459+
SELECT to_date('2011 12 18', 'YYYY MM DD');
460+
SELECT to_date('2011 12 18', 'YYYY MM DD');
461+
SELECT to_date('2011 12 18', 'YYYY MM DD');
462+
443463
--
444464
-- Check errors for some incorrect usages of to_timestamp()
445465
--

0 commit comments

Comments
 (0)