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

Commit 83adddf

Browse files
committed
- improve date/time parsing routines
- submitted by: Massimo Dal Zotto <dz@cs.unitn.it>
1 parent a7cfd65 commit 83adddf

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/backend/utils/adt/datetimes.c

+32-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/datetimes.c,v 1.1.1.1 1996/07/09 06:22:03 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/datetimes.c,v 1.2 1996/07/19 07:19:56 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -57,11 +57,15 @@ date_in(char *datestr)
5757
int4 result;
5858
DateADT *date = (DateADT*)&result;
5959

60+
#if 0
6061
#ifdef USE_SHORT_YEAR
6162
#define CHECK_DATE_LEN(datestr) (strlen(datestr) >= 8)
6263
#else
6364
#define CHECK_DATE_LEN(datestr) (strlen(datestr) == 10)
6465
#endif /* USE_SHORT_YEAR */
66+
#else
67+
#define CHECK_DATE_LEN(datestr) 1
68+
#endif
6569

6670
#ifdef AMERICAN_STYLE
6771
if (!CHECK_DATE_LEN(datestr) ||
@@ -76,6 +80,8 @@ date_in(char *datestr)
7680
datestr);
7781
}
7882
#endif
83+
if (y < 0 || y > 32767)
84+
elog(WARN, "date_in: year must be limited to values 0 through 32767 in \"%s\"", datestr);
7985
if (m < 1 || m > 12)
8086
elog(WARN, "date_in: month must be limited to values 1 through 12 in \"%s\"", datestr);
8187
if (d < 1 || d > day_tab[isleap(y)][m-1])
@@ -247,16 +253,19 @@ time_in(char *timestr)
247253
TimeADT *time;
248254

249255
if (sscanf(timestr, "%d%*c%d%*c%f", &h, &m, &sec) != 3) {
250-
elog(WARN, "time_in: time \"%s\" not of the form hh:mm:ss",
251-
timestr);
256+
sec = 0.0;
257+
if (sscanf(timestr, "%d%*c%d", &h, &m) != 2) {
258+
elog(WARN, "time_in: time \"%s\" not of the form hh:mm:ss",
259+
timestr);
260+
}
252261
}
253262

254263
if (h < 0 || h > 23)
255264
elog(WARN, "time_in: hour must be limited to values 0 through 23 in \"%s\"", timestr);
256265
if (m < 0 || m > 59)
257266
elog(WARN, "time_in: minute must be limited to values 0 through 59 in \"%s\"", timestr);
258-
if (sec < 0 || sec >= 62.0)
259-
elog(WARN, "time_in: second must be limited to values 0 through 61.99 in \"%s\"", timestr);
267+
if (sec < 0 || sec >= 60.0)
268+
elog(WARN, "time_in: second must be limited to values 0 through 59.999 in \"%s\"", timestr);
260269

261270
time = (TimeADT*)palloc(sizeof(TimeADT));
262271
time->hr = h;
@@ -268,10 +277,24 @@ time_in(char *timestr)
268277
char *
269278
time_out(TimeADT *time)
270279
{
271-
char *timestr = palloc(16);
272-
273-
sprintf(timestr, "%02d:%02d:%09.6f",
274-
(int)time->hr, (int)time->min, time->sec);
280+
char *timestr = palloc(32);
281+
int n;
282+
float f;
283+
284+
if (time->sec == 0.0) {
285+
sprintf(timestr, "%02d:%02d",
286+
(int)time->hr, (int)time->min);
287+
} else {
288+
n = (int)time->sec;
289+
f = (float)n;
290+
if (f == time->sec) {
291+
sprintf(timestr, "%02d:%02d:%02d",
292+
(int)time->hr, (int)time->min, n);
293+
} else {
294+
sprintf(timestr, "%02d:%02d:%09.6f",
295+
(int)time->hr, (int)time->min, time->sec);
296+
}
297+
}
275298

276299
return timestr;
277300
}

0 commit comments

Comments
 (0)