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