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);
894 * Convert timestamp to date data type.
897 timestamp_date(PG_FUNCTION_ARGS)
899 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
905 if (TIMESTAMP_IS_NOBEGIN(timestamp))
906 DATE_NOBEGIN(result);
907 else if (TIMESTAMP_IS_NOEND(timestamp))
911 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
913 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
914 errmsg("timestamp out of range")));
916 result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
919 PG_RETURN_DATEADT(result);
923 /* date_timestamptz()
924 * Convert date to timestamp with time zone data type.
927 date_timestamptz(PG_FUNCTION_ARGS)
929 DateADT dateVal = PG_GETARG_DATEADT(0);
932 result = date2timestamptz(dateVal);
934 PG_RETURN_TIMESTAMP(result);
938 /* timestamptz_date()
939 * Convert timestamp with time zone to date data type.
942 timestamptz_date(PG_FUNCTION_ARGS)
944 TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
952 if (TIMESTAMP_IS_NOBEGIN(timestamp))
953 DATE_NOBEGIN(result);
954 else if (TIMESTAMP_IS_NOEND(timestamp))
958 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
960 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
961 errmsg("timestamp out of range")));
963 result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
966 PG_RETURN_DATEADT(result);
971 * Convert abstime to date data type.
974 abstime_date(PG_FUNCTION_ARGS)
976 AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
984 case INVALID_ABSTIME:
986 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
987 errmsg("cannot convert reserved abstime value to date")));
988 result = 0; /* keep compiler quiet */
991 case NOSTART_ABSTIME:
992 DATE_NOBEGIN(result);
1000 abstime2tm(abstime, &tz, tm, NULL);
1001 result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
1005 PG_RETURN_DATEADT(result);
1009 /*****************************************************************************
1011 *****************************************************************************/
1014 time_in(PG_FUNCTION_ARGS)
1016 char *str = PG_GETARG_CSTRING(0);
1019 Oid typelem = PG_GETARG_OID(1);
1021 int32 typmod = PG_GETARG_INT32(2);
1029 char workbuf[MAXDATELEN + 1];
1030 char *field[MAXDATEFIELDS];
1032 int ftype[MAXDATEFIELDS];
1034 dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
1035 field, ftype, MAXDATEFIELDS, &nf);
1037 dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
1039 DateTimeParseError(dterr, str, "time");
1041 tm2time(tm, fsec, &result);
1042 AdjustTimeForTypmod(&result, typmod);
1044 PG_RETURN_TIMEADT(result);
1048 * Convert a tm structure to a time data type.
1051 tm2time(struct pg_tm * tm, fsec_t fsec, TimeADT *result)
1053 #ifdef HAVE_INT64_TIMESTAMP
1054 *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1055 * USECS_PER_SEC) + fsec;
1057 *result = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec + fsec;
1063 * Convert time data type to POSIX time structure.
1065 * For dates within the range of pg_time_t, convert to the local time zone.
1066 * If out of this range, leave as UTC (in practice that could only happen
1067 * if pg_time_t is just 32 bits) - thomas 97/05/27
1070 time2tm(TimeADT time, struct pg_tm * tm, fsec_t *fsec)
1072 #ifdef HAVE_INT64_TIMESTAMP
1073 tm->tm_hour = time / USECS_PER_HOUR;
1074 time -= tm->tm_hour * USECS_PER_HOUR;
1075 tm->tm_min = time / USECS_PER_MINUTE;
1076 time -= tm->tm_min * USECS_PER_MINUTE;
1077 tm->tm_sec = time / USECS_PER_SEC;
1078 time -= tm->tm_sec * USECS_PER_SEC;
1085 TMODULO(trem, tm->tm_hour, (double) SECS_PER_HOUR);
1086 TMODULO(trem, tm->tm_min, (double) SECS_PER_MINUTE);
1087 TMODULO(trem, tm->tm_sec, 1.0);
1088 trem = TIMEROUND(trem);
1089 /* roundoff may need to propagate to higher-order fields */
1102 time_out(PG_FUNCTION_ARGS)
1104 TimeADT time = PG_GETARG_TIMEADT(0);
1109 char buf[MAXDATELEN + 1];
1111 time2tm(time, tm, &fsec);
1112 EncodeTimeOnly(tm, fsec, NULL, DateStyle, buf);
1114 result = pstrdup(buf);
1115 PG_RETURN_CSTRING(result);
1119 * time_recv - converts external binary format to time
1121 * We make no attempt to provide compatibility between int and float
1122 * time representations ...
1125 time_recv(PG_FUNCTION_ARGS)
1127 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
1130 Oid typelem = PG_GETARG_OID(1);
1132 int32 typmod = PG_GETARG_INT32(2);
1135 #ifdef HAVE_INT64_TIMESTAMP
1136 result = pq_getmsgint64(buf);
1138 if (result < INT64CONST(0) || result > USECS_PER_DAY)
1140 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1141 errmsg("time out of range")));
1143 result = pq_getmsgfloat8(buf);
1145 if (result < 0 || result > (double) SECS_PER_DAY)
1147 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1148 errmsg("time out of range")));
1151 AdjustTimeForTypmod(&result, typmod);
1153 PG_RETURN_TIMEADT(result);
1157 * time_send - converts time to binary format
1160 time_send(PG_FUNCTION_ARGS)
1162 TimeADT time = PG_GETARG_TIMEADT(0);
1165 pq_begintypsend(&buf);
1166 #ifdef HAVE_INT64_TIMESTAMP
1167 pq_sendint64(&buf, time);
1169 pq_sendfloat8(&buf, time);
1171 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
1175 timetypmodin(PG_FUNCTION_ARGS)
1177 ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
1179 PG_RETURN_INT32(anytime_typmodin(false, ta));
1183 timetypmodout(PG_FUNCTION_ARGS)
1185 int32 typmod = PG_GETARG_INT32(0);
1187 PG_RETURN_CSTRING(anytime_typmodout(false, typmod));
1192 * Adjust time type for specified scale factor.
1193 * Used by PostgreSQL type system to stuff columns.
1196 time_scale(PG_FUNCTION_ARGS)
1198 TimeADT time = PG_GETARG_TIMEADT(0);
1199 int32 typmod = PG_GETARG_INT32(1);
1203 AdjustTimeForTypmod(&result, typmod);
1205 PG_RETURN_TIMEADT(result);
1208 /* AdjustTimeForTypmod()
1209 * Force the precision of the time value to a specified value.
1210 * Uses *exactly* the same code as in AdjustTimestampForTypemod()
1211 * but we make a separate copy because those types do not
1212 * have a fundamental tie together but rather a coincidence of
1213 * implementation. - thomas
1216 AdjustTimeForTypmod(TimeADT *time, int32 typmod)
1218 #ifdef HAVE_INT64_TIMESTAMP
1219 static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1220 INT64CONST(1000000),
1229 static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1239 /* note MAX_TIME_PRECISION differs in this case */
1240 static const double TimeScales[MAX_TIME_PRECISION + 1] = {
1255 if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1258 * Note: this round-to-nearest code is not completely consistent about
1259 * rounding values that are exactly halfway between integral values.
1260 * On most platforms, rint() will implement round-to-nearest-even, but
1261 * the integer code always rounds up (away from zero). Is it worth
1262 * trying to be consistent?
1264 #ifdef HAVE_INT64_TIMESTAMP
1265 if (*time >= INT64CONST(0))
1266 *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1269 *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1270 TimeScales[typmod]);
1272 *time = rint((double) *time * TimeScales[typmod]) / TimeScales[typmod];
1279 time_eq(PG_FUNCTION_ARGS)
1281 TimeADT time1 = PG_GETARG_TIMEADT(0);
1282 TimeADT time2 = PG_GETARG_TIMEADT(1);
1284 PG_RETURN_BOOL(time1 == time2);
1288 time_ne(PG_FUNCTION_ARGS)
1290 TimeADT time1 = PG_GETARG_TIMEADT(0);
1291 TimeADT time2 = PG_GETARG_TIMEADT(1);
1293 PG_RETURN_BOOL(time1 != time2);
1297 time_lt(PG_FUNCTION_ARGS)
1299 TimeADT time1 = PG_GETARG_TIMEADT(0);
1300 TimeADT time2 = PG_GETARG_TIMEADT(1);
1302 PG_RETURN_BOOL(time1 < time2);
1306 time_le(PG_FUNCTION_ARGS)
1308 TimeADT time1 = PG_GETARG_TIMEADT(0);
1309 TimeADT time2 = PG_GETARG_TIMEADT(1);
1311 PG_RETURN_BOOL(time1 <= time2);
1315 time_gt(PG_FUNCTION_ARGS)
1317 TimeADT time1 = PG_GETARG_TIMEADT(0);
1318 TimeADT time2 = PG_GETARG_TIMEADT(1);
1320 PG_RETURN_BOOL(time1 > time2);
1324 time_ge(PG_FUNCTION_ARGS)
1326 TimeADT time1 = PG_GETARG_TIMEADT(0);
1327 TimeADT time2 = PG_GETARG_TIMEADT(1);
1329 PG_RETURN_BOOL(time1 >= time2);
1333 time_cmp(PG_FUNCTION_ARGS)
1335 TimeADT time1 = PG_GETARG_TIMEADT(0);
1336 TimeADT time2 = PG_GETARG_TIMEADT(1);
1339 PG_RETURN_INT32(-1);
1346 time_hash(PG_FUNCTION_ARGS)
1348 /* We can use either hashint8 or hashfloat8 directly */
1349 #ifdef HAVE_INT64_TIMESTAMP
1350 return hashint8(fcinfo);
1352 return hashfloat8(fcinfo);
1357 time_larger(PG_FUNCTION_ARGS)
1359 TimeADT time1 = PG_GETARG_TIMEADT(0);
1360 TimeADT time2 = PG_GETARG_TIMEADT(1);
1362 PG_RETURN_TIMEADT((time1 > time2) ? time1 : time2);
1366 time_smaller(PG_FUNCTION_ARGS)
1368 TimeADT time1 = PG_GETARG_TIMEADT(0);
1369 TimeADT time2 = PG_GETARG_TIMEADT(1);
1371 PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
1374 /* overlaps_time() --- implements the SQL92 OVERLAPS operator.
1376 * Algorithm is per SQL92 spec. This is much harder than you'd think
1377 * because the spec requires us to deliver a non-null answer in some cases
1378 * where some of the inputs are null.
1381 overlaps_time(PG_FUNCTION_ARGS)
1384 * The arguments are TimeADT, but we leave them as generic Datums to avoid
1385 * dereferencing nulls (TimeADT is pass-by-reference!)
1387 Datum ts1 = PG_GETARG_DATUM(0);
1388 Datum te1 = PG_GETARG_DATUM(1);
1389 Datum ts2 = PG_GETARG_DATUM(2);
1390 Datum te2 = PG_GETARG_DATUM(3);
1391 bool ts1IsNull = PG_ARGISNULL(0);
1392 bool te1IsNull = PG_ARGISNULL(1);
1393 bool ts2IsNull = PG_ARGISNULL(2);
1394 bool te2IsNull = PG_ARGISNULL(3);
1396 #define TIMEADT_GT(t1,t2) \
1397 (DatumGetTimeADT(t1) > DatumGetTimeADT(t2))
1398 #define TIMEADT_LT(t1,t2) \
1399 (DatumGetTimeADT(t1) < DatumGetTimeADT(t2))
1402 * If both endpoints of interval 1 are null, the result is null (unknown).
1403 * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
1404 * take ts1 as the lesser endpoint.
1410 /* swap null for non-null */
1414 else if (!te1IsNull)
1416 if (TIMEADT_GT(ts1, te1))
1425 /* Likewise for interval 2. */
1430 /* swap null for non-null */
1434 else if (!te2IsNull)
1436 if (TIMEADT_GT(ts2, te2))
1446 * At this point neither ts1 nor ts2 is null, so we can consider three
1447 * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
1449 if (TIMEADT_GT(ts1, ts2))
1452 * This case is ts1 < te2 OR te1 < te2, which may look redundant but
1453 * in the presence of nulls it's not quite completely so.
1457 if (TIMEADT_LT(ts1, te2))
1458 PG_RETURN_BOOL(true);
1463 * If te1 is not null then we had ts1 <= te1 above, and we just found
1464 * ts1 >= te2, hence te1 >= te2.
1466 PG_RETURN_BOOL(false);
1468 else if (TIMEADT_LT(ts1, ts2))
1470 /* This case is ts2 < te1 OR te2 < te1 */
1473 if (TIMEADT_LT(ts2, te1))
1474 PG_RETURN_BOOL(true);
1479 * If te2 is not null then we had ts2 <= te2 above, and we just found
1480 * ts2 >= te1, hence te2 >= te1.
1482 PG_RETURN_BOOL(false);
1487 * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
1488 * rather silly way of saying "true if both are nonnull, else null".
1490 if (te1IsNull || te2IsNull)
1492 PG_RETURN_BOOL(true);
1500 * Convert timestamp to time data type.
1503 timestamp_time(PG_FUNCTION_ARGS)
1505 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
1511 if (TIMESTAMP_NOT_FINITE(timestamp))
1514 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1516 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1517 errmsg("timestamp out of range")));
1519 #ifdef HAVE_INT64_TIMESTAMP
1522 * Could also do this with time = (timestamp / USECS_PER_DAY *
1523 * USECS_PER_DAY) - timestamp;
1525 result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1526 USECS_PER_SEC) + fsec;
1528 result = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec + fsec;
1531 PG_RETURN_TIMEADT(result);
1534 /* timestamptz_time()
1535 * Convert timestamptz to time data type.
1538 timestamptz_time(PG_FUNCTION_ARGS)
1540 TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
1548 if (TIMESTAMP_NOT_FINITE(timestamp))
1551 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
1553 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1554 errmsg("timestamp out of range")));
1556 #ifdef HAVE_INT64_TIMESTAMP
1559 * Could also do this with time = (timestamp / USECS_PER_DAY *
1560 * USECS_PER_DAY) - timestamp;
1562 result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1563 USECS_PER_SEC) + fsec;
1565 result = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec + fsec;
1568 PG_RETURN_TIMEADT(result);
1571 /* datetime_timestamp()
1572 * Convert date and time to timestamp data type.
1575 datetime_timestamp(PG_FUNCTION_ARGS)
1577 DateADT date = PG_GETARG_DATEADT(0);
1578 TimeADT time = PG_GETARG_TIMEADT(1);
1581 result = date2timestamp(date);
1582 if (!TIMESTAMP_NOT_FINITE(result))
1585 PG_RETURN_TIMESTAMP(result);
1589 * Convert time to interval data type.
1592 time_interval(PG_FUNCTION_ARGS)
1594 TimeADT time = PG_GETARG_TIMEADT(0);
1597 result = (Interval *) palloc(sizeof(Interval));
1599 result->time = time;
1603 PG_RETURN_INTERVAL_P(result);
1607 * Convert interval to time data type.
1609 * This is defined as producing the fractional-day portion of the interval.
1610 * Therefore, we can just ignore the months field. It is not real clear
1611 * what to do with negative intervals, but we choose to subtract the floor,
1612 * so that, say, '-2 hours' becomes '22:00:00'.
1615 interval_time(PG_FUNCTION_ARGS)
1617 Interval *span = PG_GETARG_INTERVAL_P(0);
1620 #ifdef HAVE_INT64_TIMESTAMP
1623 result = span->time;
1624 if (result >= USECS_PER_DAY)
1626 days = result / USECS_PER_DAY;
1627 result -= days * USECS_PER_DAY;
1629 else if (result < 0)
1631 days = (-result + USECS_PER_DAY - 1) / USECS_PER_DAY;
1632 result += days * USECS_PER_DAY;
1635 result = span->time;
1636 if (result >= (double) SECS_PER_DAY || result < 0)
1637 result -= floor(result / (double) SECS_PER_DAY) * (double) SECS_PER_DAY;
1640 PG_RETURN_TIMEADT(result);
1644 * Subtract two times to produce an interval.
1647 time_mi_time(PG_FUNCTION_ARGS)
1649 TimeADT time1 = PG_GETARG_TIMEADT(0);
1650 TimeADT time2 = PG_GETARG_TIMEADT(1);
1653 result = (Interval *) palloc(sizeof(Interval));
1657 result->time = time1 - time2;
1659 PG_RETURN_INTERVAL_P(result);
1662 /* time_pl_interval()
1663 * Add interval to time.
1666 time_pl_interval(PG_FUNCTION_ARGS)
1668 TimeADT time = PG_GETARG_TIMEADT(0);
1669 Interval *span = PG_GETARG_INTERVAL_P(1);
1672 #ifdef HAVE_INT64_TIMESTAMP
1673 result = time + span->time;
1674 result -= result / USECS_PER_DAY * USECS_PER_DAY;
1675 if (result < INT64CONST(0))
1676 result += USECS_PER_DAY;
1680 result = time + span->time;
1681 TMODULO(result, time1, (double) SECS_PER_DAY);
1683 result += SECS_PER_DAY;
1686 PG_RETURN_TIMEADT(result);
1689 /* time_mi_interval()
1690 * Subtract interval from time.
1693 time_mi_interval(PG_FUNCTION_ARGS)
1695 TimeADT time = PG_GETARG_TIMEADT(0);
1696 Interval *span = PG_GETARG_INTERVAL_P(1);
1699 #ifdef HAVE_INT64_TIMESTAMP
1700 result = time - span->time;
1701 result -= result / USECS_PER_DAY * USECS_PER_DAY;
1702 if (result < INT64CONST(0))
1703 result += USECS_PER_DAY;
1707 result = time - span->time;
1708 TMODULO(result, time1, (double) SECS_PER_DAY);
1710 result += SECS_PER_DAY;
1713 PG_RETURN_TIMEADT(result);
1718 * Extract specified field from time type.
1721 time_part(PG_FUNCTION_ARGS)
1723 text *units = PG_GETARG_TEXT_PP(0);
1724 TimeADT time = PG_GETARG_TIMEADT(1);
1730 lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
1731 VARSIZE_ANY_EXHDR(units),
1734 type = DecodeUnits(0, lowunits, &val);
1735 if (type == UNKNOWN_FIELD)
1736 type = DecodeSpecial(0, lowunits, &val);
1744 time2tm(time, tm, &fsec);
1749 #ifdef HAVE_INT64_TIMESTAMP
1750 result = tm->tm_sec * 1000000.0 + fsec;
1752 result = (tm->tm_sec + fsec) * 1000000;
1757 #ifdef HAVE_INT64_TIMESTAMP
1758 result = tm->tm_sec * 1000.0 + fsec / 1000.0;
1760 result = (tm->tm_sec + fsec) * 1000;
1765 #ifdef HAVE_INT64_TIMESTAMP
1766 result = tm->tm_sec + fsec / 1000000.0;
1768 result = tm->tm_sec + fsec;
1773 result = tm->tm_min;
1777 result = tm->tm_hour;
1789 case DTK_MILLENNIUM:
1793 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1794 errmsg("\"time\" units \"%s\" not recognized",
1799 else if (type == RESERV && val == DTK_EPOCH)
1801 #ifdef HAVE_INT64_TIMESTAMP
1802 result = time / 1000000.0;
1810 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1811 errmsg("\"time\" units \"%s\" not recognized",
1816 PG_RETURN_FLOAT8(result);
1820 /*****************************************************************************
1821 * Time With Time Zone ADT
1822 *****************************************************************************/
1825 * Convert a tm structure to a time data type.
1828 tm2timetz(struct pg_tm * tm, fsec_t fsec, int tz, TimeTzADT *result)
1830 #ifdef HAVE_INT64_TIMESTAMP
1831 result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1832 USECS_PER_SEC) + fsec;
1834 result->time = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec + fsec;
1842 timetz_in(PG_FUNCTION_ARGS)
1844 char *str = PG_GETARG_CSTRING(0);
1847 Oid typelem = PG_GETARG_OID(1);
1849 int32 typmod = PG_GETARG_INT32(2);
1857 char workbuf[MAXDATELEN + 1];
1858 char *field[MAXDATEFIELDS];
1860 int ftype[MAXDATEFIELDS];
1862 dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
1863 field, ftype, MAXDATEFIELDS, &nf);
1865 dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
1867 DateTimeParseError(dterr, str, "time with time zone");
1869 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
1870 tm2timetz(tm, fsec, tz, result);
1871 AdjustTimeForTypmod(&(result->time), typmod);
1873 PG_RETURN_TIMETZADT_P(result);
1877 timetz_out(PG_FUNCTION_ARGS)
1879 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
1885 char buf[MAXDATELEN + 1];
1887 timetz2tm(time, tm, &fsec, &tz);
1888 EncodeTimeOnly(tm, fsec, &tz, DateStyle, buf);
1890 result = pstrdup(buf);
1891 PG_RETURN_CSTRING(result);
1895 * timetz_recv - converts external binary format to timetz
1898 timetz_recv(PG_FUNCTION_ARGS)
1900 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
1903 Oid typelem = PG_GETARG_OID(1);
1905 int32 typmod = PG_GETARG_INT32(2);
1908 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
1910 #ifdef HAVE_INT64_TIMESTAMP
1911 result->time = pq_getmsgint64(buf);
1913 if (result->time < INT64CONST(0) || result->time > USECS_PER_DAY)
1915 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1916 errmsg("time out of range")));
1918 result->time = pq_getmsgfloat8(buf);
1920 if (result->time < 0 || result->time > (double) SECS_PER_DAY)
1922 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1923 errmsg("time out of range")));
1926 result->zone = pq_getmsgint(buf, sizeof(result->zone));
1928 /* we allow GMT displacements up to 14:59:59, cf DecodeTimezone() */
1929 if (result->zone <= -15 * SECS_PER_HOUR ||
1930 result->zone >= 15 * SECS_PER_HOUR)
1932 (errcode(ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE),
1933 errmsg("time zone displacement out of range")));
1935 AdjustTimeForTypmod(&(result->time), typmod);
1937 PG_RETURN_TIMETZADT_P(result);
1941 * timetz_send - converts timetz to binary format
1944 timetz_send(PG_FUNCTION_ARGS)
1946 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
1949 pq_begintypsend(&buf);
1950 #ifdef HAVE_INT64_TIMESTAMP
1951 pq_sendint64(&buf, time->time);
1953 pq_sendfloat8(&buf, time->time);
1955 pq_sendint(&buf, time->zone, sizeof(time->zone));
1956 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
1960 timetztypmodin(PG_FUNCTION_ARGS)
1962 ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
1964 PG_RETURN_INT32(anytime_typmodin(true, ta));
1968 timetztypmodout(PG_FUNCTION_ARGS)
1970 int32 typmod = PG_GETARG_INT32(0);
1972 PG_RETURN_CSTRING(anytime_typmodout(true, typmod));
1977 * Convert TIME WITH TIME ZONE data type to POSIX time structure.
1980 timetz2tm(TimeTzADT *time, struct pg_tm * tm, fsec_t *fsec, int *tzp)
1982 TimeOffset trem = time->time;
1984 #ifdef HAVE_INT64_TIMESTAMP
1985 tm->tm_hour = trem / USECS_PER_HOUR;
1986 trem -= tm->tm_hour * USECS_PER_HOUR;
1987 tm->tm_min = trem / USECS_PER_MINUTE;
1988 trem -= tm->tm_min * USECS_PER_MINUTE;
1989 tm->tm_sec = trem / USECS_PER_SEC;
1990 *fsec = trem - tm->tm_sec * USECS_PER_SEC;
1993 TMODULO(trem, tm->tm_hour, (double) SECS_PER_HOUR);
1994 TMODULO(trem, tm->tm_min, (double) SECS_PER_MINUTE);
1995 TMODULO(trem, tm->tm_sec, 1.0);
1996 trem = TIMEROUND(trem);
1997 /* roundoff may need to propagate to higher-order fields */
2000 trem = ceil(time->time);
2013 * Adjust time type for specified scale factor.
2014 * Used by PostgreSQL type system to stuff columns.
2017 timetz_scale(PG_FUNCTION_ARGS)
2019 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2020 int32 typmod = PG_GETARG_INT32(1);
2023 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2025 result->time = time->time;
2026 result->zone = time->zone;
2028 AdjustTimeForTypmod(&(result->time), typmod);
2030 PG_RETURN_TIMETZADT_P(result);
2035 timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
2040 /* Primary sort is by true (GMT-equivalent) time */
2041 #ifdef HAVE_INT64_TIMESTAMP
2042 t1 = time1->time + (time1->zone * USECS_PER_SEC);
2043 t2 = time2->time + (time2->zone * USECS_PER_SEC);
2045 t1 = time1->time + time1->zone;
2046 t2 = time2->time + time2->zone;
2055 * If same GMT time, sort by timezone; we only want to say that two
2056 * timetz's are equal if both the time and zone parts are equal.
2058 if (time1->zone > time2->zone)
2060 if (time1->zone < time2->zone)
2067 timetz_eq(PG_FUNCTION_ARGS)
2069 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2070 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2072 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) == 0);
2076 timetz_ne(PG_FUNCTION_ARGS)
2078 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2079 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2081 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) != 0);
2085 timetz_lt(PG_FUNCTION_ARGS)
2087 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2088 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2090 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) < 0);
2094 timetz_le(PG_FUNCTION_ARGS)
2096 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2097 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2099 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) <= 0);
2103 timetz_gt(PG_FUNCTION_ARGS)
2105 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2106 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2108 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) > 0);
2112 timetz_ge(PG_FUNCTION_ARGS)
2114 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2115 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2117 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) >= 0);
2121 timetz_cmp(PG_FUNCTION_ARGS)
2123 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2124 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2126 PG_RETURN_INT32(timetz_cmp_internal(time1, time2));
2130 timetz_hash(PG_FUNCTION_ARGS)
2132 TimeTzADT *key = PG_GETARG_TIMETZADT_P(0);
2136 * To avoid any problems with padding bytes in the struct, we figure the
2137 * field hashes separately and XOR them. This also provides a convenient
2138 * framework for dealing with the fact that the time field might be either
2141 #ifdef HAVE_INT64_TIMESTAMP
2142 thash = DatumGetUInt32(DirectFunctionCall1(hashint8,
2143 Int64GetDatumFast(key->time)));
2145 thash = DatumGetUInt32(DirectFunctionCall1(hashfloat8,
2146 Float8GetDatumFast(key->time)));
2148 thash ^= DatumGetUInt32(hash_uint32(key->zone));
2149 PG_RETURN_UINT32(thash);
2153 timetz_larger(PG_FUNCTION_ARGS)
2155 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2156 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2159 if (timetz_cmp_internal(time1, time2) > 0)
2163 PG_RETURN_TIMETZADT_P(result);
2167 timetz_smaller(PG_FUNCTION_ARGS)
2169 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2170 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2173 if (timetz_cmp_internal(time1, time2) < 0)
2177 PG_RETURN_TIMETZADT_P(result);
2180 /* timetz_pl_interval()
2181 * Add interval to timetz.
2184 timetz_pl_interval(PG_FUNCTION_ARGS)
2186 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2187 Interval *span = PG_GETARG_INTERVAL_P(1);
2190 #ifndef HAVE_INT64_TIMESTAMP
2194 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2196 #ifdef HAVE_INT64_TIMESTAMP
2197 result->time = time->time + span->time;
2198 result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2199 if (result->time < INT64CONST(0))
2200 result->time += USECS_PER_DAY;
2202 result->time = time->time + span->time;
2203 TMODULO(result->time, time1.time, (double) SECS_PER_DAY);
2204 if (result->time < 0)
2205 result->time += SECS_PER_DAY;
2208 result->zone = time->zone;
2210 PG_RETURN_TIMETZADT_P(result);
2213 /* timetz_mi_interval()
2214 * Subtract interval from timetz.
2217 timetz_mi_interval(PG_FUNCTION_ARGS)
2219 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2220 Interval *span = PG_GETARG_INTERVAL_P(1);
2223 #ifndef HAVE_INT64_TIMESTAMP
2227 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2229 #ifdef HAVE_INT64_TIMESTAMP
2230 result->time = time->time - span->time;
2231 result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2232 if (result->time < INT64CONST(0))
2233 result->time += USECS_PER_DAY;
2235 result->time = time->time - span->time;
2236 TMODULO(result->time, time1.time, (double) SECS_PER_DAY);
2237 if (result->time < 0)
2238 result->time += SECS_PER_DAY;
2241 result->zone = time->zone;
2243 PG_RETURN_TIMETZADT_P(result);
2246 /* overlaps_timetz() --- implements the SQL92 OVERLAPS operator.
2248 * Algorithm is per SQL92 spec. This is much harder than you'd think
2249 * because the spec requires us to deliver a non-null answer in some cases
2250 * where some of the inputs are null.
2253 overlaps_timetz(PG_FUNCTION_ARGS)
2256 * The arguments are TimeTzADT *, but we leave them as generic Datums for
2257 * convenience of notation --- and to avoid dereferencing nulls.
2259 Datum ts1 = PG_GETARG_DATUM(0);
2260 Datum te1 = PG_GETARG_DATUM(1);
2261 Datum ts2 = PG_GETARG_DATUM(2);
2262 Datum te2 = PG_GETARG_DATUM(3);
2263 bool ts1IsNull = PG_ARGISNULL(0);
2264 bool te1IsNull = PG_ARGISNULL(1);
2265 bool ts2IsNull = PG_ARGISNULL(2);
2266 bool te2IsNull = PG_ARGISNULL(3);
2268 #define TIMETZ_GT(t1,t2) \
2269 DatumGetBool(DirectFunctionCall2(timetz_gt,t1,t2))
2270 #define TIMETZ_LT(t1,t2) \
2271 DatumGetBool(DirectFunctionCall2(timetz_lt,t1,t2))
2274 * If both endpoints of interval 1 are null, the result is null (unknown).
2275 * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2276 * take ts1 as the lesser endpoint.
2282 /* swap null for non-null */
2286 else if (!te1IsNull)
2288 if (TIMETZ_GT(ts1, te1))
2297 /* Likewise for interval 2. */
2302 /* swap null for non-null */
2306 else if (!te2IsNull)
2308 if (TIMETZ_GT(ts2, te2))
2318 * At this point neither ts1 nor ts2 is null, so we can consider three
2319 * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2321 if (TIMETZ_GT(ts1, ts2))
2324 * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2325 * in the presence of nulls it's not quite completely so.
2329 if (TIMETZ_LT(ts1, te2))
2330 PG_RETURN_BOOL(true);
2335 * If te1 is not null then we had ts1 <= te1 above, and we just found
2336 * ts1 >= te2, hence te1 >= te2.
2338 PG_RETURN_BOOL(false);
2340 else if (TIMETZ_LT(ts1, ts2))
2342 /* This case is ts2 < te1 OR te2 < te1 */
2345 if (TIMETZ_LT(ts2, te1))
2346 PG_RETURN_BOOL(true);
2351 * If te2 is not null then we had ts2 <= te2 above, and we just found
2352 * ts2 >= te1, hence te2 >= te1.
2354 PG_RETURN_BOOL(false);
2359 * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2360 * rather silly way of saying "true if both are nonnull, else null".
2362 if (te1IsNull || te2IsNull)
2364 PG_RETURN_BOOL(true);
2373 timetz_time(PG_FUNCTION_ARGS)
2375 TimeTzADT *timetz = PG_GETARG_TIMETZADT_P(0);
2378 /* swallow the time zone and just return the time */
2379 result = timetz->time;
2381 PG_RETURN_TIMEADT(result);
2386 time_timetz(PG_FUNCTION_ARGS)
2388 TimeADT time = PG_GETARG_TIMEADT(0);
2395 GetCurrentDateTime(tm);
2396 time2tm(time, tm, &fsec);
2397 tz = DetermineTimeZoneOffset(tm, session_timezone);
2399 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2401 result->time = time;
2404 PG_RETURN_TIMETZADT_P(result);
2408 /* timestamptz_timetz()
2409 * Convert timestamp to timetz data type.
2412 timestamptz_timetz(PG_FUNCTION_ARGS)
2414 TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
2422 if (TIMESTAMP_NOT_FINITE(timestamp))
2425 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
2427 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2428 errmsg("timestamp out of range")));
2430 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2432 tm2timetz(tm, fsec, tz, result);
2434 PG_RETURN_TIMETZADT_P(result);
2438 /* datetimetz_timestamptz()
2439 * Convert date and timetz to timestamp with time zone data type.
2440 * Timestamp is stored in GMT, so add the time zone
2441 * stored with the timetz to the result.
2442 * - thomas 2000-03-10
2445 datetimetz_timestamptz(PG_FUNCTION_ARGS)
2447 DateADT date = PG_GETARG_DATEADT(0);
2448 TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2451 if (DATE_IS_NOBEGIN(date))
2452 TIMESTAMP_NOBEGIN(result);
2453 else if (DATE_IS_NOEND(date))
2454 TIMESTAMP_NOEND(result);
2457 #ifdef HAVE_INT64_TIMESTAMP
2458 result = date * USECS_PER_DAY + time->time + time->zone * USECS_PER_SEC;
2460 result = date * (double) SECS_PER_DAY + time->time + time->zone;
2464 PG_RETURN_TIMESTAMP(result);
2469 * Extract specified field from time type.
2472 timetz_part(PG_FUNCTION_ARGS)
2474 text *units = PG_GETARG_TEXT_PP(0);
2475 TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2481 lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
2482 VARSIZE_ANY_EXHDR(units),
2485 type = DecodeUnits(0, lowunits, &val);
2486 if (type == UNKNOWN_FIELD)
2487 type = DecodeSpecial(0, lowunits, &val);
2497 timetz2tm(time, tm, &fsec, &tz);
2507 result /= SECS_PER_MINUTE;
2508 FMODULO(result, dummy, (double) SECS_PER_MINUTE);
2513 FMODULO(dummy, result, (double) SECS_PER_HOUR);
2517 #ifdef HAVE_INT64_TIMESTAMP
2518 result = tm->tm_sec * 1000000.0 + fsec;
2520 result = (tm->tm_sec + fsec) * 1000000;
2525 #ifdef HAVE_INT64_TIMESTAMP
2526 result = tm->tm_sec * 1000.0 + fsec / 1000.0;
2528 result = (tm->tm_sec + fsec) * 1000;
2533 #ifdef HAVE_INT64_TIMESTAMP
2534 result = tm->tm_sec + fsec / 1000000.0;
2536 result = tm->tm_sec + fsec;
2541 result = tm->tm_min;
2545 result = tm->tm_hour;
2554 case DTK_MILLENNIUM:
2557 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2558 errmsg("\"time with time zone\" units \"%s\" not recognized",
2563 else if (type == RESERV && val == DTK_EPOCH)
2565 #ifdef HAVE_INT64_TIMESTAMP
2566 result = time->time / 1000000.0 + time->zone;
2568 result = time->time + time->zone;
2574 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2575 errmsg("\"time with time zone\" units \"%s\" not recognized",
2580 PG_RETURN_FLOAT8(result);
2584 * Encode time with time zone type with specified time zone.
2585 * Applies DST rules as of the current date.
2588 timetz_zone(PG_FUNCTION_ARGS)
2590 text *zone = PG_GETARG_TEXT_PP(0);
2591 TimeTzADT *t = PG_GETARG_TIMETZADT_P(1);
2594 char tzname[TZ_STRLEN_MAX + 1];
2601 * Look up the requested timezone. First we look in the date token table
2602 * (to handle cases like "EST"), and if that fails, we look in the
2603 * timezone database (to handle cases like "America/New_York"). (This
2604 * matches the order in which timestamp input checks the cases; it's
2605 * important because the timezone database unwisely uses a few zone names
2606 * that are identical to offset abbreviations.)
2608 text_to_cstring_buffer(zone, tzname, sizeof(tzname));
2609 lowzone = downcase_truncate_identifier(tzname,
2613 type = DecodeSpecial(0, lowzone, &val);
2615 if (type == TZ || type == DTZ)
2616 tz = val * MINS_PER_HOUR;
2619 tzp = pg_tzset(tzname);
2622 /* Get the offset-from-GMT that is valid today for the zone */
2623 pg_time_t now = (pg_time_t) time(NULL);
2626 tm = pg_localtime(&now, tzp);
2627 tz = -tm->tm_gmtoff;
2632 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2633 errmsg("time zone \"%s\" not recognized", tzname)));
2634 tz = 0; /* keep compiler quiet */
2638 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2640 #ifdef HAVE_INT64_TIMESTAMP
2641 result->time = t->time + (t->zone - tz) * USECS_PER_SEC;
2642 while (result->time < INT64CONST(0))
2643 result->time += USECS_PER_DAY;
2644 while (result->time >= USECS_PER_DAY)
2645 result->time -= USECS_PER_DAY;
2647 result->time = t->time + (t->zone - tz);
2648 while (result->time < 0)
2649 result->time += SECS_PER_DAY;
2650 while (result->time >= SECS_PER_DAY)
2651 result->time -= SECS_PER_DAY;
2656 PG_RETURN_TIMETZADT_P(result);
2660 * Encode time with time zone type with specified time interval as time zone.
2663 timetz_izone(PG_FUNCTION_ARGS)
2665 Interval *zone = PG_GETARG_INTERVAL_P(0);
2666 TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2670 if (zone->month != 0)
2672 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2673 errmsg("\"interval\" time zone \"%s\" not valid",
2674 DatumGetCString(DirectFunctionCall1(interval_out,
2675 PointerGetDatum(zone))))));
2677 #ifdef HAVE_INT64_TIMESTAMP
2678 tz = -(zone->time / USECS_PER_SEC);
2683 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2685 #ifdef HAVE_INT64_TIMESTAMP
2686 result->time = time->time + (time->zone - tz) * USECS_PER_SEC;
2687 while (result->time < INT64CONST(0))
2688 result->time += USECS_PER_DAY;
2689 while (result->time >= USECS_PER_DAY)
2690 result->time -= USECS_PER_DAY;
2692 result->time = time->time + (time->zone - tz);
2693 while (result->time < 0)
2694 result->time += SECS_PER_DAY;
2695 while (result->time >= SECS_PER_DAY)
2696 result->time -= SECS_PER_DAY;
2701 PG_RETURN_TIMETZADT_P(result);