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-2001, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.83 2001/03/22 03:59:52 momjian 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;
163 CTimeZone = ((tm->tm_isdst > 0) ? (TIMEZONE_GLOBAL - 3600) : TIMEZONE_GLOBAL);
164 strcpy(CTZName, tzname[tm->tm_isdst]);
165 #else /* neither HAVE_TM_ZONE nor
166 * HAVE_INT_TIMEZONE */
167 CTimeZone = tb.timezone * 60;
168 CDayLight = (tb.dstflag != 0);
171 * XXX does this work to get the local timezone string in V7? -
174 strftime(CTZName, MAXTZLEN, "%Z", localtime(&now));
178 return (AbsoluteTime) now;
179 } /* GetCurrentAbsoluteTime() */
183 GetCurrentTime(struct tm * tm)
187 abstime2tm(GetCurrentTransactionStartTime(), &tz, tm, NULL);
190 } /* GetCurrentTime() */
194 abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char *tzn)
196 time_t time = (time_t) _time;
198 #if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
202 struct timeb tb; /* the old V7-ism */
208 #if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
211 tx = localtime((time_t *) &time);
212 #ifdef NO_MKTIME_BEFORE_1970
213 if (tx->tm_year < 70 && tx->tm_isdst == 1)
216 tx = localtime((time_t *) &time);
223 tx = gmtime((time_t *) &time);
226 tm->tm_year = tx->tm_year + 1900;
227 tm->tm_mon = tx->tm_mon + 1;
228 tm->tm_mday = tx->tm_mday;
229 tm->tm_hour = tx->tm_hour;
230 tm->tm_min = tx->tm_min;
231 tm->tm_sec = tx->tm_sec;
232 tm->tm_isdst = tx->tm_isdst;
234 #if defined(HAVE_TM_ZONE)
235 tm->tm_gmtoff = tx->tm_gmtoff;
236 tm->tm_zone = tx->tm_zone;
239 *tzp = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
240 /* XXX FreeBSD man pages indicate that this should work - tgl 97/04/23 */
245 * Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
246 * contains an error message, which doesn't fit in the buffer
248 StrNCpy(tzn, tm->tm_zone, MAXTZLEN + 1);
249 if (strlen(tm->tm_zone) > MAXTZLEN)
250 elog(NOTICE, "Invalid timezone \'%s\'", tm->tm_zone);
252 #elif defined(HAVE_INT_TIMEZONE)
254 *tzp = ((tm->tm_isdst > 0) ? (TIMEZONE_GLOBAL - 3600) : TIMEZONE_GLOBAL);
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))
601 result = INVALID_ABSTIME;
602 else if (TIMESTAMP_IS_NOBEGIN(timestamp))
603 result = NOSTART_ABSTIME;
604 else if (TIMESTAMP_IS_NOEND(timestamp))
605 result = NOEND_ABSTIME;
608 if (TIMESTAMP_IS_RELATIVE(timestamp))
610 timestamp2tm(SetTimestamp(timestamp), NULL, tm, &fsec, NULL);
611 result = tm2abstime(tm, 0);
613 else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) == 0)
614 result = tm2abstime(tm, 0);
616 result = INVALID_ABSTIME;
619 PG_RETURN_ABSOLUTETIME(result);
622 /* abstime_timestamp()
623 * Convert abstime to timestamp.
626 abstime_timestamp(PG_FUNCTION_ARGS)
628 AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
633 case INVALID_ABSTIME:
634 TIMESTAMP_INVALID(result);
637 case NOSTART_ABSTIME:
638 TIMESTAMP_NOBEGIN(result);
642 TIMESTAMP_NOEND(result);
646 TIMESTAMP_EPOCH(result);
649 case CURRENT_ABSTIME:
650 TIMESTAMP_CURRENT(result);
654 result = abstime + ((date2j(1970, 1, 1) - date2j(2000, 1, 1)) * 86400);
658 PG_RETURN_TIMESTAMP(result);
662 /*****************************************************************************
663 * USER I/O ROUTINES *
664 *****************************************************************************/
667 * reltimein - converts a reltime string in an internal format
670 reltimein(PG_FUNCTION_ARGS)
672 char *str = PG_GETARG_CSTRING(0);
678 char *field[MAXDATEFIELDS];
680 ftype[MAXDATEFIELDS];
681 char lowstr[MAXDATELEN + 1];
683 if (strlen(str) > MAXDATELEN)
684 elog(ERROR, "Bad (length) reltime external representation '%s'", str);
686 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
687 || (DecodeDateDelta(field, ftype, nf, &dtype, tm, &fsec) != 0))
688 elog(ERROR, "Bad reltime external representation '%s'", str);
693 result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec);
694 result += (((tm->tm_year * 365) + (tm->tm_mon * 30) + tm->tm_mday) * (24 * 60 * 60));
695 PG_RETURN_RELATIVETIME(result);
698 PG_RETURN_RELATIVETIME(INVALID_RELTIME);
701 elog(ERROR, "Bad reltime (internal coding error) '%s'", str);
702 PG_RETURN_RELATIVETIME(INVALID_RELTIME);
707 * reltimeout - converts the internal format to a reltime string
710 reltimeout(PG_FUNCTION_ARGS)
712 RelativeTime time = PG_GETARG_RELATIVETIME(0);
716 char buf[MAXDATELEN + 1];
718 if (time == INVALID_RELTIME)
719 strcpy(buf, INVALID_RELTIME_STR);
722 reltime2tm(time, tm);
723 EncodeTimeSpan(tm, 0, DateStyle, buf);
726 result = pstrdup(buf);
727 PG_RETURN_CSTRING(result);
732 reltime2tm(RelativeTime time, struct tm * tm)
734 TMODULO(time, tm->tm_year, 31536000);
735 TMODULO(time, tm->tm_mon, 2592000);
736 TMODULO(time, tm->tm_mday, 86400);
737 TMODULO(time, tm->tm_hour, 3600);
738 TMODULO(time, tm->tm_min, 60);
739 TMODULO(time, tm->tm_sec, 1);
753 timestring = (char *) palloc(Max(strlen(INVALID_RELTIME_STR),
755 if (timevalue == INVALID_RELTIME)
757 strcpy(timestring, INVALID_RELTIME_STR);
762 i = 1; /* unit = 'seconds' */
764 for (i = 12; i >= 0; i = i - 2)
765 if ((timevalue % sec_tab[i]) == 0)
766 break; /* appropriate unit found */
768 quantity = (timevalue / sec_tab[unitnr]);
769 if (quantity > 1 || quantity < -1)
770 unitnr++; /* adjust index for PLURAL of unit */
772 sprintf(timestring, "%c %lu %s", RELTIME_LABEL,
773 quantity, unit_tab[unitnr]);
775 sprintf(timestring, "%c %lu %s %s", RELTIME_LABEL,
776 (quantity * -1), unit_tab[unitnr], RELTIME_PAST);
784 * tintervalin - converts an interval string to internal format
787 tintervalin(PG_FUNCTION_ARGS)
789 char *intervalstr = PG_GETARG_CSTRING(0);
790 TimeInterval interval;
792 AbsoluteTime i_start,
797 interval = (TimeInterval) palloc(sizeof(TimeIntervalData));
798 error = istinterval(intervalstr, &t1, &t2);
800 interval->status = T_INTERVAL_INVAL;
801 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
802 interval->status = T_INTERVAL_INVAL; /* undefined */
805 i_start = ABSTIMEMIN(t1, t2);
806 i_end = ABSTIMEMAX(t1, t2);
807 interval->data[0] = i_start;
808 interval->data[1] = i_end;
809 interval->status = T_INTERVAL_VALID;
811 PG_RETURN_TIMEINTERVAL(interval);
816 * tintervalout - converts an internal interval format to a string
820 tintervalout(PG_FUNCTION_ARGS)
822 TimeInterval interval = PG_GETARG_TIMEINTERVAL(0);
826 i_str = (char *) palloc(T_INTERVAL_LEN); /* ['...' '...'] */
827 strcpy(i_str, "[\"");
828 if (interval->status == T_INTERVAL_INVAL)
829 strcat(i_str, INVALID_INTERVAL_STR);
832 p = DatumGetCString(DirectFunctionCall1(nabstimeout,
833 AbsoluteTimeGetDatum(interval->data[0])));
836 strcat(i_str, "\" \"");
837 p = DatumGetCString(DirectFunctionCall1(nabstimeout,
838 AbsoluteTimeGetDatum(interval->data[1])));
842 strcat(i_str, "\"]\0");
843 PG_RETURN_CSTRING(i_str);
847 /*****************************************************************************
849 *****************************************************************************/
852 interval_reltime(PG_FUNCTION_ARGS)
854 Interval *interval = PG_GETARG_INTERVAL_P(0);
860 if (INTERVAL_IS_INVALID(*interval))
861 time = INVALID_RELTIME;
864 if (interval->month == 0)
869 else if (abs(interval->month) >= 12)
871 year = (interval->month / 12);
872 month = (interval->month % 12);
877 month = interval->month;
880 span = (((((double) 365 * year) + ((double) 30 * month)) * 86400) + interval->time);
882 time = (((span > INT_MIN) && (span < INT_MAX)) ? span : INVALID_RELTIME);
885 PG_RETURN_RELATIVETIME(time);
890 reltime_interval(PG_FUNCTION_ARGS)
892 RelativeTime reltime = PG_GETARG_RELATIVETIME(0);
897 result = (Interval *) palloc(sizeof(Interval));
901 case INVALID_RELTIME:
902 INTERVAL_INVALID(*result);
906 TMODULO(reltime, year, 31536000);
907 TMODULO(reltime, month, 2592000);
909 result->time = reltime;
910 result->month = ((12 * year) + month);
914 PG_RETURN_INTERVAL_P(result);
919 * mktinterval - creates a time interval with endpoints t1 and t2
922 mktinterval(PG_FUNCTION_ARGS)
924 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
925 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
926 AbsoluteTime tstart = ABSTIMEMIN(t1, t2),
927 tend = ABSTIMEMAX(t1, t2);
928 TimeInterval interval;
930 interval = (TimeInterval) palloc(sizeof(TimeIntervalData));
931 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
932 interval->status = T_INTERVAL_INVAL;
935 interval->status = T_INTERVAL_VALID;
936 interval->data[0] = tstart;
937 interval->data[1] = tend;
940 PG_RETURN_TIMEINTERVAL(interval);
944 * timepl, timemi and abstimemi use the formula
945 * abstime + reltime = abstime
946 * so abstime - reltime = abstime
947 * and abstime - abstime = reltime
951 * timepl - returns the value of (abstime t1 + relime t2)
954 timepl(PG_FUNCTION_ARGS)
956 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
957 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
959 if (t1 == CURRENT_ABSTIME)
960 t1 = GetCurrentTransactionStartTime();
962 if (AbsoluteTimeIsReal(t1) &&
963 RelativeTimeIsValid(t2) &&
964 ((t2 > 0) ? (t1 < NOEND_ABSTIME - t2)
965 : (t1 > NOSTART_ABSTIME - t2))) /* prevent overflow */
966 PG_RETURN_ABSOLUTETIME(t1 + t2);
968 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
973 * timemi - returns the value of (abstime t1 - reltime t2)
976 timemi(PG_FUNCTION_ARGS)
978 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
979 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
981 if (t1 == CURRENT_ABSTIME)
982 t1 = GetCurrentTransactionStartTime();
984 if (AbsoluteTimeIsReal(t1) &&
985 RelativeTimeIsValid(t2) &&
986 ((t2 > 0) ? (t1 > NOSTART_ABSTIME + t2)
987 : (t1 < NOEND_ABSTIME + t2))) /* prevent overflow */
988 PG_RETURN_ABSOLUTETIME(t1 - t2);
990 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
995 * abstimemi - returns the value of (abstime t1 - abstime t2)
997 * This is not exported, so it's not been made fmgr-compatible.
1000 abstimemi(AbsoluteTime t1, AbsoluteTime t2)
1002 if (t1 == CURRENT_ABSTIME)
1003 t1 = GetCurrentTransactionStartTime();
1004 if (t2 == CURRENT_ABSTIME)
1005 t2 = GetCurrentTransactionStartTime();
1007 if (AbsoluteTimeIsReal(t1) &&
1008 AbsoluteTimeIsReal(t2))
1011 return INVALID_RELTIME;
1016 * intinterval - returns true iff absolute date is in the interval
1019 intinterval(PG_FUNCTION_ARGS)
1021 AbsoluteTime t = PG_GETARG_ABSOLUTETIME(0);
1022 TimeInterval interval = PG_GETARG_TIMEINTERVAL(1);
1024 if (interval->status == T_INTERVAL_VALID && t != INVALID_ABSTIME)
1026 if (DatumGetBool(DirectFunctionCall2(abstimege,
1027 AbsoluteTimeGetDatum(t),
1028 AbsoluteTimeGetDatum(interval->data[0]))) &&
1029 DatumGetBool(DirectFunctionCall2(abstimele,
1030 AbsoluteTimeGetDatum(t),
1031 AbsoluteTimeGetDatum(interval->data[1]))))
1032 PG_RETURN_BOOL(true);
1034 PG_RETURN_BOOL(false);
1038 * tintervalrel - returns relative time corresponding to interval
1041 tintervalrel(PG_FUNCTION_ARGS)
1043 TimeInterval interval = PG_GETARG_TIMEINTERVAL(0);
1045 if (interval->status != T_INTERVAL_VALID)
1046 PG_RETURN_RELATIVETIME(INVALID_RELTIME);
1048 PG_RETURN_RELATIVETIME(abstimemi(interval->data[1], interval->data[0]));
1052 * timenow - returns time "now", internal format
1054 * Now AbsoluteTime is time since Jan 1 1970 -mer 7 Feb 1992
1057 timenow(PG_FUNCTION_ARGS)
1062 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1063 PG_RETURN_ABSOLUTETIME((AbsoluteTime) sec);
1067 * reltimeeq - returns true iff arguments are equal
1068 * reltimene - returns true iff arguments are not equal
1069 * reltimelt - returns true iff t1 less than t2
1070 * reltimegt - returns true iff t1 greater than t2
1071 * reltimele - returns true iff t1 less than or equal to t2
1072 * reltimege - returns true iff t1 greater than or equal to t2
1075 reltimeeq(PG_FUNCTION_ARGS)
1077 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1078 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1080 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1081 PG_RETURN_BOOL(false);
1082 PG_RETURN_BOOL(t1 == t2);
1086 reltimene(PG_FUNCTION_ARGS)
1088 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1089 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1091 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1092 PG_RETURN_BOOL(false);
1093 PG_RETURN_BOOL(t1 != t2);
1097 reltimelt(PG_FUNCTION_ARGS)
1099 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1100 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1102 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1103 PG_RETURN_BOOL(false);
1104 PG_RETURN_BOOL(t1 < t2);
1108 reltimegt(PG_FUNCTION_ARGS)
1110 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1111 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1113 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1114 PG_RETURN_BOOL(false);
1115 PG_RETURN_BOOL(t1 > t2);
1119 reltimele(PG_FUNCTION_ARGS)
1121 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1122 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1124 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1125 PG_RETURN_BOOL(false);
1126 PG_RETURN_BOOL(t1 <= t2);
1130 reltimege(PG_FUNCTION_ARGS)
1132 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1133 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1135 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1136 PG_RETURN_BOOL(false);
1137 PG_RETURN_BOOL(t1 >= t2);
1142 * tintervalsame - returns true iff interval i1 is same as interval i2
1143 * Check begin and end time.
1146 tintervalsame(PG_FUNCTION_ARGS)
1148 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1149 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1151 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1152 PG_RETURN_BOOL(false);
1154 if (DatumGetBool(DirectFunctionCall2(abstimeeq,
1155 AbsoluteTimeGetDatum(i1->data[0]),
1156 AbsoluteTimeGetDatum(i2->data[0]))) &&
1157 DatumGetBool(DirectFunctionCall2(abstimeeq,
1158 AbsoluteTimeGetDatum(i1->data[1]),
1159 AbsoluteTimeGetDatum(i2->data[1]))))
1160 PG_RETURN_BOOL(true);
1161 PG_RETURN_BOOL(false);
1166 * tintervaleq - returns true iff interval i1 is equal to interval i2
1167 * Check length of intervals.
1170 tintervaleq(PG_FUNCTION_ARGS)
1172 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1173 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1179 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1180 PG_RETURN_BOOL(false);
1187 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1188 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1189 PG_RETURN_BOOL(false);
1191 if (t10 == CURRENT_ABSTIME)
1192 t10 = GetCurrentTransactionStartTime();
1193 if (t11 == CURRENT_ABSTIME)
1194 t11 = GetCurrentTransactionStartTime();
1195 if (t20 == CURRENT_ABSTIME)
1196 t20 = GetCurrentTransactionStartTime();
1197 if (t21 == CURRENT_ABSTIME)
1198 t21 = GetCurrentTransactionStartTime();
1200 PG_RETURN_BOOL((t11 - t10) == (t21 - t20));
1204 tintervalne(PG_FUNCTION_ARGS)
1206 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1207 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1213 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1214 PG_RETURN_BOOL(false);
1221 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1222 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1223 PG_RETURN_BOOL(false);
1225 if (t10 == CURRENT_ABSTIME)
1226 t10 = GetCurrentTransactionStartTime();
1227 if (t11 == CURRENT_ABSTIME)
1228 t11 = GetCurrentTransactionStartTime();
1229 if (t20 == CURRENT_ABSTIME)
1230 t20 = GetCurrentTransactionStartTime();
1231 if (t21 == CURRENT_ABSTIME)
1232 t21 = GetCurrentTransactionStartTime();
1234 PG_RETURN_BOOL((t11 - t10) != (t21 - t20));
1238 tintervallt(PG_FUNCTION_ARGS)
1240 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1241 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1247 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1248 PG_RETURN_BOOL(false);
1255 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1256 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1257 PG_RETURN_BOOL(false);
1259 if (t10 == CURRENT_ABSTIME)
1260 t10 = GetCurrentTransactionStartTime();
1261 if (t11 == CURRENT_ABSTIME)
1262 t11 = GetCurrentTransactionStartTime();
1263 if (t20 == CURRENT_ABSTIME)
1264 t20 = GetCurrentTransactionStartTime();
1265 if (t21 == CURRENT_ABSTIME)
1266 t21 = GetCurrentTransactionStartTime();
1268 PG_RETURN_BOOL((t11 - t10) < (t21 - t20));
1272 tintervalle(PG_FUNCTION_ARGS)
1274 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1275 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1281 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1282 PG_RETURN_BOOL(false);
1289 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1290 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1291 PG_RETURN_BOOL(false);
1293 if (t10 == CURRENT_ABSTIME)
1294 t10 = GetCurrentTransactionStartTime();
1295 if (t11 == CURRENT_ABSTIME)
1296 t11 = GetCurrentTransactionStartTime();
1297 if (t20 == CURRENT_ABSTIME)
1298 t20 = GetCurrentTransactionStartTime();
1299 if (t21 == CURRENT_ABSTIME)
1300 t21 = GetCurrentTransactionStartTime();
1302 PG_RETURN_BOOL((t11 - t10) <= (t21 - t20));
1306 tintervalgt(PG_FUNCTION_ARGS)
1308 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1309 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1315 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1316 PG_RETURN_BOOL(false);
1323 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1324 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1325 PG_RETURN_BOOL(false);
1327 if (t10 == CURRENT_ABSTIME)
1328 t10 = GetCurrentTransactionStartTime();
1329 if (t11 == CURRENT_ABSTIME)
1330 t11 = GetCurrentTransactionStartTime();
1331 if (t20 == CURRENT_ABSTIME)
1332 t20 = GetCurrentTransactionStartTime();
1333 if (t21 == CURRENT_ABSTIME)
1334 t21 = GetCurrentTransactionStartTime();
1336 PG_RETURN_BOOL((t11 - t10) > (t21 - t20));
1340 tintervalge(PG_FUNCTION_ARGS)
1342 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1343 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1349 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1350 PG_RETURN_BOOL(false);
1357 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1358 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1359 PG_RETURN_BOOL(false);
1361 if (t10 == CURRENT_ABSTIME)
1362 t10 = GetCurrentTransactionStartTime();
1363 if (t11 == CURRENT_ABSTIME)
1364 t11 = GetCurrentTransactionStartTime();
1365 if (t20 == CURRENT_ABSTIME)
1366 t20 = GetCurrentTransactionStartTime();
1367 if (t21 == CURRENT_ABSTIME)
1368 t21 = GetCurrentTransactionStartTime();
1370 PG_RETURN_BOOL((t11 - t10) >= (t21 - t20));
1375 * tintervalleneq - returns true iff length of interval i is equal to
1377 * tintervallenne - returns true iff length of interval i is not equal
1379 * tintervallenlt - returns true iff length of interval i is less than
1381 * tintervallengt - returns true iff length of interval i is greater
1383 * tintervallenle - returns true iff length of interval i is less or
1384 * equal than reltime t
1385 * tintervallenge - returns true iff length of interval i is greater or
1386 * equal than reltime t
1389 tintervalleneq(PG_FUNCTION_ARGS)
1391 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1392 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1395 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1396 PG_RETURN_BOOL(false);
1397 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1398 TimeIntervalGetDatum(i)));
1399 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt == t);
1403 tintervallenne(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 tintervallenlt(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 tintervallengt(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 tintervallenle(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 tintervallenge(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 * tintervalct - returns true iff interval i1 contains interval i2
1476 tintervalct(PG_FUNCTION_ARGS)
1478 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1479 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1481 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1482 PG_RETURN_BOOL(false);
1483 if (DatumGetBool(DirectFunctionCall2(abstimele,
1484 AbsoluteTimeGetDatum(i1->data[0]),
1485 AbsoluteTimeGetDatum(i2->data[0]))) &&
1486 DatumGetBool(DirectFunctionCall2(abstimege,
1487 AbsoluteTimeGetDatum(i1->data[1]),
1488 AbsoluteTimeGetDatum(i2->data[1]))))
1489 PG_RETURN_BOOL(true);
1490 PG_RETURN_BOOL(false);
1494 * tintervalov - returns true iff interval i1 (partially) overlaps i2
1497 tintervalov(PG_FUNCTION_ARGS)
1499 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1500 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1502 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1503 PG_RETURN_BOOL(false);
1504 if (DatumGetBool(DirectFunctionCall2(abstimelt,
1505 AbsoluteTimeGetDatum(i1->data[1]),
1506 AbsoluteTimeGetDatum(i2->data[0]))) ||
1507 DatumGetBool(DirectFunctionCall2(abstimegt,
1508 AbsoluteTimeGetDatum(i1->data[0]),
1509 AbsoluteTimeGetDatum(i2->data[1]))))
1510 PG_RETURN_BOOL(false);
1511 PG_RETURN_BOOL(true);
1515 * tintervalstart - returns the start of interval i
1518 tintervalstart(PG_FUNCTION_ARGS)
1520 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1522 if (i->status == T_INTERVAL_INVAL)
1523 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1524 PG_RETURN_ABSOLUTETIME(i->data[0]);
1528 * tintervalend - returns the end of interval i
1531 tintervalend(PG_FUNCTION_ARGS)
1533 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1535 if (i->status == T_INTERVAL_INVAL)
1536 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1537 PG_RETURN_ABSOLUTETIME(i->data[1]);
1541 /*****************************************************************************
1542 * PRIVATE ROUTINES *
1543 *****************************************************************************/
1547 * isreltime - returns 1, iff datestring is of type reltime
1548 * 2, iff datestring is 'invalid time' identifier
1549 * 0, iff datestring contains a syntax error
1550 * VALID time less or equal +/- `@ 68 years'
1554 isreltime(char *str)
1560 char *field[MAXDATEFIELDS];
1562 ftype[MAXDATEFIELDS];
1563 char lowstr[MAXDATELEN + 1];
1565 if (!PointerIsValid(str))
1568 if (strlen(str) > MAXDATELEN)
1571 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
1572 || (DecodeDateDelta(field, ftype, nf, &dtype, tm, &fsec) != 0))
1578 return (abs(tm->tm_year) <= 68) ? 1 : 0;
1602 char unit[UNITMAXLEN];
1603 char direction[DIRMAXLEN];
1605 int localUnitNumber;
1608 if (!PointerIsValid(sign))
1611 if (!PointerIsValid(unitnr))
1612 unitnr = &localUnitNumber;
1614 if (!PointerIsValid(quantity))
1615 quantity = &localQuantity;
1618 direction[0] = '\0';
1620 /* skip leading blanks */
1621 while ((c = *p) != '\0')
1628 /* Test whether 'invalid time' identifier or not */
1629 if (!strncmp(INVALID_RELTIME_STR, p, strlen(INVALID_RELTIME_STR) + 1))
1630 return 2; /* correct 'invalid time' identifier found */
1632 /* handle label of relative time */
1633 if (c != RELTIME_LABEL)
1634 return 0; /* syntax error */
1637 return 0; /* syntax error */
1639 /* handle the quantity */
1644 if (isdigit((unsigned char) c))
1646 *quantity = *quantity * 10 + (c - '0');
1652 break; /* correct quantity found */
1654 return 0; /* syntax error */
1664 if (c >= 'a' && c <= 'z' && i <= (UNITMAXLEN - 1))
1672 if ((c == ' ' || c == '\0')
1673 && correct_unit(unit, unitnr))
1674 break; /* correct unit found */
1676 return 0; /* syntax error */
1680 /* handle optional direction */
1688 if (c >= 'a' && c <= 'z' && i <= (DIRMAXLEN - 1))
1696 if ((c == ' ' || c == '\0') && i == 0)
1699 break; /* no direction specified */
1701 if ((c == ' ' || c == '\0') && i != 0)
1703 direction[i] = '\0';
1704 correct_dir(direction, sign);
1705 break; /* correct direction found */
1708 return 0; /* syntax error */
1716 * correct_unit - returns 1, iff unit is a correct unit description
1719 * unptr: points to an integer which is the appropriate unit number
1720 * (see function isreltime())
1723 correct_unit(char *unit, int *unptr)
1729 if (strncmp(unit, unit_tab[j], strlen(unit_tab[j])) == 0)
1736 return 0; /* invalid unit descriptor */
1740 * correct_dir - returns 1, iff direction is a correct identifier
1743 * signptr: points to -1 if dir corresponds to past tense
1747 correct_dir(char *direction, int *signptr)
1750 if (strncmp(RELTIME_PAST, direction, strlen(RELTIME_PAST) + 1) == 0)
1756 return 0; /* invalid direction descriptor */
1762 * istinterval - returns 1, iff i_string is a valid interval descr.
1763 * 0, iff i_string is NOT a valid interval desc.
1764 * 2, iff any time is INVALID_ABSTIME
1767 * i_start, i_end: interval margins
1770 * `[' {` '} `'' <AbsTime> `'' {` '} `'' <AbsTime> `'' {` '} `]'
1772 * OR `Undefined Range' (see also INVALID_INTERVAL_STR)
1774 * where <AbsTime> satisfies the syntax of absolute time.
1776 * e.g. [ ' Jan 18 1902' 'Jan 1 00:00:00 1970']
1779 istinterval(char *i_string,
1780 AbsoluteTime *i_start,
1781 AbsoluteTime *i_end)
1788 /* skip leading blanks up to '[' */
1789 while ((c = *p) != '\0')
1794 return 0; /* syntax error */
1799 /* skip leading blanks up to "'" */
1800 while ((c = *p) != '\0')
1805 return 0; /* syntax error */
1810 if (strncmp(INVALID_INTERVAL_STR, p, strlen(INVALID_INTERVAL_STR)) == 0)
1811 return 0; /* undefined range, handled like a syntax
1813 /* search for the end of the first date and change it to a NULL */
1815 while ((c = *p1) != '\0')
1824 /* get the first date */
1825 *i_start = DatumGetAbsoluteTime(DirectFunctionCall1(nabstimein,
1826 CStringGetDatum(p)));
1827 /* rechange NULL at the end of the first date to a "'" */
1830 /* skip blanks up to "'", beginning of second date */
1831 while ((c = *p) != '\0')
1836 return 0; /* syntax error */
1841 /* search for the end of the second date and change it to a NULL */
1843 while ((c = *p1) != '\0')
1852 /* get the second date */
1853 *i_end = DatumGetAbsoluteTime(DirectFunctionCall1(nabstimein,
1854 CStringGetDatum(p)));
1855 /* rechange NULL at the end of the first date to a ''' */
1858 /* skip blanks up to ']' */
1859 while ((c = *p) != '\0')
1864 return 0; /* syntax error */
1871 return 0; /* syntax error */
1872 /* it seems to be a valid interval */
1877 /*****************************************************************************
1879 *****************************************************************************/
1882 int4reltime(PG_FUNCTION_ARGS)
1884 int32 timevalue = PG_GETARG_INT32(0);
1886 /* Just coerce it directly to RelativeTime ... */
1887 PG_RETURN_RELATIVETIME((RelativeTime) timevalue);
1892 * returns the current time as a text. similar to timenow() but returns
1893 * seconds with more precision (up to microsecs). (I need this to compare
1894 * the Wisconsin benchmark with Illustra whose TimeNow() shows current
1895 * time with precision up to microsecs.) - ay 3/95
1898 timeofday(PG_FUNCTION_ARGS)
1901 struct timezone tpz;
1907 gettimeofday(&tp, &tpz);
1908 strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%06d %Y %Z",
1909 localtime((time_t *) &tp.tv_sec));
1910 snprintf(buf, sizeof(buf), templ, tp.tv_usec);
1912 len = VARHDRSZ + strlen(buf);
1913 result = (text *) palloc(len);
1914 VARATT_SIZEP(result) = len;
1915 memcpy(VARDATA(result), buf, strlen(buf));
1916 PG_RETURN_TEXT_P(result);