Skip to content

Commit 1ba73ed

Browse files
author
Thomas G. Lockhart
committed
Update some reltime code to use new common routines.
Use standard decoder for isreltime().
1 parent 9d8ae79 commit 1ba73ed

File tree

1 file changed

+103
-33
lines changed

1 file changed

+103
-33
lines changed

src/backend/utils/adt/date.c

Lines changed: 103 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* 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 $
1313
*
1414
* NOTES
1515
* This code is actually (almost) unused.
@@ -73,6 +73,7 @@
7373
#define ABSTIMEMIN(t1, t2) abstimele((t1),(t2)) ? (t1) : (t2)
7474
#define ABSTIMEMAX(t1, t2) abstimelt((t1),(t2)) ? (t2) : (t1)
7575

76+
#if FALSE
7677
static char *unit_tab[] = {
7778
"second", "seconds", "minute", "minutes",
7879
"hour", "hours", "day", "days", "week", "weeks",
@@ -85,13 +86,18 @@ static int sec_tab[] = {
8586
1,1, 60, 60,
8687
3600, 3600, 86400, 86400, 604800, 604800,
8788
2592000, 2592000, 31536000, 31536000 };
89+
#endif
8890

8991
/*
9092
* Function prototypes -- internal to this file only
9193
*/
9294

95+
void reltime2tm(int32 time, struct tm *tm);
96+
97+
#if FALSE
9398
static int correct_unit(char unit[], int *unptr);
9499
static int correct_dir(char direction[], int *signptr);
100+
#endif
95101

96102
static int istinterval(char *i_string,
97103
AbsoluteTime *i_start,
@@ -148,8 +154,44 @@ printf( "reltimein- %d fields are type %d (DTK_DATE=%d)\n", nf, dtype, DTK_DATE)
148154
/*
149155
* reltimeout - converts the internal format to a reltime string
150156
*/
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)
152183
{
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
153195
char *timestring;
154196
long quantity;
155197
register int i;
@@ -179,6 +221,7 @@ char *reltimeout(int32 timevalue)
179221
(quantity * -1), unit_tab[unitnr], RELTIME_PAST);
180222
return(timestring);
181223
}
224+
#endif
182225

183226

184227
/*
@@ -241,6 +284,7 @@ RelativeTime
241284
timespan_reltime(TimeSpan *timespan)
242285
{
243286
RelativeTime time;
287+
int year, month;
244288
double span;
245289

246290
if (!PointerIsValid(timespan))
@@ -250,7 +294,20 @@ timespan_reltime(TimeSpan *timespan)
250294
time = INVALID_RELTIME;
251295

252296
} 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);
254311

255312
#ifdef DATEDEBUG
256313
printf( "timespan_reltime- convert m%d s%f to %f [%d %d]\n",
@@ -268,6 +325,7 @@ TimeSpan *
268325
reltime_timespan(RelativeTime reltime)
269326
{
270327
TimeSpan *result;
328+
int year, month;
271329

272330
if (!PointerIsValid(result = PALLOCTYPE(TimeSpan)))
273331
elog(WARN,"Memory allocation failed, can't convert reltime to timespan",NULL);
@@ -278,8 +336,11 @@ reltime_timespan(RelativeTime reltime)
278336
break;
279337

280338
default:
339+
TMODULO(reltime, year, 31536000);
340+
TMODULO(reltime, month, 2592000);
341+
281342
result->time = reltime;
282-
result->month = 0;
343+
result->month = ((12*year)+month);
283344
};
284345

285346
return(result);
@@ -599,38 +660,46 @@ AbsoluteTime intervalend(TimeInterval i)
599660
* isreltime - returns 1, iff datestring is of type reltime
600661
* 2, iff datestring is 'invalid time' identifier
601662
* 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'
630664
*
631665
*/
632-
int isreltime(char *timestring, int *sign, long *quantity, int *unitnr)
666+
int isreltime(char *str)
633667
{
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
634703
register char *p;
635704
register char c;
636705
int i;
@@ -766,6 +835,7 @@ static int correct_dir(char direction[], int *signptr)
766835
} else
767836
return (0); /* invalid direction descriptor */
768837
}
838+
#endif
769839

770840
/*
771841
* istinterval - returns 1, iff i_string is a valid interval descr.

0 commit comments

Comments
 (0)