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

Commit 01264e8

Browse files
author
Thomas G. Lockhart
committed
Modify EncodeDateTime() to suppress trailing ".00" in seconds field.
This matches the behavior of the original formatting for abstime. Repair datetime + timespan date arithmetic for year boundaries. From patch submitted by Dave Skinner.
1 parent d9bccec commit 01264e8

File tree

1 file changed

+56
-16
lines changed
  • src/backend/utils/adt

1 file changed

+56
-16
lines changed

src/backend/utils/adt/dt.c

+56-16
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/dt.c,v 1.25 1997/06/20 17:12:54 thomas Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.26 1997/06/23 14:50:56 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -693,7 +693,7 @@ printf( "datetime_add_span- add %f to %d %f\n", *datetime, span->month, span->ti
693693
#ifdef ROUND_ALL
694694
dt = JROUND(dt + span->time);
695695
#else
696-
dt = span->time;
696+
dt += span->time;
697697
#endif
698698

699699
if (span->month != 0) {
@@ -702,12 +702,13 @@ printf( "datetime_add_span- add %f to %d %f\n", *datetime, span->month, span->ti
702702

703703
if (datetime2tm( dt, &tz, tm, &fsec, &tzn) == 0) {
704704
#ifdef DATEDEBUG
705-
printf( "datetime_add_span- date was %d.%02d.%02d\n", tm->tm_year, tm->tm_mon, tm->tm_mday);
705+
printf( "datetime_add_span- date was %04d-%02d-%02d %02d:%02d:%02d\n",
706+
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
706707
#endif
707708
tm->tm_mon += span->month;
708709
if (tm->tm_mon > 12) {
709-
tm->tm_year += (tm->tm_mon / 12);
710-
tm->tm_mon = (tm->tm_mon % 12);
710+
tm->tm_year += ((tm->tm_mon-1) / 12);
711+
tm->tm_mon = (((tm->tm_mon-1) % 12) + 1);
711712
} else if (tm->tm_mon < 1) {
712713
tm->tm_year += ((tm->tm_mon / 12) - 1);
713714
tm->tm_mon = ((tm->tm_mon % 12) + 12);
@@ -721,15 +722,15 @@ printf( "datetime_add_span- date was %d.%02d.%02d\n", tm->tm_year, tm->tm_mon, t
721722
tm->tm_mday = mdays[ tm->tm_mon-1];
722723
};
723724
};
724-
725725
#ifdef DATEDEBUG
726-
printf( "datetime_add_span- date becomes %d.%02d.%02d\n", tm->tm_year, tm->tm_mon, tm->tm_mday);
726+
printf( "datetime_add_span- date becomes %04d-%02d-%02d %02d:%02d:%02d\n",
727+
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
727728
#endif
728729
if (tm2datetime( tm, fsec, &tz, &dt) != 0)
729730
elog(WARN,"Unable to add datetime and timespan",NULL);
730731

731732
} else {
732-
DATETIME_INVALID(*result);
733+
DATETIME_INVALID(dt);
733734
};
734735
};
735736

@@ -1312,7 +1313,11 @@ static datetkn datetktbl[] = {
13121313
{ "eet", TZ, 12}, /* East. Europe, USSR Zone 1 */
13131314
{ "eetdst", DTZ, 18}, /* Eastern Europe */
13141315
{ EPOCH, RESERV, DTK_EPOCH}, /* "epoch" reserved for system epoch time */
1316+
#if USE_AUSTRALIAN_RULES
1317+
{ "est", TZ, 60}, /* Australia Eastern Std Time */
1318+
#else
13151319
{ "est", TZ, NEG(30)}, /* Eastern Standard Time */
1320+
#endif
13161321
{ "feb", MONTH, 2},
13171322
{ "february", MONTH, 2},
13181323
{ "fri", DOW, 5},
@@ -1410,6 +1415,7 @@ static datetkn datetktbl[] = {
14101415
{ "zp4", TZ, NEG(24)}, /* GMT +4 hours. */
14111416
{ "zp5", TZ, NEG(30)}, /* GMT +5 hours. */
14121417
{ "zp6", TZ, NEG(36)}, /* GMT +6 hours. */
1418+
{ "z", RESERV, DTK_ZULU}, /* 00:00:00 */
14131419
{ ZULU, RESERV, DTK_ZULU}, /* 00:00:00 */
14141420
};
14151421

@@ -1585,7 +1591,6 @@ datetime2tm( DateTime dt, int *tzp, struct tm *tm, double *fsec, char **tzn)
15851591
struct tm *tx;
15861592
#endif
15871593

1588-
15891594
date0 = date2j(2000,1,1);
15901595

15911596
time = dt;
@@ -1625,6 +1630,9 @@ printf( "datetime2tm- time is %02d:%02d:%02d %.7f\n", tm->tm_hour, tm->tm_min, t
16251630
if (tzp != NULL) {
16261631
if (IS_VALID_UTIME( tm->tm_year, tm->tm_mon, tm->tm_mday)) {
16271632
utime = (dt + (date0-date2j(1970,1,1))*86400);
1633+
#if FALSE
1634+
if (utime < -1) utime++;
1635+
#endif
16281636

16291637
#ifdef USE_POSIX_TIME
16301638
tx = localtime(&utime);
@@ -1645,7 +1653,16 @@ printf( "datetime2tm- (localtime) %d.%02d.%02d %02d:%02d:%02.0f %s dst=%d\n",
16451653
tm->tm_mday = tx->tm_mday;
16461654
tm->tm_hour = tx->tm_hour;
16471655
tm->tm_min = tx->tm_min;
1656+
#if FALSE
1657+
/* XXX HACK
1658+
* Argh! My Linux box puts in a 1 second offset for dates less than 1970
1659+
* but only if the seconds field was non-zero. So, don't copy the seconds
1660+
* field and instead carry forward from the original - tgl 97/06/18
1661+
* Note that GNU/Linux uses the standard freeware zic package as do
1662+
* many other platforms so this may not be GNU/Linux/ix86-specific.
1663+
*/
16481664
tm->tm_sec = tx->tm_sec;
1665+
#endif
16491666
tm->tm_isdst = tx->tm_isdst;
16501667

16511668
#ifdef HAVE_INT_TIMEZONE
@@ -3071,12 +3088,19 @@ printf( "EncodeDateTime- day is %d\n", day);
30713088
sprintf( (str+4), "%3s %02d", mabbrev, tm->tm_mday);
30723089
};
30733090
if (tm->tm_year > 0) {
3074-
sprintf( (str+10), " %02d:%02d:%05.2f %04d",
3075-
tm->tm_hour, tm->tm_min, sec, tm->tm_year);
3076-
3077-
if ((*tzn != NULL) && (tm->tm_isdst >= 0)) {
3078-
strcpy( (str+27), " ");
3079-
strcpy( (str+28), *tzn);
3091+
sprintf( (str+10), " %02d:%02d", tm->tm_hour, tm->tm_min);
3092+
if (fsec != 0) {
3093+
sprintf( (str+16), ":%05.2f %04d", sec, tm->tm_year);
3094+
if ((*tzn != NULL) && (tm->tm_isdst >= 0)) {
3095+
strcpy( (str+27), " ");
3096+
strcpy( (str+28), *tzn);
3097+
};
3098+
} else {
3099+
sprintf( (str+16), ":%02.0f %04d", sec, tm->tm_year);
3100+
if ((*tzn != NULL) && (tm->tm_isdst >= 0)) {
3101+
strcpy( (str+24), " ");
3102+
strcpy( (str+25), *tzn);
3103+
};
30803104
};
30813105

30823106
} else {
@@ -3107,43 +3131,59 @@ int EncodeTimeSpan(struct tm *tm, double fsec, int style, char *str)
31073131

31083132
strcpy( str, "@");
31093133
cp = str+strlen(str);
3134+
31103135
if (tm->tm_year != 0) {
31113136
is_nonzero = TRUE;
31123137
is_before |= (tm->tm_year < 0);
31133138
sprintf( cp, " %d year%s", abs(tm->tm_year), ((abs(tm->tm_year) != 1)? "s": ""));
31143139
cp += strlen(cp);
31153140
};
3141+
31163142
if (tm->tm_mon != 0) {
31173143
is_nonzero = TRUE;
31183144
is_before |= (tm->tm_mon < 0);
31193145
sprintf( cp, " %d mon%s", abs(tm->tm_mon), ((abs(tm->tm_mon) != 1)? "s": ""));
31203146
cp += strlen(cp);
31213147
};
3148+
31223149
if (tm->tm_mday != 0) {
31233150
is_nonzero = TRUE;
31243151
is_before |= (tm->tm_mday < 0);
31253152
sprintf( cp, " %d day%s", abs(tm->tm_mday), ((abs(tm->tm_mday) != 1)? "s": ""));
31263153
cp += strlen(cp);
31273154
};
3155+
31283156
if (tm->tm_hour != 0) {
31293157
is_nonzero = TRUE;
31303158
is_before |= (tm->tm_hour < 0);
31313159
sprintf( cp, " %d hour%s", abs(tm->tm_hour), ((abs(tm->tm_hour) != 1)? "s": ""));
31323160
cp += strlen(cp);
31333161
};
3162+
31343163
if (tm->tm_min != 0) {
31353164
is_nonzero = TRUE;
31363165
is_before |= (tm->tm_min < 0);
31373166
sprintf( cp, " %d min%s", abs(tm->tm_min), ((abs(tm->tm_min) != 1)? "s": ""));
31383167
cp += strlen(cp);
31393168
};
3140-
if (tm->tm_sec != 0) {
3169+
3170+
/* fractional seconds? */
3171+
if (fsec != 0) {
3172+
is_nonzero = TRUE;
3173+
fsec += tm->tm_sec;
3174+
is_before |= (fsec < 0);
3175+
sprintf( cp, " %.2f secs", fabs(fsec));
3176+
cp += strlen(cp);
3177+
3178+
/* otherwise, integer seconds only? */
3179+
} else if (tm->tm_sec != 0) {
31413180
is_nonzero = TRUE;
31423181
is_before |= (tm->tm_sec < 0);
31433182
sprintf( cp, " %d sec%s", abs(tm->tm_sec), ((abs(tm->tm_sec) != 1)? "s": ""));
31443183
cp += strlen(cp);
31453184
};
31463185

3186+
/* identically zero? then put in a unitless zero... */
31473187
if (! is_nonzero) {
31483188
strcat( cp, " 0");
31493189
cp += strlen(cp);

0 commit comments

Comments
 (0)