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 "catalog/pg_type.h"
#include "common/hashfn.h"
#include "common/int.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)
 
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)
 
Datum timetz_at_local (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ TIMEADT_GT

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

◆ TIMEADT_LT

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

◆ TIMETZ_GT

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

◆ TIMETZ_LT

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

Function Documentation

◆ AdjustTimeForTypmod()

void AdjustTimeForTypmod ( TimeADT time,
int32  typmod 
)

Definition at line 1645 of file date.c.

1646 {
1647  static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1648  INT64CONST(1000000),
1649  INT64CONST(100000),
1650  INT64CONST(10000),
1651  INT64CONST(1000),
1652  INT64CONST(100),
1653  INT64CONST(10),
1654  INT64CONST(1)
1655  };
1656 
1657  static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1658  INT64CONST(500000),
1659  INT64CONST(50000),
1660  INT64CONST(5000),
1661  INT64CONST(500),
1662  INT64CONST(50),
1663  INT64CONST(5),
1664  INT64CONST(0)
1665  };
1666 
1667  if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1668  {
1669  if (*time >= INT64CONST(0))
1670  *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1671  TimeScales[typmod];
1672  else
1673  *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1674  TimeScales[typmod]);
1675  }
1676 }
#define MAX_TIME_PRECISION
Definition: date.h:45

References MAX_TIME_PRECISION.

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

◆ anytime_typmod_check()

int32 anytime_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 71 of file date.c.

72 {
73  if (typmod < 0)
74  ereport(ERROR,
75  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
76  errmsg("TIME(%d)%s precision must not be negative",
77  typmod, (istz ? " WITH TIME ZONE" : ""))));
78  if (typmod > MAX_TIME_PRECISION)
79  {
81  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
82  errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
83  typmod, (istz ? " WITH TIME ZONE" : ""),
85  typmod = MAX_TIME_PRECISION;
86  }
87 
88  return typmod;
89 }
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149

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

Referenced by anytime_typmodin(), executeDateTimeMethod(), and transformSQLValueFunction().

◆ anytime_typmodin()

static int32 anytime_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 50 of file date.c.

51 {
52  int32 *tl;
53  int n;
54 
55  tl = ArrayGetIntegerTypmods(ta, &n);
56 
57  /*
58  * we're not too tense about good error message here because grammar
59  * shouldn't allow wrong number of modifiers for TIME
60  */
61  if (n != 1)
62  ereport(ERROR,
63  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
64  errmsg("invalid type modifier")));
65 
66  return anytime_typmod_check(istz, tl[0]);
67 }
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:233
signed int int32
Definition: c.h:494
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:71

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

Referenced by timetypmodin(), and timetztypmodin().

◆ anytime_typmodout()

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

Definition at line 93 of file date.c.

94 {
95  const char *tz = istz ? " with time zone" : " without time zone";
96 
97  if (typmod >= 0)
98  return psprintf("(%d)%s", (int) typmod, tz);
99  else
100  return pstrdup(tz);
101 }
char * pstrdup(const char *in)
Definition: mcxt.c:1695
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46

References psprintf(), and pstrdup().

Referenced by timetypmodout(), and timetztypmodout().

◆ date2timestamp()

static TimestampTz date2timestamp ( DateADT  dateVal)
static

Definition at line 608 of file date.c.

609 {
610  return date2timestamp_opt_overflow(dateVal, NULL);
611 }
Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:564

References date2timestamp_opt_overflow().

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

◆ date2timestamp_no_overflow()

double date2timestamp_no_overflow ( DateADT  dateVal)

Definition at line 720 of file date.c.

721 {
722  double result;
723 
724  if (DATE_IS_NOBEGIN(dateVal))
725  result = -DBL_MAX;
726  else if (DATE_IS_NOEND(dateVal))
727  result = DBL_MAX;
728  else
729  {
730  /* date is days since 2000, timestamp is microseconds since same... */
731  result = dateVal * (double) USECS_PER_DAY;
732  }
733 
734  return result;
735 }
#define USECS_PER_DAY
Definition: timestamp.h:131
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40

References DATE_IS_NOBEGIN, DATE_IS_NOEND, and USECS_PER_DAY.

Referenced by convert_timevalue_to_scalar().

◆ date2timestamp_opt_overflow()

Timestamp date2timestamp_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 564 of file date.c.

565 {
566  Timestamp result;
567 
568  if (overflow)
569  *overflow = 0;
570 
571  if (DATE_IS_NOBEGIN(dateVal))
572  TIMESTAMP_NOBEGIN(result);
573  else if (DATE_IS_NOEND(dateVal))
574  TIMESTAMP_NOEND(result);
575  else
576  {
577  /*
578  * Since dates have the same minimum values as timestamps, only upper
579  * boundary need be checked for overflow.
580  */
581  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
582  {
583  if (overflow)
584  {
585  *overflow = 1;
586  TIMESTAMP_NOEND(result);
587  return result;
588  }
589  else
590  {
591  ereport(ERROR,
592  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
593  errmsg("date out of range for timestamp")));
594  }
595  }
596 
597  /* date is days since 2000, timestamp is microseconds since same... */
598  result = dateVal * USECS_PER_DAY;
599  }
600 
601  return result;
602 }
int64 Timestamp
Definition: timestamp.h:38
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:159
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:253
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:235
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:164

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

◆ date2timestamptz()

static TimestampTz date2timestamptz ( DateADT  dateVal)
static

Definition at line 704 of file date.c.

705 {
706  return date2timestamptz_opt_overflow(dateVal, NULL);
707 }
TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:624

References date2timestamptz_opt_overflow().

Referenced by date_timestamptz().

◆ date2timestamptz_opt_overflow()

TimestampTz date2timestamptz_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 624 of file date.c.

625 {
626  TimestampTz result;
627  struct pg_tm tt,
628  *tm = &tt;
629  int tz;
630 
631  if (overflow)
632  *overflow = 0;
633 
634  if (DATE_IS_NOBEGIN(dateVal))
635  TIMESTAMP_NOBEGIN(result);
636  else if (DATE_IS_NOEND(dateVal))
637  TIMESTAMP_NOEND(result);
638  else
639  {
640  /*
641  * Since dates have the same minimum values as timestamps, only upper
642  * boundary need be checked for overflow.
643  */
644  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
645  {
646  if (overflow)
647  {
648  *overflow = 1;
649  TIMESTAMP_NOEND(result);
650  return result;
651  }
652  else
653  {
654  ereport(ERROR,
655  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
656  errmsg("date out of range for timestamp")));
657  }
658  }
659 
660  j2date(dateVal + POSTGRES_EPOCH_JDATE,
661  &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
662  tm->tm_hour = 0;
663  tm->tm_min = 0;
664  tm->tm_sec = 0;
666 
667  result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
668 
669  /*
670  * Since it is possible to go beyond allowed timestamptz range because
671  * of time zone, check for allowed timestamp range after adding tz.
672  */
673  if (!IS_VALID_TIMESTAMP(result))
674  {
675  if (overflow)
676  {
677  if (result < MIN_TIMESTAMP)
678  {
679  *overflow = -1;
680  TIMESTAMP_NOBEGIN(result);
681  }
682  else
683  {
684  *overflow = 1;
685  TIMESTAMP_NOEND(result);
686  }
687  }
688  else
689  {
690  ereport(ERROR,
691  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
692  errmsg("date out of range for timestamp")));
693  }
694  }
695  }
696 
697  return result;
698 }
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1585
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:311
int64 TimestampTz
Definition: timestamp.h:39
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:267
#define USECS_PER_SEC
Definition: timestamp.h:134
#define MIN_TIMESTAMP
Definition: timestamp.h:256
static struct pg_tm tm
Definition: localtime.c:104
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
Definition: pgtime.h:35
int tm_hour
Definition: pgtime.h:38
int tm_mday
Definition: pgtime.h:39
int tm_mon
Definition: pgtime.h:40
int tm_min
Definition: pgtime.h:37
int tm_sec
Definition: pgtime.h:36
int tm_year
Definition: pgtime.h:41

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

◆ date_cmp()

Datum date_cmp ( PG_FUNCTION_ARGS  )

Definition at line 437 of file date.c.

438 {
439  DateADT dateVal1 = PG_GETARG_DATEADT(0);
440  DateADT dateVal2 = PG_GETARG_DATEADT(1);
441 
442  if (dateVal1 < dateVal2)
443  PG_RETURN_INT32(-1);
444  else if (dateVal1 > dateVal2)
445  PG_RETURN_INT32(1);
446  PG_RETURN_INT32(0);
447 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:89
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354

References PG_GETARG_DATEADT, and PG_RETURN_INT32.

Referenced by compareDatetime(), and gbt_datekey_cmp().

◆ date_cmp_timestamp()

Datum date_cmp_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 814 of file date.c.

815 {
816  DateADT dateVal = PG_GETARG_DATEADT(0);
818 
820 }
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:743
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:63

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

◆ date_cmp_timestamp_internal()

int32 date_cmp_timestamp_internal ( DateADT  dateVal,
Timestamp  dt2 
)

Definition at line 743 of file date.c.

744 {
745  Timestamp dt1;
746  int overflow;
747 
748  dt1 = date2timestamp_opt_overflow(dateVal, &overflow);
749  if (overflow > 0)
750  {
751  /* dt1 is larger than any finite timestamp, but less than infinity */
752  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
753  }
754  Assert(overflow == 0); /* -1 case cannot occur */
755 
756  return timestamp_cmp_internal(dt1, dt2);
757 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2210
#define Assert(condition)
Definition: c.h:858
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:167

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

◆ date_cmp_timestamptz()

Datum date_cmp_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 898 of file date.c.

899 {
900  DateADT dateVal = PG_GETARG_DATEADT(0);
902 
904 }
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:823
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:64

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

◆ date_cmp_timestamptz_internal()

int32 date_cmp_timestamptz_internal ( DateADT  dateVal,
TimestampTz  dt2 
)

Definition at line 823 of file date.c.

824 {
825  TimestampTz dt1;
826  int overflow;
827 
828  dt1 = date2timestamptz_opt_overflow(dateVal, &overflow);
829  if (overflow > 0)
830  {
831  /* dt1 is larger than any finite timestamp, but less than infinity */
832  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
833  }
834  if (overflow < 0)
835  {
836  /* dt1 is less than any finite timestamp, but more than -infinity */
837  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
838  }
839 
840  return timestamptz_cmp_internal(dt1, dt2);
841 }
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:162
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:131

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

◆ date_eq()

Datum date_eq ( PG_FUNCTION_ARGS  )

Definition at line 383 of file date.c.

384 {
385  DateADT dateVal1 = PG_GETARG_DATEADT(0);
386  DateADT dateVal2 = PG_GETARG_DATEADT(1);
387 
388  PG_RETURN_BOOL(dateVal1 == dateVal2);
389 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_dateeq().

◆ date_eq_timestamp()

Datum date_eq_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 760 of file date.c.

761 {
762  DateADT dateVal = PG_GETARG_DATEADT(0);
764 
765  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) == 0);
766 }

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

◆ date_eq_timestamptz()

Datum date_eq_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 844 of file date.c.

845 {
846  DateADT dateVal = PG_GETARG_DATEADT(0);
848 
849  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) == 0);
850 }

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

◆ date_finite()

Datum date_finite ( PG_FUNCTION_ARGS  )

Definition at line 459 of file date.c.

460 {
462 
464 }
#define DATE_NOT_FINITE(j)
Definition: date.h:43
long date
Definition: pgtypes_date.h:9

References DATE_NOT_FINITE, PG_GETARG_DATEADT, and PG_RETURN_BOOL.

◆ date_ge()

Datum date_ge ( PG_FUNCTION_ARGS  )

Definition at line 428 of file date.c.

429 {
430  DateADT dateVal1 = PG_GETARG_DATEADT(0);
431  DateADT dateVal2 = PG_GETARG_DATEADT(1);
432 
433  PG_RETURN_BOOL(dateVal1 >= dateVal2);
434 }

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_datege().

◆ date_ge_timestamp()

Datum date_ge_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 805 of file date.c.

806 {
807  DateADT dateVal = PG_GETARG_DATEADT(0);
809 
810  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) >= 0);
811 }

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

◆ date_ge_timestamptz()

Datum date_ge_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 889 of file date.c.

890 {
891  DateADT dateVal = PG_GETARG_DATEADT(0);
893 
894  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) >= 0);
895 }

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

◆ date_gt()

Datum date_gt ( PG_FUNCTION_ARGS  )

Definition at line 419 of file date.c.

420 {
421  DateADT dateVal1 = PG_GETARG_DATEADT(0);
422  DateADT dateVal2 = PG_GETARG_DATEADT(1);
423 
424  PG_RETURN_BOOL(dateVal1 > dateVal2);
425 }

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_dategt().

◆ date_gt_timestamp()

Datum date_gt_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 787 of file date.c.

788 {
789  DateADT dateVal = PG_GETARG_DATEADT(0);
791 
792  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) > 0);
793 }

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

◆ date_gt_timestamptz()

Datum date_gt_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 871 of file date.c.

872 {
873  DateADT dateVal = PG_GETARG_DATEADT(0);
875 
877 }

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

◆ date_in()

Datum date_in ( PG_FUNCTION_ARGS  )

Definition at line 113 of file date.c.

114 {
115  char *str = PG_GETARG_CSTRING(0);
116  Node *escontext = fcinfo->context;
117  DateADT date;
118  fsec_t fsec;
119  struct pg_tm tt,
120  *tm = &tt;
121  int tzp;
122  int dtype;
123  int nf;
124  int dterr;
125  char *field[MAXDATEFIELDS];
126  int ftype[MAXDATEFIELDS];
127  char workbuf[MAXDATELEN + 1];
128  DateTimeErrorExtra extra;
129 
130  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
131  field, ftype, MAXDATEFIELDS, &nf);
132  if (dterr == 0)
133  dterr = DecodeDateTime(field, ftype, nf,
134  &dtype, tm, &fsec, &tzp, &extra);
135  if (dterr != 0)
136  {
137  DateTimeParseError(dterr, &extra, str, "date", escontext);
138  PG_RETURN_NULL();
139  }
140 
141  switch (dtype)
142  {
143  case DTK_DATE:
144  break;
145 
146  case DTK_EPOCH:
147  GetEpochTime(tm);
148  break;
149 
150  case DTK_LATE:
151  DATE_NOEND(date);
153 
154  case DTK_EARLY:
157 
158  default:
159  DateTimeParseError(DTERR_BAD_FORMAT, &extra, str, "date", escontext);
160  PG_RETURN_NULL();
161  }
162 
163  /* Prevent overflow in Julian-day routines */
165  ereturn(escontext, (Datum) 0,
166  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
167  errmsg("date out of range: \"%s\"", str)));
168 
170 
171  /* Now check for just-out-of-range dates */
172  if (!IS_VALID_DATE(date))
173  ereturn(escontext, (Datum) 0,
174  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
175  errmsg("date out of range: \"%s\"", str)));
176 
178 }
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:754
void DateTimeParseError(int dterr, DateTimeErrorExtra *extra, const char *str, const char *datatype, Node *escontext)
Definition: datetime.c:4092
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp, DateTimeErrorExtra *extra)
Definition: datetime.c:978
int date2j(int year, int month, int day)
Definition: datetime.c:286
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2168
#define IS_VALID_DATE(d)
Definition: timestamp.h:262
int32 fsec_t
Definition: timestamp.h:41
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:227
#define PG_RETURN_DATEADT(x)
Definition: date.h:93
#define DATE_NOEND(j)
Definition: date.h:41
#define DATE_NOBEGIN(j)
Definition: date.h:39
#define ereturn(context, dummy_value,...)
Definition: elog.h:276
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:345
const char * str
#define MAXDATEFIELDS
Definition: datetime.h:202
#define DTK_EPOCH
Definition: datetime.h:152
#define DTK_LATE
Definition: datetime.h:151
#define DTK_DATE
Definition: datetime.h:144
#define DTERR_BAD_FORMAT
Definition: datetime.h:282
#define DTK_EARLY
Definition: datetime.h:150
#define MAXDATELEN
Definition: datetime.h:200
uintptr_t Datum
Definition: postgres.h:64
Definition: nodes.h:129

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

◆ date_larger()

Datum date_larger ( PG_FUNCTION_ARGS  )

Definition at line 467 of file date.c.

468 {
469  DateADT dateVal1 = PG_GETARG_DATEADT(0);
470  DateADT dateVal2 = PG_GETARG_DATEADT(1);
471 
472  PG_RETURN_DATEADT((dateVal1 > dateVal2) ? dateVal1 : dateVal2);
473 }

References PG_GETARG_DATEADT, and PG_RETURN_DATEADT.

◆ date_le()

Datum date_le ( PG_FUNCTION_ARGS  )

Definition at line 410 of file date.c.

411 {
412  DateADT dateVal1 = PG_GETARG_DATEADT(0);
413  DateADT dateVal2 = PG_GETARG_DATEADT(1);
414 
415  PG_RETURN_BOOL(dateVal1 <= dateVal2);
416 }

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_datele().

◆ date_le_timestamp()

Datum date_le_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 796 of file date.c.

797 {
798  DateADT dateVal = PG_GETARG_DATEADT(0);
800 
801  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) <= 0);
802 }

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

◆ date_le_timestamptz()

Datum date_le_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 880 of file date.c.

881 {
882  DateADT dateVal = PG_GETARG_DATEADT(0);
884 
885  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) <= 0);
886 }

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

◆ date_lt()

Datum date_lt ( PG_FUNCTION_ARGS  )

Definition at line 401 of file date.c.

402 {
403  DateADT dateVal1 = PG_GETARG_DATEADT(0);
404  DateADT dateVal2 = PG_GETARG_DATEADT(1);
405 
406  PG_RETURN_BOOL(dateVal1 < dateVal2);
407 }

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_datelt().

◆ date_lt_timestamp()

Datum date_lt_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 778 of file date.c.

779 {
780  DateADT dateVal = PG_GETARG_DATEADT(0);
782 
783  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) < 0);
784 }

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

◆ date_lt_timestamptz()

Datum date_lt_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 862 of file date.c.

863 {
864  DateADT dateVal = PG_GETARG_DATEADT(0);
866 
868 }

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

◆ date_mi()

Datum date_mi ( PG_FUNCTION_ARGS  )

Definition at line 487 of file date.c.

488 {
489  DateADT dateVal1 = PG_GETARG_DATEADT(0);
490  DateADT dateVal2 = PG_GETARG_DATEADT(1);
491 
492  if (DATE_NOT_FINITE(dateVal1) || DATE_NOT_FINITE(dateVal2))
493  ereport(ERROR,
494  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
495  errmsg("cannot subtract infinite dates")));
496 
497  PG_RETURN_INT32((int32) (dateVal1 - dateVal2));
498 }

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

◆ date_mi_interval()

Datum date_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 1266 of file date.c.

1267 {
1268  DateADT dateVal = PG_GETARG_DATEADT(0);
1269  Interval *span = PG_GETARG_INTERVAL_P(1);
1270  Timestamp dateStamp;
1271 
1272  dateStamp = date2timestamp(dateVal);
1273 
1275  TimestampGetDatum(dateStamp),
1276  PointerGetDatum(span));
1277 }
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3166
static TimestampTz date2timestamp(DateADT dateVal)
Definition: date.c:608
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Datum TimestampGetDatum(Timestamp X)
Definition: timestamp.h:46
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:65

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

◆ date_mii()

Datum date_mii ( PG_FUNCTION_ARGS  )

Definition at line 528 of file date.c.

529 {
530  DateADT dateVal = PG_GETARG_DATEADT(0);
532  DateADT result;
533 
534  if (DATE_NOT_FINITE(dateVal))
535  PG_RETURN_DATEADT(dateVal); /* can't change infinity */
536 
537  result = dateVal - days;
538 
539  /* Check for integer overflow and out-of-allowed-range */
540  if ((days >= 0 ? (result > dateVal) : (result < dateVal)) ||
541  !IS_VALID_DATE(result))
542  ereport(ERROR,
543  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
544  errmsg("date out of range")));
545 
546  PG_RETURN_DATEADT(result);
547 }
const char *const days[]
Definition: datetime.c:84
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269

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

◆ date_ne()

Datum date_ne ( PG_FUNCTION_ARGS  )

Definition at line 392 of file date.c.

393 {
394  DateADT dateVal1 = PG_GETARG_DATEADT(0);
395  DateADT dateVal2 = PG_GETARG_DATEADT(1);
396 
397  PG_RETURN_BOOL(dateVal1 != dateVal2);
398 }

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

◆ date_ne_timestamp()

Datum date_ne_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 769 of file date.c.

770 {
771  DateADT dateVal = PG_GETARG_DATEADT(0);
773 
774  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) != 0);
775 }

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

◆ date_ne_timestamptz()

Datum date_ne_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 853 of file date.c.

854 {
855  DateADT dateVal = PG_GETARG_DATEADT(0);
857 
858  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) != 0);
859 }

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

◆ date_out()

Datum date_out ( PG_FUNCTION_ARGS  )

Definition at line 184 of file date.c.

185 {
187  char *result;
188  struct pg_tm tt,
189  *tm = &tt;
190  char buf[MAXDATELEN + 1];
191 
192  if (DATE_NOT_FINITE(date))
194  else
195  {
197  &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
199  }
200 
201  result = pstrdup(buf);
202  PG_RETURN_CSTRING(result);
203 }
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4227
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:294
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
int DateStyle
Definition: globals.c:122
static char * buf
Definition: pg_test_fsync.c:73

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.

Referenced by ExecGetJsonValueItemString(), and executeItemOptUnwrapTarget().

◆ date_pl_interval()

Datum date_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 1246 of file date.c.

1247 {
1248  DateADT dateVal = PG_GETARG_DATEADT(0);
1249  Interval *span = PG_GETARG_INTERVAL_P(1);
1250  Timestamp dateStamp;
1251 
1252  dateStamp = date2timestamp(dateVal);
1253 
1255  TimestampGetDatum(dateStamp),
1256  PointerGetDatum(span));
1257 }
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3049

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

◆ date_pli()

Datum date_pli ( PG_FUNCTION_ARGS  )

Definition at line 504 of file date.c.

505 {
506  DateADT dateVal = PG_GETARG_DATEADT(0);
508  DateADT result;
509 
510  if (DATE_NOT_FINITE(dateVal))
511  PG_RETURN_DATEADT(dateVal); /* can't change infinity */
512 
513  result = dateVal + days;
514 
515  /* Check for integer overflow and out-of-allowed-range */
516  if ((days >= 0 ? (result < dateVal) : (result > dateVal)) ||
517  !IS_VALID_DATE(result))
518  ereport(ERROR,
519  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
520  errmsg("date out of range")));
521 
522  PG_RETURN_DATEADT(result);
523 }

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

◆ date_recv()

Datum date_recv ( PG_FUNCTION_ARGS  )

Definition at line 209 of file date.c.

210 {
212  DateADT result;
213 
214  result = (DateADT) pq_getmsgint(buf, sizeof(DateADT));
215 
216  /* Limit to the same range that date_in() accepts. */
217  if (DATE_NOT_FINITE(result))
218  /* ok */ ;
219  else if (!IS_VALID_DATE(result))
220  ereport(ERROR,
221  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
222  errmsg("date out of range")));
223 
224  PG_RETURN_DATEADT(result);
225 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:415
StringInfoData * StringInfo
Definition: stringinfo.h:54

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

◆ date_send()

Datum date_send ( PG_FUNCTION_ARGS  )

Definition at line 231 of file date.c.

232 {
235 
237  pq_sendint32(&buf, date);
239 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:144

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

◆ date_smaller()

Datum date_smaller ( PG_FUNCTION_ARGS  )

Definition at line 476 of file date.c.

477 {
478  DateADT dateVal1 = PG_GETARG_DATEADT(0);
479  DateADT dateVal2 = PG_GETARG_DATEADT(1);
480 
481  PG_RETURN_DATEADT((dateVal1 < dateVal2) ? dateVal1 : dateVal2);
482 }

References PG_GETARG_DATEADT, and PG_RETURN_DATEADT.

◆ date_sortsupport()

Datum date_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 450 of file date.c.

451 {
453 
455  PG_RETURN_VOID();
456 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
int ssup_datum_int32_cmp(Datum x, Datum y, SortSupport ssup)
Definition: tuplesort.c:3204

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

◆ date_timestamp()

Datum date_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1283 of file date.c.

1284 {
1285  DateADT dateVal = PG_GETARG_DATEADT(0);
1286  Timestamp result;
1287 
1288  result = date2timestamp(dateVal);
1289 
1290  PG_RETURN_TIMESTAMP(result);
1291 }
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:67

References date2timestamp(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

Referenced by executeDateTimeMethod().

◆ date_timestamptz()

Datum date_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 1327 of file date.c.

1328 {
1329  DateADT dateVal = PG_GETARG_DATEADT(0);
1330  TimestampTz result;
1331 
1332  result = date2timestamptz(dateVal);
1333 
1334  PG_RETURN_TIMESTAMP(result);
1335 }
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:704

References date2timestamptz(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

Referenced by executeDateTimeMethod().

◆ datetime_timestamp()

Datum datetime_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1966 of file date.c.

1967 {
1969  TimeADT time = PG_GETARG_TIMEADT(1);
1970  Timestamp result;
1971 
1972  result = date2timestamp(date);
1973  if (!TIMESTAMP_NOT_FINITE(result))
1974  {
1975  result += time;
1976  if (!IS_VALID_TIMESTAMP(result))
1977  ereport(ERROR,
1978  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1979  errmsg("timestamp out of range")));
1980  }
1981 
1982  PG_RETURN_TIMESTAMP(result);
1983 }
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:169
#define PG_GETARG_TIMEADT(n)
Definition: date.h:90
int64 TimeADT
Definition: date.h:25

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

◆ datetimetz_timestamptz()

Datum datetimetz_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 2886 of file date.c.

2887 {
2889  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2890  TimestampTz result;
2891 
2892  if (DATE_IS_NOBEGIN(date))
2893  TIMESTAMP_NOBEGIN(result);
2894  else if (DATE_IS_NOEND(date))
2895  TIMESTAMP_NOEND(result);
2896  else
2897  {
2898  /*
2899  * Date's range is wider than timestamp's, so check for boundaries.
2900  * Since dates have the same minimum values as timestamps, only upper
2901  * boundary need be checked for overflow.
2902  */
2904  ereport(ERROR,
2905  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2906  errmsg("date out of range for timestamp")));
2907  result = date * USECS_PER_DAY + time->time + time->zone * USECS_PER_SEC;
2908 
2909  /*
2910  * Since it is possible to go beyond allowed timestamptz range because
2911  * of time zone, check for allowed timestamp range after adding tz.
2912  */
2913  if (!IS_VALID_TIMESTAMP(result))
2914  ereport(ERROR,
2915  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2916  errmsg("date out of range for timestamp")));
2917  }
2918 
2919  PG_RETURN_TIMESTAMP(result);
2920 }
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:91
Definition: date.h:28
TimeADT time
Definition: date.h:29
int32 zone
Definition: date.h:30

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.

◆ EncodeSpecialDate()

void EncodeSpecialDate ( DateADT  dt,
char *  str 
)

Definition at line 294 of file date.c.

295 {
296  if (DATE_IS_NOBEGIN(dt))
297  strcpy(str, EARLY);
298  else if (DATE_IS_NOEND(dt))
299  strcpy(str, LATE);
300  else /* shouldn't happen */
301  elog(ERROR, "invalid argument for EncodeSpecialDate");
302 }
#define elog(elevel,...)
Definition: elog.h:224
#define EARLY
Definition: datetime.h:39
#define LATE
Definition: datetime.h:40

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

Referenced by date_out(), and JsonEncodeDateTime().

◆ extract_date()

Datum extract_date ( PG_FUNCTION_ARGS  )

Definition at line 1066 of file date.c.

1067 {
1068  text *units = PG_GETARG_TEXT_PP(0);
1070  int64 intresult;
1071  int type,
1072  val;
1073  char *lowunits;
1074  int year,
1075  mon,
1076  mday;
1077 
1078  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
1079  VARSIZE_ANY_EXHDR(units),
1080  false);
1081 
1082  type = DecodeUnits(0, lowunits, &val);
1083  if (type == UNKNOWN_FIELD)
1084  type = DecodeSpecial(0, lowunits, &val);
1085 
1086  if (DATE_NOT_FINITE(date) && (type == UNITS || type == RESERV))
1087  {
1088  switch (val)
1089  {
1090  /* Oscillating units */
1091  case DTK_DAY:
1092  case DTK_MONTH:
1093  case DTK_QUARTER:
1094  case DTK_WEEK:
1095  case DTK_DOW:
1096  case DTK_ISODOW:
1097  case DTK_DOY:
1098  PG_RETURN_NULL();
1099  break;
1100 
1101  /* Monotonically-increasing units */
1102  case DTK_YEAR:
1103  case DTK_DECADE:
1104  case DTK_CENTURY:
1105  case DTK_MILLENNIUM:
1106  case DTK_JULIAN:
1107  case DTK_ISOYEAR:
1108  case DTK_EPOCH:
1109  if (DATE_IS_NOBEGIN(date))
1111  CStringGetDatum("-Infinity"),
1113  Int32GetDatum(-1))));
1114  else
1116  CStringGetDatum("Infinity"),
1118  Int32GetDatum(-1))));
1119  default:
1120  ereport(ERROR,
1121  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1122  errmsg("unit \"%s\" not supported for type %s",
1123  lowunits, format_type_be(DATEOID))));
1124  }
1125  }
1126  else if (type == UNITS)
1127  {
1128  j2date(date + POSTGRES_EPOCH_JDATE, &year, &mon, &mday);
1129 
1130  switch (val)
1131  {
1132  case DTK_DAY:
1133  intresult = mday;
1134  break;
1135 
1136  case DTK_MONTH:
1137  intresult = mon;
1138  break;
1139 
1140  case DTK_QUARTER:
1141  intresult = (mon - 1) / 3 + 1;
1142  break;
1143 
1144  case DTK_WEEK:
1145  intresult = date2isoweek(year, mon, mday);
1146  break;
1147 
1148  case DTK_YEAR:
1149  if (year > 0)
1150  intresult = year;
1151  else
1152  /* there is no year 0, just 1 BC and 1 AD */
1153  intresult = year - 1;
1154  break;
1155 
1156  case DTK_DECADE:
1157  /* see comments in timestamp_part */
1158  if (year >= 0)
1159  intresult = year / 10;
1160  else
1161  intresult = -((8 - (year - 1)) / 10);
1162  break;
1163 
1164  case DTK_CENTURY:
1165  /* see comments in timestamp_part */
1166  if (year > 0)
1167  intresult = (year + 99) / 100;
1168  else
1169  intresult = -((99 - (year - 1)) / 100);
1170  break;
1171 
1172  case DTK_MILLENNIUM:
1173  /* see comments in timestamp_part */
1174  if (year > 0)
1175  intresult = (year + 999) / 1000;
1176  else
1177  intresult = -((999 - (year - 1)) / 1000);
1178  break;
1179 
1180  case DTK_JULIAN:
1181  intresult = date + POSTGRES_EPOCH_JDATE;
1182  break;
1183 
1184  case DTK_ISOYEAR:
1185  intresult = date2isoyear(year, mon, mday);
1186  /* Adjust BC years */
1187  if (intresult <= 0)
1188  intresult -= 1;
1189  break;
1190 
1191  case DTK_DOW:
1192  case DTK_ISODOW:
1193  intresult = j2day(date + POSTGRES_EPOCH_JDATE);
1194  if (val == DTK_ISODOW && intresult == 0)
1195  intresult = 7;
1196  break;
1197 
1198  case DTK_DOY:
1199  intresult = date2j(year, mon, mday) - date2j(year, 1, 1) + 1;
1200  break;
1201 
1202  default:
1203  ereport(ERROR,
1204  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1205  errmsg("unit \"%s\" not supported for type %s",
1206  lowunits, format_type_be(DATEOID))));
1207  intresult = 0;
1208  }
1209  }
1210  else if (type == RESERV)
1211  {
1212  switch (val)
1213  {
1214  case DTK_EPOCH:
1215  intresult = ((int64) date + POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY;
1216  break;
1217 
1218  default:
1219  ereport(ERROR,
1220  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1221  errmsg("unit \"%s\" not supported for type %s",
1222  lowunits, format_type_be(DATEOID))));
1223  intresult = 0;
1224  }
1225  }
1226  else
1227  {
1228  ereport(ERROR,
1229  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1230  errmsg("unit \"%s\" not recognized for type %s",
1231  lowunits, format_type_be(DATEOID))));
1232  intresult = 0;
1233  }
1234 
1235  PG_RETURN_NUMERIC(int64_to_numeric(intresult));
1236 }
int DecodeUnits(int field, const char *lowtoken, int *val)
Definition: datetime.c:4047
int j2day(int date)
Definition: datetime.c:344
int DecodeSpecial(int field, const char *lowtoken, int *val)
Definition: datetime.c:3148
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4283
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:628
int date2isoweek(int year, int mon, int mday)
Definition: timestamp.c:5167
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:5222
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:234
#define SECS_PER_DAY
Definition: timestamp.h:126
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:646
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#define UNKNOWN_FIELD
Definition: datetime.h:124
#define DTK_DECADE
Definition: datetime.h:168
#define DTK_QUARTER
Definition: datetime.h:166
#define DTK_JULIAN
Definition: datetime.h:173
#define DTK_CENTURY
Definition: datetime.h:169
#define DTK_ISODOW
Definition: datetime.h:180
#define DTK_DAY
Definition: datetime.h:163
#define RESERV
Definition: datetime.h:90
#define DTK_MILLENNIUM
Definition: datetime.h:170
#define DTK_ISOYEAR
Definition: datetime.h:179
#define DTK_DOY
Definition: datetime.h:176
#define DTK_WEEK
Definition: datetime.h:164
#define DTK_DOW
Definition: datetime.h:175
#define DTK_YEAR
Definition: datetime.h:167
#define DTK_MONTH
Definition: datetime.h:165
#define UNITS
Definition: datetime.h:107
long val
Definition: informix.c:670
static Numeric DatumGetNumeric(Datum X)
Definition: numeric.h:61
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:80
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
#define InvalidOid
Definition: postgres_ext.h:36
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:37
Definition: c.h:687
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
const char * type

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, format_type_be(), 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, type, UNITS, UNIX_EPOCH_JDATE, UNKNOWN_FIELD, val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ extract_time()

Datum extract_time ( PG_FUNCTION_ARGS  )

Definition at line 2249 of file date.c.

2250 {
2251  return time_part_common(fcinfo, true);
2252 }
static Datum time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2140

References time_part_common().

◆ extract_timetz()

Datum extract_timetz ( PG_FUNCTION_ARGS  )

Definition at line 3050 of file date.c.

3051 {
3052  return timetz_part_common(fcinfo, true);
3053 }
static Datum timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2927

References timetz_part_common().

◆ float_time_overflows()

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

Definition at line 1451 of file date.c.

1452 {
1453  /* Range-check the fields individually. */
1454  if (hour < 0 || hour > HOURS_PER_DAY ||
1455  min < 0 || min >= MINS_PER_HOUR)
1456  return true;
1457 
1458  /*
1459  * "sec", being double, requires extra care. Cope with NaN, and round off
1460  * before applying the range check to avoid unexpected errors due to
1461  * imprecise input. (We assume rint() behaves sanely with infinities.)
1462  */
1463  if (isnan(sec))
1464  return true;
1465  sec = rint(sec * USECS_PER_SEC);
1466  if (sec < 0 || sec > SECS_PER_MINUTE * USECS_PER_SEC)
1467  return true;
1468 
1469  /*
1470  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1471  * that the total time value doesn't exceed 24:00:00. This must match the
1472  * way that callers will convert the fields to a time.
1473  */
1474  if (((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1475  * USECS_PER_SEC) + (int64) sec) > USECS_PER_DAY)
1476  return true;
1477 
1478  return false;
1479 }
#define MINS_PER_HOUR
Definition: timestamp.h:129
#define SECS_PER_MINUTE
Definition: timestamp.h:128
#define HOURS_PER_DAY
Definition: timestamp.h:118

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

◆ GetSQLCurrentDate()

DateADT GetSQLCurrentDate ( void  )

Definition at line 309 of file date.c.

310 {
311  struct pg_tm tm;
312 
313  static int cache_year = 0;
314  static int cache_mon = 0;
315  static int cache_mday = 0;
316  static DateADT cache_date;
317 
319 
320  /*
321  * date2j involves several integer divisions; moreover, unless our session
322  * lives across local midnight, we don't really have to do it more than
323  * once. So it seems worth having a separate cache here.
324  */
325  if (tm.tm_year != cache_year ||
326  tm.tm_mon != cache_mon ||
327  tm.tm_mday != cache_mday)
328  {
330  cache_year = tm.tm_year;
331  cache_mon = tm.tm_mon;
332  cache_mday = tm.tm_mday;
333  }
334 
335  return cache_date;
336 }
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:366

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

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLCurrentTime()

TimeTzADT* GetSQLCurrentTime ( int32  typmod)

Definition at line 342 of file date.c.

343 {
344  TimeTzADT *result;
345  struct pg_tm tt,
346  *tm = &tt;
347  fsec_t fsec;
348  int tz;
349 
350  GetCurrentTimeUsec(tm, &fsec, &tz);
351 
352  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
353  tm2timetz(tm, fsec, tz, result);
354  AdjustTimeForTypmod(&(result->time), typmod);
355  return result;
356 }
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:387
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2263
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1645
void * palloc(Size size)
Definition: mcxt.c:1316

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

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLLocalTime()

TimeADT GetSQLLocalTime ( int32  typmod)

Definition at line 362 of file date.c.

363 {
364  TimeADT result;
365  struct pg_tm tt,
366  *tm = &tt;
367  fsec_t fsec;
368  int tz;
369 
370  GetCurrentTimeUsec(tm, &fsec, &tz);
371 
372  tm2time(tm, fsec, &result);
373  AdjustTimeForTypmod(&result, typmod);
374  return result;
375 }
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1416

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

Referenced by ExecEvalSQLValueFunction().

◆ in_range_date_interval()

Datum in_range_date_interval ( PG_FUNCTION_ARGS  )

Definition at line 1039 of file date.c.

1040 {
1042  DateADT base = PG_GETARG_DATEADT(1);
1043  Interval *offset = PG_GETARG_INTERVAL_P(2);
1044  bool sub = PG_GETARG_BOOL(3);
1045  bool less = PG_GETARG_BOOL(4);
1046  Timestamp valStamp;
1047  Timestamp baseStamp;
1048 
1049  /* XXX we could support out-of-range cases here, perhaps */
1050  valStamp = date2timestamp(val);
1051  baseStamp = date2timestamp(base);
1052 
1054  TimestampGetDatum(valStamp),
1055  TimestampGetDatum(baseStamp),
1056  IntervalPGetDatum(offset),
1057  BoolGetDatum(sub),
1058  BoolGetDatum(less));
1059 }
Datum in_range_timestamp_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3835
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:650
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum IntervalPGetDatum(const Interval *X)
Definition: timestamp.h:58

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

◆ in_range_time_interval()

Datum in_range_time_interval ( PG_FUNCTION_ARGS  )

Definition at line 2098 of file date.c.

2099 {
2101  TimeADT base = PG_GETARG_TIMEADT(1);
2102  Interval *offset = PG_GETARG_INTERVAL_P(2);
2103  bool sub = PG_GETARG_BOOL(3);
2104  bool less = PG_GETARG_BOOL(4);
2105  TimeADT sum;
2106 
2107  /*
2108  * Like time_pl_interval/time_mi_interval, we disregard the month and day
2109  * fields of the offset. So our test for negative should too. This also
2110  * catches -infinity, so we only need worry about +infinity below.
2111  */
2112  if (offset->time < 0)
2113  ereport(ERROR,
2114  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2115  errmsg("invalid preceding or following size in window function")));
2116 
2117  /*
2118  * We can't use time_pl_interval/time_mi_interval here, because their
2119  * wraparound behavior would give wrong (or at least undesirable) answers.
2120  * Fortunately the equivalent non-wrapping behavior is trivial, except
2121  * that adding an infinite (or very large) interval might cause integer
2122  * overflow. Subtraction cannot overflow here.
2123  */
2124  if (sub)
2125  sum = base - offset->time;
2126  else if (pg_add_s64_overflow(base, offset->time, &sum))
2127  PG_RETURN_BOOL(less);
2128 
2129  if (less)
2130  PG_RETURN_BOOL(val <= sum);
2131  else
2132  PG_RETURN_BOOL(val >= sum);
2133 }
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:161
TimeOffset time
Definition: timestamp.h:49

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

◆ in_range_timetz_interval()

Datum in_range_timetz_interval ( PG_FUNCTION_ARGS  )

Definition at line 2650 of file date.c.

2651 {
2653  TimeTzADT *base = PG_GETARG_TIMETZADT_P(1);
2654  Interval *offset = PG_GETARG_INTERVAL_P(2);
2655  bool sub = PG_GETARG_BOOL(3);
2656  bool less = PG_GETARG_BOOL(4);
2657  TimeTzADT sum;
2658 
2659  /*
2660  * Like timetz_pl_interval/timetz_mi_interval, we disregard the month and
2661  * day fields of the offset. So our test for negative should too. This
2662  * also catches -infinity, so we only need worry about +infinity below.
2663  */
2664  if (offset->time < 0)
2665  ereport(ERROR,
2666  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2667  errmsg("invalid preceding or following size in window function")));
2668 
2669  /*
2670  * We can't use timetz_pl_interval/timetz_mi_interval here, because their
2671  * wraparound behavior would give wrong (or at least undesirable) answers.
2672  * Fortunately the equivalent non-wrapping behavior is trivial, except
2673  * that adding an infinite (or very large) interval might cause integer
2674  * overflow. Subtraction cannot overflow here.
2675  */
2676  if (sub)
2677  sum.time = base->time - offset->time;
2678  else if (pg_add_s64_overflow(base->time, offset->time, &sum.time))
2679  PG_RETURN_BOOL(less);
2680  sum.zone = base->zone;
2681 
2682  if (less)
2683  PG_RETURN_BOOL(timetz_cmp_internal(val, &sum) <= 0);
2684  else
2685  PG_RETURN_BOOL(timetz_cmp_internal(val, &sum) >= 0);
2686 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2443

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

◆ interval_time()

Datum interval_time ( PG_FUNCTION_ARGS  )

Definition at line 2012 of file date.c.

2013 {
2014  Interval *span = PG_GETARG_INTERVAL_P(0);
2015  TimeADT result;
2016 
2017  if (INTERVAL_NOT_FINITE(span))
2018  ereport(ERROR,
2019  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2020  errmsg("cannot convert infinite interval to time")));
2021 
2022  result = span->time % USECS_PER_DAY;
2023  if (result < 0)
2024  result += USECS_PER_DAY;
2025 
2026  PG_RETURN_TIMEADT(result);
2027 }
#define INTERVAL_NOT_FINITE(i)
Definition: timestamp.h:195
#define PG_RETURN_TIMEADT(x)
Definition: date.h:94

References ereport, errcode(), errmsg(), ERROR, INTERVAL_NOT_FINITE, PG_GETARG_INTERVAL_P, PG_RETURN_TIMEADT, Interval::time, and USECS_PER_DAY.

◆ make_date()

Datum make_date ( PG_FUNCTION_ARGS  )

Definition at line 245 of file date.c.

246 {
247  struct pg_tm tm;
248  DateADT date;
249  int dterr;
250  bool bc = false;
251 
255 
256  /* Handle negative years as BC */
257  if (tm.tm_year < 0)
258  {
259  bc = true;
260  tm.tm_year = -tm.tm_year;
261  }
262 
263  dterr = ValidateDate(DTK_DATE_M, false, false, bc, &tm);
264 
265  if (dterr != 0)
266  ereport(ERROR,
267  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
268  errmsg("date field value out of range: %d-%02d-%02d",
269  tm.tm_year, tm.tm_mon, tm.tm_mday)));
270 
271  /* Prevent overflow in Julian-day routines */
273  ereport(ERROR,
274  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
275  errmsg("date out of range: %d-%02d-%02d",
276  tm.tm_year, tm.tm_mon, tm.tm_mday)));
277 
279 
280  /* Now check for just-out-of-range dates */
281  if (!IS_VALID_DATE(date))
282  ereport(ERROR,
283  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
284  errmsg("date out of range: %d-%02d-%02d",
285  tm.tm_year, tm.tm_mon, tm.tm_mday)));
286 
288 }
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2508
#define DTK_DATE_M
Definition: datetime.h:191

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

◆ make_time()

Datum make_time ( PG_FUNCTION_ARGS  )

Definition at line 1577 of file date.c.

1578 {
1579  int tm_hour = PG_GETARG_INT32(0);
1580  int tm_min = PG_GETARG_INT32(1);
1581  double sec = PG_GETARG_FLOAT8(2);
1582  TimeADT time;
1583 
1584  /* Check for time overflow */
1585  if (float_time_overflows(tm_hour, tm_min, sec))
1586  ereport(ERROR,
1587  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
1588  errmsg("time field value out of range: %d:%02d:%02g",
1589  tm_hour, tm_min, sec)));
1590 
1591  /* This should match tm2time */
1592  time = (((tm_hour * MINS_PER_HOUR + tm_min) * SECS_PER_MINUTE)
1593  * USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
1594 
1595  PG_RETURN_TIMEADT(time);
1596 }
bool float_time_overflows(int hour, int min, double sec)
Definition: date.c:1451
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282

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.

◆ overlaps_time()

Datum overlaps_time ( PG_FUNCTION_ARGS  )

Definition at line 1783 of file date.c.

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

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

◆ overlaps_timetz()

Datum overlaps_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2695 of file date.c.

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

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

◆ time2tm()

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

Definition at line 1488 of file date.c.

1489 {
1490  tm->tm_hour = time / USECS_PER_HOUR;
1491  time -= tm->tm_hour * USECS_PER_HOUR;
1492  tm->tm_min = time / USECS_PER_MINUTE;
1493  time -= tm->tm_min * USECS_PER_MINUTE;
1494  tm->tm_sec = time / USECS_PER_SEC;
1495  time -= tm->tm_sec * USECS_PER_SEC;
1496  *fsec = time;
1497  return 0;
1498 }
#define USECS_PER_HOUR
Definition: timestamp.h:132
#define USECS_PER_MINUTE
Definition: timestamp.h:133

References tm, 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().

◆ time_cmp()

Datum time_cmp ( PG_FUNCTION_ARGS  )

Definition at line 1734 of file date.c.

1735 {
1736  TimeADT time1 = PG_GETARG_TIMEADT(0);
1737  TimeADT time2 = PG_GETARG_TIMEADT(1);
1738 
1739  if (time1 < time2)
1740  PG_RETURN_INT32(-1);
1741  if (time1 > time2)
1742  PG_RETURN_INT32(1);
1743  PG_RETURN_INT32(0);
1744 }

References PG_GETARG_TIMEADT, and PG_RETURN_INT32.

Referenced by compareDatetime(), and gbt_timekey_cmp().

◆ time_eq()

Datum time_eq ( PG_FUNCTION_ARGS  )

Definition at line 1680 of file date.c.

1681 {
1682  TimeADT time1 = PG_GETARG_TIMEADT(0);
1683  TimeADT time2 = PG_GETARG_TIMEADT(1);
1684 
1685  PG_RETURN_BOOL(time1 == time2);
1686 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timeeq().

◆ time_ge()

Datum time_ge ( PG_FUNCTION_ARGS  )

Definition at line 1725 of file date.c.

1726 {
1727  TimeADT time1 = PG_GETARG_TIMEADT(0);
1728  TimeADT time2 = PG_GETARG_TIMEADT(1);
1729 
1730  PG_RETURN_BOOL(time1 >= time2);
1731 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timege().

◆ time_gt()

Datum time_gt ( PG_FUNCTION_ARGS  )

Definition at line 1716 of file date.c.

1717 {
1718  TimeADT time1 = PG_GETARG_TIMEADT(0);
1719  TimeADT time2 = PG_GETARG_TIMEADT(1);
1720 
1721  PG_RETURN_BOOL(time1 > time2);
1722 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timegt().

◆ time_hash()

Datum time_hash ( PG_FUNCTION_ARGS  )

Definition at line 1747 of file date.c.

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

References hashint8().

◆ time_hash_extended()

Datum time_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 1753 of file date.c.

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

References hashint8extended().

◆ time_in()

Datum time_in ( PG_FUNCTION_ARGS  )

Definition at line 1374 of file date.c.

1375 {
1376  char *str = PG_GETARG_CSTRING(0);
1377 #ifdef NOT_USED
1378  Oid typelem = PG_GETARG_OID(1);
1379 #endif
1380  int32 typmod = PG_GETARG_INT32(2);
1381  Node *escontext = fcinfo->context;
1382  TimeADT result;
1383  fsec_t fsec;
1384  struct pg_tm tt,
1385  *tm = &tt;
1386  int tz;
1387  int nf;
1388  int dterr;
1389  char workbuf[MAXDATELEN + 1];
1390  char *field[MAXDATEFIELDS];
1391  int dtype;
1392  int ftype[MAXDATEFIELDS];
1393  DateTimeErrorExtra extra;
1394 
1395  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
1396  field, ftype, MAXDATEFIELDS, &nf);
1397  if (dterr == 0)
1398  dterr = DecodeTimeOnly(field, ftype, nf,
1399  &dtype, tm, &fsec, &tz, &extra);
1400  if (dterr != 0)
1401  {
1402  DateTimeParseError(dterr, &extra, str, "time", escontext);
1403  PG_RETURN_NULL();
1404  }
1405 
1406  tm2time(tm, fsec, &result);
1407  AdjustTimeForTypmod(&result, typmod);
1408 
1409  PG_RETURN_TIMEADT(result);
1410 }
int DecodeTimeOnly(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp, DateTimeErrorExtra *extra)
Definition: datetime.c:1864
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
unsigned int Oid
Definition: postgres_ext.h:31

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

◆ time_interval()

Datum time_interval ( PG_FUNCTION_ARGS  )

Definition at line 1989 of file date.c.

1990 {
1991  TimeADT time = PG_GETARG_TIMEADT(0);
1992  Interval *result;
1993 
1994  result = (Interval *) palloc(sizeof(Interval));
1995 
1996  result->time = time;
1997  result->day = 0;
1998  result->month = 0;
1999 
2000  PG_RETURN_INTERVAL_P(result);
2001 }
int32 day
Definition: timestamp.h:51
int32 month
Definition: timestamp.h:52
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:69

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

◆ time_larger()

Datum time_larger ( PG_FUNCTION_ARGS  )

Definition at line 1759 of file date.c.

1760 {
1761  TimeADT time1 = PG_GETARG_TIMEADT(0);
1762  TimeADT time2 = PG_GETARG_TIMEADT(1);
1763 
1764  PG_RETURN_TIMEADT((time1 > time2) ? time1 : time2);
1765 }

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

◆ time_le()

Datum time_le ( PG_FUNCTION_ARGS  )

Definition at line 1707 of file date.c.

1708 {
1709  TimeADT time1 = PG_GETARG_TIMEADT(0);
1710  TimeADT time2 = PG_GETARG_TIMEADT(1);
1711 
1712  PG_RETURN_BOOL(time1 <= time2);
1713 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timele().

◆ time_lt()

Datum time_lt ( PG_FUNCTION_ARGS  )

Definition at line 1698 of file date.c.

1699 {
1700  TimeADT time1 = PG_GETARG_TIMEADT(0);
1701  TimeADT time2 = PG_GETARG_TIMEADT(1);
1702 
1703  PG_RETURN_BOOL(time1 < time2);
1704 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timelt().

◆ time_mi_interval()

Datum time_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 2075 of file date.c.

2076 {
2077  TimeADT time = PG_GETARG_TIMEADT(0);
2078  Interval *span = PG_GETARG_INTERVAL_P(1);
2079  TimeADT result;
2080 
2081  if (INTERVAL_NOT_FINITE(span))
2082  ereport(ERROR,
2083  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2084  errmsg("cannot subtract infinite interval from time")));
2085 
2086  result = time - span->time;
2087  result -= result / USECS_PER_DAY * USECS_PER_DAY;
2088  if (result < INT64CONST(0))
2089  result += USECS_PER_DAY;
2090 
2091  PG_RETURN_TIMEADT(result);
2092 }

References ereport, errcode(), errmsg(), ERROR, INTERVAL_NOT_FINITE, PG_GETARG_INTERVAL_P, PG_GETARG_TIMEADT, PG_RETURN_TIMEADT, Interval::time, and USECS_PER_DAY.

◆ time_mi_time()

Datum time_mi_time ( PG_FUNCTION_ARGS  )

Definition at line 2033 of file date.c.

2034 {
2035  TimeADT time1 = PG_GETARG_TIMEADT(0);
2036  TimeADT time2 = PG_GETARG_TIMEADT(1);
2037  Interval *result;
2038 
2039  result = (Interval *) palloc(sizeof(Interval));
2040 
2041  result->month = 0;
2042  result->day = 0;
2043  result->time = time1 - time2;
2044 
2045  PG_RETURN_INTERVAL_P(result);
2046 }

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

◆ time_ne()

Datum time_ne ( PG_FUNCTION_ARGS  )

Definition at line 1689 of file date.c.

1690 {
1691  TimeADT time1 = PG_GETARG_TIMEADT(0);
1692  TimeADT time2 = PG_GETARG_TIMEADT(1);
1693 
1694  PG_RETURN_BOOL(time1 != time2);
1695 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

◆ time_out()

Datum time_out ( PG_FUNCTION_ARGS  )

Definition at line 1501 of file date.c.

1502 {
1503  TimeADT time = PG_GETARG_TIMEADT(0);
1504  char *result;
1505  struct pg_tm tt,
1506  *tm = &tt;
1507  fsec_t fsec;
1508  char buf[MAXDATELEN + 1];
1509 
1510  time2tm(time, tm, &fsec);
1511  EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf);
1512 
1513  result = pstrdup(buf);
1514  PG_RETURN_CSTRING(result);
1515 }
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:4312
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1488

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

Referenced by ExecGetJsonValueItemString(), and executeItemOptUnwrapTarget().

◆ time_overflows()

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

Definition at line 1427 of file date.c.

1428 {
1429  /* Range-check the fields individually. */
1430  if (hour < 0 || hour > HOURS_PER_DAY ||
1431  min < 0 || min >= MINS_PER_HOUR ||
1432  sec < 0 || sec > SECS_PER_MINUTE ||
1433  fsec < 0 || fsec > USECS_PER_SEC)
1434  return true;
1435 
1436  /*
1437  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1438  * that the total time value doesn't exceed 24:00:00.
1439  */
1440  if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1441  + sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1442  return true;
1443 
1444  return false;
1445 }

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

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ time_part()

Datum time_part ( PG_FUNCTION_ARGS  )

Definition at line 2243 of file date.c.

2244 {
2245  return time_part_common(fcinfo, false);
2246 }

References time_part_common().

◆ time_part_common()

static Datum time_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2140 of file date.c.

2141 {
2142  text *units = PG_GETARG_TEXT_PP(0);
2143  TimeADT time = PG_GETARG_TIMEADT(1);
2144  int64 intresult;
2145  int type,
2146  val;
2147  char *lowunits;
2148 
2149  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
2150  VARSIZE_ANY_EXHDR(units),
2151  false);
2152 
2153  type = DecodeUnits(0, lowunits, &val);
2154  if (type == UNKNOWN_FIELD)
2155  type = DecodeSpecial(0, lowunits, &val);
2156 
2157  if (type == UNITS)
2158  {
2159  fsec_t fsec;
2160  struct pg_tm tt,
2161  *tm = &tt;
2162 
2163  time2tm(time, tm, &fsec);
2164 
2165  switch (val)
2166  {
2167  case DTK_MICROSEC:
2168  intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
2169  break;
2170 
2171  case DTK_MILLISEC:
2172  if (retnumeric)
2173  /*---
2174  * tm->tm_sec * 1000 + fsec / 1000
2175  * = (tm->tm_sec * 1'000'000 + fsec) / 1000
2176  */
2177  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
2178  else
2179  PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
2180  break;
2181 
2182  case DTK_SECOND:
2183  if (retnumeric)
2184  /*---
2185  * tm->tm_sec + fsec / 1'000'000
2186  * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
2187  */
2188  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
2189  else
2190  PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
2191  break;
2192 
2193  case DTK_MINUTE:
2194  intresult = tm->tm_min;
2195  break;
2196 
2197  case DTK_HOUR:
2198  intresult = tm->tm_hour;
2199  break;
2200 
2201  case DTK_TZ:
2202  case DTK_TZ_MINUTE:
2203  case DTK_TZ_HOUR:
2204  case DTK_DAY:
2205  case DTK_MONTH:
2206  case DTK_QUARTER:
2207  case DTK_YEAR:
2208  case DTK_DECADE:
2209  case DTK_CENTURY:
2210  case DTK_MILLENNIUM:
2211  case DTK_ISOYEAR:
2212  default:
2213  ereport(ERROR,
2214  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2215  errmsg("unit \"%s\" not supported for type %s",
2216  lowunits, format_type_be(TIMEOID))));
2217  intresult = 0;
2218  }
2219  }
2220  else if (type == RESERV && val == DTK_EPOCH)
2221  {
2222  if (retnumeric)
2224  else
2225  PG_RETURN_FLOAT8(time / 1000000.0);
2226  }
2227  else
2228  {
2229  ereport(ERROR,
2230  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2231  errmsg("unit \"%s\" not recognized for type %s",
2232  lowunits, format_type_be(TIMEOID))));
2233  intresult = 0;
2234  }
2235 
2236  if (retnumeric)
2237  PG_RETURN_NUMERIC(int64_to_numeric(intresult));
2238  else
2239  PG_RETURN_FLOAT8(intresult);
2240 }
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4304
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define DTK_SECOND
Definition: datetime.h:160
#define DTK_TZ_HOUR
Definition: datetime.h:177
#define DTK_TZ_MINUTE
Definition: datetime.h:178
#define DTK_TZ
Definition: datetime.h:146
#define DTK_HOUR
Definition: datetime.h:162
#define DTK_MICROSEC
Definition: datetime.h:172
#define DTK_MILLISEC
Definition: datetime.h:171
#define DTK_MINUTE
Definition: datetime.h:161

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, format_type_be(), 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, type, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by extract_time(), and time_part().

◆ time_pl_interval()

Datum time_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 2052 of file date.c.

2053 {
2054  TimeADT time = PG_GETARG_TIMEADT(0);
2055  Interval *span = PG_GETARG_INTERVAL_P(1);
2056  TimeADT result;
2057 
2058  if (INTERVAL_NOT_FINITE(span))
2059  ereport(ERROR,
2060  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2061  errmsg("cannot add infinite interval to time")));
2062 
2063  result = time + span->time;
2064  result -= result / USECS_PER_DAY * USECS_PER_DAY;
2065  if (result < INT64CONST(0))
2066  result += USECS_PER_DAY;
2067 
2068  PG_RETURN_TIMEADT(result);
2069 }

References ereport, errcode(), errmsg(), ERROR, INTERVAL_NOT_FINITE, PG_GETARG_INTERVAL_P, PG_GETARG_TIMEADT, PG_RETURN_TIMEADT, Interval::time, and USECS_PER_DAY.

◆ time_recv()

Datum time_recv ( PG_FUNCTION_ARGS  )

Definition at line 1521 of file date.c.

1522 {
1524 
1525 #ifdef NOT_USED
1526  Oid typelem = PG_GETARG_OID(1);
1527 #endif
1528  int32 typmod = PG_GETARG_INT32(2);
1529  TimeADT result;
1530 
1531  result = pq_getmsgint64(buf);
1532 
1533  if (result < INT64CONST(0) || result > USECS_PER_DAY)
1534  ereport(ERROR,
1535  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1536  errmsg("time out of range")));
1537 
1538  AdjustTimeForTypmod(&result, typmod);
1539 
1540  PG_RETURN_TIMEADT(result);
1541 }
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:453

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.

◆ time_scale()

Datum time_scale ( PG_FUNCTION_ARGS  )

Definition at line 1625 of file date.c.

1626 {
1627  TimeADT time = PG_GETARG_TIMEADT(0);
1628  int32 typmod = PG_GETARG_INT32(1);
1629  TimeADT result;
1630 
1631  result = time;
1632  AdjustTimeForTypmod(&result, typmod);
1633 
1634  PG_RETURN_TIMEADT(result);
1635 }

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

◆ time_send()

Datum time_send ( PG_FUNCTION_ARGS  )

Definition at line 1547 of file date.c.

1548 {
1549  TimeADT time = PG_GETARG_TIMEADT(0);
1551 
1552  pq_begintypsend(&buf);
1553  pq_sendint64(&buf, time);
1555 }
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:152

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

◆ time_smaller()

Datum time_smaller ( PG_FUNCTION_ARGS  )

Definition at line 1768 of file date.c.

1769 {
1770  TimeADT time1 = PG_GETARG_TIMEADT(0);
1771  TimeADT time2 = PG_GETARG_TIMEADT(1);
1772 
1773  PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
1774 }

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

◆ time_support()

Datum time_support ( PG_FUNCTION_ARGS  )

Definition at line 1605 of file date.c.

1606 {
1607  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1608  Node *ret = NULL;
1609 
1610  if (IsA(rawreq, SupportRequestSimplify))
1611  {
1613 
1614  ret = TemporalSimplify(MAX_TIME_PRECISION, (Node *) req->fcall);
1615  }
1616 
1617  PG_RETURN_POINTER(ret);
1618 }
Node * TemporalSimplify(int32 max_precis, Node *node)
Definition: datetime.c:4840
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define IsA(nodeptr, _type_)
Definition: nodes.h:158

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

◆ time_timetz()

Datum time_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2828 of file date.c.

2829 {
2830  TimeADT time = PG_GETARG_TIMEADT(0);
2831  TimeTzADT *result;
2832  struct pg_tm tt,
2833  *tm = &tt;
2834  fsec_t fsec;
2835  int tz;
2836 
2838  time2tm(time, tm, &fsec);
2840 
2841  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2842 
2843  result->time = time;
2844  result->zone = tz;
2845 
2846  PG_RETURN_TIMETZADT_P(result);
2847 }
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:95

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

Referenced by castTimeToTimeTz(), and executeDateTimeMethod().

◆ timestamp_cmp_date()

Datum timestamp_cmp_date ( PG_FUNCTION_ARGS  )

Definition at line 961 of file date.c.

962 {
964  DateADT dateVal = PG_GETARG_DATEADT(1);
965 
967 }

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

◆ timestamp_date()

Datum timestamp_date ( PG_FUNCTION_ARGS  )

Definition at line 1297 of file date.c.

1298 {
1300  DateADT result;
1301  struct pg_tm tt,
1302  *tm = &tt;
1303  fsec_t fsec;
1304 
1306  DATE_NOBEGIN(result);
1307  else if (TIMESTAMP_IS_NOEND(timestamp))
1308  DATE_NOEND(result);
1309  else
1310  {
1311  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1312  ereport(ERROR,
1313  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1314  errmsg("timestamp out of range")));
1315 
1317  }
1318 
1319  PG_RETURN_DATEADT(result);
1320 }
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1901
int64 timestamp

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.

Referenced by executeDateTimeMethod().

◆ timestamp_eq_date()

Datum timestamp_eq_date ( PG_FUNCTION_ARGS  )

Definition at line 907 of file date.c.

908 {
910  DateADT dateVal = PG_GETARG_DATEADT(1);
911 
912  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) == 0);
913 }

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

◆ timestamp_ge_date()

Datum timestamp_ge_date ( PG_FUNCTION_ARGS  )

Definition at line 952 of file date.c.

953 {
955  DateADT dateVal = PG_GETARG_DATEADT(1);
956 
957  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) <= 0);
958 }

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

◆ timestamp_gt_date()

Datum timestamp_gt_date ( PG_FUNCTION_ARGS  )

Definition at line 934 of file date.c.

935 {
937  DateADT dateVal = PG_GETARG_DATEADT(1);
938 
939  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) < 0);
940 }

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

◆ timestamp_le_date()

Datum timestamp_le_date ( PG_FUNCTION_ARGS  )

Definition at line 943 of file date.c.

944 {
946  DateADT dateVal = PG_GETARG_DATEADT(1);
947 
948  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) >= 0);
949 }

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

◆ timestamp_lt_date()

Datum timestamp_lt_date ( PG_FUNCTION_ARGS  )

Definition at line 925 of file date.c.

926 {
928  DateADT dateVal = PG_GETARG_DATEADT(1);
929 
930  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) > 0);
931 }

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

◆ timestamp_ne_date()

Datum timestamp_ne_date ( PG_FUNCTION_ARGS  )

Definition at line 916 of file date.c.

917 {
919  DateADT dateVal = PG_GETARG_DATEADT(1);
920 
921  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) != 0);
922 }

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

◆ timestamp_time()

Datum timestamp_time ( PG_FUNCTION_ARGS  )

Definition at line 1905 of file date.c.

1906 {
1908  TimeADT result;
1909  struct pg_tm tt,
1910  *tm = &tt;
1911  fsec_t fsec;
1912 
1914  PG_RETURN_NULL();
1915 
1916  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1917  ereport(ERROR,
1918  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1919  errmsg("timestamp out of range")));
1920 
1921  /*
1922  * Could also do this with time = (timestamp / USECS_PER_DAY *
1923  * USECS_PER_DAY) - timestamp;
1924  */
1925  result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1926  USECS_PER_SEC) + fsec;
1927 
1928  PG_RETURN_TIMEADT(result);
1929 }

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.

Referenced by executeDateTimeMethod().

◆ timestamptz_cmp_date()

Datum timestamptz_cmp_date ( PG_FUNCTION_ARGS  )

Definition at line 1024 of file date.c.

1025 {
1027  DateADT dateVal = PG_GETARG_DATEADT(1);
1028 
1030 }

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

◆ timestamptz_date()

Datum timestamptz_date ( PG_FUNCTION_ARGS  )

Definition at line 1342 of file date.c.

1343 {
1345  DateADT result;
1346  struct pg_tm tt,
1347  *tm = &tt;
1348  fsec_t fsec;
1349  int tz;
1350 
1352  DATE_NOBEGIN(result);
1353  else if (TIMESTAMP_IS_NOEND(timestamp))
1354  DATE_NOEND(result);
1355  else
1356  {
1357  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1358  ereport(ERROR,
1359  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1360  errmsg("timestamp out of range")));
1361 
1363  }
1364 
1365  PG_RETURN_DATEADT(result);
1366 }

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.

Referenced by executeDateTimeMethod().

◆ timestamptz_eq_date()

Datum timestamptz_eq_date ( PG_FUNCTION_ARGS  )

Definition at line 970 of file date.c.

971 {
973  DateADT dateVal = PG_GETARG_DATEADT(1);
974 
975  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) == 0);
976 }

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

◆ timestamptz_ge_date()

Datum timestamptz_ge_date ( PG_FUNCTION_ARGS  )

Definition at line 1015 of file date.c.

1016 {
1018  DateADT dateVal = PG_GETARG_DATEADT(1);
1019 
1020  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) <= 0);
1021 }

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

◆ timestamptz_gt_date()

Datum timestamptz_gt_date ( PG_FUNCTION_ARGS  )

Definition at line 997 of file date.c.

998 {
1000  DateADT dateVal = PG_GETARG_DATEADT(1);
1001 
1002  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) < 0);
1003 }

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

◆ timestamptz_le_date()

Datum timestamptz_le_date ( PG_FUNCTION_ARGS  )

Definition at line 1006 of file date.c.

1007 {
1009  DateADT dateVal = PG_GETARG_DATEADT(1);
1010 
1011  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) >= 0);
1012 }

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

◆ timestamptz_lt_date()

Datum timestamptz_lt_date ( PG_FUNCTION_ARGS  )

Definition at line 988 of file date.c.

989 {
991  DateADT dateVal = PG_GETARG_DATEADT(1);
992 
994 }

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

◆ timestamptz_ne_date()

Datum timestamptz_ne_date ( PG_FUNCTION_ARGS  )

Definition at line 979 of file date.c.

980 {
982  DateADT dateVal = PG_GETARG_DATEADT(1);
983 
984  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) != 0);
985 }

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

◆ timestamptz_time()

Datum timestamptz_time ( PG_FUNCTION_ARGS  )

Definition at line 1935 of file date.c.

1936 {
1938  TimeADT result;
1939  struct pg_tm tt,
1940  *tm = &tt;
1941  int tz;
1942  fsec_t fsec;
1943 
1945  PG_RETURN_NULL();
1946 
1947  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1948  ereport(ERROR,
1949  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1950  errmsg("timestamp out of range")));
1951 
1952  /*
1953  * Could also do this with time = (timestamp / USECS_PER_DAY *
1954  * USECS_PER_DAY) - timestamp;
1955  */
1956  result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1957  USECS_PER_SEC) + fsec;
1958 
1959  PG_RETURN_TIMEADT(result);
1960 }

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.

Referenced by executeDateTimeMethod().

◆ timestamptz_timetz()

Datum timestamptz_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2854 of file date.c.

2855 {
2857  TimeTzADT *result;
2858  struct pg_tm tt,
2859  *tm = &tt;
2860  int tz;
2861  fsec_t fsec;
2862 
2864  PG_RETURN_NULL();
2865 
2866  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
2867  ereport(ERROR,
2868  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2869  errmsg("timestamp out of range")));
2870 
2871  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2872 
2873  tm2timetz(tm, fsec, tz, result);
2874 
2875  PG_RETURN_TIMETZADT_P(result);
2876 }

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

Referenced by executeDateTimeMethod().

◆ timetypmodin()

Datum timetypmodin ( PG_FUNCTION_ARGS  )

Definition at line 1558 of file date.c.

1559 {
1561 
1562  PG_RETURN_INT32(anytime_typmodin(false, ta));
1563 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
static int32 anytime_typmodin(bool istz, ArrayType *ta)
Definition: date.c:50

References anytime_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timetypmodout()

Datum timetypmodout ( PG_FUNCTION_ARGS  )

Definition at line 1566 of file date.c.

1567 {
1568  int32 typmod = PG_GETARG_INT32(0);
1569 
1570  PG_RETURN_CSTRING(anytime_typmodout(false, typmod));
1571 }
static char * anytime_typmodout(bool istz, int32 typmod)
Definition: date.c:93

References anytime_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

◆ timetz2tm()

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

Definition at line 2403 of file date.c.

2404 {
2405  TimeOffset trem = time->time;
2406 
2407  tm->tm_hour = trem / USECS_PER_HOUR;
2408  trem -= tm->tm_hour * USECS_PER_HOUR;
2409  tm->tm_min = trem / USECS_PER_MINUTE;
2410  trem -= tm->tm_min * USECS_PER_MINUTE;
2411  tm->tm_sec = trem / USECS_PER_SEC;
2412  *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2413 
2414  if (tzp != NULL)
2415  *tzp = time->zone;
2416 
2417  return 0;
2418 }
int64 TimeOffset
Definition: timestamp.h:40

References TimeTzADT::time, tm, 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().

◆ timetz_at_local()

Datum timetz_at_local ( PG_FUNCTION_ARGS  )

Definition at line 3165 of file date.c.

3166 {
3167  Datum time = PG_GETARG_DATUM(0);
3168  const char *tzn = pg_get_timezone_name(session_timezone);
3170 
3171  return DirectFunctionCall2(timetz_zone, zone, time);
3172 }
Datum timetz_zone(PG_FUNCTION_ARGS)
Definition: date.c:3060
const char * pg_get_timezone_name(pg_tz *tz)
Definition: localtime.c:1875
Definition: zic.c:94
text * cstring_to_text(const char *s)
Definition: varlena.c:184

References cstring_to_text(), DirectFunctionCall2, pg_get_timezone_name(), PG_GETARG_DATUM, PointerGetDatum(), session_timezone, and timetz_zone().

◆ timetz_cmp()

Datum timetz_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2524 of file date.c.

2525 {
2526  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2527  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2528 
2529  PG_RETURN_INT32(timetz_cmp_internal(time1, time2));
2530 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_INT32, and timetz_cmp_internal().

Referenced by compareDatetime().

◆ timetz_cmp_internal()

static int timetz_cmp_internal ( TimeTzADT time1,
TimeTzADT time2 
)
static

Definition at line 2443 of file date.c.

2444 {
2445  TimeOffset t1,
2446  t2;
2447 
2448  /* Primary sort is by true (GMT-equivalent) time */
2449  t1 = time1->time + (time1->zone * USECS_PER_SEC);
2450  t2 = time2->time + (time2->zone * USECS_PER_SEC);
2451 
2452  if (t1 > t2)
2453  return 1;
2454  if (t1 < t2)
2455  return -1;
2456 
2457  /*
2458  * If same GMT time, sort by timezone; we only want to say that two
2459  * timetz's are equal if both the time and zone parts are equal.
2460  */
2461  if (time1->zone > time2->zone)
2462  return 1;
2463  if (time1->zone < time2->zone)
2464  return -1;
2465 
2466  return 0;
2467 }

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

◆ timetz_eq()

Datum timetz_eq ( PG_FUNCTION_ARGS  )

Definition at line 2470 of file date.c.

2471 {
2472  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2473  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2474 
2475  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) == 0);
2476 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_ge()

Datum timetz_ge ( PG_FUNCTION_ARGS  )

Definition at line 2515 of file date.c.

2516 {
2517  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2518  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2519 
2520  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) >= 0);
2521 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_gt()

Datum timetz_gt ( PG_FUNCTION_ARGS  )

Definition at line 2506 of file date.c.

2507 {
2508  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2509  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2510 
2511  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) > 0);
2512 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_hash()

Datum timetz_hash ( PG_FUNCTION_ARGS  )

Definition at line 2533 of file date.c.

2534 {
2536  uint32 thash;
2537 
2538  /*
2539  * To avoid any problems with padding bytes in the struct, we figure the
2540  * field hashes separately and XOR them.
2541  */
2543  Int64GetDatumFast(key->time)));
2544  thash ^= DatumGetUInt32(hash_uint32(key->zone));
2545  PG_RETURN_UINT32(thash);
2546 }
unsigned int uint32
Definition: c.h:506
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:355
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
static Datum hash_uint32(uint32 k)
Definition: hashfn.h:43
static uint32 DatumGetUInt32(Datum X)
Definition: postgres.h:222
#define Int64GetDatumFast(X)
Definition: postgres.h:554

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

◆ timetz_hash_extended()

Datum timetz_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2549 of file date.c.

2550 {
2552  Datum seed = PG_GETARG_DATUM(1);
2553  uint64 thash;
2554 
2555  /* Same approach as timetz_hash */
2557  Int64GetDatumFast(key->time),
2558  seed));
2559  thash ^= DatumGetUInt64(hash_uint32_extended(key->zone,
2560  DatumGetInt64(seed)));
2561  PG_RETURN_UINT64(thash);
2562 }
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:369
static Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfn.h:49
static uint64 DatumGetUInt64(Datum X)
Definition: postgres.h:419
static int64 DatumGetInt64(Datum X)
Definition: postgres.h:385

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

◆ timetz_in()

Datum timetz_in ( PG_FUNCTION_ARGS  )

Definition at line 2273 of file date.c.

2274 {
2275  char *str = PG_GETARG_CSTRING(0);
2276 #ifdef NOT_USED
2277  Oid typelem = PG_GETARG_OID(1);
2278 #endif
2279  int32 typmod = PG_GETARG_INT32(2);
2280  Node *escontext = fcinfo->context;
2281  TimeTzADT *result;
2282  fsec_t fsec;
2283  struct pg_tm tt,
2284  *tm = &tt;
2285  int tz;
2286  int nf;
2287  int dterr;
2288  char workbuf[MAXDATELEN + 1];
2289  char *field[MAXDATEFIELDS];
2290  int dtype;
2291  int ftype[MAXDATEFIELDS];
2292  DateTimeErrorExtra extra;
2293 
2294  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
2295  field, ftype, MAXDATEFIELDS, &nf);
2296  if (dterr == 0)
2297  dterr = DecodeTimeOnly(field, ftype, nf,
2298  &dtype, tm, &fsec, &tz, &extra);
2299  if (dterr != 0)
2300  {
2301  DateTimeParseError(dterr, &extra, str, "time with time zone",
2302  escontext);
2303  PG_RETURN_NULL();
2304  }
2305 
2306  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2307  tm2timetz(tm, fsec, tz, result);
2308  AdjustTimeForTypmod(&(result->time), typmod);
2309 
2310  PG_RETURN_TIMETZADT_P(result);
2311 }

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

◆ timetz_izone()

Datum timetz_izone ( PG_FUNCTION_ARGS  )

Definition at line 3122 of file date.c.

3123 {
3125  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
3126  TimeTzADT *result;
3127  int tz;
3128 
3130  ereport(ERROR,
3131  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3132  errmsg("interval time zone \"%s\" must be finite",
3134  PointerGetDatum(zone))))));
3135 
3136  if (zone->month != 0 || zone->day != 0)
3137  ereport(ERROR,
3138  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3139  errmsg("interval time zone \"%s\" must not include months or days",
3141  PointerGetDatum(zone))))));
3142 
3143  tz = -(zone->time / USECS_PER_SEC);
3144 
3145  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
3146 
3147  result->time = time->time + (time->zone - tz) * USECS_PER_SEC;
3148  /* C99 modulo has the wrong sign convention for negative input */
3149  while (result->time < INT64CONST(0))
3150  result->time += USECS_PER_DAY;
3151  if (result->time >= USECS_PER_DAY)
3152  result->time %= USECS_PER_DAY;
3153 
3154  result->zone = tz;
3155 
3156  PG_RETURN_TIMETZADT_P(result);
3157 }
Datum interval_out(PG_FUNCTION_ARGS)
Definition: timestamp.c:982
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
static char * DatumGetCString(Datum X)
Definition: postgres.h:335

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

◆ timetz_larger()

Datum timetz_larger ( PG_FUNCTION_ARGS  )

Definition at line 2565 of file date.c.

2566 {
2567  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2568  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2569  TimeTzADT *result;
2570 
2571  if (timetz_cmp_internal(time1, time2) > 0)
2572  result = time1;
2573  else
2574  result = time2;
2575  PG_RETURN_TIMETZADT_P(result);
2576 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

◆ timetz_le()

Datum timetz_le ( PG_FUNCTION_ARGS  )

Definition at line 2497 of file date.c.

2498 {
2499  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2500  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2501 
2502  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) <= 0);
2503 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_lt()

Datum timetz_lt ( PG_FUNCTION_ARGS  )

Definition at line 2488 of file date.c.

2489 {
2490  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2491  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2492 
2493  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) < 0);
2494 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_mi_interval()

Datum timetz_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 2623 of file date.c.

2624 {
2625  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2626  Interval *span = PG_GETARG_INTERVAL_P(1);
2627  TimeTzADT *result;
2628 
2629  if (INTERVAL_NOT_FINITE(span))
2630  ereport(ERROR,
2631  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2632  errmsg("cannot subtract infinite interval from time")));
2633 
2634  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2635 
2636  result->time = time->time - span->time;
2637  result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2638  if (result->time < INT64CONST(0))
2639  result->time += USECS_PER_DAY;
2640 
2641  result->zone = time->zone;
2642 
2643  PG_RETURN_TIMETZADT_P(result);
2644 }

References ereport, errcode(), errmsg(), ERROR, INTERVAL_NOT_FINITE, palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, Interval::time, TimeTzADT::time, USECS_PER_DAY, and TimeTzADT::zone.

◆ timetz_ne()

Datum timetz_ne ( PG_FUNCTION_ARGS  )

Definition at line 2479 of file date.c.

2480 {
2481  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2482  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2483 
2484  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) != 0);
2485 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_out()

Datum timetz_out ( PG_FUNCTION_ARGS  )

Definition at line 2314 of file date.c.

2315 {
2316  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2317  char *result;
2318  struct pg_tm tt,
2319  *tm = &tt;
2320  fsec_t fsec;
2321  int tz;
2322  char buf[MAXDATELEN + 1];
2323 
2324  timetz2tm(time, tm, &fsec, &tz);
2325  EncodeTimeOnly(tm, fsec, true, tz, DateStyle, buf);
2326 
2327  result = pstrdup(buf);
2328  PG_RETURN_CSTRING(result);
2329 }
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: date.c:2403

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

Referenced by ExecGetJsonValueItemString(), and executeItemOptUnwrapTarget().

◆ timetz_part()

Datum timetz_part ( PG_FUNCTION_ARGS  )

Definition at line 3044 of file date.c.

3045 {
3046  return timetz_part_common(fcinfo, false);
3047 }

References timetz_part_common().

◆ timetz_part_common()

static Datum timetz_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2927 of file date.c.

2928 {
2929  text *units = PG_GETARG_TEXT_PP(0);
2930  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2931  int64 intresult;
2932  int type,
2933  val;
2934  char *lowunits;
2935 
2936  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
2937  VARSIZE_ANY_EXHDR(units),
2938  false);
2939 
2940  type = DecodeUnits(0, lowunits, &val);
2941  if (type == UNKNOWN_FIELD)
2942  type = DecodeSpecial(0, lowunits, &val);
2943 
2944  if (type == UNITS)
2945  {
2946  int tz;
2947  fsec_t fsec;
2948  struct pg_tm tt,
2949  *tm = &tt;
2950 
2951  timetz2tm(time, tm, &fsec, &tz);
2952 
2953  switch (val)
2954  {
2955  case DTK_TZ:
2956  intresult = -tz;
2957  break;
2958 
2959  case DTK_TZ_MINUTE:
2960  intresult = (-tz / SECS_PER_MINUTE) % MINS_PER_HOUR;
2961  break;
2962 
2963  case DTK_TZ_HOUR:
2964  intresult = -tz / SECS_PER_HOUR;
2965  break;
2966 
2967  case DTK_MICROSEC:
2968  intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
2969  break;
2970 
2971  case DTK_MILLISEC:
2972  if (retnumeric)
2973  /*---
2974  * tm->tm_sec * 1000 + fsec / 1000
2975  * = (tm->tm_sec * 1'000'000 + fsec) / 1000
2976  */
2977  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
2978  else
2979  PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
2980  break;
2981 
2982  case DTK_SECOND:
2983  if (retnumeric)
2984  /*---
2985  * tm->tm_sec + fsec / 1'000'000
2986  * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
2987  */
2988  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
2989  else
2990  PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
2991  break;
2992 
2993  case DTK_MINUTE:
2994  intresult = tm->tm_min;
2995  break;
2996 
2997  case DTK_HOUR:
2998  intresult = tm->tm_hour;
2999  break;
3000 
3001  case DTK_DAY:
3002  case DTK_MONTH:
3003  case DTK_QUARTER:
3004  case DTK_YEAR:
3005  case DTK_DECADE:
3006  case DTK_CENTURY:
3007  case DTK_MILLENNIUM:
3008  default:
3009  ereport(ERROR,
3010  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3011  errmsg("unit \"%s\" not supported for type %s",
3012  lowunits, format_type_be(TIMETZOID))));
3013  intresult = 0;
3014  }
3015  }
3016  else if (type == RESERV && val == DTK_EPOCH)
3017  {
3018  if (retnumeric)
3019  /*---
3020  * time->time / 1'000'000 + time->zone
3021  * = (time->time + time->zone * 1'000'000) / 1'000'000
3022  */
3023  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(time->time + time->zone * INT64CONST(1000000), 6));
3024  else
3025  PG_RETURN_FLOAT8(time->time / 1000000.0 + time->zone);
3026  }
3027  else
3028  {
3029  ereport(ERROR,
3030  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3031  errmsg("unit \"%s\" not recognized for type %s",
3032  lowunits, format_type_be(TIMETZOID))));
3033  intresult = 0;
3034  }
3035 
3036  if (retnumeric)
3037  PG_RETURN_NUMERIC(int64_to_numeric(intresult));
3038  else
3039  PG_RETURN_FLOAT8(intresult);
3040 }
#define SECS_PER_HOUR
Definition: timestamp.h:127

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, format_type_be(), 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, type, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY, VARSIZE_ANY_EXHDR, and TimeTzADT::zone.

Referenced by extract_timetz(), and timetz_part().

◆ timetz_pl_interval()

Datum timetz_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 2596 of file date.c.

2597 {
2598  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2599  Interval *span = PG_GETARG_INTERVAL_P(1);
2600  TimeTzADT *result;
2601 
2602  if (INTERVAL_NOT_FINITE(span))
2603  ereport(ERROR,
2604  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2605  errmsg("cannot add infinite interval to time")));
2606 
2607  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2608 
2609  result->time = time->time + span->time;
2610  result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2611  if (result->time < INT64CONST(0))
2612  result->time += USECS_PER_DAY;
2613 
2614  result->zone = time->zone;
2615 
2616  PG_RETURN_TIMETZADT_P(result);
2617 }

References ereport, errcode(), errmsg(), ERROR, INTERVAL_NOT_FINITE, palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, Interval::time, TimeTzADT::time, USECS_PER_DAY, and TimeTzADT::zone.

◆ timetz_recv()

Datum timetz_recv ( PG_FUNCTION_ARGS  )

Definition at line 2335 of file date.c.

2336 {
2338 
2339 #ifdef NOT_USED
2340  Oid typelem = PG_GETARG_OID(1);
2341 #endif
2342  int32 typmod = PG_GETARG_INT32(2);
2343  TimeTzADT *result;
2344 
2345  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2346 
2347  result->time = pq_getmsgint64(buf);
2348 
2349  if (result->time < INT64CONST(0) || result->time > USECS_PER_DAY)
2350  ereport(ERROR,
2351  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2352  errmsg("time out of range")));
2353 
2354  result->zone = pq_getmsgint(buf, sizeof(result->zone));
2355 
2356  /* Check for sane GMT displacement; see notes in datatype/timestamp.h */
2357  if (result->zone <= -TZDISP_LIMIT || result->zone >= TZDISP_LIMIT)
2358  ereport(ERROR,
2359  (errcode(ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE),
2360  errmsg("time zone displacement out of range")));
2361 
2362  AdjustTimeForTypmod(&(result->time), typmod);
2363 
2364  PG_RETURN_TIMETZADT_P(result);
2365 }
#define TZDISP_LIMIT
Definition: timestamp.h:144

References AdjustTimeForTypmod(), buf, ereport, errcode(), errmsg(), ERROR, palloc(), PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_TIMETZADT_P, pq_getmsgint(), pq_getmsgint64(), TimeTzADT::time, TZDISP_LIMIT, USECS_PER_DAY, and TimeTzADT::zone.

◆ timetz_scale()

Datum timetz_scale ( PG_FUNCTION_ARGS  )

Definition at line 2425 of file date.c.

2426 {
2427  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2428  int32 typmod = PG_GETARG_INT32(1);
2429  TimeTzADT *result;
2430 
2431  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2432 
2433  result->time = time->time;
2434  result->zone = time->zone;
2435 
2436  AdjustTimeForTypmod(&(result->time), typmod);
2437 
2438  PG_RETURN_TIMETZADT_P(result);
2439 }

References AdjustTimeForTypmod(), palloc(), PG_GETARG_INT32, PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, TimeTzADT::time, and TimeTzADT::zone.

◆ timetz_send()

Datum timetz_send ( PG_FUNCTION_ARGS  )

◆ timetz_smaller()

Datum timetz_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2579 of file date.c.

2580 {
2581  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2582  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2583  TimeTzADT *result;
2584 
2585  if (timetz_cmp_internal(time1, time2) < 0)
2586  result = time1;
2587  else
2588  result = time2;
2589  PG_RETURN_TIMETZADT_P(result);
2590 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

◆ timetz_time()

Datum timetz_time ( PG_FUNCTION_ARGS  )

Definition at line 2815 of file date.c.

2816 {
2817  TimeTzADT *timetz = PG_GETARG_TIMETZADT_P(0);
2818  TimeADT result;
2819 
2820  /* swallow the time zone and just return the time */
2821  result = timetz->time;
2822 
2823  PG_RETURN_TIMEADT(result);
2824 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMEADT, and TimeTzADT::time.

Referenced by executeDateTimeMethod().

◆ timetz_zone()

Datum timetz_zone ( PG_FUNCTION_ARGS  )

Definition at line 3060 of file date.c.

3061 {
3062  text *zone = PG_GETARG_TEXT_PP(0);
3064  TimeTzADT *result;
3065  int tz;
3066  char tzname[TZ_STRLEN_MAX + 1];
3067  int type,
3068  val;
3069  pg_tz *tzp;
3070 
3071  /*
3072  * Look up the requested timezone.
3073  */
3074  text_to_cstring_buffer(zone, tzname, sizeof(tzname));
3075 
3076  type = DecodeTimezoneName(tzname, &val, &tzp);
3077 
3078  if (type == TZNAME_FIXED_OFFSET)
3079  {
3080  /* fixed-offset abbreviation */
3081  tz = -val;
3082  }
3083  else if (type == TZNAME_DYNTZ)
3084  {
3085  /* dynamic-offset abbreviation, resolve using transaction start time */
3087  int isdst;
3088 
3089  tz = DetermineTimeZoneAbbrevOffsetTS(now, tzname, tzp, &isdst);
3090  }
3091  else
3092  {
3093  /* Get the offset-from-GMT that is valid now for the zone name */
3095  struct pg_tm tm;
3096  fsec_t fsec;
3097 
3098  if (timestamp2tm(now, &tz, &tm, &fsec, NULL, tzp) != 0)
3099  ereport(ERROR,
3100  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3101  errmsg("timestamp out of range")));
3102  }
3103 
3104  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
3105 
3106  result->time = t->time + (t->zone - tz) * USECS_PER_SEC;
3107  /* C99 modulo has the wrong sign convention for negative input */
3108  while (result->time < INT64CONST(0))
3109  result->time += USECS_PER_DAY;
3110  if (result->time >= USECS_PER_DAY)
3111  result->time %= USECS_PER_DAY;
3112 
3113  result->zone = tz;
3114 
3115  PG_RETURN_TIMETZADT_P(result);
3116 }
int DetermineTimeZoneAbbrevOffsetTS(TimestampTz ts, const char *abbr, pg_tz *tzp, int *isdst)
Definition: datetime.c:1784
int DecodeTimezoneName(const char *tzname, int *offset, pg_tz **tz)
Definition: datetime.c:3190
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1618
#define TZNAME_FIXED_OFFSET
Definition: datetime.h:299
#define TZNAME_DYNTZ
Definition: datetime.h:300
#define TZ_STRLEN_MAX
Definition: pgtime.h:54
Definition: pgtz.h:66
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition: varlena.c:248
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:867

References DecodeTimezoneName(), DetermineTimeZoneAbbrevOffsetTS(), ereport, errcode(), errmsg(), ERROR, GetCurrentTransactionStartTimestamp(), if(), now(), palloc(), PG_GETARG_TEXT_PP, PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, text_to_cstring_buffer(), TimeTzADT::time, timestamp2tm(), tm, type, TZ_STRLEN_MAX, TZNAME_DYNTZ, TZNAME_FIXED_OFFSET, USECS_PER_DAY, USECS_PER_SEC, val, and TimeTzADT::zone.

Referenced by timetz_at_local().

◆ timetztypmodin()

Datum timetztypmodin ( PG_FUNCTION_ARGS  )

Definition at line 2383 of file date.c.

2384 {
2386 
2387  PG_RETURN_INT32(anytime_typmodin(true, ta));
2388 }

References anytime_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timetztypmodout()

Datum timetztypmodout ( PG_FUNCTION_ARGS  )

Definition at line 2391 of file date.c.

2392 {
2393  int32 typmod = PG_GETARG_INT32(0);
2394 
2395  PG_RETURN_CSTRING(anytime_typmodout(true, typmod));
2396 }

References anytime_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

◆ tm2time()

int tm2time ( struct pg_tm tm,
fsec_t  fsec,
TimeADT result 
)

Definition at line 1416 of file date.c.

1417 {
1418  *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1419  * USECS_PER_SEC) + fsec;
1420  return 0;
1421 }

References MINS_PER_HOUR, SECS_PER_MINUTE, tm, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, and USECS_PER_SEC.

Referenced by GetSQLLocalTime(), parse_datetime(), and time_in().

◆ tm2timetz()

int tm2timetz ( struct pg_tm tm,
fsec_t  fsec,
int  tz,
TimeTzADT result 
)

Definition at line 2263 of file date.c.

2264 {
2265  result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2266  USECS_PER_SEC) + fsec;
2267  result->zone = tz;
2268 
2269  return 0;
2270 }

References MINS_PER_HOUR, SECS_PER_MINUTE, TimeTzADT::time, tm, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, USECS_PER_SEC, and TimeTzADT::zone.

Referenced by GetSQLCurrentTime(), parse_datetime(), timestamptz_timetz(), and timetz_in().