PostgreSQL Source Code  git master
date.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <time.h>
#include "access/xact.h"
#include "common/hashfn.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/supportnodes.h"
#include "parser/scansup.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/numeric.h"
#include "utils/sortsupport.h"
Include dependency graph for date.c:

Go to the source code of this file.

Macros

#define TIMEADT_GT(t1, t2)   (DatumGetTimeADT(t1) > DatumGetTimeADT(t2))
 
#define TIMEADT_LT(t1, t2)   (DatumGetTimeADT(t1) < DatumGetTimeADT(t2))
 
#define TIMETZ_GT(t1, t2)   DatumGetBool(DirectFunctionCall2(timetz_gt,t1,t2))
 
#define TIMETZ_LT(t1, t2)   DatumGetBool(DirectFunctionCall2(timetz_lt,t1,t2))
 

Functions

static int32 anytime_typmodin (bool istz, ArrayType *ta)
 
int32 anytime_typmod_check (bool istz, int32 typmod)
 
static char * anytime_typmodout (bool istz, int32 typmod)
 
Datum date_in (PG_FUNCTION_ARGS)
 
Datum date_out (PG_FUNCTION_ARGS)
 
Datum date_recv (PG_FUNCTION_ARGS)
 
Datum date_send (PG_FUNCTION_ARGS)
 
Datum make_date (PG_FUNCTION_ARGS)
 
void EncodeSpecialDate (DateADT dt, char *str)
 
DateADT GetSQLCurrentDate (void)
 
TimeTzADTGetSQLCurrentTime (int32 typmod)
 
TimeADT GetSQLLocalTime (int32 typmod)
 
Datum date_eq (PG_FUNCTION_ARGS)
 
Datum date_ne (PG_FUNCTION_ARGS)
 
Datum date_lt (PG_FUNCTION_ARGS)
 
Datum date_le (PG_FUNCTION_ARGS)
 
Datum date_gt (PG_FUNCTION_ARGS)
 
Datum date_ge (PG_FUNCTION_ARGS)
 
Datum date_cmp (PG_FUNCTION_ARGS)
 
static int date_fastcmp (Datum x, Datum y, SortSupport ssup)
 
Datum date_sortsupport (PG_FUNCTION_ARGS)
 
Datum date_finite (PG_FUNCTION_ARGS)
 
Datum date_larger (PG_FUNCTION_ARGS)
 
Datum date_smaller (PG_FUNCTION_ARGS)
 
Datum date_mi (PG_FUNCTION_ARGS)
 
Datum date_pli (PG_FUNCTION_ARGS)
 
Datum date_mii (PG_FUNCTION_ARGS)
 
Timestamp date2timestamp_opt_overflow (DateADT dateVal, int *overflow)
 
static TimestampTz date2timestamp (DateADT dateVal)
 
TimestampTz date2timestamptz_opt_overflow (DateADT dateVal, int *overflow)
 
static TimestampTz date2timestamptz (DateADT dateVal)
 
double date2timestamp_no_overflow (DateADT dateVal)
 
int32 date_cmp_timestamp_internal (DateADT dateVal, Timestamp dt2)
 
Datum date_eq_timestamp (PG_FUNCTION_ARGS)
 
Datum date_ne_timestamp (PG_FUNCTION_ARGS)
 
Datum date_lt_timestamp (PG_FUNCTION_ARGS)
 
Datum date_gt_timestamp (PG_FUNCTION_ARGS)
 
Datum date_le_timestamp (PG_FUNCTION_ARGS)
 
Datum date_ge_timestamp (PG_FUNCTION_ARGS)
 
Datum date_cmp_timestamp (PG_FUNCTION_ARGS)
 
int32 date_cmp_timestamptz_internal (DateADT dateVal, TimestampTz dt2)
 
Datum date_eq_timestamptz (PG_FUNCTION_ARGS)
 
Datum date_ne_timestamptz (PG_FUNCTION_ARGS)
 
Datum date_lt_timestamptz (PG_FUNCTION_ARGS)
 
Datum date_gt_timestamptz (PG_FUNCTION_ARGS)
 
Datum date_le_timestamptz (PG_FUNCTION_ARGS)
 
Datum date_ge_timestamptz (PG_FUNCTION_ARGS)
 
Datum date_cmp_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_eq_date (PG_FUNCTION_ARGS)
 
Datum timestamp_ne_date (PG_FUNCTION_ARGS)
 
Datum timestamp_lt_date (PG_FUNCTION_ARGS)
 
Datum timestamp_gt_date (PG_FUNCTION_ARGS)
 
Datum timestamp_le_date (PG_FUNCTION_ARGS)
 
Datum timestamp_ge_date (PG_FUNCTION_ARGS)
 
Datum timestamp_cmp_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_eq_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_ne_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_lt_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_gt_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_le_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_ge_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_cmp_date (PG_FUNCTION_ARGS)
 
Datum in_range_date_interval (PG_FUNCTION_ARGS)
 
Datum extract_date (PG_FUNCTION_ARGS)
 
Datum date_pl_interval (PG_FUNCTION_ARGS)
 
Datum date_mi_interval (PG_FUNCTION_ARGS)
 
Datum date_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamp_date (PG_FUNCTION_ARGS)
 
Datum date_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_date (PG_FUNCTION_ARGS)
 
Datum time_in (PG_FUNCTION_ARGS)
 
int tm2time (struct pg_tm *tm, fsec_t fsec, TimeADT *result)
 
bool time_overflows (int hour, int min, int sec, fsec_t fsec)
 
bool float_time_overflows (int hour, int min, double sec)
 
int time2tm (TimeADT time, struct pg_tm *tm, fsec_t *fsec)
 
Datum time_out (PG_FUNCTION_ARGS)
 
Datum time_recv (PG_FUNCTION_ARGS)
 
Datum time_send (PG_FUNCTION_ARGS)
 
Datum timetypmodin (PG_FUNCTION_ARGS)
 
Datum timetypmodout (PG_FUNCTION_ARGS)
 
Datum make_time (PG_FUNCTION_ARGS)
 
Datum time_support (PG_FUNCTION_ARGS)
 
Datum time_scale (PG_FUNCTION_ARGS)
 
void AdjustTimeForTypmod (TimeADT *time, int32 typmod)
 
Datum time_eq (PG_FUNCTION_ARGS)
 
Datum time_ne (PG_FUNCTION_ARGS)
 
Datum time_lt (PG_FUNCTION_ARGS)
 
Datum time_le (PG_FUNCTION_ARGS)
 
Datum time_gt (PG_FUNCTION_ARGS)
 
Datum time_ge (PG_FUNCTION_ARGS)
 
Datum time_cmp (PG_FUNCTION_ARGS)
 
Datum time_hash (PG_FUNCTION_ARGS)
 
Datum time_hash_extended (PG_FUNCTION_ARGS)
 
Datum time_larger (PG_FUNCTION_ARGS)
 
Datum time_smaller (PG_FUNCTION_ARGS)
 
Datum overlaps_time (PG_FUNCTION_ARGS)
 
Datum timestamp_time (PG_FUNCTION_ARGS)
 
Datum timestamptz_time (PG_FUNCTION_ARGS)
 
Datum datetime_timestamp (PG_FUNCTION_ARGS)
 
Datum time_interval (PG_FUNCTION_ARGS)
 
Datum interval_time (PG_FUNCTION_ARGS)
 
Datum time_mi_time (PG_FUNCTION_ARGS)
 
Datum time_pl_interval (PG_FUNCTION_ARGS)
 
Datum time_mi_interval (PG_FUNCTION_ARGS)
 
Datum in_range_time_interval (PG_FUNCTION_ARGS)
 
static Datum time_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum time_part (PG_FUNCTION_ARGS)
 
Datum extract_time (PG_FUNCTION_ARGS)
 
int tm2timetz (struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
 
Datum timetz_in (PG_FUNCTION_ARGS)
 
Datum timetz_out (PG_FUNCTION_ARGS)
 
Datum timetz_recv (PG_FUNCTION_ARGS)
 
Datum timetz_send (PG_FUNCTION_ARGS)
 
Datum timetztypmodin (PG_FUNCTION_ARGS)
 
Datum timetztypmodout (PG_FUNCTION_ARGS)
 
int timetz2tm (TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
 
Datum timetz_scale (PG_FUNCTION_ARGS)
 
static int timetz_cmp_internal (TimeTzADT *time1, TimeTzADT *time2)
 
Datum timetz_eq (PG_FUNCTION_ARGS)
 
Datum timetz_ne (PG_FUNCTION_ARGS)
 
Datum timetz_lt (PG_FUNCTION_ARGS)
 
Datum timetz_le (PG_FUNCTION_ARGS)
 
Datum timetz_gt (PG_FUNCTION_ARGS)
 
Datum timetz_ge (PG_FUNCTION_ARGS)
 
Datum timetz_cmp (PG_FUNCTION_ARGS)
 
Datum timetz_hash (PG_FUNCTION_ARGS)
 
Datum timetz_hash_extended (PG_FUNCTION_ARGS)
 
Datum timetz_larger (PG_FUNCTION_ARGS)
 
Datum timetz_smaller (PG_FUNCTION_ARGS)
 
Datum timetz_pl_interval (PG_FUNCTION_ARGS)
 
Datum timetz_mi_interval (PG_FUNCTION_ARGS)
 
Datum in_range_timetz_interval (PG_FUNCTION_ARGS)
 
Datum overlaps_timetz (PG_FUNCTION_ARGS)
 
Datum timetz_time (PG_FUNCTION_ARGS)
 
Datum time_timetz (PG_FUNCTION_ARGS)
 
Datum timestamptz_timetz (PG_FUNCTION_ARGS)
 
Datum datetimetz_timestamptz (PG_FUNCTION_ARGS)
 
static Datum timetz_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum timetz_part (PG_FUNCTION_ARGS)
 
Datum extract_timetz (PG_FUNCTION_ARGS)
 
Datum timetz_zone (PG_FUNCTION_ARGS)
 
Datum timetz_izone (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ TIMEADT_GT

#define TIMEADT_GT (   t1,
  t2 
)    (DatumGetTimeADT(t1) > DatumGetTimeADT(t2))

Referenced by overlaps_time().

◆ TIMEADT_LT

#define TIMEADT_LT (   t1,
  t2 
)    (DatumGetTimeADT(t1) < DatumGetTimeADT(t2))

Referenced by overlaps_time().

◆ TIMETZ_GT

#define TIMETZ_GT (   t1,
  t2 
)    DatumGetBool(DirectFunctionCall2(timetz_gt,t1,t2))

Referenced by overlaps_timetz().

◆ TIMETZ_LT

#define TIMETZ_LT (   t1,
  t2 
)    DatumGetBool(DirectFunctionCall2(timetz_lt,t1,t2))

Referenced by overlaps_timetz().

Function Documentation

◆ AdjustTimeForTypmod()

void AdjustTimeForTypmod ( TimeADT time,
int32  typmod 
)

Definition at line 1644 of file date.c.

References MAX_TIME_PRECISION.

Referenced by GetSQLCurrentTime(), GetSQLLocalTime(), parse_datetime(), time_in(), time_recv(), time_scale(), timetz_in(), timetz_recv(), and timetz_scale().

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 }
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ anytime_typmod_check()

int32 anytime_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 69 of file date.c.

References ereport, errcode(), errmsg(), ERROR, MAX_TIME_PRECISION, and WARNING.

Referenced by anytime_typmodin(), and transformSQLValueFunction().

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 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ anytime_typmodin()

static int32 anytime_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 48 of file date.c.

References anytime_typmod_check(), ArrayGetIntegerTypmods(), ereport, errcode(), errmsg(), and ERROR.

Referenced by timetypmodin(), and timetztypmodin().

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 }
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:69
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:231
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ anytime_typmodout()

static char* anytime_typmodout ( bool  istz,
int32  typmod 
)
static

Definition at line 91 of file date.c.

References psprintf().

Referenced by timetypmodout(), and timetztypmodout().

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 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46

◆ date2timestamp()

static TimestampTz date2timestamp ( DateADT  dateVal)
static

Definition at line 613 of file date.c.

References date2timestamp_opt_overflow().

Referenced by date_mi_interval(), date_pl_interval(), date_timestamp(), datetime_timestamp(), and in_range_date_interval().

614 {
615  return date2timestamp_opt_overflow(dateVal, NULL);
616 }
Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:569

◆ date2timestamp_no_overflow()

double date2timestamp_no_overflow ( DateADT  dateVal)

Definition at line 725 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, and USECS_PER_DAY.

Referenced by convert_timevalue_to_scalar().

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 }
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define USECS_PER_DAY
Definition: timestamp.h:91
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40

◆ date2timestamp_opt_overflow()

Timestamp date2timestamp_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 569 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, ereport, errcode(), errmsg(), ERROR, POSTGRES_EPOCH_JDATE, TIMESTAMP_END_JULIAN, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, and USECS_PER_DAY.

Referenced by date2timestamp(), and date_cmp_timestamp_internal().

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 }
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define USECS_PER_DAY
Definition: timestamp.h:91
int64 Timestamp
Definition: timestamp.h:38
#define ereport(elevel,...)
Definition: elog.h:157
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ date2timestamptz()

static TimestampTz date2timestamptz ( DateADT  dateVal)
static

Definition at line 709 of file date.c.

References date2timestamptz_opt_overflow().

Referenced by date_timestamptz().

710 {
711  return date2timestamptz_opt_overflow(dateVal, NULL);
712 }
TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:629

◆ date2timestamptz_opt_overflow()

TimestampTz date2timestamptz_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 629 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, DetermineTimeZoneOffset(), ereport, errcode(), errmsg(), ERROR, IS_VALID_TIMESTAMP, j2date(), MIN_TIMESTAMP, POSTGRES_EPOCH_JDATE, session_timezone, TIMESTAMP_END_JULIAN, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, USECS_PER_DAY, and USECS_PER_SEC.

Referenced by date2timestamptz(), and date_cmp_timestamptz_internal().

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 }
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
int tm_hour
Definition: pgtime.h:36
int errcode(int sqlerrcode)
Definition: elog.c:698
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
#define ERROR
Definition: elog.h:46
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1478
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define MIN_TIMESTAMP
Definition: timestamp.h:184
#define USECS_PER_DAY
Definition: timestamp.h:91
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
#define ereport(elevel,...)
Definition: elog.h:157
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
int tm_year
Definition: pgtime.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
int tm_sec
Definition: pgtime.h:34
int tm_min
Definition: pgtime.h:35

◆ date_cmp()

Datum date_cmp ( PG_FUNCTION_ARGS  )

Definition at line 429 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_INT32.

Referenced by compareDatetime(), gbt_datekey_cmp(), and leftmostvalue_date().

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 }
int32 DateADT
Definition: date.h:23
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_DATEADT(n)
Definition: date.h:61

◆ date_cmp_timestamp()

Datum date_cmp_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 819 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_INT32.

820 {
821  DateADT dateVal = PG_GETARG_DATEADT(0);
823 
825 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ date_cmp_timestamp_internal()

int32 date_cmp_timestamp_internal ( DateADT  dateVal,
Timestamp  dt2 
)

Definition at line 748 of file date.c.

References Assert, date2timestamp_opt_overflow(), timestamp_cmp_internal(), and TIMESTAMP_IS_NOEND.

Referenced by cmpDateToTimestamp(), date_cmp_timestamp(), date_eq_timestamp(), date_ge_timestamp(), date_gt_timestamp(), date_le_timestamp(), date_lt_timestamp(), date_ne_timestamp(), timestamp_cmp_date(), timestamp_eq_date(), timestamp_ge_date(), timestamp_gt_date(), timestamp_le_date(), timestamp_lt_date(), and timestamp_ne_date().

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 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2090
Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:569
int64 Timestamp
Definition: timestamp.h:38
#define Assert(condition)
Definition: c.h:804
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120

◆ date_cmp_timestamptz()

Datum date_cmp_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 903 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_INT32.

904 {
905  DateADT dateVal = PG_GETARG_DATEADT(0);
907 
909 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ date_cmp_timestamptz_internal()

int32 date_cmp_timestamptz_internal ( DateADT  dateVal,
TimestampTz  dt2 
)

Definition at line 828 of file date.c.

References date2timestamptz_opt_overflow(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, and timestamptz_cmp_internal.

Referenced by cmpDateToTimestampTz(), date_cmp_timestamptz(), date_eq_timestamptz(), date_ge_timestamptz(), date_gt_timestamptz(), date_le_timestamptz(), date_lt_timestamptz(), date_ne_timestamptz(), timestamptz_cmp_date(), timestamptz_eq_date(), timestamptz_ge_date(), timestamptz_gt_date(), timestamptz_le_date(), timestamptz_lt_date(), and timestamptz_ne_date().

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 }
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:629
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:100
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115

◆ date_eq()

Datum date_eq ( PG_FUNCTION_ARGS  )

Definition at line 375 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_dateeq().

376 {
377  DateADT dateVal1 = PG_GETARG_DATEADT(0);
378  DateADT dateVal2 = PG_GETARG_DATEADT(1);
379 
380  PG_RETURN_BOOL(dateVal1 == dateVal2);
381 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

◆ date_eq_timestamp()

Datum date_eq_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 765 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_BOOL.

766 {
767  DateADT dateVal = PG_GETARG_DATEADT(0);
769 
770  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) == 0);
771 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ date_eq_timestamptz()

Datum date_eq_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 849 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_BOOL.

850 {
851  DateADT dateVal = PG_GETARG_DATEADT(0);
853 
854  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) == 0);
855 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ date_fastcmp()

static int date_fastcmp ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 442 of file date.c.

References DatumGetDateADT.

Referenced by date_sortsupport().

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 }
#define DatumGetDateADT(X)
Definition: date.h:53
int32 DateADT
Definition: date.h:23

◆ date_finite()

Datum date_finite ( PG_FUNCTION_ARGS  )

Definition at line 464 of file date.c.

References DATE_NOT_FINITE, PG_GETARG_DATEADT, and PG_RETURN_BOOL.

465 {
467 
469 }
int32 DateADT
Definition: date.h:23
long date
Definition: pgtypes_date.h:9
#define DATE_NOT_FINITE(j)
Definition: date.h:43
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

◆ date_ge()

Datum date_ge ( PG_FUNCTION_ARGS  )

Definition at line 420 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_datege().

421 {
422  DateADT dateVal1 = PG_GETARG_DATEADT(0);
423  DateADT dateVal2 = PG_GETARG_DATEADT(1);
424 
425  PG_RETURN_BOOL(dateVal1 >= dateVal2);
426 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

◆ date_ge_timestamp()

Datum date_ge_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 810 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_BOOL.

811 {
812  DateADT dateVal = PG_GETARG_DATEADT(0);
814 
815  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) >= 0);
816 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ date_ge_timestamptz()

Datum date_ge_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 894 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_BOOL.

895 {
896  DateADT dateVal = PG_GETARG_DATEADT(0);
898 
899  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) >= 0);
900 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ date_gt()

Datum date_gt ( PG_FUNCTION_ARGS  )

Definition at line 411 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_dategt().

412 {
413  DateADT dateVal1 = PG_GETARG_DATEADT(0);
414  DateADT dateVal2 = PG_GETARG_DATEADT(1);
415 
416  PG_RETURN_BOOL(dateVal1 > dateVal2);
417 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

◆ date_gt_timestamp()

Datum date_gt_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 792 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_BOOL.

793 {
794  DateADT dateVal = PG_GETARG_DATEADT(0);
796 
797  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) > 0);
798 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ date_gt_timestamptz()

Datum date_gt_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 876 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_BOOL.

877 {
878  DateADT dateVal = PG_GETARG_DATEADT(0);
880 
882 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ date_in()

Datum date_in ( PG_FUNCTION_ARGS  )

Definition at line 111 of file date.c.

References date2j(), DATE_NOBEGIN, DATE_NOEND, DateTimeParseError(), DecodeDateTime(), DTERR_BAD_FORMAT, DTK_DATE, DTK_EARLY, DTK_EPOCH, DTK_LATE, ereport, errcode(), errmsg(), ERROR, GetEpochTime(), IS_VALID_DATE, IS_VALID_JULIAN, MAXDATEFIELDS, MAXDATELEN, ParseDateTime(), PG_GETARG_CSTRING, PG_RETURN_DATEADT, POSTGRES_EPOCH_JDATE, generate_unaccent_rules::str, tm, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

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 }
#define MAXDATELEN
Definition: datetime.h:201
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3764
int32 DateADT
Definition: date.h:23
int errcode(int sqlerrcode)
Definition: elog.c:698
long date
Definition: pgtypes_date.h:9
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
#define DATE_NOBEGIN(j)
Definition: date.h:39
#define IS_VALID_DATE(d)
Definition: timestamp.h:190
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:803
#define ERROR
Definition: elog.h:46
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
int32 fsec_t
Definition: timestamp.h:41
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:155
#define DTK_LATE
Definition: datetime.h:152
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define ereport(elevel,...)
Definition: elog.h:157
#define DTK_EARLY
Definition: datetime.h:151
#define MAXDATEFIELDS
Definition: datetime.h:203
#define DTK_EPOCH
Definition: datetime.h:153
int tm_year
Definition: pgtime.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:909
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2048
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
#define PG_RETURN_DATEADT(x)
Definition: date.h:65
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:582
#define DTK_DATE
Definition: datetime.h:145
#define DATE_NOEND(j)
Definition: date.h:41

◆ date_larger()

Datum date_larger ( PG_FUNCTION_ARGS  )

Definition at line 472 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_DATEADT.

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 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_DATEADT(x)
Definition: date.h:65

◆ date_le()

Datum date_le ( PG_FUNCTION_ARGS  )

Definition at line 402 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_datele().

403 {
404  DateADT dateVal1 = PG_GETARG_DATEADT(0);
405  DateADT dateVal2 = PG_GETARG_DATEADT(1);
406 
407  PG_RETURN_BOOL(dateVal1 <= dateVal2);
408 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

◆ date_le_timestamp()

Datum date_le_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 801 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_BOOL.

802 {
803  DateADT dateVal = PG_GETARG_DATEADT(0);
805 
806  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) <= 0);
807 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ date_le_timestamptz()

Datum date_le_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 885 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_BOOL.

886 {
887  DateADT dateVal = PG_GETARG_DATEADT(0);
889 
890  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) <= 0);
891 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ date_lt()

Datum date_lt ( PG_FUNCTION_ARGS  )

Definition at line 393 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_datelt().

394 {
395  DateADT dateVal1 = PG_GETARG_DATEADT(0);
396  DateADT dateVal2 = PG_GETARG_DATEADT(1);
397 
398  PG_RETURN_BOOL(dateVal1 < dateVal2);
399 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

◆ date_lt_timestamp()

Datum date_lt_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 783 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_BOOL.

784 {
785  DateADT dateVal = PG_GETARG_DATEADT(0);
787 
788  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) < 0);
789 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ date_lt_timestamptz()

Datum date_lt_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 867 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_BOOL.

868 {
869  DateADT dateVal = PG_GETARG_DATEADT(0);
871 
873 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ date_mi()

Datum date_mi ( PG_FUNCTION_ARGS  )

Definition at line 492 of file date.c.

References DATE_NOT_FINITE, ereport, errcode(), errmsg(), ERROR, PG_GETARG_DATEADT, and PG_RETURN_INT32.

Referenced by date_dist(), gbt_date_penalty(), and gdb_date_dist().

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 }
int32 DateADT
Definition: date.h:23
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:46
#define DATE_NOT_FINITE(j)
Definition: date.h:43
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ date_mi_interval()

Datum date_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 1270 of file date.c.

References date2timestamp(), DirectFunctionCall2, PG_GETARG_DATEADT, PG_GETARG_INTERVAL_P, PointerGetDatum, timestamp_mi_interval(), and TimestampGetDatum.

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 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int32 DateADT
Definition: date.h:23
#define PointerGetDatum(X)
Definition: postgres.h:600
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2917
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
static TimestampTz date2timestamp(DateADT dateVal)
Definition: date.c:613
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628

◆ date_mii()

Datum date_mii ( PG_FUNCTION_ARGS  )

Definition at line 533 of file date.c.

References DATE_NOT_FINITE, days, ereport, errcode(), errmsg(), ERROR, IS_VALID_DATE, PG_GETARG_DATEADT, PG_GETARG_INT32, and PG_RETURN_DATEADT.

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int32 DateADT
Definition: date.h:23
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
#define IS_VALID_DATE(d)
Definition: timestamp.h:190
#define ERROR
Definition: elog.h:46
#define DATE_NOT_FINITE(j)
Definition: date.h:43
const char *const days[]
Definition: datetime.c:68
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define PG_RETURN_DATEADT(x)
Definition: date.h:65

◆ date_ne()

Datum date_ne ( PG_FUNCTION_ARGS  )

Definition at line 384 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

385 {
386  DateADT dateVal1 = PG_GETARG_DATEADT(0);
387  DateADT dateVal2 = PG_GETARG_DATEADT(1);
388 
389  PG_RETURN_BOOL(dateVal1 != dateVal2);
390 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

◆ date_ne_timestamp()

Datum date_ne_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 774 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_BOOL.

775 {
776  DateADT dateVal = PG_GETARG_DATEADT(0);
778 
779  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) != 0);
780 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ date_ne_timestamptz()

Datum date_ne_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 858 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_BOOL.

859 {
860  DateADT dateVal = PG_GETARG_DATEADT(0);
862 
863  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) != 0);
864 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ date_out()

Datum date_out ( PG_FUNCTION_ARGS  )

Definition at line 176 of file date.c.

References buf, DATE_NOT_FINITE, DateStyle, EncodeDateOnly(), EncodeSpecialDate(), j2date(), MAXDATELEN, PG_GETARG_DATEADT, PG_RETURN_CSTRING, POSTGRES_EPOCH_JDATE, pstrdup(), tm, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

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 }
#define MAXDATELEN
Definition: datetime.h:201
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:3883
int32 DateADT
Definition: date.h:23
char * pstrdup(const char *in)
Definition: mcxt.c:1299
long date
Definition: pgtypes_date.h:9
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
#define DATE_NOT_FINITE(j)
Definition: date.h:43
int tm_mday
Definition: pgtime.h:37
static char * buf
Definition: pg_test_fsync.c:68
int tm_mon
Definition: pgtime.h:38
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:286
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
int DateStyle
Definition: globals.c:118
int tm_year
Definition: pgtime.h:39
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ date_pl_interval()

Datum date_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 1250 of file date.c.

References date2timestamp(), DirectFunctionCall2, PG_GETARG_DATEADT, PG_GETARG_INTERVAL_P, PointerGetDatum, timestamp_pl_interval(), and TimestampGetDatum.

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 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int32 DateADT
Definition: date.h:23
#define PointerGetDatum(X)
Definition: postgres.h:600
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2838
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
static TimestampTz date2timestamp(DateADT dateVal)
Definition: date.c:613
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628

◆ date_pli()

Datum date_pli ( PG_FUNCTION_ARGS  )

Definition at line 509 of file date.c.

References DATE_NOT_FINITE, days, ereport, errcode(), errmsg(), ERROR, IS_VALID_DATE, PG_GETARG_DATEADT, PG_GETARG_INT32, and PG_RETURN_DATEADT.

Referenced by daterange_canonical().

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int32 DateADT
Definition: date.h:23
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
#define IS_VALID_DATE(d)
Definition: timestamp.h:190
#define ERROR
Definition: elog.h:46
#define DATE_NOT_FINITE(j)
Definition: date.h:43
const char *const days[]
Definition: datetime.c:68
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define PG_RETURN_DATEADT(x)
Definition: date.h:65

◆ date_recv()

Datum date_recv ( PG_FUNCTION_ARGS  )

Definition at line 201 of file date.c.

References buf, DATE_NOT_FINITE, ereport, errcode(), errmsg(), ERROR, IS_VALID_DATE, PG_GETARG_POINTER, PG_RETURN_DATEADT, and pq_getmsgint().

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 }
int32 DateADT
Definition: date.h:23
StringInfoData * StringInfo
Definition: stringinfo.h:44
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define IS_VALID_DATE(d)
Definition: timestamp.h:190
#define ERROR
Definition: elog.h:46
#define DATE_NOT_FINITE(j)
Definition: date.h:43
static char * buf
Definition: pg_test_fsync.c:68
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define PG_RETURN_DATEADT(x)
Definition: date.h:65

◆ date_send()

Datum date_send ( PG_FUNCTION_ARGS  )

Definition at line 223 of file date.c.

References buf, PG_GETARG_DATEADT, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendint32().

224 {
227 
228  pq_begintypsend(&buf);
229  pq_sendint32(&buf, date);
231 }
int32 DateADT
Definition: date.h:23
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
long date
Definition: pgtypes_date.h:9
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_DATEADT(n)
Definition: date.h:61

◆ date_smaller()

Datum date_smaller ( PG_FUNCTION_ARGS  )

Definition at line 481 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_DATEADT.

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 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_DATEADT(x)
Definition: date.h:65

◆ date_sortsupport()

Datum date_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 455 of file date.c.

References SortSupportData::comparator, date_fastcmp(), PG_GETARG_POINTER, and PG_RETURN_VOID.

456 {
458 
459  ssup->comparator = date_fastcmp;
460  PG_RETURN_VOID();
461 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static int date_fastcmp(Datum x, Datum y, SortSupport ssup)
Definition: date.c:442
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
#define PG_RETURN_VOID()
Definition: fmgr.h:349

◆ date_timestamp()

Datum date_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1287 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

1288 {
1289  DateADT dateVal = PG_GETARG_DATEADT(0);
1290  Timestamp result;
1291 
1292  result = date2timestamp(dateVal);
1293 
1294  PG_RETURN_TIMESTAMP(result);
1295 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39
static TimestampTz date2timestamp(DateADT dateVal)
Definition: date.c:613

◆ date_timestamptz()

Datum date_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 1331 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

1332 {
1333  DateADT dateVal = PG_GETARG_DATEADT(0);
1334  TimestampTz result;
1335 
1336  result = date2timestamptz(dateVal);
1337 
1338  PG_RETURN_TIMESTAMP(result);
1339 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:709
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39

◆ datetime_timestamp()

Datum datetime_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1965 of file date.c.

References date2timestamp(), ereport, errcode(), errmsg(), ERROR, IS_VALID_TIMESTAMP, PG_GETARG_DATEADT, PG_GETARG_TIMEADT, PG_RETURN_TIMESTAMP, and TIMESTAMP_NOT_FINITE.

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 }
int32 DateADT
Definition: date.h:23
int errcode(int sqlerrcode)
Definition: elog.c:698
long date
Definition: pgtypes_date.h:9
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:46
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:909
static TimestampTz date2timestamp(DateADT dateVal)
Definition: date.c:613

◆ datetimetz_timestamptz()

Datum datetimetz_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 2859 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, ereport, errcode(), errmsg(), ERROR, IS_VALID_TIMESTAMP, PG_GETARG_DATEADT, PG_GETARG_TIMETZADT_P, PG_RETURN_TIMESTAMP, POSTGRES_EPOCH_JDATE, TimeTzADT::time, TIMESTAMP_END_JULIAN, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, USECS_PER_DAY, USECS_PER_SEC, and TimeTzADT::zone.

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 }
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
int errcode(int sqlerrcode)
Definition: elog.c:698
long date
Definition: pgtypes_date.h:9
#define ERROR
Definition: elog.h:46
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define USECS_PER_DAY
Definition: timestamp.h:91
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define ereport(elevel,...)
Definition: elog.h:157
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
Definition: date.h:27

◆ EncodeSpecialDate()

void EncodeSpecialDate ( DateADT  dt,
char *  str 
)

Definition at line 286 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, EARLY, elog, ERROR, and LATE.

Referenced by date_out(), and JsonEncodeDateTime().

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 }
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define LATE
Definition: datetime.h:41
#define ERROR
Definition: elog.h:46
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
#define elog(elevel,...)
Definition: elog.h:232
#define EARLY
Definition: datetime.h:40

◆ extract_date()

Datum extract_date ( PG_FUNCTION_ARGS  )

Definition at line 1071 of file date.c.

References CStringGetDatum, date2isoweek(), date2isoyear(), date2j(), DATE_IS_NOBEGIN, DATE_NOT_FINITE, DatumGetNumeric, DecodeSpecial(), DecodeUnits(), DirectFunctionCall3, downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_DOW, DTK_DOY, DTK_EPOCH, DTK_ISODOW, DTK_ISOYEAR, DTK_JULIAN, DTK_MILLENNIUM, DTK_MONTH, DTK_QUARTER, DTK_WEEK, DTK_YEAR, ereport, errcode(), errmsg(), ERROR, Int32GetDatum, int64_to_numeric(), InvalidOid, j2date(), j2day(), numeric_in(), ObjectIdGetDatum, PG_GETARG_DATEADT, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_NUMERIC, POSTGRES_EPOCH_JDATE, RESERV, SECS_PER_DAY, generate_unaccent_rules::type, UNITS, UNIX_EPOCH_JDATE, UNKNOWN_FIELD, val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

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 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:64
#define DTK_CENTURY
Definition: datetime.h:170
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define DTK_JULIAN
Definition: datetime.h:174
#define DTK_WEEK
Definition: datetime.h:165
#define DTK_YEAR
Definition: datetime.h:168
int32 DateADT
Definition: date.h:23
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:37
#define DTK_QUARTER
Definition: datetime.h:167
#define DTK_MILLENNIUM
Definition: datetime.h:171
int errcode(int sqlerrcode)
Definition: elog.c:698
#define DTK_ISODOW
Definition: datetime.h:181
#define UNKNOWN_FIELD
Definition: datetime.h:125
long date
Definition: pgtypes_date.h:9
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3727
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_DECADE
Definition: datetime.h:169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define SECS_PER_DAY
Definition: timestamp.h:86
#define DATE_NOT_FINITE(j)
Definition: date.h:43
int j2day(int date)
Definition: datetime.c:327
int date2isoweek(int year, int mon, int mday)
Definition: timestamp.c:4429
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4484
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4136
#define CStringGetDatum(X)
Definition: postgres.h:622
#define DTK_ISOYEAR
Definition: datetime.h:180
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:616
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:630
#define DTK_DOW
Definition: datetime.h:176
#define InvalidOid
Definition: postgres_ext.h:36
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
#define DatumGetNumeric(X)
Definition: numeric.h:59
#define DTK_DAY
Definition: datetime.h:164
#define RESERV
Definition: datetime.h:91
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3040
#define DTK_EPOCH
Definition: datetime.h:153
#define Int32GetDatum(X)
Definition: postgres.h:523
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
long val
Definition: informix.c:664
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ extract_time()

Datum extract_time ( PG_FUNCTION_ARGS  )

Definition at line 2240 of file date.c.

References time_part_common().

2241 {
2242  return time_part_common(fcinfo, true);
2243 }
static Datum time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2131

◆ extract_timetz()

Datum extract_timetz ( PG_FUNCTION_ARGS  )

Definition at line 3023 of file date.c.

References timetz_part_common().

3024 {
3025  return timetz_part_common(fcinfo, true);
3026 }
static Datum timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2900

◆ float_time_overflows()

bool float_time_overflows ( int  hour,
int  min,
double  sec 
)

Definition at line 1450 of file date.c.

References HOURS_PER_DAY, MINS_PER_HOUR, SECS_PER_MINUTE, USECS_PER_DAY, and USECS_PER_SEC.

Referenced by make_time(), and make_timestamp_internal().

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 }
#define USECS_PER_SEC
Definition: timestamp.h:94
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define HOURS_PER_DAY
Definition: timestamp.h:78
#define SECS_PER_MINUTE
Definition: timestamp.h:88
#define USECS_PER_DAY
Definition: timestamp.h:91

◆ GetSQLCurrentDate()

DateADT GetSQLCurrentDate ( void  )

Definition at line 301 of file date.c.

References date2j(), GetCurrentDateTime(), POSTGRES_EPOCH_JDATE, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

Referenced by ExecEvalSQLValueFunction().

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 
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  {
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 }
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:349
int32 DateADT
Definition: date.h:23
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
int date2j(int y, int m, int d)
Definition: datetime.c:269
int tm_year
Definition: pgtime.h:39
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ GetSQLCurrentTime()

TimeTzADT* GetSQLCurrentTime ( int32  typmod)

Definition at line 334 of file date.c.

References AdjustTimeForTypmod(), GetCurrentTimeUsec(), palloc(), TimeTzADT::time, tm, and tm2timetz().

Referenced by ExecEvalSQLValueFunction().

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 }
TimeADT time
Definition: date.h:29
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2254
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:370
int32 fsec_t
Definition: timestamp.h:41
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: date.h:27
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1644

◆ GetSQLLocalTime()

TimeADT GetSQLLocalTime ( int32  typmod)

Definition at line 354 of file date.c.

References AdjustTimeForTypmod(), GetCurrentTimeUsec(), tm, and tm2time().

Referenced by ExecEvalSQLValueFunction().

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 }
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:370
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1415
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1644

◆ in_range_date_interval()

Datum in_range_date_interval ( PG_FUNCTION_ARGS  )

Definition at line 1044 of file date.c.

References BoolGetDatum, date2timestamp(), DirectFunctionCall5, in_range_timestamp_interval(), IntervalPGetDatum, PG_GETARG_BOOL, PG_GETARG_DATEADT, PG_GETARG_INTERVAL_P, TimestampGetDatum, and val.

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 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int32 DateADT
Definition: date.h:23
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define TimestampGetDatum(X)
Definition: timestamp.h:31
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:634
int64 Timestamp
Definition: timestamp.h:38
#define BoolGetDatum(X)
Definition: postgres.h:446
Datum in_range_timestamp_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3346
static TimestampTz date2timestamp(DateADT dateVal)
Definition: date.c:613
long val
Definition: informix.c:664

◆ in_range_time_interval()

Datum in_range_time_interval ( PG_FUNCTION_ARGS  )

Definition at line 2091 of file date.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_BOOL, PG_GETARG_INTERVAL_P, PG_GETARG_TIMEADT, PG_RETURN_BOOL, Interval::time, and val.

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 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define ERROR
Definition: elog.h:46
TimeOffset time
Definition: timestamp.h:45
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
int errmsg(const char *fmt,...)
Definition: elog.c:909
long val
Definition: informix.c:664

◆ in_range_timetz_interval()

Datum in_range_timetz_interval ( PG_FUNCTION_ARGS  )

Definition at line 2625 of file date.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_BOOL, PG_GETARG_INTERVAL_P, PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, TimeTzADT::time, Interval::time, timetz_cmp_internal(), val, and TimeTzADT::zone.

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 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
TimeADT time
Definition: date.h:29
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2428
#define ERROR
Definition: elog.h:46
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: date.h:27
long val
Definition: informix.c:664

◆ interval_time()

Datum interval_time ( PG_FUNCTION_ARGS  )

Definition at line 2011 of file date.c.

References days, PG_GETARG_INTERVAL_P, PG_RETURN_TIMEADT, Interval::time, and USECS_PER_DAY.

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 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
TimeOffset time
Definition: timestamp.h:45
const char *const days[]
Definition: datetime.c:68
int64 TimeADT
Definition: date.h:25
#define USECS_PER_DAY
Definition: timestamp.h:91

◆ make_date()

Datum make_date ( PG_FUNCTION_ARGS  )

Definition at line 237 of file date.c.

References date2j(), DTK_DATE_M, ereport, errcode(), errmsg(), ERROR, IS_VALID_DATE, IS_VALID_JULIAN, PG_GETARG_INT32, PG_RETURN_DATEADT, POSTGRES_EPOCH_JDATE, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, and ValidateDate().

238 {
239  struct pg_tm tm;
240  DateADT date;
241  int dterr;
242  bool bc = false;
243 
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 */
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 
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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int32 DateADT
Definition: date.h:23
int errcode(int sqlerrcode)
Definition: elog.c:698
long date
Definition: pgtypes_date.h:9
Definition: pgtime.h:32
#define DTK_DATE_M
Definition: datetime.h:192
static struct pg_tm tm
Definition: localtime.c:102
#define IS_VALID_DATE(d)
Definition: timestamp.h:190
#define ERROR
Definition: elog.h:46
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:155
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define ereport(elevel,...)
Definition: elog.h:157
int tm_year
Definition: pgtime.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
#define PG_RETURN_DATEADT(x)
Definition: date.h:65
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2480

◆ make_time()

Datum make_time ( PG_FUNCTION_ARGS  )

Definition at line 1576 of file date.c.

References ereport, errcode(), errmsg(), ERROR, float_time_overflows(), MINS_PER_HOUR, PG_GETARG_FLOAT8, PG_GETARG_INT32, PG_RETURN_TIMEADT, SECS_PER_MINUTE, pg_tm::tm_hour, pg_tm::tm_min, and USECS_PER_SEC.

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 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:36
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
int errcode(int sqlerrcode)
Definition: elog.c:698
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define ERROR
Definition: elog.h:46
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int64 TimeADT
Definition: date.h:25
#define ereport(elevel,...)
Definition: elog.h:157
bool float_time_overflows(int hour, int min, double sec)
Definition: date.c:1450
int errmsg(const char *fmt,...)
Definition: elog.c:909
int tm_min
Definition: pgtime.h:35

◆ overlaps_time()

Datum overlaps_time ( PG_FUNCTION_ARGS  )

Definition at line 1782 of file date.c.

References PG_ARGISNULL, PG_GETARG_DATUM, PG_RETURN_BOOL, PG_RETURN_NULL, TIMEADT_GT, and TIMEADT_LT.

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 }
#define TIMEADT_LT(t1, t2)
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
uintptr_t Datum
Definition: postgres.h:411
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define TIMEADT_GT(t1, t2)
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ overlaps_timetz()

Datum overlaps_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2668 of file date.c.

References PG_ARGISNULL, PG_GETARG_DATUM, PG_RETURN_BOOL, PG_RETURN_NULL, TIMETZ_GT, and TIMETZ_LT.

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 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define TIMETZ_LT(t1, t2)
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
uintptr_t Datum
Definition: postgres.h:411
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define TIMETZ_GT(t1, t2)

◆ time2tm()

int time2tm ( TimeADT  time,
struct pg_tm tm,
fsec_t fsec 
)

Definition at line 1487 of file date.c.

References pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by JsonEncodeDateTime(), time_out(), time_part_common(), and time_timetz().

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 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:36
#define USECS_PER_MINUTE
Definition: timestamp.h:93
#define USECS_PER_HOUR
Definition: timestamp.h:92
int tm_sec
Definition: pgtime.h:34
int tm_min
Definition: pgtime.h:35

◆ time_cmp()

Datum time_cmp ( PG_FUNCTION_ARGS  )

Definition at line 1733 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_INT32.

Referenced by compareDatetime(), gbt_timekey_cmp(), and leftmostvalue_time().

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 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_eq()

Datum time_eq ( PG_FUNCTION_ARGS  )

Definition at line 1679 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timeeq().

1680 {
1681  TimeADT time1 = PG_GETARG_TIMEADT(0);
1682  TimeADT time2 = PG_GETARG_TIMEADT(1);
1683 
1684  PG_RETURN_BOOL(time1 == time2);
1685 }
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_ge()

Datum time_ge ( PG_FUNCTION_ARGS  )

Definition at line 1724 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timege().

1725 {
1726  TimeADT time1 = PG_GETARG_TIMEADT(0);
1727  TimeADT time2 = PG_GETARG_TIMEADT(1);
1728 
1729  PG_RETURN_BOOL(time1 >= time2);
1730 }
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_gt()

Datum time_gt ( PG_FUNCTION_ARGS  )

Definition at line 1715 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timegt().

1716 {
1717  TimeADT time1 = PG_GETARG_TIMEADT(0);
1718  TimeADT time2 = PG_GETARG_TIMEADT(1);
1719 
1720  PG_RETURN_BOOL(time1 > time2);
1721 }
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_hash()

Datum time_hash ( PG_FUNCTION_ARGS  )

Definition at line 1746 of file date.c.

References hashint8().

1747 {
1748  return hashint8(fcinfo);
1749 }
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:84

◆ time_hash_extended()

Datum time_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 1752 of file date.c.

References hashint8extended().

1753 {
1754  return hashint8extended(fcinfo);
1755 }
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:104

◆ time_in()

Datum time_in ( PG_FUNCTION_ARGS  )

Definition at line 1378 of file date.c.

References AdjustTimeForTypmod(), DateTimeParseError(), DecodeTimeOnly(), MAXDATEFIELDS, MAXDATELEN, ParseDateTime(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_TIMEADT, generate_unaccent_rules::str, tm, and tm2time().

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 }
#define MAXDATELEN
Definition: datetime.h:201
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3764
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
Definition: pgtime.h:32
unsigned int Oid
Definition: postgres_ext.h:31
int DecodeTimeOnly(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:1754
signed int int32
Definition: c.h:429
static struct pg_tm tm
Definition: localtime.c:102
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1415
#define MAXDATEFIELDS
Definition: datetime.h:203
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:582
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1644

◆ time_interval()

Datum time_interval ( PG_FUNCTION_ARGS  )

Definition at line 1988 of file date.c.

References Interval::day, Interval::month, palloc(), PG_GETARG_TIMEADT, PG_RETURN_INTERVAL_P, and Interval::time.

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 }
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
int64 TimeADT
Definition: date.h:25
int32 month
Definition: timestamp.h:48
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
void * palloc(Size size)
Definition: mcxt.c:1062

◆ time_larger()

Datum time_larger ( PG_FUNCTION_ARGS  )

Definition at line 1758 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

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 }
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_le()

Datum time_le ( PG_FUNCTION_ARGS  )

Definition at line 1706 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timele().

1707 {
1708  TimeADT time1 = PG_GETARG_TIMEADT(0);
1709  TimeADT time2 = PG_GETARG_TIMEADT(1);
1710 
1711  PG_RETURN_BOOL(time1 <= time2);
1712 }
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_lt()

Datum time_lt ( PG_FUNCTION_ARGS  )

Definition at line 1697 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timelt().

1698 {
1699  TimeADT time1 = PG_GETARG_TIMEADT(0);
1700  TimeADT time2 = PG_GETARG_TIMEADT(1);
1701 
1702  PG_RETURN_BOOL(time1 < time2);
1703 }
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_mi_interval()

Datum time_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 2073 of file date.c.

References PG_GETARG_INTERVAL_P, PG_GETARG_TIMEADT, PG_RETURN_TIMEADT, Interval::time, and USECS_PER_DAY.

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 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
TimeOffset time
Definition: timestamp.h:45
int64 TimeADT
Definition: date.h:25
#define USECS_PER_DAY
Definition: timestamp.h:91
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_mi_time()

Datum time_mi_time ( PG_FUNCTION_ARGS  )

Definition at line 2036 of file date.c.

References Interval::day, Interval::month, palloc(), PG_GETARG_TIMEADT, PG_RETURN_INTERVAL_P, and Interval::time.

Referenced by gbt_time_dist(), gbt_time_penalty(), and time_dist().

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 }
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
int64 TimeADT
Definition: date.h:25
int32 month
Definition: timestamp.h:48
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
void * palloc(Size size)
Definition: mcxt.c:1062

◆ time_ne()

Datum time_ne ( PG_FUNCTION_ARGS  )

Definition at line 1688 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

1689 {
1690  TimeADT time1 = PG_GETARG_TIMEADT(0);
1691  TimeADT time2 = PG_GETARG_TIMEADT(1);
1692 
1693  PG_RETURN_BOOL(time1 != time2);
1694 }
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_out()

Datum time_out ( PG_FUNCTION_ARGS  )

Definition at line 1500 of file date.c.

References buf, DateStyle, EncodeTimeOnly(), MAXDATELEN, PG_GETARG_TIMEADT, PG_RETURN_CSTRING, pstrdup(), time2tm(), and tm.

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 }
#define MAXDATELEN
Definition: datetime.h:201
char * pstrdup(const char *in)
Definition: mcxt.c:1299
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:3968
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1487
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
static char * buf
Definition: pg_test_fsync.c:68
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
int DateStyle
Definition: globals.c:118

◆ time_overflows()

bool time_overflows ( int  hour,
int  min,
int  sec,
fsec_t  fsec 
)

Definition at line 1426 of file date.c.

References HOURS_PER_DAY, MINS_PER_HOUR, SECS_PER_MINUTE, USECS_PER_DAY, and USECS_PER_SEC.

Referenced by DecodeDateTime(), and DecodeTimeOnly().

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 }
#define USECS_PER_SEC
Definition: timestamp.h:94
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define HOURS_PER_DAY
Definition: timestamp.h:78
#define SECS_PER_MINUTE
Definition: timestamp.h:88
#define USECS_PER_DAY
Definition: timestamp.h:91

◆ time_part()

Datum time_part ( PG_FUNCTION_ARGS  )

Definition at line 2234 of file date.c.

References time_part_common().

2235 {
2236  return time_part_common(fcinfo, false);
2237 }
static Datum time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2131

◆ time_part_common()

static Datum time_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2131 of file date.c.

References DecodeSpecial(), DecodeUnits(), downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_EPOCH, DTK_HOUR, DTK_ISOYEAR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_TZ, DTK_TZ_HOUR, DTK_TZ_MINUTE, DTK_YEAR, ereport, errcode(), errmsg(), ERROR, int64_div_fast_to_numeric(), int64_to_numeric(), PG_GETARG_TEXT_PP, PG_GETARG_TIMEADT, PG_RETURN_FLOAT8, PG_RETURN_NUMERIC, RESERV, time2tm(), tm, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, generate_unaccent_rules::type, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by extract_time(), and time_part().

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 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:64
#define DTK_TZ_HOUR
Definition: datetime.h:178
#define DTK_CENTURY
Definition: datetime.h:170
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define DTK_YEAR
Definition: datetime.h:168
int tm_hour
Definition: pgtime.h:36
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:37
#define DTK_QUARTER
Definition: datetime.h:167
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define DTK_MILLENNIUM
Definition: datetime.h:171
int errcode(int sqlerrcode)
Definition: elog.c:698
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1487
#define UNKNOWN_FIELD
Definition: datetime.h:125
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3727
Definition: pgtime.h:32
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static struct pg_tm tm
Definition: localtime.c:102
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_MILLISEC
Definition: datetime.h:172
#define DTK_DECADE
Definition: datetime.h:169
#define DTK_TZ
Definition: datetime.h:147
#define DTK_HOUR
Definition: datetime.h:163
#define ERROR
Definition: elog.h:46
#define DTK_SECOND
Definition: datetime.h:161
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4136
#define DTK_ISOYEAR
Definition: datetime.h:180
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
#define DTK_TZ_MINUTE
Definition: datetime.h:179
#define DTK_MINUTE
Definition: datetime.h:162
#define DTK_MICROSEC
Definition: datetime.h:173
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
#define DTK_DAY
Definition: datetime.h:164
#define RESERV
Definition: datetime.h:91
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3040
#define DTK_EPOCH
Definition: datetime.h:153
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621
int tm_sec
Definition: pgtime.h:34
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4157
int tm_min
Definition: pgtime.h:35
long val
Definition: informix.c:664

◆ time_pl_interval()

Datum time_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 2055 of file date.c.

References PG_GETARG_INTERVAL_P, PG_GETARG_TIMEADT, PG_RETURN_TIMEADT, Interval::time, and USECS_PER_DAY.

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 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
TimeOffset time
Definition: timestamp.h:45
int64 TimeADT
Definition: date.h:25
#define USECS_PER_DAY
Definition: timestamp.h:91
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_recv()

Datum time_recv ( PG_FUNCTION_ARGS  )

Definition at line 1520 of file date.c.

References AdjustTimeForTypmod(), buf, ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_TIMEADT, pq_getmsgint64(), and USECS_PER_DAY.

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
StringInfoData * StringInfo
Definition: stringinfo.h:44
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:46
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
int64 TimeADT
Definition: date.h:25
#define USECS_PER_DAY
Definition: timestamp.h:91
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1644

◆ time_scale()

Datum time_scale ( PG_FUNCTION_ARGS  )

Definition at line 1624 of file date.c.

References AdjustTimeForTypmod(), PG_GETARG_INT32, PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
signed int int32
Definition: c.h:429
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1644

◆ time_send()

Datum time_send ( PG_FUNCTION_ARGS  )

Definition at line 1546 of file date.c.

References buf, PG_GETARG_TIMEADT, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendint64().

1547 {
1548  TimeADT time = PG_GETARG_TIMEADT(0);
1550 
1551  pq_begintypsend(&buf);
1552  pq_sendint64(&buf, time);
1554 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static char * buf
Definition: pg_test_fsync.c:68
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_smaller()

Datum time_smaller ( PG_FUNCTION_ARGS  )

Definition at line 1767 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

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 }
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_support()

Datum time_support ( PG_FUNCTION_ARGS  )

Definition at line 1604 of file date.c.

References SupportRequestSimplify::fcall, IsA, MAX_TIME_PRECISION, PG_GETARG_POINTER, PG_RETURN_POINTER, and TemporalSimplify().

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 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define IsA(nodeptr, _type_)
Definition: nodes.h:589
Definition: nodes.h:538
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
Node * TemporalSimplify(int32 max_precis, Node *node)
Definition: datetime.c:4494
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ time_timetz()

Datum time_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2801 of file date.c.

References DetermineTimeZoneOffset(), GetCurrentDateTime(), palloc(), PG_GETARG_TIMEADT, PG_RETURN_TIMETZADT_P, session_timezone, TimeTzADT::time, time2tm(), tm, and TimeTzADT::zone.

Referenced by castTimeToTimeTz().

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 }
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:349
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
TimeADT time
Definition: date.h:29
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1487
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
int32 zone
Definition: date.h:30
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1478
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: date.h:27

◆ timestamp_cmp_date()

Datum timestamp_cmp_date ( PG_FUNCTION_ARGS  )

Definition at line 966 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_INT32.

967 {
969  DateADT dateVal = PG_GETARG_DATEADT(1);
970 
972 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ timestamp_date()

Datum timestamp_date ( PG_FUNCTION_ARGS  )

Definition at line 1301 of file date.c.

References date2j(), DATE_NOBEGIN, DATE_NOEND, ereport, errcode(), errmsg(), ERROR, PG_GETARG_TIMESTAMP, PG_RETURN_DATEADT, POSTGRES_EPOCH_JDATE, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, tm, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

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 }
int32 DateADT
Definition: date.h:23
int64 timestamp
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1822
int errcode(int sqlerrcode)
Definition: elog.c:698
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
#define DATE_NOBEGIN(j)
Definition: date.h:39
#define ERROR
Definition: elog.h:46
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
int32 fsec_t
Definition: timestamp.h:41
int64 Timestamp
Definition: timestamp.h:38
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define ereport(elevel,...)
Definition: elog.h:157
int tm_year
Definition: pgtime.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
#define PG_RETURN_DATEADT(x)
Definition: date.h:65
#define DATE_NOEND(j)
Definition: date.h:41

◆ timestamp_eq_date()

Datum timestamp_eq_date ( PG_FUNCTION_ARGS  )

Definition at line 912 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_BOOL.

913 {
915  DateADT dateVal = PG_GETARG_DATEADT(1);
916 
917  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) == 0);
918 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ timestamp_ge_date()

Datum timestamp_ge_date ( PG_FUNCTION_ARGS  )

Definition at line 957 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_BOOL.

958 {
960  DateADT dateVal = PG_GETARG_DATEADT(1);
961 
962  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) <= 0);
963 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ timestamp_gt_date()

Datum timestamp_gt_date ( PG_FUNCTION_ARGS  )

Definition at line 939 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_BOOL.

940 {
942  DateADT dateVal = PG_GETARG_DATEADT(1);
943 
944  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) < 0);
945 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ timestamp_le_date()

Datum timestamp_le_date ( PG_FUNCTION_ARGS  )

Definition at line 948 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_BOOL.

949 {
951  DateADT dateVal = PG_GETARG_DATEADT(1);
952 
953  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) >= 0);
954 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ timestamp_lt_date()

Datum timestamp_lt_date ( PG_FUNCTION_ARGS  )

Definition at line 930 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_BOOL.

931 {
933  DateADT dateVal = PG_GETARG_DATEADT(1);
934 
935  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) > 0);
936 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ timestamp_ne_date()

Datum timestamp_ne_date ( PG_FUNCTION_ARGS  )

Definition at line 921 of file date.c.

References date_cmp_timestamp_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, and PG_RETURN_BOOL.

922 {
924  DateADT dateVal = PG_GETARG_DATEADT(1);
925 
926  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) != 0);
927 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:748

◆ timestamp_time()

Datum timestamp_time ( PG_FUNCTION_ARGS  )

Definition at line 1904 of file date.c.

References ereport, errcode(), errmsg(), ERROR, MINS_PER_HOUR, PG_GETARG_TIMESTAMP, PG_RETURN_NULL, PG_RETURN_TIMEADT, SECS_PER_MINUTE, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, and USECS_PER_SEC.

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 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 timestamp
int tm_hour
Definition: pgtime.h:36
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1822
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
int errcode(int sqlerrcode)
Definition: elog.c:698
Definition: pgtime.h:32
#define MINS_PER_HOUR
Definition: timestamp.h:89
static struct pg_tm tm
Definition: localtime.c:102
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:46
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
int64 Timestamp
Definition: timestamp.h:38
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
int tm_sec
Definition: pgtime.h:34
int tm_min
Definition: pgtime.h:35
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ timestamptz_cmp_date()

Datum timestamptz_cmp_date ( PG_FUNCTION_ARGS  )

Definition at line 1029 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_INT32.

1030 {
1032  DateADT dateVal = PG_GETARG_DATEADT(1);
1033 
1035 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ timestamptz_date()

Datum timestamptz_date ( PG_FUNCTION_ARGS  )

Definition at line 1346 of file date.c.

References date2j(), DATE_NOBEGIN, DATE_NOEND, ereport, errcode(), errmsg(), ERROR, PG_GETARG_TIMESTAMP, PG_RETURN_DATEADT, POSTGRES_EPOCH_JDATE, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, tm, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

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 }
int32 DateADT
Definition: date.h:23
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1822
int errcode(int sqlerrcode)
Definition: elog.c:698
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
#define DATE_NOBEGIN(j)
Definition: date.h:39
#define ERROR
Definition: elog.h:46
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
int32 fsec_t
Definition: timestamp.h:41
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define ereport(elevel,...)
Definition: elog.h:157
int tm_year
Definition: pgtime.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
#define PG_RETURN_DATEADT(x)
Definition: date.h:65
#define DATE_NOEND(j)
Definition: date.h:41

◆ timestamptz_eq_date()

Datum timestamptz_eq_date ( PG_FUNCTION_ARGS  )

Definition at line 975 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_BOOL.

976 {
978  DateADT dateVal = PG_GETARG_DATEADT(1);
979 
980  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) == 0);
981 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ timestamptz_ge_date()

Datum timestamptz_ge_date ( PG_FUNCTION_ARGS  )

Definition at line 1020 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_BOOL.

1021 {
1023  DateADT dateVal = PG_GETARG_DATEADT(1);
1024 
1025  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) <= 0);
1026 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ timestamptz_gt_date()

Datum timestamptz_gt_date ( PG_FUNCTION_ARGS  )

Definition at line 1002 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_BOOL.

1003 {
1005  DateADT dateVal = PG_GETARG_DATEADT(1);
1006 
1007  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) < 0);
1008 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ timestamptz_le_date()

Datum timestamptz_le_date ( PG_FUNCTION_ARGS  )

Definition at line 1011 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_BOOL.

1012 {
1014  DateADT dateVal = PG_GETARG_DATEADT(1);
1015 
1016  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) >= 0);
1017 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ timestamptz_lt_date()

Datum timestamptz_lt_date ( PG_FUNCTION_ARGS  )

Definition at line 993 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_BOOL.

994 {
996  DateADT dateVal = PG_GETARG_DATEADT(1);
997 
999 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ timestamptz_ne_date()

Datum timestamptz_ne_date ( PG_FUNCTION_ARGS  )

Definition at line 984 of file date.c.

References date_cmp_timestamptz_internal(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_BOOL.

985 {
987  DateADT dateVal = PG_GETARG_DATEADT(1);
988 
989  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) != 0);
990 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:828

◆ timestamptz_time()

Datum timestamptz_time ( PG_FUNCTION_ARGS  )

Definition at line 1934 of file date.c.

References ereport, errcode(), errmsg(), ERROR, MINS_PER_HOUR, PG_GETARG_TIMESTAMP, PG_RETURN_NULL, PG_RETURN_TIMEADT, SECS_PER_MINUTE, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, and USECS_PER_SEC.

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 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
int tm_hour
Definition: pgtime.h:36
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1822
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
int errcode(int sqlerrcode)
Definition: elog.c:698
Definition: pgtime.h:32
#define MINS_PER_HOUR
Definition: timestamp.h:89
static struct pg_tm tm
Definition: localtime.c:102
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:46
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
int tm_sec
Definition: pgtime.h:34
int tm_min
Definition: pgtime.h:35
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ timestamptz_timetz()

Datum timestamptz_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2827 of file date.c.

References ereport, errcode(), errmsg(), ERROR, palloc(), PG_GETARG_TIMESTAMP, PG_RETURN_NULL, PG_RETURN_TIMETZADT_P, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, and tm2timetz().

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 }
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1822
int errcode(int sqlerrcode)
Definition: elog.c:698
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2254
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:46
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel,...)
Definition: elog.h:157
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: date.h:27
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ timetypmodin()

Datum timetypmodin ( PG_FUNCTION_ARGS  )

Definition at line 1557 of file date.c.

References anytime_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

1558 {
1560 
1561  PG_RETURN_INT32(anytime_typmodin(false, ta));
1562 }
static int32 anytime_typmodin(bool istz, ArrayType *ta)
Definition: date.c:48
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256

◆ timetypmodout()

Datum timetypmodout ( PG_FUNCTION_ARGS  )

Definition at line 1565 of file date.c.

References anytime_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

1566 {
1567  int32 typmod = PG_GETARG_INT32(0);
1568 
1569  PG_RETURN_CSTRING(anytime_typmodout(false, typmod));
1570 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:429
static char * anytime_typmodout(bool istz, int32 typmod)
Definition: date.c:91
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362

◆ timetz2tm()

int timetz2tm ( TimeTzADT time,
struct pg_tm tm,
fsec_t fsec,
int *  tzp 
)

Definition at line 2388 of file date.c.

References TimeTzADT::time, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, USECS_PER_HOUR, USECS_PER_MINUTE, USECS_PER_SEC, and TimeTzADT::zone.

Referenced by JsonEncodeDateTime(), timetz_out(), and timetz_part_common().

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 }
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:36
#define USECS_PER_MINUTE
Definition: timestamp.h:93
int32 zone
Definition: date.h:30
#define USECS_PER_HOUR
Definition: timestamp.h:92
int64 TimeOffset
Definition: timestamp.h:40
int tm_sec
Definition: pgtime.h:34
int tm_min
Definition: pgtime.h:35

◆ timetz_cmp()

Datum timetz_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2509 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_INT32, and timetz_cmp_internal().

Referenced by compareDatetime(), and leftmostvalue_timetz().

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 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2428
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
Definition: date.h:27

◆ timetz_cmp_internal()

static int timetz_cmp_internal ( TimeTzADT time1,
TimeTzADT time2 
)
static

Definition at line 2428 of file date.c.

References TimeTzADT::time, USECS_PER_SEC, and TimeTzADT::zone.

Referenced by in_range_timetz_interval(), timetz_cmp(), timetz_eq(), timetz_ge(), timetz_gt(), timetz_larger(), timetz_le(), timetz_lt(), timetz_ne(), and timetz_smaller().

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 }
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
int32 zone
Definition: date.h:30
int64 TimeOffset
Definition: timestamp.h:40

◆ timetz_eq()

Datum timetz_eq ( PG_FUNCTION_ARGS  )

Definition at line 2455 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

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 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2428
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: date.h:27

◆ timetz_ge()

Datum timetz_ge ( PG_FUNCTION_ARGS  )

Definition at line 2500 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

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 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2428
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: date.h:27

◆ timetz_gt()

Datum timetz_gt ( PG_FUNCTION_ARGS  )

Definition at line 2491 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

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 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2428
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: date.h:27

◆ timetz_hash()

Datum timetz_hash ( PG_FUNCTION_ARGS  )

Definition at line 2518 of file date.c.

References DatumGetUInt32, DirectFunctionCall1, hash_uint32(), hashint8(), Int64GetDatumFast, sort-test::key, PG_GETARG_TIMETZADT_P, PG_RETURN_UINT32, TimeTzADT::time, and TimeTzADT::zone.

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 }
#define DatumGetUInt32(X)
Definition: postgres.h:530
TimeADT time
Definition: date.h:29
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:84
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:355
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
unsigned int uint32
Definition: c.h:441
#define Int64GetDatumFast(X)
Definition: postgres.h:804
static Datum hash_uint32(uint32 k)
Definition: hashfn.h:43
Definition: date.h:27

◆ timetz_hash_extended()

Datum timetz_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2534 of file date.c.

References DatumGetInt64, DatumGetUInt64, DirectFunctionCall2, hash_uint32_extended(), hashint8extended(), Int64GetDatumFast, sort-test::key, PG_GETARG_DATUM, PG_GETARG_TIMETZADT_P, PG_RETURN_UINT64, TimeTzADT::time, and TimeTzADT::zone.

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 }
TimeADT time
Definition: date.h:29
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:104
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:369
int32 zone
Definition: date.h:30
#define DatumGetInt64(X)
Definition: postgres.h:651
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
uintptr_t Datum
Definition: postgres.h:411
#define Int64GetDatumFast(X)
Definition: postgres.h:804
#define DatumGetUInt64(X)
Definition: postgres.h:678
Definition: date.h:27
static Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfn.h:49
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628

◆ timetz_in()

Datum timetz_in ( PG_FUNCTION_ARGS  )

Definition at line 2264 of file date.c.

References AdjustTimeForTypmod(), DateTimeParseError(), DecodeTimeOnly(), MAXDATEFIELDS, MAXDATELEN, palloc(), ParseDateTime(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_TIMETZADT_P, generate_unaccent_rules::str, TimeTzADT::time, tm, and tm2timetz().

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 }
#define MAXDATELEN
Definition: datetime.h:201
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3764
TimeADT time
Definition: date.h:29
Definition: pgtime.h:32
unsigned int Oid
Definition: postgres_ext.h:31
int DecodeTimeOnly(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:1754
signed int int32
Definition: c.h:429
static struct pg_tm tm
Definition: localtime.c:102
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2254
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
int32 fsec_t
Definition: timestamp.h:41
#define MAXDATEFIELDS
Definition: datetime.h:203
void * palloc(Size size)
Definition: mcxt.c:1062
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:582
Definition: date.h:27
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1644

◆ timetz_izone()

Datum timetz_izone ( PG_FUNCTION_ARGS  )

Definition at line 3117 of file date.c.

References DatumGetCString, Interval::day, DirectFunctionCall1, ereport, errcode(), errmsg(), ERROR, interval_out(), Interval::month, palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, PointerGetDatum, TimeTzADT::time, Interval::time, USECS_PER_DAY, USECS_PER_SEC, and TimeTzADT::zone.

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 PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
#define PointerGetDatum(X)
Definition: postgres.h:600
int errcode(int sqlerrcode)
Definition: elog.c:698
Datum interval_out(PG_FUNCTION_ARGS)
Definition: timestamp.c:958
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:46
#define DatumGetCString(X)
Definition: postgres.h:610
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
#define ereport(elevel,...)
Definition: elog.h:157
Definition: zic.c:93
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: date.h:27

◆ timetz_larger()

Datum timetz_larger ( PG_FUNCTION_ARGS  )

Definition at line 2550 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

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 }
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2428
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
Definition: date.h:27

◆ timetz_le()

Datum timetz_le ( PG_FUNCTION_ARGS  )

Definition at line 2482 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

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 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2428
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: date.h:27

◆ timetz_lt()

Datum timetz_lt ( PG_FUNCTION_ARGS  )

Definition at line 2473 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

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 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2428
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: date.h:27

◆ timetz_mi_interval()

Datum timetz_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 2603 of file date.c.

References palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, TimeTzADT::time, Interval::time, USECS_PER_DAY, and TimeTzADT::zone.

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 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
TimeADT time
Definition: date.h:29
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: date.h:27

◆ timetz_ne()

Datum timetz_ne ( PG_FUNCTION_ARGS  )

Definition at line 2464 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

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 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2428
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: date.h:27

◆ timetz_out()

Datum timetz_out ( PG_FUNCTION_ARGS  )

Definition at line 2299 of file date.c.

References buf, DateStyle, EncodeTimeOnly(), MAXDATELEN, PG_GETARG_TIMETZADT_P, PG_RETURN_CSTRING, pstrdup(), timetz2tm(), and tm.

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 }
#define MAXDATELEN
Definition: datetime.h:201
char * pstrdup(const char *in)
Definition: mcxt.c:1299
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:3968
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
int32 fsec_t
Definition: timestamp.h:41
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: date.c:2388
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
int DateStyle
Definition: globals.c:118
Definition: date.h:27

◆ timetz_part()

Datum timetz_part ( PG_FUNCTION_ARGS  )

Definition at line 3017 of file date.c.

References timetz_part_common().

3018 {
3019  return timetz_part_common(fcinfo, false);
3020 }
static Datum timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2900

◆ timetz_part_common()

static Datum timetz_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2900 of file date.c.

References DecodeSpecial(), DecodeUnits(), downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_EPOCH, DTK_HOUR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_TZ, DTK_TZ_HOUR, DTK_TZ_MINUTE, DTK_YEAR, ereport, errcode(), errmsg(), ERROR, int64_div_fast_to_numeric(), int64_to_numeric(), MINS_PER_HOUR, PG_GETARG_TEXT_PP, PG_GETARG_TIMETZADT_P, PG_RETURN_FLOAT8, PG_RETURN_NUMERIC, RESERV, SECS_PER_HOUR, SECS_PER_MINUTE, TimeTzADT::time, timetz2tm(), tm, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, generate_unaccent_rules::type, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY, VARSIZE_ANY_EXHDR, and TimeTzADT::zone.

Referenced by extract_timetz(), and timetz_part().

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&