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.82 2001/02/21 22:03:00 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;
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 HAVE_INT_TIMEZONE */
166 CTimeZone = tb.timezone * 60;
167 CDayLight = (tb.dstflag != 0);
170 * XXX does this work to get the local timezone string in V7? -
173 strftime(CTZName, MAXTZLEN, "%Z", localtime(&now));
177 return (AbsoluteTime) now;
178 } /* GetCurrentAbsoluteTime() */
182 GetCurrentTime(struct tm * tm)
186 abstime2tm(GetCurrentTransactionStartTime(), &tz, tm, NULL);
189 } /* GetCurrentTime() */
193 abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char *tzn)
195 time_t time = (time_t) _time;
196 #if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
200 struct timeb tb; /* the old V7-ism */
206 #if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
209 tx = localtime((time_t *) &time);
210 # ifdef NO_MKTIME_BEFORE_1970
211 if (tx->tm_year < 70 && tx->tm_isdst == 1)
214 tx = localtime((time_t *) &time);
221 tx = gmtime((time_t *) &time);
224 tm->tm_year = tx->tm_year + 1900;
225 tm->tm_mon = tx->tm_mon + 1;
226 tm->tm_mday = tx->tm_mday;
227 tm->tm_hour = tx->tm_hour;
228 tm->tm_min = tx->tm_min;
229 tm->tm_sec = tx->tm_sec;
230 tm->tm_isdst = tx->tm_isdst;
232 # if defined(HAVE_TM_ZONE)
233 tm->tm_gmtoff = tx->tm_gmtoff;
234 tm->tm_zone = tx->tm_zone;
237 *tzp = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
238 /* XXX FreeBSD man pages indicate that this should work - tgl 97/04/23 */
243 * Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
244 * contains an error message, which doesn't fit in the buffer
246 StrNCpy(tzn, tm->tm_zone, MAXTZLEN+1);
247 if (strlen(tm->tm_zone) > MAXTZLEN)
248 elog(NOTICE, "Invalid timezone \'%s\'", tm->tm_zone);
250 # elif defined(HAVE_INT_TIMEZONE)
252 *tzp = ((tm->tm_isdst > 0) ? (TIMEZONE_GLOBAL - 3600) : TIMEZONE_GLOBAL);
258 * Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
259 * contains an error message, which doesn't fit in the buffer
261 StrNCpy(tzn, tzname[tm->tm_isdst], MAXTZLEN+1);
262 if (strlen(tzname[tm->tm_isdst]) > MAXTZLEN)
263 elog(NOTICE, "Invalid timezone \'%s\'", tzname[tm->tm_isdst]);
266 #else /* not (HAVE_TM_ZONE || HAVE_INT_TIMEZONE) */
268 *tzp = tb.timezone * 60;
271 * XXX does this work to get the local timezone string in V7? - tgl
276 strftime(tzn, MAXTZLEN, "%Z", localtime(&now));
277 tzn[MAXTZLEN] = '\0'; /* let's just be sure it's null-terminated */
286 * Convert a tm structure to abstime.
287 * Note that tm has full year (not 1900-based) and 1-based month.
290 tm2abstime(struct tm * tm, int tz)
295 /* validate, before going out of range on some members */
296 if (tm->tm_year < 1901 || tm->tm_year > 2038
297 || tm->tm_mon < 1 || tm->tm_mon > 12
298 || tm->tm_mday < 1 || tm->tm_mday > 31
299 || tm->tm_hour < 0 || tm->tm_hour >= 24
300 || tm->tm_min < 0 || tm->tm_min > 59
301 || tm->tm_sec < 0 || tm->tm_sec > 59)
302 return INVALID_ABSTIME;
304 day = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(1970, 1, 1));
306 /* check for time out of range */
307 if ((day < MIN_DAYNUM) || (day > MAX_DAYNUM))
308 return INVALID_ABSTIME;
310 /* convert to seconds */
311 sec = tm->tm_sec + tz + (tm->tm_min + (day * 24 + tm->tm_hour) * 60) * 60;
313 /* check for overflow */
314 if ((day == MAX_DAYNUM && sec < 0) ||
315 (day == MIN_DAYNUM && sec > 0))
316 return INVALID_ABSTIME;
318 /* check for reserved values (e.g. "current" on edge of usual range */
319 if (!AbsoluteTimeIsReal(sec))
320 return INVALID_ABSTIME;
327 * Decode date/time string and return abstime.
330 nabstimein(PG_FUNCTION_ARGS)
332 char *str = PG_GETARG_CSTRING(0);
338 char *field[MAXDATEFIELDS];
339 char lowstr[MAXDATELEN + 1];
342 ftype[MAXDATEFIELDS];
344 if (strlen(str) > MAXDATELEN)
345 elog(ERROR, "Bad (length) abstime external representation '%s'", str);
347 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
348 || (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
349 elog(ERROR, "Bad abstime external representation '%s'", str);
354 result = tm2abstime(tm, tz);
358 result = EPOCH_ABSTIME;
362 result = CURRENT_ABSTIME;
366 result = NOEND_ABSTIME;
370 result = NOSTART_ABSTIME;
374 result = INVALID_ABSTIME;
378 elog(ERROR, "Bad abstime (internal coding error) '%s'", str);
379 result = INVALID_ABSTIME;
383 PG_RETURN_ABSOLUTETIME(result);
388 * Given an AbsoluteTime return the English text version of the date
391 nabstimeout(PG_FUNCTION_ARGS)
393 AbsoluteTime time = PG_GETARG_ABSOLUTETIME(0);
399 char buf[MAXDATELEN + 1];
400 char zone[MAXDATELEN + 1],
408 case INVALID_ABSTIME:
409 strcpy(buf, INVALID);
411 case CURRENT_ABSTIME:
412 strcpy(buf, DCURRENT);
417 case NOSTART_ABSTIME:
421 abstime2tm(time, &tz, tm, tzn);
422 EncodeDateTime(tm, fsec, &tz, &tzn, DateStyle, buf);
426 result = pstrdup(buf);
427 PG_RETURN_CSTRING(result);
432 * AbsoluteTimeIsBefore -- true iff time1 is before time2.
433 * AbsoluteTimeIsBefore -- true iff time1 is after time2.
436 AbsoluteTimeIsBefore(AbsoluteTime time1, AbsoluteTime time2)
438 Assert(AbsoluteTimeIsValid(time1));
439 Assert(AbsoluteTimeIsValid(time2));
441 if (time1 == CURRENT_ABSTIME)
442 time1 = GetCurrentTransactionStartTime();
444 if (time2 == CURRENT_ABSTIME)
445 time2 = GetCurrentTransactionStartTime();
447 return time1 < time2;
452 AbsoluteTimeIsAfter(AbsoluteTime time1, AbsoluteTime time2)
454 Assert(AbsoluteTimeIsValid(time1));
455 Assert(AbsoluteTimeIsValid(time2));
457 if (time1 == CURRENT_ABSTIME)
458 time1 = GetCurrentTransactionStartTime();
460 if (time2 == CURRENT_ABSTIME)
461 time2 = GetCurrentTransactionStartTime();
463 return time1 > time2;
471 abstime_finite(PG_FUNCTION_ARGS)
473 AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
475 PG_RETURN_BOOL((abstime != INVALID_ABSTIME) &&
476 (abstime != NOSTART_ABSTIME) &&
477 (abstime != NOEND_ABSTIME));
482 * abstimeeq - returns true iff arguments are equal
483 * abstimene - returns true iff arguments are not equal
484 * abstimelt - returns true iff t1 less than t2
485 * abstimegt - returns true iff t1 greater than t2
486 * abstimele - returns true iff t1 less than or equal to t2
487 * abstimege - returns true iff t1 greater than or equal to t2
490 abstimeeq(PG_FUNCTION_ARGS)
492 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
493 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
495 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
496 PG_RETURN_BOOL(false);
497 if (t1 == CURRENT_ABSTIME)
498 t1 = GetCurrentTransactionStartTime();
499 if (t2 == CURRENT_ABSTIME)
500 t2 = GetCurrentTransactionStartTime();
502 PG_RETURN_BOOL(t1 == t2);
506 abstimene(PG_FUNCTION_ARGS)
508 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
509 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
511 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
512 PG_RETURN_BOOL(false);
513 if (t1 == CURRENT_ABSTIME)
514 t1 = GetCurrentTransactionStartTime();
515 if (t2 == CURRENT_ABSTIME)
516 t2 = GetCurrentTransactionStartTime();
518 PG_RETURN_BOOL(t1 != t2);
522 abstimelt(PG_FUNCTION_ARGS)
524 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
525 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
527 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
528 PG_RETURN_BOOL(false);
529 if (t1 == CURRENT_ABSTIME)
530 t1 = GetCurrentTransactionStartTime();
531 if (t2 == CURRENT_ABSTIME)
532 t2 = GetCurrentTransactionStartTime();
534 PG_RETURN_BOOL(t1 < t2);
538 abstimegt(PG_FUNCTION_ARGS)
540 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
541 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
543 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
544 PG_RETURN_BOOL(false);
545 if (t1 == CURRENT_ABSTIME)
546 t1 = GetCurrentTransactionStartTime();
547 if (t2 == CURRENT_ABSTIME)
548 t2 = GetCurrentTransactionStartTime();
550 PG_RETURN_BOOL(t1 > t2);
554 abstimele(PG_FUNCTION_ARGS)
556 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
557 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
559 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
560 PG_RETURN_BOOL(false);
561 if (t1 == CURRENT_ABSTIME)
562 t1 = GetCurrentTransactionStartTime();
563 if (t2 == CURRENT_ABSTIME)
564 t2 = GetCurrentTransactionStartTime();
566 PG_RETURN_BOOL(t1 <= t2);
570 abstimege(PG_FUNCTION_ARGS)
572 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
573 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
575 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
576 PG_RETURN_BOOL(false);
577 if (t1 == CURRENT_ABSTIME)
578 t1 = GetCurrentTransactionStartTime();
579 if (t2 == CURRENT_ABSTIME)
580 t2 = GetCurrentTransactionStartTime();
582 PG_RETURN_BOOL(t1 >= t2);
586 /* datetime_abstime()
587 * Convert timestamp to abstime.
590 timestamp_abstime(PG_FUNCTION_ARGS)
592 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
598 if (TIMESTAMP_IS_INVALID(timestamp))
600 result = INVALID_ABSTIME;
602 else if (TIMESTAMP_IS_NOBEGIN(timestamp))
604 result = NOSTART_ABSTIME;
606 else if (TIMESTAMP_IS_NOEND(timestamp))
608 result = NOEND_ABSTIME;
612 if (TIMESTAMP_IS_RELATIVE(timestamp))
614 timestamp2tm(SetTimestamp(timestamp), NULL, tm, &fsec, NULL);
615 result = tm2abstime(tm, 0);
617 else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) == 0)
619 result = tm2abstime(tm, 0);
623 result = INVALID_ABSTIME;
627 PG_RETURN_ABSOLUTETIME(result);
630 /* abstime_timestamp()
631 * Convert abstime to timestamp.
634 abstime_timestamp(PG_FUNCTION_ARGS)
636 AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
641 case INVALID_ABSTIME:
642 TIMESTAMP_INVALID(result);
645 case NOSTART_ABSTIME:
646 TIMESTAMP_NOBEGIN(result);
650 TIMESTAMP_NOEND(result);
654 TIMESTAMP_EPOCH(result);
657 case CURRENT_ABSTIME:
658 TIMESTAMP_CURRENT(result);
662 result = abstime + ((date2j(1970, 1, 1) - date2j(2000, 1, 1)) * 86400);
666 PG_RETURN_TIMESTAMP(result);
670 /*****************************************************************************
671 * USER I/O ROUTINES *
672 *****************************************************************************/
675 * reltimein - converts a reltime string in an internal format
678 reltimein(PG_FUNCTION_ARGS)
680 char *str = PG_GETARG_CSTRING(0);
686 char *field[MAXDATEFIELDS];
688 ftype[MAXDATEFIELDS];
689 char lowstr[MAXDATELEN + 1];
691 if (strlen(str) > MAXDATELEN)
692 elog(ERROR, "Bad (length) reltime external representation '%s'", str);
694 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
695 || (DecodeDateDelta(field, ftype, nf, &dtype, tm, &fsec) != 0))
696 elog(ERROR, "Bad reltime external representation '%s'", str);
701 result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec);
702 result += (((tm->tm_year * 365) + (tm->tm_mon * 30) + tm->tm_mday) * (24 * 60 * 60));
703 PG_RETURN_RELATIVETIME(result);
706 PG_RETURN_RELATIVETIME(INVALID_RELTIME);
709 elog(ERROR, "Bad reltime (internal coding error) '%s'", str);
710 PG_RETURN_RELATIVETIME(INVALID_RELTIME);
715 * reltimeout - converts the internal format to a reltime string
718 reltimeout(PG_FUNCTION_ARGS)
720 RelativeTime time = PG_GETARG_RELATIVETIME(0);
724 char buf[MAXDATELEN + 1];
726 if (time == INVALID_RELTIME)
728 strcpy(buf, INVALID_RELTIME_STR);
732 reltime2tm(time, tm);
733 EncodeTimeSpan(tm, 0, DateStyle, buf);
736 result = pstrdup(buf);
737 PG_RETURN_CSTRING(result);
742 reltime2tm(RelativeTime time, struct tm * tm)
744 TMODULO(time, tm->tm_year, 31536000);
745 TMODULO(time, tm->tm_mon, 2592000);
746 TMODULO(time, tm->tm_mday, 86400);
747 TMODULO(time, tm->tm_hour, 3600);
748 TMODULO(time, tm->tm_min, 60);
749 TMODULO(time, tm->tm_sec, 1);
763 timestring = (char *) palloc(Max(strlen(INVALID_RELTIME_STR),
765 if (timevalue == INVALID_RELTIME)
767 strcpy(timestring, INVALID_RELTIME_STR);
772 i = 1; /* unit = 'seconds' */
774 for (i = 12; i >= 0; i = i - 2)
775 if ((timevalue % sec_tab[i]) == 0)
776 break; /* appropriate unit found */
778 quantity = (timevalue / sec_tab[unitnr]);
779 if (quantity > 1 || quantity < -1)
780 unitnr++; /* adjust index for PLURAL of unit */
782 sprintf(timestring, "%c %lu %s", RELTIME_LABEL,
783 quantity, unit_tab[unitnr]);
785 sprintf(timestring, "%c %lu %s %s", RELTIME_LABEL,
786 (quantity * -1), unit_tab[unitnr], RELTIME_PAST);
794 * tintervalin - converts an interval string to internal format
797 tintervalin(PG_FUNCTION_ARGS)
799 char *intervalstr = PG_GETARG_CSTRING(0);
800 TimeInterval interval;
802 AbsoluteTime i_start,
807 interval = (TimeInterval) palloc(sizeof(TimeIntervalData));
808 error = istinterval(intervalstr, &t1, &t2);
810 interval->status = T_INTERVAL_INVAL;
811 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
812 interval->status = T_INTERVAL_INVAL; /* undefined */
815 i_start = ABSTIMEMIN(t1, t2);
816 i_end = ABSTIMEMAX(t1, t2);
817 interval->data[0] = i_start;
818 interval->data[1] = i_end;
819 interval->status = T_INTERVAL_VALID;
821 PG_RETURN_TIMEINTERVAL(interval);
826 * tintervalout - converts an internal interval format to a string
830 tintervalout(PG_FUNCTION_ARGS)
832 TimeInterval interval = PG_GETARG_TIMEINTERVAL(0);
836 i_str = (char *) palloc(T_INTERVAL_LEN); /* ['...' '...'] */
837 strcpy(i_str, "[\"");
838 if (interval->status == T_INTERVAL_INVAL)
839 strcat(i_str, INVALID_INTERVAL_STR);
842 p = DatumGetCString(DirectFunctionCall1(nabstimeout,
843 AbsoluteTimeGetDatum(interval->data[0])));
846 strcat(i_str, "\" \"");
847 p = DatumGetCString(DirectFunctionCall1(nabstimeout,
848 AbsoluteTimeGetDatum(interval->data[1])));
852 strcat(i_str, "\"]\0");
853 PG_RETURN_CSTRING(i_str);
857 /*****************************************************************************
859 *****************************************************************************/
862 interval_reltime(PG_FUNCTION_ARGS)
864 Interval *interval = PG_GETARG_INTERVAL_P(0);
870 if (INTERVAL_IS_INVALID(*interval))
872 time = INVALID_RELTIME;
876 if (interval->month == 0)
881 else if (abs(interval->month) >= 12)
883 year = (interval->month / 12);
884 month = (interval->month % 12);
889 month = interval->month;
892 span = (((((double) 365 * year) + ((double) 30 * month)) * 86400) + interval->time);
894 time = (((span > INT_MIN) && (span < INT_MAX)) ? span : INVALID_RELTIME);
897 PG_RETURN_RELATIVETIME(time);
902 reltime_interval(PG_FUNCTION_ARGS)
904 RelativeTime reltime = PG_GETARG_RELATIVETIME(0);
909 result = (Interval *) palloc(sizeof(Interval));
913 case INVALID_RELTIME:
914 INTERVAL_INVALID(*result);
918 TMODULO(reltime, year, 31536000);
919 TMODULO(reltime, month, 2592000);
921 result->time = reltime;
922 result->month = ((12 * year) + month);
926 PG_RETURN_INTERVAL_P(result);
931 * mktinterval - creates a time interval with endpoints t1 and t2
934 mktinterval(PG_FUNCTION_ARGS)
936 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
937 AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
938 AbsoluteTime tstart = ABSTIMEMIN(t1, t2),
939 tend = ABSTIMEMAX(t1, t2);
940 TimeInterval interval;
942 interval = (TimeInterval) palloc(sizeof(TimeIntervalData));
943 if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
944 interval->status = T_INTERVAL_INVAL;
947 interval->status = T_INTERVAL_VALID;
948 interval->data[0] = tstart;
949 interval->data[1] = tend;
952 PG_RETURN_TIMEINTERVAL(interval);
956 * timepl, timemi and abstimemi use the formula
957 * abstime + reltime = abstime
958 * so abstime - reltime = abstime
959 * and abstime - abstime = reltime
963 * timepl - returns the value of (abstime t1 + relime t2)
966 timepl(PG_FUNCTION_ARGS)
968 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
969 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
971 if (t1 == CURRENT_ABSTIME)
972 t1 = GetCurrentTransactionStartTime();
974 if (AbsoluteTimeIsReal(t1) &&
975 RelativeTimeIsValid(t2) &&
976 ((t2 > 0) ? (t1 < NOEND_ABSTIME - t2)
977 : (t1 > NOSTART_ABSTIME - t2))) /* prevent overflow */
978 PG_RETURN_ABSOLUTETIME(t1 + t2);
980 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
985 * timemi - returns the value of (abstime t1 - reltime t2)
988 timemi(PG_FUNCTION_ARGS)
990 AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
991 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
993 if (t1 == CURRENT_ABSTIME)
994 t1 = GetCurrentTransactionStartTime();
996 if (AbsoluteTimeIsReal(t1) &&
997 RelativeTimeIsValid(t2) &&
998 ((t2 > 0) ? (t1 > NOSTART_ABSTIME + t2)
999 : (t1 < NOEND_ABSTIME + t2))) /* prevent overflow */
1000 PG_RETURN_ABSOLUTETIME(t1 - t2);
1002 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1007 * abstimemi - returns the value of (abstime t1 - abstime t2)
1009 * This is not exported, so it's not been made fmgr-compatible.
1012 abstimemi(AbsoluteTime t1, AbsoluteTime t2)
1014 if (t1 == CURRENT_ABSTIME)
1015 t1 = GetCurrentTransactionStartTime();
1016 if (t2 == CURRENT_ABSTIME)
1017 t2 = GetCurrentTransactionStartTime();
1019 if (AbsoluteTimeIsReal(t1) &&
1020 AbsoluteTimeIsReal(t2))
1023 return INVALID_RELTIME;
1028 * intinterval - returns true iff absolute date is in the interval
1031 intinterval(PG_FUNCTION_ARGS)
1033 AbsoluteTime t = PG_GETARG_ABSOLUTETIME(0);
1034 TimeInterval interval = PG_GETARG_TIMEINTERVAL(1);
1036 if (interval->status == T_INTERVAL_VALID && t != INVALID_ABSTIME)
1038 if (DatumGetBool(DirectFunctionCall2(abstimege,
1039 AbsoluteTimeGetDatum(t),
1040 AbsoluteTimeGetDatum(interval->data[0]))) &&
1041 DatumGetBool(DirectFunctionCall2(abstimele,
1042 AbsoluteTimeGetDatum(t),
1043 AbsoluteTimeGetDatum(interval->data[1]))))
1044 PG_RETURN_BOOL(true);
1046 PG_RETURN_BOOL(false);
1050 * tintervalrel - returns relative time corresponding to interval
1053 tintervalrel(PG_FUNCTION_ARGS)
1055 TimeInterval interval = PG_GETARG_TIMEINTERVAL(0);
1057 if (interval->status != T_INTERVAL_VALID)
1058 PG_RETURN_RELATIVETIME(INVALID_RELTIME);
1060 PG_RETURN_RELATIVETIME(abstimemi(interval->data[1], interval->data[0]));
1064 * timenow - returns time "now", internal format
1066 * Now AbsoluteTime is time since Jan 1 1970 -mer 7 Feb 1992
1069 timenow(PG_FUNCTION_ARGS)
1074 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1075 PG_RETURN_ABSOLUTETIME((AbsoluteTime) sec);
1079 * reltimeeq - returns true iff arguments are equal
1080 * reltimene - returns true iff arguments are not equal
1081 * reltimelt - returns true iff t1 less than t2
1082 * reltimegt - returns true iff t1 greater than t2
1083 * reltimele - returns true iff t1 less than or equal to t2
1084 * reltimege - returns true iff t1 greater than or equal to t2
1087 reltimeeq(PG_FUNCTION_ARGS)
1089 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1090 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1092 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1093 PG_RETURN_BOOL(false);
1094 PG_RETURN_BOOL(t1 == t2);
1098 reltimene(PG_FUNCTION_ARGS)
1100 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1101 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1103 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1104 PG_RETURN_BOOL(false);
1105 PG_RETURN_BOOL(t1 != t2);
1109 reltimelt(PG_FUNCTION_ARGS)
1111 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1112 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1114 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1115 PG_RETURN_BOOL(false);
1116 PG_RETURN_BOOL(t1 < t2);
1120 reltimegt(PG_FUNCTION_ARGS)
1122 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1123 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1125 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1126 PG_RETURN_BOOL(false);
1127 PG_RETURN_BOOL(t1 > t2);
1131 reltimele(PG_FUNCTION_ARGS)
1133 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1134 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1136 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1137 PG_RETURN_BOOL(false);
1138 PG_RETURN_BOOL(t1 <= t2);
1142 reltimege(PG_FUNCTION_ARGS)
1144 RelativeTime t1 = PG_GETARG_RELATIVETIME(0);
1145 RelativeTime t2 = PG_GETARG_RELATIVETIME(1);
1147 if (t1 == INVALID_RELTIME || t2 == INVALID_RELTIME)
1148 PG_RETURN_BOOL(false);
1149 PG_RETURN_BOOL(t1 >= t2);
1154 * tintervalsame - returns true iff interval i1 is same as interval i2
1155 * Check begin and end time.
1158 tintervalsame(PG_FUNCTION_ARGS)
1160 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1161 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1163 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1164 PG_RETURN_BOOL(false);
1166 if (DatumGetBool(DirectFunctionCall2(abstimeeq,
1167 AbsoluteTimeGetDatum(i1->data[0]),
1168 AbsoluteTimeGetDatum(i2->data[0]))) &&
1169 DatumGetBool(DirectFunctionCall2(abstimeeq,
1170 AbsoluteTimeGetDatum(i1->data[1]),
1171 AbsoluteTimeGetDatum(i2->data[1]))))
1172 PG_RETURN_BOOL(true);
1173 PG_RETURN_BOOL(false);
1178 * tintervaleq - returns true iff interval i1 is equal to interval i2
1179 * Check length of intervals.
1182 tintervaleq(PG_FUNCTION_ARGS)
1184 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1185 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1191 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1192 PG_RETURN_BOOL(false);
1199 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1200 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1201 PG_RETURN_BOOL(false);
1203 if (t10 == CURRENT_ABSTIME)
1204 t10 = GetCurrentTransactionStartTime();
1205 if (t11 == CURRENT_ABSTIME)
1206 t11 = GetCurrentTransactionStartTime();
1207 if (t20 == CURRENT_ABSTIME)
1208 t20 = GetCurrentTransactionStartTime();
1209 if (t21 == CURRENT_ABSTIME)
1210 t21 = GetCurrentTransactionStartTime();
1212 PG_RETURN_BOOL((t11 - t10) == (t21 - t20));
1216 tintervalne(PG_FUNCTION_ARGS)
1218 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1219 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1225 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1226 PG_RETURN_BOOL(false);
1233 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1234 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1235 PG_RETURN_BOOL(false);
1237 if (t10 == CURRENT_ABSTIME)
1238 t10 = GetCurrentTransactionStartTime();
1239 if (t11 == CURRENT_ABSTIME)
1240 t11 = GetCurrentTransactionStartTime();
1241 if (t20 == CURRENT_ABSTIME)
1242 t20 = GetCurrentTransactionStartTime();
1243 if (t21 == CURRENT_ABSTIME)
1244 t21 = GetCurrentTransactionStartTime();
1246 PG_RETURN_BOOL((t11 - t10) != (t21 - t20));
1250 tintervallt(PG_FUNCTION_ARGS)
1252 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1253 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1259 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1260 PG_RETURN_BOOL(false);
1267 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1268 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1269 PG_RETURN_BOOL(false);
1271 if (t10 == CURRENT_ABSTIME)
1272 t10 = GetCurrentTransactionStartTime();
1273 if (t11 == CURRENT_ABSTIME)
1274 t11 = GetCurrentTransactionStartTime();
1275 if (t20 == CURRENT_ABSTIME)
1276 t20 = GetCurrentTransactionStartTime();
1277 if (t21 == CURRENT_ABSTIME)
1278 t21 = GetCurrentTransactionStartTime();
1280 PG_RETURN_BOOL((t11 - t10) < (t21 - t20));
1284 tintervalle(PG_FUNCTION_ARGS)
1286 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1287 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1293 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1294 PG_RETURN_BOOL(false);
1301 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1302 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1303 PG_RETURN_BOOL(false);
1305 if (t10 == CURRENT_ABSTIME)
1306 t10 = GetCurrentTransactionStartTime();
1307 if (t11 == CURRENT_ABSTIME)
1308 t11 = GetCurrentTransactionStartTime();
1309 if (t20 == CURRENT_ABSTIME)
1310 t20 = GetCurrentTransactionStartTime();
1311 if (t21 == CURRENT_ABSTIME)
1312 t21 = GetCurrentTransactionStartTime();
1314 PG_RETURN_BOOL((t11 - t10) <= (t21 - t20));
1318 tintervalgt(PG_FUNCTION_ARGS)
1320 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1321 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1327 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1328 PG_RETURN_BOOL(false);
1335 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1336 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1337 PG_RETURN_BOOL(false);
1339 if (t10 == CURRENT_ABSTIME)
1340 t10 = GetCurrentTransactionStartTime();
1341 if (t11 == CURRENT_ABSTIME)
1342 t11 = GetCurrentTransactionStartTime();
1343 if (t20 == CURRENT_ABSTIME)
1344 t20 = GetCurrentTransactionStartTime();
1345 if (t21 == CURRENT_ABSTIME)
1346 t21 = GetCurrentTransactionStartTime();
1348 PG_RETURN_BOOL((t11 - t10) > (t21 - t20));
1352 tintervalge(PG_FUNCTION_ARGS)
1354 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1355 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1361 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1362 PG_RETURN_BOOL(false);
1369 if ((t10 == INVALID_ABSTIME) || (t20 == INVALID_ABSTIME)
1370 || (t20 == INVALID_ABSTIME) || (t21 == INVALID_ABSTIME))
1371 PG_RETURN_BOOL(false);
1373 if (t10 == CURRENT_ABSTIME)
1374 t10 = GetCurrentTransactionStartTime();
1375 if (t11 == CURRENT_ABSTIME)
1376 t11 = GetCurrentTransactionStartTime();
1377 if (t20 == CURRENT_ABSTIME)
1378 t20 = GetCurrentTransactionStartTime();
1379 if (t21 == CURRENT_ABSTIME)
1380 t21 = GetCurrentTransactionStartTime();
1382 PG_RETURN_BOOL((t11 - t10) >= (t21 - t20));
1387 * tintervalleneq - returns true iff length of interval i is equal to
1389 * tintervallenne - returns true iff length of interval i is not equal
1391 * tintervallenlt - returns true iff length of interval i is less than
1393 * tintervallengt - returns true iff length of interval i is greater
1395 * tintervallenle - returns true iff length of interval i is less or
1396 * equal than reltime t
1397 * tintervallenge - returns true iff length of interval i is greater or
1398 * equal than reltime t
1401 tintervalleneq(PG_FUNCTION_ARGS)
1403 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1404 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1407 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1408 PG_RETURN_BOOL(false);
1409 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1410 TimeIntervalGetDatum(i)));
1411 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt == t);
1415 tintervallenne(PG_FUNCTION_ARGS)
1417 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1418 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1421 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1422 PG_RETURN_BOOL(false);
1423 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1424 TimeIntervalGetDatum(i)));
1425 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt != t);
1429 tintervallenlt(PG_FUNCTION_ARGS)
1431 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1432 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1435 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1436 PG_RETURN_BOOL(false);
1437 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1438 TimeIntervalGetDatum(i)));
1439 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt < t);
1443 tintervallengt(PG_FUNCTION_ARGS)
1445 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1446 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1449 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1450 PG_RETURN_BOOL(false);
1451 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1452 TimeIntervalGetDatum(i)));
1453 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt > t);
1457 tintervallenle(PG_FUNCTION_ARGS)
1459 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1460 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1463 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1464 PG_RETURN_BOOL(false);
1465 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1466 TimeIntervalGetDatum(i)));
1467 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt <= t);
1471 tintervallenge(PG_FUNCTION_ARGS)
1473 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1474 RelativeTime t = PG_GETARG_RELATIVETIME(1);
1477 if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME)
1478 PG_RETURN_BOOL(false);
1479 rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel,
1480 TimeIntervalGetDatum(i)));
1481 PG_RETURN_BOOL(rt != INVALID_RELTIME && rt >= t);
1485 * tintervalct - returns true iff interval i1 contains interval i2
1488 tintervalct(PG_FUNCTION_ARGS)
1490 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1491 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1493 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1494 PG_RETURN_BOOL(false);
1495 if (DatumGetBool(DirectFunctionCall2(abstimele,
1496 AbsoluteTimeGetDatum(i1->data[0]),
1497 AbsoluteTimeGetDatum(i2->data[0]))) &&
1498 DatumGetBool(DirectFunctionCall2(abstimege,
1499 AbsoluteTimeGetDatum(i1->data[1]),
1500 AbsoluteTimeGetDatum(i2->data[1]))))
1501 PG_RETURN_BOOL(true);
1502 PG_RETURN_BOOL(false);
1506 * tintervalov - returns true iff interval i1 (partially) overlaps i2
1509 tintervalov(PG_FUNCTION_ARGS)
1511 TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0);
1512 TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1);
1514 if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL)
1515 PG_RETURN_BOOL(false);
1516 if (DatumGetBool(DirectFunctionCall2(abstimelt,
1517 AbsoluteTimeGetDatum(i1->data[1]),
1518 AbsoluteTimeGetDatum(i2->data[0]))) ||
1519 DatumGetBool(DirectFunctionCall2(abstimegt,
1520 AbsoluteTimeGetDatum(i1->data[0]),
1521 AbsoluteTimeGetDatum(i2->data[1]))))
1522 PG_RETURN_BOOL(false);
1523 PG_RETURN_BOOL(true);
1527 * tintervalstart - returns the start of interval i
1530 tintervalstart(PG_FUNCTION_ARGS)
1532 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1534 if (i->status == T_INTERVAL_INVAL)
1535 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1536 PG_RETURN_ABSOLUTETIME(i->data[0]);
1540 * tintervalend - returns the end of interval i
1543 tintervalend(PG_FUNCTION_ARGS)
1545 TimeInterval i = PG_GETARG_TIMEINTERVAL(0);
1547 if (i->status == T_INTERVAL_INVAL)
1548 PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);
1549 PG_RETURN_ABSOLUTETIME(i->data[1]);
1553 /*****************************************************************************
1554 * PRIVATE ROUTINES *
1555 *****************************************************************************/
1559 * isreltime - returns 1, iff datestring is of type reltime
1560 * 2, iff datestring is 'invalid time' identifier
1561 * 0, iff datestring contains a syntax error
1562 * VALID time less or equal +/- `@ 68 years'
1566 isreltime(char *str)
1572 char *field[MAXDATEFIELDS];
1574 ftype[MAXDATEFIELDS];
1575 char lowstr[MAXDATELEN + 1];
1577 if (!PointerIsValid(str))
1580 if (strlen(str) > MAXDATELEN)
1583 if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
1584 || (DecodeDateDelta(field, ftype, nf, &dtype, tm, &fsec) != 0))
1590 return (abs(tm->tm_year) <= 68) ? 1 : 0;
1614 char unit[UNITMAXLEN];
1615 char direction[DIRMAXLEN];
1617 int localUnitNumber;
1620 if (!PointerIsValid(sign))
1623 if (!PointerIsValid(unitnr))
1624 unitnr = &localUnitNumber;
1626 if (!PointerIsValid(quantity))
1627 quantity = &localQuantity;
1630 direction[0] = '\0';
1632 /* skip leading blanks */
1633 while ((c = *p) != '\0')
1640 /* Test whether 'invalid time' identifier or not */
1641 if (!strncmp(INVALID_RELTIME_STR, p, strlen(INVALID_RELTIME_STR) + 1))
1642 return 2; /* correct 'invalid time' identifier found */
1644 /* handle label of relative time */
1645 if (c != RELTIME_LABEL)
1646 return 0; /* syntax error */
1649 return 0; /* syntax error */
1651 /* handle the quantity */
1656 if (isdigit((unsigned char) c))
1658 *quantity = *quantity * 10 + (c - '0');
1664 break; /* correct quantity found */
1666 return 0; /* syntax error */
1676 if (c >= 'a' && c <= 'z' && i <= (UNITMAXLEN - 1))
1684 if ((c == ' ' || c == '\0')
1685 && correct_unit(unit, unitnr))
1686 break; /* correct unit found */
1688 return 0; /* syntax error */
1692 /* handle optional direction */
1700 if (c >= 'a' && c <= 'z' && i <= (DIRMAXLEN - 1))
1708 if ((c == ' ' || c == '\0') && i == 0)
1711 break; /* no direction specified */
1713 if ((c == ' ' || c == '\0') && i != 0)
1715 direction[i] = '\0';
1716 correct_dir(direction, sign);
1717 break; /* correct direction found */
1720 return 0; /* syntax error */
1728 * correct_unit - returns 1, iff unit is a correct unit description
1731 * unptr: points to an integer which is the appropriate unit number
1732 * (see function isreltime())
1735 correct_unit(char *unit, int *unptr)
1741 if (strncmp(unit, unit_tab[j], strlen(unit_tab[j])) == 0)
1748 return 0; /* invalid unit descriptor */
1752 * correct_dir - returns 1, iff direction is a correct identifier
1755 * signptr: points to -1 if dir corresponds to past tense
1759 correct_dir(char *direction, int *signptr)
1762 if (strncmp(RELTIME_PAST, direction, strlen(RELTIME_PAST) + 1) == 0)
1768 return 0; /* invalid direction descriptor */
1774 * istinterval - returns 1, iff i_string is a valid interval descr.
1775 * 0, iff i_string is NOT a valid interval desc.
1776 * 2, iff any time is INVALID_ABSTIME
1779 * i_start, i_end: interval margins
1782 * `[' {` '} `'' <AbsTime> `'' {` '} `'' <AbsTime> `'' {` '} `]'
1784 * OR `Undefined Range' (see also INVALID_INTERVAL_STR)
1786 * where <AbsTime> satisfies the syntax of absolute time.
1788 * e.g. [ ' Jan 18 1902' 'Jan 1 00:00:00 1970']
1791 istinterval(char *i_string,
1792 AbsoluteTime *i_start,
1793 AbsoluteTime *i_end)
1800 /* skip leading blanks up to '[' */
1801 while ((c = *p) != '\0')
1806 return 0; /* syntax error */
1811 /* skip leading blanks up to "'" */
1812 while ((c = *p) != '\0')
1817 return 0; /* syntax error */
1822 if (strncmp(INVALID_INTERVAL_STR, p, strlen(INVALID_INTERVAL_STR)) == 0)
1823 return 0; /* undefined range, handled like a syntax
1825 /* search for the end of the first date and change it to a NULL */
1827 while ((c = *p1) != '\0')
1836 /* get the first date */
1837 *i_start = DatumGetAbsoluteTime(DirectFunctionCall1(nabstimein,
1838 CStringGetDatum(p)));
1839 /* rechange NULL at the end of the first date to a "'" */
1842 /* skip blanks up to "'", beginning of second date */
1843 while ((c = *p) != '\0')
1848 return 0; /* syntax error */
1853 /* search for the end of the second date and change it to a NULL */
1855 while ((c = *p1) != '\0')
1864 /* get the second date */
1865 *i_end = DatumGetAbsoluteTime(DirectFunctionCall1(nabstimein,
1866 CStringGetDatum(p)));
1867 /* rechange NULL at the end of the first date to a ''' */
1870 /* skip blanks up to ']' */
1871 while ((c = *p) != '\0')
1876 return 0; /* syntax error */
1883 return 0; /* syntax error */
1884 /* it seems to be a valid interval */
1889 /*****************************************************************************
1891 *****************************************************************************/
1894 int4reltime(PG_FUNCTION_ARGS)
1896 int32 timevalue = PG_GETARG_INT32(0);
1898 /* Just coerce it directly to RelativeTime ... */
1899 PG_RETURN_RELATIVETIME((RelativeTime) timevalue);
1904 * returns the current time as a text. similar to timenow() but returns
1905 * seconds with more precision (up to microsecs). (I need this to compare
1906 * the Wisconsin benchmark with Illustra whose TimeNow() shows current
1907 * time with precision up to microsecs.) - ay 3/95
1910 timeofday(PG_FUNCTION_ARGS)
1913 struct timezone tpz;
1919 gettimeofday(&tp, &tpz);
1920 strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%06d %Y %Z",
1921 localtime((time_t *) &tp.tv_sec));
1922 snprintf(buf, sizeof(buf), templ, tp.tv_usec);
1924 len = VARHDRSZ + strlen(buf);
1925 result = (text *) palloc(len);
1926 VARATT_SIZEP(result) = len;
1927 memcpy(VARDATA(result), buf, strlen(buf));
1928 PG_RETURN_TEXT_P(result);