1 /*-------------------------------------------------------------------------
4 * Functions for the built-in SQL92 types "timestamp" and "interval".
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.184 2008/01/01 19:45:52 momjian Exp $
13 *-------------------------------------------------------------------------
24 #include "access/hash.h"
25 #include "access/xact.h"
26 #include "catalog/pg_type.h"
27 #include "libpq/pqformat.h"
28 #include "miscadmin.h"
29 #include "parser/scansup.h"
30 #include "utils/array.h"
31 #include "utils/builtins.h"
32 #include "utils/datetime.h"
35 * gcc's -ffast-math switch breaks routines that expect exact results from
36 * expressions like timeval / SECS_PER_HOUR, where timeval is double.
39 #error -ffast-math is known to break this code
43 /* Set at postmaster start */
44 TimestampTz PgStartTime;
47 #ifdef HAVE_INT64_TIMESTAMP
48 static int64 time2t(const int hour, const int min, const int sec, const fsec_t fsec);
50 static double time2t(const int hour, const int min, const int sec, const fsec_t fsec);
52 static int EncodeSpecialTimestamp(Timestamp dt, char *str);
53 static Timestamp dt2local(Timestamp dt, int timezone);
54 static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod);
55 static void AdjustIntervalForTypmod(Interval *interval, int32 typmod);
56 static TimestampTz timestamp2timestamptz(Timestamp timestamp);
59 /* common code for timestamptypmodin and timestamptztypmodin */
61 anytimestamp_typmodin(bool istz, ArrayType *ta)
67 tl = ArrayGetIntegerTypmods(ta, &n);
70 * we're not too tense about good error message here because grammar
71 * shouldn't allow wrong number of modifiers for TIMESTAMP
75 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
76 errmsg("invalid type modifier")));
80 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
81 errmsg("TIMESTAMP(%d)%s precision must not be negative",
82 *tl, (istz ? " WITH TIME ZONE" : ""))));
83 if (*tl > MAX_TIMESTAMP_PRECISION)
86 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
87 errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
88 *tl, (istz ? " WITH TIME ZONE" : ""),
89 MAX_TIMESTAMP_PRECISION)));
90 typmod = MAX_TIMESTAMP_PRECISION;
98 /* common code for timestamptypmodout and timestamptztypmodout */
100 anytimestamp_typmodout(bool istz, int32 typmod)
102 char *res = (char *) palloc(64);
103 const char *tz = istz ? " with time zone" : " without time zone";
106 snprintf(res, 64, "(%d)%s", (int) typmod, tz);
108 snprintf(res, 64, "%s", tz);
114 /*****************************************************************************
115 * USER I/O ROUTINES *
116 *****************************************************************************/
119 * Convert a string to internal form.
122 timestamp_in(PG_FUNCTION_ARGS)
124 char *str = PG_GETARG_CSTRING(0);
127 Oid typelem = PG_GETARG_OID(1);
129 int32 typmod = PG_GETARG_INT32(2);
138 char *field[MAXDATEFIELDS];
139 int ftype[MAXDATEFIELDS];
140 char workbuf[MAXDATELEN + MAXDATEFIELDS];
142 dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
143 field, ftype, MAXDATEFIELDS, &nf);
145 dterr = DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz);
147 DateTimeParseError(dterr, str, "timestamp");
152 if (tm2timestamp(tm, fsec, NULL, &result) != 0)
154 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
155 errmsg("timestamp out of range: \"%s\"", str)));
159 result = SetEpochTimestamp();
163 TIMESTAMP_NOEND(result);
167 TIMESTAMP_NOBEGIN(result);
172 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
173 errmsg("date/time value \"%s\" is no longer supported", str)));
175 TIMESTAMP_NOEND(result);
179 elog(ERROR, "unexpected dtype %d while parsing timestamp \"%s\"",
181 TIMESTAMP_NOEND(result);
184 AdjustTimestampForTypmod(&result, typmod);
186 PG_RETURN_TIMESTAMP(result);
190 * Convert a timestamp to external form.
193 timestamp_out(PG_FUNCTION_ARGS)
195 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
201 char buf[MAXDATELEN + 1];
203 if (TIMESTAMP_NOT_FINITE(timestamp))
204 EncodeSpecialTimestamp(timestamp, buf);
205 else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
206 EncodeDateTime(tm, fsec, NULL, &tzn, DateStyle, buf);
209 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
210 errmsg("timestamp out of range")));
212 result = pstrdup(buf);
213 PG_RETURN_CSTRING(result);
217 * timestamp_recv - converts external binary format to timestamp
219 * We make no attempt to provide compatibility between int and float
220 * timestamp representations ...
223 timestamp_recv(PG_FUNCTION_ARGS)
225 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
228 Oid typelem = PG_GETARG_OID(1);
230 int32 typmod = PG_GETARG_INT32(2);
236 #ifdef HAVE_INT64_TIMESTAMP
237 timestamp = (Timestamp) pq_getmsgint64(buf);
239 timestamp = (Timestamp) pq_getmsgfloat8(buf);
242 /* rangecheck: see if timestamp_out would like it */
243 if (TIMESTAMP_NOT_FINITE(timestamp))
245 else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
247 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
248 errmsg("timestamp out of range")));
250 AdjustTimestampForTypmod(×tamp, typmod);
252 PG_RETURN_TIMESTAMP(timestamp);
256 * timestamp_send - converts timestamp to binary format
259 timestamp_send(PG_FUNCTION_ARGS)
261 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
264 pq_begintypsend(&buf);
265 #ifdef HAVE_INT64_TIMESTAMP
266 pq_sendint64(&buf, timestamp);
268 pq_sendfloat8(&buf, timestamp);
270 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
274 timestamptypmodin(PG_FUNCTION_ARGS)
276 ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
278 PG_RETURN_INT32(anytimestamp_typmodin(false, ta));
282 timestamptypmodout(PG_FUNCTION_ARGS)
284 int32 typmod = PG_GETARG_INT32(0);
286 PG_RETURN_CSTRING(anytimestamp_typmodout(false, typmod));
291 * Adjust time type for specified scale factor.
292 * Used by PostgreSQL type system to stuff columns.
295 timestamp_scale(PG_FUNCTION_ARGS)
297 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
298 int32 typmod = PG_GETARG_INT32(1);
303 AdjustTimestampForTypmod(&result, typmod);
305 PG_RETURN_TIMESTAMP(result);
309 AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
311 #ifdef HAVE_INT64_TIMESTAMP
312 static const int64 TimestampScales[MAX_TIMESTAMP_PRECISION + 1] = {
322 static const int64 TimestampOffsets[MAX_TIMESTAMP_PRECISION + 1] = {
332 static const double TimestampScales[MAX_TIMESTAMP_PRECISION + 1] = {
343 if (!TIMESTAMP_NOT_FINITE(*time)
344 && (typmod != -1) && (typmod != MAX_TIMESTAMP_PRECISION))
346 if (typmod < 0 || typmod > MAX_TIMESTAMP_PRECISION)
348 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
349 errmsg("timestamp(%d) precision must be between %d and %d",
350 typmod, 0, MAX_TIMESTAMP_PRECISION)));
353 * Note: this round-to-nearest code is not completely consistent about
354 * rounding values that are exactly halfway between integral values.
355 * On most platforms, rint() will implement round-to-nearest-even, but
356 * the integer code always rounds up (away from zero). Is it worth
357 * trying to be consistent?
359 #ifdef HAVE_INT64_TIMESTAMP
360 if (*time >= INT64CONST(0))
362 *time = ((*time + TimestampOffsets[typmod]) / TimestampScales[typmod]) *
363 TimestampScales[typmod];
367 *time = -((((-*time) + TimestampOffsets[typmod]) / TimestampScales[typmod])
368 * TimestampScales[typmod]);
371 *time = rint((double) *time * TimestampScales[typmod]) / TimestampScales[typmod];
378 * Convert a string to internal form.
381 timestamptz_in(PG_FUNCTION_ARGS)
383 char *str = PG_GETARG_CSTRING(0);
386 Oid typelem = PG_GETARG_OID(1);
388 int32 typmod = PG_GETARG_INT32(2);
397 char *field[MAXDATEFIELDS];
398 int ftype[MAXDATEFIELDS];
399 char workbuf[MAXDATELEN + MAXDATEFIELDS];
401 dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
402 field, ftype, MAXDATEFIELDS, &nf);
404 dterr = DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz);
406 DateTimeParseError(dterr, str, "timestamp with time zone");
411 if (tm2timestamp(tm, fsec, &tz, &result) != 0)
413 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
414 errmsg("timestamp out of range: \"%s\"", str)));
418 result = SetEpochTimestamp();
422 TIMESTAMP_NOEND(result);
426 TIMESTAMP_NOBEGIN(result);
431 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
432 errmsg("date/time value \"%s\" is no longer supported", str)));
434 TIMESTAMP_NOEND(result);
438 elog(ERROR, "unexpected dtype %d while parsing timestamptz \"%s\"",
440 TIMESTAMP_NOEND(result);
443 AdjustTimestampForTypmod(&result, typmod);
445 PG_RETURN_TIMESTAMPTZ(result);
449 * Convert a timestamp to external form.
452 timestamptz_out(PG_FUNCTION_ARGS)
454 TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0);
461 char buf[MAXDATELEN + 1];
463 if (TIMESTAMP_NOT_FINITE(dt))
464 EncodeSpecialTimestamp(dt, buf);
465 else if (timestamp2tm(dt, &tz, tm, &fsec, &tzn, NULL) == 0)
466 EncodeDateTime(tm, fsec, &tz, &tzn, DateStyle, buf);
469 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
470 errmsg("timestamp out of range")));
472 result = pstrdup(buf);
473 PG_RETURN_CSTRING(result);
477 * timestamptz_recv - converts external binary format to timestamptz
479 * We make no attempt to provide compatibility between int and float
480 * timestamp representations ...
483 timestamptz_recv(PG_FUNCTION_ARGS)
485 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
488 Oid typelem = PG_GETARG_OID(1);
490 int32 typmod = PG_GETARG_INT32(2);
491 TimestampTz timestamp;
498 #ifdef HAVE_INT64_TIMESTAMP
499 timestamp = (TimestampTz) pq_getmsgint64(buf);
501 timestamp = (TimestampTz) pq_getmsgfloat8(buf);
504 /* rangecheck: see if timestamptz_out would like it */
505 if (TIMESTAMP_NOT_FINITE(timestamp))
507 else if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
509 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
510 errmsg("timestamp out of range")));
512 AdjustTimestampForTypmod(×tamp, typmod);
514 PG_RETURN_TIMESTAMPTZ(timestamp);
518 * timestamptz_send - converts timestamptz to binary format
521 timestamptz_send(PG_FUNCTION_ARGS)
523 TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
526 pq_begintypsend(&buf);
527 #ifdef HAVE_INT64_TIMESTAMP
528 pq_sendint64(&buf, timestamp);
530 pq_sendfloat8(&buf, timestamp);
532 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
536 timestamptztypmodin(PG_FUNCTION_ARGS)
538 ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
540 PG_RETURN_INT32(anytimestamp_typmodin(true, ta));
544 timestamptztypmodout(PG_FUNCTION_ARGS)
546 int32 typmod = PG_GETARG_INT32(0);
548 PG_RETURN_CSTRING(anytimestamp_typmodout(true, typmod));
552 /* timestamptz_scale()
553 * Adjust time type for specified scale factor.
554 * Used by PostgreSQL type system to stuff columns.
557 timestamptz_scale(PG_FUNCTION_ARGS)
559 TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
560 int32 typmod = PG_GETARG_INT32(1);
565 AdjustTimestampForTypmod(&result, typmod);
567 PG_RETURN_TIMESTAMPTZ(result);
572 * Convert a string to internal form.
574 * External format(s):
575 * Uses the generic date/time parsing and decoding routines.
578 interval_in(PG_FUNCTION_ARGS)
580 char *str = PG_GETARG_CSTRING(0);
583 Oid typelem = PG_GETARG_OID(1);
585 int32 typmod = PG_GETARG_INT32(2);
593 char *field[MAXDATEFIELDS];
594 int ftype[MAXDATEFIELDS];
605 dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
606 ftype, MAXDATEFIELDS, &nf);
608 dterr = DecodeInterval(field, ftype, nf, &dtype, tm, &fsec);
611 if (dterr == DTERR_FIELD_OVERFLOW)
612 dterr = DTERR_INTERVAL_OVERFLOW;
613 DateTimeParseError(dterr, str, "interval");
616 result = (Interval *) palloc(sizeof(Interval));
621 if (tm2interval(tm, fsec, result) != 0)
623 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
624 errmsg("interval out of range")));
629 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
630 errmsg("date/time value \"%s\" is no longer supported", str)));
634 elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
638 AdjustIntervalForTypmod(result, typmod);
640 PG_RETURN_INTERVAL_P(result);
644 * Convert a time span to external form.
647 interval_out(PG_FUNCTION_ARGS)
649 Interval *span = PG_GETARG_INTERVAL_P(0);
654 char buf[MAXDATELEN + 1];
656 if (interval2tm(*span, tm, &fsec) != 0)
657 elog(ERROR, "could not convert interval to tm");
659 if (EncodeInterval(tm, fsec, DateStyle, buf) != 0)
660 elog(ERROR, "could not format interval");
662 result = pstrdup(buf);
663 PG_RETURN_CSTRING(result);
667 * interval_recv - converts external binary format to interval
670 interval_recv(PG_FUNCTION_ARGS)
672 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
675 Oid typelem = PG_GETARG_OID(1);
677 int32 typmod = PG_GETARG_INT32(2);
680 interval = (Interval *) palloc(sizeof(Interval));
682 #ifdef HAVE_INT64_TIMESTAMP
683 interval->time = pq_getmsgint64(buf);
685 interval->time = pq_getmsgfloat8(buf);
687 interval->day = pq_getmsgint(buf, sizeof(interval->day));
688 interval->month = pq_getmsgint(buf, sizeof(interval->month));
690 AdjustIntervalForTypmod(interval, typmod);
692 PG_RETURN_INTERVAL_P(interval);
696 * interval_send - converts interval to binary format
699 interval_send(PG_FUNCTION_ARGS)
701 Interval *interval = PG_GETARG_INTERVAL_P(0);
704 pq_begintypsend(&buf);
705 #ifdef HAVE_INT64_TIMESTAMP
706 pq_sendint64(&buf, interval->time);
708 pq_sendfloat8(&buf, interval->time);
710 pq_sendint(&buf, interval->day, sizeof(interval->day));
711 pq_sendint(&buf, interval->month, sizeof(interval->month));
712 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
716 intervaltypmodin(PG_FUNCTION_ARGS)
718 ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
723 tl = ArrayGetIntegerTypmods(ta, &n);
726 * tl[0] - opt_interval tl[1] - Iconst (optional)
728 * Note we must validate tl[0] even though it's normally guaranteed
729 * correct by the grammar --- consider SELECT 'foo'::"interval"(1000).
735 case INTERVAL_MASK(YEAR):
736 case INTERVAL_MASK(MONTH):
737 case INTERVAL_MASK(DAY):
738 case INTERVAL_MASK(HOUR):
739 case INTERVAL_MASK(MINUTE):
740 case INTERVAL_MASK(SECOND):
741 case INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH):
742 case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR):
743 case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
744 case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
745 case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
746 case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
747 case INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
748 case INTERVAL_FULL_RANGE:
753 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
754 errmsg("invalid INTERVAL type modifier")));
760 if (tl[0] != INTERVAL_FULL_RANGE)
761 typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, tl[0]);
769 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
770 errmsg("INTERVAL(%d) precision must not be negative",
772 if (tl[1] > MAX_INTERVAL_PRECISION)
775 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
776 errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
777 tl[1], MAX_INTERVAL_PRECISION)));
778 typmod = INTERVAL_TYPMOD(MAX_INTERVAL_PRECISION, tl[0]);
781 typmod = INTERVAL_TYPMOD(tl[1], tl[0]);
786 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
787 errmsg("invalid INTERVAL type modifier")));
788 typmod = 0; /* keep compiler quiet */
791 PG_RETURN_INT32(typmod);
795 intervaltypmodout(PG_FUNCTION_ARGS)
797 int32 typmod = PG_GETARG_INT32(0);
798 char *res = (char *) palloc(64);
801 const char *fieldstr;
806 PG_RETURN_CSTRING(res);
809 fields = INTERVAL_RANGE(typmod);
810 precision = INTERVAL_PRECISION(typmod);
814 case INTERVAL_MASK(YEAR):
817 case INTERVAL_MASK(MONTH):
820 case INTERVAL_MASK(DAY):
823 case INTERVAL_MASK(HOUR):
826 case INTERVAL_MASK(MINUTE):
827 fieldstr = " minute";
829 case INTERVAL_MASK(SECOND):
830 fieldstr = " second";
832 case INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH):
833 fieldstr = " year to month";
835 case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR):
836 fieldstr = " day to hour";
838 case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
839 fieldstr = " day to minute";
841 case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
842 fieldstr = " day to second";
844 case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
845 fieldstr = " hour to minute";
847 case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
848 fieldstr = " hour to second";
850 case INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
851 fieldstr = " minute to second";
853 case INTERVAL_FULL_RANGE:
857 elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
862 if (precision != INTERVAL_FULL_PRECISION)
863 snprintf(res, 64, "(%d)%s", precision, fieldstr);
865 snprintf(res, 64, "%s", fieldstr);
867 PG_RETURN_CSTRING(res);
872 * Adjust interval type for specified fields.
873 * Used by PostgreSQL type system to stuff columns.
876 interval_scale(PG_FUNCTION_ARGS)
878 Interval *interval = PG_GETARG_INTERVAL_P(0);
879 int32 typmod = PG_GETARG_INT32(1);
882 result = palloc(sizeof(Interval));
885 AdjustIntervalForTypmod(result, typmod);
887 PG_RETURN_INTERVAL_P(result);
891 * Adjust interval for specified precision, in both YEAR to SECOND
892 * range and sub-second precision.
895 AdjustIntervalForTypmod(Interval *interval, int32 typmod)
897 #ifdef HAVE_INT64_TIMESTAMP
898 static const int64 IntervalScales[MAX_INTERVAL_PRECISION + 1] = {
908 static const int64 IntervalOffsets[MAX_INTERVAL_PRECISION + 1] = {
918 static const double IntervalScales[MAX_INTERVAL_PRECISION + 1] = {
930 * Unspecified range and precision? Then not necessary to adjust. Setting
931 * typmod to -1 is the convention for all types.
935 int range = INTERVAL_RANGE(typmod);
936 int precision = INTERVAL_PRECISION(typmod);
938 if (range == INTERVAL_FULL_RANGE)
942 else if (range == INTERVAL_MASK(YEAR))
944 interval->month = (interval->month / MONTHS_PER_YEAR) * MONTHS_PER_YEAR;
948 else if (range == INTERVAL_MASK(MONTH))
950 interval->month %= MONTHS_PER_YEAR;
955 else if (range == (INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH)))
957 /* month is already year to month */
961 else if (range == INTERVAL_MASK(DAY))
966 else if (range == INTERVAL_MASK(HOUR))
971 #ifdef HAVE_INT64_TIMESTAMP
972 interval->time = (interval->time / USECS_PER_HOUR) *
975 interval->time = ((int) (interval->time / SECS_PER_HOUR)) * (double) SECS_PER_HOUR;
978 else if (range == INTERVAL_MASK(MINUTE))
980 #ifdef HAVE_INT64_TIMESTAMP
989 #ifdef HAVE_INT64_TIMESTAMP
990 hour = interval->time / USECS_PER_HOUR;
991 interval->time -= hour * USECS_PER_HOUR;
992 interval->time = (interval->time / USECS_PER_MINUTE) *
995 TMODULO(interval->time, hour, (double) SECS_PER_HOUR);
996 interval->time = ((int) (interval->time / SECS_PER_MINUTE)) * (double) SECS_PER_MINUTE;
999 else if (range == INTERVAL_MASK(SECOND))
1001 #ifdef HAVE_INT64_TIMESTAMP
1007 interval->month = 0;
1010 #ifdef HAVE_INT64_TIMESTAMP
1011 minute = interval->time / USECS_PER_MINUTE;
1012 interval->time -= minute * USECS_PER_MINUTE;
1014 TMODULO(interval->time, minute, (double) SECS_PER_MINUTE);
1015 /* return subseconds too */
1019 else if (range == (INTERVAL_MASK(DAY) |
1020 INTERVAL_MASK(HOUR)))
1022 interval->month = 0;
1024 #ifdef HAVE_INT64_TIMESTAMP
1025 interval->time = (interval->time / USECS_PER_HOUR) *
1028 interval->time = ((int) (interval->time / SECS_PER_HOUR)) * (double) SECS_PER_HOUR;
1032 else if (range == (INTERVAL_MASK(DAY) |
1033 INTERVAL_MASK(HOUR) |
1034 INTERVAL_MASK(MINUTE)))
1036 interval->month = 0;
1038 #ifdef HAVE_INT64_TIMESTAMP
1039 interval->time = (interval->time / USECS_PER_MINUTE) *
1042 interval->time = ((int) (interval->time / SECS_PER_MINUTE)) * (double) SECS_PER_MINUTE;
1046 else if (range == (INTERVAL_MASK(DAY) |
1047 INTERVAL_MASK(HOUR) |
1048 INTERVAL_MASK(MINUTE) |
1049 INTERVAL_MASK(SECOND)))
1050 interval->month = 0;
1052 /* HOUR TO MINUTE */
1053 else if (range == (INTERVAL_MASK(HOUR) |
1054 INTERVAL_MASK(MINUTE)))
1056 interval->month = 0;
1059 #ifdef HAVE_INT64_TIMESTAMP
1060 interval->time = (interval->time / USECS_PER_MINUTE) *
1063 interval->time = ((int) (interval->time / SECS_PER_MINUTE)) * (double) SECS_PER_MINUTE;
1066 /* HOUR TO SECOND */
1067 else if (range == (INTERVAL_MASK(HOUR) |
1068 INTERVAL_MASK(MINUTE) |
1069 INTERVAL_MASK(SECOND)))
1071 interval->month = 0;
1073 /* return subseconds too */
1075 /* MINUTE TO SECOND */
1076 else if (range == (INTERVAL_MASK(MINUTE) |
1077 INTERVAL_MASK(SECOND)))
1079 #ifdef HAVE_INT64_TIMESTAMP
1085 interval->month = 0;
1088 #ifdef HAVE_INT64_TIMESTAMP
1089 hour = interval->time / USECS_PER_HOUR;
1090 interval->time -= hour * USECS_PER_HOUR;
1092 TMODULO(interval->time, hour, (double) SECS_PER_HOUR);
1096 elog(ERROR, "unrecognized interval typmod: %d", typmod);
1098 /* Need to adjust subsecond precision? */
1099 if (precision != INTERVAL_FULL_PRECISION)
1101 if (precision < 0 || precision > MAX_INTERVAL_PRECISION)
1103 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1104 errmsg("interval(%d) precision must be between %d and %d",
1105 precision, 0, MAX_INTERVAL_PRECISION)));
1108 * Note: this round-to-nearest code is not completely consistent
1109 * about rounding values that are exactly halfway between integral
1110 * values. On most platforms, rint() will implement
1111 * round-to-nearest-even, but the integer code always rounds up
1112 * (away from zero). Is it worth trying to be consistent?
1114 #ifdef HAVE_INT64_TIMESTAMP
1115 if (interval->time >= INT64CONST(0))
1117 interval->time = ((interval->time +
1118 IntervalOffsets[precision]) /
1119 IntervalScales[precision]) *
1120 IntervalScales[precision];
1124 interval->time = -(((-interval->time +
1125 IntervalOffsets[precision]) /
1126 IntervalScales[precision]) *
1127 IntervalScales[precision]);
1130 interval->time = rint(((double) interval->time) *
1131 IntervalScales[precision]) /
1132 IntervalScales[precision];
1141 /* EncodeSpecialTimestamp()
1142 * Convert reserved timestamp data type to string.
1145 EncodeSpecialTimestamp(Timestamp dt, char *str)
1147 if (TIMESTAMP_IS_NOBEGIN(dt))
1149 else if (TIMESTAMP_IS_NOEND(dt))
1155 } /* EncodeSpecialTimestamp() */
1158 now(PG_FUNCTION_ARGS)
1160 PG_RETURN_TIMESTAMPTZ(GetCurrentTransactionStartTimestamp());
1164 statement_timestamp(PG_FUNCTION_ARGS)
1166 PG_RETURN_TIMESTAMPTZ(GetCurrentStatementStartTimestamp());
1170 clock_timestamp(PG_FUNCTION_ARGS)
1172 PG_RETURN_TIMESTAMPTZ(GetCurrentTimestamp());
1176 pgsql_postmaster_start_time(PG_FUNCTION_ARGS)
1178 PG_RETURN_TIMESTAMPTZ(PgStartTime);
1182 * GetCurrentTimestamp -- get the current operating system time
1184 * Result is in the form of a TimestampTz value, and is expressed to the
1185 * full precision of the gettimeofday() syscall
1188 GetCurrentTimestamp(void)
1193 gettimeofday(&tp, NULL);
1195 result = (TimestampTz) tp.tv_sec -
1196 ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
1198 #ifdef HAVE_INT64_TIMESTAMP
1199 result = (result * USECS_PER_SEC) + tp.tv_usec;
1201 result = result + (tp.tv_usec / 1000000.0);
1208 * TimestampDifference -- convert the difference between two timestamps
1209 * into integer seconds and microseconds
1211 * Both inputs must be ordinary finite timestamps (in current usage,
1212 * they'll be results from GetCurrentTimestamp()).
1214 * We expect start_time <= stop_time. If not, we return zeroes; for current
1215 * callers there is no need to be tense about which way division rounds on
1219 TimestampDifference(TimestampTz start_time, TimestampTz stop_time,
1220 long *secs, int *microsecs)
1222 TimestampTz diff = stop_time - start_time;
1231 #ifdef HAVE_INT64_TIMESTAMP
1232 *secs = (long) (diff / USECS_PER_SEC);
1233 *microsecs = (int) (diff % USECS_PER_SEC);
1235 *secs = (long) diff;
1236 *microsecs = (int) ((diff - *secs) * 1000000.0);
1242 * TimestampDifferenceExceeds -- report whether the difference between two
1243 * timestamps is >= a threshold (expressed in milliseconds)
1245 * Both inputs must be ordinary finite timestamps (in current usage,
1246 * they'll be results from GetCurrentTimestamp()).
1249 TimestampDifferenceExceeds(TimestampTz start_time,
1250 TimestampTz stop_time,
1253 TimestampTz diff = stop_time - start_time;
1255 #ifdef HAVE_INT64_TIMESTAMP
1256 return (diff >= msec * INT64CONST(1000));
1258 return (diff * 1000.0 >= msec);
1263 * Convert a time_t to TimestampTz.
1265 * We do not use time_t internally in Postgres, but this is provided for use
1266 * by functions that need to interpret, say, a stat(2) result.
1269 time_t_to_timestamptz(time_t tm)
1273 result = (TimestampTz) tm -
1274 ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
1276 #ifdef HAVE_INT64_TIMESTAMP
1277 result *= USECS_PER_SEC;
1284 * Convert a TimestampTz to time_t.
1286 * This too is just marginally useful, but some places need it.
1289 timestamptz_to_time_t(TimestampTz t)
1293 #ifdef HAVE_INT64_TIMESTAMP
1294 result = (time_t) (t / USECS_PER_SEC +
1295 ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY));
1297 result = (time_t) (t +
1298 ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY));
1305 * Produce a C-string representation of a TimestampTz.
1307 * This is mostly for use in emitting messages. The primary difference
1308 * from timestamptz_out is that we force the output format to ISO. Note
1309 * also that the result is in a static buffer, not pstrdup'd.
1312 timestamptz_to_str(TimestampTz t)
1314 static char buf[MAXDATELEN + 1];
1321 if (TIMESTAMP_NOT_FINITE(t))
1322 EncodeSpecialTimestamp(t, buf);
1323 else if (timestamp2tm(t, &tz, tm, &fsec, &tzn, NULL) == 0)
1324 EncodeDateTime(tm, fsec, &tz, &tzn, USE_ISO_DATES, buf);
1326 strlcpy(buf, "(timestamp out of range)", sizeof(buf));
1333 dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
1335 #ifdef HAVE_INT64_TIMESTAMP
1343 #ifdef HAVE_INT64_TIMESTAMP
1344 *hour = time / USECS_PER_HOUR;
1345 time -= (*hour) * USECS_PER_HOUR;
1346 *min = time / USECS_PER_MINUTE;
1347 time -= (*min) * USECS_PER_MINUTE;
1348 *sec = time / USECS_PER_SEC;
1349 *fsec = time - (*sec * USECS_PER_SEC);
1351 *hour = time / SECS_PER_HOUR;
1352 time -= (*hour) * SECS_PER_HOUR;
1353 *min = time / SECS_PER_MINUTE;
1354 time -= (*min) * SECS_PER_MINUTE;
1356 *fsec = time - *sec;
1362 * timestamp2tm() - Convert timestamp data type to POSIX time structure.
1364 * Note that year is _not_ 1900-based, but is an explicit full value.
1365 * Also, month is one-based, _not_ zero-based.
1368 * -1 on out of range
1370 * If attimezone is NULL, the global timezone (including possibly brute forced
1371 * timezone) will be used.
1374 timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm, fsec_t *fsec, char **tzn, pg_tz *attimezone)
1381 * If HasCTZSet is true then we have a brute force time zone specified. Go
1382 * ahead and rotate to the local time zone since we will later bypass any
1383 * calls which adjust the tm fields.
1385 if (attimezone == NULL && HasCTZSet && tzp != NULL)
1387 #ifdef HAVE_INT64_TIMESTAMP
1388 dt -= CTimeZone * USECS_PER_SEC;
1394 #ifdef HAVE_INT64_TIMESTAMP
1396 TMODULO(time, date, USECS_PER_DAY);
1398 if (time < INT64CONST(0))
1400 time += USECS_PER_DAY;
1404 /* add offset to go from J2000 back to standard Julian date */
1405 date += POSTGRES_EPOCH_JDATE;
1407 /* Julian day routine does not work for negative Julian days */
1408 if (date < 0 || date > (Timestamp) INT_MAX)
1411 j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1412 dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1415 TMODULO(time, date, (double) SECS_PER_DAY);
1419 time += SECS_PER_DAY;
1423 /* add offset to go from J2000 back to standard Julian date */
1424 date += POSTGRES_EPOCH_JDATE;
1427 /* Julian day routine does not work for negative Julian days */
1428 if (date < 0 || date > (Timestamp) INT_MAX)
1431 j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1433 dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1435 *fsec = TSROUND(*fsec);
1436 /* roundoff may need to propagate to higher-order fields */
1440 if (time >= (double) SECS_PER_DAY)
1450 /* Done if no TZ conversion wanted */
1462 * We have a brute force time zone per SQL99? Then use it without change
1463 * since we have already rotated to the time zone.
1465 if (attimezone == NULL && HasCTZSet)
1469 tm->tm_gmtoff = CTimeZone;
1477 * If the time falls within the range of pg_time_t, use pg_localtime() to
1478 * rotate to the local time zone.
1480 * First, convert to an integral timestamp, avoiding possibly
1481 * platform-specific roundoff-in-wrong-direction errors, and adjust to
1482 * Unix epoch. Then see if we can convert to pg_time_t without loss. This
1483 * coding avoids hardwiring any assumptions about the width of pg_time_t,
1484 * so it should behave sanely on machines without int64.
1486 #ifdef HAVE_INT64_TIMESTAMP
1487 dt = (dt - *fsec) / USECS_PER_SEC +
1488 (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY;
1490 dt = rint(dt - *fsec +
1491 (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
1493 utime = (pg_time_t) dt;
1494 if ((Timestamp) utime == dt)
1496 struct pg_tm *tx = pg_localtime(&utime,
1497 attimezone ? attimezone : session_timezone);
1499 tm->tm_year = tx->tm_year + 1900;
1500 tm->tm_mon = tx->tm_mon + 1;
1501 tm->tm_mday = tx->tm_mday;
1502 tm->tm_hour = tx->tm_hour;
1503 tm->tm_min = tx->tm_min;
1504 tm->tm_sec = tx->tm_sec;
1505 tm->tm_isdst = tx->tm_isdst;
1506 tm->tm_gmtoff = tx->tm_gmtoff;
1507 tm->tm_zone = tx->tm_zone;
1508 *tzp = -tm->tm_gmtoff;
1510 *tzn = (char *) tm->tm_zone;
1515 * When out of range of pg_time_t, treat as GMT
1518 /* Mark this as *no* time zone available */
1531 * Convert a tm structure to a timestamp data type.
1532 * Note that year is _not_ 1900-based, but is an explicit full value.
1533 * Also, month is one-based, _not_ zero-based.
1535 * Returns -1 on failure (value out of range).
1538 tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *result)
1540 #ifdef HAVE_INT64_TIMESTAMP
1548 /* Julian day routines are not correct for negative Julian days */
1549 if (!IS_VALID_JULIAN(tm->tm_year, tm->tm_mon, tm->tm_mday))
1551 *result = 0; /* keep compiler quiet */
1555 date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
1556 time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
1558 #ifdef HAVE_INT64_TIMESTAMP
1559 *result = date * USECS_PER_DAY + time;
1560 /* check for major overflow */
1561 if ((*result - time) / USECS_PER_DAY != date)
1563 *result = 0; /* keep compiler quiet */
1566 /* check for just-barely overflow (okay except time-of-day wraps) */
1567 if ((*result < 0 && date >= 0) ||
1568 (*result >= 0 && date < 0))
1570 *result = 0; /* keep compiler quiet */
1574 *result = date * SECS_PER_DAY + time;
1577 *result = dt2local(*result, -(*tzp));
1584 * Convert a interval data type to a tm structure.
1587 interval2tm(Interval span, struct pg_tm * tm, fsec_t *fsec)
1589 #ifdef HAVE_INT64_TIMESTAMP
1597 tm->tm_year = span.month / MONTHS_PER_YEAR;
1598 tm->tm_mon = span.month % MONTHS_PER_YEAR;
1599 tm->tm_mday = span.day;
1602 #ifdef HAVE_INT64_TIMESTAMP
1603 tfrac = time / USECS_PER_HOUR;
1604 time -= tfrac * USECS_PER_HOUR;
1605 tm->tm_hour = tfrac; /* could overflow ... */
1606 tfrac = time / USECS_PER_MINUTE;
1607 time -= tfrac * USECS_PER_MINUTE;
1609 tfrac = time / USECS_PER_SEC;
1610 *fsec = time - (tfrac * USECS_PER_SEC);
1614 TMODULO(time, tfrac, (double) SECS_PER_HOUR);
1615 tm->tm_hour = tfrac; /* could overflow ... */
1616 TMODULO(time, tfrac, (double) SECS_PER_MINUTE);
1618 TMODULO(time, tfrac, 1.0);
1620 time = TSROUND(time);
1621 /* roundoff may need to propagate to higher-order fields */
1624 time = ceil(span.time);
1634 tm2interval(struct pg_tm * tm, fsec_t fsec, Interval *span)
1636 span->month = tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon;
1637 span->day = tm->tm_mday;
1638 #ifdef HAVE_INT64_TIMESTAMP
1639 span->time = (((((tm->tm_hour * INT64CONST(60)) +
1640 tm->tm_min) * INT64CONST(60)) +
1641 tm->tm_sec) * USECS_PER_SEC) + fsec;
1643 span->time = (((tm->tm_hour * (double) MINS_PER_HOUR) +
1644 tm->tm_min) * (double) SECS_PER_MINUTE) +
1651 #ifdef HAVE_INT64_TIMESTAMP
1653 time2t(const int hour, const int min, const int sec, const fsec_t fsec)
1655 return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
1659 time2t(const int hour, const int min, const int sec, const fsec_t fsec)
1661 return (((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec + fsec;
1666 dt2local(Timestamp dt, int tz)
1668 #ifdef HAVE_INT64_TIMESTAMP
1669 dt -= (tz * USECS_PER_SEC);
1677 /*****************************************************************************
1679 *****************************************************************************/
1683 timestamp_finite(PG_FUNCTION_ARGS)
1685 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
1687 PG_RETURN_BOOL(!TIMESTAMP_NOT_FINITE(timestamp));
1691 interval_finite(PG_FUNCTION_ARGS)
1693 PG_RETURN_BOOL(true);
1697 /*----------------------------------------------------------
1698 * Relational operators for timestamp.
1699 *---------------------------------------------------------*/
1702 GetEpochTime(struct pg_tm * tm)
1705 pg_time_t epoch = 0;
1707 t0 = pg_gmtime(&epoch);
1709 tm->tm_year = t0->tm_year;
1710 tm->tm_mon = t0->tm_mon;
1711 tm->tm_mday = t0->tm_mday;
1712 tm->tm_hour = t0->tm_hour;
1713 tm->tm_min = t0->tm_min;
1714 tm->tm_sec = t0->tm_sec;
1716 tm->tm_year += 1900;
1721 SetEpochTimestamp(void)
1728 /* we don't bother to test for failure ... */
1729 tm2timestamp(tm, 0, NULL, &dt);
1732 } /* SetEpochTimestamp() */
1735 * We are currently sharing some code between timestamp and timestamptz.
1736 * The comparison functions are among them. - thomas 2001-09-25
1738 * timestamp_relop - is timestamp1 relop timestamp2
1740 * collate invalid timestamp at the end
1743 timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
1745 #ifdef HAVE_INT64_TIMESTAMP
1746 return (dt1 < dt2) ? -1 : ((dt1 > dt2) ? 1 : 0);
1750 * When using float representation, we have to be wary of NaNs.
1752 * We consider all NANs to be equal and larger than any non-NAN. This is
1753 * somewhat arbitrary; the important thing is to have a consistent sort
1759 return 0; /* NAN = NAN */
1761 return 1; /* NAN > non-NAN */
1763 else if (isnan(dt2))
1765 return -1; /* non-NAN < NAN */
1780 timestamp_eq(PG_FUNCTION_ARGS)
1782 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
1783 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
1785 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
1789 timestamp_ne(PG_FUNCTION_ARGS)
1791 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
1792 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
1794 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
1798 timestamp_lt(PG_FUNCTION_ARGS)
1800 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
1801 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
1803 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
1807 timestamp_gt(PG_FUNCTION_ARGS)
1809 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
1810 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
1812 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
1816 timestamp_le(PG_FUNCTION_ARGS)
1818 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
1819 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
1821 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
1825 timestamp_ge(PG_FUNCTION_ARGS)
1827 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
1828 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
1830 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
1834 timestamp_cmp(PG_FUNCTION_ARGS)
1836 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
1837 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
1839 PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2));
1843 timestamp_hash(PG_FUNCTION_ARGS)
1845 /* We can use either hashint8 or hashfloat8 directly */
1846 #ifdef HAVE_INT64_TIMESTAMP
1847 return hashint8(fcinfo);
1849 return hashfloat8(fcinfo);
1855 * Crosstype comparison functions for timestamp vs timestamptz
1859 timestamp_eq_timestamptz(PG_FUNCTION_ARGS)
1861 Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
1862 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
1865 dt1 = timestamp2timestamptz(timestampVal);
1867 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
1871 timestamp_ne_timestamptz(PG_FUNCTION_ARGS)
1873 Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
1874 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
1877 dt1 = timestamp2timestamptz(timestampVal);
1879 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
1883 timestamp_lt_timestamptz(PG_FUNCTION_ARGS)
1885 Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
1886 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
1889 dt1 = timestamp2timestamptz(timestampVal);
1891 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
1895 timestamp_gt_timestamptz(PG_FUNCTION_ARGS)
1897 Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
1898 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
1901 dt1 = timestamp2timestamptz(timestampVal);
1903 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
1907 timestamp_le_timestamptz(PG_FUNCTION_ARGS)
1909 Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
1910 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
1913 dt1 = timestamp2timestamptz(timestampVal);
1915 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
1919 timestamp_ge_timestamptz(PG_FUNCTION_ARGS)
1921 Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
1922 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
1925 dt1 = timestamp2timestamptz(timestampVal);
1927 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
1931 timestamp_cmp_timestamptz(PG_FUNCTION_ARGS)
1933 Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
1934 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
1937 dt1 = timestamp2timestamptz(timestampVal);
1939 PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2));
1943 timestamptz_eq_timestamp(PG_FUNCTION_ARGS)
1945 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
1946 Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
1949 dt2 = timestamp2timestamptz(timestampVal);
1951 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
1955 timestamptz_ne_timestamp(PG_FUNCTION_ARGS)
1957 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
1958 Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
1961 dt2 = timestamp2timestamptz(timestampVal);
1963 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
1967 timestamptz_lt_timestamp(PG_FUNCTION_ARGS)
1969 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
1970 Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
1973 dt2 = timestamp2timestamptz(timestampVal);
1975 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
1979 timestamptz_gt_timestamp(PG_FUNCTION_ARGS)
1981 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
1982 Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
1985 dt2 = timestamp2timestamptz(timestampVal);
1987 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
1991 timestamptz_le_timestamp(PG_FUNCTION_ARGS)
1993 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
1994 Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
1997 dt2 = timestamp2timestamptz(timestampVal);
1999 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
2003 timestamptz_ge_timestamp(PG_FUNCTION_ARGS)
2005 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
2006 Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
2009 dt2 = timestamp2timestamptz(timestampVal);
2011 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
2015 timestamptz_cmp_timestamp(PG_FUNCTION_ARGS)
2017 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
2018 Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
2021 dt2 = timestamp2timestamptz(timestampVal);
2023 PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2));
2028 * interval_relop - is interval1 relop interval2
2030 * collate invalid interval at the end
2033 interval_cmp_internal(Interval *interval1, Interval *interval2)
2035 #ifdef HAVE_INT64_TIMESTAMP
2043 span1 = interval1->time;
2044 span2 = interval2->time;
2046 #ifdef HAVE_INT64_TIMESTAMP
2047 span1 += interval1->month * INT64CONST(30) * USECS_PER_DAY;
2048 span1 += interval1->day * INT64CONST(24) * USECS_PER_HOUR;
2049 span2 += interval2->month * INT64CONST(30) * USECS_PER_DAY;
2050 span2 += interval2->day * INT64CONST(24) * USECS_PER_HOUR;
2052 span1 += interval1->month * ((double) DAYS_PER_MONTH * SECS_PER_DAY);
2053 span1 += interval1->day * ((double) HOURS_PER_DAY * SECS_PER_HOUR);
2054 span2 += interval2->month * ((double) DAYS_PER_MONTH * SECS_PER_DAY);
2055 span2 += interval2->day * ((double) HOURS_PER_DAY * SECS_PER_HOUR);
2058 return ((span1 < span2) ? -1 : (span1 > span2) ? 1 : 0);
2062 interval_eq(PG_FUNCTION_ARGS)
2064 Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2065 Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2067 PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
2071 interval_ne(PG_FUNCTION_ARGS)
2073 Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2074 Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2076 PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2080 interval_lt(PG_FUNCTION_ARGS)
2082 Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2083 Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2085 PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2089 interval_gt(PG_FUNCTION_ARGS)
2091 Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2092 Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2094 PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
2098 interval_le(PG_FUNCTION_ARGS)
2100 Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2101 Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2103 PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) <= 0);
2107 interval_ge(PG_FUNCTION_ARGS)
2109 Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2110 Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2112 PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
2116 interval_cmp(PG_FUNCTION_ARGS)
2118 Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2119 Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2121 PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2125 interval_hash(PG_FUNCTION_ARGS)
2127 Interval *key = PG_GETARG_INTERVAL_P(0);
2132 * To avoid any problems with padding bytes in the struct, we figure the
2133 * field hashes separately and XOR them. This also provides a convenient
2134 * framework for dealing with the fact that the time field might be either
2137 #ifdef HAVE_INT64_TIMESTAMP
2138 thash = DatumGetUInt32(DirectFunctionCall1(hashint8,
2139 Int64GetDatumFast(key->time)));
2141 thash = DatumGetUInt32(DirectFunctionCall1(hashfloat8,
2142 Float8GetDatumFast(key->time)));
2144 thash ^= DatumGetUInt32(hash_uint32(key->day));
2145 /* Shift so "k days" and "k months" don't hash to the same thing */
2146 mhash = DatumGetUInt32(hash_uint32(key->month));
2147 thash ^= mhash << 24;
2148 thash ^= mhash >> 8;
2149 PG_RETURN_UINT32(thash);
2152 /* overlaps_timestamp() --- implements the SQL92 OVERLAPS operator.
2154 * Algorithm is per SQL92 spec. This is much harder than you'd think
2155 * because the spec requires us to deliver a non-null answer in some cases
2156 * where some of the inputs are null.
2159 overlaps_timestamp(PG_FUNCTION_ARGS)
2162 * The arguments are Timestamps, but we leave them as generic Datums to
2163 * avoid unnecessary conversions between value and reference forms --- not
2164 * to mention possible dereferences of null pointers.
2166 Datum ts1 = PG_GETARG_DATUM(0);
2167 Datum te1 = PG_GETARG_DATUM(1);
2168 Datum ts2 = PG_GETARG_DATUM(2);
2169 Datum te2 = PG_GETARG_DATUM(3);
2170 bool ts1IsNull = PG_ARGISNULL(0);
2171 bool te1IsNull = PG_ARGISNULL(1);
2172 bool ts2IsNull = PG_ARGISNULL(2);
2173 bool te2IsNull = PG_ARGISNULL(3);
2175 #define TIMESTAMP_GT(t1,t2) \
2176 DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
2177 #define TIMESTAMP_LT(t1,t2) \
2178 DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
2181 * If both endpoints of interval 1 are null, the result is null (unknown).
2182 * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2183 * take ts1 as the lesser endpoint.
2189 /* swap null for non-null */
2193 else if (!te1IsNull)
2195 if (TIMESTAMP_GT(ts1, te1))
2204 /* Likewise for interval 2. */
2209 /* swap null for non-null */
2213 else if (!te2IsNull)
2215 if (TIMESTAMP_GT(ts2, te2))
2225 * At this point neither ts1 nor ts2 is null, so we can consider three
2226 * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2228 if (TIMESTAMP_GT(ts1, ts2))
2231 * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2232 * in the presence of nulls it's not quite completely so.
2236 if (TIMESTAMP_LT(ts1, te2))
2237 PG_RETURN_BOOL(true);
2242 * If te1 is not null then we had ts1 <= te1 above, and we just found
2243 * ts1 >= te2, hence te1 >= te2.
2245 PG_RETURN_BOOL(false);
2247 else if (TIMESTAMP_LT(ts1, ts2))
2249 /* This case is ts2 < te1 OR te2 < te1 */
2252 if (TIMESTAMP_LT(ts2, te1))
2253 PG_RETURN_BOOL(true);
2258 * If te2 is not null then we had ts2 <= te2 above, and we just found
2259 * ts2 >= te1, hence te2 >= te1.
2261 PG_RETURN_BOOL(false);
2266 * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2267 * rather silly way of saying "true if both are nonnull, else null".
2269 if (te1IsNull || te2IsNull)
2271 PG_RETURN_BOOL(true);
2279 /*----------------------------------------------------------
2280 * "Arithmetic" operators on date/times.
2281 *---------------------------------------------------------*/
2284 timestamp_smaller(PG_FUNCTION_ARGS)
2286 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2287 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2290 /* use timestamp_cmp_internal to be sure this agrees with comparisons */
2291 if (timestamp_cmp_internal(dt1, dt2) < 0)
2295 PG_RETURN_TIMESTAMP(result);
2299 timestamp_larger(PG_FUNCTION_ARGS)
2301 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2302 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2305 if (timestamp_cmp_internal(dt1, dt2) > 0)
2309 PG_RETURN_TIMESTAMP(result);
2314 timestamp_mi(PG_FUNCTION_ARGS)
2316 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2317 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2320 result = (Interval *) palloc(sizeof(Interval));
2322 if (TIMESTAMP_NOT_FINITE(dt1) || TIMESTAMP_NOT_FINITE(dt2))
2324 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2325 errmsg("cannot subtract infinite timestamps")));
2327 result->time = dt1 - dt2;
2333 * This is wrong, but removing it breaks a lot of regression tests.
2336 * test=> SET timezone = 'EST5EDT';
2338 * test-> ('2005-10-30 13:22:00-05'::timestamptz -
2339 * test(> '2005-10-29 13:22:00-04'::timestamptz);
2345 * so adding that to the first timestamp gets:
2348 * test-> ('2005-10-29 13:22:00-04'::timestamptz +
2349 * test(> ('2005-10-30 13:22:00-05'::timestamptz -
2350 * test(> '2005-10-29 13:22:00-04'::timestamptz)) at time zone 'EST';
2352 * --------------------
2353 * 2005-10-30 14:22:00
2357 result = DatumGetIntervalP(DirectFunctionCall1(interval_justify_hours,
2358 IntervalPGetDatum(result)));
2360 PG_RETURN_INTERVAL_P(result);
2364 * interval_justify_interval()
2366 * Adjust interval so 'month', 'day', and 'time' portions are within
2367 * customary bounds. Specifically:
2369 * 0 <= abs(time) < 24 hours
2370 * 0 <= abs(day) < 30 days
2372 * Also, the sign bit on all three fields is made equal, so either
2373 * all three fields are negative or all are positive.
2376 interval_justify_interval(PG_FUNCTION_ARGS)
2378 Interval *span = PG_GETARG_INTERVAL_P(0);
2381 #ifdef HAVE_INT64_TIMESTAMP
2388 result = (Interval *) palloc(sizeof(Interval));
2389 result->month = span->month;
2390 result->day = span->day;
2391 result->time = span->time;
2393 #ifdef HAVE_INT64_TIMESTAMP
2394 TMODULO(result->time, wholeday, USECS_PER_DAY);
2396 TMODULO(result->time, wholeday, (double) SECS_PER_DAY);
2398 result->day += wholeday; /* could overflow... */
2400 wholemonth = result->day / DAYS_PER_MONTH;
2401 result->day -= wholemonth * DAYS_PER_MONTH;
2402 result->month += wholemonth;
2404 if (result->month > 0 &&
2405 (result->day < 0 || (result->day == 0 && result->time < 0)))
2407 result->day += DAYS_PER_MONTH;
2410 else if (result->month < 0 &&
2411 (result->day > 0 || (result->day == 0 && result->time > 0)))
2413 result->day -= DAYS_PER_MONTH;
2417 if (result->day > 0 && result->time < 0)
2419 #ifdef HAVE_INT64_TIMESTAMP
2420 result->time += USECS_PER_DAY;
2422 result->time += (double) SECS_PER_DAY;
2426 else if (result->day < 0 && result->time > 0)
2428 #ifdef HAVE_INT64_TIMESTAMP
2429 result->time -= USECS_PER_DAY;
2431 result->time -= (double) SECS_PER_DAY;
2436 PG_RETURN_INTERVAL_P(result);
2440 * interval_justify_hours()
2442 * Adjust interval so 'time' contains less than a whole day, adding
2443 * the excess to 'day'. This is useful for
2444 * situations (such as non-TZ) where '1 day' = '24 hours' is valid,
2445 * e.g. interval subtraction and division.
2448 interval_justify_hours(PG_FUNCTION_ARGS)
2450 Interval *span = PG_GETARG_INTERVAL_P(0);
2453 #ifdef HAVE_INT64_TIMESTAMP
2459 result = (Interval *) palloc(sizeof(Interval));
2460 result->month = span->month;
2461 result->day = span->day;
2462 result->time = span->time;
2464 #ifdef HAVE_INT64_TIMESTAMP
2465 TMODULO(result->time, wholeday, USECS_PER_DAY);
2467 TMODULO(result->time, wholeday, (double) SECS_PER_DAY);
2469 result->day += wholeday; /* could overflow... */
2471 if (result->day > 0 && result->time < 0)
2473 #ifdef HAVE_INT64_TIMESTAMP
2474 result->time += USECS_PER_DAY;
2476 result->time += (double) SECS_PER_DAY;
2480 else if (result->day < 0 && result->time > 0)
2482 #ifdef HAVE_INT64_TIMESTAMP
2483 result->time -= USECS_PER_DAY;
2485 result->time -= (double) SECS_PER_DAY;
2490 PG_RETURN_INTERVAL_P(result);
2494 * interval_justify_days()
2496 * Adjust interval so 'day' contains less than 30 days, adding
2497 * the excess to 'month'.
2500 interval_justify_days(PG_FUNCTION_ARGS)
2502 Interval *span = PG_GETARG_INTERVAL_P(0);
2506 result = (Interval *) palloc(sizeof(Interval));
2507 result->month = span->month;
2508 result->day = span->day;
2509 result->time = span->time;
2511 wholemonth = result->day / DAYS_PER_MONTH;
2512 result->day -= wholemonth * DAYS_PER_MONTH;
2513 result->month += wholemonth;
2515 if (result->month > 0 && result->day < 0)
2517 result->day += DAYS_PER_MONTH;
2520 else if (result->month < 0 && result->day > 0)
2522 result->day -= DAYS_PER_MONTH;
2526 PG_RETURN_INTERVAL_P(result);
2529 /* timestamp_pl_interval()
2530 * Add a interval to a timestamp data type.
2531 * Note that interval has provisions for qualitative year/month and day
2532 * units, so try to do the right thing with them.
2533 * To add a month, increment the month, and use the same day of month.
2534 * Then, if the next month has fewer days, set the day of month
2535 * to the last day of month.
2536 * To add a day, increment the mday, and use the same time of day.
2537 * Lastly, add in the "quantitative time".
2540 timestamp_pl_interval(PG_FUNCTION_ARGS)
2542 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
2543 Interval *span = PG_GETARG_INTERVAL_P(1);
2546 if (TIMESTAMP_NOT_FINITE(timestamp))
2550 if (span->month != 0)
2556 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
2558 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2559 errmsg("timestamp out of range")));
2561 tm->tm_mon += span->month;
2562 if (tm->tm_mon > MONTHS_PER_YEAR)
2564 tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
2565 tm->tm_mon = ((tm->tm_mon - 1) % MONTHS_PER_YEAR) + 1;
2567 else if (tm->tm_mon < 1)
2569 tm->tm_year += tm->tm_mon / MONTHS_PER_YEAR - 1;
2570 tm->tm_mon = tm->tm_mon % MONTHS_PER_YEAR + MONTHS_PER_YEAR;
2573 /* adjust for end of month boundary problems... */
2574 if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
2575 tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
2577 if (tm2timestamp(tm, fsec, NULL, ×tamp) != 0)
2579 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2580 errmsg("timestamp out of range")));
2590 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
2592 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2593 errmsg("timestamp out of range")));
2595 /* Add days by converting to and from julian */
2596 julian = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) + span->day;
2597 j2date(julian, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2599 if (tm2timestamp(tm, fsec, NULL, ×tamp) != 0)
2601 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2602 errmsg("timestamp out of range")));
2605 timestamp += span->time;
2609 PG_RETURN_TIMESTAMP(result);
2613 timestamp_mi_interval(PG_FUNCTION_ARGS)
2615 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
2616 Interval *span = PG_GETARG_INTERVAL_P(1);
2619 tspan.month = -span->month;
2620 tspan.day = -span->day;
2621 tspan.time = -span->time;
2623 return DirectFunctionCall2(timestamp_pl_interval,
2624 TimestampGetDatum(timestamp),
2625 PointerGetDatum(&tspan));
2629 /* timestamptz_pl_interval()
2630 * Add a interval to a timestamp with time zone data type.
2631 * Note that interval has provisions for qualitative year/month
2632 * units, so try to do the right thing with them.
2633 * To add a month, increment the month, and use the same day of month.
2634 * Then, if the next month has fewer days, set the day of month
2635 * to the last day of month.
2636 * Lastly, add in the "quantitative time".
2639 timestamptz_pl_interval(PG_FUNCTION_ARGS)
2641 TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
2642 Interval *span = PG_GETARG_INTERVAL_P(1);
2647 if (TIMESTAMP_NOT_FINITE(timestamp))
2651 if (span->month != 0)
2657 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
2659 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2660 errmsg("timestamp out of range")));
2662 tm->tm_mon += span->month;
2663 if (tm->tm_mon > MONTHS_PER_YEAR)
2665 tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
2666 tm->tm_mon = ((tm->tm_mon - 1) % MONTHS_PER_YEAR) + 1;
2668 else if (tm->tm_mon < 1)
2670 tm->tm_year += tm->tm_mon / MONTHS_PER_YEAR - 1;
2671 tm->tm_mon = tm->tm_mon % MONTHS_PER_YEAR + MONTHS_PER_YEAR;
2674 /* adjust for end of month boundary problems... */
2675 if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
2676 tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
2678 tz = DetermineTimeZoneOffset(tm, session_timezone);
2680 if (tm2timestamp(tm, fsec, &tz, ×tamp) != 0)
2682 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2683 errmsg("timestamp out of range")));
2693 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
2695 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2696 errmsg("timestamp out of range")));
2698 /* Add days by converting to and from julian */
2699 julian = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) + span->day;
2700 j2date(julian, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2702 tz = DetermineTimeZoneOffset(tm, session_timezone);
2704 if (tm2timestamp(tm, fsec, &tz, ×tamp) != 0)
2706 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2707 errmsg("timestamp out of range")));
2710 timestamp += span->time;
2714 PG_RETURN_TIMESTAMP(result);
2718 timestamptz_mi_interval(PG_FUNCTION_ARGS)
2720 TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
2721 Interval *span = PG_GETARG_INTERVAL_P(1);
2724 tspan.month = -span->month;
2725 tspan.day = -span->day;
2726 tspan.time = -span->time;
2728 return DirectFunctionCall2(timestamptz_pl_interval,
2729 TimestampGetDatum(timestamp),
2730 PointerGetDatum(&tspan));
2735 interval_um(PG_FUNCTION_ARGS)
2737 Interval *interval = PG_GETARG_INTERVAL_P(0);
2740 result = (Interval *) palloc(sizeof(Interval));
2742 result->time = -interval->time;
2743 result->day = -interval->day;
2744 result->month = -interval->month;
2746 PG_RETURN_INTERVAL_P(result);
2751 interval_smaller(PG_FUNCTION_ARGS)
2753 Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2754 Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2757 /* use interval_cmp_internal to be sure this agrees with comparisons */
2758 if (interval_cmp_internal(interval1, interval2) < 0)
2762 PG_RETURN_INTERVAL_P(result);
2766 interval_larger(PG_FUNCTION_ARGS)
2768 Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2769 Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2772 if (interval_cmp_internal(interval1, interval2) > 0)
2776 PG_RETURN_INTERVAL_P(result);
2780 interval_pl(PG_FUNCTION_ARGS)
2782 Interval *span1 = PG_GETARG_INTERVAL_P(0);
2783 Interval *span2 = PG_GETARG_INTERVAL_P(1);
2786 result = (Interval *) palloc(sizeof(Interval));
2788 result->month = span1->month + span2->month;
2789 result->day = span1->day + span2->day;
2790 result->time = span1->time + span2->time;
2792 PG_RETURN_INTERVAL_P(result);
2796 interval_mi(PG_FUNCTION_ARGS)
2798 Interval *span1 = PG_GETARG_INTERVAL_P(0);
2799 Interval *span2 = PG_GETARG_INTERVAL_P(1);
2802 result = (Interval *) palloc(sizeof(Interval));
2804 result->month = span1->month - span2->month;
2805 result->day = span1->day - span2->day;
2806 result->time = span1->time - span2->time;
2808 PG_RETURN_INTERVAL_P(result);
2812 interval_mul(PG_FUNCTION_ARGS)
2814 Interval *span = PG_GETARG_INTERVAL_P(0);
2815 float8 factor = PG_GETARG_FLOAT8(1);
2816 double month_remainder_days,
2818 int32 orig_month = span->month,
2819 orig_day = span->day;
2822 result = (Interval *) palloc(sizeof(Interval));
2824 result->month = (int32) (span->month * factor);
2825 result->day = (int32) (span->day * factor);
2828 * The above correctly handles the whole-number part of the month and day
2829 * products, but we have to do something with any fractional part
2830 * resulting when the factor is nonintegral. We cascade the fractions
2831 * down to lower units using the conversion factors DAYS_PER_MONTH and
2832 * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
2833 * so by the representation. The user can choose to cascade up later,
2834 * using justify_hours and/or justify_days.
2838 * Fractional months full days into days.
2840 * Floating point calculation are inherently inprecise, so these
2841 * calculations are crafted to produce the most reliable result possible.
2842 * TSROUND() is needed to more accurately produce whole numbers where
2845 month_remainder_days = (orig_month * factor - result->month) * DAYS_PER_MONTH;
2846 month_remainder_days = TSROUND(month_remainder_days);
2847 sec_remainder = (orig_day * factor - result->day +
2848 month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
2849 sec_remainder = TSROUND(sec_remainder);
2852 * Might have 24:00:00 hours due to rounding, or >24 hours because of time
2853 * cascade from months and days. It might still be >24 if the combination
2854 * of cascade and the seconds factor operation itself.
2856 if (Abs(sec_remainder) >= SECS_PER_DAY)
2858 result->day += (int) (sec_remainder / SECS_PER_DAY);
2859 sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
2862 /* cascade units down */
2863 result->day += (int32) month_remainder_days;
2864 #ifdef HAVE_INT64_TIMESTAMP
2865 result->time = rint(span->time * factor + sec_remainder * USECS_PER_SEC);
2867 result->time = span->time * factor + sec_remainder;
2870 PG_RETURN_INTERVAL_P(result);
2874 mul_d_interval(PG_FUNCTION_ARGS)
2876 /* Args are float8 and Interval *, but leave them as generic Datum */
2877 Datum factor = PG_GETARG_DATUM(0);
2878 Datum span = PG_GETARG_DATUM(1);
2880 return DirectFunctionCall2(interval_mul, span, factor);
2884 interval_div(PG_FUNCTION_ARGS)
2886 Interval *span = PG_GETARG_INTERVAL_P(0);
2887 float8 factor = PG_GETARG_FLOAT8(1);
2888 double month_remainder_days,
2890 int32 orig_month = span->month,
2891 orig_day = span->day;
2894 result = (Interval *) palloc(sizeof(Interval));
2898 (errcode(ERRCODE_DIVISION_BY_ZERO),
2899 errmsg("division by zero")));
2901 result->month = (int32) (span->month / factor);
2902 result->day = (int32) (span->day / factor);
2905 * Fractional months full days into days. See comment in interval_mul().
2907 month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
2908 month_remainder_days = TSROUND(month_remainder_days);
2909 sec_remainder = (orig_day / factor - result->day +
2910 month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
2911 sec_remainder = TSROUND(sec_remainder);
2912 if (Abs(sec_remainder) >= SECS_PER_DAY)
2914 result->day += (int) (sec_remainder / SECS_PER_DAY);
2915 sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
2918 /* cascade units down */
2919 result->day += (int32) month_remainder_days;
2920 #ifdef HAVE_INT64_TIMESTAMP
2921 result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
2923 /* See TSROUND comment in interval_mul(). */
2924 result->time = span->time / factor + sec_remainder;
2927 PG_RETURN_INTERVAL_P(result);
2931 * interval_accum and interval_avg implement the AVG(interval) aggregate.
2933 * The transition datatype for this aggregate is a 2-element array of
2934 * intervals, where the first is the running sum and the second contains
2935 * the number of values so far in its 'time' field. This is a bit ugly
2936 * but it beats inventing a specialized datatype for the purpose.
2940 interval_accum(PG_FUNCTION_ARGS)
2942 ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2943 Interval *newval = PG_GETARG_INTERVAL_P(1);
2951 deconstruct_array(transarray,
2952 INTERVALOID, sizeof(Interval), false, 'd',
2953 &transdatums, NULL, &ndatums);
2955 elog(ERROR, "expected 2-element interval array");
2958 * XXX memcpy, instead of just extracting a pointer, to work around buggy
2959 * array code: it won't ensure proper alignment of Interval objects on
2960 * machines where double requires 8-byte alignment. That should be fixed,
2961 * but in the meantime...
2963 * Note: must use DatumGetPointer here, not DatumGetIntervalP, else some
2964 * compilers optimize into double-aligned load/store anyway.
2966 memcpy((void *) &sumX, DatumGetPointer(transdatums[0]), sizeof(Interval));
2967 memcpy((void *) &N, DatumGetPointer(transdatums[1]), sizeof(Interval));
2969 newsum = DatumGetIntervalP(DirectFunctionCall2(interval_pl,
2970 IntervalPGetDatum(&sumX),
2971 IntervalPGetDatum(newval)));
2974 transdatums[0] = IntervalPGetDatum(newsum);
2975 transdatums[1] = IntervalPGetDatum(&N);
2977 result = construct_array(transdatums, 2,
2978 INTERVALOID, sizeof(Interval), false, 'd');
2980 PG_RETURN_ARRAYTYPE_P(result);
2984 interval_avg(PG_FUNCTION_ARGS)
2986 ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2992 deconstruct_array(transarray,
2993 INTERVALOID, sizeof(Interval), false, 'd',
2994 &transdatums, NULL, &ndatums);
2996 elog(ERROR, "expected 2-element interval array");
2999 * XXX memcpy, instead of just extracting a pointer, to work around buggy
3000 * array code: it won't ensure proper alignment of Interval objects on
3001 * machines where double requires 8-byte alignment. That should be fixed,
3002 * but in the meantime...
3004 * Note: must use DatumGetPointer here, not DatumGetIntervalP, else some
3005 * compilers optimize into double-aligned load/store anyway.
3007 memcpy((void *) &sumX, DatumGetPointer(transdatums[0]), sizeof(Interval));
3008 memcpy((void *) &N, DatumGetPointer(transdatums[1]), sizeof(Interval));
3010 /* SQL92 defines AVG of no values to be NULL */
3014 return DirectFunctionCall2(interval_div,
3015 IntervalPGetDatum(&sumX),
3016 Float8GetDatum(N.time));
3021 * Calculate time difference while retaining year/month fields.
3022 * Note that this does not result in an accurate absolute time span
3023 * since year and month are out of context once the arithmetic
3027 timestamp_age(PG_FUNCTION_ARGS)
3029 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
3030 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
3042 result = (Interval *) palloc(sizeof(Interval));
3044 if (timestamp2tm(dt1, NULL, tm1, &fsec1, NULL, NULL) == 0 &&
3045 timestamp2tm(dt2, NULL, tm2, &fsec2, NULL, NULL) == 0)
3047 /* form the symbolic difference */
3048 fsec = fsec1 - fsec2;
3049 tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
3050 tm->tm_min = tm1->tm_min - tm2->tm_min;
3051 tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
3052 tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
3053 tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
3054 tm->tm_year = tm1->tm_year - tm2->tm_year;
3056 /* flip sign if necessary... */
3060 tm->tm_sec = -tm->tm_sec;
3061 tm->tm_min = -tm->tm_min;
3062 tm->tm_hour = -tm->tm_hour;
3063 tm->tm_mday = -tm->tm_mday;
3064 tm->tm_mon = -tm->tm_mon;
3065 tm->tm_year = -tm->tm_year;
3068 /* propagate any negative fields into the next higher field */
3071 #ifdef HAVE_INT64_TIMESTAMP
3072 fsec += USECS_PER_SEC;
3079 while (tm->tm_sec < 0)
3081 tm->tm_sec += SECS_PER_MINUTE;
3085 while (tm->tm_min < 0)
3087 tm->tm_min += MINS_PER_HOUR;
3091 while (tm->tm_hour < 0)
3093 tm->tm_hour += HOURS_PER_DAY;
3097 while (tm->tm_mday < 0)
3101 tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
3106 tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
3111 while (tm->tm_mon < 0)
3113 tm->tm_mon += MONTHS_PER_YEAR;
3117 /* recover sign if necessary... */
3121 tm->tm_sec = -tm->tm_sec;
3122 tm->tm_min = -tm->tm_min;
3123 tm->tm_hour = -tm->tm_hour;
3124 tm->tm_mday = -tm->tm_mday;
3125 tm->tm_mon = -tm->tm_mon;
3126 tm->tm_year = -tm->tm_year;
3129 if (tm2interval(tm, fsec, result) != 0)
3131 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3132 errmsg("interval out of range")));
3136 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3137 errmsg("timestamp out of range")));
3139 PG_RETURN_INTERVAL_P(result);
3143 /* timestamptz_age()
3144 * Calculate time difference while retaining year/month fields.
3145 * Note that this does not result in an accurate absolute time span
3146 * since year and month are out of context once the arithmetic
3150 timestamptz_age(PG_FUNCTION_ARGS)
3152 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
3153 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
3168 result = (Interval *) palloc(sizeof(Interval));
3170 if (timestamp2tm(dt1, &tz1, tm1, &fsec1, &tzn, NULL) == 0 &&
3171 timestamp2tm(dt2, &tz2, tm2, &fsec2, &tzn, NULL) == 0)
3173 /* form the symbolic difference */
3174 fsec = fsec1 - fsec2;
3175 tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
3176 tm->tm_min = tm1->tm_min - tm2->tm_min;
3177 tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
3178 tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
3179 tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
3180 tm->tm_year = tm1->tm_year - tm2->tm_year;
3182 /* flip sign if necessary... */
3186 tm->tm_sec = -tm->tm_sec;
3187 tm->tm_min = -tm->tm_min;
3188 tm->tm_hour = -tm->tm_hour;
3189 tm->tm_mday = -tm->tm_mday;
3190 tm->tm_mon = -tm->tm_mon;
3191 tm->tm_year = -tm->tm_year;
3194 /* propagate any negative fields into the next higher field */
3197 #ifdef HAVE_INT64_TIMESTAMP
3198 fsec += USECS_PER_SEC;
3205 while (tm->tm_sec < 0)
3207 tm->tm_sec += SECS_PER_MINUTE;
3211 while (tm->tm_min < 0)
3213 tm->tm_min += MINS_PER_HOUR;
3217 while (tm->tm_hour < 0)
3219 tm->tm_hour += HOURS_PER_DAY;
3223 while (tm->tm_mday < 0)
3227 tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
3232 tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
3237 while (tm->tm_mon < 0)
3239 tm->tm_mon += MONTHS_PER_YEAR;
3244 * Note: we deliberately ignore any difference between tz1 and tz2.
3247 /* recover sign if necessary... */
3251 tm->tm_sec = -tm->tm_sec;
3252 tm->tm_min = -tm->tm_min;
3253 tm->tm_hour = -tm->tm_hour;
3254 tm->tm_mday = -tm->tm_mday;
3255 tm->tm_mon = -tm->tm_mon;
3256 tm->tm_year = -tm->tm_year;
3259 if (tm2interval(tm, fsec, result) != 0)
3261 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3262 errmsg("interval out of range")));
3266 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3267 errmsg("timestamp out of range")));
3269 PG_RETURN_INTERVAL_P(result);
3273 /*----------------------------------------------------------
3274 * Conversion operators.
3275 *---------------------------------------------------------*/
3278 /* timestamp_trunc()
3279 * Truncate timestamp to specified units.
3282 timestamp_trunc(PG_FUNCTION_ARGS)
3284 text *units = PG_GETARG_TEXT_P(0);
3285 Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
3294 if (TIMESTAMP_NOT_FINITE(timestamp))
3295 PG_RETURN_TIMESTAMP(timestamp);
3297 lowunits = downcase_truncate_identifier(VARDATA(units),
3298 VARSIZE(units) - VARHDRSZ,
3301 type = DecodeUnits(0, lowunits, &val);
3305 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
3307 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3308 errmsg("timestamp out of range")));
3316 woy = date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday);
3319 * If it is week 52/53 and the month is January, then the
3320 * week must belong to the previous year. Also, some
3321 * December dates belong to the next year.
3323 if (woy >= 52 && tm->tm_mon == 1)
3325 if (woy <= 1 && tm->tm_mon == MONTHS_PER_YEAR)
3327 isoweek2date(woy, &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
3334 case DTK_MILLENNIUM:
3335 /* see comments in timestamptz_trunc */
3336 if (tm->tm_year > 0)
3337 tm->tm_year = ((tm->tm_year + 999) / 1000) * 1000 - 999;
3339 tm->tm_year = -((999 - (tm->tm_year - 1)) / 1000) * 1000 + 1;
3341 /* see comments in timestamptz_trunc */
3342 if (tm->tm_year > 0)
3343 tm->tm_year = ((tm->tm_year + 99) / 100) * 100 - 99;
3345 tm->tm_year = -((99 - (tm->tm_year - 1)) / 100) * 100 + 1;
3347 /* see comments in timestamptz_trunc */
3348 if (val != DTK_MILLENNIUM && val != DTK_CENTURY)
3350 if (tm->tm_year > 0)
3351 tm->tm_year = (tm->tm_year / 10) * 10;
3353 tm->tm_year = -((8 - (tm->tm_year - 1)) / 10) * 10;
3358 tm->tm_mon = (3 * ((tm->tm_mon - 1) / 3)) + 1;
3372 #ifdef HAVE_INT64_TIMESTAMP
3373 fsec = (fsec / 1000) * 1000;
3375 fsec = rint(fsec * 1000) / 1000;
3380 #ifndef HAVE_INT64_TIMESTAMP
3381 fsec = rint(fsec * 1000000) / 1000000;
3387 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3388 errmsg("timestamp units \"%s\" not supported",
3393 if (tm2timestamp(tm, fsec, NULL, &result) != 0)
3395 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3396 errmsg("timestamp out of range")));
3401 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3402 errmsg("timestamp units \"%s\" not recognized",
3407 PG_RETURN_TIMESTAMP(result);
3410 /* timestamptz_trunc()
3411 * Truncate timestamp to specified units.
3414 timestamptz_trunc(PG_FUNCTION_ARGS)
3416 text *units = PG_GETARG_TEXT_P(0);
3417 TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
3422 bool redotz = false;
3429 if (TIMESTAMP_NOT_FINITE(timestamp))
3430 PG_RETURN_TIMESTAMPTZ(timestamp);
3432 lowunits = downcase_truncate_identifier(VARDATA(units),
3433 VARSIZE(units) - VARHDRSZ,
3436 type = DecodeUnits(0, lowunits, &val);
3440 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
3442 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3443 errmsg("timestamp out of range")));
3451 woy = date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday);
3454 * If it is week 52/53 and the month is January, then the
3455 * week must belong to the previous year. Also, some
3456 * December dates belong to the next year.
3458 if (woy >= 52 && tm->tm_mon == 1)
3460 if (woy <= 1 && tm->tm_mon == MONTHS_PER_YEAR)
3462 isoweek2date(woy, &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
3470 /* one may consider DTK_THOUSAND and DTK_HUNDRED... */
3471 case DTK_MILLENNIUM:
3474 * truncating to the millennium? what is this supposed to
3475 * mean? let us put the first year of the millennium... i.e.
3476 * -1000, 1, 1001, 2001...
3478 if (tm->tm_year > 0)
3479 tm->tm_year = ((tm->tm_year + 999) / 1000) * 1000 - 999;
3481 tm->tm_year = -((999 - (tm->tm_year - 1)) / 1000) * 1000 + 1;
3484 /* truncating to the century? as above: -100, 1, 101... */
3485 if (tm->tm_year > 0)
3486 tm->tm_year = ((tm->tm_year + 99) / 100) * 100 - 99;
3488 tm->tm_year = -((99 - (tm->tm_year - 1)) / 100) * 100 + 1;
3493 * truncating to the decade? first year of the decade. must
3494 * not be applied if year was truncated before!
3496 if (val != DTK_MILLENNIUM && val != DTK_CENTURY)
3498 if (tm->tm_year > 0)
3499 tm->tm_year = (tm->tm_year / 10) * 10;
3501 tm->tm_year = -((8 - (tm->tm_year - 1)) / 10) * 10;
3508 tm->tm_mon = (3 * ((tm->tm_mon - 1) / 3)) + 1;
3515 redotz = true; /* for all cases >= DAY */
3528 #ifdef HAVE_INT64_TIMESTAMP
3529 fsec = (fsec / 1000) * 1000;
3531 fsec = rint(fsec * 1000) / 1000;
3535 #ifndef HAVE_INT64_TIMESTAMP
3536 fsec = rint(fsec * 1000000) / 1000000;
3542 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3543 errmsg("timestamp with time zone units \"%s\" not "
3544 "supported", lowunits)));
3549 tz = DetermineTimeZoneOffset(tm, session_timezone);
3551 if (tm2timestamp(tm, fsec, &tz, &result) != 0)
3553 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3554 errmsg("timestamp out of range")));
3559 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3560 errmsg("timestamp with time zone units \"%s\" not recognized",
3565 PG_RETURN_TIMESTAMPTZ(result);
3569 * Extract specified field from interval.
3572 interval_trunc(PG_FUNCTION_ARGS)
3574 text *units = PG_GETARG_TEXT_P(0);
3575 Interval *interval = PG_GETARG_INTERVAL_P(1);
3584 result = (Interval *) palloc(sizeof(Interval));
3586 lowunits = downcase_truncate_identifier(VARDATA(units),
3587 VARSIZE(units) - VARHDRSZ,
3590 type = DecodeUnits(0, lowunits, &val);
3594 if (interval2tm(*interval, tm, &fsec) == 0)
3599 case DTK_MILLENNIUM:
3600 /* caution: C division may have negative remainder */
3601 tm->tm_year = (tm->tm_year / 1000) * 1000;
3603 /* caution: C division may have negative remainder */
3604 tm->tm_year = (tm->tm_year / 100) * 100;
3606 /* caution: C division may have negative remainder */
3607 tm->tm_year = (tm->tm_year / 10) * 10;
3611 tm->tm_mon = 3 * (tm->tm_mon / 3);
3625 #ifdef HAVE_INT64_TIMESTAMP
3626 fsec = (fsec / 1000) * 1000;
3628 fsec = rint(fsec * 1000) / 1000;
3632 #ifndef HAVE_INT64_TIMESTAMP
3633 fsec = rint(fsec * 1000000) / 1000000;
3639 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3640 errmsg("interval units \"%s\" not supported",
3644 if (tm2interval(tm, fsec, result) != 0)
3646 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3647 errmsg("interval out of range")));
3650 elog(ERROR, "could not convert interval to tm");
3655 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3656 errmsg("interval units \"%s\" not recognized",
3657 DatumGetCString(DirectFunctionCall1(textout,
3658 PointerGetDatum(units))))));
3659 *result = *interval;
3662 PG_RETURN_INTERVAL_P(result);
3667 * Return the Julian day which corresponds to the first day (Monday) of the given ISO 8601 year and week.
3668 * Julian days are used to convert between ISO week dates and Gregorian dates.
3671 isoweek2j(int year, int week)
3678 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3679 errmsg("cannot calculate week number without year information")));
3681 /* fourth day of current year */
3682 day4 = date2j(year, 1, 4);
3684 /* day0 == offset to first day of week (Monday) */
3685 day0 = j2day(day4 - 1);
3687 return ((week - 1) * 7) + (day4 - day0);
3691 * Convert ISO week of year number to date.
3692 * The year field must be specified with the ISO year!
3696 isoweek2date(int woy, int *year, int *mon, int *mday)
3698 j2date(isoweek2j(*year, woy), year, mon, mday);
3701 /* isoweekdate2date()
3703 * Convert an ISO 8601 week date (ISO year, ISO week and day of week) into a Gregorian date.
3704 * Populates year, mon, and mday with the correct Gregorian values.
3705 * year must be passed in as the ISO year.
3708 isoweekdate2date(int isoweek, int isowday, int *year, int *mon, int *mday)
3712 jday = isoweek2j(*year, isoweek);
3713 jday += isowday - 1;
3715 j2date(jday, year, mon, mday);
3720 * Returns ISO week number of year.
3723 date2isoweek(int year, int mon, int mday)
3731 dayn = date2j(year, mon, mday);
3733 /* fourth day of current year */
3734 day4 = date2j(year, 1, 4);
3736 /* day0 == offset to first day of week (Monday) */
3737 day0 = j2day(day4 - 1);
3740 * We need the first week containing a Thursday, otherwise this day falls
3741 * into the previous year for purposes of counting weeks
3743 if (dayn < day4 - day0)
3745 day4 = date2j(year - 1, 1, 4);
3747 /* day0 == offset to first day of week (Monday) */
3748 day0 = j2day(day4 - 1);
3751 result = (dayn - (day4 - day0)) / 7 + 1;
3754 * Sometimes the last few days in a year will fall into the first week of
3755 * the next year, so check for this.
3759 day4 = date2j(year + 1, 1, 4);
3761 /* day0 == offset to first day of week (Monday) */
3762 day0 = j2day(day4 - 1);
3764 if (dayn >= day4 - day0)
3765 result = (dayn - (day4 - day0)) / 7 + 1;
3768 return (int) result;
3774 * Returns ISO 8601 year number.
3777 date2isoyear(int year, int mon, int mday)
3785 dayn = date2j(year, mon, mday);
3787 /* fourth day of current year */
3788 day4 = date2j(year, 1, 4);
3790 /* day0 == offset to first day of week (Monday) */
3791 day0 = j2day(day4 - 1);
3794 * We need the first week containing a Thursday, otherwise this day falls
3795 * into the previous year for purposes of counting weeks
3797 if (dayn < day4 - day0)
3799 day4 = date2j(year - 1, 1, 4);
3801 /* day0 == offset to first day of week (Monday) */
3802 day0 = j2day(day4 - 1);
3807 result = (dayn - (day4 - day0)) / 7 + 1;
3810 * Sometimes the last few days in a year will fall into the first week of
3811 * the next year, so check for this.
3815 day4 = date2j(year + 1, 1, 4);
3817 /* day0 == offset to first day of week (Monday) */
3818 day0 = j2day(day4 - 1);
3820 if (dayn >= day4 - day0)
3828 /* date2isoyearday()
3830 * Returns the ISO 8601 day-of-year, given a Gregorian year, month and day.
3831 * Possible return values are 1 through 371 (364 in non-leap years).
3834 date2isoyearday(int year, int mon, int mday)
3836 return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
3840 * Extract specified field from timestamp.
3843 timestamp_part(PG_FUNCTION_ARGS)
3845 text *units = PG_GETARG_TEXT_P(0);
3846 Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
3855 if (TIMESTAMP_NOT_FINITE(timestamp))
3858 PG_RETURN_FLOAT8(result);
3861 lowunits = downcase_truncate_identifier(VARDATA(units),
3862 VARSIZE(units) - VARHDRSZ,
3865 type = DecodeUnits(0, lowunits, &val);
3866 if (type == UNKNOWN_FIELD)
3867 type = DecodeSpecial(0, lowunits, &val);
3871 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
3873 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3874 errmsg("timestamp out of range")));
3879 #ifdef HAVE_INT64_TIMESTAMP
3880 result = tm->tm_sec * 1000000.0 + fsec;
3882 result = (tm->tm_sec + fsec) * 1000000;
3887 #ifdef HAVE_INT64_TIMESTAMP
3888 result = tm->tm_sec * 1000.0 + fsec / 1000.0;
3890 result = (tm->tm_sec + fsec) * 1000;
3895 #ifdef HAVE_INT64_TIMESTAMP
3896 result = tm->tm_sec + fsec / 1000000.0;
3898 result = tm->tm_sec + fsec;
3903 result = tm->tm_min;
3907 result = tm->tm_hour;
3911 result = tm->tm_mday;
3915 result = tm->tm_mon;
3919 result = (tm->tm_mon - 1) / 3 + 1;
3923 result = (float8) date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday);
3927 if (tm->tm_year > 0)
3928 result = tm->tm_year;
3930 /* there is no year 0, just 1 BC and 1 AD */
3931 result = tm->tm_year - 1;
3937 * what is a decade wrt dates? let us assume that decade 199
3938 * is 1990 thru 1999... decade 0 starts on year 1 BC, and -1
3939 * is 11 BC thru 2 BC...
3941 if (tm->tm_year >= 0)
3942 result = tm->tm_year / 10;
3944 result = -((8 - (tm->tm_year - 1)) / 10);
3950 * centuries AD, c>0: year in [ (c-1)* 100 + 1 : c*100 ]
3951 * centuries BC, c<0: year in [ c*100 : (c+1) * 100 - 1]
3952 * there is no number 0 century.
3955 if (tm->tm_year > 0)
3956 result = (tm->tm_year + 99) / 100;
3958 /* caution: C division may have negative remainder */
3959 result = -((99 - (tm->tm_year - 1)) / 100);
3962 case DTK_MILLENNIUM:
3963 /* see comments above. */
3964 if (tm->tm_year > 0)
3965 result = (tm->tm_year + 999) / 1000;
3967 result = -((999 - (tm->tm_year - 1)) / 1000);
3971 result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
3972 #ifdef HAVE_INT64_TIMESTAMP
3973 result += ((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) +
3974 tm->tm_sec + (fsec / 1000000.0)) / (double) SECS_PER_DAY;
3976 result += ((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) +
3977 tm->tm_sec + fsec) / (double) SECS_PER_DAY;
3982 result = date2isoyear(tm->tm_year, tm->tm_mon, tm->tm_mday);
3990 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3991 errmsg("timestamp units \"%s\" not supported",
3996 else if (type == RESERV)
4003 TimestampTz timestamptz;
4006 * convert to timestamptz to produce consistent results
4008 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4010 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4011 errmsg("timestamp out of range")));
4013 tz = DetermineTimeZoneOffset(tm, session_timezone);
4015 if (tm2timestamp(tm, fsec, &tz, ×tamptz) != 0)
4017 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4018 errmsg("timestamp out of range")));
4020 #ifdef HAVE_INT64_TIMESTAMP
4021 result = (timestamptz - SetEpochTimestamp()) / 1000000.0;
4023 result = timestamptz - SetEpochTimestamp();
4029 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4031 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4032 errmsg("timestamp out of range")));
4033 result = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
4034 if (val == DTK_ISODOW && result == 0)
4039 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4041 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4042 errmsg("timestamp out of range")));
4043 result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
4044 - date2j(tm->tm_year, 1, 1) + 1);
4049 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4050 errmsg("timestamp units \"%s\" not supported",
4059 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4060 errmsg("timestamp units \"%s\" not recognized", lowunits)));
4064 PG_RETURN_FLOAT8(result);
4067 /* timestamptz_part()
4068 * Extract specified field from timestamp with time zone.
4071 timestamptz_part(PG_FUNCTION_ARGS)
4073 text *units = PG_GETARG_TEXT_P(0);
4074 TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
4086 if (TIMESTAMP_NOT_FINITE(timestamp))
4089 PG_RETURN_FLOAT8(result);
4092 lowunits = downcase_truncate_identifier(VARDATA(units),
4093 VARSIZE(units) - VARHDRSZ,
4096 type = DecodeUnits(0, lowunits, &val);
4097 if (type == UNKNOWN_FIELD)
4098 type = DecodeSpecial(0, lowunits, &val);
4102 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
4104 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4105 errmsg("timestamp out of range")));
4115 result /= MINS_PER_HOUR;
4116 FMODULO(result, dummy, (double) MINS_PER_HOUR);
4121 FMODULO(dummy, result, (double) SECS_PER_HOUR);
4125 #ifdef HAVE_INT64_TIMESTAMP
4126 result = tm->tm_sec * 1000000.0 + fsec;
4128 result = (tm->tm_sec + fsec) * 1000000;
4133 #ifdef HAVE_INT64_TIMESTAMP
4134 result = tm->tm_sec * 1000.0 + fsec / 1000.0;
4136 result = (tm->tm_sec + fsec) * 1000;
4141 #ifdef HAVE_INT64_TIMESTAMP
4142 result = tm->tm_sec + fsec / 1000000.0;
4144 result = tm->tm_sec + fsec;
4149 result = tm->tm_min;
4153 result = tm->tm_hour;
4157 result = tm->tm_mday;
4161 result = tm->tm_mon;
4165 result = (tm->tm_mon - 1) / 3 + 1;
4169 result = (float8) date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday);
4173 if (tm->tm_year > 0)
4174 result = tm->tm_year;
4176 /* there is no year 0, just 1 BC and 1 AD */
4177 result = tm->tm_year - 1;
4181 /* see comments in timestamp_part */
4182 if (tm->tm_year > 0)
4183 result = tm->tm_year / 10;
4185 result = -((8 - (tm->tm_year - 1)) / 10);
4189 /* see comments in timestamp_part */
4190 if (tm->tm_year > 0)
4191 result = (tm->tm_year + 99) / 100;
4193 result = -((99 - (tm->tm_year - 1)) / 100);
4196 case DTK_MILLENNIUM:
4197 /* see comments in timestamp_part */
4198 if (tm->tm_year > 0)
4199 result = (tm->tm_year + 999) / 1000;
4201 result = -((999 - (tm->tm_year - 1)) / 1000);
4205 result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
4206 #ifdef HAVE_INT64_TIMESTAMP
4207 result += ((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) +
4208 tm->tm_sec + (fsec / 1000000.0)) / (double) SECS_PER_DAY;
4210 result += ((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) +
4211 tm->tm_sec + fsec) / (double) SECS_PER_DAY;
4216 result = date2isoyear(tm->tm_year, tm->tm_mon, tm->tm_mday);
4221 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4222 errmsg("timestamp with time zone units \"%s\" not supported",
4228 else if (type == RESERV)
4233 #ifdef HAVE_INT64_TIMESTAMP
4234 result = (timestamp - SetEpochTimestamp()) / 1000000.0;
4236 result = timestamp - SetEpochTimestamp();
4242 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
4244 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4245 errmsg("timestamp out of range")));
4246 result = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
4247 if (val == DTK_ISODOW && result == 0)
4252 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
4254 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4255 errmsg("timestamp out of range")));
4256 result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
4257 - date2j(tm->tm_year, 1, 1) + 1);
4262 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4263 errmsg("timestamp with time zone units \"%s\" not supported",
4271 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4272 errmsg("timestamp with time zone units \"%s\" not recognized",
4278 PG_RETURN_FLOAT8(result);
4283 * Extract specified field from interval.
4286 interval_part(PG_FUNCTION_ARGS)
4288 text *units = PG_GETARG_TEXT_P(0);
4289 Interval *interval = PG_GETARG_INTERVAL_P(1);
4298 lowunits = downcase_truncate_identifier(VARDATA(units),
4299 VARSIZE(units) - VARHDRSZ,
4302 type = DecodeUnits(0, lowunits, &val);
4303 if (type == UNKNOWN_FIELD)
4304 type = DecodeSpecial(0, lowunits, &val);
4308 if (interval2tm(*interval, tm, &fsec) == 0)
4313 #ifdef HAVE_INT64_TIMESTAMP
4314 result = tm->tm_sec * 1000000.0 + fsec;
4316 result = (tm->tm_sec + fsec) * 1000000;
4321 #ifdef HAVE_INT64_TIMESTAMP
4322 result = tm->tm_sec * 1000.0 + fsec / 1000.0;
4324 result = (tm->tm_sec + fsec) * 1000;
4329 #ifdef HAVE_INT64_TIMESTAMP
4330 result = tm->tm_sec + fsec / 1000000.0;
4332 result = tm->tm_sec + fsec;
4337 result = tm->tm_min;
4341 result = tm->tm_hour;
4345 result = tm->tm_mday;
4349 result = tm->tm_mon;
4353 result = (tm->tm_mon / 3) + 1;
4357 result = tm->tm_year;
4361 /* caution: C division may have negative remainder */
4362 result = tm->tm_year / 10;
4366 /* caution: C division may have negative remainder */
4367 result = tm->tm_year / 100;
4370 case DTK_MILLENNIUM:
4371 /* caution: C division may have negative remainder */
4372 result = tm->tm_year / 1000;
4377 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4378 errmsg("interval units \"%s\" not supported",
4379 DatumGetCString(DirectFunctionCall1(textout,
4380 PointerGetDatum(units))))));
4387 elog(ERROR, "could not convert interval to tm");
4391 else if (type == RESERV && val == DTK_EPOCH)
4393 #ifdef HAVE_INT64_TIMESTAMP
4394 result = interval->time / 1000000.0;
4396 result = interval->time;
4398 result += ((double) DAYS_PER_YEAR * SECS_PER_DAY) * (interval->month / MONTHS_PER_YEAR);
4399 result += ((double) DAYS_PER_MONTH * SECS_PER_DAY) * (interval->month % MONTHS_PER_YEAR);
4400 result += ((double) SECS_PER_DAY) * interval->day;
4405 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4406 errmsg("interval units \"%s\" not recognized",
4407 DatumGetCString(DirectFunctionCall1(textout,
4408 PointerGetDatum(units))))));
4412 PG_RETURN_FLOAT8(result);
4417 * Encode timestamp type with specified time zone.
4418 * This function is just timestamp2timestamptz() except instead of
4419 * shifting to the global timezone, we shift to the specified timezone.
4420 * This is different from the other AT TIME ZONE cases because instead
4421 * of shifting to a _to_ a new time zone, it sets the time to _be_ the
4422 * specified timezone.
4425 timestamp_zone(PG_FUNCTION_ARGS)
4427 text *zone = PG_GETARG_TEXT_P(0);
4428 Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
4432 char tzname[TZ_STRLEN_MAX + 1];
4435 if (TIMESTAMP_NOT_FINITE(timestamp))
4436 PG_RETURN_TIMESTAMPTZ(timestamp);
4439 * Look up the requested timezone. First we look in the timezone database
4440 * (to handle cases like "America/New_York"), and if that fails, we look
4441 * in the date token table (to handle cases like "EST").
4443 len = Min(VARSIZE(zone) - VARHDRSZ, TZ_STRLEN_MAX);
4444 memcpy(tzname, VARDATA(zone), len);
4446 tzp = pg_tzset(tzname);
4449 /* Apply the timezone change */
4453 if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, tzp) != 0)
4455 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4456 errmsg("timestamp out of range")));
4457 tz = DetermineTimeZoneOffset(&tm, tzp);
4458 if (tm2timestamp(&tm, fsec, &tz, &result) != 0)
4460 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4461 errmsg("could not convert to time zone \"%s\"",
4470 lowzone = downcase_truncate_identifier(VARDATA(zone),
4471 VARSIZE(zone) - VARHDRSZ,
4473 type = DecodeSpecial(0, lowzone, &val);
4475 if (type == TZ || type == DTZ)
4480 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4481 errmsg("time zone \"%s\" not recognized", tzname)));
4482 tz = 0; /* keep compiler quiet */
4485 result = dt2local(timestamp, tz);
4488 PG_RETURN_TIMESTAMPTZ(result);
4491 /* timestamp_izone()
4492 * Encode timestamp type with specified time interval as time zone.
4495 timestamp_izone(PG_FUNCTION_ARGS)
4497 Interval *zone = PG_GETARG_INTERVAL_P(0);
4498 Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
4502 if (TIMESTAMP_NOT_FINITE(timestamp))
4503 PG_RETURN_TIMESTAMPTZ(timestamp);
4505 if (zone->month != 0)
4507 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4508 errmsg("interval time zone \"%s\" must not specify month",
4509 DatumGetCString(DirectFunctionCall1(interval_out,
4510 PointerGetDatum(zone))))));
4512 #ifdef HAVE_INT64_TIMESTAMP
4513 tz = zone->time / USECS_PER_SEC;
4518 result = dt2local(timestamp, tz);
4520 PG_RETURN_TIMESTAMPTZ(result);
4521 } /* timestamp_izone() */
4523 /* timestamp_timestamptz()
4524 * Convert local timestamp to timestamp at GMT
4527 timestamp_timestamptz(PG_FUNCTION_ARGS)
4529 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
4531 PG_RETURN_TIMESTAMPTZ(timestamp2timestamptz(timestamp));
4535 timestamp2timestamptz(Timestamp timestamp)
4543 if (TIMESTAMP_NOT_FINITE(timestamp))
4547 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4549 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4550 errmsg("timestamp out of range")));
4552 tz = DetermineTimeZoneOffset(tm, session_timezone);
4554 if (tm2timestamp(tm, fsec, &tz, &result) != 0)
4556 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4557 errmsg("timestamp out of range")));
4563 /* timestamptz_timestamp()
4564 * Convert timestamp at GMT to local timestamp
4567 timestamptz_timestamp(PG_FUNCTION_ARGS)
4569 TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
4577 if (TIMESTAMP_NOT_FINITE(timestamp))
4581 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
4583 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4584 errmsg("timestamp out of range")));
4585 if (tm2timestamp(tm, fsec, NULL, &result) != 0)
4587 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4588 errmsg("timestamp out of range")));
4590 PG_RETURN_TIMESTAMP(result);
4593 /* timestamptz_zone()
4594 * Evaluate timestamp with time zone type at the specified time zone.
4595 * Returns a timestamp without time zone.
4598 timestamptz_zone(PG_FUNCTION_ARGS)
4600 text *zone = PG_GETARG_TEXT_P(0);
4601 TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
4605 char tzname[TZ_STRLEN_MAX + 1];
4608 if (TIMESTAMP_NOT_FINITE(timestamp))
4609 PG_RETURN_TIMESTAMP(timestamp);
4612 * Look up the requested timezone. First we look in the timezone database
4613 * (to handle cases like "America/New_York"), and if that fails, we look
4614 * in the date token table (to handle cases like "EST").
4616 len = Min(VARSIZE(zone) - VARHDRSZ, TZ_STRLEN_MAX);
4617 memcpy(tzname, VARDATA(zone), len);
4619 tzp = pg_tzset(tzname);
4622 /* Apply the timezone change */
4626 if (timestamp2tm(timestamp, &tz, &tm, &fsec, NULL, tzp) != 0)
4628 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4629 errmsg("timestamp out of range")));
4630 if (tm2timestamp(&tm, fsec, NULL, &result) != 0)
4632 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4633 errmsg("could not convert to time zone \"%s\"",
4642 lowzone = downcase_truncate_identifier(VARDATA(zone),
4643 VARSIZE(zone) - VARHDRSZ,
4645 type = DecodeSpecial(0, lowzone, &val);
4647 if (type == TZ || type == DTZ)
4652 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4653 errmsg("time zone \"%s\" not recognized", tzname)));
4654 tz = 0; /* keep compiler quiet */
4657 result = dt2local(timestamp, tz);
4660 PG_RETURN_TIMESTAMP(result);
4663 /* timestamptz_izone()
4664 * Encode timestamp with time zone type with specified time interval as time zone.
4665 * Returns a timestamp without time zone.
4668 timestamptz_izone(PG_FUNCTION_ARGS)
4670 Interval *zone = PG_GETARG_INTERVAL_P(0);
4671 TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
4675 if (TIMESTAMP_NOT_FINITE(timestamp))
4676 PG_RETURN_TIMESTAMP(timestamp);
4678 if (zone->month != 0)
4680 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4681 errmsg("interval time zone \"%s\" must not specify month",
4682 DatumGetCString(DirectFunctionCall1(interval_out,
4683 PointerGetDatum(zone))))));
4685 #ifdef HAVE_INT64_TIMESTAMP
4686 tz = -(zone->time / USECS_PER_SEC);
4691 result = dt2local(timestamp, tz);
4693 PG_RETURN_TIMESTAMP(result);