7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.61 1999/01/10 17:20:54 thomas Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.62 1999/01/20 16:29:39 thomas Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
31
31
#endif
32
32
#include "utils/builtins.h"
33
33
34
+
34
35
static int DecodeDate (char * str , int fmask , int * tmask , struct tm * tm );
35
36
static int DecodeNumber (int flen , char * field ,
36
- int fmask , int * tmask , struct tm * tm , double * fsec );
37
+ int fmask , int * tmask , struct tm * tm , double * fsec , int * is2digits );
37
38
static int DecodeNumberField (int len , char * str ,
38
- int fmask , int * tmask , struct tm * tm , double * fsec );
39
+ int fmask , int * tmask , struct tm * tm , double * fsec , int * is2digits );
39
40
static int DecodeSpecial (int field , char * lowtoken , int * val );
40
41
static int DecodeTime (char * str , int fmask , int * tmask ,
41
42
struct tm * tm , double * fsec );
@@ -50,12 +51,20 @@ static double time2t(const int hour, const int min, const double sec);
50
51
static int timespan2tm (TimeSpan span , struct tm * tm , float8 * fsec );
51
52
static int tm2timespan (struct tm * tm , double fsec , TimeSpan * span );
52
53
54
+
53
55
#define USE_DATE_CACHE 1
54
56
#define ROUND_ALL 0
55
57
58
+ #if 0
56
59
#define isleap (y ) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
57
60
58
61
int mdays [] = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 , 0 };
62
+ #endif
63
+
64
+ int day_tab [2 ][13 ] = {
65
+ {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 , 0 },
66
+ {31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 , 0 }};
67
+
59
68
60
69
char * months [] = {"Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" ,
61
70
"Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" , NULL };
@@ -873,13 +882,17 @@ datetime_pl_span(DateTime *datetime, TimeSpan *span)
873
882
}
874
883
875
884
/* adjust for end of month boundary problems... */
885
+ #if 0
876
886
if (tm -> tm_mday > mdays [tm -> tm_mon - 1 ])
877
887
{
878
888
if ((tm -> tm_mon == 2 ) && isleap (tm -> tm_year ))
879
889
tm -> tm_mday = (mdays [tm -> tm_mon - 1 ] + 1 );
880
890
else
881
891
tm -> tm_mday = mdays [tm -> tm_mon - 1 ];
882
892
}
893
+ #endif
894
+ if (tm -> tm_mday > day_tab [isleap (tm -> tm_year )][tm -> tm_mon - 1 ])
895
+ tm -> tm_mday = (day_tab [isleap (tm -> tm_year )][tm -> tm_mon - 1 ]);
883
896
884
897
#ifdef DATEDEBUG
885
898
printf ("datetime_pl_span- date becomes %04d-%02d-%02d %02d:%02d:%02d\n" ,
@@ -1184,16 +1197,22 @@ datetime_age(DateTime *datetime1, DateTime *datetime2)
1184
1197
{
1185
1198
if (dt1 < dt2 )
1186
1199
{
1200
+ #if 0
1187
1201
tm -> tm_mday += mdays [tm1 -> tm_mon - 1 ];
1188
1202
if (isleap (tm1 -> tm_year ) && (tm1 -> tm_mon == 2 ))
1189
1203
tm -> tm_mday ++ ;
1204
+ #endif
1205
+ tm -> tm_mday += day_tab [isleap (tm1 -> tm_year )][tm1 -> tm_mon - 1 ];
1190
1206
tm -> tm_mon -- ;
1191
1207
}
1192
1208
else
1193
1209
{
1210
+ #if 0
1194
1211
tm -> tm_mday += mdays [tm2 -> tm_mon - 1 ];
1195
1212
if (isleap (tm2 -> tm_year ) && (tm2 -> tm_mon == 2 ))
1196
1213
tm -> tm_mday ++ ;
1214
+ #endif
1215
+ tm -> tm_mday += day_tab [isleap (tm2 -> tm_year )][tm2 -> tm_mon - 1 ];
1197
1216
tm -> tm_mon -- ;
1198
1217
}
1199
1218
}
@@ -2036,7 +2055,7 @@ static datetkn datetktbl[] = {
2036
2055
{"adt" , DTZ , NEG (18 )}, /* Atlantic Daylight Time */
2037
2056
{"aesst" , DTZ , 66 }, /* E. Australia */
2038
2057
{"aest" , TZ , 60 }, /* Australia Eastern Std Time */
2039
- {"ahst" , TZ , 60 }, /* Alaska-Hawaii Std Time */
2058
+ {"ahst" , TZ , NEG ( 60 )}, /* Alaska-Hawaii Std Time */
2040
2059
{"allballs" , RESERV , DTK_ZULU }, /* 00:00:00 */
2041
2060
{"am" , AMPM , AM },
2042
2061
{"apr" , MONTH , 4 },
@@ -2087,12 +2106,12 @@ static datetkn datetktbl[] = {
2087
2106
{"hmt" , DTZ , 18 }, /* Hellas ? ? */
2088
2107
{"hst" , TZ , NEG (60 )}, /* Hawaii Std Time */
2089
2108
{"idle" , TZ , 72 }, /* Intl. Date Line, East */
2090
- {"idlw" , TZ , NEG (72 )}, /* Intl. Date Line,, est */
2109
+ {"idlw" , TZ , NEG (72 )}, /* Intl. Date Line, West */
2091
2110
{LATE , RESERV , DTK_LATE }, /* "infinity" reserved for "late time" */
2092
- {INVALID , RESERV , DTK_INVALID }, /* "invalid" reserved for invalid
2093
- * time */
2111
+ {INVALID , RESERV , DTK_INVALID },
2112
+ /* "invalid" reserved for invalid time */
2094
2113
{"ist" , TZ , 12 }, /* Israel */
2095
- {"it" , TZ , 22 }, /* Iran Time */
2114
+ {"it" , TZ , 21 }, /* Iran Time */
2096
2115
{"jan" , MONTH , 1 },
2097
2116
{"january" , MONTH , 1 },
2098
2117
{"jst" , TZ , 54 }, /* Japan Std Time,USSR Zone 8 */
@@ -2283,6 +2302,8 @@ datetkn *deltacache[MAXDATEFIELDS] = {NULL};
2283
2302
* These routines will be used by other date/time packages - tgl 97/02/25
2284
2303
*/
2285
2304
2305
+ #if 0
2306
+ XXX moved to dt .h - thomas 1999 - 01 - 15
2286
2307
/* Set the minimum year to one greater than the year of the first valid day
2287
2308
* to avoid having to check year and day both. - tgl 97/05/08
2288
2309
*/
@@ -2294,6 +2315,7 @@ datetkn *deltacache[MAXDATEFIELDS] = {NULL};
2294
2315
#define IS_VALID_JULIAN (y ,m ,d ) ((y > JULIAN_MINYEAR) \
2295
2316
|| ((y == JULIAN_MINYEAR) && ((m > JULIAN_MINMONTH) \
2296
2317
|| ((m == JULIAN_MINMONTH) && (d >= JULIAN_MINDAY)))))
2318
+ #endif
2297
2319
2298
2320
int
2299
2321
date2j (int y , int m , int d )
@@ -2792,6 +2814,7 @@ DecodeDateTime(char **field, int *ftype, int nf,
2792
2814
int flen ,
2793
2815
val ;
2794
2816
int mer = HR24 ;
2817
+ int is2digits = FALSE;
2795
2818
int bc = FALSE;
2796
2819
2797
2820
* dtype = DTK_DATE ;
@@ -2843,14 +2866,14 @@ DecodeDateTime(char **field, int *ftype, int nf,
2843
2866
* then interpret as a concatenated date or time... */
2844
2867
if ((flen > 4 ) && !((fmask & DTK_DATE_M ) && (fmask & DTK_TIME_M )))
2845
2868
{
2846
- if (DecodeNumberField (flen , field [i ], fmask , & tmask , tm , fsec ) != 0 )
2869
+ if (DecodeNumberField (flen , field [i ], fmask , & tmask , tm , fsec , & is2digits ) != 0 )
2847
2870
return -1 ;
2848
2871
2849
2872
}
2850
2873
/* otherwise it is a single date/time field... */
2851
2874
else
2852
2875
{
2853
- if (DecodeNumber (flen , field [i ], fmask , & tmask , tm , fsec ) != 0 )
2876
+ if (DecodeNumber (flen , field [i ], fmask , & tmask , tm , fsec , & is2digits ) != 0 )
2854
2877
return -1 ;
2855
2878
}
2856
2879
break ;
@@ -3009,6 +3032,13 @@ DecodeDateTime(char **field, int *ftype, int nf,
3009
3032
else
3010
3033
elog (ERROR ,"Inconsistant use of year %04d and 'BC'" , tm -> tm_year );
3011
3034
}
3035
+ else if (is2digits )
3036
+ {
3037
+ if (tm -> tm_year < 70 )
3038
+ tm -> tm_year += 2000 ;
3039
+ else if (tm -> tm_year < 100 )
3040
+ tm -> tm_year += 1900 ;
3041
+ }
3012
3042
3013
3043
if ((mer != HR24 ) && (tm -> tm_hour > 12 ))
3014
3044
return -1 ;
@@ -3083,6 +3113,7 @@ DecodeTimeOnly(char **field, int *ftype, int nf, int *dtype, struct tm * tm, dou
3083
3113
int i ;
3084
3114
int flen ,
3085
3115
val ;
3116
+ int is2digits = FALSE;
3086
3117
int mer = HR24 ;
3087
3118
3088
3119
* dtype = DTK_TIME ;
@@ -3110,7 +3141,7 @@ DecodeTimeOnly(char **field, int *ftype, int nf, int *dtype, struct tm * tm, dou
3110
3141
case DTK_NUMBER :
3111
3142
flen = strlen (field [i ]);
3112
3143
3113
- if (DecodeNumberField (flen , field [i ], fmask , & tmask , tm , fsec ) != 0 )
3144
+ if (DecodeNumberField (flen , field [i ], fmask , & tmask , tm , fsec , & is2digits ) != 0 )
3114
3145
return -1 ;
3115
3146
break ;
3116
3147
@@ -3209,6 +3240,8 @@ DecodeDate(char *str, int fmask, int *tmask, struct tm * tm)
3209
3240
int nf = 0 ;
3210
3241
int i ,
3211
3242
len ;
3243
+ int bc = FALSE;
3244
+ int is2digits = FALSE;
3212
3245
int type ,
3213
3246
val ,
3214
3247
dmask = 0 ;
@@ -3238,9 +3271,11 @@ DecodeDate(char *str, int fmask, int *tmask, struct tm * tm)
3238
3271
nf ++ ;
3239
3272
}
3240
3273
3274
+ #if 0
3241
3275
/* don't allow too many fields */
3242
3276
if (nf > 3 )
3243
3277
return -1 ;
3278
+ #endif
3244
3279
3245
3280
* tmask = 0 ;
3246
3281
@@ -3263,6 +3298,10 @@ DecodeDate(char *str, int fmask, int *tmask, struct tm * tm)
3263
3298
tm -> tm_mon = val ;
3264
3299
break ;
3265
3300
3301
+ case ADBC :
3302
+ bc = (val == BC );
3303
+ break ;
3304
+
3266
3305
default :
3267
3306
#ifdef DATEDEBUG
3268
3307
printf ("DecodeDate- illegal field %s value is %d\n" , field [i ], val );
@@ -3289,7 +3328,7 @@ DecodeDate(char *str, int fmask, int *tmask, struct tm * tm)
3289
3328
if ((len = strlen (field [i ])) <= 0 )
3290
3329
return -1 ;
3291
3330
3292
- if (DecodeNumber (len , field [i ], fmask , & dmask , tm , & fsec ) != 0 )
3331
+ if (DecodeNumber (len , field [i ], fmask , & dmask , tm , & fsec , & is2digits ) != 0 )
3293
3332
return -1 ;
3294
3333
3295
3334
if (fmask & dmask )
@@ -3299,6 +3338,25 @@ DecodeDate(char *str, int fmask, int *tmask, struct tm * tm)
3299
3338
* tmask |= dmask ;
3300
3339
}
3301
3340
3341
+ if ((fmask & ~(DTK_M (DOY ) | DTK_M (TZ ))) != DTK_DATE_M )
3342
+ return -1 ;
3343
+
3344
+ /* there is no year zero in AD/BC notation; i.e. "1 BC" == year 0 */
3345
+ if (bc )
3346
+ {
3347
+ if (tm -> tm_year > 0 )
3348
+ tm -> tm_year = - (tm -> tm_year - 1 );
3349
+ else
3350
+ elog (ERROR ,"Inconsistant use of year %04d and 'BC'" , tm -> tm_year );
3351
+ }
3352
+ else if (is2digits )
3353
+ {
3354
+ if (tm -> tm_year < 70 )
3355
+ tm -> tm_year += 2000 ;
3356
+ else if (tm -> tm_year < 100 )
3357
+ tm -> tm_year += 1900 ;
3358
+ }
3359
+
3302
3360
return 0 ;
3303
3361
} /* DecodeDate() */
3304
3362
@@ -3362,7 +3420,8 @@ DecodeTime(char *str, int fmask, int *tmask, struct tm * tm, double *fsec)
3362
3420
* Interpret numeric field as a date value in context.
3363
3421
*/
3364
3422
static int
3365
- DecodeNumber (int flen , char * str , int fmask , int * tmask , struct tm * tm , double * fsec )
3423
+ DecodeNumber (int flen , char * str , int fmask ,
3424
+ int * tmask , struct tm * tm , double * fsec , int * is2digits )
3366
3425
{
3367
3426
int val ;
3368
3427
char * cp ;
@@ -3475,13 +3534,16 @@ DecodeNumber(int flen, char *str, int fmask, int *tmask, struct tm * tm, double
3475
3534
tm -> tm_year = val ;
3476
3535
3477
3536
/* adjust ONLY if exactly two digits... */
3537
+ #if 0
3478
3538
if (flen == 2 )
3479
3539
{
3480
3540
if (tm -> tm_year < 70 )
3481
3541
tm -> tm_year += 2000 ;
3482
3542
else if (tm -> tm_year < 100 )
3483
3543
tm -> tm_year += 1900 ;
3484
3544
}
3545
+ #endif
3546
+ * is2digits = (flen == 2 );
3485
3547
3486
3548
}
3487
3549
else
@@ -3495,7 +3557,8 @@ DecodeNumber(int flen, char *str, int fmask, int *tmask, struct tm * tm, double
3495
3557
* Interpret numeric string as a concatenated date field.
3496
3558
*/
3497
3559
static int
3498
- DecodeNumberField (int len , char * str , int fmask , int * tmask , struct tm * tm , double * fsec )
3560
+ DecodeNumberField (int len , char * str , int fmask ,
3561
+ int * tmask , struct tm * tm , double * fsec , int * is2digits )
3499
3562
{
3500
3563
char * cp ;
3501
3564
@@ -3546,10 +3609,13 @@ DecodeNumberField(int len, char *str, int fmask, int *tmask, struct tm * tm, dou
3546
3609
* (str + 2 ) = '\0' ;
3547
3610
tm -> tm_year = atoi (str + 0 );
3548
3611
3612
+ #if 0
3549
3613
if (tm -> tm_year < 70 )
3550
3614
tm -> tm_year += 2000 ;
3551
3615
else if (tm -> tm_year < 100 )
3552
3616
tm -> tm_year += 1900 ;
3617
+ #endif
3618
+ * is2digits = TRUE;
3553
3619
}
3554
3620
3555
3621
}
@@ -3564,10 +3630,13 @@ DecodeNumberField(int len, char *str, int fmask, int *tmask, struct tm * tm, dou
3564
3630
tm -> tm_mon = 1 ;
3565
3631
tm -> tm_year = atoi (str + 0 );
3566
3632
3633
+ #if 0
3567
3634
if (tm -> tm_year < 70 )
3568
3635
tm -> tm_year += 2000 ;
3569
3636
else if (tm -> tm_year < 100 )
3570
3637
tm -> tm_year += 1900 ;
3638
+ #endif
3639
+ * is2digits = TRUE;
3571
3640
}
3572
3641
else if (strchr (str , '.' ) != NULL )
3573
3642
{
0 commit comments