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