3 * Utilities for the built-in type "AbsoluteTime".
4 * Functions for the built-in type "RelativeTime".
5 * Functions for the built-in type "TimeInterval".
7 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.75 2000/10/29 13:17:34 petere Exp $
16 *-------------------------------------------------------------------------
23 #include <sys/types.h>
27 #if !(defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE))
28 #include <sys/timeb.h>
31 #include "access/xact.h"
32 #include "miscadmin.h"
33 #include "utils/builtins.h"
37 static AbsoluteTime tm2abstime(struct tm * tm, int tz);
42 #define MIN_DAYNUM -24856 /* December 13, 1901 */
43 #define MAX_DAYNUM 24854 /* January 18, 2038 */
45 #define INVALID_RELTIME_STR "Undefined RelTime"
46 #define INVALID_RELTIME_STR_LEN (sizeof(INVALID_RELTIME_STR)-1)
47 #define RELTIME_LABEL '@'
48 #define RELTIME_PAST "ago"
49 #define DIRMAXLEN (sizeof(RELTIME_PAST)-1)
52 * Unix epoch is Jan 1 00:00:00 1970.
53 * Postgres knows about times sixty-eight years on either side of that
54 * for these 4-byte types.
56 * "tinterval" is two 4-byte fields.
57 * Definitions for parsing tinterval.
60 #define IsSpace(C) ((C) == ' ')
62 #define T_INTERVAL_INVAL 0 /* data represents no valid interval */
63 #define T_INTERVAL_VALID 1 /* data represents a valid interval */
65 * ['Mon May 10 23:59:12 1943 PST' 'Sun Jan 14 03:14:21 1973 PST']
67 * 1234567890123456789012345678901234567890123456789012345678901234
69 * we allocate some extra -- timezones are usually 3 characters but
70 * this is not in the POSIX standard...
72 #define T_INTERVAL_LEN 80
73 #define INVALID_INTERVAL_STR "Undefined Range"
74 #define INVALID_INTERVAL_STR_LEN (sizeof(INVALID_INTERVAL_STR)-1)
76 #define ABSTIMEMIN(t1, t2) \
77 (DatumGetBool(DirectFunctionCall2(abstimele, \
78 AbsoluteTimeGetDatum(t1), \
79 AbsoluteTimeGetDatum(t2))) ? (t1) : (t2))
80 #define ABSTIMEMAX(t1, t2) \
81 (DatumGetBool(DirectFunctionCall2(abstimelt, \
82 AbsoluteTimeGetDatum(t1), \
83 AbsoluteTimeGetDatum(t2))) ? (t2) : (t1))
86 static char *unit_tab[] = {
87 "second", "seconds", "minute", "minutes",
88 "hour", "hours", "day", "days", "week", "weeks",
89 "month", "months", "year", "years"};
91 #define UNITMAXLEN 7 /* max length of a unit name */
92 #define NUNITS 14 /* number of different units */
94 /* table of seconds per unit (month = 30 days, year = 365 days) */
95 static int sec_tab[] = {
97 3600, 3600, 86400, 86400, 604800, 604800,
98 2592000, 2592000, 31536000, 31536000};
103 * Function prototypes -- internal to this file only
106 static void reltime2tm(RelativeTime time, struct tm * tm);
109 static int correct_unit(char *unit, int *unptr);
110 static int correct_dir(char *direction, int *signptr);
114 static int istinterval(char *i_string,
115 AbsoluteTime *i_start,
116 AbsoluteTime *i_end);
118 /* GetCurrentAbsoluteTime()
119 * Get the current system time. Set timezone parameters if not specified elsewhere.
120 * Define HasTZSet to allow clients to specify the default timezone.
122 * Returns the number of seconds since epoch (January 1 1970 GMT)
125 GetCurrentAbsoluteTime(void)
129 #if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
134 struct timeb tb; /* the old V7-ism */
142 #if defined(HAVE_TM_ZONE)
143 tm = localtime(&now);
145 CTimeZone = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
146 CDayLight = (tm->tm_isdst > 0);
151 * XXX is there a better way to get local timezone string w/o
152 * tzname? - tgl 97/03/18
154 strftime(CTZName, MAXTZLEN, "%Z", tm);
158 * XXX FreeBSD man pages indicate that this should work - thomas
161 strcpy(CTZName, tm->tm_zone);
163 #elif defined(HAVE_INT_TIMEZONE)
164 tm = localtime(&now);
166 CDayLight = tm->tm_isdst;
169 (tm->tm_isdst ? (_timezone - 3600) : _timezone);
171 (tm->tm_isdst ? (timezone - 3600) : timezone);
173 strcpy(CTZName, tzname[tm->tm_isdst]);
174 #else /* neither HAVE_TM_ZONE nor HAVE_INT_TIMEZONE */
175 CTimeZone = tb.timezone * 60;
176 CDayLight = (tb.dstflag != 0);
179 * XXX does this work to get the local timezone string in V7? -
182 strftime(CTZName, MAXTZLEN, "%Z", localtime(&now));
186 return (AbsoluteTime) now;
187 } /* GetCurrentAbsoluteTime() */
191 GetCurrentTime(struct tm * tm)
195 abstime2tm(GetCurrentTransactionStartTime(), &tz, tm, NULL);
198 } /* GetCurrentTime() */
202 abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char *tzn)
204 time_t time = (time_t) _time;
205 #if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
209 struct timeb tb; /* the old V7-ism */
215 #if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
217 tx = localtime((time_t *) &time);
220 tx = gmtime((time_t *) &time);
223 tm->tm_year = tx->tm_year + 1900;
224 tm->tm_mon = tx->tm_mon + 1;
225 tm->tm_mday = tx->tm_mday;
226 tm->tm_hour = tx->tm_hour;
227 tm->tm_min = tx->tm_min;
228 tm->tm_sec = tx->tm_sec;
229 tm->tm_isdst = tx->tm_isdst;
231 # if defined(HAVE_TM_ZONE)
232 tm->tm_gmtoff = tx->tm_gmtoff;
233 tm->tm_zone = tx->tm_zone;
236 *tzp = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
237 /* XXX FreeBSD man pages indicate that this should work - tgl 97/04/23 */
242 * Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
243 * contains an error message, which doesn't fit in the buffer
245 StrNCpy(tzn, tm->tm_zone, MAXTZLEN+1);
246 if (strlen(tm->tm_zone) > MAXTZLEN)
247 elog(NOTICE, "Invalid timezone \'%s\'", tm->tm_zone);
249 # elif defined(HAVE_INT_TIMEZONE)
252 *tzp = (tm->tm_isdst ? (_timezone - 3600) : _timezone);
254 *tzp = (tm->tm_isdst ? (timezone - 3600) : timezone);
260 * Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
261 * contains an error message, which doesn't fit in the buffer
263 StrNCpy(tzn, tzname[tm->tm_isdst], MAXTZLEN+1);
264 if (strlen(tzname[tm->tm_isdst]) > MAXTZLEN)
265 elog(NOTICE, "Invalid timezone \'%s\'", tzname[tm->tm_isdst]);
268 #else /* not (HAVE_TM_ZONE || HAVE_INT_TIMEZONE) */
270 *tzp = tb.timezone * 60;
273 * XXX does this work to get the local timezone string in V7? - tgl
278 strftime(tzn, MAXTZLEN, "%Z", localtime(&now));
279 tzn[MAXTZLEN] = '\0'; /* let's just be sure it's null-terminated */
288 * Convert a tm structure to abstime.
289 * Note that tm has full year (not 1900-based) and 1-based month.
292 tm2abstime(struct tm * tm, int tz)
297 /* validate, before going out of range on some members */
298 if (tm->tm_year < 1901 || tm->tm_year > 2038
299 || tm->tm_mon < 1 || tm->tm_mon > 12
300 || tm->tm_mday < 1 || tm->tm_mday > 31
301 || tm->tm_hour < 0 || tm->tm_hour >= 24
302 || tm->tm_min < 0 || tm->tm_min > 59
303 || tm->tm_sec < 0 || tm->tm_sec > 59)
304 return INVALID_ABSTIME;
306 day = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(1970, 1, 1));
308 /* check for time out of range */
309 if ((day < MIN_DAYNUM) || (day > MAX_DAYNUM))
310 return INVALID_ABSTIME;
312 /* convert to seconds */
313 sec = tm->tm_sec + tz + (tm->tm_min + (day * 24 + tm->tm_hour) * 60) * 60;
315 /* check for overflow */
316 if ((day == MAX_DAYNUM && sec < 0) ||
317 (day == MIN_DAYNUM && sec > 0))
318 return INVALID_ABSTIME;
320 /* check for reserved values (e.g. "current" on edge of usual range */
321 if (!AbsoluteTimeIsReal(sec))
322 return INVALID_ABSTIME;
329 * Decode date/time string and return abstime.
332 nabstimein(PG_FUNCTION_ARGS)
334 char *str = PG_GETARG_CSTRING(0);
340 char *field[MAXDATEFIELDS];
341 char lowstr[MAXDATELEN + 1];
344 ftype[MAXDATEFIELDS];
346 if (strlen(str) > MAXDATELEN)
347 elog(ERROR, "Bad (length) abstime external representation '%s'", str);
349 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
350 || (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
351 elog(ERROR, "Bad abstime external representation '%s'", str);
356 result = tm2abstime(tm, tz);
360 result = EPOCH_ABSTIME;
364 result = CURRENT_ABSTIME;
368 result = NOEND_ABSTIME;
372 result = NOSTART_ABSTIME;
376 result = INVALID_ABSTIME;
380 elog(ERROR, "Bad abstime (internal coding error) '%s'", str);
381 result = INVALID_ABSTIME;
385 PG_RETURN_ABSOLUTETIME(result);
390 * Given an AbsoluteTime return the English text version of the date
393 nabstimeout(PG_FUNCTION_ARGS)
395 AbsoluteTime time = PG_GETARG_ABSOLUTETIME(0);
401 char buf[MAXDATELEN + 1];
402 char zone[MAXDATELEN + 1],
410 case INVALID_ABSTIME:
411 strcpy(buf, INVALID);
413 case CURRENT_ABSTIME:
414 strcpy(buf, DCURRENT);
419 case NOSTART_ABSTIME:
423 abstime2tm(time, &tz, tm, tzn);
424 EncodeDateTime(tm, fsec, &tz, &tzn, DateStyle, buf);
428 result = pstrdup(buf);
429 PG_RETURN_CSTRING(result);
434 * AbsoluteTimeIsBefore -- true iff time1 is before time2.
435 * AbsoluteTimeIsBefore -- true iff time1 is after time2.
438 AbsoluteTimeIsBefore(AbsoluteTime time1, AbsoluteTime time2)
440 Assert(AbsoluteTimeIsValid(time1));
441 Assert(AbsoluteTimeIsValid(time2));
443 if (time1 == CURRENT_ABSTIME)
444 time1 = GetCurrentTransactionStartTime();
446 if (time2 == CURRENT_ABSTIME)
447 time2 = GetCurrentTransactionStartTime();
449 return time1 < time2;
454 AbsoluteTimeIsAfter(AbsoluteTime time1, AbsoluteTime time2)
456 Assert(AbsoluteTimeIsValid(time1));
457 Assert(AbsoluteTimeIsValid(time2));
459 if (time1 == CURRENT_ABSTIME)
460 time1 = GetCurrentTransactionStartTime();
462 if (time2 == CURRENT_ABSTIME)
463 time2 = GetCurrentTransactionStartTime();
465 return time1 > time2;
473 abstime_finite(PG_FUNCTION_ARGS)
475 AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
477 PG_RETURN_BOOL((abstime != INVALID_ABSTIME) &&
478 (abstime != NOSTART_ABSTIME) &&
479 (abstime != NOEND_ABSTIME));
484 * abstimeeq - returns true iff arguments are equal
485 * abstimene - returns true iff arguments are not equal
486 * abstimelt - returns true iff t1 less than t2
487 * abstimegt - returns true iff t1 greater than t2
488 * abstimele - returns true iff t1 less than or equal to t2
489 * abstimege - returns true iff t1 greater than or equal to t2
492 abstimeeq(PG_FUNCTION_ARGS)
494 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
495 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
497 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
498 PG_RETURN_BOOL(false);
499 if (t1 == CURRENT_ABSTIME)
500 t1 = GetCurrentTransactionStartTime();
501 if (t2 == CURRENT_ABSTIME)
502 t2 = GetCurrentTransactionStartTime();
504 PG_RETURN_BOOL(t1 == t2);
508 abstimene(PG_FUNCTION_ARGS)
510 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
511 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
513 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
514 PG_RETURN_BOOL(false);
515 if (t1 == CURRENT_ABSTIME)
516 t1 = GetCurrentTransactionStartTime();
517 if (t2 == CURRENT_ABSTIME)
518 t2 = GetCurrentTransactionStartTime();
520 PG_RETURN_BOOL(t1 != t2);
524 abstimelt(PG_FUNCTION_ARGS)
526 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
527 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
529 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
530 PG_RETURN_BOOL(false);
531 if (t1 == CURRENT_ABSTIME)
532 t1 = GetCurrentTransactionStartTime();
533 if (t2 == CURRENT_ABSTIME)
534 t2 = GetCurrentTransactionStartTime();
536 PG_RETURN_BOOL(t1 < t2);
540 abstimegt(PG_FUNCTION_ARGS)
542 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
543 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
545 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
546 PG_RETURN_BOOL(false);
547 if (t1 == CURRENT_ABSTIME)
548 t1 = GetCurrentTransactionStartTime();
549 if (t2 == CURRENT_ABSTIME)
550 t2 = GetCurrentTransactionStartTime();
552 PG_RETURN_BOOL(t1 > t2);
556 abstimele(PG_FUNCTION_ARGS)
558 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
559 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
561 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
562 PG_RETURN_BOOL(false);
563 if (t1 == CURRENT_ABSTIME)
564 t1 = GetCurrentTransactionStartTime();
565 if (t2 == CURRENT_ABSTIME)
566 t2 = GetCurrentTransactionStartTime();
568 PG_RETURN_BOOL(t1 <= t2);
572 abstimege(PG_FUNCTION_ARGS)
574 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
575 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
577 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
578 PG_RETURN_BOOL(false);
579 if (t1 == CURRENT_ABSTIME)
580 t1 = GetCurrentTransactionStartTime();
581 if (t2 == CURRENT_ABSTIME)
582 t2 = GetCurrentTransactionStartTime();
584 PG_RETURN_BOOL(t1 >= t2);
588 /* datetime_abstime()
589 * Convert timestamp to abstime.
592 timestamp_abstime(PG_FUNCTION_ARGS)
594 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
600 if (TIMESTAMP_IS_INVALID(timestamp))
602 result = INVALID_ABSTIME;
604 else if (TIMESTAMP_IS_NOBEGIN(timestamp))
606 result = NOSTART_ABSTIME;
608 else if (TIMESTAMP_IS_NOEND(timestamp))
610 result = NOEND_ABSTIME;
614 if (TIMESTAMP_IS_RELATIVE(timestamp))
616 timestamp2tm(SetTimestamp(timestamp), NULL, tm, &fsec, NULL);
617 result = tm2abstime(tm, 0);
619 else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) == 0)
621 result = tm2abstime(tm, 0);
625 result = INVALID_ABSTIME;
629 PG_RETURN_ABSOLUTETIME(result);
632 /* abstime_timestamp()
633 * Convert abstime to timestamp.
636 abstime_timestamp(PG_FUNCTION_ARGS)
638 AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
643 case INVALID_ABSTIME:
644 TIMESTAMP_INVALID(result);
647 case NOSTART_ABSTIME:
648 TIMESTAMP_NOBEGIN(result);
652 TIMESTAMP_NOEND(result);
656 TIMESTAMP_EPOCH(result);
659 case CURRENT_ABSTIME:
660 TIMESTAMP_CURRENT(result);
664 result = abstime + ((date2j(1970, 1, 1) - date2j(2000, 1, 1)) * 86400);
668 PG_RETURN_TIMESTAMP(result);
672 /*****************************************************************************
673 * USER I/O ROUTINES *
674 *****************************************************************************/
677 * reltimein - converts a reltime string in an internal format
680 reltimein(PG_FUNCTION_ARGS)
682 char *str = PG_GETARG_CSTRING(0);
688 char *field[MAXDATEFIELDS];
690 ftype[MAXDATEFIELDS];
691 char lowstr[MAXDATELEN + 1];
693 if (strlen(str) > MAXDATELEN)
694 elog(ERROR, "Bad (length) reltime external representation '%s'", str);
696 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
697 || (DecodeDateDelta(field, ftype, nf, &dtype, tm, &fsec) != 0))
698 elog(ERROR, "Bad reltime external representation '%s'", str);
703 result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec);
704 result += (((tm->tm_year * 365) + (tm->tm_mon * 30) + tm->tm_mday) * (24 * 60 * 60));
705 PG_RETURN_RELATIVETIME(result);
708 PG_RETURN_RELATIVETIME(INVALID_RELTIME);
711 elog(ERROR, "Bad reltime (internal coding error) '%s'", str);
712 PG_RETURN_RELATIVETIME(INVALID_RELTIME);
717 * reltimeout - converts the internal format to a reltime string
720 reltimeout(PG_FUNCTION_ARGS)
722 RelativeTime time = PG_GETARG_RELATIVETIME(0);
726 char buf[MAXDATELEN + 1];
728 if (time == INVALID_RELTIME)
730 strcpy(buf, INVALID_RELTIME_STR);
734 reltime2tm(time, tm);
735 EncodeTimeSpan(tm, 0, DateStyle, buf);
738 result = pstrdup(buf);
739 PG_RETURN_CSTRING(result);
744 reltime2tm(RelativeTime time, struct tm * tm)
746 TMODULO(time, tm->tm_year, 31536000);
747 TMODULO(time, tm->tm_mon, 2592000);
748 TMODULO(time, tm->tm_mday, 86400);
749 TMODULO(time, tm->tm_hour, 3600);
750 TMODULO(time, tm->tm_min, 60);
751 TMODULO(time, tm->tm_sec, 1);
765 timestring = (char *) palloc(Max(strlen(INVALID_RELTIME_STR),
767 if (timevalue == INVALID_RELTIME)
769 strcpy(timestring, INVALID_RELTIME_STR);
774 i = 1; /* unit = 'seconds' */
776 for (i = 12; i >= 0; i = i - 2)
777 if ((timevalue % sec_tab[i]) == 0)
778 break; /* appropriate unit found */
780 quantity = (timevalue / sec_tab[unitnr]);
781 if (quantity > 1 || quantity < -1)
782 unitnr++; /* adjust index for PLURAL of unit */
784 sprintf(timestring, "%c %lu %s", RELTIME_LABEL,
785 quantity, unit_tab[unitnr]);
787 sprintf(timestring, "%c %lu %s %s", RELTIME_LABEL,
788 (quantity * -1), unit_tab[unitnr], RELTIME_PAST);
796 * tintervalin - converts an interval string to internal format
799 tintervalin(PG_FUNCTION_ARGS)
801 char *intervalstr = PG_GETARG_CSTRING(0);
802 TimeInterval interval;
804 AbsoluteTime i_start,
809 interval = (TimeInterval) palloc(sizeof(TimeIntervalData));
810 error = istinterval(intervalstr, &t1, &t2);
812 interval->status = T_INTERVAL_INVAL;
813 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
814 interval->status = T_INTERVAL_INVAL; /* undefined */
817 i_start = ABSTIMEMIN(t1, t2);
818 i_end = ABSTIMEMAX(t1, t2);
819 interval->data[0] = i_start;
820 interval->data[1] = i_end;
821 interval->status = T_INTERVAL_VALID;
823 PG_RETURN_TIMEINTERVAL(interval);
828 * tintervalout - converts an internal interval format to a string
832 tintervalout(PG_FUNCTION_ARGS)
834 TimeInterval interval = PG_GETARG_TIMEINTERVAL(0);
838 i_str = (char *) palloc(T_INTERVAL_LEN); /* ['...' '...'] */
839 strcpy(i_str, "[\"");
840 if (interval->status == T_INTERVAL_INVAL)
841 strcat(i_str, INVALID_INTERVAL_STR);
844 p = DatumGetCString(DirectFunctionCall1(nabstimeout,
845 AbsoluteTimeGetDatum(interval->data[0])));
848 strcat(i_str, "\" \"");
849 p = DatumGetCString(DirectFunctionCall1(nabstimeout,
850 AbsoluteTimeGetDatum(interval->data[1])));
854 strcat(i_str, "\"]\0");
855 PG_RETURN_CSTRING(i_str);
859 /*****************************************************************************
861 *****************************************************************************/
864 interval_reltime(PG_FUNCTION_ARGS)
866 Interval *interval = PG_GETARG_INTERVAL_P(0);
872 if (INTERVAL_IS_INVALID(*interval))
874 time = INVALID_RELTIME;
878 if (interval->month == 0)
883 else if (abs(interval->month) >= 12)
885 year = (interval->month / 12);
886 month = (interval->month % 12);
891 month = interval->month;
894 span = (((((double) 365 * year) + ((double) 30 * month)) * 86400) + interval->time);
896 time = (((span > INT_MIN) && (span < INT_MAX)) ? span : INVALID_RELTIME);
899 PG_RETURN_RELATIVETIME(time);
904 reltime_interval(PG_FUNCTION_ARGS)
906 RelativeTime reltime = PG_GETARG_RELATIVETIME(0);
911 result = (Interval *) palloc(sizeof(Interval));
915 case INVALID_RELTIME:
916 INTERVAL_INVALID(*result);
920 TMODULO(reltime, year, 31536000);
921 TMODULO(reltime, month, 2592000);
923 result->time = reltime;
924 result->month = ((12 * year) + month);
928 PG_RETURN_INTERVAL_P(result);
933 * mktinterval - creates a time interval with endpoints t1 and t2
936 mktinterval(PG_FUNCTION_ARGS)
938 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
939 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
940 AbsoluteTime tstart = ABSTIMEMIN(t1, t2),
941 tend = ABSTIMEMAX(t1, t2);
942 TimeInterval interval;
944 interval = (TimeInterval) palloc(sizeof(TimeIntervalData));
945 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
946 interval->status = T_INTERVAL_INVAL;
949 interval->status = T_INTERVAL_VALID;
950 interval->data[0] = tstart;
951 interval->data[1] = tend;
954 PG_RETURN_TIMEINTERVAL(interval);
958 * timepl, timemi and abstimemi use the formula
959 * abstime + reltime = abstime
960 * so abstime - reltime = abstime
961 * and abstime - abstime = reltime
965 * timepl - returns the value of (abstime t1 + relime t2)
968 timepl(PG_FUNCTION_ARGS)
970 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
971 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
973 if (t1 == CURRENT_ABSTIME)
974 t1 = GetCurrentTransactionStartTime();
976 if (AbsoluteTimeIsReal(t1) &&
977 RelativeTimeIsValid(t2) &&
978 ((t2 > 0) ? (t1 < NOEND_ABSTIME - t2)
979 : (t1 > NOSTART_ABSTIME - t2))) /* prevent overflow */
980 PG_RETURN_ABSOLUTETIME(t1 + t2);
982 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
987 * timemi - returns the value of (abstime t1 - reltime t2)
990 timemi(PG_FUNCTION_ARGS)
992 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
993 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
995 if (t1 == CURRENT_ABSTIME)
996 t1 = GetCurrentTransactionStartTime();
998 if (AbsoluteTimeIsReal(t1) &&
999 RelativeTimeIsValid(t2) &&
1000 ((t2 > 0) ? (t1 > NOSTART_ABSTIME + t2)
1001 : (t1 < NOEND_ABSTIME + t2))) /* prevent overflow */
1002 PG_RETURN_ABSOLUTETIME(t1 - t2);
1004 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1009 * abstimemi - returns the value of (abstime t1 - abstime t2)
1011 * This is not exported, so it's not been made fmgr-compatible.
1014 abstimemi(AbsoluteTime t1, AbsoluteTime t2)
1016 if (t1 == CURRENT_ABSTIME)
1017 t1 = GetCurrentTransactionStartTime();
1018 if (t2 == CURRENT_ABSTIME)
1019 t2 = GetCurrentTransactionStartTime();
1021 if (AbsoluteTimeIsReal(t1) &&
1022 AbsoluteTimeIsReal(t2))
1025 return INVALID_RELTIME;
1030 * intinterval - returns true iff absolute date is in the interval
1033 intinterval(PG_FUNCTION_ARGS)
1035 AbsoluteTime t = PG_GETARG_ABSOLUTETIME(0);
1036 TimeInterval interval = PG_GETARG_TIMEINTERVAL(1);
1038 if (interval->status == T_INTERVAL_VALID && t != INVALID_ABSTIME)
1040 if (DatumGetBool(DirectFunctionCall2(abstimege,
1041 AbsoluteTimeGetDatum(t),
1042 AbsoluteTimeGetDatum(interval->data[0]))) &&
1043 DatumGetBool(DirectFunctionCall2(abstimele,
1044 AbsoluteTimeGetDatum(t),
1045 AbsoluteTimeGetDatum(interval->data[1]))))
1046 PG_RETURN_BOOL(true);
1048 PG_RETURN_BOOL(false);
1052 * tintervalrel - returns relative time corresponding to interval
1055 tintervalrel(PG_FUNCTION_ARGS)
1057 TimeInterval interval = PG_GETARG_TIMEINTERVAL(0);
1059 if (interval->status != T_INTERVAL_VALID)
1060 PG_RETURN_RELATIVETIME(INVALID_RELTIME);
1062 PG_RETURN_RELATIVETIME(abstimemi(interval->data[1], interval->data[0]));
1066 * timenow - returns time "now", internal format
1068 * Now AbsoluteTime is time since Jan 1 1970 -mer 7 Feb 1992
1071 timenow(PG_FUNCTION_ARGS)
1076 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1077 PG_RETURN_ABSOLUTETIME((AbsoluteTime) sec);
1081 * reltimeeq - returns true iff arguments are equal
1082 * reltimene - returns true iff arguments are not equal
1083 * reltimelt - returns true iff t1 less than t2
1084 * reltimegt - returns true iff t1 greater than t2
1085 * reltimele - returns true iff t1 less than or equal to t2
1086 * reltimege - returns true iff t1 greater than or equal to t2
1089 reltimeeq(PG_FUNCTION_ARGS)
1091 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1092 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1094 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1095 PG_RETURN_BOOL(false);
1096 PG_RETURN_BOOL(t1 == t2);
1100 reltimene(PG_FUNCTION_ARGS)
1102 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1103 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1105 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1106 PG_RETURN_BOOL(false);
1107 PG_RETURN_BOOL(t1 != t2);
1111 reltimelt(PG_FUNCTION_ARGS)
1113 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1114 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1116 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1117 PG_RETURN_BOOL(false);
1118 PG_RETURN_BOOL(t1 < t2);
1122 reltimegt(PG_FUNCTION_ARGS)
1124 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1125 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1127 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1128 PG_RETURN_BOOL(false);
1129 PG_RETURN_BOOL(t1 > t2);
1133 reltimele(PG_FUNCTION_ARGS)
1135 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1136 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1138 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1139 PG_RETURN_BOOL(false);
1140 PG_RETURN_BOOL(t1 <= t2);
1144 reltimege(PG_FUNCTION_ARGS)
1146 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1147 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1149 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1150 PG_RETURN_BOOL(false);
1151 PG_RETURN_BOOL(t1 >= t2);
1156 * tintervalsame - returns true iff interval i1 is same as interval i2
1157 * Check begin and end time.
1160 tintervalsame(PG_FUNCTION_ARGS)
1162 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1163 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1165 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1166 PG_RETURN_BOOL(false);
1168 if (DatumGetBool(DirectFunctionCall2(abstimeeq,
1169 AbsoluteTimeGetDatum(i1->data[0]),
1170 AbsoluteTimeGetDatum(i2->data[0]))) &&
1171 DatumGetBool(DirectFunctionCall2(abstimeeq,
1172 AbsoluteTimeGetDatum(i1->data[1]),
1173 AbsoluteTimeGetDatum(i2->data[1]))))
1174 PG_RETURN_BOOL(true);
1175 PG_RETURN_BOOL(false);
1180 * tintervaleq - returns true iff interval i1 is equal to interval i2
1181 * Check length of intervals.
1184 tintervaleq(PG_FUNCTION_ARGS)
1186 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1187 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1193 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1194 PG_RETURN_BOOL(false);
1201 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1202 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1203 PG_RETURN_BOOL(false);
1205 if (t10 == CURRENT_ABSTIME)
1206 t10 = GetCurrentTransactionStartTime();
1207 if (t11 == CURRENT_ABSTIME)
1208 t11 = GetCurrentTransactionStartTime();
1209 if (t20 == CURRENT_ABSTIME)
1210 t20 = GetCurrentTransactionStartTime();
1211 if (t21 == CURRENT_ABSTIME)
1212 t21 = GetCurrentTransactionStartTime();
1214 PG_RETURN_BOOL((t11 - t10) == (t21 - t20));
1218 tintervalne(PG_FUNCTION_ARGS)
1220 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1221 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1227 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1228 PG_RETURN_BOOL(false);
1235 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1236 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1237 PG_RETURN_BOOL(false);
1239 if (t10 == CURRENT_ABSTIME)
1240 t10 = GetCurrentTransactionStartTime();
1241 if (t11 == CURRENT_ABSTIME)
1242 t11 = GetCurrentTransactionStartTime();
1243 if (t20 == CURRENT_ABSTIME)
1244 t20 = GetCurrentTransactionStartTime();
1245 if (t21 == CURRENT_ABSTIME)
1246 t21 = GetCurrentTransactionStartTime();
1248 PG_RETURN_BOOL((t11 - t10) != (t21 - t20));
1252 tintervallt(PG_FUNCTION_ARGS)
1254 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1255 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1261 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1262 PG_RETURN_BOOL(false);
1269 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1270 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1271 PG_RETURN_BOOL(false);
1273 if (t10 == CURRENT_ABSTIME)
1274 t10 = GetCurrentTransactionStartTime();
1275 if (t11 == CURRENT_ABSTIME)
1276 t11 = GetCurrentTransactionStartTime();
1277 if (t20 == CURRENT_ABSTIME)
1278 t20 = GetCurrentTransactionStartTime();
1279 if (t21 == CURRENT_ABSTIME)
1280 t21 = GetCurrentTransactionStartTime();
1282 PG_RETURN_BOOL((t11 - t10) < (t21 - t20));
1286 tintervalle(PG_FUNCTION_ARGS)
1288 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1289 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1295 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1296 PG_RETURN_BOOL(false);
1303 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1304 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1305 PG_RETURN_BOOL(false);
1307 if (t10 == CURRENT_ABSTIME)
1308 t10 = GetCurrentTransactionStartTime();
1309 if (t11 == CURRENT_ABSTIME)
1310 t11 = GetCurrentTransactionStartTime();
1311 if (t20 == CURRENT_ABSTIME)
1312 t20 = GetCurrentTransactionStartTime();
1313 if (t21 == CURRENT_ABSTIME)
1314 t21 = GetCurrentTransactionStartTime();
1316 PG_RETURN_BOOL((t11 - t10) <= (t21 - t20));
1320 tintervalgt(PG_FUNCTION_ARGS)
1322 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1323 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1329 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1330 PG_RETURN_BOOL(false);
1337 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1338 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1339 PG_RETURN_BOOL(false);
1341 if (t10 == CURRENT_ABSTIME)
1342 t10 = GetCurrentTransactionStartTime();
1343 if (t11 == CURRENT_ABSTIME)
1344 t11 = GetCurrentTransactionStartTime();
1345 if (t20 == CURRENT_ABSTIME)
1346 t20 = GetCurrentTransactionStartTime();
1347 if (t21 == CURRENT_ABSTIME)
1348 t21 = GetCurrentTransactionStartTime();
1350 PG_RETURN_BOOL((t11 - t10) > (t21 - t20));
1354 tintervalge(PG_FUNCTION_ARGS)
1356 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1357 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1363 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1364 PG_RETURN_BOOL(false);
1371 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1372 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1373 PG_RETURN_BOOL(false);
1375 if (t10 == CURRENT_ABSTIME)
1376 t10 = GetCurrentTransactionStartTime();
1377 if (t11 == CURRENT_ABSTIME)
1378 t11 = GetCurrentTransactionStartTime();
1379 if (t20 == CURRENT_ABSTIME)
1380 t20 = GetCurrentTransactionStartTime();
1381 if (t21 == CURRENT_ABSTIME)
1382 t21 = GetCurrentTransactionStartTime();
1384 PG_RETURN_BOOL((t11 - t10) >= (t21 - t20));
1389 * tintervalleneq - returns true iff length of interval i is equal to
1391 * tintervallenne - returns true iff length of interval i is not equal
1393 * tintervallenlt - returns true iff length of interval i is less than
1395 * tintervallengt - returns true iff length of interval i is greater
1397 * tintervallenle - returns true iff length of interval i is less or
1398 * equal than reltime t
1399 * tintervallenge - returns true iff length of interval i is greater or
1400 * equal than reltime t
1403 tintervalleneq(PG_FUNCTION_ARGS)
1405 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1406 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1409 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1410 PG_RETURN_BOOL(false);
1411 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1412 TimeIntervalGetDatum(i)));
1413 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt == t);
1417 tintervallenne(PG_FUNCTION_ARGS)
1419 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1420 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1423 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1424 PG_RETURN_BOOL(false);
1425 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1426 TimeIntervalGetDatum(i)));
1427 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt != t);
1431 tintervallenlt(PG_FUNCTION_ARGS)
1433 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1434 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1437 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1438 PG_RETURN_BOOL(false);
1439 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1440 TimeIntervalGetDatum(i)));
1441 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt < t);
1445 tintervallengt(PG_FUNCTION_ARGS)
1447 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1448 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1451 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1452 PG_RETURN_BOOL(false);
1453 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1454 TimeIntervalGetDatum(i)));
1455 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt > t);
1459 tintervallenle(PG_FUNCTION_ARGS)
1461 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1462 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1465 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1466 PG_RETURN_BOOL(false);
1467 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1468 TimeIntervalGetDatum(i)));
1469 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt <= t);
1473 tintervallenge(PG_FUNCTION_ARGS)
1475 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1476 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1479 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1480 PG_RETURN_BOOL(false);
1481 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1482 TimeIntervalGetDatum(i)));
1483 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt >= t);
1487 * tintervalct - returns true iff interval i1 contains interval i2
1490 tintervalct(PG_FUNCTION_ARGS)
1492 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1493 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1495 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1496 PG_RETURN_BOOL(false);
1497 if (DatumGetBool(DirectFunctionCall2(abstimele,
1498 AbsoluteTimeGetDatum(i1->data[0]),
1499 AbsoluteTimeGetDatum(i2->data[0]))) &&
1500 DatumGetBool(DirectFunctionCall2(abstimege,
1501 AbsoluteTimeGetDatum(i1->data[1]),
1502 AbsoluteTimeGetDatum(i2->data[1]))))
1503 PG_RETURN_BOOL(true);
1504 PG_RETURN_BOOL(false);
1508 * tintervalov - returns true iff interval i1 (partially) overlaps i2
1511 tintervalov(PG_FUNCTION_ARGS)
1513 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1514 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1516 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1517 PG_RETURN_BOOL(false);
1518 if (DatumGetBool(DirectFunctionCall2(abstimelt,
1519 AbsoluteTimeGetDatum(i1->data[1]),
1520 AbsoluteTimeGetDatum(i2->data[0]))) ||
1521 DatumGetBool(DirectFunctionCall2(abstimegt,
1522 AbsoluteTimeGetDatum(i1->data[0]),
1523 AbsoluteTimeGetDatum(i2->data[1]))))
1524 PG_RETURN_BOOL(false);
1525 PG_RETURN_BOOL(true);
1529 * tintervalstart - returns the start of interval i
1532 tintervalstart(PG_FUNCTION_ARGS)
1534 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1536 if (i->status == T_INTERVAL_INVAL)
1537 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1538 PG_RETURN_ABSOLUTETIME(i->data[0]);
1542 * tintervalend - returns the end of interval i
1545 tintervalend(PG_FUNCTION_ARGS)
1547 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1549 if (i->status == T_INTERVAL_INVAL)
1550 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1551 PG_RETURN_ABSOLUTETIME(i->data[1]);
1555 /*****************************************************************************
1556 * PRIVATE ROUTINES *
1557 *****************************************************************************/
1561 * isreltime - returns 1, iff datestring is of type reltime
1562 * 2, iff datestring is 'invalid time' identifier
1563 * 0, iff datestring contains a syntax error
1564 * VALID time less or equal +/- `@ 68 years'
1568 isreltime(char *str)
1574 char *field[MAXDATEFIELDS];
1576 ftype[MAXDATEFIELDS];
1577 char lowstr[MAXDATELEN + 1];
1579 if (!PointerIsValid(str))
1582 if (strlen(str) > MAXDATELEN)
1585 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
1586 || (DecodeDateDelta(field, ftype, nf, &dtype, tm, &fsec) != 0))
1592 return (abs(tm->tm_year) <= 68) ? 1 : 0;
1616 char unit[UNITMAXLEN];
1617 char direction[DIRMAXLEN];
1619 int localUnitNumber;
1622 if (!PointerIsValid(sign))
1625 if (!PointerIsValid(unitnr))
1626 unitnr = &localUnitNumber;
1628 if (!PointerIsValid(quantity))
1629 quantity = &localQuantity;
1632 direction[0] = '\0';
1634 /* skip leading blanks */
1635 while ((c = *p) != '\0')
1642 /* Test whether 'invalid time' identifier or not */
1643 if (!strncmp(INVALID_RELTIME_STR, p, strlen(INVALID_RELTIME_STR) + 1))
1644 return 2; /* correct 'invalid time' identifier found */
1646 /* handle label of relative time */
1647 if (c != RELTIME_LABEL)
1648 return 0; /* syntax error */
1651 return 0; /* syntax error */
1653 /* handle the quantity */
1660 *quantity = *quantity * 10 + (c - '0');
1666 break; /* correct quantity found */
1668 return 0; /* syntax error */
1678 if (c >= 'a' && c <= 'z' && i <= (UNITMAXLEN - 1))
1686 if ((c == ' ' || c == '\0')
1687 && correct_unit(unit, unitnr))
1688 break; /* correct unit found */
1690 return 0; /* syntax error */
1694 /* handle optional direction */
1702 if (c >= 'a' && c <= 'z' && i <= (DIRMAXLEN - 1))
1710 if ((c == ' ' || c == '\0') && i == 0)
1713 break; /* no direction specified */
1715 if ((c == ' ' || c == '\0') && i != 0)
1717 direction[i] = '\0';
1718 correct_dir(direction, sign);
1719 break; /* correct direction found */
1722 return 0; /* syntax error */
1730 * correct_unit - returns 1, iff unit is a correct unit description
1733 * unptr: points to an integer which is the appropriate unit number
1734 * (see function isreltime())
1737 correct_unit(char *unit, int *unptr)
1743 if (strncmp(unit, unit_tab[j], strlen(unit_tab[j])) == 0)
1750 return 0; /* invalid unit descriptor */
1754 * correct_dir - returns 1, iff direction is a correct identifier
1757 * signptr: points to -1 if dir corresponds to past tense
1761 correct_dir(char *direction, int *signptr)
1764 if (strncmp(RELTIME_PAST, direction, strlen(RELTIME_PAST) + 1) == 0)
1770 return 0; /* invalid direction descriptor */
1776 * istinterval - returns 1, iff i_string is a valid interval descr.
1777 * 0, iff i_string is NOT a valid interval desc.
1778 * 2, iff any time is INVALID_ABSTIME
1781 * i_start, i_end: interval margins
1784 * `[' {` '} `'' <AbsTime> `'' {` '} `'' <AbsTime> `'' {` '} `]'
1786 * OR `Undefined Range' (see also INVALID_INTERVAL_STR)
1788 * where <AbsTime> satisfies the syntax of absolute time.
1790 * e.g. [ ' Jan 18 1902' 'Jan 1 00:00:00 1970']
1793 istinterval(char *i_string,
1794 AbsoluteTime *i_start,
1795 AbsoluteTime *i_end)
1802 /* skip leading blanks up to '[' */
1803 while ((c = *p) != '\0')
1808 return 0; /* syntax error */
1813 /* skip leading blanks up to "'" */
1814 while ((c = *p) != '\0')
1819 return 0; /* syntax error */
1824 if (strncmp(INVALID_INTERVAL_STR, p, strlen(INVALID_INTERVAL_STR)) == 0)
1825 return 0; /* undefined range, handled like a syntax
1827 /* search for the end of the first date and change it to a NULL */
1829 while ((c = *p1) != '\0')
1838 /* get the first date */
1839 *i_start = DatumGetAbsoluteTime(DirectFunctionCall1(nabstimein,
1840 CStringGetDatum(p)));
1841 /* rechange NULL at the end of the first date to a "'" */
1844 /* skip blanks up to "'", beginning of second date */
1845 while ((c = *p) != '\0')
1850 return 0; /* syntax error */
1855 /* search for the end of the second date and change it to a NULL */
1857 while ((c = *p1) != '\0')
1866 /* get the second date */
1867 *i_end = DatumGetAbsoluteTime(DirectFunctionCall1(nabstimein,
1868 CStringGetDatum(p)));
1869 /* rechange NULL at the end of the first date to a ''' */
1872 /* skip blanks up to ']' */
1873 while ((c = *p) != '\0')
1878 return 0; /* syntax error */
1885 return 0; /* syntax error */
1886 /* it seems to be a valid interval */
1891 /*****************************************************************************
1893 *****************************************************************************/
1896 int4reltime(PG_FUNCTION_ARGS)
1898 int32 timevalue = PG_GETARG_INT32(0);
1900 /* Just coerce it directly to RelativeTime ... */
1901 PG_RETURN_RELATIVETIME((RelativeTime) timevalue);
1906 * returns the current time as a text. similar to timenow() but returns
1907 * seconds with more precision (up to microsecs). (I need this to compare
1908 * the Wisconsin benchmark with Illustra whose TimeNow() shows current
1909 * time with precision up to microsecs.) - ay 3/95
1912 timeofday(PG_FUNCTION_ARGS)
1915 struct timezone tpz;
1921 gettimeofday(&tp, &tpz);
1922 strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%d %Y %Z",
1923 localtime((time_t *) &tp.tv_sec));
1924 snprintf(buf, sizeof(buf), templ, tp.tv_usec);
1926 len = VARHDRSZ + strlen(buf);
1927 result = (text *) palloc(len);
1928 VARATT_SIZEP(result) = len;
1929 memcpy(VARDATA(result), buf, strlen(buf));
1930 PG_RETURN_TEXT_P(result);