Commit 3f1e529e authored by Tom Lane's avatar Tom Lane

Make to_timestamp and friends skip leading spaces before an integer field,

even when not in FM mode.  This improves compatibility with Oracle and with
our pre-8.4 behavior, as per bug #4862.

Brendan Jurd

Add a couple of regression test cases for this.  In passing, get rid of the
labeling of the individual test cases; doesn't seem to be good for anything
except causing extra work when inserting a test...

Tom Lane
parent 18df0ffb
/* -----------------------------------------------------------------------
* formatting.c
*
* $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.157 2009/06/11 14:49:03 momjian Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.158 2009/06/22 17:54:30 tgl Exp $
*
*
* Portions Copyright (c) 1999-2009, PostgreSQL Global Development Group
......@@ -1817,7 +1817,7 @@ from_char_set_int(int *dest, const int value, const FormatNode *node)
* 'dest'. If 'dest' is NULL, the result is discarded.
*
* In fixed-width mode (the node does not have the FM suffix), consume at most
* 'len' characters.
* 'len' characters. However, any leading whitespace isn't counted in 'len'.
*
* We use strtol() to recover the integer value from the source string, in
* accordance with the given FormatNode.
......@@ -1840,6 +1840,11 @@ 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);
......
This diff is collapsed.
......@@ -379,68 +379,76 @@ RESET DateStyle;
-- to_timestamp()
--
SELECT '' AS to_timestamp_1, to_timestamp('0097/Feb/16 --> 08:14:30', 'YYYY/Mon/DD --> HH:MI:SS');
SELECT to_timestamp('0097/Feb/16 --> 08:14:30', 'YYYY/Mon/DD --> HH:MI:SS');
SELECT '' AS to_timestamp_2, to_timestamp('97/2/16 8:14:30', 'FMYYYY/FMMM/FMDD FMHH:FMMI:FMSS');
SELECT to_timestamp('97/2/16 8:14:30', 'FMYYYY/FMMM/FMDD FMHH:FMMI:FMSS');
SELECT '' AS to_timestamp_3, to_timestamp('1985 January 12', 'YYYY FMMonth DD');
SELECT to_timestamp('1985 January 12', 'YYYY FMMonth DD');
SELECT '' AS to_timestamp_4, to_timestamp('My birthday-> Year: 1976, Month: May, Day: 16',
'"My birthday-> Year" YYYY, "Month:" FMMonth, "Day:" DD');
SELECT to_timestamp('My birthday-> Year: 1976, Month: May, Day: 16',
'"My birthday-> Year" YYYY, "Month:" FMMonth, "Day:" DD');
SELECT '' AS to_timestamp_5, to_timestamp('1,582nd VIII 21', 'Y,YYYth FMRM DD');
SELECT to_timestamp('1,582nd VIII 21', 'Y,YYYth FMRM DD');
SELECT '' AS to_timestamp_6, to_timestamp('15 "text between quote marks" 98 54 45',
E'HH24 "\\text between quote marks\\"" YY MI SS');
SELECT to_timestamp('15 "text between quote marks" 98 54 45',
E'HH24 "\\text between quote marks\\"" YY MI SS');
SELECT '' AS to_timestamp_7, to_timestamp('05121445482000', 'MMDDHH24MISSYYYY');
SELECT to_timestamp('05121445482000', 'MMDDHH24MISSYYYY');
SELECT '' AS to_timestamp_8, to_timestamp('2000January09Sunday', 'YYYYFMMonthDDFMDay');
SELECT to_timestamp('2000January09Sunday', 'YYYYFMMonthDDFMDay');
SELECT '' AS to_timestamp_9, to_timestamp('97/Feb/16', 'YYMonDD');
SELECT to_timestamp('97/Feb/16', 'YYMonDD');
SELECT '' AS to_timestamp_10, to_timestamp('19971116', 'YYYYMMDD');
SELECT to_timestamp('19971116', 'YYYYMMDD');
SELECT '' AS to_timestamp_11, to_timestamp('20000-1116', 'YYYY-MMDD');
SELECT to_timestamp('20000-1116', 'YYYY-MMDD');
SELECT '' AS to_timestamp_12, to_timestamp('9-1116', 'Y-MMDD');
SELECT to_timestamp('9-1116', 'Y-MMDD');
SELECT '' AS to_timestamp_13, to_timestamp('95-1116', 'YY-MMDD');
SELECT to_timestamp('95-1116', 'YY-MMDD');
SELECT '' AS to_timestamp_14, to_timestamp('995-1116', 'YYY-MMDD');
SELECT to_timestamp('995-1116', 'YYY-MMDD');
SELECT '' AS to_timestamp_15, to_timestamp('2005426', 'YYYYWWD');
SELECT to_timestamp('2005426', 'YYYYWWD');
SELECT '' AS to_timestamp_16, to_timestamp('2005300', 'YYYYDDD');
SELECT to_timestamp('2005300', 'YYYYDDD');
SELECT '' AS to_timestamp_17, to_timestamp('2005527', 'IYYYIWID');
SELECT to_timestamp('2005527', 'IYYYIWID');
SELECT '' AS to_timestamp_18, to_timestamp('005527', 'IYYIWID');
SELECT to_timestamp('005527', 'IYYIWID');
SELECT '' AS to_timestamp_19, to_timestamp('05527', 'IYIWID');
SELECT to_timestamp('05527', 'IYIWID');
SELECT '' AS to_timestamp_20, to_timestamp('5527', 'IIWID');
SELECT to_timestamp('5527', 'IIWID');
SELECT '' AS to_timestamp_21, to_timestamp('2005364', 'IYYYIDDD');
SELECT to_timestamp('2005364', 'IYYYIDDD');
SELECT to_timestamp('20050302', 'YYYYMMDD');
SELECT to_timestamp('2005 03 02', 'YYYYMMDD');
SELECT to_timestamp(' 2005 03 02', 'YYYYMMDD');
SELECT to_timestamp(' 20050302', 'YYYYMMDD');
--
-- Check errors for some incorrect usages of to_timestamp()
--
-- Mixture of date conventions (ISO week and Gregorian):
SELECT '' AS to_timestamp_22, to_timestamp('2005527', 'YYYYIWID');
SELECT to_timestamp('2005527', 'YYYYIWID');
-- Insufficient characters in the source string:
SELECT '' AS to_timestamp_23, to_timestamp('19971', 'YYYYMMDD');
SELECT to_timestamp('19971', 'YYYYMMDD');
-- Insufficient digit characters for a single node:
SELECT '' AS to_timestamp_24, to_timestamp('19971)24', 'YYYYMMDD');
SELECT to_timestamp('19971)24', 'YYYYMMDD');
-- Value clobbering:
SELECT '' AS to_timestamp_25, to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
SELECT to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
-- Non-numeric input:
SELECT '' AS to_timestamp_26, to_timestamp('199711xy', 'YYYYMMDD');
SELECT to_timestamp('199711xy', 'YYYYMMDD');
-- Input that doesn't fit in an int:
SELECT '' AS to_timestamp_27, to_timestamp('10000000000', 'FMYYYY');
SELECT to_timestamp('10000000000', 'FMYYYY');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment