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.44 1998/07/19 10:08:15 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);
82 #else /* ! USE_POSIX_TIME */
83 CTimeZone = tb.timezone * 60;
84 CDayLight = (tb.dstflag != 0);
87 * XXX does this work to get the local timezone string in V7? -
90 strftime(CTZName, MAXTZLEN, "%Z", localtime(&now));
95 printf("GetCurrentAbsoluteTime- timezone is %s -> %d seconds from UTC\n",
99 return ((AbsoluteTime) now);
100 } /* GetCurrentAbsoluteTime() */
104 GetCurrentTime(struct tm * tm)
108 abstime2tm(GetCurrentTransactionStartTime(), &tz, tm, NULL);
111 } /* GetCurrentTime() */
115 abstime2tm(AbsoluteTime time, int *tzp, struct tm * tm, char *tzn)
117 #ifdef USE_POSIX_TIME
120 #else /* ! USE_POSIX_TIME */
121 struct timeb tb; /* the old V7-ism */
126 #ifdef USE_POSIX_TIME
128 tx = localtime((time_t *) &time);
131 tx = gmtime((time_t *) &time);
135 #if defined(DATEDEBUG)
136 #if defined(HAVE_INT_TIMEZONE)
137 printf("datetime2tm- (localtime) %d.%02d.%02d %02d:%02d:%02d %s %s dst=%d\n",
138 tx->tm_year, tx->tm_mon, tx->tm_mday, tx->tm_hour, tx->tm_min, tx->tm_sec,
139 tzname[0], tzname[1], tx->tm_isdst);
141 printf("datetime2tm- (localtime) %d.%02d.%02d %02d:%02d:%02d %s dst=%d\n",
142 tx->tm_year, tx->tm_mon, tx->tm_mday, tx->tm_hour, tx->tm_min, tx->tm_sec,
143 tx->tm_zone, tx->tm_isdst);
147 #ifdef USE_POSIX_TIME
149 tm->tm_year = tx->tm_year + 1900;
150 tm->tm_mon = tx->tm_mon + 1;
151 tm->tm_mday = tx->tm_mday;
152 tm->tm_hour = tx->tm_hour;
153 tm->tm_min = tx->tm_min;
154 tm->tm_sec = tx->tm_sec;
155 tm->tm_isdst = tx->tm_isdst;
157 #ifdef HAVE_INT_TIMEZONE
159 *tzp = (tm->tm_isdst ? (timezone - 3600) : timezone);
161 strcpy(tzn, tzname[tm->tm_isdst]);
162 #else /* !HAVE_INT_TIMEZONE */
163 tm->tm_gmtoff = tx->tm_gmtoff;
164 tm->tm_zone = tx->tm_zone;
167 *tzp = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
168 /* XXX FreeBSD man pages indicate that this should work - tgl 97/04/23 */
170 strcpy(tzn, tm->tm_zone);
172 #else /* ! USE_POSIX_TIME */
174 *tzp = tb.timezone * 60;
177 * XXX does this work to get the local timezone string in V7? - tgl
181 strftime(tzn, MAXTZLEN, "%Z", localtime(&now));
189 * Convert a tm structure to abstime.
190 * Note that tm has full year (not 1900-based) and 1-based month.
193 tm2abstime(struct tm * tm, int tz)
198 /* validate, before going out of range on some members */
199 if (tm->tm_year < 1901 || tm->tm_year > 2038
200 || tm->tm_mon < 1 || tm->tm_mon > 12
201 || tm->tm_mday < 1 || tm->tm_mday > 31
202 || tm->tm_hour < 0 || tm->tm_hour >= 24
203 || tm->tm_min < 0 || tm->tm_min > 59
204 || tm->tm_sec < 0 || tm->tm_sec > 59)
205 return (INVALID_ABSTIME);
207 day = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(1970, 1, 1));
209 /* check for time out of range */
210 if ((day < MIN_DAYNUM) || (day > MAX_DAYNUM))
211 return (INVALID_ABSTIME);
213 /* convert to seconds */
214 sec = tm->tm_sec + tz + (tm->tm_min + (day * 24 + tm->tm_hour) * 60) * 60;
216 /* check for overflow */
217 if ((day == MAX_DAYNUM && sec < 0) ||
218 (day == MIN_DAYNUM && sec > 0))
219 return (INVALID_ABSTIME);
221 /* check for reserved values (e.g. "current" on edge of usual range */
222 if (!AbsoluteTimeIsReal(sec))
223 return (INVALID_ABSTIME);
230 * Decode date/time string and return abstime.
233 nabstimein(char *str)
242 char *field[MAXDATEFIELDS];
243 char lowstr[MAXDATELEN + 1];
246 ftype[MAXDATEFIELDS];
248 if (!PointerIsValid(str))
249 elog(ERROR, "Bad (null) abstime external representation", NULL);
251 if (strlen(str) > MAXDATELEN)
252 elog(ERROR, "Bad (length) abstime external representation '%s'", str);
254 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
255 || (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
256 elog(ERROR, "Bad abstime external representation '%s'", str);
259 printf("nabstimein- %d fields are type %d (DTK_DATE=%d)\n", nf, dtype, DTK_DATE);
265 result = tm2abstime(tm, tz);
269 result = EPOCH_ABSTIME;
273 result = CURRENT_ABSTIME;
277 result = NOEND_ABSTIME;
281 result = NOSTART_ABSTIME;
285 result = INVALID_ABSTIME;
289 elog(ERROR, "Bad abstime (internal coding error) '%s'", str);
290 result = INVALID_ABSTIME;
299 * Given an AbsoluteTime return the English text version of the date
302 nabstimeout(AbsoluteTime time)
309 char buf[MAXDATELEN + 1];
310 char zone[MAXDATELEN + 1],
318 case INVALID_ABSTIME:
319 strcpy(buf, INVALID);
321 case CURRENT_ABSTIME:
322 strcpy(buf, DCURRENT);
327 case NOSTART_ABSTIME:
331 abstime2tm(time, &tz, tm, tzn);
334 EncodeDateTime(tm, fsec, &tz, &tzn, DateStyle, buf);
338 result = palloc(strlen(buf) + 1);
342 } /* nabstimeout() */
346 * AbsoluteTimeIsBefore -- true iff time1 is before time2.
347 * AbsoluteTimeIsBefore -- true iff time1 is after time2.
350 AbsoluteTimeIsBefore(AbsoluteTime time1, AbsoluteTime time2)
352 Assert(AbsoluteTimeIsValid(time1));
353 Assert(AbsoluteTimeIsValid(time2));
355 if (time1 == CURRENT_ABSTIME)
356 time1 = GetCurrentTransactionStartTime();
358 if (time2 == CURRENT_ABSTIME)
359 time2 = GetCurrentTransactionStartTime();
361 return (time1 < time2);
365 AbsoluteTimeIsAfter(AbsoluteTime time1, AbsoluteTime time2)
367 Assert(AbsoluteTimeIsValid(time1));
368 Assert(AbsoluteTimeIsValid(time2));
370 if (time1 == CURRENT_ABSTIME)
371 time1 = GetCurrentTransactionStartTime();
373 if (time2 == CURRENT_ABSTIME)
374 time2 = GetCurrentTransactionStartTime();
376 return (time1 > time2);
383 abstime_finite(AbsoluteTime abstime)
385 return ((abstime != INVALID_ABSTIME)
386 && (abstime != NOSTART_ABSTIME) && (abstime != NOEND_ABSTIME));
387 } /* abstime_finite() */
391 * abstimeeq - returns 1, iff arguments are equal
392 * abstimene - returns 1, iff arguments are not equal
393 * abstimelt - returns 1, iff t1 less than t2
394 * abstimegt - returns 1, iff t1 greater than t2
395 * abstimele - returns 1, iff t1 less than or equal to t2
396 * abstimege - returns 1, iff t1 greater than or equal to t2
399 abstimeeq(AbsoluteTime t1, AbsoluteTime t2)
401 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
403 if (t1 == CURRENT_ABSTIME)
404 t1 = GetCurrentTransactionStartTime();
405 if (t2 == CURRENT_ABSTIME)
406 t2 = GetCurrentTransactionStartTime();
412 abstimene(AbsoluteTime t1, AbsoluteTime t2)
414 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
416 if (t1 == CURRENT_ABSTIME)
417 t1 = GetCurrentTransactionStartTime();
418 if (t2 == CURRENT_ABSTIME)
419 t2 = GetCurrentTransactionStartTime();
425 abstimelt(AbsoluteTime t1, AbsoluteTime t2)
427 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
429 if (t1 == CURRENT_ABSTIME)
430 t1 = GetCurrentTransactionStartTime();
431 if (t2 == CURRENT_ABSTIME)
432 t2 = GetCurrentTransactionStartTime();
438 abstimegt(AbsoluteTime t1, AbsoluteTime t2)
440 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
442 if (t1 == CURRENT_ABSTIME)
443 t1 = GetCurrentTransactionStartTime();
444 if (t2 == CURRENT_ABSTIME)
445 t2 = GetCurrentTransactionStartTime();
451 abstimele(AbsoluteTime t1, AbsoluteTime t2)
453 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
455 if (t1 == CURRENT_ABSTIME)
456 t1 = GetCurrentTransactionStartTime();
457 if (t2 == CURRENT_ABSTIME)
458 t2 = GetCurrentTransactionStartTime();
464 abstimege(AbsoluteTime t1, AbsoluteTime t2)
466 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
468 if (t1 == CURRENT_ABSTIME)
469 t1 = GetCurrentTransactionStartTime();
470 if (t2 == CURRENT_ABSTIME)
471 t2 = GetCurrentTransactionStartTime();
477 /* datetime_abstime()
478 * Convert datetime to abstime.
481 datetime_abstime(DateTime *datetime)
489 if (!PointerIsValid(datetime))
491 result = INVALID_ABSTIME;
494 else if (DATETIME_IS_INVALID(*datetime))
496 result = INVALID_ABSTIME;
499 else if (DATETIME_IS_NOBEGIN(*datetime))
501 result = NOSTART_ABSTIME;
504 else if (DATETIME_IS_NOEND(*datetime))
506 result = NOEND_ABSTIME;
511 if (DATETIME_IS_RELATIVE(*datetime))
513 datetime2tm(SetDateTime(*datetime), NULL, tm, &fsec, NULL);
514 result = tm2abstime(tm, 0);
517 else if (datetime2tm(*datetime, NULL, tm, &fsec, NULL) == 0)
519 result = tm2abstime(tm, 0);
524 result = INVALID_ABSTIME;
529 } /* datetime_abstime() */
531 /* abstime_datetime()
532 * Convert abstime to datetime.
535 abstime_datetime(AbsoluteTime abstime)
539 if (!PointerIsValid(result = palloc(sizeof(DateTime))))
540 elog(ERROR, "Unable to allocate space to convert abstime to datetime", NULL);
544 case INVALID_ABSTIME:
545 DATETIME_INVALID(*result);
548 case NOSTART_ABSTIME:
549 DATETIME_NOBEGIN(*result);
553 DATETIME_NOEND(*result);
557 DATETIME_EPOCH(*result);
560 case CURRENT_ABSTIME:
561 DATETIME_CURRENT(*result);
565 *result = abstime + ((date2j(1970, 1, 1) - date2j(2000, 1, 1)) * 86400);
570 } /* abstime_datetime() */