Skip to content

Commit d1031cd

Browse files
committed
Adjust date/time input parsing code to correctly distinguish the four
SQLSTATE error codes required by SQL99 (invalid format, datetime field overflow, interval field overflow, invalid time zone displacement value). Also emit a HINT about DateStyle in cases where it seems appropriate. Per recent gripes.
1 parent 3722226 commit d1031cd

File tree

13 files changed

+465
-307
lines changed

13 files changed

+465
-307
lines changed

src/backend/utils/adt/date.c

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.90 2003/08/08 00:10:31 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.91 2003/08/27 23:29:27 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -62,20 +62,19 @@ date_in(PG_FUNCTION_ARGS)
6262
int tzp;
6363
int dtype;
6464
int nf;
65+
int dterr;
6566
char *field[MAXDATEFIELDS];
6667
int ftype[MAXDATEFIELDS];
6768
char lowstr[MAXDATELEN + 1];
6869

6970
if (strlen(str) >= sizeof(lowstr))
70-
ereport(ERROR,
71-
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
72-
errmsg("invalid input syntax for date: \"%s\"", str)));
73-
74-
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
75-
|| (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp) != 0))
76-
ereport(ERROR,
77-
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
78-
errmsg("invalid input syntax for date: \"%s\"", str)));
71+
dterr = DTERR_BAD_FORMAT;
72+
else
73+
dterr = ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf);
74+
if (dterr == 0)
75+
dterr = DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp);
76+
if (dterr != 0)
77+
DateTimeParseError(dterr, str, "date");
7978

8079
switch (dtype)
8180
{
@@ -95,9 +94,8 @@ date_in(PG_FUNCTION_ARGS)
9594
break;
9695

9796
default:
98-
ereport(ERROR,
99-
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
100-
errmsg("invalid input syntax for date: \"%s\"", str)));
97+
DateTimeParseError(DTERR_BAD_FORMAT, str, "date");
98+
break;
10199
}
102100

103101
date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
@@ -559,21 +557,20 @@ time_in(PG_FUNCTION_ARGS)
559557
*tm = &tt;
560558
int tz;
561559
int nf;
560+
int dterr;
562561
char lowstr[MAXDATELEN + 1];
563562
char *field[MAXDATEFIELDS];
564563
int dtype;
565564
int ftype[MAXDATEFIELDS];
566565

567566
if (strlen(str) >= sizeof(lowstr))
568-
ereport(ERROR,
569-
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
570-
errmsg("invalid input syntax for time: \"%s\"", str)));
571-
572-
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
573-
|| (DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
574-
ereport(ERROR,
575-
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
576-
errmsg("invalid input syntax for time: \"%s\"", str)));
567+
dterr = DTERR_BAD_FORMAT;
568+
else
569+
dterr = ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf);
570+
if (dterr == 0)
571+
dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
572+
if (dterr != 0)
573+
DateTimeParseError(dterr, str, "time");
577574

578575
tm2time(tm, fsec, &result);
579576
AdjustTimeForTypmod(&result, typmod);
@@ -1424,23 +1421,20 @@ timetz_in(PG_FUNCTION_ARGS)
14241421
*tm = &tt;
14251422
int tz;
14261423
int nf;
1424+
int dterr;
14271425
char lowstr[MAXDATELEN + 1];
14281426
char *field[MAXDATEFIELDS];
14291427
int dtype;
14301428
int ftype[MAXDATEFIELDS];
14311429

14321430
if (strlen(str) >= sizeof(lowstr))
1433-
ereport(ERROR,
1434-
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
1435-
errmsg("invalid input syntax for time with time zone: \"%s\"",
1436-
str)));
1437-
1438-
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
1439-
|| (DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
1440-
ereport(ERROR,
1441-
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
1442-
errmsg("invalid input syntax for time with time zone: \"%s\"",
1443-
str)));
1431+
dterr = DTERR_BAD_FORMAT;
1432+
else
1433+
dterr = ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf);
1434+
if (dterr == 0)
1435+
dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
1436+
if (dterr != 0)
1437+
DateTimeParseError(dterr, str, "time with time zone");
14441438

14451439
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
14461440
tm2timetz(tm, fsec, tz, result);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy