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 1646 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().

1647 {
1648  static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1649  INT64CONST(1000000),
1650  INT64CONST(100000),
1651  INT64CONST(10000),
1652  INT64CONST(1000),
1653  INT64CONST(100),
1654  INT64CONST(10),
1655  INT64CONST(1)
1656  };
1657 
1658  static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1659  INT64CONST(500000),
1660  INT64CONST(50000),
1661  INT64CONST(5000),
1662  INT64CONST(500),
1663  INT64CONST(50),
1664  INT64CONST(5),
1665  INT64CONST(0)
1666  };
1667 
1668  if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1669  {
1670  if (*time >= INT64CONST(0))
1671  *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1672  TimeScales[typmod];
1673  else
1674  *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1675  TimeScales[typmod]);
1676  }
1677 }
#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:200
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:29
int errcode(int sqlerrcode)
Definition: elog.c:698
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
#define ERROR
Definition: elog.h:46
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
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:32
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ 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:3768
int32 DateADT
Definition: date.h:23
int errcode(int sqlerrcode)
Definition: elog.c:698
long date
Definition: pgtypes_date.h:9
Definition: pgtime.h:25
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:30
int tm_mon
Definition: pgtime.h:31
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:32
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:2920
#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:3887
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:25
static struct pg_tm tm
Definition: localtime.c:102
#define DATE_NOT_FINITE(j)
Definition: date.h:43
int tm_mday
Definition: pgtime.h:30
static char * buf
Definition: pg_test_fsync.c:68
int tm_mon
Definition: pgtime.h:31
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:32
#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:2841
#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 1967 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.

1968 {
1970  TimeADT time = PG_GETARG_TIMEADT(1);
1971  Timestamp result;
1972 
1973  result = date2timestamp(date);
1974  if (!TIMESTAMP_NOT_FINITE(result))
1975  {
1976  result += time;
1977  if (!IS_VALID_TIMESTAMP(result))
1978  ereport(ERROR,
1979  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1980  errmsg("timestamp out of range")));
1981  }
1982 
1983  PG_RETURN_TIMESTAMP(result);
1984 }
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 2861 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.

2862 {
2864  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2865  TimestampTz result;
2866 
2867  if (DATE_IS_NOBEGIN(date))
2868  TIMESTAMP_NOBEGIN(result);
2869  else if (DATE_IS_NOEND(date))
2870  TIMESTAMP_NOEND(result);
2871  else
2872  {
2873  /*
2874  * Date's range is wider than timestamp's, so check for boundaries.
2875  * Since dates have the same minimum values as timestamps, only upper
2876  * boundary need be checked for overflow.
2877  */
2879  ereport(ERROR,
2880  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2881  errmsg("date out of range for timestamp")));
2882  result = date * USECS_PER_DAY + time->time + time->zone * USECS_PER_SEC;
2883 
2884  /*
2885  * Since it is possible to go beyond allowed timestamptz range because
2886  * of time zone, check for allowed timestamp range after adding tz.
2887  */
2888  if (!IS_VALID_TIMESTAMP(result))
2889  ereport(ERROR,
2890  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2891  errmsg("date out of range for timestamp")));
2892  }
2893 
2894  PG_RETURN_TIMESTAMP(result);
2895 }
#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:54
#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:3731
#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:4422
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4477
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4079
#define CStringGetDatum(X)
Definition: postgres.h:622
#define DTK_ISOYEAR
Definition: datetime.h:180
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:621
#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:49
#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 2242 of file date.c.

References time_part_common().

2243 {
2244  return time_part_common(fcinfo, true);
2245 }
static Datum time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2133

◆ extract_timetz()

Datum extract_timetz ( PG_FUNCTION_ARGS  )

Definition at line 3025 of file date.c.

References timetz_part_common().

3026 {
3027  return timetz_part_common(fcinfo, true);
3028 }
static Datum timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2902

◆ 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:25
static struct pg_tm tm
Definition: localtime.c:102
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int date2j(int y, int m, int d)
Definition: datetime.c:269
int tm_year
Definition: pgtime.h:32
#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:25
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:2256
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:1646

◆ 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:25
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:1646

◆ 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:3349
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 2093 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.

2094 {
2096  TimeADT base = PG_GETARG_TIMEADT(1);
2097  Interval *offset = PG_GETARG_INTERVAL_P(2);
2098  bool sub = PG_GETARG_BOOL(3);
2099  bool less = PG_GETARG_BOOL(4);
2100  TimeADT sum;
2101 
2102  /*
2103  * Like time_pl_interval/time_mi_interval, we disregard the month and day
2104  * fields of the offset. So our test for negative should too.
2105  */
2106  if (offset->time < 0)
2107  ereport(ERROR,
2108  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2109  errmsg("invalid preceding or following size in window function")));
2110 
2111  /*
2112  * We can't use time_pl_interval/time_mi_interval here, because their
2113  * wraparound behavior would give wrong (or at least undesirable) answers.
2114  * Fortunately the equivalent non-wrapping behavior is trivial, especially
2115  * since we don't worry about integer overflow.
2116  */
2117  if (sub)
2118  sum = base - offset->time;
2119  else
2120  sum = base + offset->time;
2121 
2122  if (less)
2123  PG_RETURN_BOOL(val <= sum);
2124  else
2125  PG_RETURN_BOOL(val >= sum);
2126 }
#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 2627 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.

2628 {
2630  TimeTzADT *base = PG_GETARG_TIMETZADT_P(1);
2631  Interval *offset = PG_GETARG_INTERVAL_P(2);
2632  bool sub = PG_GETARG_BOOL(3);
2633  bool less = PG_GETARG_BOOL(4);
2634  TimeTzADT sum;
2635 
2636  /*
2637  * Like timetz_pl_interval/timetz_mi_interval, we disregard the month and
2638  * day fields of the offset. So our test for negative should too.
2639  */
2640  if (offset->time < 0)
2641  ereport(ERROR,
2642  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2643  errmsg("invalid preceding or following size in window function")));
2644 
2645  /*
2646  * We can't use timetz_pl_interval/timetz_mi_interval here, because their
2647  * wraparound behavior would give wrong (or at least undesirable) answers.
2648  * Fortunately the equivalent non-wrapping behavior is trivial, especially
2649  * since we don't worry about integer overflow.
2650  */
2651  if (sub)
2652  sum.time = base->time - offset->time;
2653  else
2654  sum.time = base->time + offset->time;
2655  sum.zone = base->zone;
2656 
2657  if (less)
2658  PG_RETURN_BOOL(timetz_cmp_internal(val, &sum) <= 0);
2659  else
2660  PG_RETURN_BOOL(timetz_cmp_internal(val, &sum) >= 0);
2661 }
#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:2430
#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 2013 of file date.c.

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

2014 {
2015  Interval *span = PG_GETARG_INTERVAL_P(0);
2016  TimeADT result;
2017  int64 days;
2018 
2019  result = span->time;
2020  if (result >= USECS_PER_DAY)
2021  {
2022  days = result / USECS_PER_DAY;
2023  result -= days * USECS_PER_DAY;
2024  }
2025  else if (result < 0)
2026  {
2027  days = (-result + USECS_PER_DAY - 1) / USECS_PER_DAY;
2028  result += days * USECS_PER_DAY;
2029  }
2030 
2031  PG_RETURN_TIMEADT(result);
2032 }
#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:25
#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:30
int tm_mon
Definition: pgtime.h:31
#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:32
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 1578 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.

1579 {
1580  int tm_hour = PG_GETARG_INT32(0);
1581  int tm_min = PG_GETARG_INT32(1);
1582  double sec = PG_GETARG_FLOAT8(2);
1583  TimeADT time;
1584 
1585  /* Check for time overflow */
1586  if (float_time_overflows(tm_hour, tm_min, sec))
1587  ereport(ERROR,
1588  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
1589  errmsg("time field value out of range: %d:%02d:%02g",
1590  tm_hour, tm_min, sec)));
1591 
1592  /* This should match tm2time */
1593  time = (((tm_hour * MINS_PER_HOUR + tm_min) * SECS_PER_MINUTE)
1594  * USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
1595 
1596  PG_RETURN_TIMEADT(time);
1597 }
#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:29
#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:28

◆ overlaps_time()

Datum overlaps_time ( PG_FUNCTION_ARGS  )

Definition at line 1784 of file date.c.

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

1785 {
1786  /*
1787  * The arguments are TimeADT, but we leave them as generic Datums to avoid
1788  * dereferencing nulls (TimeADT is pass-by-reference!)
1789  */
1790  Datum ts1 = PG_GETARG_DATUM(0);
1791  Datum te1 = PG_GETARG_DATUM(1);
1792  Datum ts2 = PG_GETARG_DATUM(2);
1793  Datum te2 = PG_GETARG_DATUM(3);
1794  bool ts1IsNull = PG_ARGISNULL(0);
1795  bool te1IsNull = PG_ARGISNULL(1);
1796  bool ts2IsNull = PG_ARGISNULL(2);
1797  bool te2IsNull = PG_ARGISNULL(3);
1798 
1799 #define TIMEADT_GT(t1,t2) \
1800  (DatumGetTimeADT(t1) > DatumGetTimeADT(t2))
1801 #define TIMEADT_LT(t1,t2) \
1802  (DatumGetTimeADT(t1) < DatumGetTimeADT(t2))
1803 
1804  /*
1805  * If both endpoints of interval 1 are null, the result is null (unknown).
1806  * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
1807  * take ts1 as the lesser endpoint.
1808  */
1809  if (ts1IsNull)
1810  {
1811  if (te1IsNull)
1812  PG_RETURN_NULL();
1813  /* swap null for non-null */
1814  ts1 = te1;
1815  te1IsNull = true;
1816  }
1817  else if (!te1IsNull)
1818  {
1819  if (TIMEADT_GT(ts1, te1))
1820  {
1821  Datum tt = ts1;
1822 
1823  ts1 = te1;
1824  te1 = tt;
1825  }
1826  }
1827 
1828  /* Likewise for interval 2. */
1829  if (ts2IsNull)
1830  {
1831  if (te2IsNull)
1832  PG_RETURN_NULL();
1833  /* swap null for non-null */
1834  ts2 = te2;
1835  te2IsNull = true;
1836  }
1837  else if (!te2IsNull)
1838  {
1839  if (TIMEADT_GT(ts2, te2))
1840  {
1841  Datum tt = ts2;
1842 
1843  ts2 = te2;
1844  te2 = tt;
1845  }
1846  }
1847 
1848  /*
1849  * At this point neither ts1 nor ts2 is null, so we can consider three
1850  * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
1851  */
1852  if (TIMEADT_GT(ts1, ts2))
1853  {
1854  /*
1855  * This case is ts1 < te2 OR te1 < te2, which may look redundant but
1856  * in the presence of nulls it's not quite completely so.
1857  */
1858  if (te2IsNull)
1859  PG_RETURN_NULL();
1860  if (TIMEADT_LT(ts1, te2))
1861  PG_RETURN_BOOL(true);
1862  if (te1IsNull)
1863  PG_RETURN_NULL();
1864 
1865  /*
1866  * If te1 is not null then we had ts1 <= te1 above, and we just found
1867  * ts1 >= te2, hence te1 >= te2.
1868  */
1869  PG_RETURN_BOOL(false);
1870  }
1871  else if (TIMEADT_LT(ts1, ts2))
1872  {
1873  /* This case is ts2 < te1 OR te2 < te1 */
1874  if (te1IsNull)
1875  PG_RETURN_NULL();
1876  if (TIMEADT_LT(ts2, te1))
1877  PG_RETURN_BOOL(true);
1878  if (te2IsNull)
1879  PG_RETURN_NULL();
1880 
1881  /*
1882  * If te2 is not null then we had ts2 <= te2 above, and we just found
1883  * ts2 >= te1, hence te2 >= te1.
1884  */
1885  PG_RETURN_BOOL(false);
1886  }
1887  else
1888  {
1889  /*
1890  * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
1891  * rather silly way of saying "true if both are nonnull, else null".
1892  */
1893  if (te1IsNull || te2IsNull)
1894  PG_RETURN_NULL();
1895  PG_RETURN_BOOL(true);
1896  }
1897 
1898 #undef TIMEADT_GT
1899 #undef TIMEADT_LT
1900 }
#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 2670 of file date.c.

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

2671 {
2672  /*
2673  * The arguments are TimeTzADT *, but we leave them as generic Datums for
2674  * convenience of notation --- and to avoid dereferencing nulls.
2675  */
2676  Datum ts1 = PG_GETARG_DATUM(0);
2677  Datum te1 = PG_GETARG_DATUM(1);
2678  Datum ts2 = PG_GETARG_DATUM(2);
2679  Datum te2 = PG_GETARG_DATUM(3);
2680  bool ts1IsNull = PG_ARGISNULL(0);
2681  bool te1IsNull = PG_ARGISNULL(1);
2682  bool ts2IsNull = PG_ARGISNULL(2);
2683  bool te2IsNull = PG_ARGISNULL(3);
2684 
2685 #define TIMETZ_GT(t1,t2) \
2686  DatumGetBool(DirectFunctionCall2(timetz_gt,t1,t2))
2687 #define TIMETZ_LT(t1,t2) \
2688  DatumGetBool(DirectFunctionCall2(timetz_lt,t1,t2))
2689 
2690  /*
2691  * If both endpoints of interval 1 are null, the result is null (unknown).
2692  * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2693  * take ts1 as the lesser endpoint.
2694  */
2695  if (ts1IsNull)
2696  {
2697  if (te1IsNull)
2698  PG_RETURN_NULL();
2699  /* swap null for non-null */
2700  ts1 = te1;
2701  te1IsNull = true;
2702  }
2703  else if (!te1IsNull)
2704  {
2705  if (TIMETZ_GT(ts1, te1))
2706  {
2707  Datum tt = ts1;
2708 
2709  ts1 = te1;
2710  te1 = tt;
2711  }
2712  }
2713 
2714  /* Likewise for interval 2. */
2715  if (ts2IsNull)
2716  {
2717  if (te2IsNull)
2718  PG_RETURN_NULL();
2719  /* swap null for non-null */
2720  ts2 = te2;
2721  te2IsNull = true;
2722  }
2723  else if (!te2IsNull)
2724  {
2725  if (TIMETZ_GT(ts2, te2))
2726  {
2727  Datum tt = ts2;
2728 
2729  ts2 = te2;
2730  te2 = tt;
2731  }
2732  }
2733 
2734  /*
2735  * At this point neither ts1 nor ts2 is null, so we can consider three
2736  * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2737  */
2738  if (TIMETZ_GT(ts1, ts2))
2739  {
2740  /*
2741  * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2742  * in the presence of nulls it's not quite completely so.
2743  */
2744  if (te2IsNull)
2745  PG_RETURN_NULL();
2746  if (TIMETZ_LT(ts1, te2))
2747  PG_RETURN_BOOL(true);
2748  if (te1IsNull)
2749  PG_RETURN_NULL();
2750 
2751  /*
2752  * If te1 is not null then we had ts1 <= te1 above, and we just found
2753  * ts1 >= te2, hence te1 >= te2.
2754  */
2755  PG_RETURN_BOOL(false);
2756  }
2757  else if (TIMETZ_LT(ts1, ts2))
2758  {
2759  /* This case is ts2 < te1 OR te2 < te1 */
2760  if (te1IsNull)
2761  PG_RETURN_NULL();
2762  if (TIMETZ_LT(ts2, te1))
2763  PG_RETURN_BOOL(true);
2764  if (te2IsNull)
2765  PG_RETURN_NULL();
2766 
2767  /*
2768  * If te2 is not null then we had ts2 <= te2 above, and we just found
2769  * ts2 >= te1, hence te2 >= te1.
2770  */
2771  PG_RETURN_BOOL(false);
2772  }
2773  else
2774  {
2775  /*
2776  * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2777  * rather silly way of saying "true if both are nonnull, else null".
2778  */
2779  if (te1IsNull || te2IsNull)
2780  PG_RETURN_NULL();
2781  PG_RETURN_BOOL(true);
2782  }
2783 
2784 #undef TIMETZ_GT
2785 #undef TIMETZ_LT
2786 }
#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 1489 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().

1490 {
1491  tm->tm_hour = time / USECS_PER_HOUR;
1492  time -= tm->tm_hour * USECS_PER_HOUR;
1493  tm->tm_min = time / USECS_PER_MINUTE;
1494  time -= tm->tm_min * USECS_PER_MINUTE;
1495  tm->tm_sec = time / USECS_PER_SEC;
1496  time -= tm->tm_sec * USECS_PER_SEC;
1497  *fsec = time;
1498  return 0;
1499 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define USECS_PER_MINUTE
Definition: timestamp.h:93
#define USECS_PER_HOUR
Definition: timestamp.h:92
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ time_cmp()

Datum time_cmp ( PG_FUNCTION_ARGS  )

Definition at line 1735 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_INT32.

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

1736 {
1737  TimeADT time1 = PG_GETARG_TIMEADT(0);
1738  TimeADT time2 = PG_GETARG_TIMEADT(1);
1739 
1740  if (time1 < time2)
1741  PG_RETURN_INT32(-1);
1742  if (time1 > time2)
1743  PG_RETURN_INT32(1);
1744  PG_RETURN_INT32(0);
1745 }
#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 1681 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timeeq().

1682 {
1683  TimeADT time1 = PG_GETARG_TIMEADT(0);
1684  TimeADT time2 = PG_GETARG_TIMEADT(1);
1685 
1686  PG_RETURN_BOOL(time1 == time2);
1687 }
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 1726 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timege().

1727 {
1728  TimeADT time1 = PG_GETARG_TIMEADT(0);
1729  TimeADT time2 = PG_GETARG_TIMEADT(1);
1730 
1731  PG_RETURN_BOOL(time1 >= time2);
1732 }
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 1717 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timegt().

1718 {
1719  TimeADT time1 = PG_GETARG_TIMEADT(0);
1720  TimeADT time2 = PG_GETARG_TIMEADT(1);
1721 
1722  PG_RETURN_BOOL(time1 > time2);
1723 }
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 1748 of file date.c.

References hashint8().

1749 {
1750  return hashint8(fcinfo);
1751 }
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:83

◆ time_hash_extended()

Datum time_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 1754 of file date.c.

References hashint8extended().

1755 {
1756  return hashint8extended(fcinfo);
1757 }
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103

◆ 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:3768
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
Definition: pgtime.h:25
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:1646

◆ time_interval()

Datum time_interval ( PG_FUNCTION_ARGS  )

Definition at line 1990 of file date.c.

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

1991 {
1992  TimeADT time = PG_GETARG_TIMEADT(0);
1993  Interval *result;
1994 
1995  result = (Interval *) palloc(sizeof(Interval));
1996 
1997  result->time = time;
1998  result->day = 0;
1999  result->month = 0;
2000 
2001  PG_RETURN_INTERVAL_P(result);
2002 }
#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 1760 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

1761 {
1762  TimeADT time1 = PG_GETARG_TIMEADT(0);
1763  TimeADT time2 = PG_GETARG_TIMEADT(1);
1764 
1765  PG_RETURN_TIMEADT((time1 > time2) ? time1 : time2);
1766 }
#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 1708 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timele().

1709 {
1710  TimeADT time1 = PG_GETARG_TIMEADT(0);
1711  TimeADT time2 = PG_GETARG_TIMEADT(1);
1712 
1713  PG_RETURN_BOOL(time1 <= time2);
1714 }
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 1699 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timelt().

1700 {
1701  TimeADT time1 = PG_GETARG_TIMEADT(0);
1702  TimeADT time2 = PG_GETARG_TIMEADT(1);
1703 
1704  PG_RETURN_BOOL(time1 < time2);
1705 }
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 2075 of file date.c.

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

2076 {
2077  TimeADT time = PG_GETARG_TIMEADT(0);
2078  Interval *span = PG_GETARG_INTERVAL_P(1);
2079  TimeADT result;
2080 
2081  result = time - span->time;
2082  result -= result / USECS_PER_DAY * USECS_PER_DAY;
2083  if (result < INT64CONST(0))
2084  result += USECS_PER_DAY;
2085 
2086  PG_RETURN_TIMEADT(result);
2087 }
#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 2038 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().

2039 {
2040  TimeADT time1 = PG_GETARG_TIMEADT(0);
2041  TimeADT time2 = PG_GETARG_TIMEADT(1);
2042  Interval *result;
2043 
2044  result = (Interval *) palloc(sizeof(Interval));
2045 
2046  result->month = 0;
2047  result->day = 0;
2048  result->time = time1 - time2;
2049 
2050  PG_RETURN_INTERVAL_P(result);
2051 }
#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 1690 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

1691 {
1692  TimeADT time1 = PG_GETARG_TIMEADT(0);
1693  TimeADT time2 = PG_GETARG_TIMEADT(1);
1694 
1695  PG_RETURN_BOOL(time1 != time2);
1696 }
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 1502 of file date.c.

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

1503 {
1504  TimeADT time = PG_GETARG_TIMEADT(0);
1505  char *result;
1506  struct pg_tm tt,
1507  *tm = &tt;
1508  fsec_t fsec;
1509  char buf[MAXDATELEN + 1];
1510 
1511  time2tm(time, tm, &fsec);
1512  EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf);
1513 
1514  result = pstrdup(buf);
1515  PG_RETURN_CSTRING(result);
1516 }
#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:3972
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1489
Definition: pgtime.h:25
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 2236 of file date.c.

References time_part_common().

2237 {
2238  return time_part_common(fcinfo, false);
2239 }
static Datum time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2133

◆ time_part_common()

static Datum time_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2133 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().

2134 {
2135  text *units = PG_GETARG_TEXT_PP(0);
2136  TimeADT time = PG_GETARG_TIMEADT(1);
2137  int64 intresult;
2138  int type,
2139  val;
2140  char *lowunits;
2141 
2142  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
2143  VARSIZE_ANY_EXHDR(units),
2144  false);
2145 
2146  type = DecodeUnits(0, lowunits, &val);
2147  if (type == UNKNOWN_FIELD)
2148  type = DecodeSpecial(0, lowunits, &val);
2149 
2150  if (type == UNITS)
2151  {
2152  fsec_t fsec;
2153  struct pg_tm tt,
2154  *tm = &tt;
2155 
2156  time2tm(time, tm, &fsec);
2157 
2158  switch (val)
2159  {
2160  case DTK_MICROSEC:
2161  intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
2162  break;
2163 
2164  case DTK_MILLISEC:
2165  if (retnumeric)
2166  /*---
2167  * tm->tm_sec * 1000 + fsec / 1000
2168  * = (tm->tm_sec * 1'000'000 + fsec) / 1000
2169  */
2170  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
2171  else
2172  PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
2173  break;
2174 
2175  case DTK_SECOND:
2176  if (retnumeric)
2177  /*---
2178  * tm->tm_sec + fsec / 1'000'000
2179  * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
2180  */
2181  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
2182  else
2183  PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
2184  break;
2185 
2186  case DTK_MINUTE:
2187  intresult = tm->tm_min;
2188  break;
2189 
2190  case DTK_HOUR:
2191  intresult = tm->tm_hour;
2192  break;
2193 
2194  case DTK_TZ:
2195  case DTK_TZ_MINUTE:
2196  case DTK_TZ_HOUR:
2197  case DTK_DAY:
2198  case DTK_MONTH:
2199  case DTK_QUARTER:
2200  case DTK_YEAR:
2201  case DTK_DECADE:
2202  case DTK_CENTURY:
2203  case DTK_MILLENNIUM:
2204  case DTK_ISOYEAR:
2205  default:
2206  ereport(ERROR,
2207  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2208  errmsg("\"time\" units \"%s\" not recognized",
2209  lowunits)));
2210  intresult = 0;
2211  }
2212  }
2213  else if (type == RESERV && val == DTK_EPOCH)
2214  {
2215  if (retnumeric)
2217  else
2218  PG_RETURN_FLOAT8(time / 1000000.0);
2219  }
2220  else
2221  {
2222  ereport(ERROR,
2223  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2224  errmsg("\"time\" units \"%s\" not recognized",
2225  lowunits)));
2226  intresult = 0;
2227  }
2228 
2229  if (retnumeric)
2230  PG_RETURN_NUMERIC(int64_to_numeric(intresult));
2231  else
2232  PG_RETURN_FLOAT8(intresult);
2233 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#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:29
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:1489
#define UNKNOWN_FIELD
Definition: datetime.h:125
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3731
Definition: pgtime.h:25
#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:4079
#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:27
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4100
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:664

◆ time_pl_interval()

Datum time_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 2057 of file date.c.

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

2058 {
2059  TimeADT time = PG_GETARG_TIMEADT(0);
2060  Interval *span = PG_GETARG_INTERVAL_P(1);
2061  TimeADT result;
2062 
2063  result = time + span->time;
2064  result -= result / USECS_PER_DAY * USECS_PER_DAY;
2065  if (result < INT64CONST(0))
2066  result += USECS_PER_DAY;
2067 
2068  PG_RETURN_TIMEADT(result);
2069 }
#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 1522 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.

1523 {
1525 
1526 #ifdef NOT_USED
1527  Oid typelem = PG_GETARG_OID(1);
1528 #endif
1529  int32 typmod = PG_GETARG_INT32(2);
1530  TimeADT result;
1531 
1532  result = pq_getmsgint64(buf);
1533 
1534  if (result < INT64CONST(0) || result > USECS_PER_DAY)
1535  ereport(ERROR,
1536  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1537  errmsg("time out of range")));
1538 
1539  AdjustTimeForTypmod(&result, typmod);
1540 
1541  PG_RETURN_TIMEADT(result);
1542 }
#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:1646

◆ time_scale()

Datum time_scale ( PG_FUNCTION_ARGS  )

Definition at line 1626 of file date.c.

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

1627 {
1628  TimeADT time = PG_GETARG_TIMEADT(0);
1629  int32 typmod = PG_GETARG_INT32(1);
1630  TimeADT result;
1631 
1632  result = time;
1633  AdjustTimeForTypmod(&result, typmod);
1634 
1635  PG_RETURN_TIMEADT(result);
1636 }
#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:1646

◆ time_send()

Datum time_send ( PG_FUNCTION_ARGS  )

Definition at line 1548 of file date.c.

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

1549 {
1550  TimeADT time = PG_GETARG_TIMEADT(0);
1552 
1553  pq_begintypsend(&buf);
1554  pq_sendint64(&buf, time);
1556 }
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 1769 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

1770 {
1771  TimeADT time1 = PG_GETARG_TIMEADT(0);
1772  TimeADT time2 = PG_GETARG_TIMEADT(1);
1773 
1774  PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
1775 }
#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 1606 of file date.c.

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

1607 {
1608  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1609  Node *ret = NULL;
1610 
1611  if (IsA(rawreq, SupportRequestSimplify))
1612  {
1614 
1615  ret = TemporalSimplify(MAX_TIME_PRECISION, (Node *) req->fcall);
1616  }
1617 
1618  PG_RETURN_POINTER(ret);
1619 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Definition: nodes.h:539
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
Node * TemporalSimplify(int32 max_precis, Node *node)
Definition: datetime.c:4497
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ time_timetz()

Datum time_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2803 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().

2804 {
2805  TimeADT time = PG_GETARG_TIMEADT(0);
2806  TimeTzADT *result;
2807  struct pg_tm tt,
2808  *tm = &tt;
2809  fsec_t fsec;
2810  int tz;
2811 
2812  GetCurrentDateTime(tm);
2813  time2tm(time, tm, &fsec);
2815 
2816  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2817 
2818  result->time = time;
2819  result->zone = tz;
2820 
2821  PG_RETURN_TIMETZADT_P(result);
2822 }
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:1489
Definition: pgtime.h:25
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:25
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:30
int tm_mon
Definition: pgtime.h:31
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:32
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 1906 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.

1907 {
1909  TimeADT result;
1910  struct pg_tm tt,
1911  *tm = &tt;
1912  fsec_t fsec;
1913 
1914  if (TIMESTAMP_NOT_FINITE(timestamp))
1915  PG_RETURN_NULL();
1916 
1917  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1918  ereport(ERROR,
1919  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1920  errmsg("timestamp out of range")));
1921 
1922  /*
1923  * Could also do this with time = (timestamp / USECS_PER_DAY *
1924  * USECS_PER_DAY) - timestamp;
1925  */
1926  result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1927  USECS_PER_SEC) + fsec;
1928 
1929  PG_RETURN_TIMEADT(result);
1930 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 timestamp
int tm_hour
Definition: pgtime.h:29
#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:25
#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:27
int tm_min
Definition: pgtime.h:28
#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:25
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:30
int tm_mon
Definition: pgtime.h:31
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:32
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 1936 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.

1937 {
1939  TimeADT result;
1940  struct pg_tm tt,
1941  *tm = &tt;
1942  int tz;
1943  fsec_t fsec;
1944 
1945  if (TIMESTAMP_NOT_FINITE(timestamp))
1946  PG_RETURN_NULL();
1947 
1948  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1949  ereport(ERROR,
1950  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1951  errmsg("timestamp out of range")));
1952 
1953  /*
1954  * Could also do this with time = (timestamp / USECS_PER_DAY *
1955  * USECS_PER_DAY) - timestamp;
1956  */
1957  result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1958  USECS_PER_SEC) + fsec;
1959 
1960  PG_RETURN_TIMEADT(result);
1961 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
int tm_hour
Definition: pgtime.h:29
#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:25
#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:27
int tm_min
Definition: pgtime.h:28
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ timestamptz_timetz()

Datum timestamptz_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2829 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().

2830 {
2832  TimeTzADT *result;
2833  struct pg_tm tt,
2834  *tm = &tt;
2835  int tz;
2836  fsec_t fsec;
2837 
2838  if (TIMESTAMP_NOT_FINITE(timestamp))
2839  PG_RETURN_NULL();
2840 
2841  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
2842  ereport(ERROR,
2843  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2844  errmsg("timestamp out of range")));
2845 
2846  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2847 
2848  tm2timetz(tm, fsec, tz, result);
2849 
2850  PG_RETURN_TIMETZADT_P(result);
2851 }
#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:25
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:2256
#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 1559 of file date.c.

References anytime_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

1560 {
1562 
1563  PG_RETURN_INT32(anytime_typmodin(false, ta));
1564 }
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 1567 of file date.c.

References anytime_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

1568 {
1569  int32 typmod = PG_GETARG_INT32(0);
1570 
1571  PG_RETURN_CSTRING(anytime_typmodout(false, typmod));
1572 }
#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 2390 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().

2391 {
2392  TimeOffset trem = time->time;
2393 
2394  tm->tm_hour = trem / USECS_PER_HOUR;
2395  trem -= tm->tm_hour * USECS_PER_HOUR;
2396  tm->tm_min = trem / USECS_PER_MINUTE;
2397  trem -= tm->tm_min * USECS_PER_MINUTE;
2398  tm->tm_sec = trem / USECS_PER_SEC;
2399  *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2400 
2401  if (tzp != NULL)
2402  *tzp = time->zone;
2403 
2404  return 0;
2405 }
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#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:27
int tm_min
Definition: pgtime.h:28

◆ timetz_cmp()

Datum timetz_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2511 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_INT32, and timetz_cmp_internal().

Referenced by compareDatetime(), and leftmostvalue_timetz().

2512 {
2513  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2514  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2515 
2516  PG_RETURN_INT32(timetz_cmp_internal(time1, time2));
2517 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2430
#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 2430 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().

2431 {
2432  TimeOffset t1,
2433  t2;
2434 
2435  /* Primary sort is by true (GMT-equivalent) time */
2436  t1 = time1->time + (time1->zone * USECS_PER_SEC);
2437  t2 = time2->time + (time2->zone * USECS_PER_SEC);
2438 
2439  if (t1 > t2)
2440  return 1;
2441  if (t1 < t2)
2442  return -1;
2443 
2444  /*
2445  * If same GMT time, sort by timezone; we only want to say that two
2446  * timetz's are equal if both the time and zone parts are equal.
2447  */
2448  if (time1->zone > time2->zone)
2449  return 1;
2450  if (time1->zone < time2->zone)
2451  return -1;
2452 
2453  return 0;
2454 }
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 2457 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

2458 {
2459  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2460  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2461 
2462  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) == 0);
2463 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2430
#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 2502 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

2503 {
2504  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2505  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2506 
2507  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) >= 0);
2508 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2430
#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 2493 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

2494 {
2495  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2496  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2497 
2498  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) > 0);
2499 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2430
#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 2520 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.

2521 {
2523  uint32 thash;
2524 
2525  /*
2526  * To avoid any problems with padding bytes in the struct, we figure the
2527  * field hashes separately and XOR them.
2528  */
2530  Int64GetDatumFast(key->time)));
2531  thash ^= DatumGetUInt32(hash_uint32(key->zone));
2532  PG_RETURN_UINT32(thash);
2533 }
#define DatumGetUInt32(X)
Definition: postgres.h:530
TimeADT time
Definition: date.h:29
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:83
#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 2536 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.

2537 {
2539  Datum seed = PG_GETARG_DATUM(1);
2540  uint64 thash;
2541 
2542  /* Same approach as timetz_hash */
2544  Int64GetDatumFast(key->time),
2545  seed));
2547  DatumGetInt64(seed)));
2548  PG_RETURN_UINT64(thash);
2549 }
TimeADT time
Definition: date.h:29
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103
#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 2266 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().

2267 {
2268  char *str = PG_GETARG_CSTRING(0);
2269 
2270 #ifdef NOT_USED
2271  Oid typelem = PG_GETARG_OID(1);
2272 #endif
2273  int32 typmod = PG_GETARG_INT32(2);
2274  TimeTzADT *result;
2275  fsec_t fsec;
2276  struct pg_tm tt,
2277  *tm = &tt;
2278  int tz;
2279  int nf;
2280  int dterr;
2281  char workbuf[MAXDATELEN + 1];
2282  char *field[MAXDATEFIELDS];
2283  int dtype;
2284  int ftype[MAXDATEFIELDS];
2285 
2286  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
2287  field, ftype, MAXDATEFIELDS, &nf);
2288  if (dterr == 0)
2289  dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
2290  if (dterr != 0)
2291  DateTimeParseError(dterr, str, "time with time zone");
2292 
2293  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2294  tm2timetz(tm, fsec, tz, result);
2295  AdjustTimeForTypmod(&(result->time), typmod);
2296 
2297  PG_RETURN_TIMETZADT_P(result);
2298 }
#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:3768
TimeADT time
Definition: date.h:29
Definition: pgtime.h:25
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:2256
#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:1646

◆ 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 2552 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

2553 {
2554  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2555  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2556  TimeTzADT *result;
2557 
2558  if (timetz_cmp_internal(time1, time2) > 0)
2559  result = time1;
2560  else
2561  result = time2;
2562  PG_RETURN_TIMETZADT_P(result);
2563 }
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2430
#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 2484 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

2485 {
2486  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2487  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2488 
2489  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) <= 0);
2490 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2430
#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 2475 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

2476 {
2477  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2478  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2479 
2480  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) < 0);
2481 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2430
#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 2605 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.

2606 {
2607  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2608  Interval *span = PG_GETARG_INTERVAL_P(1);
2609  TimeTzADT *result;
2610 
2611  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2612 
2613  result->time = time->time - span->time;
2614  result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2615  if (result->time < INT64CONST(0))
2616  result->time += USECS_PER_DAY;
2617 
2618  result->zone = time->zone;
2619 
2620  PG_RETURN_TIMETZADT_P(result);
2621 }
#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 2466 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

2467 {
2468  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2469  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2470 
2471  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) != 0);
2472 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2430
#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 2301 of file date.c.

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

2302 {
2303  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2304  char *result;
2305  struct pg_tm tt,
2306  *tm = &tt;
2307  fsec_t fsec;
2308  int tz;
2309  char buf[MAXDATELEN + 1];
2310 
2311  timetz2tm(time, tm, &fsec, &tz);
2312  EncodeTimeOnly(tm, fsec, true, tz, DateStyle, buf);
2313 
2314  result = pstrdup(buf);
2315  PG_RETURN_CSTRING(result);
2316 }
#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:3972
Definition: pgtime.h:25
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:2390
#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 3019 of file date.c.

References timetz_part_common().

3020 {
3021  return timetz_part_common(fcinfo, false);
3022 }
static Datum timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2902

◆ timetz_part_common()

static Datum timetz_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2902 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().

2903 {
2904  text *units = PG_GETARG_TEXT_PP(0);
2905  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2906  int64 intresult;
2907  int type,
2908  val;
2909  char *lowunits;
2910 
2911  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
2912  VARSIZE_ANY_EXHDR(units),
2913  false);
2914 
2915  type = DecodeUnits(0, lowunits, &val);
2916  if (type == UNKNOWN_FIELD)
2917  type = DecodeSpecial(0, lowunits, &val);
2918 
2919  if (type == UNITS)
2920  {
2921  int tz;
2922  fsec_t fsec;
2923  struct pg_tm tt,
2924  *tm = &tt;
2925 
2926  timetz2tm(time, tm, &fsec, &tz);
2927 
2928  switch (val)
2929  {
2930  case DTK_TZ:
2931  intresult = -tz;
2932  break;
2933 
2934  case DTK_TZ_MINUTE:
2935  intresult = (-tz / SECS_PER_MINUTE) % MINS_PER_HOUR;
2936  break;
2937 
2938  case DTK_TZ_HOUR:
2939  intresult = -tz / SECS_PER_HOUR;
2940  break;
2941 
2942  case DTK_MICROSEC:
2943  intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
2944  break;
2945 
2946  case DTK_MILLISEC:
2947  if (retnumeric)
2948  /*---
2949  * tm->tm_sec * 1000 + fsec / 1000
2950  * = (tm->tm_sec * 1'000'000 + fsec) / 1000
2951  */
2952  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
2953  else
2954  PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
2955  break;
2956 
2957  case DTK_SECOND:
2958  if (retnumeric)
2959  /*---
2960  * tm->tm_sec + fsec / 1'000'000
2961  * = (tm->tm_sec * 1'000'000 +