7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.16 1997/04/22 17:36:44 scrappy Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.17 1997/04/25 18:40:13 scrappy Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -120,6 +120,7 @@ char *
120
120
datetime_out (DateTime * dt )
121
121
{
122
122
char * result ;
123
+ int tz ;
123
124
struct tm tt , * tm = & tt ;
124
125
double fsec ;
125
126
char buf [MAXDATELEN + 1 ];
@@ -130,8 +131,8 @@ datetime_out(DateTime *dt)
130
131
if (DATETIME_IS_RESERVED (* dt )) {
131
132
EncodeSpecialDateTime (* dt , buf );
132
133
133
- } else if (datetime2tm ( * dt , & CTimeZone , tm , & fsec ) == 0 ) {
134
- EncodeDateTime (tm , fsec , DateStyle , buf );
134
+ } else if (datetime2tm ( * dt , & tz , tm , & fsec ) == 0 ) {
135
+ EncodeDateTime (tm , fsec , & tz , DateStyle , buf );
135
136
136
137
} else {
137
138
EncodeSpecialDateTime (DT_INVALID , buf );
@@ -840,6 +841,7 @@ datetime_part(text *units, DateTime *datetime)
840
841
float64 result ;
841
842
842
843
DateTime dt ;
844
+ int tz ;
843
845
int type , val ;
844
846
int i ;
845
847
char * up , * lp , lowunits [MAXDATELEN + 1 ];
@@ -874,10 +876,10 @@ printf( "datetime_part- units %s type=%d value=%d\n", lowunits, type, val);
874
876
875
877
dt = (DATETIME_IS_RELATIVE (* datetime )? SetDateTime (* datetime ): * datetime );
876
878
877
- if (datetime2tm ( dt , & CTimeZone , tm , & fsec ) == 0 ) {
879
+ if (datetime2tm ( dt , & tz , tm , & fsec ) == 0 ) {
878
880
switch (val ) {
879
881
case DTK_TZ :
880
- * result = CTimeZone ;
882
+ * result = tz ;
881
883
break ;
882
884
883
885
case DTK_MICROSEC :
@@ -929,7 +931,7 @@ printf( "datetime_part- units %s type=%d value=%d\n", lowunits, type, val);
929
931
break ;
930
932
931
933
default :
932
- elog (WARN ,"Datetime units %s not yet supported" ,units );
934
+ elog (WARN ,"Datetime units %s not supported" ,units );
933
935
* result = 0 ;
934
936
};
935
937
@@ -1401,21 +1403,9 @@ datetime2tm( DateTime dt, int *tzp, struct tm *tm, double *fsec)
1401
1403
{
1402
1404
double date , time , sec ;
1403
1405
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 );
1406
+ #ifdef USE_POSIX_TIME
1407
+ struct tm * tx ;
1412
1408
#endif
1413
- dt = dt2local ( dt , timezone );
1414
-
1415
- } else {
1416
- dt = dt2local ( dt , * tzp );
1417
- };
1418
- };
1419
1409
1420
1410
time = (modf ( dt /86400 , & date )* 86400 );
1421
1411
date += date2j (2000 ,1 ,1 );
@@ -1428,16 +1418,15 @@ printf( "datetime2tm- use system time zone = %ld (CTimeZone = %d)\n", (long int)
1428
1418
if (date < 0 ) return -1 ;
1429
1419
1430
1420
#ifdef DATEDEBUG
1431
- printf ( "datetime2tm- date is %f (%f %f)\n" ,
1432
- ((tzp != NULL )? dt2local (dt , - (* tzp )): dt ), date , time );
1421
+ printf ( "datetime2tm- date is %f (%f %f)\n" , dt , date , time );
1433
1422
#endif
1434
1423
1435
1424
j2date ((int ) date , & tm -> tm_year , & tm -> tm_mon , & tm -> tm_mday );
1436
1425
dt2time ( time , & tm -> tm_hour , & tm -> tm_min , & sec );
1437
1426
1438
1427
#ifdef DATEDEBUG
1439
1428
printf ( "datetime2tm- date is %d.%02d.%02d\n" , tm -> tm_year , tm -> tm_mon , tm -> tm_mday );
1440
- printf ( "datetime2tm- time is %02d:%02d:%2.2f \n" , tm -> tm_hour , tm -> tm_min , sec );
1429
+ printf ( "datetime2tm- time is %02d:%02d:%02.0f \n" , tm -> tm_hour , tm -> tm_min , sec );
1441
1430
#endif
1442
1431
1443
1432
* fsec = modf (JROUND (sec ),& sec );
@@ -1447,11 +1436,62 @@ printf( "datetime2tm- time is %02d:%02d:%2.2f\n", tm->tm_hour, tm->tm_min, sec);
1447
1436
printf ( "datetime2tm- time is %02d:%02d:%02d %.7f\n" , tm -> tm_hour , tm -> tm_min , tm -> tm_sec , * fsec );
1448
1437
#endif
1449
1438
1450
- tm -> tm_isdst = -1 ;
1439
+ if (tzp != NULL ) {
1440
+ /* XXX HACK to get time behavior compatible with Postgres v6.0 - tgl 97/04/07 */
1441
+ if ((tm -> tm_year >= 1902 ) && (tm -> tm_year < 2038 )) {
1442
+ utime = (dt + (date2j (2000 ,1 ,1 )- date2j (1970 ,1 ,1 ))* 86400 );
1443
+ #ifdef USE_POSIX_TIME
1444
+ tx = localtime (& utime );
1445
+ #ifdef DATEDEBUG
1446
+ printf ( "datetime2tm- (localtime) %d.%02d.%02d %02d:%02d:%02.0f %s %s dst=%d\n" ,
1447
+ tx -> tm_year , tx -> tm_mon , tx -> tm_mday , tx -> tm_hour , tx -> tm_min , sec ,
1448
+ tzname [0 ], tzname [1 ], tx -> tm_isdst );
1449
+ #endif
1450
+ tm -> tm_year = tx -> tm_year + 1900 ;
1451
+ tm -> tm_mon = tx -> tm_mon + 1 ;
1452
+ tm -> tm_mday = tx -> tm_mday ;
1453
+ tm -> tm_hour = tx -> tm_hour ;
1454
+ tm -> tm_min = tx -> tm_min ;
1455
+ tm -> tm_sec = tx -> tm_sec ;
1456
+ tm -> tm_isdst = tx -> tm_isdst ;
1457
+ #ifdef HAVE_INT_TIMEZONE
1458
+ * tzp = (tm -> tm_isdst ? (timezone - 3600 ): timezone );
1459
+ #else /* !HAVE_INT_TIMEZONE */
1460
+ * tzp = (tm -> tm_isdst ? (tm -> tm_gmtoff - 3600 ): tm -> tm_gmtoff ); /* tm_gmtoff is Sun/DEC-ism */
1461
+ #endif
1462
+ #else /* !USE_POSIX_TIME */
1463
+ * tzp = CTimeZone ; /* V7 conventions; don't know timezone? */
1464
+ #endif
1465
+ } else {
1466
+ * tzp = 0 ;
1467
+ tm -> tm_isdst = 0 ;
1468
+ #ifdef USE_POSIX_TIME
1469
+ #ifdef HAVE_INT_TIMEZONE
1470
+ tzname [0 ] = "GMT" ;
1471
+ #else /* !HAVE_INT_TIMEZONE */
1472
+ tm -> tm_zone = "GMT" ;
1473
+ #endif
1474
+ #else /* !USE_POSIX_TIME */
1475
+ strcpy ( CTZName , "GMT" );
1476
+ #endif
1477
+ };
1478
+
1479
+ dt = dt2local ( dt , * tzp );
1480
+
1481
+ } else {
1482
+ tm -> tm_isdst = 0 ;
1483
+ };
1484
+
1485
+ #ifdef DATEDEBUG
1486
+ printf ( "datetime2tm- date is %d.%02d.%02d\n" , tm -> tm_year , tm -> tm_mon , tm -> tm_mday );
1487
+ printf ( "datetime2tm- time is %02d:%02d:%02d %.7f\n" , tm -> tm_hour , tm -> tm_min , tm -> tm_sec , * fsec );
1488
+ #endif
1451
1489
1452
1490
#ifdef DATEDEBUG
1491
+ #ifdef HAVE_INT_TIMEZONE
1453
1492
printf ( "datetime2tm- timezone is %s; offset is %d (%d); daylight is %d\n" ,
1454
- CTZName , ((tzp != NULL )? * tzp : 0 ), CTimeZone , CDayLight );
1493
+ tzname [tm -> tm_isdst != 0 ], ((tzp != NULL )? * tzp : 0 ), CTimeZone , CDayLight );
1494
+ #endif
1455
1495
#endif
1456
1496
1457
1497
return 0 ;
@@ -1824,9 +1864,13 @@ printf( "DecodeDateTime- month field %s value is %d\n", field[i], val);
1824
1864
break ;
1825
1865
1826
1866
case DTZ :
1827
- tm -> tm_isdst = 0 ;
1828
- /* FALLTHROUGH! */
1867
+ tm -> tm_isdst = 1 ;
1868
+ if (tzp == NULL ) return -1 ;
1869
+ * tzp = val * 60 ;
1870
+ break ;
1871
+
1829
1872
case TZ :
1873
+ tm -> tm_isdst = 0 ;
1830
1874
if (tzp == NULL ) return -1 ;
1831
1875
* tzp = val * 60 ;
1832
1876
break ;
@@ -1883,15 +1927,29 @@ printf( " %02d:%02d:%02d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);
1883
1927
/* timezone not specified? then find local timezone if possible */
1884
1928
/* XXX HACK to get correct behavior relative to Postgres v6.0 - tgl 97/04/07 */
1885
1929
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 ;
1930
+ && (tzp != NULL ) && (! (fmask & DTK_M (TZ )))) {
1931
+
1932
+ if ((tm -> tm_year >= 1902 ) && (tm -> tm_year < 2038 )) {
1933
+ #ifdef USE_POSIX_TIME
1934
+ tm -> tm_year -= 1900 ;
1935
+ tm -> tm_mon -= 1 ;
1936
+ tm -> tm_isdst = -1 ;
1937
+ mktime (tm );
1938
+ tm -> tm_year += 1900 ;
1939
+ tm -> tm_mon += 1 ;
1893
1940
1894
- * tzp = timezone ;
1941
+ #ifdef HAVE_INT_TIMEZONE
1942
+ * tzp = ((tm -> tm_isdst > 0 )? (timezone - 3600 ): timezone );
1943
+ #else /* !HAVE_INT_TIMEZONE */
1944
+ * tzp = tm -> tm_gmtoff ;
1945
+ #endif
1946
+ #else /* !USE_POSIX_TIME */
1947
+ * tzp = CTimeZone ;
1948
+ #endif
1949
+ } else {
1950
+ tm -> tm_isdst = 0 ;
1951
+ * tzp = 0 ;
1952
+ };
1895
1953
};
1896
1954
1897
1955
return 0 ;
@@ -1960,7 +2018,7 @@ printf( "DecodeTimeOnly- RESERV field %s value is %d\n", field[i], val);
1960
2018
tm -> tm_hour = 0 ;
1961
2019
tm -> tm_min = 0 ;
1962
2020
tm -> tm_sec = 0 ;
1963
- tm -> tm_isdst = -1 ;
2021
+ tm -> tm_isdst = 0 ;
1964
2022
break ;
1965
2023
1966
2024
default :
@@ -2695,7 +2753,7 @@ printf( "EncodeSpecialDateTime- unrecognized date\n");
2695
2753
/* EncodeDateTime()
2696
2754
* Encode date and time interpreted as local time.
2697
2755
*/
2698
- int EncodeDateTime (struct tm * tm , double fsec , int style , char * str )
2756
+ int EncodeDateTime (struct tm * tm , double fsec , int * tzp , int style , char * str )
2699
2757
{
2700
2758
char mabbrev [4 ], dabbrev [4 ];
2701
2759
int day , hour , min ;
@@ -2706,11 +2764,36 @@ int EncodeDateTime(struct tm *tm, double fsec, int style, char *str)
2706
2764
2707
2765
sec = (tm -> tm_sec + fsec );
2708
2766
2767
+ #if FALSE
2709
2768
tm -> tm_isdst = -1 ;
2769
+ #endif
2710
2770
2711
2771
#ifdef DATEDEBUG
2772
+ #ifdef USE_POSIX_TIME
2773
+ #ifdef HAVE_INT_TIMEZONE
2774
+ printf ( "EncodeDateTime- timezone is %s (%s); offset is %d (%d); daylight is %d (%d)\n" ,
2775
+ tzname [0 ], CTZName , * tzp , CTimeZone , tm -> tm_isdst , CDayLight );
2776
+ #else
2712
2777
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 );
2778
+ tm -> tm_zone , CTZName , (- tm -> tm_gmtoff ), CTimeZone , tm -> tm_isdst , CDayLight );
2779
+ #endif
2780
+ #else
2781
+ printf ( "EncodeDateTime- timezone is %s; offset is %d; daylight is %d\n" ,
2782
+ CTZName , CTimeZone , CDayLight );
2783
+ #endif
2784
+ #endif
2785
+
2786
+ #ifdef USE_POSIX_TIME
2787
+ /* XXX HACK to get time behavior compatible with Postgres v6.0 - tgl 97/04/07 */
2788
+ if ((tm -> tm_year >= 1902 ) && (tm -> tm_year < 2038 )) {
2789
+ tm -> tm_year -= 1900 ;
2790
+ tm -> tm_mon -= 1 ;
2791
+ mktime (tm );
2792
+ tm -> tm_year += 1900 ;
2793
+ tm -> tm_mon += 1 ;
2794
+ } else {
2795
+ tm -> tm_isdst = -1 ;
2796
+ };
2714
2797
#endif
2715
2798
2716
2799
day = date2j ( tm -> tm_year , tm -> tm_mon , tm -> tm_mday );
@@ -2730,21 +2813,20 @@ printf( "EncodeDateTime- day is %d\n", day);
2730
2813
/* compatible with ISO date formats */
2731
2814
if (style == USE_ISO_DATES ) {
2732
2815
if (tm -> tm_year > 0 ) {
2733
- #if FALSE
2734
- sprintf ( str , "%04d-%02d-%02d %02d:%02d:%05.2f %s" ,
2735
- tm -> tm_year , tm -> tm_mon , tm -> tm_mday , tm -> tm_hour , tm -> tm_min , sec , CTZName );
2736
- #endif
2737
2816
sprintf ( str , "%04d-%02d-%02d %02d:%02d:" ,
2738
2817
tm -> tm_year , tm -> tm_mon , tm -> tm_mday , tm -> tm_hour , tm -> tm_min );
2739
2818
sprintf ( (str + 17 ), ((fsec != 0 )? "%05.2f" : "%02.0f" ), sec );
2740
- hour = - (CTimeZone / 3600 );
2741
- min = ((abs (CTimeZone ) / 60 ) % 60 );
2742
- sprintf ( (str + strlen (str )), ((min != 0 )? "%+03d:%02d" : "%+03d" ), hour , min );
2743
- #if FALSE
2744
- sprintf ( str , "%04d-%02d-%02d %02d:%02d:%05.2f%+03d:%02d" ,
2745
- tm -> tm_year , tm -> tm_mon , tm -> tm_mday , tm -> tm_hour , tm -> tm_min , sec ,
2746
- hour , min );
2747
- #endif
2819
+
2820
+ if (tm -> tm_isdst >= 0 ) {
2821
+ if (tzp != NULL ) {
2822
+ hour = - (* tzp / 3600 );
2823
+ min = ((abs (* tzp ) / 60 ) % 60 );
2824
+ } else {
2825
+ hour = 0 ;
2826
+ min = 0 ;
2827
+ };
2828
+ sprintf ( (str + strlen (str )), ((min != 0 )? "%+03d:%02d" : "%+03d" ), hour , min );
2829
+ };
2748
2830
2749
2831
} else {
2750
2832
if (tm -> tm_hour || tm -> tm_min ) {
@@ -2764,8 +2846,20 @@ printf( "EncodeDateTime- day is %d\n", day);
2764
2846
sprintf ( str , "%02d/%02d" , tm -> tm_mon , tm -> tm_mday );
2765
2847
};
2766
2848
if (tm -> tm_year > 0 ) {
2767
- sprintf ( (str + 5 ), "/%04d %02d:%02d:%05.2f %s" ,
2768
- tm -> tm_year , tm -> tm_hour , tm -> tm_min , sec , CTZName );
2849
+ sprintf ( (str + 5 ), "/%04d %02d:%02d:%05.2f" ,
2850
+ tm -> tm_year , tm -> tm_hour , tm -> tm_min , sec );
2851
+
2852
+ if (tm -> tm_isdst >= 0 ) {
2853
+ #ifdef USE_POSIX_TIME
2854
+ #ifdef HAVE_INT_TIMEZONE
2855
+ sprintf ( (str + 22 ), " %s" , tzname [(tm -> tm_isdst > 0 )]);
2856
+ #else /* !HAVE_INT_TIMEZONE */
2857
+ sprintf ( (str + 22 ), " %s" , tm -> tm_zone );
2858
+ #endif
2859
+ #else /* !USE_POSIX_TIME */
2860
+ sprintf ( (str + 22 ), " %s" , CTZName );
2861
+ #endif
2862
+ };
2769
2863
2770
2864
} else {
2771
2865
sprintf ( (str + 5 ), "/%04d %02d:%02d %s" ,
@@ -2781,12 +2875,20 @@ printf( "EncodeDateTime- day is %d\n", day);
2781
2875
sprintf ( (str + 4 ), "%3s %02d" , mabbrev , tm -> tm_mday );
2782
2876
};
2783
2877
if (tm -> tm_year > 0 ) {
2784
- #if FALSE
2785
- sprintf ( (str + 10 ), " %02d:%02d:%05.2f %04d %s" ,
2786
- tm -> tm_hour , tm -> tm_min , sec , tm -> tm_year , CTZName );
2878
+ sprintf ( (str + 10 ), " %02d:%02d:%05.2f %04d" ,
2879
+ tm -> tm_hour , tm -> tm_min , sec , tm -> tm_year );
2880
+
2881
+ if (tm -> tm_isdst >= 0 ) {
2882
+ #ifdef USE_POSIX_TIME
2883
+ #ifdef HAVE_INT_TIMEZONE
2884
+ sprintf ( (str + 27 ), " %s" , tzname [(tm -> tm_isdst > 0 )]);
2885
+ #else
2886
+ sprintf ( (str + 27 ), " %s" , tm -> tm_zone );
2787
2887
#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 ]));
2888
+ #else
2889
+ sprintf ( (str + 27 ), " %s" , CTZName );
2890
+ #endif
2891
+ };
2790
2892
2791
2893
} else {
2792
2894
sprintf ( (str + 10 ), " %02d:%02d %04d %s" ,
@@ -2798,7 +2900,7 @@ printf( "EncodeDateTime- day is %d\n", day);
2798
2900
printf ( "EncodeDateTime- date result is %s\n" , str );
2799
2901
#endif
2800
2902
2801
- #ifdef DATEDEBUG
2903
+ #if defined( DATEDEBUG ) && FALSE
2802
2904
if (tm -> tm_year >= 1000 ) tm -> tm_year -= 1900 ;
2803
2905
tm -> tm_mon -= 1 ;
2804
2906
strftime ( buf , sizeof (buf ), "%y.%m.%d %H:%M:%S %Z" , tm );
0 commit comments