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