1 /*-------------------------------------------------------------------------
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.77 2000/12/09 20:40:57 tgl 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"
36 #define MIN_DAYNUM -24856 /* December 13, 1901 */
37 #define MAX_DAYNUM 24854 /* January 18, 2038 */
39 #define INVALID_RELTIME_STR "Undefined RelTime"
40 #define INVALID_RELTIME_STR_LEN (sizeof(INVALID_RELTIME_STR)-1)
41 #define RELTIME_LABEL '@'
42 #define RELTIME_PAST "ago"
43 #define DIRMAXLEN (sizeof(RELTIME_PAST)-1)
46 * Unix epoch is Jan 1 00:00:00 1970.
47 * Postgres knows about times sixty-eight years on either side of that
48 * for these 4-byte types.
50 * "tinterval" is two 4-byte fields.
51 * Definitions for parsing tinterval.
54 #define IsSpace(C) ((C) == ' ')
56 #define T_INTERVAL_INVAL 0 /* data represents no valid interval */
57 #define T_INTERVAL_VALID 1 /* data represents a valid interval */
59 * ['Mon May 10 23:59:12 1943 PST' 'Sun Jan 14 03:14:21 1973 PST']
61 * 1234567890123456789012345678901234567890123456789012345678901234
63 * we allocate some extra -- timezones are usually 3 characters but
64 * this is not in the POSIX standard...
66 #define T_INTERVAL_LEN 80
67 #define INVALID_INTERVAL_STR "Undefined Range"
68 #define INVALID_INTERVAL_STR_LEN (sizeof(INVALID_INTERVAL_STR)-1)
70 #define ABSTIMEMIN(t1, t2) \
71 (DatumGetBool(DirectFunctionCall2(abstimele, \
72 AbsoluteTimeGetDatum(t1), \
73 AbsoluteTimeGetDatum(t2))) ? (t1) : (t2))
74 #define ABSTIMEMAX(t1, t2) \
75 (DatumGetBool(DirectFunctionCall2(abstimelt, \
76 AbsoluteTimeGetDatum(t1), \
77 AbsoluteTimeGetDatum(t2))) ? (t2) : (t1))
80 static char *unit_tab[] = {
81 "second", "seconds", "minute", "minutes",
82 "hour", "hours", "day", "days", "week", "weeks",
83 "month", "months", "year", "years"};
85 #define UNITMAXLEN 7 /* max length of a unit name */
86 #define NUNITS 14 /* number of different units */
88 /* table of seconds per unit (month = 30 days, year = 365 days) */
89 static int sec_tab[] = {
91 3600, 3600, 86400, 86400, 604800, 604800,
92 2592000, 2592000, 31536000, 31536000};
97 * Function prototypes -- internal to this file only
100 static AbsoluteTime tm2abstime(struct tm * tm, int tz);
101 static void reltime2tm(RelativeTime time, struct tm * tm);
104 static int correct_unit(char *unit, int *unptr);
105 static int correct_dir(char *direction, int *signptr);
109 static int istinterval(char *i_string,
110 AbsoluteTime *i_start,
111 AbsoluteTime *i_end);
114 /* GetCurrentAbsoluteTime()
115 * Get the current system time. Set timezone parameters if not specified elsewhere.
116 * Define HasTZSet to allow clients to specify the default timezone.
118 * Returns the number of seconds since epoch (January 1 1970 GMT)
121 GetCurrentAbsoluteTime(void)
125 #if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
130 struct timeb tb; /* the old V7-ism */
138 #if defined(HAVE_TM_ZONE)
139 tm = localtime(&now);
141 CTimeZone = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
142 CDayLight = (tm->tm_isdst > 0);
147 * XXX is there a better way to get local timezone string w/o
148 * tzname? - tgl 97/03/18
150 strftime(CTZName, MAXTZLEN, "%Z", tm);
154 * XXX FreeBSD man pages indicate that this should work - thomas
157 strcpy(CTZName, tm->tm_zone);
159 #elif defined(HAVE_INT_TIMEZONE)
160 tm = localtime(&now);
162 CDayLight = tm->tm_isdst;
165 (tm->tm_isdst ? (_timezone - 3600) : _timezone);
167 (tm->tm_isdst ? (timezone - 3600) : timezone);
169 strcpy(CTZName, tzname[tm->tm_isdst]);
170 #else /* neither HAVE_TM_ZONE nor HAVE_INT_TIMEZONE */
171 CTimeZone = tb.timezone * 60;
172 CDayLight = (tb.dstflag != 0);
175 * XXX does this work to get the local timezone string in V7? -
178 strftime(CTZName, MAXTZLEN, "%Z", localtime(&now));
182 return (AbsoluteTime) now;
183 } /* GetCurrentAbsoluteTime() */
187 GetCurrentTime(struct tm * tm)
191 abstime2tm(GetCurrentTransactionStartTime(), &tz, tm, NULL);
194 } /* GetCurrentTime() */
198 abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char *tzn)
200 time_t time = (time_t) _time;
201 #if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
205 struct timeb tb; /* the old V7-ism */
211 #if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
213 tx = localtime((time_t *) &time);
216 tx = gmtime((time_t *) &time);
219 tm->tm_year = tx->tm_year + 1900;
220 tm->tm_mon = tx->tm_mon + 1;
221 tm->tm_mday = tx->tm_mday;
222 tm->tm_hour = tx->tm_hour;
223 tm->tm_min = tx->tm_min;
224 tm->tm_sec = tx->tm_sec;
225 tm->tm_isdst = tx->tm_isdst;
227 # if defined(HAVE_TM_ZONE)
228 tm->tm_gmtoff = tx->tm_gmtoff;
229 tm->tm_zone = tx->tm_zone;
232 *tzp = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
233 /* XXX FreeBSD man pages indicate that this should work - tgl 97/04/23 */
238 * Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
239 * contains an error message, which doesn't fit in the buffer
241 StrNCpy(tzn, tm->tm_zone, MAXTZLEN+1);
242 if (strlen(tm->tm_zone) > MAXTZLEN)
243 elog(NOTICE, "Invalid timezone \'%s\'", tm->tm_zone);
245 # elif defined(HAVE_INT_TIMEZONE)
248 *tzp = (tm->tm_isdst ? (_timezone - 3600) : _timezone);
250 *tzp = (tm->tm_isdst ? (timezone - 3600) : timezone);
256 * Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
257 * contains an error message, which doesn't fit in the buffer
259 StrNCpy(tzn, tzname[tm->tm_isdst], MAXTZLEN+1);
260 if (strlen(tzname[tm->tm_isdst]) > MAXTZLEN)
261 elog(NOTICE, "Invalid timezone \'%s\'", tzname[tm->tm_isdst]);
264 #else /* not (HAVE_TM_ZONE || HAVE_INT_TIMEZONE) */
266 *tzp = tb.timezone * 60;
269 * XXX does this work to get the local timezone string in V7? - tgl
274 strftime(tzn, MAXTZLEN, "%Z", localtime(&now));
275 tzn[MAXTZLEN] = '\0'; /* let's just be sure it's null-terminated */
284 * Convert a tm structure to abstime.
285 * Note that tm has full year (not 1900-based) and 1-based month.
288 tm2abstime(struct tm * tm, int tz)
293 /* validate, before going out of range on some members */
294 if (tm->tm_year < 1901 || tm->tm_year > 2038
295 || tm->tm_mon < 1 || tm->tm_mon > 12
296 || tm->tm_mday < 1 || tm->tm_mday > 31
297 || tm->tm_hour < 0 || tm->tm_hour >= 24
298 || tm->tm_min < 0 || tm->tm_min > 59
299 || tm->tm_sec < 0 || tm->tm_sec > 59)
300 return INVALID_ABSTIME;
302 day = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(1970, 1, 1));
304 /* check for time out of range */
305 if ((day < MIN_DAYNUM) || (day > MAX_DAYNUM))
306 return INVALID_ABSTIME;
308 /* convert to seconds */
309 sec = tm->tm_sec + tz + (tm->tm_min + (day * 24 + tm->tm_hour) * 60) * 60;
311 /* check for overflow */
312 if ((day == MAX_DAYNUM && sec < 0) ||
313 (day == MIN_DAYNUM && sec > 0))
314 return INVALID_ABSTIME;
316 /* check for reserved values (e.g. "current" on edge of usual range */
317 if (!AbsoluteTimeIsReal(sec))
318 return INVALID_ABSTIME;
325 * Decode date/time string and return abstime.
328 nabstimein(PG_FUNCTION_ARGS)
330 char *str = PG_GETARG_CSTRING(0);
336 char *field[MAXDATEFIELDS];
337 char lowstr[MAXDATELEN + 1];
340 ftype[MAXDATEFIELDS];
342 if (strlen(str) > MAXDATELEN)
343 elog(ERROR, "Bad (length) abstime external representation '%s'", str);
345 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
346 || (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
347 elog(ERROR, "Bad abstime external representation '%s'", str);
352 result = tm2abstime(tm, tz);
356 result = EPOCH_ABSTIME;
360 result = CURRENT_ABSTIME;
364 result = NOEND_ABSTIME;
368 result = NOSTART_ABSTIME;
372 result = INVALID_ABSTIME;
376 elog(ERROR, "Bad abstime (internal coding error) '%s'", str);
377 result = INVALID_ABSTIME;
381 PG_RETURN_ABSOLUTETIME(result);
386 * Given an AbsoluteTime return the English text version of the date
389 nabstimeout(PG_FUNCTION_ARGS)
391 AbsoluteTime time = PG_GETARG_ABSOLUTETIME(0);
397 char buf[MAXDATELEN + 1];
398 char zone[MAXDATELEN + 1],
406 case INVALID_ABSTIME:
407 strcpy(buf, INVALID);
409 case CURRENT_ABSTIME:
410 strcpy(buf, DCURRENT);
415 case NOSTART_ABSTIME:
419 abstime2tm(time, &tz, tm, tzn);
420 EncodeDateTime(tm, fsec, &tz, &tzn, DateStyle, buf);
424 result = pstrdup(buf);
425 PG_RETURN_CSTRING(result);
430 * AbsoluteTimeIsBefore -- true iff time1 is before time2.
431 * AbsoluteTimeIsBefore -- true iff time1 is after time2.
434 AbsoluteTimeIsBefore(AbsoluteTime time1, AbsoluteTime time2)
436 Assert(AbsoluteTimeIsValid(time1));
437 Assert(AbsoluteTimeIsValid(time2));
439 if (time1 == CURRENT_ABSTIME)
440 time1 = GetCurrentTransactionStartTime();
442 if (time2 == CURRENT_ABSTIME)
443 time2 = GetCurrentTransactionStartTime();
445 return time1 < time2;
450 AbsoluteTimeIsAfter(AbsoluteTime time1, AbsoluteTime time2)
452 Assert(AbsoluteTimeIsValid(time1));
453 Assert(AbsoluteTimeIsValid(time2));
455 if (time1 == CURRENT_ABSTIME)
456 time1 = GetCurrentTransactionStartTime();
458 if (time2 == CURRENT_ABSTIME)
459 time2 = GetCurrentTransactionStartTime();
461 return time1 > time2;
469 abstime_finite(PG_FUNCTION_ARGS)
471 AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
473 PG_RETURN_BOOL((abstime != INVALID_ABSTIME) &&
474 (abstime != NOSTART_ABSTIME) &&
475 (abstime != NOEND_ABSTIME));
480 * abstimeeq - returns true iff arguments are equal
481 * abstimene - returns true iff arguments are not equal
482 * abstimelt - returns true iff t1 less than t2
483 * abstimegt - returns true iff t1 greater than t2
484 * abstimele - returns true iff t1 less than or equal to t2
485 * abstimege - returns true iff t1 greater than or equal to t2
488 abstimeeq(PG_FUNCTION_ARGS)
490 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
491 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
493 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
494 PG_RETURN_BOOL(false);
495 if (t1 == CURRENT_ABSTIME)
496 t1 = GetCurrentTransactionStartTime();
497 if (t2 == CURRENT_ABSTIME)
498 t2 = GetCurrentTransactionStartTime();
500 PG_RETURN_BOOL(t1 == t2);
504 abstimene(PG_FUNCTION_ARGS)
506 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
507 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
509 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
510 PG_RETURN_BOOL(false);
511 if (t1 == CURRENT_ABSTIME)
512 t1 = GetCurrentTransactionStartTime();
513 if (t2 == CURRENT_ABSTIME)
514 t2 = GetCurrentTransactionStartTime();
516 PG_RETURN_BOOL(t1 != t2);
520 abstimelt(PG_FUNCTION_ARGS)
522 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
523 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
525 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
526 PG_RETURN_BOOL(false);
527 if (t1 == CURRENT_ABSTIME)
528 t1 = GetCurrentTransactionStartTime();
529 if (t2 == CURRENT_ABSTIME)
530 t2 = GetCurrentTransactionStartTime();
532 PG_RETURN_BOOL(t1 < t2);
536 abstimegt(PG_FUNCTION_ARGS)
538 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
539 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
541 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
542 PG_RETURN_BOOL(false);
543 if (t1 == CURRENT_ABSTIME)
544 t1 = GetCurrentTransactionStartTime();
545 if (t2 == CURRENT_ABSTIME)
546 t2 = GetCurrentTransactionStartTime();
548 PG_RETURN_BOOL(t1 > t2);
552 abstimele(PG_FUNCTION_ARGS)
554 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
555 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
557 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
558 PG_RETURN_BOOL(false);
559 if (t1 == CURRENT_ABSTIME)
560 t1 = GetCurrentTransactionStartTime();
561 if (t2 == CURRENT_ABSTIME)
562 t2 = GetCurrentTransactionStartTime();
564 PG_RETURN_BOOL(t1 <= t2);
568 abstimege(PG_FUNCTION_ARGS)
570 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
571 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
573 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
574 PG_RETURN_BOOL(false);
575 if (t1 == CURRENT_ABSTIME)
576 t1 = GetCurrentTransactionStartTime();
577 if (t2 == CURRENT_ABSTIME)
578 t2 = GetCurrentTransactionStartTime();
580 PG_RETURN_BOOL(t1 >= t2);
584 /* datetime_abstime()
585 * Convert timestamp to abstime.
588 timestamp_abstime(PG_FUNCTION_ARGS)
590 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
596 if (TIMESTAMP_IS_INVALID(timestamp))
598 result = INVALID_ABSTIME;
600 else if (TIMESTAMP_IS_NOBEGIN(timestamp))
602 result = NOSTART_ABSTIME;
604 else if (TIMESTAMP_IS_NOEND(timestamp))
606 result = NOEND_ABSTIME;
610 if (TIMESTAMP_IS_RELATIVE(timestamp))
612 timestamp2tm(SetTimestamp(timestamp), NULL, tm, &fsec, NULL);
613 result = tm2abstime(tm, 0);
615 else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) == 0)
617 result = tm2abstime(tm, 0);
621 result = INVALID_ABSTIME;
625 PG_RETURN_ABSOLUTETIME(result);
628 /* abstime_timestamp()
629 * Convert abstime to timestamp.
632 abstime_timestamp(PG_FUNCTION_ARGS)
634 AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
639 case INVALID_ABSTIME:
640 TIMESTAMP_INVALID(result);
643 case NOSTART_ABSTIME:
644 TIMESTAMP_NOBEGIN(result);
648 TIMESTAMP_NOEND(result);
652 TIMESTAMP_EPOCH(result);
655 case CURRENT_ABSTIME:
656 TIMESTAMP_CURRENT(result);
660 result = abstime + ((date2j(1970, 1, 1) - date2j(2000, 1, 1)) * 86400);
664 PG_RETURN_TIMESTAMP(result);
668 /*****************************************************************************
669 * USER I/O ROUTINES *
670 *****************************************************************************/
673 * reltimein - converts a reltime string in an internal format
676 reltimein(PG_FUNCTION_ARGS)
678 char *str = PG_GETARG_CSTRING(0);
684 char *field[MAXDATEFIELDS];
686 ftype[MAXDATEFIELDS];
687 char lowstr[MAXDATELEN + 1];
689 if (strlen(str) > MAXDATELEN)
690 elog(ERROR, "Bad (length) reltime external representation '%s'", str);
692 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
693 || (DecodeDateDelta(field, ftype, nf, &dtype, tm, &fsec) != 0))
694 elog(ERROR, "Bad reltime external representation '%s'", str);
699 result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec);
700 result += (((tm->tm_year * 365) + (tm->tm_mon * 30) + tm->tm_mday) * (24 * 60 * 60));
701 PG_RETURN_RELATIVETIME(result);
704 PG_RETURN_RELATIVETIME(INVALID_RELTIME);
707 elog(ERROR, "Bad reltime (internal coding error) '%s'", str);
708 PG_RETURN_RELATIVETIME(INVALID_RELTIME);
713 * reltimeout - converts the internal format to a reltime string
716 reltimeout(PG_FUNCTION_ARGS)
718 RelativeTime time = PG_GETARG_RELATIVETIME(0);
722 char buf[MAXDATELEN + 1];
724 if (time == INVALID_RELTIME)
726 strcpy(buf, INVALID_RELTIME_STR);
730 reltime2tm(time, tm);
731 EncodeTimeSpan(tm, 0, DateStyle, buf);
734 result = pstrdup(buf);
735 PG_RETURN_CSTRING(result);
740 reltime2tm(RelativeTime time, struct tm * tm)
742 TMODULO(time, tm->tm_year, 31536000);
743 TMODULO(time, tm->tm_mon, 2592000);
744 TMODULO(time, tm->tm_mday, 86400);
745 TMODULO(time, tm->tm_hour, 3600);
746 TMODULO(time, tm->tm_min, 60);
747 TMODULO(time, tm->tm_sec, 1);
761 timestring = (char *) palloc(Max(strlen(INVALID_RELTIME_STR),
763 if (timevalue == INVALID_RELTIME)
765 strcpy(timestring, INVALID_RELTIME_STR);
770 i = 1; /* unit = 'seconds' */
772 for (i = 12; i >= 0; i = i - 2)
773 if ((timevalue % sec_tab[i]) == 0)
774 break; /* appropriate unit found */
776 quantity = (timevalue / sec_tab[unitnr]);
777 if (quantity > 1 || quantity < -1)
778 unitnr++; /* adjust index for PLURAL of unit */
780 sprintf(timestring, "%c %lu %s", RELTIME_LABEL,
781 quantity, unit_tab[unitnr]);
783 sprintf(timestring, "%c %lu %s %s", RELTIME_LABEL,
784 (quantity * -1), unit_tab[unitnr], RELTIME_PAST);
792 * tintervalin - converts an interval string to internal format
795 tintervalin(PG_FUNCTION_ARGS)
797 char *intervalstr = PG_GETARG_CSTRING(0);
798 TimeInterval interval;
800 AbsoluteTime i_start,
805 interval = (TimeInterval) palloc(sizeof(TimeIntervalData));
806 error = istinterval(intervalstr, &t1, &t2);
808 interval->status = T_INTERVAL_INVAL;
809 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
810 interval->status = T_INTERVAL_INVAL; /* undefined */
813 i_start = ABSTIMEMIN(t1, t2);
814 i_end = ABSTIMEMAX(t1, t2);
815 interval->data[0] = i_start;
816 interval->data[1] = i_end;
817 interval->status = T_INTERVAL_VALID;
819 PG_RETURN_TIMEINTERVAL(interval);
824 * tintervalout - converts an internal interval format to a string
828 tintervalout(PG_FUNCTION_ARGS)
830 TimeInterval interval = PG_GETARG_TIMEINTERVAL(0);
834 i_str = (char *) palloc(T_INTERVAL_LEN); /* ['...' '...'] */
835 strcpy(i_str, "[\"");
836 if (interval->status == T_INTERVAL_INVAL)
837 strcat(i_str, INVALID_INTERVAL_STR);
840 p = DatumGetCString(DirectFunctionCall1(nabstimeout,
841 AbsoluteTimeGetDatum(interval->data[0])));
844 strcat(i_str, "\" \"");
845 p = DatumGetCString(DirectFunctionCall1(nabstimeout,
846 AbsoluteTimeGetDatum(interval->data[1])));
850 strcat(i_str, "\"]\0");
851 PG_RETURN_CSTRING(i_str);
855 /*****************************************************************************
857 *****************************************************************************/
860 interval_reltime(PG_FUNCTION_ARGS)
862 Interval *interval = PG_GETARG_INTERVAL_P(0);
868 if (INTERVAL_IS_INVALID(*interval))
870 time = INVALID_RELTIME;
874 if (interval->month == 0)
879 else if (abs(interval->month) >= 12)
881 year = (interval->month / 12);
882 month = (interval->month % 12);
887 month = interval->month;
890 span = (((((double) 365 * year) + ((double) 30 * month)) * 86400) + interval->time);
892 time = (((span > INT_MIN) && (span < INT_MAX)) ? span : INVALID_RELTIME);
895 PG_RETURN_RELATIVETIME(time);
900 reltime_interval(PG_FUNCTION_ARGS)
902 RelativeTime reltime = PG_GETARG_RELATIVETIME(0);
907 result = (Interval *) palloc(sizeof(Interval));
911 case INVALID_RELTIME:
912 INTERVAL_INVALID(*result);
916 TMODULO(reltime, year, 31536000);
917 TMODULO(reltime, month, 2592000);
919 result->time = reltime;
920 result->month = ((12 * year) + month);
924 PG_RETURN_INTERVAL_P(result);
929 * mktinterval - creates a time interval with endpoints t1 and t2
932 mktinterval(PG_FUNCTION_ARGS)
934 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
935 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
936 AbsoluteTime tstart = ABSTIMEMIN(t1, t2),
937 tend = ABSTIMEMAX(t1, t2);
938 TimeInterval interval;
940 interval = (TimeInterval) palloc(sizeof(TimeIntervalData));
941 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
942 interval->status = T_INTERVAL_INVAL;
945 interval->status = T_INTERVAL_VALID;
946 interval->data[0] = tstart;
947 interval->data[1] = tend;
950 PG_RETURN_TIMEINTERVAL(interval);
954 * timepl, timemi and abstimemi use the formula
955 * abstime + reltime = abstime
956 * so abstime - reltime = abstime
957 * and abstime - abstime = reltime
961 * timepl - returns the value of (abstime t1 + relime t2)
964 timepl(PG_FUNCTION_ARGS)
966 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
967 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
969 if (t1 == CURRENT_ABSTIME)
970 t1 = GetCurrentTransactionStartTime();
972 if (AbsoluteTimeIsReal(t1) &&
973 RelativeTimeIsValid(t2) &&
974 ((t2 > 0) ? (t1 < NOEND_ABSTIME - t2)
975 : (t1 > NOSTART_ABSTIME - t2))) /* prevent overflow */
976 PG_RETURN_ABSOLUTETIME(t1 + t2);
978 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
983 * timemi - returns the value of (abstime t1 - reltime t2)
986 timemi(PG_FUNCTION_ARGS)
988 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
989 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
991 if (t1 == CURRENT_ABSTIME)
992 t1 = GetCurrentTransactionStartTime();
994 if (AbsoluteTimeIsReal(t1) &&
995 RelativeTimeIsValid(t2) &&
996 ((t2 > 0) ? (t1 > NOSTART_ABSTIME + t2)
997 : (t1 < NOEND_ABSTIME + t2))) /* prevent overflow */
998 PG_RETURN_ABSOLUTETIME(t1 - t2);
1000 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1005 * abstimemi - returns the value of (abstime t1 - abstime t2)
1007 * This is not exported, so it's not been made fmgr-compatible.
1010 abstimemi(AbsoluteTime t1, AbsoluteTime t2)
1012 if (t1 == CURRENT_ABSTIME)
1013 t1 = GetCurrentTransactionStartTime();
1014 if (t2 == CURRENT_ABSTIME)
1015 t2 = GetCurrentTransactionStartTime();
1017 if (AbsoluteTimeIsReal(t1) &&
1018 AbsoluteTimeIsReal(t2))
1021 return INVALID_RELTIME;
1026 * intinterval - returns true iff absolute date is in the interval
1029 intinterval(PG_FUNCTION_ARGS)
1031 AbsoluteTime t = PG_GETARG_ABSOLUTETIME(0);
1032 TimeInterval interval = PG_GETARG_TIMEINTERVAL(1);
1034 if (interval->status == T_INTERVAL_VALID && t != INVALID_ABSTIME)
1036 if (DatumGetBool(DirectFunctionCall2(abstimege,
1037 AbsoluteTimeGetDatum(t),
1038 AbsoluteTimeGetDatum(interval->data[0]))) &&
1039 DatumGetBool(DirectFunctionCall2(abstimele,
1040 AbsoluteTimeGetDatum(t),
1041 AbsoluteTimeGetDatum(interval->data[1]))))
1042 PG_RETURN_BOOL(true);
1044 PG_RETURN_BOOL(false);
1048 * tintervalrel - returns relative time corresponding to interval
1051 tintervalrel(PG_FUNCTION_ARGS)
1053 TimeInterval interval = PG_GETARG_TIMEINTERVAL(0);
1055 if (interval->status != T_INTERVAL_VALID)
1056 PG_RETURN_RELATIVETIME(INVALID_RELTIME);
1058 PG_RETURN_RELATIVETIME(abstimemi(interval->data[1], interval->data[0]));
1062 * timenow - returns time "now", internal format
1064 * Now AbsoluteTime is time since Jan 1 1970 -mer 7 Feb 1992
1067 timenow(PG_FUNCTION_ARGS)
1072 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1073 PG_RETURN_ABSOLUTETIME((AbsoluteTime) sec);
1077 * reltimeeq - returns true iff arguments are equal
1078 * reltimene - returns true iff arguments are not equal
1079 * reltimelt - returns true iff t1 less than t2
1080 * reltimegt - returns true iff t1 greater than t2
1081 * reltimele - returns true iff t1 less than or equal to t2
1082 * reltimege - returns true iff t1 greater than or equal to t2
1085 reltimeeq(PG_FUNCTION_ARGS)
1087 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1088 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1090 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1091 PG_RETURN_BOOL(false);
1092 PG_RETURN_BOOL(t1 == t2);
1096 reltimene(PG_FUNCTION_ARGS)
1098 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1099 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1101 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1102 PG_RETURN_BOOL(false);
1103 PG_RETURN_BOOL(t1 != t2);
1107 reltimelt(PG_FUNCTION_ARGS)
1109 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1110 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1112 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1113 PG_RETURN_BOOL(false);
1114 PG_RETURN_BOOL(t1 < t2);
1118 reltimegt(PG_FUNCTION_ARGS)
1120 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1121 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1123 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1124 PG_RETURN_BOOL(false);
1125 PG_RETURN_BOOL(t1 > t2);
1129 reltimele(PG_FUNCTION_ARGS)
1131 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1132 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1134 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1135 PG_RETURN_BOOL(false);
1136 PG_RETURN_BOOL(t1 <= t2);
1140 reltimege(PG_FUNCTION_ARGS)
1142 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1143 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1145 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1146 PG_RETURN_BOOL(false);
1147 PG_RETURN_BOOL(t1 >= t2);
1152 * tintervalsame - returns true iff interval i1 is same as interval i2
1153 * Check begin and end time.
1156 tintervalsame(PG_FUNCTION_ARGS)
1158 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1159 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1161 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1162 PG_RETURN_BOOL(false);
1164 if (DatumGetBool(DirectFunctionCall2(abstimeeq,
1165 AbsoluteTimeGetDatum(i1->data[0]),
1166 AbsoluteTimeGetDatum(i2->data[0]))) &&
1167 DatumGetBool(DirectFunctionCall2(abstimeeq,
1168 AbsoluteTimeGetDatum(i1->data[1]),
1169 AbsoluteTimeGetDatum(i2->data[1]))))
1170 PG_RETURN_BOOL(true);
1171 PG_RETURN_BOOL(false);
1176 * tintervaleq - returns true iff interval i1 is equal to interval i2
1177 * Check length of intervals.
1180 tintervaleq(PG_FUNCTION_ARGS)
1182 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1183 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1189 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1190 PG_RETURN_BOOL(false);
1197 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1198 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1199 PG_RETURN_BOOL(false);
1201 if (t10 == CURRENT_ABSTIME)
1202 t10 = GetCurrentTransactionStartTime();
1203 if (t11 == CURRENT_ABSTIME)
1204 t11 = GetCurrentTransactionStartTime();
1205 if (t20 == CURRENT_ABSTIME)
1206 t20 = GetCurrentTransactionStartTime();
1207 if (t21 == CURRENT_ABSTIME)
1208 t21 = GetCurrentTransactionStartTime();
1210 PG_RETURN_BOOL((t11 - t10) == (t21 - t20));
1214 tintervalne(PG_FUNCTION_ARGS)
1216 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1217 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1223 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1224 PG_RETURN_BOOL(false);
1231 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1232 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1233 PG_RETURN_BOOL(false);
1235 if (t10 == CURRENT_ABSTIME)
1236 t10 = GetCurrentTransactionStartTime();
1237 if (t11 == CURRENT_ABSTIME)
1238 t11 = GetCurrentTransactionStartTime();
1239 if (t20 == CURRENT_ABSTIME)
1240 t20 = GetCurrentTransactionStartTime();
1241 if (t21 == CURRENT_ABSTIME)
1242 t21 = GetCurrentTransactionStartTime();
1244 PG_RETURN_BOOL((t11 - t10) != (t21 - t20));
1248 tintervallt(PG_FUNCTION_ARGS)
1250 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1251 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1257 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1258 PG_RETURN_BOOL(false);
1265 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1266 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1267 PG_RETURN_BOOL(false);
1269 if (t10 == CURRENT_ABSTIME)
1270 t10 = GetCurrentTransactionStartTime();
1271 if (t11 == CURRENT_ABSTIME)
1272 t11 = GetCurrentTransactionStartTime();
1273 if (t20 == CURRENT_ABSTIME)
1274 t20 = GetCurrentTransactionStartTime();
1275 if (t21 == CURRENT_ABSTIME)
1276 t21 = GetCurrentTransactionStartTime();
1278 PG_RETURN_BOOL((t11 - t10) < (t21 - t20));
1282 tintervalle(PG_FUNCTION_ARGS)
1284 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1285 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1291 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1292 PG_RETURN_BOOL(false);
1299 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1300 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1301 PG_RETURN_BOOL(false);
1303 if (t10 == CURRENT_ABSTIME)
1304 t10 = GetCurrentTransactionStartTime();
1305 if (t11 == CURRENT_ABSTIME)
1306 t11 = GetCurrentTransactionStartTime();
1307 if (t20 == CURRENT_ABSTIME)
1308 t20 = GetCurrentTransactionStartTime();
1309 if (t21 == CURRENT_ABSTIME)
1310 t21 = GetCurrentTransactionStartTime();
1312 PG_RETURN_BOOL((t11 - t10) <= (t21 - t20));
1316 tintervalgt(PG_FUNCTION_ARGS)
1318 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1319 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1325 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1326 PG_RETURN_BOOL(false);
1333 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1334 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1335 PG_RETURN_BOOL(false);
1337 if (t10 == CURRENT_ABSTIME)
1338 t10 = GetCurrentTransactionStartTime();
1339 if (t11 == CURRENT_ABSTIME)
1340 t11 = GetCurrentTransactionStartTime();
1341 if (t20 == CURRENT_ABSTIME)
1342 t20 = GetCurrentTransactionStartTime();
1343 if (t21 == CURRENT_ABSTIME)
1344 t21 = GetCurrentTransactionStartTime();
1346 PG_RETURN_BOOL((t11 - t10) > (t21 - t20));
1350 tintervalge(PG_FUNCTION_ARGS)
1352 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1353 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1359 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1360 PG_RETURN_BOOL(false);
1367 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1368 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1369 PG_RETURN_BOOL(false);
1371 if (t10 == CURRENT_ABSTIME)
1372 t10 = GetCurrentTransactionStartTime();
1373 if (t11 == CURRENT_ABSTIME)
1374 t11 = GetCurrentTransactionStartTime();
1375 if (t20 == CURRENT_ABSTIME)
1376 t20 = GetCurrentTransactionStartTime();
1377 if (t21 == CURRENT_ABSTIME)
1378 t21 = GetCurrentTransactionStartTime();
1380 PG_RETURN_BOOL((t11 - t10) >= (t21 - t20));
1385 * tintervalleneq - returns true iff length of interval i is equal to
1387 * tintervallenne - returns true iff length of interval i is not equal
1389 * tintervallenlt - returns true iff length of interval i is less than
1391 * tintervallengt - returns true iff length of interval i is greater
1393 * tintervallenle - returns true iff length of interval i is less or
1394 * equal than reltime t
1395 * tintervallenge - returns true iff length of interval i is greater or
1396 * equal than reltime t
1399 tintervalleneq(PG_FUNCTION_ARGS)
1401 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1402 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1405 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1406 PG_RETURN_BOOL(false);
1407 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1408 TimeIntervalGetDatum(i)));
1409 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt == t);
1413 tintervallenne(PG_FUNCTION_ARGS)
1415 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1416 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1419 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1420 PG_RETURN_BOOL(false);
1421 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1422 TimeIntervalGetDatum(i)));
1423 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt != t);
1427 tintervallenlt(PG_FUNCTION_ARGS)
1429 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1430 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1433 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1434 PG_RETURN_BOOL(false);
1435 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1436 TimeIntervalGetDatum(i)));
1437 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt < t);
1441 tintervallengt(PG_FUNCTION_ARGS)
1443 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1444 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1447 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1448 PG_RETURN_BOOL(false);
1449 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1450 TimeIntervalGetDatum(i)));
1451 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt > t);
1455 tintervallenle(PG_FUNCTION_ARGS)
1457 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1458 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1461 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1462 PG_RETURN_BOOL(false);
1463 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1464 TimeIntervalGetDatum(i)));
1465 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt <= t);
1469 tintervallenge(PG_FUNCTION_ARGS)
1471 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1472 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1475 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1476 PG_RETURN_BOOL(false);
1477 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1478 TimeIntervalGetDatum(i)));
1479 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt >= t);
1483 * tintervalct - returns true iff interval i1 contains interval i2
1486 tintervalct(PG_FUNCTION_ARGS)
1488 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1489 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1491 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1492 PG_RETURN_BOOL(false);
1493 if (DatumGetBool(DirectFunctionCall2(abstimele,
1494 AbsoluteTimeGetDatum(i1->data[0]),
1495 AbsoluteTimeGetDatum(i2->data[0]))) &&
1496 DatumGetBool(DirectFunctionCall2(abstimege,
1497 AbsoluteTimeGetDatum(i1->data[1]),
1498 AbsoluteTimeGetDatum(i2->data[1]))))
1499 PG_RETURN_BOOL(true);
1500 PG_RETURN_BOOL(false);
1504 * tintervalov - returns true iff interval i1 (partially) overlaps i2
1507 tintervalov(PG_FUNCTION_ARGS)
1509 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1510 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1512 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1513 PG_RETURN_BOOL(false);
1514 if (DatumGetBool(DirectFunctionCall2(abstimelt,
1515 AbsoluteTimeGetDatum(i1->data[1]),
1516 AbsoluteTimeGetDatum(i2->data[0]))) ||
1517 DatumGetBool(DirectFunctionCall2(abstimegt,
1518 AbsoluteTimeGetDatum(i1->data[0]),
1519 AbsoluteTimeGetDatum(i2->data[1]))))
1520 PG_RETURN_BOOL(false);
1521 PG_RETURN_BOOL(true);
1525 * tintervalstart - returns the start of interval i
1528 tintervalstart(PG_FUNCTION_ARGS)
1530 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1532 if (i->status == T_INTERVAL_INVAL)
1533 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1534 PG_RETURN_ABSOLUTETIME(i->data[0]);
1538 * tintervalend - returns the end of interval i
1541 tintervalend(PG_FUNCTION_ARGS)
1543 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1545 if (i->status == T_INTERVAL_INVAL)
1546 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1547 PG_RETURN_ABSOLUTETIME(i->data[1]);
1551 /*****************************************************************************
1552 * PRIVATE ROUTINES *
1553 *****************************************************************************/
1557 * isreltime - returns 1, iff datestring is of type reltime
1558 * 2, iff datestring is 'invalid time' identifier
1559 * 0, iff datestring contains a syntax error
1560 * VALID time less or equal +/- `@ 68 years'
1564 isreltime(char *str)
1570 char *field[MAXDATEFIELDS];
1572 ftype[MAXDATEFIELDS];
1573 char lowstr[MAXDATELEN + 1];
1575 if (!PointerIsValid(str))
1578 if (strlen(str) > MAXDATELEN)
1581 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
1582 || (DecodeDateDelta(field, ftype, nf, &dtype, tm, &fsec) != 0))
1588 return (abs(tm->tm_year) <= 68) ? 1 : 0;
1612 char unit[UNITMAXLEN];
1613 char direction[DIRMAXLEN];
1615 int localUnitNumber;
1618 if (!PointerIsValid(sign))
1621 if (!PointerIsValid(unitnr))
1622 unitnr = &localUnitNumber;
1624 if (!PointerIsValid(quantity))
1625 quantity = &localQuantity;
1628 direction[0] = '\0';
1630 /* skip leading blanks */
1631 while ((c = *p) != '\0')
1638 /* Test whether 'invalid time' identifier or not */
1639 if (!strncmp(INVALID_RELTIME_STR, p, strlen(INVALID_RELTIME_STR) + 1))
1640 return 2; /* correct 'invalid time' identifier found */
1642 /* handle label of relative time */
1643 if (c != RELTIME_LABEL)
1644 return 0; /* syntax error */
1647 return 0; /* syntax error */
1649 /* handle the quantity */
1654 if (isdigit((unsigned char) c))
1656 *quantity = *quantity * 10 + (c - '0');
1662 break; /* correct quantity found */
1664 return 0; /* syntax error */
1674 if (c >= 'a' && c <= 'z' && i <= (UNITMAXLEN - 1))
1682 if ((c == ' ' || c == '\0')
1683 && correct_unit(unit, unitnr))
1684 break; /* correct unit found */
1686 return 0; /* syntax error */
1690 /* handle optional direction */
1698 if (c >= 'a' && c <= 'z' && i <= (DIRMAXLEN - 1))
1706 if ((c == ' ' || c == '\0') && i == 0)
1709 break; /* no direction specified */
1711 if ((c == ' ' || c == '\0') && i != 0)
1713 direction[i] = '\0';
1714 correct_dir(direction, sign);
1715 break; /* correct direction found */
1718 return 0; /* syntax error */
1726 * correct_unit - returns 1, iff unit is a correct unit description
1729 * unptr: points to an integer which is the appropriate unit number
1730 * (see function isreltime())
1733 correct_unit(char *unit, int *unptr)
1739 if (strncmp(unit, unit_tab[j], strlen(unit_tab[j])) == 0)
1746 return 0; /* invalid unit descriptor */
1750 * correct_dir - returns 1, iff direction is a correct identifier
1753 * signptr: points to -1 if dir corresponds to past tense
1757 correct_dir(char *direction, int *signptr)
1760 if (strncmp(RELTIME_PAST, direction, strlen(RELTIME_PAST) + 1) == 0)
1766 return 0; /* invalid direction descriptor */
1772 * istinterval - returns 1, iff i_string is a valid interval descr.
1773 * 0, iff i_string is NOT a valid interval desc.
1774 * 2, iff any time is INVALID_ABSTIME
1777 * i_start, i_end: interval margins
1780 * `[' {` '} `'' <AbsTime> `'' {` '} `'' <AbsTime> `'' {` '} `]'
1782 * OR `Undefined Range' (see also INVALID_INTERVAL_STR)
1784 * where <AbsTime> satisfies the syntax of absolute time.
1786 * e.g. [ ' Jan 18 1902' 'Jan 1 00:00:00 1970']
1789 istinterval(char *i_string,
1790 AbsoluteTime *i_start,
1791 AbsoluteTime *i_end)
1798 /* skip leading blanks up to '[' */
1799 while ((c = *p) != '\0')
1804 return 0; /* syntax error */
1809 /* skip leading blanks up to "'" */
1810 while ((c = *p) != '\0')
1815 return 0; /* syntax error */
1820 if (strncmp(INVALID_INTERVAL_STR, p, strlen(INVALID_INTERVAL_STR)) == 0)
1821 return 0; /* undefined range, handled like a syntax
1823 /* search for the end of the first date and change it to a NULL */
1825 while ((c = *p1) != '\0')
1834 /* get the first date */
1835 *i_start = DatumGetAbsoluteTime(DirectFunctionCall1(nabstimein,
1836 CStringGetDatum(p)));
1837 /* rechange NULL at the end of the first date to a "'" */
1840 /* skip blanks up to "'", beginning of second date */
1841 while ((c = *p) != '\0')
1846 return 0; /* syntax error */
1851 /* search for the end of the second date and change it to a NULL */
1853 while ((c = *p1) != '\0')
1862 /* get the second date */
1863 *i_end = DatumGetAbsoluteTime(DirectFunctionCall1(nabstimein,
1864 CStringGetDatum(p)));
1865 /* rechange NULL at the end of the first date to a ''' */
1868 /* skip blanks up to ']' */
1869 while ((c = *p) != '\0')
1874 return 0; /* syntax error */
1881 return 0; /* syntax error */
1882 /* it seems to be a valid interval */
1887 /*****************************************************************************
1889 *****************************************************************************/
1892 int4reltime(PG_FUNCTION_ARGS)
1894 int32 timevalue = PG_GETARG_INT32(0);
1896 /* Just coerce it directly to RelativeTime ... */
1897 PG_RETURN_RELATIVETIME((RelativeTime) timevalue);
1902 * returns the current time as a text. similar to timenow() but returns
1903 * seconds with more precision (up to microsecs). (I need this to compare
1904 * the Wisconsin benchmark with Illustra whose TimeNow() shows current
1905 * time with precision up to microsecs.) - ay 3/95
1908 timeofday(PG_FUNCTION_ARGS)
1911 struct timezone tpz;
1917 gettimeofday(&tp, &tpz);
1918 strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%d %Y %Z",
1919 localtime((time_t *) &tp.tv_sec));
1920 snprintf(buf, sizeof(buf), templ, tp.tv_usec);
1922 len = VARHDRSZ + strlen(buf);
1923 result = (text *) palloc(len);
1924 VARATT_SIZEP(result) = len;
1925 memcpy(VARDATA(result), buf, strlen(buf));
1926 PG_RETURN_TEXT_P(result);