PostgreSQL Source Code  git master
date.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * date.c
4  * implements DATE and TIME data types specified in SQL standard
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994-5, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/adt/date.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include <ctype.h>
19 #include <limits.h>
20 #include <float.h>
21 #include <math.h>
22 #include <time.h>
23 
24 #include "access/xact.h"
25 #include "common/hashfn.h"
26 #include "libpq/pqformat.h"
27 #include "miscadmin.h"
28 #include "nodes/supportnodes.h"
29 #include "parser/scansup.h"
30 #include "utils/array.h"
31 #include "utils/builtins.h"
32 #include "utils/date.h"
33 #include "utils/datetime.h"
34 #include "utils/sortsupport.h"
35 
36 /*
37  * gcc's -ffast-math switch breaks routines that expect exact results from
38  * expressions like timeval / SECS_PER_HOUR, where timeval is double.
39  */
40 #ifdef __FAST_MATH__
41 #error -ffast-math is known to break this code
42 #endif
43 
44 
45 /* common code for timetypmodin and timetztypmodin */
46 static int32
47 anytime_typmodin(bool istz, ArrayType *ta)
48 {
49  int32 *tl;
50  int n;
51 
52  tl = ArrayGetIntegerTypmods(ta, &n);
53 
54  /*
55  * we're not too tense about good error message here because grammar
56  * shouldn't allow wrong number of modifiers for TIME
57  */
58  if (n != 1)
59  ereport(ERROR,
60  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
61  errmsg("invalid type modifier")));
62 
63  return anytime_typmod_check(istz, tl[0]);
64 }
65 
66 /* exported so parse_expr.c can use it */
67 int32
68 anytime_typmod_check(bool istz, int32 typmod)
69 {
70  if (typmod < 0)
71  ereport(ERROR,
72  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
73  errmsg("TIME(%d)%s precision must not be negative",
74  typmod, (istz ? " WITH TIME ZONE" : ""))));
75  if (typmod > MAX_TIME_PRECISION)
76  {
78  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
79  errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
80  typmod, (istz ? " WITH TIME ZONE" : ""),
82  typmod = MAX_TIME_PRECISION;
83  }
84 
85  return typmod;
86 }
87 
88 /* common code for timetypmodout and timetztypmodout */
89 static char *
90 anytime_typmodout(bool istz, int32 typmod)
91 {
92  const char *tz = istz ? " with time zone" : " without time zone";
93 
94  if (typmod >= 0)
95  return psprintf("(%d)%s", (int) typmod, tz);
96  else
97  return psprintf("%s", tz);
98 }
99 
100 
101 /*****************************************************************************
102  * Date ADT
103  *****************************************************************************/
104 
105 
106 /* date_in()
107  * Given date text string, convert to internal date format.
108  */
109 Datum
111 {
112  char *str = PG_GETARG_CSTRING(0);
113  DateADT date;
114  fsec_t fsec;
115  struct pg_tm tt,
116  *tm = &tt;
117  int tzp;
118  int dtype;
119  int nf;
120  int dterr;
121  char *field[MAXDATEFIELDS];
122  int ftype[MAXDATEFIELDS];
123  char workbuf[MAXDATELEN + 1];
124 
125  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
126  field, ftype, MAXDATEFIELDS, &nf);
127  if (dterr == 0)
128  dterr = DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp);
129  if (dterr != 0)
130  DateTimeParseError(dterr, str, "date");
131 
132  switch (dtype)
133  {
134  case DTK_DATE:
135  break;
136 
137  case DTK_EPOCH:
138  GetEpochTime(tm);
139  break;
140 
141  case DTK_LATE:
142  DATE_NOEND(date);
143  PG_RETURN_DATEADT(date);
144 
145  case DTK_EARLY:
146  DATE_NOBEGIN(date);
147  PG_RETURN_DATEADT(date);
148 
149  default:
150  DateTimeParseError(DTERR_BAD_FORMAT, str, "date");
151  break;
152  }
153 
154  /* Prevent overflow in Julian-day routines */
155  if (!IS_VALID_JULIAN(tm->tm_year, tm->tm_mon, tm->tm_mday))
156  ereport(ERROR,
157  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
158  errmsg("date out of range: \"%s\"", str)));
159 
160  date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
161 
162  /* Now check for just-out-of-range dates */
163  if (!IS_VALID_DATE(date))
164  ereport(ERROR,
165  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
166  errmsg("date out of range: \"%s\"", str)));
167 
168  PG_RETURN_DATEADT(date);
169 }
170 
171 /* date_out()
172  * Given internal format date, convert to text string.
173  */
174 Datum
176 {
178  char *result;
179  struct pg_tm tt,
180  *tm = &tt;
181  char buf[MAXDATELEN + 1];
182 
183  if (DATE_NOT_FINITE(date))
184  EncodeSpecialDate(date, buf);
185  else
186  {
188  &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
189  EncodeDateOnly(tm, DateStyle, buf);
190  }
191 
192  result = pstrdup(buf);
193  PG_RETURN_CSTRING(result);
194 }
195 
196 /*
197  * date_recv - converts external binary format to date
198  */
199 Datum
201 {
203  DateADT result;
204 
205  result = (DateADT) pq_getmsgint(buf, sizeof(DateADT));
206 
207  /* Limit to the same range that date_in() accepts. */
208  if (DATE_NOT_FINITE(result))
209  /* ok */ ;
210  else if (!IS_VALID_DATE(result))
211  ereport(ERROR,
212  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
213  errmsg("date out of range")));
214 
215  PG_RETURN_DATEADT(result);
216 }
217 
218 /*
219  * date_send - converts date to binary format
220  */
221 Datum
223 {
226 
227  pq_begintypsend(&buf);
228  pq_sendint32(&buf, date);
230 }
231 
232 /*
233  * make_date - date constructor
234  */
235 Datum
237 {
238  struct pg_tm tm;
239  DateADT date;
240  int dterr;
241  bool bc = false;
242 
243  tm.tm_year = PG_GETARG_INT32(0);
244  tm.tm_mon = PG_GETARG_INT32(1);
245  tm.tm_mday = PG_GETARG_INT32(2);
246 
247  /* Handle negative years as BC */
248  if (tm.tm_year < 0)
249  {
250  bc = true;
251  tm.tm_year = -tm.tm_year;
252  }
253 
254  dterr = ValidateDate(DTK_DATE_M, false, false, bc, &tm);
255 
256  if (dterr != 0)
257  ereport(ERROR,
258  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
259  errmsg("date field value out of range: %d-%02d-%02d",
260  tm.tm_year, tm.tm_mon, tm.tm_mday)));
261 
262  /* Prevent overflow in Julian-day routines */
263  if (!IS_VALID_JULIAN(tm.tm_year, tm.tm_mon, tm.tm_mday))
264  ereport(ERROR,
265  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
266  errmsg("date out of range: %d-%02d-%02d",
267  tm.tm_year, tm.tm_mon, tm.tm_mday)));
268 
269  date = date2j(tm.tm_year, tm.tm_mon, tm.tm_mday) - POSTGRES_EPOCH_JDATE;
270 
271  /* Now check for just-out-of-range dates */
272  if (!IS_VALID_DATE(date))
273  ereport(ERROR,
274  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
275  errmsg("date out of range: %d-%02d-%02d",
276  tm.tm_year, tm.tm_mon, tm.tm_mday)));
277 
278  PG_RETURN_DATEADT(date);
279 }
280 
281 /*
282  * Convert reserved date values to string.
283  */
284 void
286 {
287  if (DATE_IS_NOBEGIN(dt))
288  strcpy(str, EARLY);
289  else if (DATE_IS_NOEND(dt))
290  strcpy(str, LATE);
291  else /* shouldn't happen */
292  elog(ERROR, "invalid argument for EncodeSpecialDate");
293 }
294 
295 
296 /*
297  * GetSQLCurrentDate -- implements CURRENT_DATE
298  */
299 DateADT
301 {
302  struct pg_tm tm;
303 
304  static int cache_year = 0;
305  static int cache_mon = 0;
306  static int cache_mday = 0;
307  static DateADT cache_date;
308 
309  GetCurrentDateTime(&tm);
310 
311  /*
312  * date2j involves several integer divisions; moreover, unless our session
313  * lives across local midnight, we don't really have to do it more than
314  * once. So it seems worth having a separate cache here.
315  */
316  if (tm.tm_year != cache_year ||
317  tm.tm_mon != cache_mon ||
318  tm.tm_mday != cache_mday)
319  {
320  cache_date = date2j(tm.tm_year, tm.tm_mon, tm.tm_mday) - POSTGRES_EPOCH_JDATE;
321  cache_year = tm.tm_year;
322  cache_mon = tm.tm_mon;
323  cache_mday = tm.tm_mday;
324  }
325 
326  return cache_date;
327 }
328 
329 /*
330  * GetSQLCurrentTime -- implements CURRENT_TIME, CURRENT_TIME(n)
331  */
332 TimeTzADT *
334 {
335  TimeTzADT *result;
336  struct pg_tm tt,
337  *tm = &tt;
338  fsec_t fsec;
339  int tz;
340 
341  GetCurrentTimeUsec(tm, &fsec, &tz);
342 
343  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
344  tm2timetz(tm, fsec, tz, result);
345  AdjustTimeForTypmod(&(result->time), typmod);
346  return result;
347 }
348 
349 /*
350  * GetSQLLocalTime -- implements LOCALTIME, LOCALTIME(n)
351  */
352 TimeADT
354 {
355  TimeADT result;
356  struct pg_tm tt,
357  *tm = &tt;
358  fsec_t fsec;
359  int tz;
360 
361  GetCurrentTimeUsec(tm, &fsec, &tz);
362 
363  tm2time(tm, fsec, &result);
364  AdjustTimeForTypmod(&result, typmod);
365  return result;
366 }
367 
368 
369 /*
370  * Comparison functions for dates
371  */
372 
373 Datum
375 {
376  DateADT dateVal1 = PG_GETARG_DATEADT(0);
377  DateADT dateVal2 = PG_GETARG_DATEADT(1);
378 
379  PG_RETURN_BOOL(dateVal1 == dateVal2);
380 }
381 
382 Datum
384 {
385  DateADT dateVal1 = PG_GETARG_DATEADT(0);
386  DateADT dateVal2 = PG_GETARG_DATEADT(1);
387 
388  PG_RETURN_BOOL(dateVal1 != dateVal2);
389 }
390 
391 Datum
393 {
394  DateADT dateVal1 = PG_GETARG_DATEADT(0);
395  DateADT dateVal2 = PG_GETARG_DATEADT(1);
396 
397  PG_RETURN_BOOL(dateVal1 < dateVal2);
398 }
399 
400 Datum
402 {
403  DateADT dateVal1 = PG_GETARG_DATEADT(0);
404  DateADT dateVal2 = PG_GETARG_DATEADT(1);
405 
406  PG_RETURN_BOOL(dateVal1 <= dateVal2);
407 }
408 
409 Datum
411 {
412  DateADT dateVal1 = PG_GETARG_DATEADT(0);
413  DateADT dateVal2 = PG_GETARG_DATEADT(1);
414 
415  PG_RETURN_BOOL(dateVal1 > dateVal2);
416 }
417 
418 Datum
420 {
421  DateADT dateVal1 = PG_GETARG_DATEADT(0);
422  DateADT dateVal2 = PG_GETARG_DATEADT(1);
423 
424  PG_RETURN_BOOL(dateVal1 >= dateVal2);
425 }
426 
427 Datum
429 {
430  DateADT dateVal1 = PG_GETARG_DATEADT(0);
431  DateADT dateVal2 = PG_GETARG_DATEADT(1);
432 
433  if (dateVal1 < dateVal2)
434  PG_RETURN_INT32(-1);
435  else if (dateVal1 > dateVal2)
436  PG_RETURN_INT32(1);
437  PG_RETURN_INT32(0);
438 }
439 
440 static int
442 {
443  DateADT a = DatumGetDateADT(x);
444  DateADT b = DatumGetDateADT(y);
445 
446  if (a < b)
447  return -1;
448  else if (a > b)
449  return 1;
450  return 0;
451 }
452 
453 Datum
455 {
457 
458  ssup->comparator = date_fastcmp;
459  PG_RETURN_VOID();
460 }
461 
462 Datum
464 {
466 
468 }
469 
470 Datum
472 {
473  DateADT dateVal1 = PG_GETARG_DATEADT(0);
474  DateADT dateVal2 = PG_GETARG_DATEADT(1);
475 
476  PG_RETURN_DATEADT((dateVal1 > dateVal2) ? dateVal1 : dateVal2);
477 }
478 
479 Datum
481 {
482  DateADT dateVal1 = PG_GETARG_DATEADT(0);
483  DateADT dateVal2 = PG_GETARG_DATEADT(1);
484 
485  PG_RETURN_DATEADT((dateVal1 < dateVal2) ? dateVal1 : dateVal2);
486 }
487 
488 /* Compute difference between two dates in days.
489  */
490 Datum
492 {
493  DateADT dateVal1 = PG_GETARG_DATEADT(0);
494  DateADT dateVal2 = PG_GETARG_DATEADT(1);
495 
496  if (DATE_NOT_FINITE(dateVal1) || DATE_NOT_FINITE(dateVal2))
497  ereport(ERROR,
498  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
499  errmsg("cannot subtract infinite dates")));
500 
501  PG_RETURN_INT32((int32) (dateVal1 - dateVal2));
502 }
503 
504 /* Add a number of days to a date, giving a new date.
505  * Must handle both positive and negative numbers of days.
506  */
507 Datum
509 {
510  DateADT dateVal = PG_GETARG_DATEADT(0);
512  DateADT result;
513 
514  if (DATE_NOT_FINITE(dateVal))
515  PG_RETURN_DATEADT(dateVal); /* can't change infinity */
516 
517  result = dateVal + days;
518 
519  /* Check for integer overflow and out-of-allowed-range */
520  if ((days >= 0 ? (result < dateVal) : (result > dateVal)) ||
521  !IS_VALID_DATE(result))
522  ereport(ERROR,
523  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
524  errmsg("date out of range")));
525 
526  PG_RETURN_DATEADT(result);
527 }
528 
529 /* Subtract a number of days from a date, giving a new date.
530  */
531 Datum
533 {
534  DateADT dateVal = PG_GETARG_DATEADT(0);
536  DateADT result;
537 
538  if (DATE_NOT_FINITE(dateVal))
539  PG_RETURN_DATEADT(dateVal); /* can't change infinity */
540 
541  result = dateVal - days;
542 
543  /* Check for integer overflow and out-of-allowed-range */
544  if ((days >= 0 ? (result > dateVal) : (result < dateVal)) ||
545  !IS_VALID_DATE(result))
546  ereport(ERROR,
547  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
548  errmsg("date out of range")));
549 
550  PG_RETURN_DATEADT(result);
551 }
552 
553 
554 /*
555  * Promote date to timestamp.
556  *
557  * On successful conversion, *overflow is set to zero if it's not NULL.
558  *
559  * If the date is finite but out of the valid range for timestamp, then:
560  * if overflow is NULL, we throw an out-of-range error.
561  * if overflow is not NULL, we store +1 or -1 there to indicate the sign
562  * of the overflow, and return the appropriate timestamp infinity.
563  *
564  * Note: *overflow = -1 is actually not possible currently, since both
565  * datatypes have the same lower bound, Julian day zero.
566  */
567 Timestamp
568 date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
569 {
570  Timestamp result;
571 
572  if (overflow)
573  *overflow = 0;
574 
575  if (DATE_IS_NOBEGIN(dateVal))
576  TIMESTAMP_NOBEGIN(result);
577  else if (DATE_IS_NOEND(dateVal))
578  TIMESTAMP_NOEND(result);
579  else
580  {
581  /*
582  * Since dates have the same minimum values as timestamps, only upper
583  * boundary need be checked for overflow.
584  */
585  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
586  {
587  if (overflow)
588  {
589  *overflow = 1;
590  TIMESTAMP_NOEND(result);
591  return result;
592  }
593  else
594  {
595  ereport(ERROR,
596  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
597  errmsg("date out of range for timestamp")));
598  }
599  }
600 
601  /* date is days since 2000, timestamp is microseconds since same... */
602  result = dateVal * USECS_PER_DAY;
603  }
604 
605  return result;
606 }
607 
608 /*
609  * Promote date to timestamp, throwing error for overflow.
610  */
611 static TimestampTz
613 {
614  return date2timestamp_opt_overflow(dateVal, NULL);
615 }
616 
617 /*
618  * Promote date to timestamp with time zone.
619  *
620  * On successful conversion, *overflow is set to zero if it's not NULL.
621  *
622  * If the date is finite but out of the valid range for timestamptz, then:
623  * if overflow is NULL, we throw an out-of-range error.
624  * if overflow is not NULL, we store +1 or -1 there to indicate the sign
625  * of the overflow, and return the appropriate timestamptz infinity.
626  */
628 date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
629 {
630  TimestampTz result;
631  struct pg_tm tt,
632  *tm = &tt;
633  int tz;
634 
635  if (overflow)
636  *overflow = 0;
637 
638  if (DATE_IS_NOBEGIN(dateVal))
639  TIMESTAMP_NOBEGIN(result);
640  else if (DATE_IS_NOEND(dateVal))
641  TIMESTAMP_NOEND(result);
642  else
643  {
644  /*
645  * Since dates have the same minimum values as timestamps, only upper
646  * boundary need be checked for overflow.
647  */
648  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
649  {
650  if (overflow)
651  {
652  *overflow = 1;
653  TIMESTAMP_NOEND(result);
654  return result;
655  }
656  else
657  {
658  ereport(ERROR,
659  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
660  errmsg("date out of range for timestamp")));
661  }
662  }
663 
664  j2date(dateVal + POSTGRES_EPOCH_JDATE,
665  &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
666  tm->tm_hour = 0;
667  tm->tm_min = 0;
668  tm->tm_sec = 0;
670 
671  result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
672 
673  /*
674  * Since it is possible to go beyond allowed timestamptz range because
675  * of time zone, check for allowed timestamp range after adding tz.
676  */
677  if (!IS_VALID_TIMESTAMP(result))
678  {
679  if (overflow)
680  {
681  if (result < MIN_TIMESTAMP)
682  {
683  *overflow = -1;
684  TIMESTAMP_NOBEGIN(result);
685  }
686  else
687  {
688  *overflow = 1;
689  TIMESTAMP_NOEND(result);
690  }
691  }
692  else
693  {
694  ereport(ERROR,
695  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
696  errmsg("date out of range for timestamp")));
697  }
698  }
699  }
700 
701  return result;
702 }
703 
704 /*
705  * Promote date to timestamptz, throwing error for overflow.
706  */
707 static TimestampTz
709 {
710  return date2timestamptz_opt_overflow(dateVal, NULL);
711 }
712 
713 /*
714  * date2timestamp_no_overflow
715  *
716  * This is chartered to produce a double value that is numerically
717  * equivalent to the corresponding Timestamp value, if the date is in the
718  * valid range of Timestamps, but in any case not throw an overflow error.
719  * We can do this since the numerical range of double is greater than
720  * that of non-erroneous timestamps. The results are currently only
721  * used for statistical estimation purposes.
722  */
723 double
725 {
726  double result;
727 
728  if (DATE_IS_NOBEGIN(dateVal))
729  result = -DBL_MAX;
730  else if (DATE_IS_NOEND(dateVal))
731  result = DBL_MAX;
732  else
733  {
734  /* date is days since 2000, timestamp is microseconds since same... */
735  result = dateVal * (double) USECS_PER_DAY;
736  }
737 
738  return result;
739 }
740 
741 
742 /*
743  * Crosstype comparison functions for dates
744  */
745 
746 int32
748 {
749  Timestamp dt1;
750  int overflow;
751 
752  dt1 = date2timestamp_opt_overflow(dateVal, &overflow);
753  if (overflow > 0)
754  {
755  /* dt1 is larger than any finite timestamp, but less than infinity */
756  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
757  }
758  Assert(overflow == 0); /* -1 case cannot occur */
759 
760  return timestamp_cmp_internal(dt1, dt2);
761 }
762 
763 Datum
765 {
766  DateADT dateVal = PG_GETARG_DATEADT(0);
768 
769  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) == 0);
770 }
771 
772 Datum
774 {
775  DateADT dateVal = PG_GETARG_DATEADT(0);
777 
778  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) != 0);
779 }
780 
781 Datum
783 {
784  DateADT dateVal = PG_GETARG_DATEADT(0);
786 
787  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) < 0);
788 }
789 
790 Datum
792 {
793  DateADT dateVal = PG_GETARG_DATEADT(0);
795 
796  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) > 0);
797 }
798 
799 Datum
801 {
802  DateADT dateVal = PG_GETARG_DATEADT(0);
804 
805  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) <= 0);
806 }
807 
808 Datum
810 {
811  DateADT dateVal = PG_GETARG_DATEADT(0);
813 
814  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) >= 0);
815 }
816 
817 Datum
819 {
820  DateADT dateVal = PG_GETARG_DATEADT(0);
822 
824 }
825 
826 int32
828 {
829  TimestampTz dt1;
830  int overflow;
831 
832  dt1 = date2timestamptz_opt_overflow(dateVal, &overflow);
833  if (overflow > 0)
834  {
835  /* dt1 is larger than any finite timestamp, but less than infinity */
836  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
837  }
838  if (overflow < 0)
839  {
840  /* dt1 is less than any finite timestamp, but more than -infinity */
841  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
842  }
843 
844  return timestamptz_cmp_internal(dt1, dt2);
845 }
846 
847 Datum
849 {
850  DateADT dateVal = PG_GETARG_DATEADT(0);
852 
853  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) == 0);
854 }
855 
856 Datum
858 {
859  DateADT dateVal = PG_GETARG_DATEADT(0);
861 
862  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) != 0);
863 }
864 
865 Datum
867 {
868  DateADT dateVal = PG_GETARG_DATEADT(0);
870 
872 }
873 
874 Datum
876 {
877  DateADT dateVal = PG_GETARG_DATEADT(0);
879 
881 }
882 
883 Datum
885 {
886  DateADT dateVal = PG_GETARG_DATEADT(0);
888 
889  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) <= 0);
890 }
891 
892 Datum
894 {
895  DateADT dateVal = PG_GETARG_DATEADT(0);
897 
898  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) >= 0);
899 }
900 
901 Datum
903 {
904  DateADT dateVal = PG_GETARG_DATEADT(0);
906 
908 }
909 
910 Datum
912 {
914  DateADT dateVal = PG_GETARG_DATEADT(1);
915 
916  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) == 0);
917 }
918 
919 Datum
921 {
923  DateADT dateVal = PG_GETARG_DATEADT(1);
924 
925  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) != 0);
926 }
927 
928 Datum
930 {
932  DateADT dateVal = PG_GETARG_DATEADT(1);
933 
934  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) > 0);
935 }
936 
937 Datum
939 {
941  DateADT dateVal = PG_GETARG_DATEADT(1);
942 
943  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) < 0);
944 }
945 
946 Datum
948 {
950  DateADT dateVal = PG_GETARG_DATEADT(1);
951 
952  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) >= 0);
953 }
954 
955 Datum
957 {
959  DateADT dateVal = PG_GETARG_DATEADT(1);
960 
961  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) <= 0);
962 }
963 
964 Datum
966 {
968  DateADT dateVal = PG_GETARG_DATEADT(1);
969 
971 }
972 
973 Datum
975 {
977  DateADT dateVal = PG_GETARG_DATEADT(1);
978 
979  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) == 0);
980 }
981 
982 Datum
984 {
986  DateADT dateVal = PG_GETARG_DATEADT(1);
987 
988  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) != 0);
989 }
990 
991 Datum
993 {
995  DateADT dateVal = PG_GETARG_DATEADT(1);
996 
998 }
999 
1000 Datum
1002 {
1004  DateADT dateVal = PG_GETARG_DATEADT(1);
1005 
1006  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) < 0);
1007 }
1008 
1009 Datum
1011 {
1013  DateADT dateVal = PG_GETARG_DATEADT(1);
1014 
1015  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) >= 0);
1016 }
1017 
1018 Datum
1020 {
1022  DateADT dateVal = PG_GETARG_DATEADT(1);
1023 
1024  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) <= 0);
1025 }
1026 
1027 Datum
1029 {
1031  DateADT dateVal = PG_GETARG_DATEADT(1);
1032 
1034 }
1035 
1036 /*
1037  * in_range support function for date.
1038  *
1039  * We implement this by promoting the dates to timestamp (without time zone)
1040  * and then using the timestamp-and-interval in_range function.
1041  */
1042 Datum
1044 {
1046  DateADT base = PG_GETARG_DATEADT(1);
1047  Interval *offset = PG_GETARG_INTERVAL_P(2);
1048  bool sub = PG_GETARG_BOOL(3);
1049  bool less = PG_GETARG_BOOL(4);
1050  Timestamp valStamp;
1051  Timestamp baseStamp;
1052 
1053  /* XXX we could support out-of-range cases here, perhaps */
1054  valStamp = date2timestamp(val);
1055  baseStamp = date2timestamp(base);
1056 
1058  TimestampGetDatum(valStamp),
1059  TimestampGetDatum(baseStamp),
1060  IntervalPGetDatum(offset),
1061  BoolGetDatum(sub),
1062  BoolGetDatum(less));
1063 }
1064 
1065 
1066 /* Add an interval to a date, giving a new date.
1067  * Must handle both positive and negative intervals.
1068  *
1069  * We implement this by promoting the date to timestamp (without time zone)
1070  * and then using the timestamp plus interval function.
1071  */
1072 Datum
1074 {
1075  DateADT dateVal = PG_GETARG_DATEADT(0);
1076  Interval *span = PG_GETARG_INTERVAL_P(1);
1077  Timestamp dateStamp;
1078 
1079  dateStamp = date2timestamp(dateVal);
1080 
1082  TimestampGetDatum(dateStamp),
1083  PointerGetDatum(span));
1084 }
1085 
1086 /* Subtract an interval from a date, giving a new date.
1087  * Must handle both positive and negative intervals.
1088  *
1089  * We implement this by promoting the date to timestamp (without time zone)
1090  * and then using the timestamp minus interval function.
1091  */
1092 Datum
1094 {
1095  DateADT dateVal = PG_GETARG_DATEADT(0);
1096  Interval *span = PG_GETARG_INTERVAL_P(1);
1097  Timestamp dateStamp;
1098 
1099  dateStamp = date2timestamp(dateVal);
1100 
1102  TimestampGetDatum(dateStamp),
1103  PointerGetDatum(span));
1104 }
1105 
1106 /* date_timestamp()
1107  * Convert date to timestamp data type.
1108  */
1109 Datum
1111 {
1112  DateADT dateVal = PG_GETARG_DATEADT(0);
1113  Timestamp result;
1114 
1115  result = date2timestamp(dateVal);
1116 
1117  PG_RETURN_TIMESTAMP(result);
1118 }
1119 
1120 /* timestamp_date()
1121  * Convert timestamp to date data type.
1122  */
1123 Datum
1125 {
1127  DateADT result;
1128  struct pg_tm tt,
1129  *tm = &tt;
1130  fsec_t fsec;
1131 
1132  if (TIMESTAMP_IS_NOBEGIN(timestamp))
1133  DATE_NOBEGIN(result);
1134  else if (TIMESTAMP_IS_NOEND(timestamp))
1135  DATE_NOEND(result);
1136  else
1137  {
1138  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1139  ereport(ERROR,
1140  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1141  errmsg("timestamp out of range")));
1142 
1143  result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
1144  }
1145 
1146  PG_RETURN_DATEADT(result);
1147 }
1148 
1149 
1150 /* date_timestamptz()
1151  * Convert date to timestamp with time zone data type.
1152  */
1153 Datum
1155 {
1156  DateADT dateVal = PG_GETARG_DATEADT(0);
1157  TimestampTz result;
1158 
1159  result = date2timestamptz(dateVal);
1160 
1161  PG_RETURN_TIMESTAMP(result);
1162 }
1163 
1164 
1165 /* timestamptz_date()
1166  * Convert timestamp with time zone to date data type.
1167  */
1168 Datum
1170 {
1172  DateADT result;
1173  struct pg_tm tt,
1174  *tm = &tt;
1175  fsec_t fsec;
1176  int tz;
1177 
1178  if (TIMESTAMP_IS_NOBEGIN(timestamp))
1179  DATE_NOBEGIN(result);
1180  else if (TIMESTAMP_IS_NOEND(timestamp))
1181  DATE_NOEND(result);
1182  else
1183  {
1184  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1185  ereport(ERROR,
1186  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1187  errmsg("timestamp out of range")));
1188 
1189  result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
1190  }
1191 
1192  PG_RETURN_DATEADT(result);
1193 }
1194 
1195 
1196 /*****************************************************************************
1197  * Time ADT
1198  *****************************************************************************/
1199 
1200 Datum
1202 {
1203  char *str = PG_GETARG_CSTRING(0);
1204 
1205 #ifdef NOT_USED
1206  Oid typelem = PG_GETARG_OID(1);
1207 #endif
1208  int32 typmod = PG_GETARG_INT32(2);
1209  TimeADT result;
1210  fsec_t fsec;
1211  struct pg_tm tt,
1212  *tm = &tt;
1213  int tz;
1214  int nf;
1215  int dterr;
1216  char workbuf[MAXDATELEN + 1];
1217  char *field[MAXDATEFIELDS];
1218  int dtype;
1219  int ftype[MAXDATEFIELDS];
1220 
1221  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
1222  field, ftype, MAXDATEFIELDS, &nf);
1223  if (dterr == 0)
1224  dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
1225  if (dterr != 0)
1226  DateTimeParseError(dterr, str, "time");
1227 
1228  tm2time(tm, fsec, &result);
1229  AdjustTimeForTypmod(&result, typmod);
1230 
1231  PG_RETURN_TIMEADT(result);
1232 }
1233 
1234 /* tm2time()
1235  * Convert a tm structure to a time data type.
1236  */
1237 int
1238 tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
1239 {
1240  *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1241  * USECS_PER_SEC) + fsec;
1242  return 0;
1243 }
1244 
1245 /* time_overflows()
1246  * Check to see if a broken-down time-of-day is out of range.
1247  */
1248 bool
1249 time_overflows(int hour, int min, int sec, fsec_t fsec)
1250 {
1251  /* Range-check the fields individually. */
1252  if (hour < 0 || hour > HOURS_PER_DAY ||
1253  min < 0 || min >= MINS_PER_HOUR ||
1254  sec < 0 || sec > SECS_PER_MINUTE ||
1255  fsec < 0 || fsec > USECS_PER_SEC)
1256  return true;
1257 
1258  /*
1259  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1260  * that the total time value doesn't exceed 24:00:00.
1261  */
1262  if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1263  + sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1264  return true;
1265 
1266  return false;
1267 }
1268 
1269 /* float_time_overflows()
1270  * Same, when we have seconds + fractional seconds as one "double" value.
1271  */
1272 bool
1273 float_time_overflows(int hour, int min, double sec)
1274 {
1275  /* Range-check the fields individually. */
1276  if (hour < 0 || hour > HOURS_PER_DAY ||
1277  min < 0 || min >= MINS_PER_HOUR)
1278  return true;
1279 
1280  /*
1281  * "sec", being double, requires extra care. Cope with NaN, and round off
1282  * before applying the range check to avoid unexpected errors due to
1283  * imprecise input. (We assume rint() behaves sanely with infinities.)
1284  */
1285  if (isnan(sec))
1286  return true;
1287  sec = rint(sec * USECS_PER_SEC);
1288  if (sec < 0 || sec > SECS_PER_MINUTE * USECS_PER_SEC)
1289  return true;
1290 
1291  /*
1292  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1293  * that the total time value doesn't exceed 24:00:00. This must match the
1294  * way that callers will convert the fields to a time.
1295  */
1296  if (((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1297  * USECS_PER_SEC) + (int64) sec) > USECS_PER_DAY)
1298  return true;
1299 
1300  return false;
1301 }
1302 
1303 
1304 /* time2tm()
1305  * Convert time data type to POSIX time structure.
1306  *
1307  * For dates within the range of pg_time_t, convert to the local time zone.
1308  * If out of this range, leave as UTC (in practice that could only happen
1309  * if pg_time_t is just 32 bits) - thomas 97/05/27
1310  */
1311 int
1312 time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
1313 {
1314  tm->tm_hour = time / USECS_PER_HOUR;
1315  time -= tm->tm_hour * USECS_PER_HOUR;
1316  tm->tm_min = time / USECS_PER_MINUTE;
1317  time -= tm->tm_min * USECS_PER_MINUTE;
1318  tm->tm_sec = time / USECS_PER_SEC;
1319  time -= tm->tm_sec * USECS_PER_SEC;
1320  *fsec = time;
1321  return 0;
1322 }
1323 
1324 Datum
1326 {
1327  TimeADT time = PG_GETARG_TIMEADT(0);
1328  char *result;
1329  struct pg_tm tt,
1330  *tm = &tt;
1331  fsec_t fsec;
1332  char buf[MAXDATELEN + 1];
1333 
1334  time2tm(time, tm, &fsec);
1335  EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf);
1336 
1337  result = pstrdup(buf);
1338  PG_RETURN_CSTRING(result);
1339 }
1340 
1341 /*
1342  * time_recv - converts external binary format to time
1343  */
1344 Datum
1346 {
1348 
1349 #ifdef NOT_USED
1350  Oid typelem = PG_GETARG_OID(1);
1351 #endif
1352  int32 typmod = PG_GETARG_INT32(2);
1353  TimeADT result;
1354 
1355  result = pq_getmsgint64(buf);
1356 
1357  if (result < INT64CONST(0) || result > USECS_PER_DAY)
1358  ereport(ERROR,
1359  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1360  errmsg("time out of range")));
1361 
1362  AdjustTimeForTypmod(&result, typmod);
1363 
1364  PG_RETURN_TIMEADT(result);
1365 }
1366 
1367 /*
1368  * time_send - converts time to binary format
1369  */
1370 Datum
1372 {
1373  TimeADT time = PG_GETARG_TIMEADT(0);
1375 
1376  pq_begintypsend(&buf);
1377  pq_sendint64(&buf, time);
1379 }
1380 
1381 Datum
1383 {
1385 
1386  PG_RETURN_INT32(anytime_typmodin(false, ta));
1387 }
1388 
1389 Datum
1391 {
1392  int32 typmod = PG_GETARG_INT32(0);
1393 
1394  PG_RETURN_CSTRING(anytime_typmodout(false, typmod));
1395 }
1396 
1397 /*
1398  * make_time - time constructor
1399  */
1400 Datum
1402 {
1403  int tm_hour = PG_GETARG_INT32(0);
1404  int tm_min = PG_GETARG_INT32(1);
1405  double sec = PG_GETARG_FLOAT8(2);
1406  TimeADT time;
1407 
1408  /* Check for time overflow */
1409  if (float_time_overflows(tm_hour, tm_min, sec))
1410  ereport(ERROR,
1411  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
1412  errmsg("time field value out of range: %d:%02d:%02g",
1413  tm_hour, tm_min, sec)));
1414 
1415  /* This should match tm2time */
1416  time = (((tm_hour * MINS_PER_HOUR + tm_min) * SECS_PER_MINUTE)
1417  * USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
1418 
1419  PG_RETURN_TIMEADT(time);
1420 }
1421 
1422 
1423 /* time_support()
1424  *
1425  * Planner support function for the time_scale() and timetz_scale()
1426  * length coercion functions (we need not distinguish them here).
1427  */
1428 Datum
1430 {
1431  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1432  Node *ret = NULL;
1433 
1434  if (IsA(rawreq, SupportRequestSimplify))
1435  {
1437 
1438  ret = TemporalSimplify(MAX_TIME_PRECISION, (Node *) req->fcall);
1439  }
1440 
1441  PG_RETURN_POINTER(ret);
1442 }
1443 
1444 /* time_scale()
1445  * Adjust time type for specified scale factor.
1446  * Used by PostgreSQL type system to stuff columns.
1447  */
1448 Datum
1450 {
1451  TimeADT time = PG_GETARG_TIMEADT(0);
1452  int32 typmod = PG_GETARG_INT32(1);
1453  TimeADT result;
1454 
1455  result = time;
1456  AdjustTimeForTypmod(&result, typmod);
1457 
1458  PG_RETURN_TIMEADT(result);
1459 }
1460 
1461 /* AdjustTimeForTypmod()
1462  * Force the precision of the time value to a specified value.
1463  * Uses *exactly* the same code as in AdjustTimestampForTypmod()
1464  * but we make a separate copy because those types do not
1465  * have a fundamental tie together but rather a coincidence of
1466  * implementation. - thomas
1467  */
1468 void
1470 {
1471  static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1472  INT64CONST(1000000),
1473  INT64CONST(100000),
1474  INT64CONST(10000),
1475  INT64CONST(1000),
1476  INT64CONST(100),
1477  INT64CONST(10),
1478  INT64CONST(1)
1479  };
1480 
1481  static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1482  INT64CONST(500000),
1483  INT64CONST(50000),
1484  INT64CONST(5000),
1485  INT64CONST(500),
1486  INT64CONST(50),
1487  INT64CONST(5),
1488  INT64CONST(0)
1489  };
1490 
1491  if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1492  {
1493  if (*time >= INT64CONST(0))
1494  *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1495  TimeScales[typmod];
1496  else
1497  *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1498  TimeScales[typmod]);
1499  }
1500 }
1501 
1502 
1503 Datum
1505 {
1506  TimeADT time1 = PG_GETARG_TIMEADT(0);
1507  TimeADT time2 = PG_GETARG_TIMEADT(1);
1508 
1509  PG_RETURN_BOOL(time1 == time2);
1510 }
1511 
1512 Datum
1514 {
1515  TimeADT time1 = PG_GETARG_TIMEADT(0);
1516  TimeADT time2 = PG_GETARG_TIMEADT(1);
1517 
1518  PG_RETURN_BOOL(time1 != time2);
1519 }
1520 
1521 Datum
1523 {
1524  TimeADT time1 = PG_GETARG_TIMEADT(0);
1525  TimeADT time2 = PG_GETARG_TIMEADT(1);
1526 
1527  PG_RETURN_BOOL(time1 < time2);
1528 }
1529 
1530 Datum
1532 {
1533  TimeADT time1 = PG_GETARG_TIMEADT(0);
1534  TimeADT time2 = PG_GETARG_TIMEADT(1);
1535 
1536  PG_RETURN_BOOL(time1 <= time2);
1537 }
1538 
1539 Datum
1541 {
1542  TimeADT time1 = PG_GETARG_TIMEADT(0);
1543  TimeADT time2 = PG_GETARG_TIMEADT(1);
1544 
1545  PG_RETURN_BOOL(time1 > time2);
1546 }
1547 
1548 Datum
1550 {
1551  TimeADT time1 = PG_GETARG_TIMEADT(0);
1552  TimeADT time2 = PG_GETARG_TIMEADT(1);
1553 
1554  PG_RETURN_BOOL(time1 >= time2);
1555 }
1556 
1557 Datum
1559 {
1560  TimeADT time1 = PG_GETARG_TIMEADT(0);
1561  TimeADT time2 = PG_GETARG_TIMEADT(1);
1562 
1563  if (time1 < time2)
1564  PG_RETURN_INT32(-1);
1565  if (time1 > time2)
1566  PG_RETURN_INT32(1);
1567  PG_RETURN_INT32(0);
1568 }
1569 
1570 Datum
1572 {
1573  return hashint8(fcinfo);
1574 }
1575 
1576 Datum
1578 {
1579  return hashint8extended(fcinfo);
1580 }
1581 
1582 Datum
1584 {
1585  TimeADT time1 = PG_GETARG_TIMEADT(0);
1586  TimeADT time2 = PG_GETARG_TIMEADT(1);
1587 
1588  PG_RETURN_TIMEADT((time1 > time2) ? time1 : time2);
1589 }
1590 
1591 Datum
1593 {
1594  TimeADT time1 = PG_GETARG_TIMEADT(0);
1595  TimeADT time2 = PG_GETARG_TIMEADT(1);
1596 
1597  PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
1598 }
1599 
1600 /* overlaps_time() --- implements the SQL OVERLAPS operator.
1601  *
1602  * Algorithm is per SQL spec. This is much harder than you'd think
1603  * because the spec requires us to deliver a non-null answer in some cases
1604  * where some of the inputs are null.
1605  */
1606 Datum
1608 {
1609  /*
1610  * The arguments are TimeADT, but we leave them as generic Datums to avoid
1611  * dereferencing nulls (TimeADT is pass-by-reference!)
1612  */
1613  Datum ts1 = PG_GETARG_DATUM(0);
1614  Datum te1 = PG_GETARG_DATUM(1);
1615  Datum ts2 = PG_GETARG_DATUM(2);
1616  Datum te2 = PG_GETARG_DATUM(3);
1617  bool ts1IsNull = PG_ARGISNULL(0);
1618  bool te1IsNull = PG_ARGISNULL(1);
1619  bool ts2IsNull = PG_ARGISNULL(2);
1620  bool te2IsNull = PG_ARGISNULL(3);
1621 
1622 #define TIMEADT_GT(t1,t2) \
1623  (DatumGetTimeADT(t1) > DatumGetTimeADT(t2))
1624 #define TIMEADT_LT(t1,t2) \
1625  (DatumGetTimeADT(t1) < DatumGetTimeADT(t2))
1626 
1627  /*
1628  * If both endpoints of interval 1 are null, the result is null (unknown).
1629  * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
1630  * take ts1 as the lesser endpoint.
1631  */
1632  if (ts1IsNull)
1633  {
1634  if (te1IsNull)
1635  PG_RETURN_NULL();
1636  /* swap null for non-null */
1637  ts1 = te1;
1638  te1IsNull = true;
1639  }
1640  else if (!te1IsNull)
1641  {
1642  if (TIMEADT_GT(ts1, te1))
1643  {
1644  Datum tt = ts1;
1645 
1646  ts1 = te1;
1647  te1 = tt;
1648  }
1649  }
1650 
1651  /* Likewise for interval 2. */
1652  if (ts2IsNull)
1653  {
1654  if (te2IsNull)
1655  PG_RETURN_NULL();
1656  /* swap null for non-null */
1657  ts2 = te2;
1658  te2IsNull = true;
1659  }
1660  else if (!te2IsNull)
1661  {
1662  if (TIMEADT_GT(ts2, te2))
1663  {
1664  Datum tt = ts2;
1665 
1666  ts2 = te2;
1667  te2 = tt;
1668  }
1669  }
1670 
1671  /*
1672  * At this point neither ts1 nor ts2 is null, so we can consider three
1673  * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
1674  */
1675  if (TIMEADT_GT(ts1, ts2))
1676  {
1677  /*
1678  * This case is ts1 < te2 OR te1 < te2, which may look redundant but
1679  * in the presence of nulls it's not quite completely so.
1680  */
1681  if (te2IsNull)
1682  PG_RETURN_NULL();
1683  if (TIMEADT_LT(ts1, te2))
1684  PG_RETURN_BOOL(true);
1685  if (te1IsNull)
1686  PG_RETURN_NULL();
1687 
1688  /*
1689  * If te1 is not null then we had ts1 <= te1 above, and we just found
1690  * ts1 >= te2, hence te1 >= te2.
1691  */
1692  PG_RETURN_BOOL(false);
1693  }
1694  else if (TIMEADT_LT(ts1, ts2))
1695  {
1696  /* This case is ts2 < te1 OR te2 < te1 */
1697  if (te1IsNull)
1698  PG_RETURN_NULL();
1699  if (TIMEADT_LT(ts2, te1))
1700  PG_RETURN_BOOL(true);
1701  if (te2IsNull)
1702  PG_RETURN_NULL();
1703 
1704  /*
1705  * If te2 is not null then we had ts2 <= te2 above, and we just found
1706  * ts2 >= te1, hence te2 >= te1.
1707  */
1708  PG_RETURN_BOOL(false);
1709  }
1710  else
1711  {
1712  /*
1713  * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
1714  * rather silly way of saying "true if both are nonnull, else null".
1715  */
1716  if (te1IsNull || te2IsNull)
1717  PG_RETURN_NULL();
1718  PG_RETURN_BOOL(true);
1719  }
1720 
1721 #undef TIMEADT_GT
1722 #undef TIMEADT_LT
1723 }
1724 
1725 /* timestamp_time()
1726  * Convert timestamp to time data type.
1727  */
1728 Datum
1730 {
1732  TimeADT result;
1733  struct pg_tm tt,
1734  *tm = &tt;
1735  fsec_t fsec;
1736 
1737  if (TIMESTAMP_NOT_FINITE(timestamp))
1738  PG_RETURN_NULL();
1739 
1740  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1741  ereport(ERROR,
1742  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1743  errmsg("timestamp out of range")));
1744 
1745  /*
1746  * Could also do this with time = (timestamp / USECS_PER_DAY *
1747  * USECS_PER_DAY) - timestamp;
1748  */
1749  result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1750  USECS_PER_SEC) + fsec;
1751 
1752  PG_RETURN_TIMEADT(result);
1753 }
1754 
1755 /* timestamptz_time()
1756  * Convert timestamptz to time data type.
1757  */
1758 Datum
1760 {
1762  TimeADT result;
1763  struct pg_tm tt,
1764  *tm = &tt;
1765  int tz;
1766  fsec_t fsec;
1767 
1768  if (TIMESTAMP_NOT_FINITE(timestamp))
1769  PG_RETURN_NULL();
1770 
1771  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1772  ereport(ERROR,
1773  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1774  errmsg("timestamp out of range")));
1775 
1776  /*
1777  * Could also do this with time = (timestamp / USECS_PER_DAY *
1778  * USECS_PER_DAY) - timestamp;
1779  */
1780  result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1781  USECS_PER_SEC) + fsec;
1782 
1783  PG_RETURN_TIMEADT(result);
1784 }
1785 
1786 /* datetime_timestamp()
1787  * Convert date and time to timestamp data type.
1788  */
1789 Datum
1791 {
1793  TimeADT time = PG_GETARG_TIMEADT(1);
1794  Timestamp result;
1795 
1796  result = date2timestamp(date);
1797  if (!TIMESTAMP_NOT_FINITE(result))
1798  {
1799  result += time;
1800  if (!IS_VALID_TIMESTAMP(result))
1801  ereport(ERROR,
1802  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1803  errmsg("timestamp out of range")));
1804  }
1805 
1806  PG_RETURN_TIMESTAMP(result);
1807 }
1808 
1809 /* time_interval()
1810  * Convert time to interval data type.
1811  */
1812 Datum
1814 {
1815  TimeADT time = PG_GETARG_TIMEADT(0);
1816  Interval *result;
1817 
1818  result = (Interval *) palloc(sizeof(Interval));
1819 
1820  result->time = time;
1821  result->day = 0;
1822  result->month = 0;
1823 
1824  PG_RETURN_INTERVAL_P(result);
1825 }
1826 
1827 /* interval_time()
1828  * Convert interval to time data type.
1829  *
1830  * This is defined as producing the fractional-day portion of the interval.
1831  * Therefore, we can just ignore the months field. It is not real clear
1832  * what to do with negative intervals, but we choose to subtract the floor,
1833  * so that, say, '-2 hours' becomes '22:00:00'.
1834  */
1835 Datum
1837 {
1838  Interval *span = PG_GETARG_INTERVAL_P(0);
1839  TimeADT result;
1840  int64 days;
1841 
1842  result = span->time;
1843  if (result >= USECS_PER_DAY)
1844  {
1845  days = result / USECS_PER_DAY;
1846  result -= days * USECS_PER_DAY;
1847  }
1848  else if (result < 0)
1849  {
1850  days = (-result + USECS_PER_DAY - 1) / USECS_PER_DAY;
1851  result += days * USECS_PER_DAY;
1852  }
1853 
1854  PG_RETURN_TIMEADT(result);
1855 }
1856 
1857 /* time_mi_time()
1858  * Subtract two times to produce an interval.
1859  */
1860 Datum
1862 {
1863  TimeADT time1 = PG_GETARG_TIMEADT(0);
1864  TimeADT time2 = PG_GETARG_TIMEADT(1);
1865  Interval *result;
1866 
1867  result = (Interval *) palloc(sizeof(Interval));
1868 
1869  result->month = 0;
1870  result->day = 0;
1871  result->time = time1 - time2;
1872 
1873  PG_RETURN_INTERVAL_P(result);
1874 }
1875 
1876 /* time_pl_interval()
1877  * Add interval to time.
1878  */
1879 Datum
1881 {
1882  TimeADT time = PG_GETARG_TIMEADT(0);
1883  Interval *span = PG_GETARG_INTERVAL_P(1);
1884  TimeADT result;
1885 
1886  result = time + span->time;
1887  result -= result / USECS_PER_DAY * USECS_PER_DAY;
1888  if (result < INT64CONST(0))
1889  result += USECS_PER_DAY;
1890 
1891  PG_RETURN_TIMEADT(result);
1892 }
1893 
1894 /* time_mi_interval()
1895  * Subtract interval from time.
1896  */
1897 Datum
1899 {
1900  TimeADT time = PG_GETARG_TIMEADT(0);
1901  Interval *span = PG_GETARG_INTERVAL_P(1);
1902  TimeADT result;
1903 
1904  result = time - span->time;
1905  result -= result / USECS_PER_DAY * USECS_PER_DAY;
1906  if (result < INT64CONST(0))
1907  result += USECS_PER_DAY;
1908 
1909  PG_RETURN_TIMEADT(result);
1910 }
1911 
1912 /*
1913  * in_range support function for time.
1914  */
1915 Datum
1917 {
1919  TimeADT base = PG_GETARG_TIMEADT(1);
1920  Interval *offset = PG_GETARG_INTERVAL_P(2);
1921  bool sub = PG_GETARG_BOOL(3);
1922  bool less = PG_GETARG_BOOL(4);
1923  TimeADT sum;
1924 
1925  /*
1926  * Like time_pl_interval/time_mi_interval, we disregard the month and day
1927  * fields of the offset. So our test for negative should too.
1928  */
1929  if (offset->time < 0)
1930  ereport(ERROR,
1931  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
1932  errmsg("invalid preceding or following size in window function")));
1933 
1934  /*
1935  * We can't use time_pl_interval/time_mi_interval here, because their
1936  * wraparound behavior would give wrong (or at least undesirable) answers.
1937  * Fortunately the equivalent non-wrapping behavior is trivial, especially
1938  * since we don't worry about integer overflow.
1939  */
1940  if (sub)
1941  sum = base - offset->time;
1942  else
1943  sum = base + offset->time;
1944 
1945  if (less)
1946  PG_RETURN_BOOL(val <= sum);
1947  else
1948  PG_RETURN_BOOL(val >= sum);
1949 }
1950 
1951 
1952 /* time_part()
1953  * Extract specified field from time type.
1954  */
1955 Datum
1957 {
1958  text *units = PG_GETARG_TEXT_PP(0);
1959  TimeADT time = PG_GETARG_TIMEADT(1);
1960  float8 result;
1961  int type,
1962  val;
1963  char *lowunits;
1964 
1965  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
1966  VARSIZE_ANY_EXHDR(units),
1967  false);
1968 
1969  type = DecodeUnits(0, lowunits, &val);
1970  if (type == UNKNOWN_FIELD)
1971  type = DecodeSpecial(0, lowunits, &val);
1972 
1973  if (type == UNITS)
1974  {
1975  fsec_t fsec;
1976  struct pg_tm tt,
1977  *tm = &tt;
1978 
1979  time2tm(time, tm, &fsec);
1980 
1981  switch (val)
1982  {
1983  case DTK_MICROSEC:
1984  result = tm->tm_sec * 1000000.0 + fsec;
1985  break;
1986 
1987  case DTK_MILLISEC:
1988  result = tm->tm_sec * 1000.0 + fsec / 1000.0;
1989  break;
1990 
1991  case DTK_SECOND:
1992  result = tm->tm_sec + fsec / 1000000.0;
1993  break;
1994 
1995  case DTK_MINUTE:
1996  result = tm->tm_min;
1997  break;
1998 
1999  case DTK_HOUR:
2000  result = tm->tm_hour;
2001  break;
2002 
2003  case DTK_TZ:
2004  case DTK_TZ_MINUTE:
2005  case DTK_TZ_HOUR:
2006  case DTK_DAY:
2007  case DTK_MONTH:
2008  case DTK_QUARTER:
2009  case DTK_YEAR:
2010  case DTK_DECADE:
2011  case DTK_CENTURY:
2012  case DTK_MILLENNIUM:
2013  case DTK_ISOYEAR:
2014  default:
2015  ereport(ERROR,
2016  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2017  errmsg("\"time\" units \"%s\" not recognized",
2018  lowunits)));
2019  result = 0;
2020  }
2021  }
2022  else if (type == RESERV && val == DTK_EPOCH)
2023  {
2024  result = time / 1000000.0;
2025  }
2026  else
2027  {
2028  ereport(ERROR,
2029  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2030  errmsg("\"time\" units \"%s\" not recognized",
2031  lowunits)));
2032  result = 0;
2033  }
2034 
2035  PG_RETURN_FLOAT8(result);
2036 }
2037 
2038 
2039 /*****************************************************************************
2040  * Time With Time Zone ADT
2041  *****************************************************************************/
2042 
2043 /* tm2timetz()
2044  * Convert a tm structure to a time data type.
2045  */
2046 int
2047 tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
2048 {
2049  result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2050  USECS_PER_SEC) + fsec;
2051  result->zone = tz;
2052 
2053  return 0;
2054 }
2055 
2056 Datum
2058 {
2059  char *str = PG_GETARG_CSTRING(0);
2060 
2061 #ifdef NOT_USED
2062  Oid typelem = PG_GETARG_OID(1);
2063 #endif
2064  int32 typmod = PG_GETARG_INT32(2);
2065  TimeTzADT *result;
2066  fsec_t fsec;
2067  struct pg_tm tt,
2068  *tm = &tt;
2069  int tz;
2070  int nf;
2071  int dterr;
2072  char workbuf[MAXDATELEN + 1];
2073  char *field[MAXDATEFIELDS];
2074  int dtype;
2075  int ftype[MAXDATEFIELDS];
2076 
2077  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
2078  field, ftype, MAXDATEFIELDS, &nf);
2079  if (dterr == 0)
2080  dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
2081  if (dterr != 0)
2082  DateTimeParseError(dterr, str, "time with time zone");
2083 
2084  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2085  tm2timetz(tm, fsec, tz, result);
2086  AdjustTimeForTypmod(&(result->time), typmod);
2087 
2088  PG_RETURN_TIMETZADT_P(result);
2089 }
2090 
2091 Datum
2093 {
2094  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2095  char *result;
2096  struct pg_tm tt,
2097  *tm = &tt;
2098  fsec_t fsec;
2099  int tz;
2100  char buf[MAXDATELEN + 1];
2101 
2102  timetz2tm(time, tm, &fsec, &tz);
2103  EncodeTimeOnly(tm, fsec, true, tz, DateStyle, buf);
2104 
2105  result = pstrdup(buf);
2106  PG_RETURN_CSTRING(result);
2107 }
2108 
2109 /*
2110  * timetz_recv - converts external binary format to timetz
2111  */
2112 Datum
2114 {
2116 
2117 #ifdef NOT_USED
2118  Oid typelem = PG_GETARG_OID(1);
2119 #endif
2120  int32 typmod = PG_GETARG_INT32(2);
2121  TimeTzADT *result;
2122 
2123  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2124 
2125  result->time = pq_getmsgint64(buf);
2126 
2127  if (result->time < INT64CONST(0) || result->time > USECS_PER_DAY)
2128  ereport(ERROR,
2129  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2130  errmsg("time out of range")));
2131 
2132  result->zone = pq_getmsgint(buf, sizeof(result->zone));
2133 
2134  /* Check for sane GMT displacement; see notes in datatype/timestamp.h */
2135  if (result->zone <= -TZDISP_LIMIT || result->zone >= TZDISP_LIMIT)
2136  ereport(ERROR,
2137  (errcode(ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE),
2138  errmsg("time zone displacement out of range")));
2139 
2140  AdjustTimeForTypmod(&(result->time), typmod);
2141 
2142  PG_RETURN_TIMETZADT_P(result);
2143 }
2144 
2145 /*
2146  * timetz_send - converts timetz to binary format
2147  */
2148 Datum
2150 {
2151  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2153 
2154  pq_begintypsend(&buf);
2155  pq_sendint64(&buf, time->time);
2156  pq_sendint32(&buf, time->zone);
2158 }
2159 
2160 Datum
2162 {
2164 
2165  PG_RETURN_INT32(anytime_typmodin(true, ta));
2166 }
2167 
2168 Datum
2170 {
2171  int32 typmod = PG_GETARG_INT32(0);
2172 
2173  PG_RETURN_CSTRING(anytime_typmodout(true, typmod));
2174 }
2175 
2176 
2177 /* timetz2tm()
2178  * Convert TIME WITH TIME ZONE data type to POSIX time structure.
2179  */
2180 int
2181 timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
2182 {
2183  TimeOffset trem = time->time;
2184 
2185  tm->tm_hour = trem / USECS_PER_HOUR;
2186  trem -= tm->tm_hour * USECS_PER_HOUR;
2187  tm->tm_min = trem / USECS_PER_MINUTE;
2188  trem -= tm->tm_min * USECS_PER_MINUTE;
2189  tm->tm_sec = trem / USECS_PER_SEC;
2190  *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2191 
2192  if (tzp != NULL)
2193  *tzp = time->zone;
2194 
2195  return 0;
2196 }
2197 
2198 /* timetz_scale()
2199  * Adjust time type for specified scale factor.
2200  * Used by PostgreSQL type system to stuff columns.
2201  */
2202 Datum
2204 {
2205  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2206  int32 typmod = PG_GETARG_INT32(1);
2207  TimeTzADT *result;
2208 
2209  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2210 
2211  result->time = time->time;
2212  result->zone = time->zone;
2213 
2214  AdjustTimeForTypmod(&(result->time), typmod);
2215 
2216  PG_RETURN_TIMETZADT_P(result);
2217 }
2218 
2219 
2220 static int
2222 {
2223  TimeOffset t1,
2224  t2;
2225 
2226  /* Primary sort is by true (GMT-equivalent) time */
2227  t1 = time1->time + (time1->zone * USECS_PER_SEC);
2228  t2 = time2->time + (time2->zone * USECS_PER_SEC);
2229 
2230  if (t1 > t2)
2231  return 1;
2232  if (t1 < t2)
2233  return -1;
2234 
2235  /*
2236  * If same GMT time, sort by timezone; we only want to say that two
2237  * timetz's are equal if both the time and zone parts are equal.
2238  */
2239  if (time1->zone > time2->zone)
2240  return 1;
2241  if (time1->zone < time2->zone)
2242  return -1;
2243 
2244  return 0;
2245 }
2246 
2247 Datum
2249 {
2250  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2251  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2252 
2253  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) == 0);
2254 }
2255 
2256 Datum
2258 {
2259  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2260  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2261 
2262  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) != 0);
2263 }
2264 
2265 Datum
2267 {
2268  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2269  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2270 
2271  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) < 0);
2272 }
2273 
2274 Datum
2276 {
2277  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2278  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2279 
2280  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) <= 0);
2281 }
2282 
2283 Datum
2285 {
2286  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2287  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2288 
2289  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) > 0);
2290 }
2291 
2292 Datum
2294 {
2295  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2296  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2297 
2298  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) >= 0);
2299 }
2300 
2301 Datum
2303 {
2304  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2305  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2306 
2307  PG_RETURN_INT32(timetz_cmp_internal(time1, time2));
2308 }
2309 
2310 Datum
2312 {
2314  uint32 thash;
2315 
2316  /*
2317  * To avoid any problems with padding bytes in the struct, we figure the
2318  * field hashes separately and XOR them.
2319  */
2321  Int64GetDatumFast(key->time)));
2322  thash ^= DatumGetUInt32(hash_uint32(key->zone));
2323  PG_RETURN_UINT32(thash);
2324 }
2325 
2326 Datum
2328 {
2330  Datum seed = PG_GETARG_DATUM(1);
2331  uint64 thash;
2332 
2333  /* Same approach as timetz_hash */
2335  Int64GetDatumFast(key->time),
2336  seed));
2338  DatumGetInt64(seed)));
2339  PG_RETURN_UINT64(thash);
2340 }
2341 
2342 Datum
2344 {
2345  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2346  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2347  TimeTzADT *result;
2348 
2349  if (timetz_cmp_internal(time1, time2) > 0)
2350  result = time1;
2351  else
2352  result = time2;
2353  PG_RETURN_TIMETZADT_P(result);
2354 }
2355 
2356 Datum
2358 {
2359  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2360  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2361  TimeTzADT *result;
2362 
2363  if (timetz_cmp_internal(time1, time2) < 0)
2364  result = time1;
2365  else
2366  result = time2;
2367  PG_RETURN_TIMETZADT_P(result);
2368 }
2369 
2370 /* timetz_pl_interval()
2371  * Add interval to timetz.
2372  */
2373 Datum
2375 {
2376  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2377  Interval *span = PG_GETARG_INTERVAL_P(1);
2378  TimeTzADT *result;
2379 
2380  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2381 
2382  result->time = time->time + span->time;
2383  result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2384  if (result->time < INT64CONST(0))
2385  result->time += USECS_PER_DAY;
2386 
2387  result->zone = time->zone;
2388 
2389  PG_RETURN_TIMETZADT_P(result);
2390 }
2391 
2392 /* timetz_mi_interval()
2393  * Subtract interval from timetz.
2394  */
2395 Datum
2397 {
2398  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2399  Interval *span = PG_GETARG_INTERVAL_P(1);
2400  TimeTzADT *result;
2401 
2402  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2403 
2404  result->time = time->time - span->time;
2405  result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2406  if (result->time < INT64CONST(0))
2407  result->time += USECS_PER_DAY;
2408 
2409  result->zone = time->zone;
2410 
2411  PG_RETURN_TIMETZADT_P(result);
2412 }
2413 
2414 /*
2415  * in_range support function for timetz.
2416  */
2417 Datum
2419 {
2421  TimeTzADT *base = PG_GETARG_TIMETZADT_P(1);
2422  Interval *offset = PG_GETARG_INTERVAL_P(2);
2423  bool sub = PG_GETARG_BOOL(3);
2424  bool less = PG_GETARG_BOOL(4);
2425  TimeTzADT sum;
2426 
2427  /*
2428  * Like timetz_pl_interval/timetz_mi_interval, we disregard the month and
2429  * day fields of the offset. So our test for negative should too.
2430  */
2431  if (offset->time < 0)
2432  ereport(ERROR,
2433  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2434  errmsg("invalid preceding or following size in window function")));
2435 
2436  /*
2437  * We can't use timetz_pl_interval/timetz_mi_interval here, because their
2438  * wraparound behavior would give wrong (or at least undesirable) answers.
2439  * Fortunately the equivalent non-wrapping behavior is trivial, especially
2440  * since we don't worry about integer overflow.
2441  */
2442  if (sub)
2443  sum.time = base->time - offset->time;
2444  else
2445  sum.time = base->time + offset->time;
2446  sum.zone = base->zone;
2447 
2448  if (less)
2449  PG_RETURN_BOOL(timetz_cmp_internal(val, &sum) <= 0);
2450  else
2451  PG_RETURN_BOOL(timetz_cmp_internal(val, &sum) >= 0);
2452 }
2453 
2454 /* overlaps_timetz() --- implements the SQL OVERLAPS operator.
2455  *
2456  * Algorithm is per SQL spec. This is much harder than you'd think
2457  * because the spec requires us to deliver a non-null answer in some cases
2458  * where some of the inputs are null.
2459  */
2460 Datum
2462 {
2463  /*
2464  * The arguments are TimeTzADT *, but we leave them as generic Datums for
2465  * convenience of notation --- and to avoid dereferencing nulls.
2466  */
2467  Datum ts1 = PG_GETARG_DATUM(0);
2468  Datum te1 = PG_GETARG_DATUM(1);
2469  Datum ts2 = PG_GETARG_DATUM(2);
2470  Datum te2 = PG_GETARG_DATUM(3);
2471  bool ts1IsNull = PG_ARGISNULL(0);
2472  bool te1IsNull = PG_ARGISNULL(1);
2473  bool ts2IsNull = PG_ARGISNULL(2);
2474  bool te2IsNull = PG_ARGISNULL(3);
2475 
2476 #define TIMETZ_GT(t1,t2) \
2477  DatumGetBool(DirectFunctionCall2(timetz_gt,t1,t2))
2478 #define TIMETZ_LT(t1,t2) \
2479  DatumGetBool(DirectFunctionCall2(timetz_lt,t1,t2))
2480 
2481  /*
2482  * If both endpoints of interval 1 are null, the result is null (unknown).
2483  * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2484  * take ts1 as the lesser endpoint.
2485  */
2486  if (ts1IsNull)
2487  {
2488  if (te1IsNull)
2489  PG_RETURN_NULL();
2490  /* swap null for non-null */
2491  ts1 = te1;
2492  te1IsNull = true;
2493  }
2494  else if (!te1IsNull)
2495  {
2496  if (TIMETZ_GT(ts1, te1))
2497  {
2498  Datum tt = ts1;
2499 
2500  ts1 = te1;
2501  te1 = tt;
2502  }
2503  }
2504 
2505  /* Likewise for interval 2. */
2506  if (ts2IsNull)
2507  {
2508  if (te2IsNull)
2509  PG_RETURN_NULL();
2510  /* swap null for non-null */
2511  ts2 = te2;
2512  te2IsNull = true;
2513  }
2514  else if (!te2IsNull)
2515  {
2516  if (TIMETZ_GT(ts2, te2))
2517  {
2518  Datum tt = ts2;
2519 
2520  ts2 = te2;
2521  te2 = tt;
2522  }
2523  }
2524 
2525  /*
2526  * At this point neither ts1 nor ts2 is null, so we can consider three
2527  * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2528  */
2529  if (TIMETZ_GT(ts1, ts2))
2530  {
2531  /*
2532  * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2533  * in the presence of nulls it's not quite completely so.
2534  */
2535  if (te2IsNull)
2536  PG_RETURN_NULL();
2537  if (TIMETZ_LT(ts1, te2))
2538  PG_RETURN_BOOL(true);
2539  if (te1IsNull)
2540  PG_RETURN_NULL();
2541 
2542  /*
2543  * If te1 is not null then we had ts1 <= te1 above, and we just found
2544  * ts1 >= te2, hence te1 >= te2.
2545  */
2546  PG_RETURN_BOOL(false);
2547  }
2548  else if (TIMETZ_LT(ts1, ts2))
2549  {
2550  /* This case is ts2 < te1 OR te2 < te1 */
2551  if (te1IsNull)
2552  PG_RETURN_NULL();
2553  if (TIMETZ_LT(ts2, te1))
2554  PG_RETURN_BOOL(true);
2555  if (te2IsNull)
2556  PG_RETURN_NULL();
2557 
2558  /*
2559  * If te2 is not null then we had ts2 <= te2 above, and we just found
2560  * ts2 >= te1, hence te2 >= te1.
2561  */
2562  PG_RETURN_BOOL(false);
2563  }
2564  else
2565  {
2566  /*
2567  * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2568  * rather silly way of saying "true if both are nonnull, else null".
2569  */
2570  if (te1IsNull || te2IsNull)
2571  PG_RETURN_NULL();
2572  PG_RETURN_BOOL(true);
2573  }
2574 
2575 #undef TIMETZ_GT
2576 #undef TIMETZ_LT
2577 }
2578 
2579 
2580 Datum
2582 {
2583  TimeTzADT *timetz = PG_GETARG_TIMETZADT_P(0);
2584  TimeADT result;
2585 
2586  /* swallow the time zone and just return the time */
2587  result = timetz->time;
2588 
2589  PG_RETURN_TIMEADT(result);
2590 }
2591 
2592 
2593 Datum
2595 {
2596  TimeADT time = PG_GETARG_TIMEADT(0);
2597  TimeTzADT *result;
2598  struct pg_tm tt,
2599  *tm = &tt;
2600  fsec_t fsec;
2601  int tz;
2602 
2603  GetCurrentDateTime(tm);
2604  time2tm(time, tm, &fsec);
2606 
2607  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2608 
2609  result->time = time;
2610  result->zone = tz;
2611 
2612  PG_RETURN_TIMETZADT_P(result);
2613 }
2614 
2615 
2616 /* timestamptz_timetz()
2617  * Convert timestamp to timetz data type.
2618  */
2619 Datum
2621 {
2623  TimeTzADT *result;
2624  struct pg_tm tt,
2625  *tm = &tt;
2626  int tz;
2627  fsec_t fsec;
2628 
2629  if (TIMESTAMP_NOT_FINITE(timestamp))
2630  PG_RETURN_NULL();
2631 
2632  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
2633  ereport(ERROR,
2634  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2635  errmsg("timestamp out of range")));
2636 
2637  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2638 
2639  tm2timetz(tm, fsec, tz, result);
2640 
2641  PG_RETURN_TIMETZADT_P(result);
2642 }
2643 
2644 
2645 /* datetimetz_timestamptz()
2646  * Convert date and timetz to timestamp with time zone data type.
2647  * Timestamp is stored in GMT, so add the time zone
2648  * stored with the timetz to the result.
2649  * - thomas 2000-03-10
2650  */
2651 Datum
2653 {
2655  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2656  TimestampTz result;
2657 
2658  if (DATE_IS_NOBEGIN(date))
2659  TIMESTAMP_NOBEGIN(result);
2660  else if (DATE_IS_NOEND(date))
2661  TIMESTAMP_NOEND(result);
2662  else
2663  {
2664  /*
2665  * Date's range is wider than timestamp's, so check for boundaries.
2666  * Since dates have the same minimum values as timestamps, only upper
2667  * boundary need be checked for overflow.
2668  */
2670  ereport(ERROR,
2671  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2672  errmsg("date out of range for timestamp")));
2673  result = date * USECS_PER_DAY + time->time + time->zone * USECS_PER_SEC;
2674 
2675  /*
2676  * Since it is possible to go beyond allowed timestamptz range because
2677  * of time zone, check for allowed timestamp range after adding tz.
2678  */
2679  if (!IS_VALID_TIMESTAMP(result))
2680  ereport(ERROR,
2681  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2682  errmsg("date out of range for timestamp")));
2683  }
2684 
2685  PG_RETURN_TIMESTAMP(result);
2686 }
2687 
2688 
2689 /* timetz_part()
2690  * Extract specified field from time type.
2691  */
2692 Datum
2694 {
2695  text *units = PG_GETARG_TEXT_PP(0);
2696  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2697  float8 result;
2698  int type,
2699  val;
2700  char *lowunits;
2701 
2702  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
2703  VARSIZE_ANY_EXHDR(units),
2704  false);
2705 
2706  type = DecodeUnits(0, lowunits, &val);
2707  if (type == UNKNOWN_FIELD)
2708  type = DecodeSpecial(0, lowunits, &val);
2709 
2710  if (type == UNITS)
2711  {
2712  double dummy;
2713  int tz;
2714  fsec_t fsec;
2715  struct pg_tm tt,
2716  *tm = &tt;
2717 
2718  timetz2tm(time, tm, &fsec, &tz);
2719 
2720  switch (val)
2721  {
2722  case DTK_TZ:
2723  result = -tz;
2724  break;
2725 
2726  case DTK_TZ_MINUTE:
2727  result = -tz;
2728  result /= SECS_PER_MINUTE;
2729  FMODULO(result, dummy, (double) SECS_PER_MINUTE);
2730  break;
2731 
2732  case DTK_TZ_HOUR:
2733  dummy = -tz;
2734  FMODULO(dummy, result, (double) SECS_PER_HOUR);
2735  break;
2736 
2737  case DTK_MICROSEC:
2738  result = tm->tm_sec * 1000000.0 + fsec;
2739  break;
2740 
2741  case DTK_MILLISEC:
2742  result = tm->tm_sec * 1000.0 + fsec / 1000.0;
2743  break;
2744 
2745  case DTK_SECOND:
2746  result = tm->tm_sec + fsec / 1000000.0;
2747  break;
2748 
2749  case DTK_MINUTE:
2750  result = tm->tm_min;
2751  break;
2752 
2753  case DTK_HOUR:
2754  result = tm->tm_hour;
2755  break;
2756 
2757  case DTK_DAY:
2758  case DTK_MONTH:
2759  case DTK_QUARTER:
2760  case DTK_YEAR:
2761  case DTK_DECADE:
2762  case DTK_CENTURY:
2763  case DTK_MILLENNIUM:
2764  default:
2765  ereport(ERROR,
2766  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2767  errmsg("\"time with time zone\" units \"%s\" not recognized",
2768  lowunits)));
2769  result = 0;
2770  }
2771  }
2772  else if (type == RESERV && val == DTK_EPOCH)
2773  {
2774  result = time->time / 1000000.0 + time->zone;
2775  }
2776  else
2777  {
2778  ereport(ERROR,
2779  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2780  errmsg("\"time with time zone\" units \"%s\" not recognized",
2781  lowunits)));
2782  result = 0;
2783  }
2784 
2785  PG_RETURN_FLOAT8(result);
2786 }
2787 
2788 /* timetz_zone()
2789  * Encode time with time zone type with specified time zone.
2790  * Applies DST rules as of the current date.
2791  */
2792 Datum
2794 {
2795  text *zone = PG_GETARG_TEXT_PP(0);
2797  TimeTzADT *result;
2798  int tz;
2799  char tzname[TZ_STRLEN_MAX + 1];
2800  char *lowzone;
2801  int type,
2802  val;
2803  pg_tz *tzp;
2804 
2805  /*
2806  * Look up the requested timezone. First we look in the timezone
2807  * abbreviation table (to handle cases like "EST"), and if that fails, we
2808  * look in the timezone database (to handle cases like
2809  * "America/New_York"). (This matches the order in which timestamp input
2810  * checks the cases; it's important because the timezone database unwisely
2811  * uses a few zone names that are identical to offset abbreviations.)
2812  */
2813  text_to_cstring_buffer(zone, tzname, sizeof(tzname));
2814 
2815  /* DecodeTimezoneAbbrev requires lowercase input */
2816  lowzone = downcase_truncate_identifier(tzname,
2817  strlen(tzname),
2818  false);
2819 
2820  type = DecodeTimezoneAbbrev(0, lowzone, &val, &tzp);
2821 
2822  if (type == TZ || type == DTZ)
2823  {
2824  /* fixed-offset abbreviation */
2825  tz = -val;
2826  }
2827  else if (type == DYNTZ)
2828  {
2829  /* dynamic-offset abbreviation, resolve using current time */
2830  pg_time_t now = (pg_time_t) time(NULL);
2831  struct pg_tm *tm;
2832 
2833  tm = pg_localtime(&now, tzp);
2834  tz = DetermineTimeZoneAbbrevOffset(tm, tzname, tzp);
2835  }
2836  else
2837  {
2838  /* try it as a full zone name */
2839  tzp = pg_tzset(tzname);
2840  if (tzp)
2841  {
2842  /* Get the offset-from-GMT that is valid today for the zone */
2843  pg_time_t now = (pg_time_t) time(NULL);
2844  struct pg_tm *tm;
2845 
2846  tm = pg_localtime(&now, tzp);
2847  tz = -tm->tm_gmtoff;
2848  }
2849  else
2850  {
2851  ereport(ERROR,
2852  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2853  errmsg("time zone \"%s\" not recognized", tzname)));
2854  tz = 0; /* keep compiler quiet */
2855  }
2856  }
2857 
2858  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2859 
2860  result->time = t->time + (t->zone - tz) * USECS_PER_SEC;
2861  while (result->time < INT64CONST(0))
2862  result->time += USECS_PER_DAY;
2863  while (result->time >= USECS_PER_DAY)
2864  result->time -= USECS_PER_DAY;
2865 
2866  result->zone = tz;
2867 
2868  PG_RETURN_TIMETZADT_P(result);
2869 }
2870 
2871 /* timetz_izone()
2872  * Encode time with time zone type with specified time interval as time zone.
2873  */
2874 Datum
2876 {
2878  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2879  TimeTzADT *result;
2880  int tz;
2881 
2882  if (zone->month != 0 || zone->day != 0)
2883  ereport(ERROR,
2884  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2885  errmsg("interval time zone \"%s\" must not include months or days",
2887  PointerGetDatum(zone))))));
2888 
2889  tz = -(zone->time / USECS_PER_SEC);
2890 
2891  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2892 
2893  result->time = time->time + (time->zone - tz) * USECS_PER_SEC;
2894  while (result->time < INT64CONST(0))
2895  result->time += USECS_PER_DAY;
2896  while (result->time >= USECS_PER_DAY)
2897  result->time -= USECS_PER_DAY;
2898 
2899  result->zone = tz;
2900 
2901  PG_RETURN_TIMETZADT_P(result);
2902 }
#define MAXDATELEN
Definition: datetime.h:201
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:3887
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
Datum date_cmp_timestamptz(PG_FUNCTION_ARGS)
Definition: date.c:902
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define TIMEADT_LT(t1, t2)
static int32 anytime_typmodin(bool istz, ArrayType *ta)
Definition: date.c:47
Datum timetypmodin(PG_FUNCTION_ARGS)
Definition: date.c:1382
Datum timetz_cmp(PG_FUNCTION_ARGS)
Definition: date.c:2302
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define DatumGetUInt32(X)
Definition: postgres.h:486
Datum time_mi_interval(PG_FUNCTION_ARGS)
Definition: date.c:1898
Datum date_eq(PG_FUNCTION_ARGS)
Definition: date.c:374
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
Datum timetz_pl_interval(PG_FUNCTION_ARGS)
Definition: date.c:2374
#define DTK_TZ_HOUR
Definition: datetime.h:178
#define DATE_IS_NOEND(j)
Definition: date.h:42
Datum timetz_ge(PG_FUNCTION_ARGS)
Definition: date.c:2293
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define DTK_CENTURY
Definition: datetime.h:170
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:349
Datum time_pl_interval(PG_FUNCTION_ARGS)
Definition: date.c:1880
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
Datum date_le(PG_FUNCTION_ARGS)
Definition: date.c:401
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
#define DatumGetDateADT(X)
Definition: date.h:53
Datum date_lt(PG_FUNCTION_ARGS)
Definition: date.c:392
#define UNITS
Definition: datetime.h:108
Datum timestamp_ne_date(PG_FUNCTION_ARGS)
Definition: date.c:920
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3768
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
Datum time_part(PG_FUNCTION_ARGS)
Definition: date.c:1956
int64 pg_time_t
Definition: pgtime.h:23
TimeADT time
Definition: date.h:29
Datum timetz_scale(PG_FUNCTION_ARGS)
Definition: date.c:2203
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2088
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
Datum timetz_ne(PG_FUNCTION_ARGS)
Definition: date.c:2257
#define DTK_YEAR
Definition: datetime.h:168
Datum datetime_timestamp(PG_FUNCTION_ARGS)
Definition: date.c:1790
#define LATE
Definition: datetime.h:41
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:68
#define USECS_PER_SEC
Definition: timestamp.h:94
int32 DateADT
Definition: date.h:23
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:83
Datum timetz_gt(PG_FUNCTION_ARGS)
Definition: date.c:2284
int64 timestamp
Datum timestamptz_ne_date(PG_FUNCTION_ARGS)
Definition: date.c:983
int64 TimestampTz
Definition: timestamp.h:39
#define PointerGetDatum(X)
Definition: postgres.h:556
int tm_hour
Definition: pgtime.h:29
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:37
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
Datum timestamp_date(PG_FUNCTION_ARGS)
Definition: date.c:1124
Datum time_le(PG_FUNCTION_ARGS)
Definition: date.c:1531
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:200
char * pstrdup(const char *in)
Definition: mcxt.c:1187
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define DTK_QUARTER
Definition: datetime.h:167
Datum date_ne_timestamptz(PG_FUNCTION_ARGS)
Definition: date.c:857
Datum date_eq_timestamp(PG_FUNCTION_ARGS)
Definition: date.c:764
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1820
Datum timestamp_ge_date(PG_FUNCTION_ARGS)
Definition: date.c:956
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
Datum timetz_hash_extended(PG_FUNCTION_ARGS)
Definition: date.c:2327
#define FMODULO(t, q, u)
Definition: datetime.h:238
Datum time_eq(PG_FUNCTION_ARGS)
Definition: date.c:1504
Datum timetypmodout(PG_FUNCTION_ARGS)
Definition: date.c:1390
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
Datum time_interval(PG_FUNCTION_ARGS)
Definition: date.c:1813
Definition: nodes.h:527
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:3972
#define DTK_MILLENNIUM
Definition: datetime.h:171
Datum in_range_time_interval(PG_FUNCTION_ARGS)
Definition: date.c:1916
#define USECS_PER_MINUTE
Definition: timestamp.h:93
int errcode(int sqlerrcode)
Definition: elog.c:691
Datum date_lt_timestamptz(PG_FUNCTION_ARGS)
Definition: date.c:866
Datum timetz_lt(PG_FUNCTION_ARGS)
Definition: date.c:2266
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1312
double date2timestamp_no_overflow(DateADT dateVal)
Definition: date.c:724
#define TZ
Definition: datetime.h:96
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define UNKNOWN_FIELD
Definition: datetime.h:125
long date
Definition: pgtypes_date.h:9
static int date_fastcmp(Datum x, Datum y, SortSupport ssup)
Definition: date.c:441
Datum interval_out(PG_FUNCTION_ARGS)
Definition: timestamp.c:956
Datum timetz_mi_interval(PG_FUNCTION_ARGS)
Definition: date.c:2396
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3731
Datum timestamptz_cmp_date(PG_FUNCTION_ARGS)
Definition: date.c:1028
Datum timestamp_gt_date(PG_FUNCTION_ARGS)
Definition: date.c:938
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103
long int tm_gmtoff
Definition: pgtime.h:36
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2221
Datum date_eq_timestamptz(PG_FUNCTION_ARGS)
Definition: date.c:848
Definition: pgtime.h:25
DateADT GetSQLCurrentDate(void)
Definition: date.c:300
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:369
Datum in_range_date_interval(PG_FUNCTION_ARGS)
Definition: date.c:1043
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition: varlena.c:252
int DecodeTimeOnly(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:1754
Datum date_send(PG_FUNCTION_ARGS)
Definition: date.c:222
Datum date_sortsupport(PG_FUNCTION_ARGS)
Definition: date.c:454
#define DTK_DATE_M
Definition: datetime.h:192
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:355
signed int int32
Definition: c.h:417
Datum datetimetz_timestamptz(PG_FUNCTION_ARGS)
Definition: date.c:2652
int32 day
Definition: timestamp.h:47
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
Datum timestamp_le_date(PG_FUNCTION_ARGS)
Definition: date.c:947
static struct pg_tm tm
Definition: localtime.c:102
Datum time_scale(PG_FUNCTION_ARGS)
Definition: date.c:1449
#define DATE_NOBEGIN(j)
Definition: date.h:39
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2047
pg_tz * pg_tzset(const char *tzname)
Definition: pgtz.c:236
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_MILLISEC
Definition: datetime.h:172
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2918
Datum time_larger(PG_FUNCTION_ARGS)
Definition: date.c:1583
#define IS_VALID_DATE(d)
Definition: timestamp.h:190
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2839
Datum in_range_timetz_interval(PG_FUNCTION_ARGS)
Definition: date.c:2418
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define DTK_DECADE
Definition: datetime.h:169
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:803
#define TZ_STRLEN_MAX
Definition: pgtime.h:44
#define DTK_TZ
Definition: datetime.h:147
#define DTK_HOUR
Definition: datetime.h:163
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:553
Datum time_lt(PG_FUNCTION_ARGS)
Definition: date.c:1522
#define DatumGetCString(X)
Definition: postgres.h:566
int DecodeTimezoneAbbrev(int field, char *lowtoken, int *offset, pg_tz **tz)
Definition: datetime.c:2985
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
Datum time_cmp(PG_FUNCTION_ARGS)
Definition: date.c:1558
Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:568
#define DTK_SECOND
Definition: datetime.h:161
Datum date_in(PG_FUNCTION_ARGS)
Definition: date.c:110
int32 zone
Definition: date.h:30
#define DATE_NOT_FINITE(j)
Definition: date.h:43
Datum make_time(PG_FUNCTION_ARGS)
Definition: date.c:1401
Datum time_out(PG_FUNCTION_ARGS)
Definition: date.c:1325
Datum timetztypmodin(PG_FUNCTION_ARGS)
Definition: date.c:2161
TimeTzADT * GetSQLCurrentTime(int32 typmod)
Definition: date.c:333
Datum timetz_send(PG_FUNCTION_ARGS)
Definition: date.c:2149
Datum time_gt(PG_FUNCTION_ARGS)
Definition: date.c:1540
#define DatumGetInt64(X)
Definition: postgres.h:607
Datum date_larger(PG_FUNCTION_ARGS)
Definition: date.c:471
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:628
Datum date_timestamp(PG_FUNCTION_ARGS)
Definition: date.c:1110
Datum date_gt_timestamp(PG_FUNCTION_ARGS)
Definition: date.c:791
int tm_mday
Definition: pgtime.h:30
static char * buf
Definition: pg_test_fsync.c:68
#define HOURS_PER_DAY
Definition: timestamp.h:78
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
int tm_mon
Definition: pgtime.h:31
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1478
#define SECS_PER_MINUTE
Definition: timestamp.h:88
Datum timetztypmodout(PG_FUNCTION_ARGS)
Definition: date.c:2169
Datum date_ne(PG_FUNCTION_ARGS)
Definition: date.c:383
#define USECS_PER_HOUR
Definition: timestamp.h:92
TimeOffset time
Definition: timestamp.h:45
bool time_overflows(int hour, int min, int sec, fsec_t fsec)
Definition: date.c:1249
#define TIMETZ_LT(t1, t2)
static char * anytime_typmodout(bool istz, int32 typmod)
Definition: date.c:90
Datum date_gt(PG_FUNCTION_ARGS)
Definition: date.c:410
unsigned int uint32
Definition: c.h:429
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:285
Datum timestamptz_date(PG_FUNCTION_ARGS)
Definition: date.c:1169
#define DTK_ISOYEAR
Definition: datetime.h:180
const char *const days[]
Definition: datetime.c:68
Datum timestamptz_le_date(PG_FUNCTION_ARGS)
Definition: date.c:1010
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:370
int32 fsec_t
Definition: timestamp.h:41
#define MIN_TIMESTAMP
Definition: timestamp.h:184
int64 TimeADT
Definition: date.h:25
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:155
#define USECS_PER_DAY
Definition: timestamp.h:91
#define DTK_TZ_MINUTE
Definition: datetime.h:179
Datum timetz_le(PG_FUNCTION_ARGS)
Definition: date.c:2275
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
#define SECS_PER_HOUR
Definition: timestamp.h:87
#define TimestampGetDatum(X)
Definition: timestamp.h:31
TimeADT GetSQLLocalTime(int32 typmod)
Definition: date.c:353
#define DTK_MINUTE
Definition: datetime.h:162
#define TZDISP_LIMIT
Definition: timestamp.h:104
Datum time_recv(PG_FUNCTION_ARGS)
Definition: date.c:1345
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:634
Datum date_le_timestamptz(PG_FUNCTION_ARGS)
Definition: date.c:884
int32 month
Definition: timestamp.h:48
int64 Timestamp
Definition: timestamp.h:38
Datum date_mii(PG_FUNCTION_ARGS)
Definition: date.c:532
Datum date_lt_timestamp(PG_FUNCTION_ARGS)
Definition: date.c:782
#define WARNING
Definition: elog.h:40
Datum date_cmp_timestamp(PG_FUNCTION_ARGS)
Definition: date.c:818
#define DTK_LATE
Definition: datetime.h:152
Datum timetz_zone(PG_FUNCTION_ARGS)
Definition: date.c:2793
int64 TimeOffset
Definition: timestamp.h:40
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:708
#define DTK_MICROSEC
Definition: datetime.h:173
Node * TemporalSimplify(int32 max_precis, Node *node)
Definition: datetime.c:4497
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
uintptr_t Datum
Definition: postgres.h:367
Datum date_smaller(PG_FUNCTION_ARGS)
Definition: date.c:480
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: date.c:2181
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:100
Definition: pgtz.h:65
#define Int64GetDatumFast(X)
Definition: postgres.h:760
#define BoolGetDatum(X)
Definition: postgres.h:402
Datum time_ne(PG_FUNCTION_ARGS)
Definition: date.c:1513
Datum time_send(PG_FUNCTION_ARGS)
Definition: date.c:1371
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define ereport(elevel,...)
Definition: elog.h:155
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
Datum date_out(PG_FUNCTION_ARGS)
Definition: date.c:175
Datum date_gt_timestamptz(PG_FUNCTION_ARGS)
Definition: date.c:875
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define DatumGetUInt64(X)
Definition: postgres.h:634
Datum timestamptz_ge_date(PG_FUNCTION_ARGS)
Definition: date.c:1019
Datum timetz_time(PG_FUNCTION_ARGS)
Definition: date.c:2581
Datum timetz_smaller(PG_FUNCTION_ARGS)
Definition: date.c:2357
Datum timestamptz_lt_date(PG_FUNCTION_ARGS)
Definition: date.c:992
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
Datum timestamp_cmp_date(PG_FUNCTION_ARGS)
Definition: date.c:965
#define Assert(condition)
Definition: c.h:800
Datum timetz_recv(PG_FUNCTION_ARGS)
Definition: date.c:2113
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1238
static Datum hash_uint32(uint32 k)
Definition: hashfn.h:43
Datum time_hash_extended(PG_FUNCTION_ARGS)
Definition: date.c:1577
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
Datum time_support(PG_FUNCTION_ARGS)
Definition: date.c:1429
Datum interval_time(PG_FUNCTION_ARGS)
Definition: date.c:1836
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
Datum timetz_eq(PG_FUNCTION_ARGS)
Definition: date.c:2248
#define DTK_EARLY
Definition: datetime.h:151
Datum timetz_hash(PG_FUNCTION_ARGS)
Definition: date.c:2311
bool float_time_overflows(int hour, int min, double sec)
Definition: date.c:1273
#define MAXDATEFIELDS
Definition: datetime.h:203
Datum overlaps_time(PG_FUNCTION_ARGS)
Definition: date.c:1607
#define DTK_DAY
Definition: datetime.h:164
Datum make_date(PG_FUNCTION_ARGS)
Definition: date.c:236
int DateStyle
Definition: globals.c:115
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
Datum date_cmp(PG_FUNCTION_ARGS)
Definition: date.c:428
#define RESERV
Definition: datetime.h:91
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3040
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1342
Datum time_smaller(PG_FUNCTION_ARGS)
Definition: date.c:1592
Datum date_ge_timestamptz(PG_FUNCTION_ARGS)
Definition: date.c:893
Datum timestamptz_timetz(PG_FUNCTION_ARGS)
Definition: date.c:2620
Datum time_timetz(PG_FUNCTION_ARGS)
Definition: date.c:2594
Datum date_ne_timestamp(PG_FUNCTION_ARGS)
Definition: date.c:773
Datum date_timestamptz(PG_FUNCTION_ARGS)
Definition: date.c:1154
#define DTZ
Definition: datetime.h:97
#define DTK_EPOCH
Definition: datetime.h:153
Datum date_le_timestamp(PG_FUNCTION_ARGS)
Definition: date.c:800
Datum timetz_part(PG_FUNCTION_ARGS)
Definition: date.c:2693
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39
Definition: zic.c:93
Datum timetz_in(PG_FUNCTION_ARGS)
Definition: date.c:2057
int tm_year
Definition: pgtime.h:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:902
Datum in_range_timestamp_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3347
#define TIMEADT_GT(t1, t2)
Datum time_mi_time(PG_FUNCTION_ARGS)
Definition: date.c:1861
#define DYNTZ
Definition: datetime.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
Datum date_recv(PG_FUNCTION_ARGS)
Definition: date.c:200
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
Datum timestamptz_gt_date(PG_FUNCTION_ARGS)
Definition: date.c:1001
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115
#define elog(elevel,...)
Definition: elog.h:228
Datum time_in(PG_FUNCTION_ARGS)
Definition: date.c:1201
Datum timestamptz_eq_date(PG_FUNCTION_ARGS)
Definition: date.c:974
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:827
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
Datum date_ge_timestamp(PG_FUNCTION_ARGS)
Definition: date.c:809
Datum date_mi_interval(PG_FUNCTION_ARGS)
Definition: date.c:1093
Datum timestamp_time(PG_FUNCTION_ARGS)
Definition: date.c:1729
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2046
Datum timestamp_lt_date(PG_FUNCTION_ARGS)
Definition: date.c:929
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
Definition: c.h:617
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
static TimestampTz date2timestamp(DateADT dateVal)
Definition: date.c:612
#define PG_RETURN_DATEADT(x)
Definition: date.h:65
int tm_sec
Definition: pgtime.h:27
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:582
Datum time_ge(PG_FUNCTION_ARGS)
Definition: date.c:1549
Datum date_mi(PG_FUNCTION_ARGS)
Definition: date.c:491
Definition: date.h:27
#define EARLY
Definition: datetime.h:40
int tm_min
Definition: pgtime.h:28
Datum overlaps_timetz(PG_FUNCTION_ARGS)
Definition: date.c:2461
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1469
static Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfn.h:49
int DetermineTimeZoneAbbrevOffset(struct pg_tm *tm, const char *abbr, pg_tz *tzp)
Definition: datetime.c:1639
Datum timetz_larger(PG_FUNCTION_ARGS)
Definition: date.c:2343
long val
Definition: informix.c:664
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1542
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:747
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define DTK_DATE
Definition: datetime.h:145
Datum timetz_out(PG_FUNCTION_ARGS)
Definition: date.c:2092
#define DATE_NOEND(j)
Definition: date.h:41
Datum time_hash(PG_FUNCTION_ARGS)
Definition: date.c:1571
Datum date_pl_interval(PG_FUNCTION_ARGS)
Definition: date.c:1073
Datum timestamptz_time(PG_FUNCTION_ARGS)
Definition: date.c:1759
#define TIMETZ_GT(t1, t2)
Datum date_ge(PG_FUNCTION_ARGS)
Definition: date.c:419
#define MAX_TIME_PRECISION
Definition: date.h:51
Datum date_finite(PG_FUNCTION_ARGS)
Definition: date.c:463
Datum timestamp_eq_date(PG_FUNCTION_ARGS)
Definition: date.c:911
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2480
Datum timetz_izone(PG_FUNCTION_ARGS)
Definition: date.c:2875
Datum date_pli(PG_FUNCTION_ARGS)
Definition: date.c:508