PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 hashdate (PG_FUNCTION_ARGS)
 
Datum hashdateextended (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 1657 of file date.c.

1658 {
1659  static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1660  INT64CONST(1000000),
1661  INT64CONST(100000),
1662  INT64CONST(10000),
1663  INT64CONST(1000),
1664  INT64CONST(100),
1665  INT64CONST(10),
1666  INT64CONST(1)
1667  };
1668 
1669  static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1670  INT64CONST(500000),
1671  INT64CONST(50000),
1672  INT64CONST(5000),
1673  INT64CONST(500),
1674  INT64CONST(50),
1675  INT64CONST(5),
1676  INT64CONST(0)
1677  };
1678 
1679  if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1680  {
1681  if (*time >= INT64CONST(0))
1682  *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1683  TimeScales[typmod];
1684  else
1685  *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1686  TimeScales[typmod]);
1687  }
1688 }
#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:853
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:508
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:1696
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43

References psprintf(), and pstrdup().

Referenced by timetypmodout(), and timetztypmodout().

◆ date2timestamp()

static TimestampTz date2timestamp ( DateADT  dateVal)
static

Definition at line 620 of file date.c.

621 {
622  return date2timestamp_opt_overflow(dateVal, NULL);
623 }
Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:576

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

733 {
734  double result;
735 
736  if (DATE_IS_NOBEGIN(dateVal))
737  result = -DBL_MAX;
738  else if (DATE_IS_NOEND(dateVal))
739  result = DBL_MAX;
740  else
741  {
742  /* date is days since 2000, timestamp is microseconds since same... */
743  result = dateVal * (double) USECS_PER_DAY;
744  }
745 
746  return result;
747 }
#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 576 of file date.c.

577 {
578  Timestamp result;
579 
580  if (overflow)
581  *overflow = 0;
582 
583  if (DATE_IS_NOBEGIN(dateVal))
584  TIMESTAMP_NOBEGIN(result);
585  else if (DATE_IS_NOEND(dateVal))
586  TIMESTAMP_NOEND(result);
587  else
588  {
589  /*
590  * Since dates have the same minimum values as timestamps, only upper
591  * boundary need be checked for overflow.
592  */
593  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
594  {
595  if (overflow)
596  {
597  *overflow = 1;
598  TIMESTAMP_NOEND(result);
599  return result;
600  }
601  else
602  {
603  ereport(ERROR,
604  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
605  errmsg("date out of range for timestamp")));
606  }
607  }
608 
609  /* date is days since 2000, timestamp is microseconds since same... */
610  result = dateVal * USECS_PER_DAY;
611  }
612 
613  return result;
614 }
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 716 of file date.c.

717 {
718  return date2timestamptz_opt_overflow(dateVal, NULL);
719 }
TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:636

References date2timestamptz_opt_overflow().

Referenced by date_timestamptz().

◆ date2timestamptz_opt_overflow()

TimestampTz date2timestamptz_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 636 of file date.c.

637 {
638  TimestampTz result;
639  struct pg_tm tt,
640  *tm = &tt;
641  int tz;
642 
643  if (overflow)
644  *overflow = 0;
645 
646  if (DATE_IS_NOBEGIN(dateVal))
647  TIMESTAMP_NOBEGIN(result);
648  else if (DATE_IS_NOEND(dateVal))
649  TIMESTAMP_NOEND(result);
650  else
651  {
652  /*
653  * Since dates have the same minimum values as timestamps, only upper
654  * boundary need be checked for overflow.
655  */
656  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
657  {
658  if (overflow)
659  {
660  *overflow = 1;
661  TIMESTAMP_NOEND(result);
662  return result;
663  }
664  else
665  {
666  ereport(ERROR,
667  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
668  errmsg("date out of range for timestamp")));
669  }
670  }
671 
672  j2date(dateVal + POSTGRES_EPOCH_JDATE,
673  &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
674  tm->tm_hour = 0;
675  tm->tm_min = 0;
676  tm->tm_sec = 0;
678 
679  result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
680 
681  /*
682  * Since it is possible to go beyond allowed timestamptz range because
683  * of time zone, check for allowed timestamp range after adding tz.
684  */
685  if (!IS_VALID_TIMESTAMP(result))
686  {
687  if (overflow)
688  {
689  if (result < MIN_TIMESTAMP)
690  {
691  *overflow = -1;
692  TIMESTAMP_NOBEGIN(result);
693  }
694  else
695  {
696  *overflow = 1;
697  TIMESTAMP_NOEND(result);
698  }
699  }
700  else
701  {
702  ereport(ERROR,
703  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
704  errmsg("date out of range for timestamp")));
705  }
706  }
707  }
708 
709  return result;
710 }
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 826 of file date.c.

827 {
828  DateADT dateVal = PG_GETARG_DATEADT(0);
830 
832 }
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:755
#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 755 of file date.c.

756 {
757  Timestamp dt1;
758  int overflow;
759 
760  dt1 = date2timestamp_opt_overflow(dateVal, &overflow);
761  if (overflow > 0)
762  {
763  /* dt1 is larger than any finite timestamp, but less than infinity */
764  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
765  }
766  Assert(overflow == 0); /* -1 case cannot occur */
767 
768  return timestamp_cmp_internal(dt1, dt2);
769 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2191
#define Assert(condition)
Definition: c.h:863
#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 910 of file date.c.

911 {
912  DateADT dateVal = PG_GETARG_DATEADT(0);
914 
916 }
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:835
#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 835 of file date.c.

836 {
837  TimestampTz dt1;
838  int overflow;
839 
840  dt1 = date2timestamptz_opt_overflow(dateVal, &overflow);
841  if (overflow > 0)
842  {
843  /* dt1 is larger than any finite timestamp, but less than infinity */
844  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
845  }
846  if (overflow < 0)
847  {
848  /* dt1 is less than any finite timestamp, but more than -infinity */
849  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
850  }
851 
852  return timestamptz_cmp_internal(dt1, dt2);
853 }
#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 772 of file date.c.

773 {
774  DateADT dateVal = PG_GETARG_DATEADT(0);
776 
777  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) == 0);
778 }

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

857 {
858  DateADT dateVal = PG_GETARG_DATEADT(0);
860 
861  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) == 0);
862 }

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

472 {
474 
476 }
#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 817 of file date.c.

818 {
819  DateADT dateVal = PG_GETARG_DATEADT(0);
821 
822  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) >= 0);
823 }

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

902 {
903  DateADT dateVal = PG_GETARG_DATEADT(0);
905 
906  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) >= 0);
907 }

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

800 {
801  DateADT dateVal = PG_GETARG_DATEADT(0);
803 
804  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) > 0);
805 }

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

884 {
885  DateADT dateVal = PG_GETARG_DATEADT(0);
887 
889 }

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:2149
#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:277
#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 479 of file date.c.

480 {
481  DateADT dateVal1 = PG_GETARG_DATEADT(0);
482  DateADT dateVal2 = PG_GETARG_DATEADT(1);
483 
484  PG_RETURN_DATEADT((dateVal1 > dateVal2) ? dateVal1 : dateVal2);
485 }

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

809 {
810  DateADT dateVal = PG_GETARG_DATEADT(0);
812 
813  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) <= 0);
814 }

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

893 {
894  DateADT dateVal = PG_GETARG_DATEADT(0);
896 
897  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) <= 0);
898 }

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

791 {
792  DateADT dateVal = PG_GETARG_DATEADT(0);
794 
795  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) < 0);
796 }

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

875 {
876  DateADT dateVal = PG_GETARG_DATEADT(0);
878 
880 }

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

500 {
501  DateADT dateVal1 = PG_GETARG_DATEADT(0);
502  DateADT dateVal2 = PG_GETARG_DATEADT(1);
503 
504  if (DATE_NOT_FINITE(dateVal1) || DATE_NOT_FINITE(dateVal2))
505  ereport(ERROR,
506  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
507  errmsg("cannot subtract infinite dates")));
508 
509  PG_RETURN_INT32((int32) (dateVal1 - dateVal2));
510 }

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

1279 {
1280  DateADT dateVal = PG_GETARG_DATEADT(0);
1281  Interval *span = PG_GETARG_INTERVAL_P(1);
1282  Timestamp dateStamp;
1283 
1284  dateStamp = date2timestamp(dateVal);
1285 
1287  TimestampGetDatum(dateStamp),
1288  PointerGetDatum(span));
1289 }
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3159
static TimestampTz date2timestamp(DateADT dateVal)
Definition: date.c:620
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
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 540 of file date.c.

541 {
542  DateADT dateVal = PG_GETARG_DATEADT(0);
544  DateADT result;
545 
546  if (DATE_NOT_FINITE(dateVal))
547  PG_RETURN_DATEADT(dateVal); /* can't change infinity */
548 
549  result = dateVal - days;
550 
551  /* Check for integer overflow and out-of-allowed-range */
552  if ((days >= 0 ? (result > dateVal) : (result < dateVal)) ||
553  !IS_VALID_DATE(result))
554  ereport(ERROR,
555  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
556  errmsg("date out of range")));
557 
558  PG_RETURN_DATEADT(result);
559 }
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 781 of file date.c.

782 {
783  DateADT dateVal = PG_GETARG_DATEADT(0);
785 
786  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) != 0);
787 }

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

866 {
867  DateADT dateVal = PG_GETARG_DATEADT(0);
869 
870  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) != 0);
871 }

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:124
static char * buf
Definition: pg_test_fsync.c:72

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

◆ date_pl_interval()

Datum date_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 1258 of file date.c.

1259 {
1260  DateADT dateVal = PG_GETARG_DATEADT(0);
1261  Interval *span = PG_GETARG_INTERVAL_P(1);
1262  Timestamp dateStamp;
1263 
1264  dateStamp = date2timestamp(dateVal);
1265 
1267  TimestampGetDatum(dateStamp),
1268  PointerGetDatum(span));
1269 }
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3042

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

517 {
518  DateADT dateVal = PG_GETARG_DATEADT(0);
520  DateADT result;
521 
522  if (DATE_NOT_FINITE(dateVal))
523  PG_RETURN_DATEADT(dateVal); /* can't change infinity */
524 
525  result = dateVal + days;
526 
527  /* Check for integer overflow and out-of-allowed-range */
528  if ((days >= 0 ? (result < dateVal) : (result > dateVal)) ||
529  !IS_VALID_DATE(result))
530  ereport(ERROR,
531  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
532  errmsg("date out of range")));
533 
534  PG_RETURN_DATEADT(result);
535 }

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

489 {
490  DateADT dateVal1 = PG_GETARG_DATEADT(0);
491  DateADT dateVal2 = PG_GETARG_DATEADT(1);
492 
493  PG_RETURN_DATEADT((dateVal1 < dateVal2) ? dateVal1 : dateVal2);
494 }

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:3166

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

1296 {
1297  DateADT dateVal = PG_GETARG_DATEADT(0);
1298  Timestamp result;
1299 
1300  result = date2timestamp(dateVal);
1301 
1302  PG_RETURN_TIMESTAMP(result);
1303 }
#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 1339 of file date.c.

1340 {
1341  DateADT dateVal = PG_GETARG_DATEADT(0);
1342  TimestampTz result;
1343 
1344  result = date2timestamptz(dateVal);
1345 
1346  PG_RETURN_TIMESTAMP(result);
1347 }
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:716

References date2timestamptz(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

Referenced by executeDateTimeMethod().

◆ datetime_timestamp()

Datum datetime_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1978 of file date.c.

1979 {
1981  TimeADT time = PG_GETARG_TIMEADT(1);
1982  Timestamp result;
1983 
1984  result = date2timestamp(date);
1985  if (!TIMESTAMP_NOT_FINITE(result))
1986  {
1987  result += time;
1988  if (!IS_VALID_TIMESTAMP(result))
1989  ereport(ERROR,
1990  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1991  errmsg("timestamp out of range")));
1992  }
1993 
1994  PG_RETURN_TIMESTAMP(result);
1995 }
#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 2898 of file date.c.

2899 {
2901  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2902  TimestampTz result;
2903 
2904  if (DATE_IS_NOBEGIN(date))
2905  TIMESTAMP_NOBEGIN(result);
2906  else if (DATE_IS_NOEND(date))
2907  TIMESTAMP_NOEND(result);
2908  else
2909  {
2910  /*
2911  * Date's range is wider than timestamp's, so check for boundaries.
2912  * Since dates have the same minimum values as timestamps, only upper
2913  * boundary need be checked for overflow.
2914  */
2916  ereport(ERROR,
2917  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2918  errmsg("date out of range for timestamp")));
2919  result = date * USECS_PER_DAY + time->time + time->zone * USECS_PER_SEC;
2920 
2921  /*
2922  * Since it is possible to go beyond allowed timestamptz range because
2923  * of time zone, check for allowed timestamp range after adding tz.
2924  */
2925  if (!IS_VALID_TIMESTAMP(result))
2926  ereport(ERROR,
2927  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2928  errmsg("date out of range for timestamp")));
2929  }
2930 
2931  PG_RETURN_TIMESTAMP(result);
2932 }
#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:225
#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 1078 of file date.c.

1079 {
1080  text *units = PG_GETARG_TEXT_PP(0);
1082  int64 intresult;
1083  int type,
1084  val;
1085  char *lowunits;
1086  int year,
1087  mon,
1088  mday;
1089 
1090  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
1091  VARSIZE_ANY_EXHDR(units),
1092  false);
1093 
1094  type = DecodeUnits(0, lowunits, &val);
1095  if (type == UNKNOWN_FIELD)
1096  type = DecodeSpecial(0, lowunits, &val);
1097 
1098  if (DATE_NOT_FINITE(date) && (type == UNITS || type == RESERV))
1099  {
1100  switch (val)
1101  {
1102  /* Oscillating units */
1103  case DTK_DAY:
1104  case DTK_MONTH:
1105  case DTK_QUARTER:
1106  case DTK_WEEK:
1107  case DTK_DOW:
1108  case DTK_ISODOW:
1109  case DTK_DOY:
1110  PG_RETURN_NULL();
1111  break;
1112 
1113  /* Monotonically-increasing units */
1114  case DTK_YEAR:
1115  case DTK_DECADE:
1116  case DTK_CENTURY:
1117  case DTK_MILLENNIUM:
1118  case DTK_JULIAN:
1119  case DTK_ISOYEAR:
1120  case DTK_EPOCH:
1121  if (DATE_IS_NOBEGIN(date))
1123  CStringGetDatum("-Infinity"),
1125  Int32GetDatum(-1))));
1126  else
1128  CStringGetDatum("Infinity"),
1130  Int32GetDatum(-1))));
1131  default:
1132  ereport(ERROR,
1133  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1134  errmsg("unit \"%s\" not supported for type %s",
1135  lowunits, format_type_be(DATEOID))));
1136  }
1137  }
1138  else if (type == UNITS)
1139  {
1140  j2date(date + POSTGRES_EPOCH_JDATE, &year, &mon, &mday);
1141 
1142  switch (val)
1143  {
1144  case DTK_DAY:
1145  intresult = mday;
1146  break;
1147 
1148  case DTK_MONTH:
1149  intresult = mon;
1150  break;
1151 
1152  case DTK_QUARTER:
1153  intresult = (mon - 1) / 3 + 1;
1154  break;
1155 
1156  case DTK_WEEK:
1157  intresult = date2isoweek(year, mon, mday);
1158  break;
1159 
1160  case DTK_YEAR:
1161  if (year > 0)
1162  intresult = year;
1163  else
1164  /* there is no year 0, just 1 BC and 1 AD */
1165  intresult = year - 1;
1166  break;
1167 
1168  case DTK_DECADE:
1169  /* see comments in timestamp_part */
1170  if (year >= 0)
1171  intresult = year / 10;
1172  else
1173  intresult = -((8 - (year - 1)) / 10);
1174  break;
1175 
1176  case DTK_CENTURY:
1177  /* see comments in timestamp_part */
1178  if (year > 0)
1179  intresult = (year + 99) / 100;
1180  else
1181  intresult = -((99 - (year - 1)) / 100);
1182  break;
1183 
1184  case DTK_MILLENNIUM:
1185  /* see comments in timestamp_part */
1186  if (year > 0)
1187  intresult = (year + 999) / 1000;
1188  else
1189  intresult = -((999 - (year - 1)) / 1000);
1190  break;
1191 
1192  case DTK_JULIAN:
1193  intresult = date + POSTGRES_EPOCH_JDATE;
1194  break;
1195 
1196  case DTK_ISOYEAR:
1197  intresult = date2isoyear(year, mon, mday);
1198  /* Adjust BC years */
1199  if (intresult <= 0)
1200  intresult -= 1;
1201  break;
1202 
1203  case DTK_DOW:
1204  case DTK_ISODOW:
1205  intresult = j2day(date + POSTGRES_EPOCH_JDATE);
1206  if (val == DTK_ISODOW && intresult == 0)
1207  intresult = 7;
1208  break;
1209 
1210  case DTK_DOY:
1211  intresult = date2j(year, mon, mday) - date2j(year, 1, 1) + 1;
1212  break;
1213 
1214  default:
1215  ereport(ERROR,
1216  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1217  errmsg("unit \"%s\" not supported for type %s",
1218  lowunits, format_type_be(DATEOID))));
1219  intresult = 0;
1220  }
1221  }
1222  else if (type == RESERV)
1223  {
1224  switch (val)
1225  {
1226  case DTK_EPOCH:
1227  intresult = ((int64) date + POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY;
1228  break;
1229 
1230  default:
1231  ereport(ERROR,
1232  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1233  errmsg("unit \"%s\" not supported for type %s",
1234  lowunits, format_type_be(DATEOID))));
1235  intresult = 0;
1236  }
1237  }
1238  else
1239  {
1240  ereport(ERROR,
1241  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1242  errmsg("unit \"%s\" not recognized for type %s",
1243  lowunits, format_type_be(DATEOID))));
1244  intresult = 0;
1245  }
1246 
1247  PG_RETURN_NUMERIC(int64_to_numeric(intresult));
1248 }
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:4280
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:636
int date2isoweek(int year, int mon, int mday)
Definition: timestamp.c:5160
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:5215
#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:645
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:689
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:692
#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 2261 of file date.c.

2262 {
2263  return time_part_common(fcinfo, true);
2264 }
static Datum time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2152

References time_part_common().

◆ extract_timetz()

Datum extract_timetz ( PG_FUNCTION_ARGS  )

Definition at line 3062 of file date.c.

3063 {
3064  return timetz_part_common(fcinfo, true);
3065 }
static Datum timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2939

References timetz_part_common().

◆ float_time_overflows()

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

Definition at line 1463 of file date.c.

1464 {
1465  /* Range-check the fields individually. */
1466  if (hour < 0 || hour > HOURS_PER_DAY ||
1467  min < 0 || min >= MINS_PER_HOUR)
1468  return true;
1469 
1470  /*
1471  * "sec", being double, requires extra care. Cope with NaN, and round off
1472  * before applying the range check to avoid unexpected errors due to
1473  * imprecise input. (We assume rint() behaves sanely with infinities.)
1474  */
1475  if (isnan(sec))
1476  return true;
1477  sec = rint(sec * USECS_PER_SEC);
1478  if (sec < 0 || sec > SECS_PER_MINUTE * USECS_PER_SEC)
1479  return true;
1480 
1481  /*
1482  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1483  * that the total time value doesn't exceed 24:00:00. This must match the
1484  * way that callers will convert the fields to a time.
1485  */
1486  if (((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1487  * USECS_PER_SEC) + (int64) sec) > USECS_PER_DAY)
1488  return true;
1489 
1490  return false;
1491 }
#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:2275
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1657
void * palloc(Size size)
Definition: mcxt.c:1317

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:1428

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

Referenced by ExecEvalSQLValueFunction().

◆ hashdate()

Datum hashdate ( PG_FUNCTION_ARGS  )

Definition at line 459 of file date.c.

460 {
461  return hash_uint32(PG_GETARG_DATEADT(0));
462 }
static Datum hash_uint32(uint32 k)
Definition: hashfn.h:43

References hash_uint32(), and PG_GETARG_DATEADT.

◆ hashdateextended()

Datum hashdateextended ( PG_FUNCTION_ARGS  )

Definition at line 465 of file date.c.

466 {
468 }
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
static Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfn.h:49

References hash_uint32_extended(), PG_GETARG_DATEADT, and PG_GETARG_INT64.

◆ in_range_date_interval()

Datum in_range_date_interval ( PG_FUNCTION_ARGS  )

Definition at line 1051 of file date.c.

1052 {
1054  DateADT base = PG_GETARG_DATEADT(1);
1055  Interval *offset = PG_GETARG_INTERVAL_P(2);
1056  bool sub = PG_GETARG_BOOL(3);
1057  bool less = PG_GETARG_BOOL(4);
1058  Timestamp valStamp;
1059  Timestamp baseStamp;
1060 
1061  /* XXX we could support out-of-range cases here, perhaps */
1062  valStamp = date2timestamp(val);
1063  baseStamp = date2timestamp(base);
1064 
1066  TimestampGetDatum(valStamp),
1067  TimestampGetDatum(baseStamp),
1068  IntervalPGetDatum(offset),
1069  BoolGetDatum(sub),
1070  BoolGetDatum(less));
1071 }
Datum in_range_timestamp_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3828
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:649
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 2110 of file date.c.

2111 {
2113  TimeADT base = PG_GETARG_TIMEADT(1);
2114  Interval *offset = PG_GETARG_INTERVAL_P(2);
2115  bool sub = PG_GETARG_BOOL(3);
2116  bool less = PG_GETARG_BOOL(4);
2117  TimeADT sum;
2118 
2119  /*
2120  * Like time_pl_interval/time_mi_interval, we disregard the month and day
2121  * fields of the offset. So our test for negative should too. This also
2122  * catches -infinity, so we only need worry about +infinity below.
2123  */
2124  if (offset->time < 0)
2125  ereport(ERROR,
2126  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2127  errmsg("invalid preceding or following size in window function")));
2128 
2129  /*
2130  * We can't use time_pl_interval/time_mi_interval here, because their
2131  * wraparound behavior would give wrong (or at least undesirable) answers.
2132  * Fortunately the equivalent non-wrapping behavior is trivial, except
2133  * that adding an infinite (or very large) interval might cause integer
2134  * overflow. Subtraction cannot overflow here.
2135  */
2136  if (sub)
2137  sum = base - offset->time;
2138  else if (pg_add_s64_overflow(base, offset->time, &sum))
2139  PG_RETURN_BOOL(less);
2140 
2141  if (less)
2142  PG_RETURN_BOOL(val <= sum);
2143  else
2144  PG_RETURN_BOOL(val >= sum);
2145 }
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:203
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 2662 of file date.c.

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

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

2025 {
2026  Interval *span = PG_GETARG_INTERVAL_P(0);
2027  TimeADT result;
2028 
2029  if (INTERVAL_NOT_FINITE(span))
2030  ereport(ERROR,
2031  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2032  errmsg("cannot convert infinite interval to time")));
2033 
2034  result = span->time % USECS_PER_DAY;
2035  if (result < 0)
2036  result += USECS_PER_DAY;
2037 
2038  PG_RETURN_TIMEADT(result);
2039 }
#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 1589 of file date.c.

1590 {
1591  int tm_hour = PG_GETARG_INT32(0);
1592  int tm_min = PG_GETARG_INT32(1);
1593  double sec = PG_GETARG_FLOAT8(2);
1594  TimeADT time;
1595 
1596  /* Check for time overflow */
1597  if (float_time_overflows(tm_hour, tm_min, sec))
1598  ereport(ERROR,
1599  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
1600  errmsg("time field value out of range: %d:%02d:%02g",
1601  tm_hour, tm_min, sec)));
1602 
1603  /* This should match tm2time */
1604  time = (((tm_hour * MINS_PER_HOUR + tm_min) * SECS_PER_MINUTE)
1605  * USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
1606 
1607  PG_RETURN_TIMEADT(time);
1608 }
bool float_time_overflows(int hour, int min, double sec)
Definition: date.c:1463
#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 1795 of file date.c.

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

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

1501 {
1502  tm->tm_hour = time / USECS_PER_HOUR;
1503  time -= tm->tm_hour * USECS_PER_HOUR;
1504  tm->tm_min = time / USECS_PER_MINUTE;
1505  time -= tm->tm_min * USECS_PER_MINUTE;
1506  tm->tm_sec = time / USECS_PER_SEC;
1507  time -= tm->tm_sec * USECS_PER_SEC;
1508  *fsec = time;
1509  return 0;
1510 }
#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 1746 of file date.c.

1747 {
1748  TimeADT time1 = PG_GETARG_TIMEADT(0);
1749  TimeADT time2 = PG_GETARG_TIMEADT(1);
1750 
1751  if (time1 < time2)
1752  PG_RETURN_INT32(-1);
1753  if (time1 > time2)
1754  PG_RETURN_INT32(1);
1755  PG_RETURN_INT32(0);
1756 }

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

1693 {
1694  TimeADT time1 = PG_GETARG_TIMEADT(0);
1695  TimeADT time2 = PG_GETARG_TIMEADT(1);
1696 
1697  PG_RETURN_BOOL(time1 == time2);
1698 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timeeq().

◆ time_ge()

Datum time_ge ( PG_FUNCTION_ARGS  )

Definition at line 1737 of file date.c.

1738 {
1739  TimeADT time1 = PG_GETARG_TIMEADT(0);
1740  TimeADT time2 = PG_GETARG_TIMEADT(1);
1741 
1742  PG_RETURN_BOOL(time1 >= time2);
1743 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timege().

◆ time_gt()

Datum time_gt ( PG_FUNCTION_ARGS  )

Definition at line 1728 of file date.c.

1729 {
1730  TimeADT time1 = PG_GETARG_TIMEADT(0);
1731  TimeADT time2 = PG_GETARG_TIMEADT(1);
1732 
1733  PG_RETURN_BOOL(time1 > time2);
1734 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timegt().

◆ time_hash()

Datum time_hash ( PG_FUNCTION_ARGS  )

Definition at line 1759 of file date.c.

1760 {
1761  return hashint8(fcinfo);
1762 }
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:83

References hashint8().

◆ time_hash_extended()

Datum time_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 1765 of file date.c.

1766 {
1767  return hashint8extended(fcinfo);
1768 }
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103

References hashint8extended().

◆ time_in()

Datum time_in ( PG_FUNCTION_ARGS  )

Definition at line 1386 of file date.c.

1387 {
1388  char *str = PG_GETARG_CSTRING(0);
1389 #ifdef NOT_USED
1390  Oid typelem = PG_GETARG_OID(1);
1391 #endif
1392  int32 typmod = PG_GETARG_INT32(2);
1393  Node *escontext = fcinfo->context;
1394  TimeADT result;
1395  fsec_t fsec;
1396  struct pg_tm tt,
1397  *tm = &tt;
1398  int tz;
1399  int nf;
1400  int dterr;
1401  char workbuf[MAXDATELEN + 1];
1402  char *field[MAXDATEFIELDS];
1403  int dtype;
1404  int ftype[MAXDATEFIELDS];
1405  DateTimeErrorExtra extra;
1406 
1407  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
1408  field, ftype, MAXDATEFIELDS, &nf);
1409  if (dterr == 0)
1410  dterr = DecodeTimeOnly(field, ftype, nf,
1411  &dtype, tm, &fsec, &tz, &extra);
1412  if (dterr != 0)
1413  {
1414  DateTimeParseError(dterr, &extra, str, "time", escontext);
1415  PG_RETURN_NULL();
1416  }
1417 
1418  tm2time(tm, fsec, &result);
1419  AdjustTimeForTypmod(&result, typmod);
1420 
1421  PG_RETURN_TIMEADT(result);
1422 }
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 2001 of file date.c.

2002 {
2003  TimeADT time = PG_GETARG_TIMEADT(0);
2004  Interval *result;
2005 
2006  result = (Interval *) palloc(sizeof(Interval));
2007 
2008  result->time = time;
2009  result->day = 0;
2010  result->month = 0;
2011 
2012  PG_RETURN_INTERVAL_P(result);
2013 }
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 1771 of file date.c.

1772 {
1773  TimeADT time1 = PG_GETARG_TIMEADT(0);
1774  TimeADT time2 = PG_GETARG_TIMEADT(1);
1775 
1776  PG_RETURN_TIMEADT((time1 > time2) ? time1 : time2);
1777 }

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

◆ time_le()

Datum time_le ( PG_FUNCTION_ARGS  )

Definition at line 1719 of file date.c.

1720 {
1721  TimeADT time1 = PG_GETARG_TIMEADT(0);
1722  TimeADT time2 = PG_GETARG_TIMEADT(1);
1723 
1724  PG_RETURN_BOOL(time1 <= time2);
1725 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timele().

◆ time_lt()

Datum time_lt ( PG_FUNCTION_ARGS  )

Definition at line 1710 of file date.c.

1711 {
1712  TimeADT time1 = PG_GETARG_TIMEADT(0);
1713  TimeADT time2 = PG_GETARG_TIMEADT(1);
1714 
1715  PG_RETURN_BOOL(time1 < time2);
1716 }

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

2088 {
2089  TimeADT time = PG_GETARG_TIMEADT(0);
2090  Interval *span = PG_GETARG_INTERVAL_P(1);
2091  TimeADT result;
2092 
2093  if (INTERVAL_NOT_FINITE(span))
2094  ereport(ERROR,
2095  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2096  errmsg("cannot subtract infinite interval from time")));
2097 
2098  result = time - span->time;
2099  result -= result / USECS_PER_DAY * USECS_PER_DAY;
2100  if (result < INT64CONST(0))
2101  result += USECS_PER_DAY;
2102 
2103  PG_RETURN_TIMEADT(result);
2104 }

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

2046 {
2047  TimeADT time1 = PG_GETARG_TIMEADT(0);
2048  TimeADT time2 = PG_GETARG_TIMEADT(1);
2049  Interval *result;
2050 
2051  result = (Interval *) palloc(sizeof(Interval));
2052 
2053  result->month = 0;
2054  result->day = 0;
2055  result->time = time1 - time2;
2056 
2057  PG_RETURN_INTERVAL_P(result);
2058 }

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

1702 {
1703  TimeADT time1 = PG_GETARG_TIMEADT(0);
1704  TimeADT time2 = PG_GETARG_TIMEADT(1);
1705 
1706  PG_RETURN_BOOL(time1 != time2);
1707 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

◆ time_out()

Datum time_out ( PG_FUNCTION_ARGS  )

Definition at line 1513 of file date.c.

1514 {
1515  TimeADT time = PG_GETARG_TIMEADT(0);
1516  char *result;
1517  struct pg_tm tt,
1518  *tm = &tt;
1519  fsec_t fsec;
1520  char buf[MAXDATELEN + 1];
1521 
1522  time2tm(time, tm, &fsec);
1523  EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf);
1524 
1525  result = pstrdup(buf);
1526  PG_RETURN_CSTRING(result);
1527 }
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:1500

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

Referenced by ExecGetJsonValueItemString().

◆ time_overflows()

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

Definition at line 1439 of file date.c.

1440 {
1441  /* Range-check the fields individually. */
1442  if (hour < 0 || hour > HOURS_PER_DAY ||
1443  min < 0 || min >= MINS_PER_HOUR ||
1444  sec < 0 || sec > SECS_PER_MINUTE ||
1445  fsec < 0 || fsec > USECS_PER_SEC)
1446  return true;
1447 
1448  /*
1449  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1450  * that the total time value doesn't exceed 24:00:00.
1451  */
1452  if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1453  + sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1454  return true;
1455 
1456  return false;
1457 }

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

2256 {
2257  return time_part_common(fcinfo, false);
2258 }

References time_part_common().

◆ time_part_common()

static Datum time_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2152 of file date.c.

2153 {
2154  text *units = PG_GETARG_TEXT_PP(0);
2155  TimeADT time = PG_GETARG_TIMEADT(1);
2156  int64 intresult;
2157  int type,
2158  val;
2159  char *lowunits;
2160 
2161  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
2162  VARSIZE_ANY_EXHDR(units),
2163  false);
2164 
2165  type = DecodeUnits(0, lowunits, &val);
2166  if (type == UNKNOWN_FIELD)
2167  type = DecodeSpecial(0, lowunits, &val);
2168 
2169  if (type == UNITS)
2170  {
2171  fsec_t fsec;
2172  struct pg_tm tt,
2173  *tm = &tt;
2174 
2175  time2tm(time, tm, &fsec);
2176 
2177  switch (val)
2178  {
2179  case DTK_MICROSEC:
2180  intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
2181  break;
2182 
2183  case DTK_MILLISEC:
2184  if (retnumeric)
2185  /*---
2186  * tm->tm_sec * 1000 + fsec / 1000
2187  * = (tm->tm_sec * 1'000'000 + fsec) / 1000
2188  */
2189  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
2190  else
2191  PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
2192  break;
2193 
2194  case DTK_SECOND:
2195  if (retnumeric)
2196  /*---
2197  * tm->tm_sec + fsec / 1'000'000
2198  * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
2199  */
2200  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
2201  else
2202  PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
2203  break;
2204 
2205  case DTK_MINUTE:
2206  intresult = tm->tm_min;
2207  break;
2208 
2209  case DTK_HOUR:
2210  intresult = tm->tm_hour;
2211  break;
2212 
2213  case DTK_TZ:
2214  case DTK_TZ_MINUTE:
2215  case DTK_TZ_HOUR:
2216  case DTK_DAY:
2217  case DTK_MONTH:
2218  case DTK_QUARTER:
2219  case DTK_YEAR:
2220  case DTK_DECADE:
2221  case DTK_CENTURY:
2222  case DTK_MILLENNIUM:
2223  case DTK_ISOYEAR:
2224  default:
2225  ereport(ERROR,
2226  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2227  errmsg("unit \"%s\" not supported for type %s",
2228  lowunits, format_type_be(TIMEOID))));
2229  intresult = 0;
2230  }
2231  }
2232  else if (type == RESERV && val == DTK_EPOCH)
2233  {
2234  if (retnumeric)
2236  else
2237  PG_RETURN_FLOAT8(time / 1000000.0);
2238  }
2239  else
2240  {
2241  ereport(ERROR,
2242  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2243  errmsg("unit \"%s\" not recognized for type %s",
2244  lowunits, format_type_be(TIMEOID))));
2245  intresult = 0;
2246  }
2247 
2248  if (retnumeric)
2249  PG_RETURN_NUMERIC(int64_to_numeric(intresult));
2250  else
2251  PG_RETURN_FLOAT8(intresult);
2252 }
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4301
#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 2064 of file date.c.

2065 {
2066  TimeADT time = PG_GETARG_TIMEADT(0);
2067  Interval *span = PG_GETARG_INTERVAL_P(1);
2068  TimeADT result;
2069 
2070  if (INTERVAL_NOT_FINITE(span))
2071  ereport(ERROR,
2072  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2073  errmsg("cannot add infinite interval to time")));
2074 
2075  result = time + span->time;
2076  result -= result / USECS_PER_DAY * USECS_PER_DAY;
2077  if (result < INT64CONST(0))
2078  result += USECS_PER_DAY;
2079 
2080  PG_RETURN_TIMEADT(result);
2081 }

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

1534 {
1536 
1537 #ifdef NOT_USED
1538  Oid typelem = PG_GETARG_OID(1);
1539 #endif
1540  int32 typmod = PG_GETARG_INT32(2);
1541  TimeADT result;
1542 
1543  result = pq_getmsgint64(buf);
1544 
1545  if (result < INT64CONST(0) || result > USECS_PER_DAY)
1546  ereport(ERROR,
1547  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1548  errmsg("time out of range")));
1549 
1550  AdjustTimeForTypmod(&result, typmod);
1551 
1552  PG_RETURN_TIMEADT(result);
1553 }
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 1637 of file date.c.

1638 {
1639  TimeADT time = PG_GETARG_TIMEADT(0);
1640  int32 typmod = PG_GETARG_INT32(1);
1641  TimeADT result;
1642 
1643  result = time;
1644  AdjustTimeForTypmod(&result, typmod);
1645 
1646  PG_RETURN_TIMEADT(result);
1647 }

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

◆ time_send()

Datum time_send ( PG_FUNCTION_ARGS  )

Definition at line 1559 of file date.c.

1560 {
1561  TimeADT time = PG_GETARG_TIMEADT(0);
1563 
1564  pq_begintypsend(&buf);
1565  pq_sendint64(&buf, time);
1567 }
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 1780 of file date.c.

1781 {
1782  TimeADT time1 = PG_GETARG_TIMEADT(0);
1783  TimeADT time2 = PG_GETARG_TIMEADT(1);
1784 
1785  PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
1786 }

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

◆ time_support()

Datum time_support ( PG_FUNCTION_ARGS  )

Definition at line 1617 of file date.c.

1618 {
1619  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1620  Node *ret = NULL;
1621 
1622  if (IsA(rawreq, SupportRequestSimplify))
1623  {
1625 
1626  ret = TemporalSimplify(MAX_TIME_PRECISION, (Node *) req->fcall);
1627  }
1628 
1629  PG_RETURN_POINTER(ret);
1630 }
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 2840 of file date.c.

2841 {
2842  TimeADT time = PG_GETARG_TIMEADT(0);
2843  TimeTzADT *result;
2844  struct pg_tm tt,
2845  *tm = &tt;
2846  fsec_t fsec;
2847  int tz;
2848 
2850  time2tm(time, tm, &fsec);
2852 
2853  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2854 
2855  result->time = time;
2856  result->zone = tz;
2857 
2858  PG_RETURN_TIMETZADT_P(result);
2859 }
#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 973 of file date.c.

974 {
976  DateADT dateVal = PG_GETARG_DATEADT(1);
977 
979 }

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

1310 {
1312  DateADT result;
1313  struct pg_tm tt,
1314  *tm = &tt;
1315  fsec_t fsec;
1316 
1318  DATE_NOBEGIN(result);
1319  else if (TIMESTAMP_IS_NOEND(timestamp))
1320  DATE_NOEND(result);
1321  else
1322  {
1323  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1324  ereport(ERROR,
1325  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1326  errmsg("timestamp out of range")));
1327 
1329  }
1330 
1331  PG_RETURN_DATEADT(result);
1332 }
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1891
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 919 of file date.c.

920 {
922  DateADT dateVal = PG_GETARG_DATEADT(1);
923 
924  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) == 0);
925 }

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

965 {
967  DateADT dateVal = PG_GETARG_DATEADT(1);
968 
969  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) <= 0);
970 }

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

947 {
949  DateADT dateVal = PG_GETARG_DATEADT(1);
950 
951  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) < 0);
952 }

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

956 {
958  DateADT dateVal = PG_GETARG_DATEADT(1);
959 
960  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) >= 0);
961 }

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

938 {
940  DateADT dateVal = PG_GETARG_DATEADT(1);
941 
942  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) > 0);
943 }

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

929 {
931  DateADT dateVal = PG_GETARG_DATEADT(1);
932 
933  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) != 0);
934 }

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

1918 {
1920  TimeADT result;
1921  struct pg_tm tt,
1922  *tm = &tt;
1923  fsec_t fsec;
1924 
1926  PG_RETURN_NULL();
1927 
1928  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1929  ereport(ERROR,
1930  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1931  errmsg("timestamp out of range")));
1932 
1933  /*
1934  * Could also do this with time = (timestamp / USECS_PER_DAY *
1935  * USECS_PER_DAY) - timestamp;
1936  */
1937  result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1938  USECS_PER_SEC) + fsec;
1939 
1940  PG_RETURN_TIMEADT(result);
1941 }

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

1037 {
1039  DateADT dateVal = PG_GETARG_DATEADT(1);
1040 
1042 }

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

1355 {
1357  DateADT result;
1358  struct pg_tm tt,
1359  *tm = &tt;
1360  fsec_t fsec;
1361  int tz;
1362 
1364  DATE_NOBEGIN(result);
1365  else if (TIMESTAMP_IS_NOEND(timestamp))
1366  DATE_NOEND(result);
1367  else
1368  {
1369  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1370  ereport(ERROR,
1371  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1372  errmsg("timestamp out of range")));
1373 
1375  }
1376 
1377  PG_RETURN_DATEADT(result);
1378 }

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

983 {
985  DateADT dateVal = PG_GETARG_DATEADT(1);
986 
987  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) == 0);
988 }

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

1028 {
1030  DateADT dateVal = PG_GETARG_DATEADT(1);
1031 
1032  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) <= 0);
1033 }

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

1010 {
1012  DateADT dateVal = PG_GETARG_DATEADT(1);
1013 
1014  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) < 0);
1015 }

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

1019 {
1021  DateADT dateVal = PG_GETARG_DATEADT(1);
1022 
1023  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) >= 0);
1024 }

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

1001 {
1003  DateADT dateVal = PG_GETARG_DATEADT(1);
1004 
1005  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) > 0);
1006 }

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

992 {
994  DateADT dateVal = PG_GETARG_DATEADT(1);
995 
996  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) != 0);
997 }

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

1948 {
1950  TimeADT result;
1951  struct pg_tm tt,
1952  *tm = &tt;
1953  int tz;
1954  fsec_t fsec;
1955 
1957  PG_RETURN_NULL();
1958 
1959  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1960  ereport(ERROR,
1961  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1962  errmsg("timestamp out of range")));
1963 
1964  /*
1965  * Could also do this with time = (timestamp / USECS_PER_DAY *
1966  * USECS_PER_DAY) - timestamp;
1967  */
1968  result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1969  USECS_PER_SEC) + fsec;
1970 
1971  PG_RETURN_TIMEADT(result);
1972 }

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

2867 {
2869  TimeTzADT *result;
2870  struct pg_tm tt,
2871  *tm = &tt;
2872  int tz;
2873  fsec_t fsec;
2874 
2876  PG_RETURN_NULL();
2877 
2878  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
2879  ereport(ERROR,
2880  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2881  errmsg("timestamp out of range")));
2882 
2883  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2884 
2885  tm2timetz(tm, fsec, tz, result);
2886 
2887  PG_RETURN_TIMETZADT_P(result);
2888 }

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

1571 {
1573 
1574  PG_RETURN_INT32(anytime_typmodin(false, ta));
1575 }
#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 1578 of file date.c.

1579 {
1580  int32 typmod = PG_GETARG_INT32(0);
1581 
1582  PG_RETURN_CSTRING(anytime_typmodout(false, typmod));
1583 }
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 2415 of file date.c.

2416 {
2417  TimeOffset trem = time->time;
2418 
2419  tm->tm_hour = trem / USECS_PER_HOUR;
2420  trem -= tm->tm_hour * USECS_PER_HOUR;
2421  tm->tm_min = trem / USECS_PER_MINUTE;
2422  trem -= tm->tm_min * USECS_PER_MINUTE;
2423  tm->tm_sec = trem / USECS_PER_SEC;
2424  *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2425 
2426  if (tzp != NULL)
2427  *tzp = time->zone;
2428 
2429  return 0;
2430 }
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 3177 of file date.c.

3178 {
3179  Datum time = PG_GETARG_DATUM(0);
3180  const char *tzn = pg_get_timezone_name(session_timezone);
3182 
3183  return DirectFunctionCall2(timetz_zone, zone, time);
3184 }
Datum timetz_zone(PG_FUNCTION_ARGS)
Definition: date.c:3072
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 2536 of file date.c.

2537 {
2538  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2539  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2540 
2541  PG_RETURN_INT32(timetz_cmp_internal(time1, time2));
2542 }

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

2456 {
2457  TimeOffset t1,
2458  t2;
2459 
2460  /* Primary sort is by true (GMT-equivalent) time */
2461  t1 = time1->time + (time1->zone * USECS_PER_SEC);
2462  t2 = time2->time + (time2->zone * USECS_PER_SEC);
2463 
2464  if (t1 > t2)
2465  return 1;
2466  if (t1 < t2)
2467  return -1;
2468 
2469  /*
2470  * If same GMT time, sort by timezone; we only want to say that two
2471  * timetz's are equal if both the time and zone parts are equal.
2472  */
2473  if (time1->zone > time2->zone)
2474  return 1;
2475  if (time1->zone < time2->zone)
2476  return -1;
2477 
2478  return 0;
2479 }

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

2483 {
2484  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2485  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2486 
2487  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) == 0);
2488 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_ge()

Datum timetz_ge ( PG_FUNCTION_ARGS  )

Definition at line 2527 of file date.c.

2528 {
2529  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2530  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2531 
2532  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) >= 0);
2533 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_gt()

Datum timetz_gt ( PG_FUNCTION_ARGS  )

Definition at line 2518 of file date.c.

2519 {
2520  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2521  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2522 
2523  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) > 0);
2524 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_hash()

Datum timetz_hash ( PG_FUNCTION_ARGS  )

Definition at line 2545 of file date.c.

2546 {
2548  uint32 thash;
2549 
2550  /*
2551  * To avoid any problems with padding bytes in the struct, we figure the
2552  * field hashes separately and XOR them.
2553  */
2555  Int64GetDatumFast(key->time)));
2556  thash ^= DatumGetUInt32(hash_uint32(key->zone));
2557  PG_RETURN_UINT32(thash);
2558 }
unsigned int uint32
Definition: c.h:518
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:355
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
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 2561 of file date.c.

2562 {
2564  Datum seed = PG_GETARG_DATUM(1);
2565  uint64 thash;
2566 
2567  /* Same approach as timetz_hash */
2569  Int64GetDatumFast(key->time),
2570  seed));
2571  thash ^= DatumGetUInt64(hash_uint32_extended(key->zone,
2572  DatumGetInt64(seed)));
2573  PG_RETURN_UINT64(thash);
2574 }
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:369
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 2285 of file date.c.

2286 {
2287  char *str = PG_GETARG_CSTRING(0);
2288 #ifdef NOT_USED
2289  Oid typelem = PG_GETARG_OID(1);
2290 #endif
2291  int32 typmod = PG_GETARG_INT32(2);
2292  Node *escontext = fcinfo->context;
2293  TimeTzADT *result;
2294  fsec_t fsec;
2295  struct pg_tm tt,
2296  *tm = &tt;
2297  int tz;
2298  int nf;
2299  int dterr;
2300  char workbuf[MAXDATELEN + 1];
2301  char *field[MAXDATEFIELDS];
2302  int dtype;
2303  int ftype[MAXDATEFIELDS];
2304  DateTimeErrorExtra extra;
2305 
2306  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
2307  field, ftype, MAXDATEFIELDS, &nf);
2308  if (dterr == 0)
2309  dterr = DecodeTimeOnly(field, ftype, nf,
2310  &dtype, tm, &fsec, &tz, &extra);
2311  if (dterr != 0)
2312  {
2313  DateTimeParseError(dterr, &extra, str, "time with time zone",
2314  escontext);
2315  PG_RETURN_NULL();
2316  }
2317 
2318  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2319  tm2timetz(tm, fsec, tz, result);
2320  AdjustTimeForTypmod(&(result->time), typmod);
2321 
2322  PG_RETURN_TIMETZADT_P(result);
2323 }

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

3135 {
3137  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
3138  TimeTzADT *result;
3139  int tz;
3140 
3142  ereport(ERROR,
3143  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3144  errmsg("interval time zone \"%s\" must be finite",
3146  PointerGetDatum(zone))))));
3147 
3148  if (zone->month != 0 || zone->day != 0)
3149  ereport(ERROR,
3150  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3151  errmsg("interval time zone \"%s\" must not include months or days",
3153  PointerGetDatum(zone))))));
3154 
3155  tz = -(zone->time / USECS_PER_SEC);
3156 
3157  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
3158 
3159  result->time = time->time + (time->zone - tz) * USECS_PER_SEC;
3160  /* C99 modulo has the wrong sign convention for negative input */
3161  while (result->time < INT64CONST(0))
3162  result->time += USECS_PER_DAY;
3163  if (result->time >= USECS_PER_DAY)
3164  result->time %= USECS_PER_DAY;
3165 
3166  result->zone = tz;
3167 
3168  PG_RETURN_TIMETZADT_P(result);
3169 }
Datum interval_out(PG_FUNCTION_ARGS)
Definition: timestamp.c:972
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
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 2577 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

◆ timetz_le()

Datum timetz_le ( PG_FUNCTION_ARGS  )

Definition at line 2509 of file date.c.

2510 {
2511  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2512  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2513 
2514  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) <= 0);
2515 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_lt()

Datum timetz_lt ( PG_FUNCTION_ARGS  )

Definition at line 2500 of file date.c.

2501 {
2502  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2503  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2504 
2505  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) < 0);
2506 }

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

2636 {
2637  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2638  Interval *span = PG_GETARG_INTERVAL_P(1);
2639  TimeTzADT *result;
2640 
2641  if (INTERVAL_NOT_FINITE(span))
2642  ereport(ERROR,
2643  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2644  errmsg("cannot subtract infinite interval from time")));
2645 
2646  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2647 
2648  result->time = time->time - span->time;
2649  result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2650  if (result->time < INT64CONST(0))
2651  result->time += USECS_PER_DAY;
2652 
2653  result->zone = time->zone;
2654 
2655  PG_RETURN_TIMETZADT_P(result);
2656 }

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

2492 {
2493  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2494  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2495 
2496  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) != 0);
2497 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_out()

Datum timetz_out ( PG_FUNCTION_ARGS  )

Definition at line 2326 of file date.c.

2327 {
2328  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2329  char *result;
2330  struct pg_tm tt,
2331  *tm = &tt;
2332  fsec_t fsec;
2333  int tz;
2334  char buf[MAXDATELEN + 1];
2335 
2336  timetz2tm(time, tm, &fsec, &tz);
2337  EncodeTimeOnly(tm, fsec, true, tz, DateStyle, buf);
2338 
2339  result = pstrdup(buf);
2340  PG_RETURN_CSTRING(result);
2341 }
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: date.c:2415

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

Referenced by ExecGetJsonValueItemString().

◆ timetz_part()

Datum timetz_part ( PG_FUNCTION_ARGS  )

Definition at line 3056 of file date.c.

3057 {
3058  return timetz_part_common(fcinfo, false);
3059 }

References timetz_part_common().

◆ timetz_part_common()

static Datum timetz_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2939 of file date.c.

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

2609 {
2610  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2611  Interval *span = PG_GETARG_INTERVAL_P(1);
2612  TimeTzADT *result;
2613 
2614  if (INTERVAL_NOT_FINITE(span))
2615  ereport(ERROR,
2616  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2617  errmsg("cannot add infinite interval to time")));
2618 
2619  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2620 
2621  result->time = time->time + span->time;
2622  result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2623  if (result->time < INT64CONST(0))
2624  result->time += USECS_PER_DAY;
2625 
2626  result->zone = time->zone;
2627 
2628  PG_RETURN_TIMETZADT_P(result);
2629 }

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

2348 {
2350 
2351 #ifdef NOT_USED
2352  Oid typelem = PG_GETARG_OID(1);
2353 #endif
2354  int32 typmod = PG_GETARG_INT32(2);
2355  TimeTzADT *result;
2356 
2357  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2358 
2359  result->time = pq_getmsgint64(buf);
2360 
2361  if (result->time < INT64CONST(0) || result->time > USECS_PER_DAY)
2362  ereport(ERROR,
2363  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2364  errmsg("time out of range")));
2365 
2366  result->zone = pq_getmsgint(buf, sizeof(result->zone));
2367 
2368  /* Check for sane GMT displacement; see notes in datatype/timestamp.h */
2369  if (result->zone <= -TZDISP_LIMIT || result->zone >= TZDISP_LIMIT)
2370  ereport(ERROR,
2371  (errcode(ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE),
2372  errmsg("time zone displacement out of range")));
2373 
2374  AdjustTimeForTypmod(&(result->time), typmod);
2375 
2376  PG_RETURN_TIMETZADT_P(result);
2377 }
#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 2437 of file date.c.

2438 {
2439  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2440  int32 typmod = PG_GETARG_INT32(1);
2441  TimeTzADT *result;
2442 
2443  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2444 
2445  result->time = time->time;
2446  result->zone = time->zone;
2447 
2448  AdjustTimeForTypmod(&(result->time), typmod);
2449 
2450  PG_RETURN_TIMETZADT_P(result);
2451 }

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

2592 {
2593  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2594  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2595  TimeTzADT *result;
2596 
2597  if (timetz_cmp_internal(time1, time2) < 0)
2598  result = time1;
2599  else
2600  result = time2;
2601  PG_RETURN_TIMETZADT_P(result);
2602 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

◆ timetz_time()

Datum timetz_time ( PG_FUNCTION_ARGS  )

Definition at line 2827 of file date.c.

2828 {
2829  TimeTzADT *timetz = PG_GETARG_TIMETZADT_P(0);
2830  TimeADT result;
2831 
2832  /* swallow the time zone and just return the time */
2833  result = timetz->time;
2834 
2835  PG_RETURN_TIMEADT(result);
2836 }

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

3073 {
3074  text *zone = PG_GETARG_TEXT_PP(0);
3076  TimeTzADT *result;
3077  int tz;
3078  char tzname[TZ_STRLEN_MAX + 1];
3079  int type,
3080  val;
3081  pg_tz *tzp;
3082 
3083  /*
3084  * Look up the requested timezone.
3085  */
3086  text_to_cstring_buffer(zone, tzname, sizeof(tzname));
3087 
3088  type = DecodeTimezoneName(tzname, &val, &tzp);
3089 
3090  if (type == TZNAME_FIXED_OFFSET)
3091  {
3092  /* fixed-offset abbreviation */
3093  tz = -val;
3094  }
3095  else if (type == TZNAME_DYNTZ)
3096  {
3097  /* dynamic-offset abbreviation, resolve using transaction start time */
3099  int isdst;
3100 
3101  tz = DetermineTimeZoneAbbrevOffsetTS(now, tzname, tzp, &isdst);
3102  }
3103  else
3104  {
3105  /* Get the offset-from-GMT that is valid now for the zone name */
3107  struct pg_tm tm;
3108  fsec_t fsec;
3109 
3110  if (timestamp2tm(now, &tz, &tm, &fsec, NULL, tzp) != 0)
3111  ereport(ERROR,
3112  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3113  errmsg("timestamp out of range")));
3114  }
3115 
3116  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
3117 
3118  result->time = t->time + (t->zone - tz) * USECS_PER_SEC;
3119  /* C99 modulo has the wrong sign convention for negative input */
3120  while (result->time < INT64CONST(0))
3121  result->time += USECS_PER_DAY;
3122  if (result->time >= USECS_PER_DAY)
3123  result->time %= USECS_PER_DAY;
3124 
3125  result->zone = tz;
3126 
3127  PG_RETURN_TIMETZADT_P(result);
3128 }
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:1608
#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:869

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

2396 {
2398 
2399  PG_RETURN_INT32(anytime_typmodin(true, ta));
2400 }

References anytime_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timetztypmodout()

Datum timetztypmodout ( PG_FUNCTION_ARGS  )

Definition at line 2403 of file date.c.

2404 {
2405  int32 typmod = PG_GETARG_INT32(0);
2406 
2407  PG_RETURN_CSTRING(anytime_typmodout(true, typmod));
2408 }

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

1429 {
1430  *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1431  * USECS_PER_SEC) + fsec;
1432  return 0;
1433 }

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

2276 {
2277  result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2278  USECS_PER_SEC) + fsec;
2279  result->zone = tz;
2280 
2281  return 0;
2282 }

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