9
9
*
10
10
*
11
11
* IDENTIFICATION
12
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.9 1997/04/20 21:49:17 scrappy Exp $
12
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.10 1997/07/29 15:54:49 thomas Exp $
13
13
*
14
14
* NOTES
15
15
* This code is actually (almost) unused.
73
73
#define ABSTIMEMIN (t1 , t2 ) abstimele((t1),(t2)) ? (t1) : (t2)
74
74
#define ABSTIMEMAX (t1 , t2 ) abstimelt((t1),(t2)) ? (t2) : (t1)
75
75
76
+ #if FALSE
76
77
static char * unit_tab [] = {
77
78
"second" , "seconds" , "minute" , "minutes" ,
78
79
"hour" , "hours" , "day" , "days" , "week" , "weeks" ,
@@ -85,13 +86,18 @@ static int sec_tab[] = {
85
86
1 ,1 , 60 , 60 ,
86
87
3600 , 3600 , 86400 , 86400 , 604800 , 604800 ,
87
88
2592000 , 2592000 , 31536000 , 31536000 };
89
+ #endif
88
90
89
91
/*
90
92
* Function prototypes -- internal to this file only
91
93
*/
92
94
95
+ void reltime2tm (int32 time , struct tm * tm );
96
+
97
+ #if FALSE
93
98
static int correct_unit (char unit [], int * unptr );
94
99
static int correct_dir (char direction [], int * signptr );
100
+ #endif
95
101
96
102
static int istinterval (char * i_string ,
97
103
AbsoluteTime * i_start ,
@@ -148,8 +154,44 @@ printf( "reltimein- %d fields are type %d (DTK_DATE=%d)\n", nf, dtype, DTK_DATE)
148
154
/*
149
155
* reltimeout - converts the internal format to a reltime string
150
156
*/
151
- char * reltimeout (int32 timevalue )
157
+ char * reltimeout (int32 time )
158
+ {
159
+ char * result ;
160
+ struct tm tt , * tm = & tt ;
161
+ char buf [MAXDATELEN + 1 ];
162
+
163
+ if (time == INVALID_RELTIME ) {
164
+ strcpy ( buf , INVALID_RELTIME_STR );
165
+
166
+ } else {
167
+ reltime2tm (time , tm );
168
+ EncodeTimeSpan ( tm , 0 , DateStyle , buf );
169
+ };
170
+
171
+ result = PALLOC (strlen (buf )+ 1 );
172
+ strcpy ( result , buf );
173
+
174
+ return (result );
175
+ } /* reltimeout() */
176
+
177
+
178
+ #define TMODULO (t ,q ,u ) {q = (t / u); \
179
+ if (q != 0) t -= (q * u);}
180
+
181
+ void
182
+ reltime2tm (int32 time , struct tm * tm )
152
183
{
184
+ TMODULO (time , tm -> tm_year , 31536000 );
185
+ TMODULO (time , tm -> tm_mon , 2592000 );
186
+ TMODULO (time , tm -> tm_mday , 86400 );
187
+ TMODULO (time , tm -> tm_hour , 3600 );
188
+ TMODULO (time , tm -> tm_min , 60 );
189
+ TMODULO (time , tm -> tm_sec , 1 );
190
+
191
+ return ;
192
+ } /* reltime2tm() */
193
+
194
+ #if FALSE
153
195
char * timestring ;
154
196
long quantity ;
155
197
register int i ;
@@ -179,6 +221,7 @@ char *reltimeout(int32 timevalue)
179
221
(quantity * -1 ), unit_tab [unitnr ], RELTIME_PAST );
180
222
return (timestring );
181
223
}
224
+ #endif
182
225
183
226
184
227
/*
@@ -241,6 +284,7 @@ RelativeTime
241
284
timespan_reltime (TimeSpan * timespan )
242
285
{
243
286
RelativeTime time ;
287
+ int year , month ;
244
288
double span ;
245
289
246
290
if (!PointerIsValid (timespan ))
@@ -250,7 +294,20 @@ timespan_reltime(TimeSpan *timespan)
250
294
time = INVALID_RELTIME ;
251
295
252
296
} else {
253
- span = ((((double ) 30 * 86400 )* timespan -> month ) + timespan -> time );
297
+ if (timespan -> month == 0 ) {
298
+ year = 0 ;
299
+ month = 0 ;
300
+
301
+ } else if (abs (timespan -> month ) >= 12 ) {
302
+ year = (timespan -> month / 12 );
303
+ month = (timespan -> month % 12 );
304
+
305
+ } else {
306
+ year = 0 ;
307
+ month = timespan -> month ;
308
+ };
309
+
310
+ span = (((((double ) 365 * year )+ ((double ) 30 * month ))* 86400 ) + timespan -> time );
254
311
255
312
#ifdef DATEDEBUG
256
313
printf ( "timespan_reltime- convert m%d s%f to %f [%d %d]\n" ,
@@ -268,6 +325,7 @@ TimeSpan *
268
325
reltime_timespan (RelativeTime reltime )
269
326
{
270
327
TimeSpan * result ;
328
+ int year , month ;
271
329
272
330
if (!PointerIsValid (result = PALLOCTYPE (TimeSpan )))
273
331
elog (WARN ,"Memory allocation failed, can't convert reltime to timespan" ,NULL );
@@ -278,8 +336,11 @@ reltime_timespan(RelativeTime reltime)
278
336
break ;
279
337
280
338
default :
339
+ TMODULO (reltime , year , 31536000 );
340
+ TMODULO (reltime , month , 2592000 );
341
+
281
342
result -> time = reltime ;
282
- result -> month = 0 ;
343
+ result -> month = (( 12 * year ) + month ) ;
283
344
};
284
345
285
346
return (result );
@@ -599,38 +660,46 @@ AbsoluteTime intervalend(TimeInterval i)
599
660
* isreltime - returns 1, iff datestring is of type reltime
600
661
* 2, iff datestring is 'invalid time' identifier
601
662
* 0, iff datestring contains a syntax error
602
- *
603
- * output parameter:
604
- * sign = -1, iff direction is 'ago'
605
- * else sign = 1.
606
- * quantity : quantity of unit
607
- * unitnr : 0 or 1 ... sec
608
- * 2 or 3 ... min
609
- * 4 or 5 ... hour
610
- * 6 or 7 ... day
611
- * 8 or 9 ... week
612
- * 10 or 11... month
613
- * 12 or 13... year
614
- *
615
- *
616
- * Relative time:
617
- *
618
- * `@' ` ' Quantity ` ' Unit [ ` ' Direction]
619
- *
620
- * OR `Undefined RelTime' (see also INVALID_RELTIME_STR)
621
- *
622
- * where
623
- * Quantity is `1', `2', ...
624
- * Unit is `second', `minute', `hour', `day', `week',
625
- * `month' (30-days), or `year' (365-days),
626
- * or PLURAL of these units.
627
- * Direction is `ago'
628
- *
629
- * VALID time less or equal `@ 68 years'
663
+ * VALID time less or equal +/- `@ 68 years'
630
664
*
631
665
*/
632
- int isreltime (char * timestring , int * sign , long * quantity , int * unitnr )
666
+ int isreltime (char * str )
633
667
{
668
+ struct tm tt , * tm = & tt ;
669
+ double fsec ;
670
+ int dtype ;
671
+ char * field [MAXDATEFIELDS ];
672
+ int nf , ftype [MAXDATEFIELDS ];
673
+ char lowstr [MAXDATELEN + 1 ];
674
+
675
+ if (!PointerIsValid (str ))
676
+ return 0 ;
677
+
678
+ if (strlen (str ) > MAXDATELEN )
679
+ return 0 ;
680
+
681
+ if ((ParseDateTime ( str , lowstr , field , ftype , MAXDATEFIELDS , & nf ) != 0 )
682
+ || (DecodeDateDelta ( field , ftype , nf , & dtype , tm , & fsec ) != 0 ))
683
+ return 0 ;
684
+
685
+ switch (dtype ) {
686
+ case (DTK_DELTA ):
687
+ return ((abs (tm -> tm_year ) <= 68 )? 1 : 0 );
688
+ break ;
689
+
690
+ case (DTK_INVALID ):
691
+ return 2 ;
692
+ break ;
693
+
694
+ default :
695
+ return 0 ;
696
+ break ;
697
+ };
698
+
699
+ return 0 ;
700
+ } /* isreltime() */
701
+
702
+ #if FALSE
634
703
register char * p ;
635
704
register char c ;
636
705
int i ;
@@ -766,6 +835,7 @@ static int correct_dir(char direction[], int *signptr)
766
835
} else
767
836
return (0 ); /* invalid direction descriptor */
768
837
}
838
+ #endif
769
839
770
840
/*
771
841
* istinterval - returns 1, iff i_string is a valid interval descr.
0 commit comments