1 /*-------------------------------------------------------------------------
4 * implements DATE and TIME data types specified in SQL-92 standard
6 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994-5, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.95 2004/02/14 20:16:17 tgl Exp $
13 *-------------------------------------------------------------------------
23 #include "access/hash.h"
24 #include "libpq/pqformat.h"
25 #include "miscadmin.h"
26 #include "utils/builtins.h"
27 #include "utils/date.h"
28 #include "utils/nabstime.h"
29 #include "utils/timestamp.h"
32 * gcc's -ffast-math switch breaks routines that expect exact results from
33 * expressions like timeval / 3600, where timeval is double.
36 #error -ffast-math is known to break this code
40 static int time2tm(TimeADT time, struct tm * tm, fsec_t *fsec);
41 static int timetz2tm(TimeTzADT *time, struct tm * tm, fsec_t *fsec, int *tzp);
42 static int tm2time(struct tm * tm, fsec_t fsec, TimeADT *result);
43 static int tm2timetz(struct tm * tm, fsec_t fsec, int tz, TimeTzADT *result);
44 static void AdjustTimeForTypmod(TimeADT *time, int32 typmod);
46 /*****************************************************************************
48 *****************************************************************************/
52 * Given date text string, convert to internal date format.
55 date_in(PG_FUNCTION_ARGS)
57 char *str = PG_GETARG_CSTRING(0);
66 char *field[MAXDATEFIELDS];
67 int ftype[MAXDATEFIELDS];
68 char lowstr[MAXDATELEN + 1];
70 if (strlen(str) >= sizeof(lowstr))
71 dterr = DTERR_BAD_FORMAT;
73 dterr = ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf);
75 dterr = DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp);
77 DateTimeParseError(dterr, str, "date");
86 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
87 errmsg("date/time value \"current\" is no longer supported")));
89 GetCurrentDateTime(tm);
97 DateTimeParseError(DTERR_BAD_FORMAT, str, "date");
101 date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
103 PG_RETURN_DATEADT(date);
107 * Given internal format date, convert to text string.
110 date_out(PG_FUNCTION_ARGS)
112 DateADT date = PG_GETARG_DATEADT(0);
116 char buf[MAXDATELEN + 1];
118 j2date(date + POSTGRES_EPOCH_JDATE,
119 &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
121 EncodeDateOnly(tm, DateStyle, buf);
123 result = pstrdup(buf);
124 PG_RETURN_CSTRING(result);
128 * date_recv - converts external binary format to date
131 date_recv(PG_FUNCTION_ARGS)
133 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
135 PG_RETURN_DATEADT((DateADT) pq_getmsgint(buf, sizeof(DateADT)));
139 * date_send - converts date to binary format
142 date_send(PG_FUNCTION_ARGS)
144 DateADT date = PG_GETARG_DATEADT(0);
147 pq_begintypsend(&buf);
148 pq_sendint(&buf, date, sizeof(date));
149 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
154 * Comparison functions for dates
158 date_eq(PG_FUNCTION_ARGS)
160 DateADT dateVal1 = PG_GETARG_DATEADT(0);
161 DateADT dateVal2 = PG_GETARG_DATEADT(1);
163 PG_RETURN_BOOL(dateVal1 == dateVal2);
167 date_ne(PG_FUNCTION_ARGS)
169 DateADT dateVal1 = PG_GETARG_DATEADT(0);
170 DateADT dateVal2 = PG_GETARG_DATEADT(1);
172 PG_RETURN_BOOL(dateVal1 != dateVal2);
176 date_lt(PG_FUNCTION_ARGS)
178 DateADT dateVal1 = PG_GETARG_DATEADT(0);
179 DateADT dateVal2 = PG_GETARG_DATEADT(1);
181 PG_RETURN_BOOL(dateVal1 < dateVal2);
185 date_le(PG_FUNCTION_ARGS)
187 DateADT dateVal1 = PG_GETARG_DATEADT(0);
188 DateADT dateVal2 = PG_GETARG_DATEADT(1);
190 PG_RETURN_BOOL(dateVal1 <= dateVal2);
194 date_gt(PG_FUNCTION_ARGS)
196 DateADT dateVal1 = PG_GETARG_DATEADT(0);
197 DateADT dateVal2 = PG_GETARG_DATEADT(1);
199 PG_RETURN_BOOL(dateVal1 > dateVal2);
203 date_ge(PG_FUNCTION_ARGS)
205 DateADT dateVal1 = PG_GETARG_DATEADT(0);
206 DateADT dateVal2 = PG_GETARG_DATEADT(1);
208 PG_RETURN_BOOL(dateVal1 >= dateVal2);
212 date_cmp(PG_FUNCTION_ARGS)
214 DateADT dateVal1 = PG_GETARG_DATEADT(0);
215 DateADT dateVal2 = PG_GETARG_DATEADT(1);
217 if (dateVal1 < dateVal2)
219 else if (dateVal1 > dateVal2)
225 date_larger(PG_FUNCTION_ARGS)
227 DateADT dateVal1 = PG_GETARG_DATEADT(0);
228 DateADT dateVal2 = PG_GETARG_DATEADT(1);
230 PG_RETURN_DATEADT((dateVal1 > dateVal2) ? dateVal1 : dateVal2);
234 date_smaller(PG_FUNCTION_ARGS)
236 DateADT dateVal1 = PG_GETARG_DATEADT(0);
237 DateADT dateVal2 = PG_GETARG_DATEADT(1);
239 PG_RETURN_DATEADT((dateVal1 < dateVal2) ? dateVal1 : dateVal2);
242 /* Compute difference between two dates in days.
245 date_mi(PG_FUNCTION_ARGS)
247 DateADT dateVal1 = PG_GETARG_DATEADT(0);
248 DateADT dateVal2 = PG_GETARG_DATEADT(1);
250 PG_RETURN_INT32((int32) (dateVal1 - dateVal2));
253 /* Add a number of days to a date, giving a new date.
254 * Must handle both positive and negative numbers of days.
257 date_pli(PG_FUNCTION_ARGS)
259 DateADT dateVal = PG_GETARG_DATEADT(0);
260 int32 days = PG_GETARG_INT32(1);
262 PG_RETURN_DATEADT(dateVal + days);
265 /* Subtract a number of days from a date, giving a new date.
268 date_mii(PG_FUNCTION_ARGS)
270 DateADT dateVal = PG_GETARG_DATEADT(0);
271 int32 days = PG_GETARG_INT32(1);
273 PG_RETURN_DATEADT(dateVal - days);
277 * Internal routines for promoting date to timestamp and timestamp with
281 #ifdef HAVE_INT64_TIMESTAMP
282 /* date is days since 2000, timestamp is microseconds since same... */
283 #define date2timestamp(dateVal) \
284 ((Timestamp) ((dateVal) * INT64CONST(86400000000)))
286 /* date is days since 2000, timestamp is seconds since same... */
287 #define date2timestamp(dateVal) \
288 ((Timestamp) ((dateVal) * 86400.0))
292 date2timestamptz(DateADT dateVal)
298 j2date(dateVal + POSTGRES_EPOCH_JDATE,
299 &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
301 if (IS_VALID_UTIME(tm->tm_year, tm->tm_mon, tm->tm_mday))
308 tz = DetermineLocalTimeZone(tm);
310 #ifdef HAVE_INT64_TIMESTAMP
311 result = (dateVal * INT64CONST(86400000000))
312 + (tz * INT64CONST(1000000));
314 result = dateVal * 86400.0 + tz;
319 /* Outside of range for timezone support, so assume UTC */
320 #ifdef HAVE_INT64_TIMESTAMP
321 result = (dateVal * INT64CONST(86400000000));
323 result = dateVal * 86400.0;
332 * Crosstype comparison functions for dates
336 date_eq_timestamp(PG_FUNCTION_ARGS)
338 DateADT dateVal = PG_GETARG_DATEADT(0);
339 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
342 dt1 = date2timestamp(dateVal);
344 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
348 date_ne_timestamp(PG_FUNCTION_ARGS)
350 DateADT dateVal = PG_GETARG_DATEADT(0);
351 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
354 dt1 = date2timestamp(dateVal);
356 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
360 date_lt_timestamp(PG_FUNCTION_ARGS)
362 DateADT dateVal = PG_GETARG_DATEADT(0);
363 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
366 dt1 = date2timestamp(dateVal);
368 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
372 date_gt_timestamp(PG_FUNCTION_ARGS)
374 DateADT dateVal = PG_GETARG_DATEADT(0);
375 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
378 dt1 = date2timestamp(dateVal);
380 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
384 date_le_timestamp(PG_FUNCTION_ARGS)
386 DateADT dateVal = PG_GETARG_DATEADT(0);
387 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
390 dt1 = date2timestamp(dateVal);
392 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
396 date_ge_timestamp(PG_FUNCTION_ARGS)
398 DateADT dateVal = PG_GETARG_DATEADT(0);
399 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
402 dt1 = date2timestamp(dateVal);
404 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
408 date_cmp_timestamp(PG_FUNCTION_ARGS)
410 DateADT dateVal = PG_GETARG_DATEADT(0);
411 Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
414 dt1 = date2timestamp(dateVal);
416 PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2));
420 date_eq_timestamptz(PG_FUNCTION_ARGS)
422 DateADT dateVal = PG_GETARG_DATEADT(0);
423 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
426 dt1 = date2timestamptz(dateVal);
428 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) == 0);
432 date_ne_timestamptz(PG_FUNCTION_ARGS)
434 DateADT dateVal = PG_GETARG_DATEADT(0);
435 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
438 dt1 = date2timestamptz(dateVal);
440 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) != 0);
444 date_lt_timestamptz(PG_FUNCTION_ARGS)
446 DateADT dateVal = PG_GETARG_DATEADT(0);
447 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
450 dt1 = date2timestamptz(dateVal);
452 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) < 0);
456 date_gt_timestamptz(PG_FUNCTION_ARGS)
458 DateADT dateVal = PG_GETARG_DATEADT(0);
459 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
462 dt1 = date2timestamptz(dateVal);
464 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) > 0);
468 date_le_timestamptz(PG_FUNCTION_ARGS)
470 DateADT dateVal = PG_GETARG_DATEADT(0);
471 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
474 dt1 = date2timestamptz(dateVal);
476 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) <= 0);
480 date_ge_timestamptz(PG_FUNCTION_ARGS)
482 DateADT dateVal = PG_GETARG_DATEADT(0);
483 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
486 dt1 = date2timestamptz(dateVal);
488 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) >= 0);
492 date_cmp_timestamptz(PG_FUNCTION_ARGS)
494 DateADT dateVal = PG_GETARG_DATEADT(0);
495 TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
498 dt1 = date2timestamptz(dateVal);
500 PG_RETURN_INT32(timestamptz_cmp_internal(dt1, dt2));
504 timestamp_eq_date(PG_FUNCTION_ARGS)
506 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
507 DateADT dateVal = PG_GETARG_DATEADT(1);
510 dt2 = date2timestamp(dateVal);
512 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
516 timestamp_ne_date(PG_FUNCTION_ARGS)
518 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
519 DateADT dateVal = PG_GETARG_DATEADT(1);
522 dt2 = date2timestamp(dateVal);
524 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
528 timestamp_lt_date(PG_FUNCTION_ARGS)
530 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
531 DateADT dateVal = PG_GETARG_DATEADT(1);
534 dt2 = date2timestamp(dateVal);
536 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
540 timestamp_gt_date(PG_FUNCTION_ARGS)
542 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
543 DateADT dateVal = PG_GETARG_DATEADT(1);
546 dt2 = date2timestamp(dateVal);
548 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
552 timestamp_le_date(PG_FUNCTION_ARGS)
554 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
555 DateADT dateVal = PG_GETARG_DATEADT(1);
558 dt2 = date2timestamp(dateVal);
560 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
564 timestamp_ge_date(PG_FUNCTION_ARGS)
566 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
567 DateADT dateVal = PG_GETARG_DATEADT(1);
570 dt2 = date2timestamp(dateVal);
572 PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
576 timestamp_cmp_date(PG_FUNCTION_ARGS)
578 Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
579 DateADT dateVal = PG_GETARG_DATEADT(1);
582 dt2 = date2timestamp(dateVal);
584 PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2));
588 timestamptz_eq_date(PG_FUNCTION_ARGS)
590 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
591 DateADT dateVal = PG_GETARG_DATEADT(1);
594 dt2 = date2timestamptz(dateVal);
596 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) == 0);
600 timestamptz_ne_date(PG_FUNCTION_ARGS)
602 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
603 DateADT dateVal = PG_GETARG_DATEADT(1);
606 dt2 = date2timestamptz(dateVal);
608 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) != 0);
612 timestamptz_lt_date(PG_FUNCTION_ARGS)
614 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
615 DateADT dateVal = PG_GETARG_DATEADT(1);
618 dt2 = date2timestamptz(dateVal);
620 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) < 0);
624 timestamptz_gt_date(PG_FUNCTION_ARGS)
626 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
627 DateADT dateVal = PG_GETARG_DATEADT(1);
630 dt2 = date2timestamptz(dateVal);
632 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) > 0);
636 timestamptz_le_date(PG_FUNCTION_ARGS)
638 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
639 DateADT dateVal = PG_GETARG_DATEADT(1);
642 dt2 = date2timestamptz(dateVal);
644 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) <= 0);
648 timestamptz_ge_date(PG_FUNCTION_ARGS)
650 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
651 DateADT dateVal = PG_GETARG_DATEADT(1);
654 dt2 = date2timestamptz(dateVal);
656 PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) >= 0);
660 timestamptz_cmp_date(PG_FUNCTION_ARGS)
662 TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
663 DateADT dateVal = PG_GETARG_DATEADT(1);
666 dt2 = date2timestamptz(dateVal);
668 PG_RETURN_INT32(timestamptz_cmp_internal(dt1, dt2));
672 /* Add an interval to a date, giving a new date.
673 * Must handle both positive and negative intervals.
675 * We implement this by promoting the date to timestamp (without time zone)
676 * and then using the timestamp plus interval function.
679 date_pl_interval(PG_FUNCTION_ARGS)
681 DateADT dateVal = PG_GETARG_DATEADT(0);
682 Interval *span = PG_GETARG_INTERVAL_P(1);
685 dateStamp = date2timestamp(dateVal);
687 return DirectFunctionCall2(timestamp_pl_interval,
688 TimestampGetDatum(dateStamp),
689 PointerGetDatum(span));
692 /* Subtract an interval from a date, giving a new date.
693 * Must handle both positive and negative intervals.
695 * We implement this by promoting the date to timestamp (without time zone)
696 * and then using the timestamp minus interval function.
699 date_mi_interval(PG_FUNCTION_ARGS)
701 DateADT dateVal = PG_GETARG_DATEADT(0);
702 Interval *span = PG_GETARG_INTERVAL_P(1);
705 dateStamp = date2timestamp(dateVal);
707 return DirectFunctionCall2(timestamp_mi_interval,
708 TimestampGetDatum(dateStamp),
709 PointerGetDatum(span));
713 * Convert date to timestamp data type.
716 date_timestamp(PG_FUNCTION_ARGS)
718 DateADT dateVal = PG_GETARG_DATEADT(0);
721 result = date2timestamp(dateVal);
723 PG_RETURN_TIMESTAMP(result);
728 * Convert timestamp to date data type.
731 timestamp_date(PG_FUNCTION_ARGS)
733 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
739 if (TIMESTAMP_NOT_FINITE(timestamp))
742 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) != 0)
744 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
745 errmsg("timestamp out of range")));
747 result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
749 PG_RETURN_DATEADT(result);
753 /* date_timestamptz()
754 * Convert date to timestamp with time zone data type.
757 date_timestamptz(PG_FUNCTION_ARGS)
759 DateADT dateVal = PG_GETARG_DATEADT(0);
762 result = date2timestamptz(dateVal);
764 PG_RETURN_TIMESTAMP(result);
768 /* timestamptz_date()
769 * Convert timestamp with time zone to date data type.
772 timestamptz_date(PG_FUNCTION_ARGS)
774 TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
782 if (TIMESTAMP_NOT_FINITE(timestamp))
785 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) != 0)
787 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
788 errmsg("timestamp out of range")));
790 result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
792 PG_RETURN_DATEADT(result);
797 * Convert abstime to date data type.
800 abstime_date(PG_FUNCTION_ARGS)
802 AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
810 case INVALID_ABSTIME:
811 case NOSTART_ABSTIME:
814 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
815 errmsg("cannot convert reserved abstime value to date")));
818 * pretend to drop through to make compiler think that result
823 abstime2tm(abstime, &tz, tm, NULL);
824 result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
828 PG_RETURN_DATEADT(result);
833 * Convert date to text data type.
836 date_text(PG_FUNCTION_ARGS)
838 /* Input is a Date, but may as well leave it in Datum form */
839 Datum date = PG_GETARG_DATUM(0);
844 str = DatumGetCString(DirectFunctionCall1(date_out, date));
846 len = (strlen(str) + VARHDRSZ);
848 result = palloc(len);
850 VARATT_SIZEP(result) = len;
851 memmove(VARDATA(result), str, (len - VARHDRSZ));
855 PG_RETURN_TEXT_P(result);
860 * Convert text string to date.
861 * Text type is not null terminated, so use temporary string
862 * then call the standard input routine.
865 text_date(PG_FUNCTION_ARGS)
867 text *str = PG_GETARG_TEXT_P(0);
871 dstr[MAXDATELEN + 1];
873 if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
875 (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
876 errmsg("invalid input syntax for type date: \"%s\"",
881 for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++)
885 return DirectFunctionCall1(date_in,
886 CStringGetDatum(dstr));
890 /*****************************************************************************
892 *****************************************************************************/
895 time_in(PG_FUNCTION_ARGS)
897 char *str = PG_GETARG_CSTRING(0);
900 Oid typelem = PG_GETARG_OID(1);
902 int32 typmod = PG_GETARG_INT32(2);
910 char lowstr[MAXDATELEN + 1];
911 char *field[MAXDATEFIELDS];
913 int ftype[MAXDATEFIELDS];
915 if (strlen(str) >= sizeof(lowstr))
916 dterr = DTERR_BAD_FORMAT;
918 dterr = ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf);
920 dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
922 DateTimeParseError(dterr, str, "time");
924 tm2time(tm, fsec, &result);
925 AdjustTimeForTypmod(&result, typmod);
927 PG_RETURN_TIMEADT(result);
931 * Convert a tm structure to a time data type.
934 tm2time(struct tm * tm, fsec_t fsec, TimeADT *result)
936 #ifdef HAVE_INT64_TIMESTAMP
937 *result = ((((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec)
938 * INT64CONST(1000000)) + fsec);
940 *result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
946 * Convert time data type to POSIX time structure.
947 * For dates within the system-supported time_t range, convert to the
948 * local time zone. If out of this range, leave as GMT. - tgl 97/05/27
951 time2tm(TimeADT time, struct tm * tm, fsec_t *fsec)
953 #ifdef HAVE_INT64_TIMESTAMP
954 tm->tm_hour = (time / INT64CONST(3600000000));
955 time -= (tm->tm_hour * INT64CONST(3600000000));
956 tm->tm_min = (time / INT64CONST(60000000));
957 time -= (tm->tm_min * INT64CONST(60000000));
958 tm->tm_sec = (time / INT64CONST(1000000));
959 time -= (tm->tm_sec * INT64CONST(1000000));
965 TMODULO(trem, tm->tm_hour, 3600e0);
966 TMODULO(trem, tm->tm_min, 60e0);
967 TMODULO(trem, tm->tm_sec, 1e0);
975 time_out(PG_FUNCTION_ARGS)
977 TimeADT time = PG_GETARG_TIMEADT(0);
982 char buf[MAXDATELEN + 1];
984 time2tm(time, tm, &fsec);
985 EncodeTimeOnly(tm, fsec, NULL, DateStyle, buf);
987 result = pstrdup(buf);
988 PG_RETURN_CSTRING(result);
992 * time_recv - converts external binary format to time
994 * We make no attempt to provide compatibility between int and float
995 * time representations ...
998 time_recv(PG_FUNCTION_ARGS)
1000 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
1002 #ifdef HAVE_INT64_TIMESTAMP
1003 PG_RETURN_TIMEADT((TimeADT) pq_getmsgint64(buf));
1005 PG_RETURN_TIMEADT((TimeADT) pq_getmsgfloat8(buf));
1010 * time_send - converts time to binary format
1013 time_send(PG_FUNCTION_ARGS)
1015 TimeADT time = PG_GETARG_TIMEADT(0);
1018 pq_begintypsend(&buf);
1019 #ifdef HAVE_INT64_TIMESTAMP
1020 pq_sendint64(&buf, time);
1022 pq_sendfloat8(&buf, time);
1024 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
1029 * Adjust time type for specified scale factor.
1030 * Used by PostgreSQL type system to stuff columns.
1033 time_scale(PG_FUNCTION_ARGS)
1035 TimeADT time = PG_GETARG_TIMEADT(0);
1036 int32 typmod = PG_GETARG_INT32(1);
1040 AdjustTimeForTypmod(&result, typmod);
1042 PG_RETURN_TIMEADT(result);
1045 /* AdjustTimeForTypmod()
1046 * Force the precision of the time value to a specified value.
1047 * Uses *exactly* the same code as in AdjustTimestampForTypemod()
1048 * but we make a separate copy because those types do not
1049 * have a fundamental tie together but rather a coincidence of
1050 * implementation. - thomas
1053 AdjustTimeForTypmod(TimeADT *time, int32 typmod)
1055 #ifdef HAVE_INT64_TIMESTAMP
1056 static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1057 INT64CONST(1000000),
1066 static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1077 /* note MAX_TIME_PRECISION differs in this case */
1078 static const double TimeScales[MAX_TIME_PRECISION + 1] = {
1093 if ((typmod >= 0) && (typmod <= MAX_TIME_PRECISION))
1096 * Note: this round-to-nearest code is not completely consistent
1097 * about rounding values that are exactly halfway between integral
1098 * values. On most platforms, rint() will implement
1099 * round-to-nearest-even, but the integer code always rounds up
1100 * (away from zero). Is it worth trying to be consistent?
1102 #ifdef HAVE_INT64_TIMESTAMP
1103 if (*time >= INT64CONST(0))
1105 *time = (((*time + TimeOffsets[typmod]) / TimeScales[typmod])
1106 * TimeScales[typmod]);
1110 *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod])
1111 * TimeScales[typmod]);
1114 *time = (rint(((double) *time) * TimeScales[typmod])
1115 / TimeScales[typmod]);
1122 time_eq(PG_FUNCTION_ARGS)
1124 TimeADT time1 = PG_GETARG_TIMEADT(0);
1125 TimeADT time2 = PG_GETARG_TIMEADT(1);
1127 PG_RETURN_BOOL(time1 == time2);
1131 time_ne(PG_FUNCTION_ARGS)
1133 TimeADT time1 = PG_GETARG_TIMEADT(0);
1134 TimeADT time2 = PG_GETARG_TIMEADT(1);
1136 PG_RETURN_BOOL(time1 != time2);
1140 time_lt(PG_FUNCTION_ARGS)
1142 TimeADT time1 = PG_GETARG_TIMEADT(0);
1143 TimeADT time2 = PG_GETARG_TIMEADT(1);
1145 PG_RETURN_BOOL(time1 < time2);
1149 time_le(PG_FUNCTION_ARGS)
1151 TimeADT time1 = PG_GETARG_TIMEADT(0);
1152 TimeADT time2 = PG_GETARG_TIMEADT(1);
1154 PG_RETURN_BOOL(time1 <= time2);
1158 time_gt(PG_FUNCTION_ARGS)
1160 TimeADT time1 = PG_GETARG_TIMEADT(0);
1161 TimeADT time2 = PG_GETARG_TIMEADT(1);
1163 PG_RETURN_BOOL(time1 > time2);
1167 time_ge(PG_FUNCTION_ARGS)
1169 TimeADT time1 = PG_GETARG_TIMEADT(0);
1170 TimeADT time2 = PG_GETARG_TIMEADT(1);
1172 PG_RETURN_BOOL(time1 >= time2);
1176 time_cmp(PG_FUNCTION_ARGS)
1178 TimeADT time1 = PG_GETARG_TIMEADT(0);
1179 TimeADT time2 = PG_GETARG_TIMEADT(1);
1182 PG_RETURN_INT32(-1);
1189 time_larger(PG_FUNCTION_ARGS)
1191 TimeADT time1 = PG_GETARG_TIMEADT(0);
1192 TimeADT time2 = PG_GETARG_TIMEADT(1);
1194 PG_RETURN_TIMEADT((time1 > time2) ? time1 : time2);
1198 time_smaller(PG_FUNCTION_ARGS)
1200 TimeADT time1 = PG_GETARG_TIMEADT(0);
1201 TimeADT time2 = PG_GETARG_TIMEADT(1);
1203 PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
1206 /* overlaps_time() --- implements the SQL92 OVERLAPS operator.
1208 * Algorithm is per SQL92 spec. This is much harder than you'd think
1209 * because the spec requires us to deliver a non-null answer in some cases
1210 * where some of the inputs are null.
1213 overlaps_time(PG_FUNCTION_ARGS)
1216 * The arguments are TimeADT, but we leave them as generic Datums to
1217 * avoid dereferencing nulls (TimeADT is pass-by-reference!)
1219 Datum ts1 = PG_GETARG_DATUM(0);
1220 Datum te1 = PG_GETARG_DATUM(1);
1221 Datum ts2 = PG_GETARG_DATUM(2);
1222 Datum te2 = PG_GETARG_DATUM(3);
1223 bool ts1IsNull = PG_ARGISNULL(0);
1224 bool te1IsNull = PG_ARGISNULL(1);
1225 bool ts2IsNull = PG_ARGISNULL(2);
1226 bool te2IsNull = PG_ARGISNULL(3);
1228 #define TIMEADT_GT(t1,t2) \
1229 (DatumGetTimeADT(t1) > DatumGetTimeADT(t2))
1230 #define TIMEADT_LT(t1,t2) \
1231 (DatumGetTimeADT(t1) < DatumGetTimeADT(t2))
1234 * If both endpoints of interval 1 are null, the result is null
1235 * (unknown). If just one endpoint is null, take ts1 as the non-null
1236 * one. Otherwise, take ts1 as the lesser endpoint.
1242 /* swap null for non-null */
1246 else if (!te1IsNull)
1248 if (TIMEADT_GT(ts1, te1))
1257 /* Likewise for interval 2. */
1262 /* swap null for non-null */
1266 else if (!te2IsNull)
1268 if (TIMEADT_GT(ts2, te2))
1278 * At this point neither ts1 nor ts2 is null, so we can consider three
1279 * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
1281 if (TIMEADT_GT(ts1, ts2))
1284 * This case is ts1 < te2 OR te1 < te2, which may look redundant
1285 * but in the presence of nulls it's not quite completely so.
1289 if (TIMEADT_LT(ts1, te2))
1290 PG_RETURN_BOOL(true);
1295 * If te1 is not null then we had ts1 <= te1 above, and we just
1296 * found ts1 >= te2, hence te1 >= te2.
1298 PG_RETURN_BOOL(false);
1300 else if (TIMEADT_LT(ts1, ts2))
1302 /* This case is ts2 < te1 OR te2 < te1 */
1305 if (TIMEADT_LT(ts2, te1))
1306 PG_RETURN_BOOL(true);
1311 * If te2 is not null then we had ts2 <= te2 above, and we just
1312 * found ts2 >= te1, hence te2 >= te1.
1314 PG_RETURN_BOOL(false);
1319 * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
1320 * rather silly way of saying "true if both are nonnull, else
1323 if (te1IsNull || te2IsNull)
1325 PG_RETURN_BOOL(true);
1333 * Convert timestamp to time data type.
1336 timestamp_time(PG_FUNCTION_ARGS)
1338 Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
1344 if (TIMESTAMP_NOT_FINITE(timestamp))
1347 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) != 0)
1349 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1350 errmsg("timestamp out of range")));
1352 #ifdef HAVE_INT64_TIMESTAMP
1355 * Could also do this with time = (timestamp / 86400000000 *
1356 * 86400000000) - timestamp;
1358 result = ((((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec)
1359 * INT64CONST(1000000)) + fsec);
1361 result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
1364 PG_RETURN_TIMEADT(result);
1367 /* timestamptz_time()
1368 * Convert timestamptz to time data type.
1371 timestamptz_time(PG_FUNCTION_ARGS)
1373 TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
1381 if (TIMESTAMP_NOT_FINITE(timestamp))
1384 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) != 0)
1386 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1387 errmsg("timestamp out of range")));
1389 #ifdef HAVE_INT64_TIMESTAMP
1392 * Could also do this with time = (timestamp / 86400000000 *
1393 * 86400000000) - timestamp;
1395 result = ((((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec)
1396 * INT64CONST(1000000)) + fsec);
1398 result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
1401 PG_RETURN_TIMEADT(result);
1404 /* datetime_timestamp()
1405 * Convert date and time to timestamp data type.
1408 datetime_timestamp(PG_FUNCTION_ARGS)
1410 DateADT date = PG_GETARG_DATEADT(0);
1411 TimeADT time = PG_GETARG_TIMEADT(1);
1414 result = DatumGetTimestamp(DirectFunctionCall1(date_timestamp,
1415 DateADTGetDatum(date)));
1418 PG_RETURN_TIMESTAMP(result);
1422 * Convert time to interval data type.
1425 time_interval(PG_FUNCTION_ARGS)
1427 TimeADT time = PG_GETARG_TIMEADT(0);
1430 result = (Interval *) palloc(sizeof(Interval));
1432 result->time = time;
1435 PG_RETURN_INTERVAL_P(result);
1439 * Convert interval to time data type.
1441 * This is defined as producing the fractional-day portion of the interval.
1442 * Therefore, we can just ignore the months field. It is not real clear
1443 * what to do with negative intervals, but we choose to subtract the floor,
1444 * so that, say, '-2 hours' becomes '22:00:00'.
1447 interval_time(PG_FUNCTION_ARGS)
1449 Interval *span = PG_GETARG_INTERVAL_P(0);
1452 #ifdef HAVE_INT64_TIMESTAMP
1455 result = span->time;
1456 if (result >= INT64CONST(86400000000))
1458 days = result / INT64CONST(86400000000);
1459 result -= days * INT64CONST(86400000000);
1461 else if (result < 0)
1463 days = (-result + INT64CONST(86400000000 - 1)) / INT64CONST(86400000000);
1464 result += days * INT64CONST(86400000000);
1467 result = span->time;
1468 if (result >= 86400e0 || result < 0)
1469 result -= floor(result / 86400e0) * 86400e0;
1472 PG_RETURN_TIMEADT(result);
1476 * Subtract two times to produce an interval.
1479 time_mi_time(PG_FUNCTION_ARGS)
1481 TimeADT time1 = PG_GETARG_TIMEADT(0);
1482 TimeADT time2 = PG_GETARG_TIMEADT(1);
1485 result = (Interval *) palloc(sizeof(Interval));
1487 result->time = (time1 - time2);
1490 PG_RETURN_INTERVAL_P(result);
1493 /* time_pl_interval()
1494 * Add interval to time.
1497 time_pl_interval(PG_FUNCTION_ARGS)
1499 TimeADT time = PG_GETARG_TIMEADT(0);
1500 Interval *span = PG_GETARG_INTERVAL_P(1);
1503 #ifdef HAVE_INT64_TIMESTAMP
1504 result = (time + span->time);
1505 result -= (result / INT64CONST(86400000000) * INT64CONST(86400000000));
1506 if (result < INT64CONST(0))
1507 result += INT64CONST(86400000000);
1511 result = (time + span->time);
1512 TMODULO(result, time1, 86400e0);
1517 PG_RETURN_TIMEADT(result);
1520 /* time_mi_interval()
1521 * Subtract interval from time.
1524 time_mi_interval(PG_FUNCTION_ARGS)
1526 TimeADT time = PG_GETARG_TIMEADT(0);
1527 Interval *span = PG_GETARG_INTERVAL_P(1);
1530 #ifdef HAVE_INT64_TIMESTAMP
1531 result = (time - span->time);
1532 result -= (result / INT64CONST(86400000000) * INT64CONST(86400000000));
1533 if (result < INT64CONST(0))
1534 result += INT64CONST(86400000000);
1538 result = (time - span->time);
1539 TMODULO(result, time1, 86400e0);
1544 PG_RETURN_TIMEADT(result);
1547 /* interval_pl_time()
1548 * Add time to interval.
1551 interval_pl_time(PG_FUNCTION_ARGS)
1553 Datum span = PG_GETARG_DATUM(0);
1554 Datum time = PG_GETARG_DATUM(1);
1556 return DirectFunctionCall2(time_pl_interval, time, span);
1561 * Convert time to text data type.
1564 time_text(PG_FUNCTION_ARGS)
1566 /* Input is a Time, but may as well leave it in Datum form */
1567 Datum time = PG_GETARG_DATUM(0);
1572 str = DatumGetCString(DirectFunctionCall1(time_out, time));
1574 len = (strlen(str) + VARHDRSZ);
1576 result = palloc(len);
1578 VARATT_SIZEP(result) = len;
1579 memmove(VARDATA(result), str, (len - VARHDRSZ));
1583 PG_RETURN_TEXT_P(result);
1588 * Convert text string to time.
1589 * Text type is not null terminated, so use temporary string
1590 * then call the standard input routine.
1593 text_time(PG_FUNCTION_ARGS)
1595 text *str = PG_GETARG_TEXT_P(0);
1599 dstr[MAXDATELEN + 1];
1601 if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
1603 (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
1604 errmsg("invalid input syntax for type time: \"%s\"",
1609 for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++)
1613 return DirectFunctionCall3(time_in,
1614 CStringGetDatum(dstr),
1615 ObjectIdGetDatum(InvalidOid),
1620 * Extract specified field from time type.
1623 time_part(PG_FUNCTION_ARGS)
1625 text *units = PG_GETARG_TEXT_P(0);
1626 TimeADT time = PG_GETARG_TIMEADT(1);
1633 lowunits[MAXDATELEN + 1];
1635 if (VARSIZE(units) - VARHDRSZ > MAXDATELEN)
1637 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1638 errmsg("\"time\" units \"%s\" not recognized",
1639 DatumGetCString(DirectFunctionCall1(textout,
1640 PointerGetDatum(units))))));
1642 up = VARDATA(units);
1644 for (i = 0; i < (VARSIZE(units) - VARHDRSZ); i++)
1645 *lp++ = tolower((unsigned char) *up++);
1648 type = DecodeUnits(0, lowunits, &val);
1649 if (type == UNKNOWN_FIELD)
1650 type = DecodeSpecial(0, lowunits, &val);
1658 time2tm(time, tm, &fsec);
1663 #ifdef HAVE_INT64_TIMESTAMP
1664 result = ((tm->tm_sec * INT64CONST(1000000)) + fsec);
1666 result = ((tm->tm_sec + fsec) * 1000000);
1671 #ifdef HAVE_INT64_TIMESTAMP
1672 result = ((tm->tm_sec * INT64CONST(1000))
1673 + (fsec / INT64CONST(1000)));
1675 result = ((tm->tm_sec + fsec) * 1000);
1680 #ifdef HAVE_INT64_TIMESTAMP
1681 result = (tm->tm_sec + (fsec / INT64CONST(1000000)));
1683 result = (tm->tm_sec + fsec);
1688 result = tm->tm_min;
1692 result = tm->tm_hour;
1704 case DTK_MILLENNIUM:
1707 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1708 errmsg("\"time\" units \"%s\" not recognized",
1709 DatumGetCString(DirectFunctionCall1(textout,
1710 PointerGetDatum(units))))));
1715 else if ((type == RESERV) && (val == DTK_EPOCH))
1717 #ifdef HAVE_INT64_TIMESTAMP
1718 result = (time / 1000000e0);
1726 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1727 errmsg("\"time\" units \"%s\" not recognized",
1728 DatumGetCString(DirectFunctionCall1(textout,
1729 PointerGetDatum(units))))));
1733 PG_RETURN_FLOAT8(result);
1737 /*****************************************************************************
1738 * Time With Time Zone ADT
1739 *****************************************************************************/
1742 * Convert a tm structure to a time data type.
1745 tm2timetz(struct tm * tm, fsec_t fsec, int tz, TimeTzADT *result)
1747 #ifdef HAVE_INT64_TIMESTAMP
1748 result->time = ((((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec)
1749 * INT64CONST(1000000)) + fsec);
1751 result->time = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
1759 timetz_in(PG_FUNCTION_ARGS)
1761 char *str = PG_GETARG_CSTRING(0);
1764 Oid typelem = PG_GETARG_OID(1);
1766 int32 typmod = PG_GETARG_INT32(2);
1774 char lowstr[MAXDATELEN + 1];
1775 char *field[MAXDATEFIELDS];
1777 int ftype[MAXDATEFIELDS];
1779 if (strlen(str) >= sizeof(lowstr))
1780 dterr = DTERR_BAD_FORMAT;
1782 dterr = ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf);
1784 dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
1786 DateTimeParseError(dterr, str, "time with time zone");
1788 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
1789 tm2timetz(tm, fsec, tz, result);
1790 AdjustTimeForTypmod(&(result->time), typmod);
1792 PG_RETURN_TIMETZADT_P(result);
1796 timetz_out(PG_FUNCTION_ARGS)
1798 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
1804 char buf[MAXDATELEN + 1];
1806 timetz2tm(time, tm, &fsec, &tz);
1807 EncodeTimeOnly(tm, fsec, &tz, DateStyle, buf);
1809 result = pstrdup(buf);
1810 PG_RETURN_CSTRING(result);
1814 * timetz_recv - converts external binary format to timetz
1817 timetz_recv(PG_FUNCTION_ARGS)
1819 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
1822 time = (TimeTzADT *) palloc(sizeof(TimeTzADT));
1824 #ifdef HAVE_INT64_TIMESTAMP
1825 time->time = pq_getmsgint64(buf);
1827 time->time = pq_getmsgfloat8(buf);
1829 time->zone = pq_getmsgint(buf, sizeof(time->zone));
1831 PG_RETURN_TIMETZADT_P(time);
1835 * timetz_send - converts timetz to binary format
1838 timetz_send(PG_FUNCTION_ARGS)
1840 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
1843 pq_begintypsend(&buf);
1844 #ifdef HAVE_INT64_TIMESTAMP
1845 pq_sendint64(&buf, time->time);
1847 pq_sendfloat8(&buf, time->time);
1849 pq_sendint(&buf, time->zone, sizeof(time->zone));
1850 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
1855 * Convert TIME WITH TIME ZONE data type to POSIX time structure.
1858 timetz2tm(TimeTzADT *time, struct tm * tm, fsec_t *fsec, int *tzp)
1860 #ifdef HAVE_INT64_TIMESTAMP
1861 int64 trem = time->time;
1863 tm->tm_hour = (trem / INT64CONST(3600000000));
1864 trem -= (tm->tm_hour * INT64CONST(3600000000));
1865 tm->tm_min = (trem / INT64CONST(60000000));
1866 trem -= (tm->tm_min * INT64CONST(60000000));
1867 tm->tm_sec = (trem / INT64CONST(1000000));
1868 *fsec = (trem - (tm->tm_sec * INT64CONST(1000000)));
1870 double trem = time->time;
1872 TMODULO(trem, tm->tm_hour, 3600e0);
1873 TMODULO(trem, tm->tm_min, 60e0);
1874 TMODULO(trem, tm->tm_sec, 1e0);
1885 * Adjust time type for specified scale factor.
1886 * Used by PostgreSQL type system to stuff columns.
1889 timetz_scale(PG_FUNCTION_ARGS)
1891 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
1892 int32 typmod = PG_GETARG_INT32(1);
1895 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
1897 result->time = time->time;
1898 result->zone = time->zone;
1900 AdjustTimeForTypmod(&(result->time), typmod);
1902 PG_RETURN_TIMETZADT_P(result);
1907 timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
1912 /* Primary sort is by true (GMT-equivalent) time */
1913 t1 = time1->time + time1->zone;
1914 t2 = time2->time + time2->zone;
1922 * If same GMT time, sort by timezone; we only want to say that two
1923 * timetz's are equal if both the time and zone parts are equal.
1925 if (time1->zone > time2->zone)
1927 if (time1->zone < time2->zone)
1934 timetz_eq(PG_FUNCTION_ARGS)
1936 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
1937 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
1939 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) == 0);
1943 timetz_ne(PG_FUNCTION_ARGS)
1945 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
1946 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
1948 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) != 0);
1952 timetz_lt(PG_FUNCTION_ARGS)
1954 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
1955 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
1957 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) < 0);
1961 timetz_le(PG_FUNCTION_ARGS)
1963 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
1964 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
1966 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) <= 0);
1970 timetz_gt(PG_FUNCTION_ARGS)
1972 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
1973 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
1975 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) > 0);
1979 timetz_ge(PG_FUNCTION_ARGS)
1981 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
1982 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
1984 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) >= 0);
1988 timetz_cmp(PG_FUNCTION_ARGS)
1990 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
1991 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
1993 PG_RETURN_INT32(timetz_cmp_internal(time1, time2));
1997 * timetz, being an unusual size, needs a specialized hash function.
2000 timetz_hash(PG_FUNCTION_ARGS)
2002 TimeTzADT *key = PG_GETARG_TIMETZADT_P(0);
2005 * Specify hash length as sizeof(double) + sizeof(int4), not as
2006 * sizeof(TimeTzADT), so that any garbage pad bytes in the structure
2007 * won't be included in the hash!
2009 return hash_any((unsigned char *) key, sizeof(key->time) + sizeof(key->zone));
2013 timetz_larger(PG_FUNCTION_ARGS)
2015 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2016 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2019 if (timetz_cmp_internal(time1, time2) > 0)
2023 PG_RETURN_TIMETZADT_P(result);
2027 timetz_smaller(PG_FUNCTION_ARGS)
2029 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2030 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2033 if (timetz_cmp_internal(time1, time2) < 0)
2037 PG_RETURN_TIMETZADT_P(result);
2040 /* timetz_pl_interval()
2041 * Add interval to timetz.
2044 timetz_pl_interval(PG_FUNCTION_ARGS)
2046 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2047 Interval *span = PG_GETARG_INTERVAL_P(1);
2050 #ifndef HAVE_INT64_TIMESTAMP
2054 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2056 #ifdef HAVE_INT64_TIMESTAMP
2057 result->time = (time->time + span->time);
2058 result->time -= (result->time / INT64CONST(86400000000) * INT64CONST(86400000000));
2059 if (result->time < INT64CONST(0))
2060 result->time += INT64CONST(86400000000);
2062 result->time = (time->time + span->time);
2063 TMODULO(result->time, time1.time, 86400e0);
2064 if (result->time < 0)
2065 result->time += 86400;
2068 result->zone = time->zone;
2070 PG_RETURN_TIMETZADT_P(result);
2073 /* timetz_mi_interval()
2074 * Subtract interval from timetz.
2077 timetz_mi_interval(PG_FUNCTION_ARGS)
2079 TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2080 Interval *span = PG_GETARG_INTERVAL_P(1);
2083 #ifndef HAVE_INT64_TIMESTAMP
2087 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2089 #ifdef HAVE_INT64_TIMESTAMP
2090 result->time = (time->time - span->time);
2091 result->time -= (result->time / INT64CONST(86400000000) * INT64CONST(86400000000));
2092 if (result->time < INT64CONST(0))
2093 result->time += INT64CONST(86400000000);
2095 result->time = (time->time - span->time);
2096 TMODULO(result->time, time1.time, 86400e0);
2097 if (result->time < 0)
2098 result->time += 86400;
2101 result->zone = time->zone;
2103 PG_RETURN_TIMETZADT_P(result);
2106 /* overlaps_timetz() --- implements the SQL92 OVERLAPS operator.
2108 * Algorithm is per SQL92 spec. This is much harder than you'd think
2109 * because the spec requires us to deliver a non-null answer in some cases
2110 * where some of the inputs are null.
2113 overlaps_timetz(PG_FUNCTION_ARGS)
2116 * The arguments are TimeTzADT *, but we leave them as generic Datums
2117 * for convenience of notation --- and to avoid dereferencing nulls.
2119 Datum ts1 = PG_GETARG_DATUM(0);
2120 Datum te1 = PG_GETARG_DATUM(1);
2121 Datum ts2 = PG_GETARG_DATUM(2);
2122 Datum te2 = PG_GETARG_DATUM(3);
2123 bool ts1IsNull = PG_ARGISNULL(0);
2124 bool te1IsNull = PG_ARGISNULL(1);
2125 bool ts2IsNull = PG_ARGISNULL(2);
2126 bool te2IsNull = PG_ARGISNULL(3);
2128 #define TIMETZ_GT(t1,t2) \
2129 DatumGetBool(DirectFunctionCall2(timetz_gt,t1,t2))
2130 #define TIMETZ_LT(t1,t2) \
2131 DatumGetBool(DirectFunctionCall2(timetz_lt,t1,t2))
2134 * If both endpoints of interval 1 are null, the result is null
2135 * (unknown). If just one endpoint is null, take ts1 as the non-null
2136 * one. Otherwise, take ts1 as the lesser endpoint.
2142 /* swap null for non-null */
2146 else if (!te1IsNull)
2148 if (TIMETZ_GT(ts1, te1))
2157 /* Likewise for interval 2. */
2162 /* swap null for non-null */
2166 else if (!te2IsNull)
2168 if (TIMETZ_GT(ts2, te2))
2178 * At this point neither ts1 nor ts2 is null, so we can consider three
2179 * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2181 if (TIMETZ_GT(ts1, ts2))
2184 * This case is ts1 < te2 OR te1 < te2, which may look redundant
2185 * but in the presence of nulls it's not quite completely so.
2189 if (TIMETZ_LT(ts1, te2))
2190 PG_RETURN_BOOL(true);
2195 * If te1 is not null then we had ts1 <= te1 above, and we just
2196 * found ts1 >= te2, hence te1 >= te2.
2198 PG_RETURN_BOOL(false);
2200 else if (TIMETZ_LT(ts1, ts2))
2202 /* This case is ts2 < te1 OR te2 < te1 */
2205 if (TIMETZ_LT(ts2, te1))
2206 PG_RETURN_BOOL(true);
2211 * If te2 is not null then we had ts2 <= te2 above, and we just
2212 * found ts2 >= te1, hence te2 >= te1.
2214 PG_RETURN_BOOL(false);
2219 * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2220 * rather silly way of saying "true if both are nonnull, else
2223 if (te1IsNull || te2IsNull)
2225 PG_RETURN_BOOL(true);
2234 timetz_time(PG_FUNCTION_ARGS)
2236 TimeTzADT *timetz = PG_GETARG_TIMETZADT_P(0);
2239 /* swallow the time zone and just return the time */
2240 result = timetz->time;
2242 PG_RETURN_TIMEADT(result);
2247 time_timetz(PG_FUNCTION_ARGS)
2249 TimeADT time = PG_GETARG_TIMEADT(0);
2256 GetCurrentDateTime(tm);
2257 time2tm(time, tm, &fsec);
2258 tz = DetermineLocalTimeZone(tm);
2260 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2262 result->time = time;
2265 PG_RETURN_TIMETZADT_P(result);
2269 /* timestamptz_timetz()
2270 * Convert timestamp to timetz data type.
2273 timestamptz_timetz(PG_FUNCTION_ARGS)
2275 TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
2283 if (TIMESTAMP_NOT_FINITE(timestamp))
2286 if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) != 0)
2288 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2289 errmsg("timestamp out of range")));
2291 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2293 tm2timetz(tm, fsec, tz, result);
2295 PG_RETURN_TIMETZADT_P(result);
2299 /* datetimetz_timestamptz()
2300 * Convert date and timetz to timestamp with time zone data type.
2301 * Timestamp is stored in GMT, so add the time zone
2302 * stored with the timetz to the result.
2303 * - thomas 2000-03-10
2306 datetimetz_timestamptz(PG_FUNCTION_ARGS)
2308 DateADT date = PG_GETARG_DATEADT(0);
2309 TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2312 #ifdef HAVE_INT64_TIMESTAMP
2313 result = (((date * INT64CONST(86400000000)) + time->time)
2314 + (time->zone * INT64CONST(1000000)));
2316 result = (((date * 86400.0) + time->time) + time->zone);
2319 PG_RETURN_TIMESTAMP(result);
2324 * Convert timetz to text data type.
2327 timetz_text(PG_FUNCTION_ARGS)
2329 /* Input is a Timetz, but may as well leave it in Datum form */
2330 Datum timetz = PG_GETARG_DATUM(0);
2335 str = DatumGetCString(DirectFunctionCall1(timetz_out, timetz));
2337 len = (strlen(str) + VARHDRSZ);
2339 result = palloc(len);
2341 VARATT_SIZEP(result) = len;
2342 memmove(VARDATA(result), str, (len - VARHDRSZ));
2346 PG_RETURN_TEXT_P(result);
2351 * Convert text string to timetz.
2352 * Text type is not null terminated, so use temporary string
2353 * then call the standard input routine.
2356 text_timetz(PG_FUNCTION_ARGS)
2358 text *str = PG_GETARG_TEXT_P(0);
2362 dstr[MAXDATELEN + 1];
2364 if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
2366 (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2367 errmsg("invalid input syntax for type time with time zone: \"%s\"",
2372 for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++)
2376 return DirectFunctionCall3(timetz_in,
2377 CStringGetDatum(dstr),
2378 ObjectIdGetDatum(InvalidOid),
2383 * Extract specified field from time type.
2386 timetz_part(PG_FUNCTION_ARGS)
2388 text *units = PG_GETARG_TEXT_P(0);
2389 TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2396 lowunits[MAXDATELEN + 1];
2398 if (VARSIZE(units) - VARHDRSZ > MAXDATELEN)
2400 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2401 errmsg("\"time with time zone\" units \"%s\" not recognized",
2402 DatumGetCString(DirectFunctionCall1(textout,
2403 PointerGetDatum(units))))));
2405 up = VARDATA(units);
2407 for (i = 0; i < (VARSIZE(units) - VARHDRSZ); i++)
2408 *lp++ = tolower((unsigned char) *up++);
2411 type = DecodeUnits(0, lowunits, &val);
2412 if (type == UNKNOWN_FIELD)
2413 type = DecodeSpecial(0, lowunits, &val);
2423 timetz2tm(time, tm, &fsec, &tz);
2434 FMODULO(result, dummy, 60e0);
2439 FMODULO(dummy, result, 3600e0);
2443 #ifdef HAVE_INT64_TIMESTAMP
2444 result = ((tm->tm_sec * INT64CONST(1000000)) + fsec);
2446 result = ((tm->tm_sec + fsec) * 1000000);
2451 #ifdef HAVE_INT64_TIMESTAMP
2452 result = ((tm->tm_sec * INT64CONST(1000))
2453 + (fsec / INT64CONST(1000)));
2455 result = ((tm->tm_sec + fsec) * 1000);
2460 #ifdef HAVE_INT64_TIMESTAMP
2461 result = (tm->tm_sec + (fsec / INT64CONST(1000000)));
2463 result = (tm->tm_sec + fsec);
2468 result = tm->tm_min;
2472 result = tm->tm_hour;
2481 case DTK_MILLENNIUM:
2484 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2485 errmsg("\"time with time zone\" units \"%s\" not recognized",
2486 DatumGetCString(DirectFunctionCall1(textout,
2487 PointerGetDatum(units))))));
2492 else if ((type == RESERV) && (val == DTK_EPOCH))
2494 #ifdef HAVE_INT64_TIMESTAMP
2495 result = ((time->time / 1000000e0) - time->zone);
2497 result = (time->time - time->zone);
2503 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2504 errmsg("\"time with time zone\" units \"%s\" not recognized",
2505 DatumGetCString(DirectFunctionCall1(textout,
2506 PointerGetDatum(units))))));
2511 PG_RETURN_FLOAT8(result);
2515 * Encode time with time zone type with specified time zone.
2518 timetz_zone(PG_FUNCTION_ARGS)
2520 text *zone = PG_GETARG_TEXT_P(0);
2521 TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2529 lowzone[MAXDATELEN + 1];
2531 if (VARSIZE(zone) - VARHDRSZ > MAXDATELEN)
2533 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2534 errmsg("time zone \"%s\" not recognized",
2535 DatumGetCString(DirectFunctionCall1(textout,
2536 PointerGetDatum(zone))))));
2540 for (i = 0; i < (VARSIZE(zone) - VARHDRSZ); i++)
2541 *lp++ = tolower((unsigned char) *up++);
2544 type = DecodeSpecial(0, lowzone, &val);
2546 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2548 if ((type == TZ) || (type == DTZ))
2551 #ifdef HAVE_INT64_TIMESTAMP
2552 result->time = time->time + ((time->zone - tz) * INT64CONST(1000000));
2553 while (result->time < INT64CONST(0))
2554 result->time += INT64CONST(86400000000);
2555 while (result->time >= INT64CONST(86400000000))
2556 result->time -= INT64CONST(86400000000);
2558 result->time = time->time + (time->zone - tz);
2559 while (result->time < 0)
2560 result->time += 86400;
2561 while (result->time >= 86400)
2562 result->time -= 86400;
2570 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2571 errmsg("time zone \"%s\" not recognized", lowzone)));
2576 PG_RETURN_TIMETZADT_P(result);
2577 } /* timetz_zone() */
2580 * Encode time with time zone type with specified time interval as time zone.
2583 timetz_izone(PG_FUNCTION_ARGS)
2585 Interval *zone = PG_GETARG_INTERVAL_P(0);
2586 TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2590 if (zone->month != 0)
2592 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2593 errmsg("\"interval\" time zone \"%s\" not valid",
2594 DatumGetCString(DirectFunctionCall1(interval_out,
2595 PointerGetDatum(zone))))));
2597 #ifdef HAVE_INT64_TIMESTAMP
2598 tz = -(zone->time / INT64CONST(1000000));
2603 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2605 #ifdef HAVE_INT64_TIMESTAMP
2606 result->time = time->time + ((time->zone - tz) * INT64CONST(1000000));
2607 while (result->time < INT64CONST(0))
2608 result->time += INT64CONST(86400000000);
2609 while (result->time >= INT64CONST(86400000000))
2610 result->time -= INT64CONST(86400000000);
2612 result->time = time->time + (time->zone - tz);
2613 while (result->time < 0)
2614 result->time += 86400;
2615 while (result->time >= 86400)
2616 result->time -= 86400;
2621 PG_RETURN_TIMETZADT_P(result);
2622 } /* timetz_izone() */