1 /*-------------------------------------------------------------------------
4 * parse almost any absolute date getdate(3) can (& some it can't)
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.36 1997/10/30 14:06:47 momjian Exp $
12 *-------------------------------------------------------------------------
17 #include <sys/types.h>
20 #include <miscadmin.h>
27 #ifndef USE_POSIX_TIME
28 #include <sys/timeb.h>
30 #include "utils/builtins.h"
31 #include "access/xact.h"
33 static AbsoluteTime tm2abstime(struct tm * tm, int tz);
35 #define MIN_DAYNUM -24856 /* December 13, 1901 */
36 #define MAX_DAYNUM 24854 /* January 18, 2038 */
39 /* GetCurrentAbsoluteTime()
40 * Get the current system time. Set timezone parameters if not specified elsewhere.
41 * Define HasTZSet to allow clients to specify the default timezone.
43 * Returns the number of seconds since epoch (January 1 1970 GMT)
46 GetCurrentAbsoluteTime(void)
54 #else /* ! USE_POSIX_TIME */
55 struct timeb tb; /* the old V7-ism */
64 #if defined(HAVE_TZSET) && defined(HAVE_INT_TIMEZONE)
67 CDayLight = tm->tm_isdst;
68 CTimeZone = (tm->tm_isdst ? (timezone - 3600) : timezone);
69 strcpy(CTZName, tzname[tm->tm_isdst]);
70 #else /* !HAVE_TZSET */
73 CTimeZone = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
74 CDayLight = (tm->tm_isdst > 0);
77 * XXX is there a better way to get local timezone string w/o
78 * tzname? - tgl 97/03/18
80 strftime(CTZName, MAXTZLEN, "%Z", tm);
83 * XXX FreeBSD man pages indicate that this should work - tgl
86 strcpy(CTZName, tm->tm_zone);
88 #else /* ! USE_POSIX_TIME */
89 CTimeZone = tb.timezone * 60;
90 CDayLight = (tb.dstflag != 0);
93 * XXX does this work to get the local timezone string in V7? -
96 strftime(CTZName, MAXTZLEN, "%Z", localtime(&now));
101 printf("GetCurrentAbsoluteTime- timezone is %s -> %d seconds from UTC\n",
105 return ((AbsoluteTime) now);
106 } /* GetCurrentAbsoluteTime() */
110 GetCurrentTime(struct tm * tm)
114 abstime2tm(GetCurrentTransactionStartTime(), &tz, tm, NULL);
117 } /* GetCurrentTime() */
121 abstime2tm(AbsoluteTime time, int *tzp, struct tm * tm, char *tzn)
123 #ifdef USE_POSIX_TIME
126 #else /* ! USE_POSIX_TIME */
127 struct timeb tb; /* the old V7-ism */
132 #ifdef USE_POSIX_TIME
135 tx = localtime((time_t *) &time);
139 tx = gmtime((time_t *) &time);
145 #ifdef HAVE_INT_TIMEZONE
146 printf("datetime2tm- (localtime) %d.%02d.%02d %02d:%02d:%02d %s %s dst=%d\n",
147 tx->tm_year, tx->tm_mon, tx->tm_mday, tx->tm_hour, tx->tm_min, tx->tm_sec,
148 tzname[0], tzname[1], tx->tm_isdst);
150 printf("datetime2tm- (localtime) %d.%02d.%02d %02d:%02d:%02d %s dst=%d\n",
151 tx->tm_year, tx->tm_mon, tx->tm_mday, tx->tm_hour, tx->tm_min, tx->tm_sec,
152 tx->tm_zone, tx->tm_isdst);
157 #ifdef USE_POSIX_TIME
159 tm->tm_year = tx->tm_year + 1900;
160 tm->tm_mon = tx->tm_mon + 1;
161 tm->tm_mday = tx->tm_mday;
162 tm->tm_hour = tx->tm_hour;
163 tm->tm_min = tx->tm_min;
164 tm->tm_sec = tx->tm_sec;
165 tm->tm_isdst = tx->tm_isdst;
167 #ifdef HAVE_INT_TIMEZONE
169 *tzp = (tm->tm_isdst ? (timezone - 3600) : timezone);
171 strcpy(tzn, tzname[tm->tm_isdst]);
172 #else /* !HAVE_INT_TIMEZONE */
173 tm->tm_gmtoff = tx->tm_gmtoff;
174 tm->tm_zone = tx->tm_zone;
177 *tzp = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
178 /* XXX FreeBSD man pages indicate that this should work - tgl 97/04/23 */
180 strcpy(tzn, tm->tm_zone);
182 #else /* ! USE_POSIX_TIME */
184 *tzp = tb.timezone * 60;
187 * XXX does this work to get the local timezone string in V7? - tgl
191 strftime(tzn, MAXTZLEN, "%Z", localtime(&now));
199 * Convert a tm structure to abstime.
200 * Note that tm has full year (not 1900-based) and 1-based month.
203 tm2abstime(struct tm * tm, int tz)
208 /* validate, before going out of range on some members */
209 if (tm->tm_year < 1901 || tm->tm_year > 2038
210 || tm->tm_mon < 1 || tm->tm_mon > 12
211 || tm->tm_mday < 1 || tm->tm_mday > 31
212 || tm->tm_hour < 0 || tm->tm_hour >= 24
213 || tm->tm_min < 0 || tm->tm_min > 59
214 || tm->tm_sec < 0 || tm->tm_sec > 59)
215 return (INVALID_ABSTIME);
217 day = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(1970, 1, 1));
219 /* check for time out of range */
220 if ((day < MIN_DAYNUM) || (day > MAX_DAYNUM))
221 return (INVALID_ABSTIME);
223 /* convert to seconds */
224 sec = tm->tm_sec + tz + (tm->tm_min + (day * 24 + tm->tm_hour) * 60) * 60;
226 /* check for overflow */
227 if ((day == MAX_DAYNUM && sec < 0) ||
228 (day == MIN_DAYNUM && sec > 0))
229 return (INVALID_ABSTIME);
231 /* check for reserved values (e.g. "current" on edge of usual range */
232 if (!AbsoluteTimeIsReal(sec))
233 return (INVALID_ABSTIME);
240 * Decode date/time string and return abstime.
243 nabstimein(char *str)
252 char *field[MAXDATEFIELDS];
253 char lowstr[MAXDATELEN + 1];
256 ftype[MAXDATEFIELDS];
258 if (!PointerIsValid(str))
259 elog(WARN, "Bad (null) abstime external representation", NULL);
261 if (strlen(str) > MAXDATELEN)
262 elog(WARN, "Bad (length) abstime external representation '%s'", str);
264 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
265 || (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
266 elog(WARN, "Bad abstime external representation '%s'", str);
269 printf("nabstimein- %d fields are type %d (DTK_DATE=%d)\n", nf, dtype, DTK_DATE);
275 result = tm2abstime(tm, tz);
279 result = EPOCH_ABSTIME;
283 result = CURRENT_ABSTIME;
287 result = NOEND_ABSTIME;
291 result = NOSTART_ABSTIME;
295 result = INVALID_ABSTIME;
299 elog(WARN, "Bad abstime (internal coding error) '%s'", str);
300 result = INVALID_ABSTIME;
309 * Given an AbsoluteTime return the English text version of the date
312 nabstimeout(AbsoluteTime time)
319 char buf[MAXDATELEN + 1];
320 char zone[MAXDATELEN + 1],
328 case INVALID_ABSTIME:
329 strcpy(buf, INVALID);
331 case CURRENT_ABSTIME:
332 strcpy(buf, DCURRENT);
337 case NOSTART_ABSTIME:
341 abstime2tm(time, &tz, tm, tzn);
344 EncodeDateTime(tm, fsec, &tz, &tzn, DateStyle, buf);
348 result = PALLOC(strlen(buf) + 1);
352 } /* nabstimeout() */
356 * AbsoluteTimeIsBefore -- true iff time1 is before time2.
357 * AbsoluteTimeIsBefore -- true iff time1 is after time2.
360 AbsoluteTimeIsBefore(AbsoluteTime time1, AbsoluteTime time2)
362 Assert(AbsoluteTimeIsValid(time1));
363 Assert(AbsoluteTimeIsValid(time2));
365 if (time1 == CURRENT_ABSTIME)
366 time1 = GetCurrentTransactionStartTime();
368 if (time2 == CURRENT_ABSTIME)
369 time2 = GetCurrentTransactionStartTime();
371 return (time1 < time2);
375 AbsoluteTimeIsAfter(AbsoluteTime time1, AbsoluteTime time2)
377 Assert(AbsoluteTimeIsValid(time1));
378 Assert(AbsoluteTimeIsValid(time2));
380 if (time1 == CURRENT_ABSTIME)
381 time1 = GetCurrentTransactionStartTime();
383 if (time2 == CURRENT_ABSTIME)
384 time2 = GetCurrentTransactionStartTime();
386 return (time1 > time2);
393 abstime_finite(AbsoluteTime abstime)
395 return ((abstime != INVALID_ABSTIME)
396 && (abstime != NOSTART_ABSTIME) && (abstime != NOEND_ABSTIME));
397 } /* abstime_finite() */
401 * abstimeeq - returns 1, iff arguments are equal
402 * abstimene - returns 1, iff arguments are not equal
403 * abstimelt - returns 1, iff t1 less than t2
404 * abstimegt - returns 1, iff t1 greater than t2
405 * abstimele - returns 1, iff t1 less than or equal to t2
406 * abstimege - returns 1, iff t1 greater than or equal to t2
409 abstimeeq(AbsoluteTime t1, AbsoluteTime t2)
411 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
413 if (t1 == CURRENT_ABSTIME)
414 t1 = GetCurrentTransactionStartTime();
415 if (t2 == CURRENT_ABSTIME)
416 t2 = GetCurrentTransactionStartTime();
422 abstimene(AbsoluteTime t1, AbsoluteTime t2)
424 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
426 if (t1 == CURRENT_ABSTIME)
427 t1 = GetCurrentTransactionStartTime();
428 if (t2 == CURRENT_ABSTIME)
429 t2 = GetCurrentTransactionStartTime();
435 abstimelt(AbsoluteTime t1, AbsoluteTime t2)
437 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
439 if (t1 == CURRENT_ABSTIME)
440 t1 = GetCurrentTransactionStartTime();
441 if (t2 == CURRENT_ABSTIME)
442 t2 = GetCurrentTransactionStartTime();
448 abstimegt(AbsoluteTime t1, AbsoluteTime t2)
450 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
452 if (t1 == CURRENT_ABSTIME)
453 t1 = GetCurrentTransactionStartTime();
454 if (t2 == CURRENT_ABSTIME)
455 t2 = GetCurrentTransactionStartTime();
461 abstimele(AbsoluteTime t1, AbsoluteTime t2)
463 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
465 if (t1 == CURRENT_ABSTIME)
466 t1 = GetCurrentTransactionStartTime();
467 if (t2 == CURRENT_ABSTIME)
468 t2 = GetCurrentTransactionStartTime();
474 abstimege(AbsoluteTime t1, AbsoluteTime t2)
476 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
478 if (t1 == CURRENT_ABSTIME)
479 t1 = GetCurrentTransactionStartTime();
480 if (t2 == CURRENT_ABSTIME)
481 t2 = GetCurrentTransactionStartTime();
487 /* datetime_abstime()
488 * Convert datetime to abstime.
491 datetime_abstime(DateTime *datetime)
499 if (!PointerIsValid(datetime))
501 result = INVALID_ABSTIME;
504 else if (DATETIME_IS_INVALID(*datetime))
506 result = INVALID_ABSTIME;
509 else if (DATETIME_IS_NOBEGIN(*datetime))
511 result = NOSTART_ABSTIME;
514 else if (DATETIME_IS_NOEND(*datetime))
516 result = NOEND_ABSTIME;
521 if (DATETIME_IS_RELATIVE(*datetime))
523 datetime2tm(SetDateTime(*datetime), NULL, tm, &fsec, NULL);
524 result = tm2abstime(tm, 0);
527 else if (datetime2tm(*datetime, NULL, tm, &fsec, NULL) == 0)
529 result = tm2abstime(tm, 0);
534 result = INVALID_ABSTIME;
539 } /* datetime_abstime() */
541 /* abstime_datetime()
542 * Convert abstime to datetime.
545 abstime_datetime(AbsoluteTime abstime)
549 if (!PointerIsValid(result = PALLOCTYPE(DateTime)))
550 elog(WARN, "Unable to allocate space to convert abstime to datetime", NULL);
554 case INVALID_ABSTIME:
555 DATETIME_INVALID(*result);
558 case NOSTART_ABSTIME:
559 DATETIME_NOBEGIN(*result);
563 DATETIME_NOEND(*result);
567 DATETIME_EPOCH(*result);
570 case CURRENT_ABSTIME:
571 DATETIME_CURRENT(*result);
575 *result = abstime + ((date2j(1970, 1, 1) - date2j(2000, 1, 1)) * 86400);
580 } /* abstime_datetime() */