1 /*-------------------------------------------------------------------------
4 * implements DATE and TIME data types specified in SQL-92 standard
6 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994-5, Regents of the University of California
11 * src/backend/utils/adt/date.c
13 *-------------------------------------------------------------------------
23 #include "access/hash.h"
24 #include "libpq/pqformat.h"
25 #include "miscadmin.h"
26 #include "parser/scansup.h"
27 #include "utils/array.h"
28 #include "utils/builtins.h"
29 #include "utils/date.h"
30 #include "utils/datetime.h"
31 #include "utils/nabstime.h"
34 * gcc's -ffast-math switch breaks routines that expect exact results from
35 * expressions like timeval / SECS_PER_HOUR, where timeval is double.
38 #error -ffast-math is known to break this code
42 static void EncodeSpecialDate(DateADT dt, char *str);
43 static int time2tm(TimeADT time, struct pg_tm * tm, fsec_t *fsec);
44 static int timetz2tm(TimeTzADT *time, struct pg_tm * tm, fsec_t *fsec, int *tzp);
45 static int tm2time(struct pg_tm * tm, fsec_t fsec, TimeADT *result);
46 static int tm2timetz(struct pg_tm * tm, fsec_t fsec, int tz, TimeTzADT *result);
47 static void AdjustTimeForTypmod(TimeADT *time, int32 typmod);
50 /* common code for timetypmodin and timetztypmodin */
52 anytime_typmodin(bool istz, ArrayType *ta)
58 tl = ArrayGetIntegerTypmods(ta, &n);
61 * we're not too tense about good error message here because grammar
62 * shouldn't allow wrong number of modifiers for TIME
66 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
67 errmsg("invalid type modifier")));
71 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
72 errmsg("TIME(%d)%s precision must not be negative",
73 *tl, (istz ? " WITH TIME ZONE" : ""))));
74 if (*tl > MAX_TIME_PRECISION)
77 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
78 errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
79 *tl, (istz ? " WITH TIME ZONE" : ""),
80 MAX_TIME_PRECISION)));
81 typmod = MAX_TIME_PRECISION;
89 /* common code for timetypmodout and timetztypmodout */
91 anytime_typmodout(bool istz, int32 typmod)
93 char *res = (char *) palloc(64);
94 const char *tz = istz ? " with time zone" : " without time zone";
97 snprintf(res, 64, "(%d)%s", (int) typmod, tz);
99 snprintf(res, 64, "%s", tz);
104 /*****************************************************************************
106 *****************************************************************************/
110 * Given date text string, convert to internal date format.
113 date_in(PG_FUNCTION_ARGS)
115 char *str = PG_GETARG_CSTRING(0);
124 char *field[MAXDATEFIELDS];
125 int ftype[MAXDATEFIELDS];
126 char workbuf[MAXDATELEN + 1];
128 dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
129 field, ftype, MAXDATEFIELDS, &nf);
131 dterr = DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp);
133 DateTimeParseError(dterr, str, "date");
142 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
143 errmsg("date/time value \"current\" is no longer supported")));
145 GetCurrentDateTime(tm);
154 PG_RETURN_DATEADT(date);
158 PG_RETURN_DATEADT(date);
161 DateTimeParseError(DTERR_BAD_FORMAT, str, "date");
165 if (!IS_VALID_JULIAN(tm->tm_year, tm->tm_mon, tm->tm_mday))
167 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
168 errmsg("date out of range: \"%s\"", str)));
170 date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
172 PG_RETURN_DATEADT(date);
176 * Given internal format date, convert to text string.
179 date_out(PG_FUNCTION_ARGS)
181 DateADT date = PG_GETARG_DATEADT(0);
185 char buf[MAXDATELEN + 1];
187 if (DATE_NOT_FINITE(date))
188 EncodeSpecialDate(date, buf);
191 j2date(date + POSTGRES_EPOCH_JDATE,
192 &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
193 EncodeDateOnly(tm, DateStyle, buf);
196 result = pstrdup(buf);
197 PG_RETURN_CSTRING(result);
201 * date_recv - converts external binary format to date
204 date_recv(PG_FUNCTION_ARGS)
206 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
209 result = (DateADT) pq_getmsgint(buf, sizeof(DateADT));
211 /* Limit to the same range that date_in() accepts. */
212 if (DATE_NOT_FINITE(result))
214 else if (result < -POSTGRES_EPOCH_JDATE ||
215 result >= JULIAN_MAX - POSTGRES_EPOCH_JDATE)
217 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
218 errmsg("date out of range")));
220 PG_RETURN_DATEADT(result);
224 * date_send - converts date to binary format
227 date_send(PG_FUNCTION_ARGS)
229 DateADT date = PG_GETARG_DATEADT(0);
232 pq_begintypsend(&buf);
233 pq_sendint(&buf, date, sizeof(date));
234 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
238 * Convert reserved date values to string.
241 EncodeSpecialDate(DateADT dt, char *str)
243 if (DATE_IS_NOBEGIN(dt))
245 else if (DATE_IS_NOEND(dt))
247 else /* shouldn't happen */
248 elog(ERROR, "invalid argument for EncodeSpecialDate");
253 * Comparison functions for dates
257 date_eq(PG_FUNCTION_ARGS)
259 DateADT dateVal1 = PG_GETARG_DATEADT(0);
260 DateADT dateVal2 = PG_GETARG_DATEADT(1);
262 PG_RETURN_BOOL(dateVal1 == dateVal2);
266 date_ne(PG_FUNCTION_ARGS)
268 DateADT dateVal1 = PG_GETARG_DATEADT(0);
269 DateADT dateVal2 = PG_GETARG_DATEADT(1);
271 PG_RETURN_BOOL(dateVal1 != dateVal2);
275 date_lt(PG_FUNCTION_ARGS)
277 DateADT dateVal1 = PG_GETARG_DATEADT(0);
278 DateADT dateVal2 = PG_GETARG_DATEADT(1);
280 PG_RETURN_BOOL(dateVal1 < dateVal2);
284 date_le(PG_FUNCTION_ARGS)
286 DateADT dateVal1 = PG_GETARG_DATEADT(0);
287 DateADT dateVal2 = PG_GETARG_DATEADT(1);
289 PG_RETURN_BOOL(dateVal1 <= dateVal2);
293 date_gt(PG_FUNCTION_ARGS)
295 DateADT dateVal1 = PG_GETARG_DATEADT(0);
296 DateADT dateVal2 = PG_GETARG_DATEADT(1);
298 PG_RETURN_BOOL(dateVal1 > dateVal2);
302 date_ge(PG_FUNCTION_ARGS)
304 DateADT dateVal1 = PG_GETARG_DATEADT(0);
305 DateADT dateVal2 = PG_GETARG_DATEADT(1);
307 PG_RETURN_BOOL(dateVal1 >= dateVal2);
311 date_cmp(PG_FUNCTION_ARGS)
313 DateADT dateVal1 = PG_GETARG_DATEADT(0);
314 DateADT dateVal2 = PG_GETARG_DATEADT(1);
316 if (dateVal1 < dateVal2)
318 else if (dateVal1 > dateVal2)
324 date_finite(PG_FUNCTION_ARGS)
326 DateADT date = PG_GETARG_DATEADT(0);
328 PG_RETURN_BOOL(!DATE_NOT_FINITE(date));
332 date_larger(PG_FUNCTION_ARGS)
334 DateADT dateVal1 = PG_GETARG_DATEADT(0);
335 DateADT dateVal2 = PG_GETARG_DATEADT(1);
337 PG_RETURN_DATEADT((dateVal1 > dateVal2) ? dateVal1 : dateVal2);
341 date_smaller(PG_FUNCTION_ARGS)
343 DateADT dateVal1 = PG_GETARG_DATEADT(0);
344 DateADT dateVal2 = PG_GETARG_DATEADT(1);
346 PG_RETURN_DATEADT((dateVal1 < dateVal2) ? dateVal1 : dateVal2);
349 /* Compute difference between two dates in days.
352 date_mi(PG_FUNCTION_ARGS)
354 DateADT dateVal1 = PG_GETARG_DATEADT(0);
355 DateADT dateVal2 = PG_GETARG_DATEADT(1);
357 if (DATE_NOT_FINITE(dateVal1) || DATE_NOT_FINITE(dateVal2))
359 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
360 errmsg("cannot subtract infinite dates")));
362 PG_RETURN_INT32((int32) (dateVal1 - dateVal2));
365 /* Add a number of days to a date, giving a new date.
366 * Must handle both positive and negative numbers of days.
369 date_pli(PG_FUNCTION_ARGS)
371 DateADT dateVal = PG_GETARG_DATEADT(0);
372 int32 days = PG_GETARG_INT32(1);
374 if (DATE_NOT_FINITE(dateVal))
375 days = 0; /* can't change infinity */
377 PG_RETURN_DATEADT(dateVal + days);
380 /* Subtract a number of days from a date, giving a new date.
383 date_mii(PG_FUNCTION_ARGS)
385 DateADT dateVal = PG_GETARG_DATEADT(0);
386 int32 days = PG_GETARG_INT32(1);
388 if (DATE_NOT_FINITE(dateVal))
389 days = 0; /* can't change infinity */
391 PG_RETURN_DATEADT(dateVal - days);
395 * Internal routines for promoting date to timestamp and timestamp with
400 date2timestamp(DateADT dateVal)
404 if (DATE_IS_NOBEGIN(dateVal))
405 TIMESTAMP_NOBEGIN(result);
406 else if (DATE_IS_NOEND(dateVal))
407 TIMESTAMP_NOEND(result);
410 #ifdef HAVE_INT64_TIMESTAMP
411 /* date is days since 2000, timestamp is microseconds since same... */
412 result = dateVal * USECS_PER_DAY;
413 /* Date's range is wider than timestamp's, so check for overflow */
414 if (result / USECS_PER_DAY != dateVal)
416 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
417 errmsg("date out of range for timestamp")));
419 /* date is days since 2000, timestamp is seconds since same... */
420 result = dateVal * (double) SECS_PER_DAY;
428 date2timestamptz(DateADT dateVal)
435 if (DATE_IS_NOBEGIN(dateVal))
436 TIMESTAMP_NOBEGIN(result);
437 else if (DATE_IS_NOEND(dateVal))
438 TIMESTAMP_NOEND(result);
441 j2date(dateVal + POSTGRES_EPOCH_JDATE,
442 &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
446 tz = DetermineTimeZoneOffset(tm, session_timezone);
448 #ifdef HAVE_INT64_TIMESTAMP
449 result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
450 /* Date's range is wider than timestamp's, so check for overflow */
451 if ((result - tz * USECS_PER_SEC) / USECS_PER_DAY != dateVal)
453 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
454 errmsg("date out of range for timestamp")));
456 result = dateVal * (double) SECS_PER_DAY + tz;
464 * date2timestamp_no_overflow
466 * This is chartered to produce a double value that is numerically
467 * equivalent to the corresponding Timestamp value, if the date is in the
468 * valid range of Timestamps, but in any case not throw an overflow error.
469 * We can do this since the numerical range of double is greater than
470 * that of non-erroneous timestamps. The results are currently only
471 * used for statistical estimation purposes.
474 date2timestamp_no_overflow(DateADT dateVal)
478 if (DATE_IS_NOBEGIN(dateVal))
480 else if (DATE_IS_NOEND(dateVal))
484 #ifdef HAVE_INT64_TIMESTAMP
485 /* date is days since 2000, timestamp is microseconds since same... */
486 result = dateVal * (double) USECS_PER_DAY;
488 /* date is days since 2000, timestamp is seconds since same... */
489 result = dateVal * (double) SECS_PER_DAY;
498 * Crosstype comparison functions for dates
502 date_eq_timestamp(PG_FUNCTION_ARGS)
504 DateADT dateVal = PG_GETARG_DATEADT(0);
505 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
508 dt1 = date2timestamp(dateVal);
510 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
514 date_ne_timestamp(PG_FUNCTION_ARGS)
516 DateADT dateVal = PG_GETARG_DATEADT(0);
517 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
520 dt1 = date2timestamp(dateVal);
522 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
526 date_lt_timestamp(PG_FUNCTION_ARGS)
528 DateADT dateVal = PG_GETARG_DATEADT(0);
529 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
532 dt1 = date2timestamp(dateVal);
534 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
538 date_gt_timestamp(PG_FUNCTION_ARGS)
540 DateADT dateVal = PG_GETARG_DATEADT(0);
541 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
544 dt1 = date2timestamp(dateVal);
546 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
550 date_le_timestamp(PG_FUNCTION_ARGS)
552 DateADT dateVal = PG_GETARG_DATEADT(0);
553 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
556 dt1 = date2timestamp(dateVal);
558 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
562 date_ge_timestamp(PG_FUNCTION_ARGS)
564 DateADT dateVal = PG_GETARG_DATEADT(0);
565 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
568 dt1 = date2timestamp(dateVal);
570 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
574 date_cmp_timestamp(PG_FUNCTION_ARGS)
576 DateADT dateVal = PG_GETARG_DATEADT(0);
577 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
580 dt1 = date2timestamp(dateVal);
582 PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2));
586 date_eq_timestamptz(PG_FUNCTION_ARGS)
588 DateADT dateVal = PG_GETARG_DATEADT(0);
589 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
592 dt1 = date2timestamptz(dateVal);
594 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) == 0);
598 date_ne_timestamptz(PG_FUNCTION_ARGS)
600 DateADT dateVal = PG_GETARG_DATEADT(0);
601 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
604 dt1 = date2timestamptz(dateVal);
606 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) != 0);
610 date_lt_timestamptz(PG_FUNCTION_ARGS)
612 DateADT dateVal = PG_GETARG_DATEADT(0);
613 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
616 dt1 = date2timestamptz(dateVal);
618 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) < 0);
622 date_gt_timestamptz(PG_FUNCTION_ARGS)
624 DateADT dateVal = PG_GETARG_DATEADT(0);
625 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
628 dt1 = date2timestamptz(dateVal);
630 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) > 0);
634 date_le_timestamptz(PG_FUNCTION_ARGS)
636 DateADT dateVal = PG_GETARG_DATEADT(0);
637 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
640 dt1 = date2timestamptz(dateVal);
642 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) <= 0);
646 date_ge_timestamptz(PG_FUNCTION_ARGS)
648 DateADT dateVal = PG_GETARG_DATEADT(0);
649 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
652 dt1 = date2timestamptz(dateVal);
654 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) >= 0);
658 date_cmp_timestamptz(PG_FUNCTION_ARGS)
660 DateADT dateVal = PG_GETARG_DATEADT(0);
661 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
664 dt1 = date2timestamptz(dateVal);
666 PG_RETURN_INT32(timestamptz_cmp_internal(dt1, dt2));
670 timestamp_eq_date(PG_FUNCTION_ARGS)
672 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
673 DateADT dateVal = PG_GETARG_DATEADT(1);
676 dt2 = date2timestamp(dateVal);
678 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
682 timestamp_ne_date(PG_FUNCTION_ARGS)
684 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
685 DateADT dateVal = PG_GETARG_DATEADT(1);
688 dt2 = date2timestamp(dateVal);
690 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
694 timestamp_lt_date(PG_FUNCTION_ARGS)
696 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
697 DateADT dateVal = PG_GETARG_DATEADT(1);
700 dt2 = date2timestamp(dateVal);
702 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
706 timestamp_gt_date(PG_FUNCTION_ARGS)
708 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
709 DateADT dateVal = PG_GETARG_DATEADT(1);
712 dt2 = date2timestamp(dateVal);
714 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
718 timestamp_le_date(PG_FUNCTION_ARGS)
720 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
721 DateADT dateVal = PG_GETARG_DATEADT(1);
724 dt2 = date2timestamp(dateVal);
726 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
730 timestamp_ge_date(PG_FUNCTION_ARGS)
732 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
733 DateADT dateVal = PG_GETARG_DATEADT(1);
736 dt2 = date2timestamp(dateVal);
738 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
742 timestamp_cmp_date(PG_FUNCTION_ARGS)
744 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
745 DateADT dateVal = PG_GETARG_DATEADT(1);
748 dt2 = date2timestamp(dateVal);
750 PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2));
754 timestamptz_eq_date(PG_FUNCTION_ARGS)
756 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
757 DateADT dateVal = PG_GETARG_DATEADT(1);
760 dt2 = date2timestamptz(dateVal);
762 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) == 0);
766 timestamptz_ne_date(PG_FUNCTION_ARGS)
768 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
769 DateADT dateVal = PG_GETARG_DATEADT(1);
772 dt2 = date2timestamptz(dateVal);
774 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) != 0);
778 timestamptz_lt_date(PG_FUNCTION_ARGS)
780 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
781 DateADT dateVal = PG_GETARG_DATEADT(1);
784 dt2 = date2timestamptz(dateVal);
786 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) < 0);
790 timestamptz_gt_date(PG_FUNCTION_ARGS)
792 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
793 DateADT dateVal = PG_GETARG_DATEADT(1);
796 dt2 = date2timestamptz(dateVal);
798 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) > 0);
802 timestamptz_le_date(PG_FUNCTION_ARGS)
804 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
805 DateADT dateVal = PG_GETARG_DATEADT(1);
808 dt2 = date2timestamptz(dateVal);
810 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) <= 0);
814 timestamptz_ge_date(PG_FUNCTION_ARGS)
816 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
817 DateADT dateVal = PG_GETARG_DATEADT(1);
820 dt2 = date2timestamptz(dateVal);
822 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) >= 0);
826 timestamptz_cmp_date(PG_FUNCTION_ARGS)
828 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
829 DateADT dateVal = PG_GETARG_DATEADT(1);
832 dt2 = date2timestamptz(dateVal);
834 PG_RETURN_INT32(timestamptz_cmp_internal(dt1, dt2));
838 /* Add an interval to a date, giving a new date.
839 * Must handle both positive and negative intervals.
841 * We implement this by promoting the date to timestamp (without time zone)
842 * and then using the timestamp plus interval function.
845 date_pl_interval(PG_FUNCTION_ARGS)
847 DateADT dateVal = PG_GETARG_DATEADT(0);
848 Interval *span = PG_GETARG_INTERVAL_P(1);
851 dateStamp = date2timestamp(dateVal);
853 return DirectFunctionCall2(timestamp_pl_interval,
854 TimestampGetDatum(dateStamp),
855 PointerGetDatum(span));
858 /* Subtract an interval from a date, giving a new date.
859 * Must handle both positive and negative intervals.
861 * We implement this by promoting the date to timestamp (without time zone)
862 * and then using the timestamp minus interval function.
865 date_mi_interval(PG_FUNCTION_ARGS)
867 DateADT dateVal = PG_GETARG_DATEADT(0);
868 Interval *span = PG_GETARG_INTERVAL_P(1);
871 dateStamp = date2timestamp(dateVal);
873 return DirectFunctionCall2(timestamp_mi_interval,
874 TimestampGetDatum(dateStamp),
875 PointerGetDatum(span));
879 * Convert date to timestamp data type.
882 date_timestamp(PG_FUNCTION_ARGS)
884 DateADT dateVal = PG_GETARG_DATEADT(0);
887 result = date2timestamp(dateVal);
889 PG_RETURN_TIMESTAMP(result);
893 * Convert timestamp to date data type.
896 timestamp_date(PG_FUNCTION_ARGS)
898 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
904 if (TIMESTAMP_IS_NOBEGIN(timestamp))
905 DATE_NOBEGIN(result);
906 else if (TIMESTAMP_IS_NOEND(timestamp))
910 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
912 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
913 errmsg("timestamp out of range")));
915 result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
918 PG_RETURN_DATEADT(result);
922 /* date_timestamptz()
923 * Convert date to timestamp with time zone data type.
926 date_timestamptz(PG_FUNCTION_ARGS)
928 DateADT dateVal = PG_GETARG_DATEADT(0);
931 result = date2timestamptz(dateVal);
933 PG_RETURN_TIMESTAMP(result);
937 /* timestamptz_date()
938 * Convert timestamp with time zone to date data type.
941 timestamptz_date(PG_FUNCTION_ARGS)
943 TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
951 if (TIMESTAMP_IS_NOBEGIN(timestamp))
952 DATE_NOBEGIN(result);
953 else if (TIMESTAMP_IS_NOEND(timestamp))
957 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
959 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
960 errmsg("timestamp out of range")));
962 result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
965 PG_RETURN_DATEADT(result);
970 * Convert abstime to date data type.
973 abstime_date(PG_FUNCTION_ARGS)
975 AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
983 case INVALID_ABSTIME:
985 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
986 errmsg("cannot convert reserved abstime value to date")));
987 result = 0; /* keep compiler quiet */
990 case NOSTART_ABSTIME:
991 DATE_NOBEGIN(result);
999 abstime2tm(abstime, &tz, tm, NULL);
1000 result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
1004 PG_RETURN_DATEADT(result);
1008 /*****************************************************************************
1010 *****************************************************************************/
1013 time_in(PG_FUNCTION_ARGS)
1015 char *str = PG_GETARG_CSTRING(0);
1018 Oid typelem = PG_GETARG_OID(1);
1020 int32 typmod = PG_GETARG_INT32(2);
1028 char workbuf[MAXDATELEN + 1];
1029 char *field[MAXDATEFIELDS];
1031 int ftype[MAXDATEFIELDS];
1033 dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
1034 field, ftype, MAXDATEFIELDS, &nf);
1036 dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
1038 DateTimeParseError(dterr, str, "time");
1040 tm2time(tm, fsec, &result);
1041 AdjustTimeForTypmod(&result, typmod);
1043 PG_RETURN_TIMEADT(result);
1047 * Convert a tm structure to a time data type.
1050 tm2time(struct pg_tm * tm, fsec_t fsec, TimeADT *result)
1052 #ifdef HAVE_INT64_TIMESTAMP
1053 *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1054 * USECS_PER_SEC) + fsec;
1056 *result = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec + fsec;
1062 * Convert time data type to POSIX time structure.
1064 * For dates within the range of pg_time_t, convert to the local time zone.
1065 * If out of this range, leave as UTC (in practice that could only happen
1066 * if pg_time_t is just 32 bits) - thomas 97/05/27
1069 time2tm(TimeADT time, struct pg_tm * tm, fsec_t *fsec)
1071 #ifdef HAVE_INT64_TIMESTAMP
1072 tm->tm_hour = time / USECS_PER_HOUR;
1073 time -= tm->tm_hour * USECS_PER_HOUR;
1074 tm->tm_min = time / USECS_PER_MINUTE;
1075 time -= tm->tm_min * USECS_PER_MINUTE;
1076 tm->tm_sec = time / USECS_PER_SEC;
1077 time -= tm->tm_sec * USECS_PER_SEC;
1084 TMODULO(trem, tm->tm_hour, (double) SECS_PER_HOUR);
1085 TMODULO(trem, tm->tm_min, (double) SECS_PER_MINUTE);
1086 TMODULO(trem, tm->tm_sec, 1.0);
1087 trem = TIMEROUND(trem);
1088 /* roundoff may need to propagate to higher-order fields */
1101 time_out(PG_FUNCTION_ARGS)
1103 TimeADT time = PG_GETARG_TIMEADT(0);
1108 char buf[MAXDATELEN + 1];
1110 time2tm(time, tm, &fsec);
1111 EncodeTimeOnly(tm, fsec, NULL, DateStyle, buf);
1113 result = pstrdup(buf);
1114 PG_RETURN_CSTRING(result);
1118 * time_recv - converts external binary format to time
1120 * We make no attempt to provide compatibility between int and float
1121 * time representations ...
1124 time_recv(PG_FUNCTION_ARGS)
1126 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
1129 Oid typelem = PG_GETARG_OID(1);
1131 int32 typmod = PG_GETARG_INT32(2);
1134 #ifdef HAVE_INT64_TIMESTAMP
1135 result = pq_getmsgint64(buf);
1137 if (result < INT64CONST(0) || result > USECS_PER_DAY)
1139 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1140 errmsg("time out of range")));
1142 result = pq_getmsgfloat8(buf);
1144 if (result < 0 || result > (double) SECS_PER_DAY)
1146 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1147 errmsg("time out of range")));
1150 AdjustTimeForTypmod(&result, typmod);
1152 PG_RETURN_TIMEADT(result);
1156 * time_send - converts time to binary format
1159 time_send(PG_FUNCTION_ARGS)
1161 TimeADT time = PG_GETARG_TIMEADT(0);
1164 pq_begintypsend(&buf);
1165 #ifdef HAVE_INT64_TIMESTAMP
1166 pq_sendint64(&buf, time);
1168 pq_sendfloat8(&buf, time);
1170 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
1174 timetypmodin(PG_FUNCTION_ARGS)
1176 ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
1178 PG_RETURN_INT32(anytime_typmodin(false, ta));
1182 timetypmodout(PG_FUNCTION_ARGS)
1184 int32 typmod = PG_GETARG_INT32(0);
1186 PG_RETURN_CSTRING(anytime_typmodout(false, typmod));
1191 * Adjust time type for specified scale factor.
1192 * Used by PostgreSQL type system to stuff columns.
1195 time_scale(PG_FUNCTION_ARGS)
1197 TimeADT time = PG_GETARG_TIMEADT(0);
1198 int32 typmod = PG_GETARG_INT32(1);
1202 AdjustTimeForTypmod(&result, typmod);
1204 PG_RETURN_TIMEADT(result);
1207 /* AdjustTimeForTypmod()
1208 * Force the precision of the time value to a specified value.
1209 * Uses *exactly* the same code as in AdjustTimestampForTypemod()
1210 * but we make a separate copy because those types do not
1211 * have a fundamental tie together but rather a coincidence of
1212 * implementation. - thomas
1215 AdjustTimeForTypmod(TimeADT *time, int32 typmod)
1217 #ifdef HAVE_INT64_TIMESTAMP
1218 static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1219 INT64CONST(1000000),
1228 static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1238 /* note MAX_TIME_PRECISION differs in this case */
1239 static const double TimeScales[MAX_TIME_PRECISION + 1] = {
1254 if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1257 * Note: this round-to-nearest code is not completely consistent about
1258 * rounding values that are exactly halfway between integral values.
1259 * On most platforms, rint() will implement round-to-nearest-even, but
1260 * the integer code always rounds up (away from zero). Is it worth
1261 * trying to be consistent?
1263 #ifdef HAVE_INT64_TIMESTAMP
1264 if (*time >= INT64CONST(0))
1265 *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1268 *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1269 TimeScales[typmod]);
1271 *time = rint((double) *time * TimeScales[typmod]) / TimeScales[typmod];
1278 time_eq(PG_FUNCTION_ARGS)
1280 TimeADT time1 = PG_GETARG_TIMEADT(0);
1281 TimeADT time2 = PG_GETARG_TIMEADT(1);
1283 PG_RETURN_BOOL(time1 == time2);
1287 time_ne(PG_FUNCTION_ARGS)
1289 TimeADT time1 = PG_GETARG_TIMEADT(0);
1290 TimeADT time2 = PG_GETARG_TIMEADT(1);
1292 PG_RETURN_BOOL(time1 != time2);
1296 time_lt(PG_FUNCTION_ARGS)
1298 TimeADT time1 = PG_GETARG_TIMEADT(0);
1299 TimeADT time2 = PG_GETARG_TIMEADT(1);
1301 PG_RETURN_BOOL(time1 < time2);
1305 time_le(PG_FUNCTION_ARGS)
1307 TimeADT time1 = PG_GETARG_TIMEADT(0);
1308 TimeADT time2 = PG_GETARG_TIMEADT(1);
1310 PG_RETURN_BOOL(time1 <= time2);
1314 time_gt(PG_FUNCTION_ARGS)
1316 TimeADT time1 = PG_GETARG_TIMEADT(0);
1317 TimeADT time2 = PG_GETARG_TIMEADT(1);
1319 PG_RETURN_BOOL(time1 > time2);
1323 time_ge(PG_FUNCTION_ARGS)
1325 TimeADT time1 = PG_GETARG_TIMEADT(0);
1326 TimeADT time2 = PG_GETARG_TIMEADT(1);
1328 PG_RETURN_BOOL(time1 >= time2);
1332 time_cmp(PG_FUNCTION_ARGS)
1334 TimeADT time1 = PG_GETARG_TIMEADT(0);
1335 TimeADT time2 = PG_GETARG_TIMEADT(1);
1338 PG_RETURN_INT32(-1);
1345 time_hash(PG_FUNCTION_ARGS)
1347 /* We can use either hashint8 or hashfloat8 directly */
1348 #ifdef HAVE_INT64_TIMESTAMP
1349 return hashint8(fcinfo);
1351 return hashfloat8(fcinfo);
1356 time_larger(PG_FUNCTION_ARGS)
1358 TimeADT time1 = PG_GETARG_TIMEADT(0);
1359 TimeADT time2 = PG_GETARG_TIMEADT(1);
1361 PG_RETURN_TIMEADT((time1 > time2) ? time1 : time2);
1365 time_smaller(PG_FUNCTION_ARGS)
1367 TimeADT time1 = PG_GETARG_TIMEADT(0);
1368 TimeADT time2 = PG_GETARG_TIMEADT(1);
1370 PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
1373 /* overlaps_time() --- implements the SQL92 OVERLAPS operator.
1375 * Algorithm is per SQL92 spec. This is much harder than you'd think
1376 * because the spec requires us to deliver a non-null answer in some cases
1377 * where some of the inputs are null.
1380 overlaps_time(PG_FUNCTION_ARGS)
1383 * The arguments are TimeADT, but we leave them as generic Datums to avoid
1384 * dereferencing nulls (TimeADT is pass-by-reference!)
1386 Datum ts1 = PG_GETARG_DATUM(0);
1387 Datum te1 = PG_GETARG_DATUM(1);
1388 Datum ts2 = PG_GETARG_DATUM(2);
1389 Datum te2 = PG_GETARG_DATUM(3);
1390 bool ts1IsNull = PG_ARGISNULL(0);
1391 bool te1IsNull = PG_ARGISNULL(1);
1392 bool ts2IsNull = PG_ARGISNULL(2);
1393 bool te2IsNull = PG_ARGISNULL(3);
1395 #define TIMEADT_GT(t1,t2) \
1396 (DatumGetTimeADT(t1) > DatumGetTimeADT(t2))
1397 #define TIMEADT_LT(t1,t2) \
1398 (DatumGetTimeADT(t1) < DatumGetTimeADT(t2))
1401 * If both endpoints of interval 1 are null, the result is null (unknown).
1402 * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
1403 * take ts1 as the lesser endpoint.
1409 /* swap null for non-null */
1413 else if (!te1IsNull)
1415 if (TIMEADT_GT(ts1, te1))
1424 /* Likewise for interval 2. */
1429 /* swap null for non-null */
1433 else if (!te2IsNull)
1435 if (TIMEADT_GT(ts2, te2))
1445 * At this point neither ts1 nor ts2 is null, so we can consider three
1446 * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
1448 if (TIMEADT_GT(ts1, ts2))
1451 * This case is ts1 < te2 OR te1 < te2, which may look redundant but
1452 * in the presence of nulls it's not quite completely so.
1456 if (TIMEADT_LT(ts1, te2))
1457 PG_RETURN_BOOL(true);
1462 * If te1 is not null then we had ts1 <= te1 above, and we just found
1463 * ts1 >= te2, hence te1 >= te2.
1465 PG_RETURN_BOOL(false);
1467 else if (TIMEADT_LT(ts1, ts2))
1469 /* This case is ts2 < te1 OR te2 < te1 */
1472 if (TIMEADT_LT(ts2, te1))
1473 PG_RETURN_BOOL(true);
1478 * If te2 is not null then we had ts2 <= te2 above, and we just found
1479 * ts2 >= te1, hence te2 >= te1.
1481 PG_RETURN_BOOL(false);
1486 * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
1487 * rather silly way of saying "true if both are nonnull, else null".
1489 if (te1IsNull || te2IsNull)
1491 PG_RETURN_BOOL(true);
1499 * Convert timestamp to time data type.
1502 timestamp_time(PG_FUNCTION_ARGS)
1504 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
1510 if (TIMESTAMP_NOT_FINITE(timestamp))
1513 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1515 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1516 errmsg("timestamp out of range")));
1518 #ifdef HAVE_INT64_TIMESTAMP
1521 * Could also do this with time = (timestamp / USECS_PER_DAY *
1522 * USECS_PER_DAY) - timestamp;
1524 result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1525 USECS_PER_SEC) + fsec;
1527 result = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec + fsec;
1530 PG_RETURN_TIMEADT(result);
1533 /* timestamptz_time()
1534 * Convert timestamptz to time data type.
1537 timestamptz_time(PG_FUNCTION_ARGS)
1539 TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
1547 if (TIMESTAMP_NOT_FINITE(timestamp))
1550 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
1552 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1553 errmsg("timestamp out of range")));
1555 #ifdef HAVE_INT64_TIMESTAMP
1558 * Could also do this with time = (timestamp / USECS_PER_DAY *
1559 * USECS_PER_DAY) - timestamp;
1561 result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1562 USECS_PER_SEC) + fsec;
1564 result = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec + fsec;
1567 PG_RETURN_TIMEADT(result);
1570 /* datetime_timestamp()
1571 * Convert date and time to timestamp data type.
1574 datetime_timestamp(PG_FUNCTION_ARGS)
1576 DateADT date = PG_GETARG_DATEADT(0);
1577 TimeADT time = PG_GETARG_TIMEADT(1);
1580 result = date2timestamp(date);
1581 if (!TIMESTAMP_NOT_FINITE(result))
1584 PG_RETURN_TIMESTAMP(result);
1588 * Convert time to interval data type.
1591 time_interval(PG_FUNCTION_ARGS)
1593 TimeADT time = PG_GETARG_TIMEADT(0);
1596 result = (Interval *) palloc(sizeof(Interval));
1598 result->time = time;
1602 PG_RETURN_INTERVAL_P(result);
1606 * Convert interval to time data type.
1608 * This is defined as producing the fractional-day portion of the interval.
1609 * Therefore, we can just ignore the months field. It is not real clear
1610 * what to do with negative intervals, but we choose to subtract the floor,
1611 * so that, say, '-2 hours' becomes '22:00:00'.
1614 interval_time(PG_FUNCTION_ARGS)
1616 Interval *span = PG_GETARG_INTERVAL_P(0);
1619 #ifdef HAVE_INT64_TIMESTAMP
1622 result = span->time;
1623 if (result >= USECS_PER_DAY)
1625 days = result / USECS_PER_DAY;
1626 result -= days * USECS_PER_DAY;
1628 else if (result < 0)
1630 days = (-result + USECS_PER_DAY - 1) / USECS_PER_DAY;
1631 result += days * USECS_PER_DAY;
1634 result = span->time;
1635 if (result >= (double) SECS_PER_DAY || result < 0)
1636 result -= floor(result / (double) SECS_PER_DAY) * (double) SECS_PER_DAY;
1639 PG_RETURN_TIMEADT(result);
1643 * Subtract two times to produce an interval.
1646 time_mi_time(PG_FUNCTION_ARGS)
1648 TimeADT time1 = PG_GETARG_TIMEADT(0);
1649 TimeADT time2 = PG_GETARG_TIMEADT(1);
1652 result = (Interval *) palloc(sizeof(Interval));
1656 result->time = time1 - time2;
1658 PG_RETURN_INTERVAL_P(result);
1661 /* time_pl_interval()
1662 * Add interval to time.
1665 time_pl_interval(PG_FUNCTION_ARGS)
1667 TimeADT time = PG_GETARG_TIMEADT(0);
1668 Interval *span = PG_GETARG_INTERVAL_P(1);
1671 #ifdef HAVE_INT64_TIMESTAMP
1672 result = time + span->time;
1673 result -= result / USECS_PER_DAY * USECS_PER_DAY;
1674 if (result < INT64CONST(0))
1675 result += USECS_PER_DAY;
1679 result = time + span->time;
1680 TMODULO(result, time1, (double) SECS_PER_DAY);
1682 result += SECS_PER_DAY;
1685 PG_RETURN_TIMEADT(result);
1688 /* time_mi_interval()
1689 * Subtract interval from time.
1692 time_mi_interval(PG_FUNCTION_ARGS)
1694 TimeADT time = PG_GETARG_TIMEADT(0);
1695 Interval *span = PG_GETARG_INTERVAL_P(1);
1698 #ifdef HAVE_INT64_TIMESTAMP
1699 result = time - span->time;
1700 result -= result / USECS_PER_DAY * USECS_PER_DAY;
1701 if (result < INT64CONST(0))
1702 result += USECS_PER_DAY;
1706 result = time - span->time;
1707 TMODULO(result, time1, (double) SECS_PER_DAY);
1709 result += SECS_PER_DAY;
1712 PG_RETURN_TIMEADT(result);
1717 * Extract specified field from time type.
1720 time_part(PG_FUNCTION_ARGS)
1722 text *units = PG_GETARG_TEXT_PP(0);
1723 TimeADT time = PG_GETARG_TIMEADT(1);
1729 lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
1730 VARSIZE_ANY_EXHDR(units),
1733 type = DecodeUnits(0, lowunits, &val);
1734 if (type == UNKNOWN_FIELD)
1735 type = DecodeSpecial(0, lowunits, &val);
1743 time2tm(time, tm, &fsec);
1748 #ifdef HAVE_INT64_TIMESTAMP
1749 result = tm->tm_sec * 1000000.0 + fsec;
1751 result = (tm->tm_sec + fsec) * 1000000;
1756 #ifdef HAVE_INT64_TIMESTAMP
1757 result = tm->tm_sec * 1000.0 + fsec / 1000.0;
1759 result = (tm->tm_sec + fsec) * 1000;
1764 #ifdef HAVE_INT64_TIMESTAMP
1765 result = tm->tm_sec + fsec / 1000000.0;
1767 result = tm->tm_sec + fsec;
1772 result = tm->tm_min;
1776 result = tm->tm_hour;
1788 case DTK_MILLENNIUM:
1792 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1793 errmsg("\"time\" units \"%s\" not recognized",
1798 else if (type == RESERV && val == DTK_EPOCH)
1800 #ifdef HAVE_INT64_TIMESTAMP
1801 result = time / 1000000.0;
1809 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1810 errmsg("\"time\" units \"%s\" not recognized",
1815 PG_RETURN_FLOAT8(result);
1819 /*****************************************************************************
1820 * Time With Time Zone ADT
1821 *****************************************************************************/
1824 * Convert a tm structure to a time data type.
1827 tm2timetz(struct pg_tm * tm, fsec_t fsec, int tz, TimeTzADT *result)
1829 #ifdef HAVE_INT64_TIMESTAMP
1830 result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1831 USECS_PER_SEC) + fsec;
1833 result->time = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec + fsec;
1841 timetz_in(PG_FUNCTION_ARGS)
1843 char *str = PG_GETARG_CSTRING(0);
1846 Oid typelem = PG_GETARG_OID(1);
1848 int32 typmod = PG_GETARG_INT32(2);
1856 char workbuf[MAXDATELEN + 1];
1857 char *field[MAXDATEFIELDS];
1859 int ftype[MAXDATEFIELDS];
1861 dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
1862 field, ftype, MAXDATEFIELDS, &nf);
1864 dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
1866 DateTimeParseError(dterr, str, "time with time zone");
1868 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
1869 tm2timetz(tm, fsec, tz, result);
1870 AdjustTimeForTypmod(&(result->time), typmod);
1872 PG_RETURN_TIMETZADT_P(result);
1876 timetz_out(PG_FUNCTION_ARGS)
1878 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
1884 char buf[MAXDATELEN + 1];
1886 timetz2tm(time, tm, &fsec, &tz);
1887 EncodeTimeOnly(tm, fsec, &tz, DateStyle, buf);
1889 result = pstrdup(buf);
1890 PG_RETURN_CSTRING(result);
1894 * timetz_recv - converts external binary format to timetz
1897 timetz_recv(PG_FUNCTION_ARGS)
1899 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
1902 Oid typelem = PG_GETARG_OID(1);
1904 int32 typmod = PG_GETARG_INT32(2);
1907 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
1909 #ifdef HAVE_INT64_TIMESTAMP
1910 result->time = pq_getmsgint64(buf);
1912 if (result->time < INT64CONST(0) || result->time > USECS_PER_DAY)
1914 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1915 errmsg("time out of range")));
1917 result->time = pq_getmsgfloat8(buf);
1919 if (result->time < 0 || result->time > (double) SECS_PER_DAY)
1921 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1922 errmsg("time out of range")));
1925 result->zone = pq_getmsgint(buf, sizeof(result->zone));
1927 /* we allow GMT displacements up to 14:59:59, cf DecodeTimezone() */
1928 if (result->zone <= -15 * SECS_PER_HOUR ||
1929 result->zone >= 15 * SECS_PER_HOUR)
1931 (errcode(ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE),
1932 errmsg("time zone displacement out of range")));
1934 AdjustTimeForTypmod(&(result->time), typmod);
1936 PG_RETURN_TIMETZADT_P(result);
1940 * timetz_send - converts timetz to binary format
1943 timetz_send(PG_FUNCTION_ARGS)
1945 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
1948 pq_begintypsend(&buf);
1949 #ifdef HAVE_INT64_TIMESTAMP
1950 pq_sendint64(&buf, time->time);
1952 pq_sendfloat8(&buf, time->time);
1954 pq_sendint(&buf, time->zone, sizeof(time->zone));
1955 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
1959 timetztypmodin(PG_FUNCTION_ARGS)
1961 ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
1963 PG_RETURN_INT32(anytime_typmodin(true, ta));
1967 timetztypmodout(PG_FUNCTION_ARGS)
1969 int32 typmod = PG_GETARG_INT32(0);
1971 PG_RETURN_CSTRING(anytime_typmodout(true, typmod));
1976 * Convert TIME WITH TIME ZONE data type to POSIX time structure.
1979 timetz2tm(TimeTzADT *time, struct pg_tm * tm, fsec_t *fsec, int *tzp)
1981 TimeOffset trem = time->time;
1983 #ifdef HAVE_INT64_TIMESTAMP
1984 tm->tm_hour = trem / USECS_PER_HOUR;
1985 trem -= tm->tm_hour * USECS_PER_HOUR;
1986 tm->tm_min = trem / USECS_PER_MINUTE;
1987 trem -= tm->tm_min * USECS_PER_MINUTE;
1988 tm->tm_sec = trem / USECS_PER_SEC;
1989 *fsec = trem - tm->tm_sec * USECS_PER_SEC;
1992 TMODULO(trem, tm->tm_hour, (double) SECS_PER_HOUR);
1993 TMODULO(trem, tm->tm_min, (double) SECS_PER_MINUTE);
1994 TMODULO(trem, tm->tm_sec, 1.0);
1995 trem = TIMEROUND(trem);
1996 /* roundoff may need to propagate to higher-order fields */
1999 trem = ceil(time->time);
2012 * Adjust time type for specified scale factor.
2013 * Used by PostgreSQL type system to stuff columns.
2016 timetz_scale(PG_FUNCTION_ARGS)
2018 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2019 int32 typmod = PG_GETARG_INT32(1);
2022 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2024 result->time = time->time;
2025 result->zone = time->zone;
2027 AdjustTimeForTypmod(&(result->time), typmod);
2029 PG_RETURN_TIMETZADT_P(result);
2034 timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
2039 /* Primary sort is by true (GMT-equivalent) time */
2040 #ifdef HAVE_INT64_TIMESTAMP
2041 t1 = time1->time + (time1->zone * USECS_PER_SEC);
2042 t2 = time2->time + (time2->zone * USECS_PER_SEC);
2044 t1 = time1->time + time1->zone;
2045 t2 = time2->time + time2->zone;
2054 * If same GMT time, sort by timezone; we only want to say that two
2055 * timetz's are equal if both the time and zone parts are equal.
2057 if (time1->zone > time2->zone)
2059 if (time1->zone < time2->zone)
2066 timetz_eq(PG_FUNCTION_ARGS)
2068 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2069 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2071 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) == 0);
2075 timetz_ne(PG_FUNCTION_ARGS)
2077 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2078 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2080 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) != 0);
2084 timetz_lt(PG_FUNCTION_ARGS)
2086 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2087 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2089 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) < 0);
2093 timetz_le(PG_FUNCTION_ARGS)
2095 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2096 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2098 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) <= 0);
2102 timetz_gt(PG_FUNCTION_ARGS)
2104 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2105 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2107 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) > 0);
2111 timetz_ge(PG_FUNCTION_ARGS)
2113 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2114 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2116 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) >= 0);
2120 timetz_cmp(PG_FUNCTION_ARGS)
2122 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2123 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2125 PG_RETURN_INT32(timetz_cmp_internal(time1, time2));
2129 timetz_hash(PG_FUNCTION_ARGS)
2131 TimeTzADT *key = PG_GETARG_TIMETZADT_P(0);
2135 * To avoid any problems with padding bytes in the struct, we figure the
2136 * field hashes separately and XOR them. This also provides a convenient
2137 * framework for dealing with the fact that the time field might be either
2140 #ifdef HAVE_INT64_TIMESTAMP
2141 thash = DatumGetUInt32(DirectFunctionCall1(hashint8,
2142 Int64GetDatumFast(key->time)));
2144 thash = DatumGetUInt32(DirectFunctionCall1(hashfloat8,
2145 Float8GetDatumFast(key->time)));
2147 thash ^= DatumGetUInt32(hash_uint32(key->zone));
2148 PG_RETURN_UINT32(thash);
2152 timetz_larger(PG_FUNCTION_ARGS)
2154 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2155 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2158 if (timetz_cmp_internal(time1, time2) > 0)
2162 PG_RETURN_TIMETZADT_P(result);
2166 timetz_smaller(PG_FUNCTION_ARGS)
2168 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2169 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2172 if (timetz_cmp_internal(time1, time2) < 0)
2176 PG_RETURN_TIMETZADT_P(result);
2179 /* timetz_pl_interval()
2180 * Add interval to timetz.
2183 timetz_pl_interval(PG_FUNCTION_ARGS)
2185 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2186 Interval *span = PG_GETARG_INTERVAL_P(1);
2189 #ifndef HAVE_INT64_TIMESTAMP
2193 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2195 #ifdef HAVE_INT64_TIMESTAMP
2196 result->time = time->time + span->time;
2197 result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2198 if (result->time < INT64CONST(0))
2199 result->time += USECS_PER_DAY;
2201 result->time = time->time + span->time;
2202 TMODULO(result->time, time1.time, (double) SECS_PER_DAY);
2203 if (result->time < 0)
2204 result->time += SECS_PER_DAY;
2207 result->zone = time->zone;
2209 PG_RETURN_TIMETZADT_P(result);
2212 /* timetz_mi_interval()
2213 * Subtract interval from timetz.
2216 timetz_mi_interval(PG_FUNCTION_ARGS)
2218 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2219 Interval *span = PG_GETARG_INTERVAL_P(1);
2222 #ifndef HAVE_INT64_TIMESTAMP
2226 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2228 #ifdef HAVE_INT64_TIMESTAMP
2229 result->time = time->time - span->time;
2230 result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2231 if (result->time < INT64CONST(0))
2232 result->time += USECS_PER_DAY;
2234 result->time = time->time - span->time;
2235 TMODULO(result->time, time1.time, (double) SECS_PER_DAY);
2236 if (result->time < 0)
2237 result->time += SECS_PER_DAY;
2240 result->zone = time->zone;
2242 PG_RETURN_TIMETZADT_P(result);
2245 /* overlaps_timetz() --- implements the SQL92 OVERLAPS operator.
2247 * Algorithm is per SQL92 spec. This is much harder than you'd think
2248 * because the spec requires us to deliver a non-null answer in some cases
2249 * where some of the inputs are null.
2252 overlaps_timetz(PG_FUNCTION_ARGS)
2255 * The arguments are TimeTzADT *, but we leave them as generic Datums for
2256 * convenience of notation --- and to avoid dereferencing nulls.
2258 Datum ts1 = PG_GETARG_DATUM(0);
2259 Datum te1 = PG_GETARG_DATUM(1);
2260 Datum ts2 = PG_GETARG_DATUM(2);
2261 Datum te2 = PG_GETARG_DATUM(3);
2262 bool ts1IsNull = PG_ARGISNULL(0);
2263 bool te1IsNull = PG_ARGISNULL(1);
2264 bool ts2IsNull = PG_ARGISNULL(2);
2265 bool te2IsNull = PG_ARGISNULL(3);
2267 #define TIMETZ_GT(t1,t2) \
2268 DatumGetBool(DirectFunctionCall2(timetz_gt,t1,t2))
2269 #define TIMETZ_LT(t1,t2) \
2270 DatumGetBool(DirectFunctionCall2(timetz_lt,t1,t2))
2273 * If both endpoints of interval 1 are null, the result is null (unknown).
2274 * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2275 * take ts1 as the lesser endpoint.
2281 /* swap null for non-null */
2285 else if (!te1IsNull)
2287 if (TIMETZ_GT(ts1, te1))
2296 /* Likewise for interval 2. */
2301 /* swap null for non-null */
2305 else if (!te2IsNull)
2307 if (TIMETZ_GT(ts2, te2))
2317 * At this point neither ts1 nor ts2 is null, so we can consider three
2318 * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2320 if (TIMETZ_GT(ts1, ts2))
2323 * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2324 * in the presence of nulls it's not quite completely so.
2328 if (TIMETZ_LT(ts1, te2))
2329 PG_RETURN_BOOL(true);
2334 * If te1 is not null then we had ts1 <= te1 above, and we just found
2335 * ts1 >= te2, hence te1 >= te2.
2337 PG_RETURN_BOOL(false);
2339 else if (TIMETZ_LT(ts1, ts2))
2341 /* This case is ts2 < te1 OR te2 < te1 */
2344 if (TIMETZ_LT(ts2, te1))
2345 PG_RETURN_BOOL(true);
2350 * If te2 is not null then we had ts2 <= te2 above, and we just found
2351 * ts2 >= te1, hence te2 >= te1.
2353 PG_RETURN_BOOL(false);
2358 * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2359 * rather silly way of saying "true if both are nonnull, else null".
2361 if (te1IsNull || te2IsNull)
2363 PG_RETURN_BOOL(true);
2372 timetz_time(PG_FUNCTION_ARGS)
2374 TimeTzADT *timetz = PG_GETARG_TIMETZADT_P(0);
2377 /* swallow the time zone and just return the time */
2378 result = timetz->time;
2380 PG_RETURN_TIMEADT(result);
2385 time_timetz(PG_FUNCTION_ARGS)
2387 TimeADT time = PG_GETARG_TIMEADT(0);
2394 GetCurrentDateTime(tm);
2395 time2tm(time, tm, &fsec);
2396 tz = DetermineTimeZoneOffset(tm, session_timezone);
2398 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2400 result->time = time;
2403 PG_RETURN_TIMETZADT_P(result);
2407 /* timestamptz_timetz()
2408 * Convert timestamp to timetz data type.
2411 timestamptz_timetz(PG_FUNCTION_ARGS)
2413 TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
2421 if (TIMESTAMP_NOT_FINITE(timestamp))
2424 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
2426 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2427 errmsg("timestamp out of range")));
2429 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2431 tm2timetz(tm, fsec, tz, result);
2433 PG_RETURN_TIMETZADT_P(result);
2437 /* datetimetz_timestamptz()
2438 * Convert date and timetz to timestamp with time zone data type.
2439 * Timestamp is stored in GMT, so add the time zone
2440 * stored with the timetz to the result.
2441 * - thomas 2000-03-10
2444 datetimetz_timestamptz(PG_FUNCTION_ARGS)
2446 DateADT date = PG_GETARG_DATEADT(0);
2447 TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2450 if (DATE_IS_NOBEGIN(date))
2451 TIMESTAMP_NOBEGIN(result);
2452 else if (DATE_IS_NOEND(date))
2453 TIMESTAMP_NOEND(result);
2456 #ifdef HAVE_INT64_TIMESTAMP
2457 result = date * USECS_PER_DAY + time->time + time->zone * USECS_PER_SEC;
2459 result = date * (double) SECS_PER_DAY + time->time + time->zone;
2463 PG_RETURN_TIMESTAMP(result);
2468 * Extract specified field from time type.
2471 timetz_part(PG_FUNCTION_ARGS)
2473 text *units = PG_GETARG_TEXT_PP(0);
2474 TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2480 lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
2481 VARSIZE_ANY_EXHDR(units),
2484 type = DecodeUnits(0, lowunits, &val);
2485 if (type == UNKNOWN_FIELD)
2486 type = DecodeSpecial(0, lowunits, &val);
2496 timetz2tm(time, tm, &fsec, &tz);
2506 result /= SECS_PER_MINUTE;
2507 FMODULO(result, dummy, (double) SECS_PER_MINUTE);
2512 FMODULO(dummy, result, (double) SECS_PER_HOUR);
2516 #ifdef HAVE_INT64_TIMESTAMP
2517 result = tm->tm_sec * 1000000.0 + fsec;
2519 result = (tm->tm_sec + fsec) * 1000000;
2524 #ifdef HAVE_INT64_TIMESTAMP
2525 result = tm->tm_sec * 1000.0 + fsec / 1000.0;
2527 result = (tm->tm_sec + fsec) * 1000;
2532 #ifdef HAVE_INT64_TIMESTAMP
2533 result = tm->tm_sec + fsec / 1000000.0;
2535 result = tm->tm_sec + fsec;
2540 result = tm->tm_min;
2544 result = tm->tm_hour;
2553 case DTK_MILLENNIUM:
2556 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2557 errmsg("\"time with time zone\" units \"%s\" not recognized",
2562 else if (type == RESERV && val == DTK_EPOCH)
2564 #ifdef HAVE_INT64_TIMESTAMP
2565 result = time->time / 1000000.0 + time->zone;
2567 result = time->time + time->zone;
2573 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2574 errmsg("\"time with time zone\" units \"%s\" not recognized",
2579 PG_RETURN_FLOAT8(result);
2583 * Encode time with time zone type with specified time zone.
2584 * Applies DST rules as of the current date.
2587 timetz_zone(PG_FUNCTION_ARGS)
2589 text *zone = PG_GETARG_TEXT_PP(0);
2590 TimeTzADT *t = PG_GETARG_TIMETZADT_P(1);
2593 char tzname[TZ_STRLEN_MAX + 1];
2600 * Look up the requested timezone. First we look in the date token table
2601 * (to handle cases like "EST"), and if that fails, we look in the
2602 * timezone database (to handle cases like "America/New_York"). (This
2603 * matches the order in which timestamp input checks the cases; it's
2604 * important because the timezone database unwisely uses a few zone names
2605 * that are identical to offset abbreviations.)
2607 text_to_cstring_buffer(zone, tzname, sizeof(tzname));
2608 lowzone = downcase_truncate_identifier(tzname,
2612 type = DecodeSpecial(0, lowzone, &val);
2614 if (type == TZ || type == DTZ)
2615 tz = val * MINS_PER_HOUR;
2618 tzp = pg_tzset(tzname);
2621 /* Get the offset-from-GMT that is valid today for the zone */
2622 pg_time_t now = (pg_time_t) time(NULL);
2625 tm = pg_localtime(&now, tzp);
2626 tz = -tm->tm_gmtoff;
2631 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2632 errmsg("time zone \"%s\" not recognized", tzname)));
2633 tz = 0; /* keep compiler quiet */
2637 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2639 #ifdef HAVE_INT64_TIMESTAMP
2640 result->time = t->time + (t->zone - tz) * USECS_PER_SEC;
2641 while (result->time < INT64CONST(0))
2642 result->time += USECS_PER_DAY;
2643 while (result->time >= USECS_PER_DAY)
2644 result->time -= USECS_PER_DAY;
2646 result->time = t->time + (t->zone - tz);
2647 while (result->time < 0)
2648 result->time += SECS_PER_DAY;
2649 while (result->time >= SECS_PER_DAY)
2650 result->time -= SECS_PER_DAY;
2655 PG_RETURN_TIMETZADT_P(result);
2659 * Encode time with time zone type with specified time interval as time zone.
2662 timetz_izone(PG_FUNCTION_ARGS)
2664 Interval *zone = PG_GETARG_INTERVAL_P(0);
2665 TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2669 if (zone->month != 0)
2671 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2672 errmsg("\"interval\" time zone \"%s\" not valid",
2673 DatumGetCString(DirectFunctionCall1(interval_out,
2674 PointerGetDatum(zone))))));
2676 #ifdef HAVE_INT64_TIMESTAMP
2677 tz = -(zone->time / USECS_PER_SEC);
2682 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2684 #ifdef HAVE_INT64_TIMESTAMP
2685 result->time = time->time + (time->zone - tz) * USECS_PER_SEC;
2686 while (result->time < INT64CONST(0))
2687 result->time += USECS_PER_DAY;
2688 while (result->time >= USECS_PER_DAY)
2689 result->time -= USECS_PER_DAY;
2691 result->time = time->time + (time->zone - tz);
2692 while (result->time < 0)
2693 result->time += SECS_PER_DAY;
2694 while (result->time >= SECS_PER_DAY)
2695 result->time -= SECS_PER_DAY;
2700 PG_RETURN_TIMETZADT_P(result);