7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.15 1997/04/05 02:51:41 scrappy Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.16 1997/04/22 17:36:44 scrappy Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -1400,8 +1400,23 @@ int
1400
1400
datetime2tm ( DateTime dt , int * tzp , struct tm * tm , double * fsec )
1401
1401
{
1402
1402
double date , time , sec ;
1403
+ time_t utime ;
1404
+
1405
+ if (tzp != NULL ) {
1406
+ /* XXX HACK to get time behavior compatible with Postgres v6.0 - tgl 97/04/07 */
1407
+ if ((tm -> tm_year > 1902 ) && (tm -> tm_year < 2038 )) {
1408
+ utime = ((date2j (2000 ,1 ,1 )- date2j (1970 ,1 ,1 ))* 86400 + dt );
1409
+ localtime ((time_t * ) & utime );
1410
+ #ifdef DATEDEBUG
1411
+ printf ( "datetime2tm- use system time zone = %ld (CTimeZone = %d)\n" , (long int ) utime , CTimeZone );
1412
+ #endif
1413
+ dt = dt2local ( dt , timezone );
1414
+
1415
+ } else {
1416
+ dt = dt2local ( dt , * tzp );
1417
+ };
1418
+ };
1403
1419
1404
- if (tzp != NULL ) dt = dt2local ( dt , * tzp );
1405
1420
time = (modf ( dt /86400 , & date )* 86400 );
1406
1421
date += date2j (2000 ,1 ,1 );
1407
1422
if (time < 0 ) {
@@ -1529,9 +1544,9 @@ printf( "tm2timespan- %d %f = %04d-%02d-%02d %02d:%02d:%02d %.2f\n", span->month
1529
1544
} /* tm2timespan() */
1530
1545
1531
1546
1532
- DateTime dt2local (DateTime dt , int timezone )
1547
+ DateTime dt2local (DateTime dt , int tz )
1533
1548
{
1534
- dt -= timezone ;
1549
+ dt -= tz ;
1535
1550
dt = JROUND (dt );
1536
1551
return (dt );
1537
1552
} /* dt2local() */
@@ -1711,6 +1726,8 @@ printf( "DecodeDateTime- field[%d] is %s (type %d)\n", i, field[i], ftype[i]);
1711
1726
1712
1727
case DTK_TIME :
1713
1728
if (DecodeTime (field [i ], fmask , & tmask , tm , fsec ) != 0 ) return -1 ;
1729
+ /* check upper limit on hours; other limits checked in DecodeTime() */
1730
+ if (tm -> tm_hour > 23 ) return -1 ;
1714
1731
break ;
1715
1732
1716
1733
case DTK_TZ :
@@ -1863,6 +1880,20 @@ printf( " %02d:%02d:%02d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);
1863
1880
if ((* dtype == DTK_DATE ) && ((fmask & DTK_DATE_M ) != DTK_DATE_M ))
1864
1881
return (((fmask & DTK_TIME_M ) == DTK_TIME_M )? 1 : -1 );
1865
1882
1883
+ /* timezone not specified? then find local timezone if possible */
1884
+ /* XXX HACK to get correct behavior relative to Postgres v6.0 - tgl 97/04/07 */
1885
+ if ((* dtype == DTK_DATE ) && ((fmask & DTK_DATE_M ) == DTK_DATE_M )
1886
+ && (tzp != NULL ) && (! (fmask & DTK_M (TZ )))
1887
+ && (tm -> tm_year > 1902 ) && (tm -> tm_year < 2038 )) {
1888
+ tm -> tm_year -= 1900 ;
1889
+ tm -> tm_mon -= 1 ;
1890
+ mktime (tm );
1891
+ tm -> tm_year += 1900 ;
1892
+ tm -> tm_mon += 1 ;
1893
+
1894
+ * tzp = timezone ;
1895
+ };
1896
+
1866
1897
return 0 ;
1867
1898
} /* DecodeDateTime() */
1868
1899
@@ -2066,6 +2097,8 @@ printf( "DecodeDate- illegal field %s value is %d\n", field[i], val);
2066
2097
2067
2098
/* DecodeTime()
2068
2099
* Decode time string which includes delimiters.
2100
+ * Only check the lower limit on hours, since this same code
2101
+ * can be used to represent time spans.
2069
2102
*/
2070
2103
int
2071
2104
DecodeTime (char * str , int fmask , int * tmask , struct tm * tm , double * fsec )
@@ -2099,6 +2132,11 @@ DecodeTime(char *str, int fmask, int *tmask, struct tm *tm, double *fsec)
2099
2132
};
2100
2133
};
2101
2134
2135
+ /* do a sanity check */
2136
+ if ((tm -> tm_hour < 0 )
2137
+ || (tm -> tm_min < 0 ) || (tm -> tm_min > 59 )
2138
+ || (tm -> tm_sec < 0 ) || (tm -> tm_sec > 59 )) return -1 ;
2139
+
2102
2140
return 0 ;
2103
2141
} /* DecodeTime() */
2104
2142
@@ -2654,6 +2692,9 @@ printf( "EncodeSpecialDateTime- unrecognized date\n");
2654
2692
} /* EncodeSpecialDateTime() */
2655
2693
2656
2694
2695
+ /* EncodeDateTime()
2696
+ * Encode date and time interpreted as local time.
2697
+ */
2657
2698
int EncodeDateTime (struct tm * tm , double fsec , int style , char * str )
2658
2699
{
2659
2700
char mabbrev [4 ], dabbrev [4 ];
@@ -2668,8 +2709,8 @@ int EncodeDateTime(struct tm *tm, double fsec, int style, char *str)
2668
2709
tm -> tm_isdst = -1 ;
2669
2710
2670
2711
#ifdef DATEDEBUG
2671
- printf ( "EncodeDateTime- timezone is %s; offset is %d ; daylight is %d\n" ,
2672
- CTZName , CTimeZone , CDayLight );
2712
+ printf ( "EncodeDateTime- timezone is %s (%s) ; offset is %ld (%d) ; daylight is %d (%d) \n" ,
2713
+ tzname [ 0 ], CTZName , ( long int ) timezone , CTimeZone , daylight , CDayLight );
2673
2714
#endif
2674
2715
2675
2716
day = date2j ( tm -> tm_year , tm -> tm_mon , tm -> tm_mday );
@@ -2723,10 +2764,8 @@ printf( "EncodeDateTime- day is %d\n", day);
2723
2764
sprintf ( str , "%02d/%02d" , tm -> tm_mon , tm -> tm_mday );
2724
2765
};
2725
2766
if (tm -> tm_year > 0 ) {
2726
- sprintf ( (str + 5 ), "/%04d %02d:%02d:%5 .2f %s" ,
2767
+ sprintf ( (str + 5 ), "/%04d %02d:%02d:%05 .2f %s" ,
2727
2768
tm -> tm_year , tm -> tm_hour , tm -> tm_min , sec , CTZName );
2728
- /* XXX brute-force fill in leading zero on seconds */
2729
- if (* (str + 17 ) == ' ' ) * (str + 17 ) = '0' ;
2730
2769
2731
2770
} else {
2732
2771
sprintf ( (str + 5 ), "/%04d %02d:%02d %s" ,
@@ -2742,10 +2781,12 @@ printf( "EncodeDateTime- day is %d\n", day);
2742
2781
sprintf ( (str + 4 ), "%3s %02d" , mabbrev , tm -> tm_mday );
2743
2782
};
2744
2783
if (tm -> tm_year > 0 ) {
2745
- sprintf ( (str + 10 ), " %02d:%02d:%5.2f %04d %s" ,
2784
+ #if FALSE
2785
+ sprintf ( (str + 10 ), " %02d:%02d:%05.2f %04d %s" ,
2746
2786
tm -> tm_hour , tm -> tm_min , sec , tm -> tm_year , CTZName );
2747
- /* XXX brute-force fill in leading zero on seconds */
2748
- if (* (str + 17 ) == ' ' ) * (str + 17 ) = '0' ;
2787
+ #endif
2788
+ sprintf ( (str + 10 ), " %02d:%02d:%05.2f %04d %s" ,
2789
+ tm -> tm_hour , tm -> tm_min , sec , tm -> tm_year , (daylight ? tzname [1 ]: tzname [0 ]));
2749
2790
2750
2791
} else {
2751
2792
sprintf ( (str + 10 ), " %02d:%02d %04d %s" ,
0 commit comments