PostgreSQL Source Code  git master
date.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <time.h>
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "common/hashfn.h"
#include "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_typmod_check (bool istz, int32 typmod)
 
static int32 anytime_typmodin (bool istz, ArrayType *ta)
 
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)
 
Datum current_date (PG_FUNCTION_ARGS)
 
Datum current_time (PG_FUNCTION_ARGS)
 
Datum sql_localtime (PG_FUNCTION_ARGS)
 
Datum date_eq (PG_FUNCTION_ARGS)
 
Datum date_ne (PG_FUNCTION_ARGS)
 
Datum date_lt (PG_FUNCTION_ARGS)
 
Datum date_le (PG_FUNCTION_ARGS)
 
Datum date_gt (PG_FUNCTION_ARGS)
 
Datum date_ge (PG_FUNCTION_ARGS)
 
Datum date_cmp (PG_FUNCTION_ARGS)
 
Datum date_sortsupport (PG_FUNCTION_ARGS)
 
Datum date_finite (PG_FUNCTION_ARGS)
 
Datum date_larger (PG_FUNCTION_ARGS)
 
Datum date_smaller (PG_FUNCTION_ARGS)
 
Datum date_mi (PG_FUNCTION_ARGS)
 
Datum date_pli (PG_FUNCTION_ARGS)
 
Datum date_mii (PG_FUNCTION_ARGS)
 
Timestamp date2timestamp_opt_overflow (DateADT dateVal, int *overflow)
 
static TimestampTz date2timestamp (DateADT dateVal)
 
TimestampTz date2timestamptz_opt_overflow (DateADT dateVal, int *overflow)
 
static TimestampTz date2timestamptz (DateADT dateVal)
 
double date2timestamp_no_overflow (DateADT dateVal)
 
int32 date_cmp_timestamp_internal (DateADT dateVal, Timestamp dt2)
 
Datum date_eq_timestamp (PG_FUNCTION_ARGS)
 
Datum date_ne_timestamp (PG_FUNCTION_ARGS)
 
Datum date_lt_timestamp (PG_FUNCTION_ARGS)
 
Datum date_gt_timestamp (PG_FUNCTION_ARGS)
 
Datum date_le_timestamp (PG_FUNCTION_ARGS)
 
Datum date_ge_timestamp (PG_FUNCTION_ARGS)
 
Datum date_cmp_timestamp (PG_FUNCTION_ARGS)
 
int32 date_cmp_timestamptz_internal (DateADT dateVal, TimestampTz dt2)
 
Datum date_eq_timestamptz (PG_FUNCTION_ARGS)
 
Datum date_ne_timestamptz (PG_FUNCTION_ARGS)
 
Datum date_lt_timestamptz (PG_FUNCTION_ARGS)
 
Datum date_gt_timestamptz (PG_FUNCTION_ARGS)
 
Datum date_le_timestamptz (PG_FUNCTION_ARGS)
 
Datum date_ge_timestamptz (PG_FUNCTION_ARGS)
 
Datum date_cmp_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_eq_date (PG_FUNCTION_ARGS)
 
Datum timestamp_ne_date (PG_FUNCTION_ARGS)
 
Datum timestamp_lt_date (PG_FUNCTION_ARGS)
 
Datum timestamp_gt_date (PG_FUNCTION_ARGS)
 
Datum timestamp_le_date (PG_FUNCTION_ARGS)
 
Datum timestamp_ge_date (PG_FUNCTION_ARGS)
 
Datum timestamp_cmp_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_eq_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_ne_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_lt_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_gt_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_le_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_ge_date (PG_FUNCTION_ARGS)
 
Datum timestamptz_cmp_date (PG_FUNCTION_ARGS)
 
Datum in_range_date_interval (PG_FUNCTION_ARGS)
 
Datum extract_date (PG_FUNCTION_ARGS)
 
Datum date_pl_interval (PG_FUNCTION_ARGS)
 
Datum date_mi_interval (PG_FUNCTION_ARGS)
 
Datum date_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamp_date (PG_FUNCTION_ARGS)
 
Datum date_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_date (PG_FUNCTION_ARGS)
 
Datum time_in (PG_FUNCTION_ARGS)
 
int tm2time (struct pg_tm *tm, fsec_t fsec, TimeADT *result)
 
bool time_overflows (int hour, int min, int sec, fsec_t fsec)
 
bool float_time_overflows (int hour, int min, double sec)
 
int time2tm (TimeADT time, struct pg_tm *tm, fsec_t *fsec)
 
Datum time_out (PG_FUNCTION_ARGS)
 
Datum time_recv (PG_FUNCTION_ARGS)
 
Datum time_send (PG_FUNCTION_ARGS)
 
Datum timetypmodin (PG_FUNCTION_ARGS)
 
Datum timetypmodout (PG_FUNCTION_ARGS)
 
Datum make_time (PG_FUNCTION_ARGS)
 
Datum time_support (PG_FUNCTION_ARGS)
 
Datum time_scale (PG_FUNCTION_ARGS)
 
void AdjustTimeForTypmod (TimeADT *time, int32 typmod)
 
Datum time_eq (PG_FUNCTION_ARGS)
 
Datum time_ne (PG_FUNCTION_ARGS)
 
Datum time_lt (PG_FUNCTION_ARGS)
 
Datum time_le (PG_FUNCTION_ARGS)
 
Datum time_gt (PG_FUNCTION_ARGS)
 
Datum time_ge (PG_FUNCTION_ARGS)
 
Datum time_cmp (PG_FUNCTION_ARGS)
 
Datum time_hash (PG_FUNCTION_ARGS)
 
Datum time_hash_extended (PG_FUNCTION_ARGS)
 
Datum time_larger (PG_FUNCTION_ARGS)
 
Datum time_smaller (PG_FUNCTION_ARGS)
 
Datum overlaps_time (PG_FUNCTION_ARGS)
 
Datum timestamp_time (PG_FUNCTION_ARGS)
 
Datum timestamptz_time (PG_FUNCTION_ARGS)
 
Datum datetime_timestamp (PG_FUNCTION_ARGS)
 
Datum time_interval (PG_FUNCTION_ARGS)
 
Datum interval_time (PG_FUNCTION_ARGS)
 
Datum time_mi_time (PG_FUNCTION_ARGS)
 
Datum time_pl_interval (PG_FUNCTION_ARGS)
 
Datum time_mi_interval (PG_FUNCTION_ARGS)
 
Datum in_range_time_interval (PG_FUNCTION_ARGS)
 
static Datum time_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum time_part (PG_FUNCTION_ARGS)
 
Datum extract_time (PG_FUNCTION_ARGS)
 
int tm2timetz (struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
 
Datum timetz_in (PG_FUNCTION_ARGS)
 
Datum timetz_out (PG_FUNCTION_ARGS)
 
Datum timetz_recv (PG_FUNCTION_ARGS)
 
Datum timetz_send (PG_FUNCTION_ARGS)
 
Datum timetztypmodin (PG_FUNCTION_ARGS)
 
Datum timetztypmodout (PG_FUNCTION_ARGS)
 
int timetz2tm (TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
 
Datum timetz_scale (PG_FUNCTION_ARGS)
 
static int timetz_cmp_internal (TimeTzADT *time1, TimeTzADT *time2)
 
Datum timetz_eq (PG_FUNCTION_ARGS)
 
Datum timetz_ne (PG_FUNCTION_ARGS)
 
Datum timetz_lt (PG_FUNCTION_ARGS)
 
Datum timetz_le (PG_FUNCTION_ARGS)
 
Datum timetz_gt (PG_FUNCTION_ARGS)
 
Datum timetz_ge (PG_FUNCTION_ARGS)
 
Datum timetz_cmp (PG_FUNCTION_ARGS)
 
Datum timetz_hash (PG_FUNCTION_ARGS)
 
Datum timetz_hash_extended (PG_FUNCTION_ARGS)
 
Datum timetz_larger (PG_FUNCTION_ARGS)
 
Datum timetz_smaller (PG_FUNCTION_ARGS)
 
Datum timetz_pl_interval (PG_FUNCTION_ARGS)
 
Datum timetz_mi_interval (PG_FUNCTION_ARGS)
 
Datum in_range_timetz_interval (PG_FUNCTION_ARGS)
 
Datum overlaps_timetz (PG_FUNCTION_ARGS)
 
Datum timetz_time (PG_FUNCTION_ARGS)
 
Datum time_timetz (PG_FUNCTION_ARGS)
 
Datum timestamptz_timetz (PG_FUNCTION_ARGS)
 
Datum datetimetz_timestamptz (PG_FUNCTION_ARGS)
 
static Datum timetz_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum timetz_part (PG_FUNCTION_ARGS)
 
Datum extract_timetz (PG_FUNCTION_ARGS)
 
Datum timetz_zone (PG_FUNCTION_ARGS)
 
Datum timetz_izone (PG_FUNCTION_ARGS)
 

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

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

References MAX_TIME_PRECISION.

Referenced by current_time(), parse_datetime(), sql_localtime(), time_in(), time_recv(), time_scale(), timetz_in(), timetz_recv(), and timetz_scale().

◆ anytime_typmod_check()

static int32 anytime_typmod_check ( bool  istz,
int32  typmod 
)
static

Definition at line 49 of file date.c.

50 {
51  if (typmod < 0)
52  ereport(ERROR,
53  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
54  errmsg("TIME(%d)%s precision must not be negative",
55  typmod, (istz ? " WITH TIME ZONE" : ""))));
56  if (typmod > MAX_TIME_PRECISION)
57  {
59  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
60  errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
61  typmod, (istz ? " WITH TIME ZONE" : ""),
63  typmod = MAX_TIME_PRECISION;
64  }
65 
66  return typmod;
67 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#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(), current_time(), and sql_localtime().

◆ anytime_typmodin()

static int32 anytime_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 70 of file date.c.

71 {
72  int32 *tl;
73  int n;
74 
75  tl = ArrayGetIntegerTypmods(ta, &n);
76 
77  /*
78  * we're not too tense about good error message here because grammar
79  * shouldn't allow wrong number of modifiers for TIME
80  */
81  if (n != 1)
82  ereport(ERROR,
83  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
84  errmsg("invalid type modifier")));
85 
86  return anytime_typmod_check(istz, tl[0]);
87 }
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:254
signed int int32
Definition: c.h:478
static int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:49

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

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

References psprintf(), and pstrdup().

Referenced by timetypmodout(), and timetztypmodout().

◆ current_date()

Datum current_date ( PG_FUNCTION_ARGS  )

Definition at line 307 of file date.c.

308 {
309  struct pg_tm tm;
310 
311  static int cache_year = 0;
312  static int cache_mon = 0;
313  static int cache_mday = 0;
314  static DateADT cache_date;
315 
317 
318  /*
319  * date2j involves several integer divisions; moreover, unless our session
320  * lives across local midnight, we don't really have to do it more than
321  * once. So it seems worth having a separate cache here.
322  */
323  if (tm.tm_year != cache_year ||
324  tm.tm_mon != cache_mon ||
325  tm.tm_mday != cache_mday)
326  {
328  cache_year = tm.tm_year;
329  cache_mon = tm.tm_mon;
330  cache_mday = tm.tm_mday;
331  }
332 
333  return DateADTGetDatum(cache_date);
334 }
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:367
int date2j(int year, int month, int day)
Definition: datetime.c:287
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:209
static Datum DateADTGetDatum(DateADT X)
Definition: date.h:72
int32 DateADT
Definition: date.h:23
static struct pg_tm tm
Definition: localtime.c:104
Definition: pgtime.h:35
int tm_mday
Definition: pgtime.h:39
int tm_mon
Definition: pgtime.h:40
int tm_year
Definition: pgtime.h:41

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

◆ current_time()

Datum current_time ( PG_FUNCTION_ARGS  )

Definition at line 340 of file date.c.

341 {
342  TimeTzADT *result;
343  struct pg_tm tt,
344  *tm = &tt;
345  fsec_t fsec;
346  int tz;
347  int32 typmod = -1;
348 
349  if (!PG_ARGISNULL(0))
350  typmod = anytime_typmod_check(true, PG_GETARG_INT32(0));
351 
352  GetCurrentTimeUsec(tm, &fsec, &tz);
353 
354  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
355  tm2timetz(tm, fsec, tz, result);
356  AdjustTimeForTypmod(&(result->time), typmod);
357 
358  return TimeTzADTPGetDatum(result);
359 }
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:388
int32 fsec_t
Definition: timestamp.h:41
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2263
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1653
static Datum TimeTzADTPGetDatum(const TimeTzADT *X)
Definition: date.h:84
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
void * palloc(Size size)
Definition: mcxt.c:1210
Definition: date.h:28
TimeADT time
Definition: date.h:29

References AdjustTimeForTypmod(), anytime_typmod_check(), GetCurrentTimeUsec(), palloc(), PG_ARGISNULL, PG_GETARG_INT32, TimeTzADT::time, TimeTzADTPGetDatum(), tm, and tm2timetz().

Referenced by AutoVacLauncherMain(), do_start_worker(), launcher_determine_sleep(), rebuild_database_list(), run_permutation(), and try_complete_step().

◆ date2timestamp()

static TimestampTz date2timestamp ( DateADT  dateVal)
static

Definition at line 616 of file date.c.

617 {
618  return date2timestamp_opt_overflow(dateVal, NULL);
619 }
Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:572

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

729 {
730  double result;
731 
732  if (DATE_IS_NOBEGIN(dateVal))
733  result = -DBL_MAX;
734  else if (DATE_IS_NOEND(dateVal))
735  result = DBL_MAX;
736  else
737  {
738  /* date is days since 2000, timestamp is microseconds since same... */
739  result = dateVal * (double) USECS_PER_DAY;
740  }
741 
742  return result;
743 }
#define USECS_PER_DAY
Definition: timestamp.h:130
#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 572 of file date.c.

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

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

713 {
714  return date2timestamptz_opt_overflow(dateVal, NULL);
715 }
TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:632

References date2timestamptz_opt_overflow().

Referenced by date_timestamptz().

◆ date2timestamptz_opt_overflow()

TimestampTz date2timestamptz_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 632 of file date.c.

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

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

446 {
447  DateADT dateVal1 = PG_GETARG_DATEADT(0);
448  DateADT dateVal2 = PG_GETARG_DATEADT(1);
449 
450  if (dateVal1 < dateVal2)
451  PG_RETURN_INT32(-1);
452  else if (dateVal1 > dateVal2)
453  PG_RETURN_INT32(1);
454  PG_RETURN_INT32(0);
455 }
#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 822 of file date.c.

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

752 {
753  Timestamp dt1;
754  int overflow;
755 
756  dt1 = date2timestamp_opt_overflow(dateVal, &overflow);
757  if (overflow > 0)
758  {
759  /* dt1 is larger than any finite timestamp, but less than infinity */
760  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
761  }
762  Assert(overflow == 0); /* -1 case cannot occur */
763 
764  return timestamp_cmp_internal(dt1, dt2);
765 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2136
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:166
Assert(fmt[strlen(fmt) - 1] !='\n')

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

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

832 {
833  TimestampTz dt1;
834  int overflow;
835 
836  dt1 = date2timestamptz_opt_overflow(dateVal, &overflow);
837  if (overflow > 0)
838  {
839  /* dt1 is larger than any finite timestamp, but less than infinity */
840  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
841  }
842  if (overflow < 0)
843  {
844  /* dt1 is less than any finite timestamp, but more than -infinity */
845  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
846  }
847 
848  return timestamptz_cmp_internal(dt1, dt2);
849 }
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:161
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:127

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

392 {
393  DateADT dateVal1 = PG_GETARG_DATEADT(0);
394  DateADT dateVal2 = PG_GETARG_DATEADT(1);
395 
396  PG_RETURN_BOOL(dateVal1 == dateVal2);
397 }
#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 768 of file date.c.

769 {
770  DateADT dateVal = PG_GETARG_DATEADT(0);
772 
773  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) == 0);
774 }

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

853 {
854  DateADT dateVal = PG_GETARG_DATEADT(0);
856 
857  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) == 0);
858 }

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

468 {
470 
472 }
#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 436 of file date.c.

437 {
438  DateADT dateVal1 = PG_GETARG_DATEADT(0);
439  DateADT dateVal2 = PG_GETARG_DATEADT(1);
440 
441  PG_RETURN_BOOL(dateVal1 >= dateVal2);
442 }

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

814 {
815  DateADT dateVal = PG_GETARG_DATEADT(0);
817 
818  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) >= 0);
819 }

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

898 {
899  DateADT dateVal = PG_GETARG_DATEADT(0);
901 
902  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) >= 0);
903 }

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

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

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

796 {
797  DateADT dateVal = PG_GETARG_DATEADT(0);
799 
800  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) > 0);
801 }

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

880 {
881  DateADT dateVal = PG_GETARG_DATEADT(0);
883 
885 }

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

112 {
113  char *str = PG_GETARG_CSTRING(0);
114  Node *escontext = fcinfo->context;
115  DateADT date;
116  fsec_t fsec;
117  struct pg_tm tt,
118  *tm = &tt;
119  int tzp;
120  int dtype;
121  int nf;
122  int dterr;
123  char *field[MAXDATEFIELDS];
124  int ftype[MAXDATEFIELDS];
125  char workbuf[MAXDATELEN + 1];
126  DateTimeErrorExtra extra;
127 
128  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
129  field, ftype, MAXDATEFIELDS, &nf);
130  if (dterr == 0)
131  dterr = DecodeDateTime(field, ftype, nf,
132  &dtype, tm, &fsec, &tzp, &extra);
133  if (dterr != 0)
134  {
135  DateTimeParseError(dterr, &extra, str, "date", escontext);
136  PG_RETURN_NULL();
137  }
138 
139  switch (dtype)
140  {
141  case DTK_DATE:
142  break;
143 
144  case DTK_EPOCH:
145  GetEpochTime(tm);
146  break;
147 
148  case DTK_LATE:
149  DATE_NOEND(date);
151 
152  case DTK_EARLY:
155 
156  default:
157  DateTimeParseError(DTERR_BAD_FORMAT, &extra, str, "date", escontext);
158  PG_RETURN_NULL();
159  }
160 
161  /* Prevent overflow in Julian-day routines */
163  ereturn(escontext, (Datum) 0,
164  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
165  errmsg("date out of range: \"%s\"", str)));
166 
168 
169  /* Now check for just-out-of-range dates */
170  if (!IS_VALID_DATE(date))
171  ereturn(escontext, (Datum) 0,
172  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
173  errmsg("date out of range: \"%s\"", str)));
174 
176 }
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:755
void DateTimeParseError(int dterr, DateTimeErrorExtra *extra, const char *str, const char *datatype, Node *escontext)
Definition: datetime.c:4043
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp, DateTimeErrorExtra *extra)
Definition: datetime.c:979
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2094
#define IS_VALID_DATE(d)
Definition: timestamp.h:236
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:201
#define PG_RETURN_DATEADT(x)
Definition: date.h:93
#define DATE_NOEND(j)
Definition: date.h:41
#define DATE_NOBEGIN(j)
Definition: date.h:39
#define ereturn(context, dummy_value,...)
Definition: elog.h:276
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#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, generate_unaccent_rules::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 475 of file date.c.

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

References PG_GETARG_DATEADT, and PG_RETURN_DATEADT.

◆ date_le()

Datum date_le ( PG_FUNCTION_ARGS  )

Definition at line 418 of file date.c.

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

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

805 {
806  DateADT dateVal = PG_GETARG_DATEADT(0);
808 
809  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) <= 0);
810 }

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

889 {
890  DateADT dateVal = PG_GETARG_DATEADT(0);
892 
893  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) <= 0);
894 }

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

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

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

787 {
788  DateADT dateVal = PG_GETARG_DATEADT(0);
790 
791  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) < 0);
792 }

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

871 {
872  DateADT dateVal = PG_GETARG_DATEADT(0);
874 
876 }

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

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

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

1275 {
1276  DateADT dateVal = PG_GETARG_DATEADT(0);
1277  Interval *span = PG_GETARG_INTERVAL_P(1);
1278  Timestamp dateStamp;
1279 
1280  dateStamp = date2timestamp(dateVal);
1281 
1283  TimestampGetDatum(dateStamp),
1284  PointerGetDatum(span));
1285 }
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3001
static TimestampTz date2timestamp(DateADT dateVal)
Definition: date.c:616
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Datum TimestampGetDatum(Timestamp X)
Definition: timestamp.h:46
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:65

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

◆ date_mii()

Datum date_mii ( PG_FUNCTION_ARGS  )

Definition at line 536 of file date.c.

537 {
538  DateADT dateVal = PG_GETARG_DATEADT(0);
540  DateADT result;
541 
542  if (DATE_NOT_FINITE(dateVal))
543  PG_RETURN_DATEADT(dateVal); /* can't change infinity */
544 
545  result = dateVal - days;
546 
547  /* Check for integer overflow and out-of-allowed-range */
548  if ((days >= 0 ? (result > dateVal) : (result < dateVal)) ||
549  !IS_VALID_DATE(result))
550  ereport(ERROR,
551  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
552  errmsg("date out of range")));
553 
554  PG_RETURN_DATEADT(result);
555 }
const char *const days[]
Definition: datetime.c:85

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

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

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

◆ date_ne_timestamp()

Datum date_ne_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 777 of file date.c.

778 {
779  DateADT dateVal = PG_GETARG_DATEADT(0);
781 
782  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) != 0);
783 }

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

862 {
863  DateADT dateVal = PG_GETARG_DATEADT(0);
865 
866  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) != 0);
867 }

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

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

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.

◆ date_pl_interval()

Datum date_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 1254 of file date.c.

1255 {
1256  DateADT dateVal = PG_GETARG_DATEADT(0);
1257  Interval *span = PG_GETARG_INTERVAL_P(1);
1258  Timestamp dateStamp;
1259 
1260  dateStamp = date2timestamp(dateVal);
1261 
1263  TimestampGetDatum(dateStamp),
1264  PointerGetDatum(span));
1265 }
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2922

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

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

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

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

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

230 {
233 
235  pq_sendint32(&buf, date);
237 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:349
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145

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

485 {
486  DateADT dateVal1 = PG_GETARG_DATEADT(0);
487  DateADT dateVal2 = PG_GETARG_DATEADT(1);
488 
489  PG_RETURN_DATEADT((dateVal1 < dateVal2) ? dateVal1 : dateVal2);
490 }

References PG_GETARG_DATEADT, and PG_RETURN_DATEADT.

◆ date_sortsupport()

Datum date_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 458 of file date.c.

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

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

◆ date_timestamp()

Datum date_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1291 of file date.c.

1292 {
1293  DateADT dateVal = PG_GETARG_DATEADT(0);
1294  Timestamp result;
1295 
1296  result = date2timestamp(dateVal);
1297 
1298  PG_RETURN_TIMESTAMP(result);
1299 }
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:67

References date2timestamp(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

◆ date_timestamptz()

Datum date_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 1335 of file date.c.

1336 {
1337  DateADT dateVal = PG_GETARG_DATEADT(0);
1338  TimestampTz result;
1339 
1340  result = date2timestamptz(dateVal);
1341 
1342  PG_RETURN_TIMESTAMP(result);
1343 }
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:712

References date2timestamptz(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

◆ datetime_timestamp()

Datum datetime_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1974 of file date.c.

1975 {
1977  TimeADT time = PG_GETARG_TIMEADT(1);
1978  Timestamp result;
1979 
1980  result = date2timestamp(date);
1981  if (!TIMESTAMP_NOT_FINITE(result))
1982  {
1983  result += time;
1984  if (!IS_VALID_TIMESTAMP(result))
1985  ereport(ERROR,
1986  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1987  errmsg("timestamp out of range")));
1988  }
1989 
1990  PG_RETURN_TIMESTAMP(result);
1991 }
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:168
#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 2874 of file date.c.

2875 {
2877  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2878  TimestampTz result;
2879 
2880  if (DATE_IS_NOBEGIN(date))
2881  TIMESTAMP_NOBEGIN(result);
2882  else if (DATE_IS_NOEND(date))
2883  TIMESTAMP_NOEND(result);
2884  else
2885  {
2886  /*
2887  * Date's range is wider than timestamp's, so check for boundaries.
2888  * Since dates have the same minimum values as timestamps, only upper
2889  * boundary need be checked for overflow.
2890  */
2892  ereport(ERROR,
2893  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2894  errmsg("date out of range for timestamp")));
2895  result = date * USECS_PER_DAY + time->time + time->zone * USECS_PER_SEC;
2896 
2897  /*
2898  * Since it is possible to go beyond allowed timestamptz range because
2899  * of time zone, check for allowed timestamp range after adding tz.
2900  */
2901  if (!IS_VALID_TIMESTAMP(result))
2902  ereport(ERROR,
2903  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2904  errmsg("date out of range for timestamp")));
2905  }
2906 
2907  PG_RETURN_TIMESTAMP(result);
2908 }
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:91
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 292 of file date.c.

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

References DATE_IS_NOBEGIN, DATE_IS_NOEND, EARLY, elog(), ERROR, LATE, and generate_unaccent_rules::str.

Referenced by date_out(), and JsonEncodeDateTime().

◆ extract_date()

Datum extract_date ( PG_FUNCTION_ARGS  )

Definition at line 1074 of file date.c.

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

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, generate_unaccent_rules::type, UNITS, UNIX_EPOCH_JDATE, UNKNOWN_FIELD, val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ extract_time()

Datum extract_time ( PG_FUNCTION_ARGS  )

Definition at line 2249 of file date.c.

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

References time_part_common().

◆ extract_timetz()

Datum extract_timetz ( PG_FUNCTION_ARGS  )

Definition at line 3038 of file date.c.

3039 {
3040  return timetz_part_common(fcinfo, true);
3041 }
static Datum timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2915

References timetz_part_common().

◆ float_time_overflows()

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

Definition at line 1459 of file date.c.

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

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

◆ in_range_date_interval()

Datum in_range_date_interval ( PG_FUNCTION_ARGS  )

Definition at line 1047 of file date.c.

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

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

◆ in_range_time_interval()

Datum in_range_time_interval ( PG_FUNCTION_ARGS  )

Definition at line 2100 of file date.c.

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

References ereport, errcode(), errmsg(), ERROR, 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 2640 of file date.c.

2641 {
2643  TimeTzADT *base = PG_GETARG_TIMETZADT_P(1);
2644  Interval *offset = PG_GETARG_INTERVAL_P(2);
2645  bool sub = PG_GETARG_BOOL(3);
2646  bool less = PG_GETARG_BOOL(4);
2647  TimeTzADT sum;
2648 
2649  /*
2650  * Like timetz_pl_interval/timetz_mi_interval, we disregard the month and
2651  * day fields of the offset. So our test for negative should too.
2652  */
2653  if (offset->time < 0)
2654  ereport(ERROR,
2655  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2656  errmsg("invalid preceding or following size in window function")));
2657 
2658  /*
2659  * We can't use timetz_pl_interval/timetz_mi_interval here, because their
2660  * wraparound behavior would give wrong (or at least undesirable) answers.
2661  * Fortunately the equivalent non-wrapping behavior is trivial, especially
2662  * since we don't worry about integer overflow.
2663  */
2664  if (sub)
2665  sum.time = base->time - offset->time;
2666  else
2667  sum.time = base->time + offset->time;
2668  sum.zone = base->zone;
2669 
2670  if (less)
2671  PG_RETURN_BOOL(timetz_cmp_internal(val, &sum) <= 0);
2672  else
2673  PG_RETURN_BOOL(timetz_cmp_internal(val, &sum) >= 0);
2674 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2443

References ereport, errcode(), errmsg(), ERROR, 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 2020 of file date.c.

2021 {
2022  Interval *span = PG_GETARG_INTERVAL_P(0);
2023  TimeADT result;
2024  int64 days;
2025 
2026  result = span->time;
2027  if (result >= USECS_PER_DAY)
2028  {
2029  days = result / USECS_PER_DAY;
2030  result -= days * USECS_PER_DAY;
2031  }
2032  else if (result < 0)
2033  {
2034  days = (-result + USECS_PER_DAY - 1) / USECS_PER_DAY;
2035  result += days * USECS_PER_DAY;
2036  }
2037 
2038  PG_RETURN_TIMEADT(result);
2039 }
#define PG_RETURN_TIMEADT(x)
Definition: date.h:94

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

◆ make_date()

Datum make_date ( PG_FUNCTION_ARGS  )

Definition at line 243 of file date.c.

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

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

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

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

◆ sql_localtime()

Datum sql_localtime ( PG_FUNCTION_ARGS  )

Definition at line 365 of file date.c.

366 {
367  TimeADT result;
368  struct pg_tm tt,
369  *tm = &tt;
370  fsec_t fsec;
371  int tz;
372  int32 typmod = -1;
373 
374  if (!PG_ARGISNULL(0))
375  typmod = anytime_typmod_check(false, PG_GETARG_INT32(0));
376 
377  GetCurrentTimeUsec(tm, &fsec, &tz);
378 
379  tm2time(tm, fsec, &result);
380  AdjustTimeForTypmod(&result, typmod);
381 
382  return TimeADTGetDatum(result);
383 }
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1424
static Datum TimeADTGetDatum(TimeADT X)
Definition: date.h:78

References AdjustTimeForTypmod(), anytime_typmod_check(), GetCurrentTimeUsec(), PG_ARGISNULL, PG_GETARG_INT32, TimeADTGetDatum(), tm, and tm2time().

◆ time2tm()

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

Definition at line 1496 of file date.c.

1497 {
1498  tm->tm_hour = time / USECS_PER_HOUR;
1499  time -= tm->tm_hour * USECS_PER_HOUR;
1500  tm->tm_min = time / USECS_PER_MINUTE;
1501  time -= tm->tm_min * USECS_PER_MINUTE;
1502  tm->tm_sec = time / USECS_PER_SEC;
1503  time -= tm->tm_sec * USECS_PER_SEC;
1504  *fsec = time;
1505  return 0;
1506 }
#define USECS_PER_HOUR
Definition: timestamp.h:131
#define USECS_PER_MINUTE
Definition: timestamp.h:132

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

1743 {
1744  TimeADT time1 = PG_GETARG_TIMEADT(0);
1745  TimeADT time2 = PG_GETARG_TIMEADT(1);
1746 
1747  if (time1 < time2)
1748  PG_RETURN_INT32(-1);
1749  if (time1 > time2)
1750  PG_RETURN_INT32(1);
1751  PG_RETURN_INT32(0);
1752 }

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

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

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timeeq().

◆ time_ge()

Datum time_ge ( PG_FUNCTION_ARGS  )

Definition at line 1733 of file date.c.

1734 {
1735  TimeADT time1 = PG_GETARG_TIMEADT(0);
1736  TimeADT time2 = PG_GETARG_TIMEADT(1);
1737 
1738  PG_RETURN_BOOL(time1 >= time2);
1739 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timege().

◆ time_gt()

Datum time_gt ( PG_FUNCTION_ARGS  )

Definition at line 1724 of file date.c.

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

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timegt().

◆ time_hash()

Datum time_hash ( PG_FUNCTION_ARGS  )

Definition at line 1755 of file date.c.

1756 {
1757  return hashint8(fcinfo);
1758 }
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:85

References hashint8().

◆ time_hash_extended()

Datum time_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 1761 of file date.c.

1762 {
1763  return hashint8extended(fcinfo);
1764 }
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:105

References hashint8extended().

◆ time_in()

Datum time_in ( PG_FUNCTION_ARGS  )

Definition at line 1382 of file date.c.

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

◆ time_interval()

Datum time_interval ( PG_FUNCTION_ARGS  )

Definition at line 1997 of file date.c.

1998 {
1999  TimeADT time = PG_GETARG_TIMEADT(0);
2000  Interval *result;
2001 
2002  result = (Interval *) palloc(sizeof(Interval));
2003 
2004  result->time = time;
2005  result->day = 0;
2006  result->month = 0;
2007 
2008  PG_RETURN_INTERVAL_P(result);
2009 }
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 1767 of file date.c.

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

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

◆ time_le()

Datum time_le ( PG_FUNCTION_ARGS  )

Definition at line 1715 of file date.c.

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

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timele().

◆ time_lt()

Datum time_lt ( PG_FUNCTION_ARGS  )

Definition at line 1706 of file date.c.

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

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

2083 {
2084  TimeADT time = PG_GETARG_TIMEADT(0);
2085  Interval *span = PG_GETARG_INTERVAL_P(1);
2086  TimeADT result;
2087 
2088  result = time - span->time;
2089  result -= result / USECS_PER_DAY * USECS_PER_DAY;
2090  if (result < INT64CONST(0))
2091  result += USECS_PER_DAY;
2092 
2093  PG_RETURN_TIMEADT(result);
2094 }

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

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

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

◆ time_out()

Datum time_out ( PG_FUNCTION_ARGS  )

Definition at line 1509 of file date.c.

1510 {
1511  TimeADT time = PG_GETARG_TIMEADT(0);
1512  char *result;
1513  struct pg_tm tt,
1514  *tm = &tt;
1515  fsec_t fsec;
1516  char buf[MAXDATELEN + 1];
1517 
1518  time2tm(time, tm, &fsec);
1519  EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf);
1520 
1521  result = pstrdup(buf);
1522  PG_RETURN_CSTRING(result);
1523 }
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:4263
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1496

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

◆ time_overflows()

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

Definition at line 1435 of file date.c.

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

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

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ time_part()

Datum time_part ( PG_FUNCTION_ARGS  )

Definition at line 2243 of file date.c.

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

References time_part_common().

◆ time_part_common()

static Datum time_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2140 of file date.c.

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

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

1530 {
1532 
1533 #ifdef NOT_USED
1534  Oid typelem = PG_GETARG_OID(1);
1535 #endif
1536  int32 typmod = PG_GETARG_INT32(2);
1537  TimeADT result;
1538 
1539  result = pq_getmsgint64(buf);
1540 
1541  if (result < INT64CONST(0) || result > USECS_PER_DAY)
1542  ereport(ERROR,
1543  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1544  errmsg("time out of range")));
1545 
1546  AdjustTimeForTypmod(&result, typmod);
1547 
1548  PG_RETURN_TIMEADT(result);
1549 }
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:456

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

1634 {
1635  TimeADT time = PG_GETARG_TIMEADT(0);
1636  int32 typmod = PG_GETARG_INT32(1);
1637  TimeADT result;
1638 
1639  result = time;
1640  AdjustTimeForTypmod(&result, typmod);
1641 
1642  PG_RETURN_TIMEADT(result);
1643 }

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

◆ time_send()

Datum time_send ( PG_FUNCTION_ARGS  )

Definition at line 1555 of file date.c.

1556 {
1557  TimeADT time = PG_GETARG_TIMEADT(0);
1559 
1560  pq_begintypsend(&buf);
1561  pq_sendint64(&buf, time);
1563 }
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153

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

1777 {
1778  TimeADT time1 = PG_GETARG_TIMEADT(0);
1779  TimeADT time2 = PG_GETARG_TIMEADT(1);
1780 
1781  PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
1782 }

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

◆ time_support()

Datum time_support ( PG_FUNCTION_ARGS  )

Definition at line 1613 of file date.c.

1614 {
1615  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1616  Node *ret = NULL;
1617 
1618  if (IsA(rawreq, SupportRequestSimplify))
1619  {
1621 
1622  ret = TemporalSimplify(MAX_TIME_PRECISION, (Node *) req->fcall);
1623  }
1624 
1625  PG_RETURN_POINTER(ret);
1626 }
Node * TemporalSimplify(int32 max_precis, Node *node)
Definition: datetime.c:4791
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define IsA(nodeptr, _type_)
Definition: nodes.h:179

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

2817 {
2818  TimeADT time = PG_GETARG_TIMEADT(0);
2819  TimeTzADT *result;
2820  struct pg_tm tt,
2821  *tm = &tt;
2822  fsec_t fsec;
2823  int tz;
2824 
2826  time2tm(time, tm, &fsec);
2828 
2829  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2830 
2831  result->time = time;
2832  result->zone = tz;
2833 
2834  PG_RETURN_TIMETZADT_P(result);
2835 }
#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().

◆ timestamp_cmp_date()

Datum timestamp_cmp_date ( PG_FUNCTION_ARGS  )

Definition at line 969 of file date.c.

970 {
972  DateADT dateVal = PG_GETARG_DATEADT(1);
973 
975 }

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

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

◆ timestamp_eq_date()

Datum timestamp_eq_date ( PG_FUNCTION_ARGS  )

Definition at line 915 of file date.c.

916 {
918  DateADT dateVal = PG_GETARG_DATEADT(1);
919 
920  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) == 0);
921 }

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

961 {
963  DateADT dateVal = PG_GETARG_DATEADT(1);
964 
965  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) <= 0);
966 }

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

943 {
945  DateADT dateVal = PG_GETARG_DATEADT(1);
946 
947  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) < 0);
948 }

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

952 {
954  DateADT dateVal = PG_GETARG_DATEADT(1);
955 
956  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) >= 0);
957 }

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

934 {
936  DateADT dateVal = PG_GETARG_DATEADT(1);
937 
938  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) > 0);
939 }

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

925 {
927  DateADT dateVal = PG_GETARG_DATEADT(1);
928 
929  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) != 0);
930 }

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

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

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.

◆ timestamptz_cmp_date()

Datum timestamptz_cmp_date ( PG_FUNCTION_ARGS  )

Definition at line 1032 of file date.c.

1033 {
1035  DateADT dateVal = PG_GETARG_DATEADT(1);
1036 
1038 }

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

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

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.

◆ timestamptz_eq_date()

Datum timestamptz_eq_date ( PG_FUNCTION_ARGS  )

Definition at line 978 of file date.c.

979 {
981  DateADT dateVal = PG_GETARG_DATEADT(1);
982 
983  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) == 0);
984 }

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

1024 {
1026  DateADT dateVal = PG_GETARG_DATEADT(1);
1027 
1028  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) <= 0);
1029 }

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

1006 {
1008  DateADT dateVal = PG_GETARG_DATEADT(1);
1009 
1010  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) < 0);
1011 }

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

1015 {
1017  DateADT dateVal = PG_GETARG_DATEADT(1);
1018 
1019  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) >= 0);
1020 }

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

997 {
999  DateADT dateVal = PG_GETARG_DATEADT(1);
1000 
1001  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) > 0);
1002 }

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

988 {
990  DateADT dateVal = PG_GETARG_DATEADT(1);
991 
992  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) != 0);
993 }

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

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

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.

◆ timestamptz_timetz()

Datum timestamptz_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2842 of file date.c.

2843 {
2845  TimeTzADT *result;
2846  struct pg_tm tt,
2847  *tm = &tt;
2848  int tz;
2849  fsec_t fsec;
2850 
2852  PG_RETURN_NULL();
2853 
2854  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
2855  ereport(ERROR,
2856  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2857  errmsg("timestamp out of range")));
2858 
2859  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2860 
2861  tm2timetz(tm, fsec, tz, result);
2862 
2863  PG_RETURN_TIMETZADT_P(result);
2864 }

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

◆ timetypmodin()

Datum timetypmodin ( PG_FUNCTION_ARGS  )

Definition at line 1566 of file date.c.

1567 {
1569 
1570  PG_RETURN_INT32(anytime_typmodin(false, ta));
1571 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
static int32 anytime_typmodin(bool istz, ArrayType *ta)
Definition: date.c:70

References anytime_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timetypmodout()

Datum timetypmodout ( PG_FUNCTION_ARGS  )

Definition at line 1574 of file date.c.

1575 {
1576  int32 typmod = PG_GETARG_INT32(0);
1577 
1578  PG_RETURN_CSTRING(anytime_typmodout(false, typmod));
1579 }
static char * anytime_typmodout(bool istz, int32 typmod)
Definition: date.c:91

References anytime_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

◆ timetz2tm()

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

Definition at line 2403 of file date.c.

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

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

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

◆ timetz_cmp()

Datum timetz_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2524 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_INT32, and timetz_cmp_internal().

Referenced by compareDatetime().

◆ timetz_cmp_internal()

static int timetz_cmp_internal ( TimeTzADT time1,
TimeTzADT time2 
)
static

Definition at line 2443 of file date.c.

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

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

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

◆ timetz_eq()

Datum timetz_eq ( PG_FUNCTION_ARGS  )

Definition at line 2470 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_ge()

Datum timetz_ge ( PG_FUNCTION_ARGS  )

Definition at line 2515 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_gt()

Datum timetz_gt ( PG_FUNCTION_ARGS  )

Definition at line 2506 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_hash()

Datum timetz_hash ( PG_FUNCTION_ARGS  )

Definition at line 2533 of file date.c.

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

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

◆ timetz_hash_extended()

Datum timetz_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2549 of file date.c.

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

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

◆ timetz_in()

Datum timetz_in ( PG_FUNCTION_ARGS  )

Definition at line 2273 of file date.c.

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

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

◆ timetz_izone()

Datum timetz_izone ( PG_FUNCTION_ARGS  )

Definition at line 3136 of file date.c.

3137 {
3139  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
3140  TimeTzADT *result;
3141  int tz;
3142 
3143  if (zone->month != 0 || zone->day != 0)
3144  ereport(ERROR,
3145  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3146  errmsg("interval time zone \"%s\" must not include months or days",
3148  PointerGetDatum(zone))))));
3149 
3150  tz = -(zone->time / USECS_PER_SEC);
3151 
3152  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
3153 
3154  result->time = time->time + (time->zone - tz) * USECS_PER_SEC;
3155  while (result->time < INT64CONST(0))
3156  result->time += USECS_PER_DAY;
3157  while (result->time >= USECS_PER_DAY)
3158  result->time -= USECS_PER_DAY;
3159 
3160  result->zone = tz;
3161 
3162  PG_RETURN_TIMETZADT_P(result);
3163 }
Datum interval_out(PG_FUNCTION_ARGS)
Definition: timestamp.c:960
while(p+4<=pend)
static char * DatumGetCString(Datum X)
Definition: postgres.h:335
Definition: zic.c:94

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

◆ timetz_larger()

Datum timetz_larger ( PG_FUNCTION_ARGS  )

Definition at line 2565 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

◆ timetz_le()

Datum timetz_le ( PG_FUNCTION_ARGS  )

Definition at line 2497 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_lt()

Datum timetz_lt ( PG_FUNCTION_ARGS  )

Definition at line 2488 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_mi_interval()

Datum timetz_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 2618 of file date.c.

2619 {
2620  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2621  Interval *span = PG_GETARG_INTERVAL_P(1);
2622  TimeTzADT *result;
2623 
2624  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2625 
2626  result->time = time->time - span->time;
2627  result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2628  if (result->time < INT64CONST(0))
2629  result->time += USECS_PER_DAY;
2630 
2631  result->zone = time->zone;
2632 
2633  PG_RETURN_TIMETZADT_P(result);
2634 }

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

◆ timetz_ne()

Datum timetz_ne ( PG_FUNCTION_ARGS  )

Definition at line 2479 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_out()

Datum timetz_out ( PG_FUNCTION_ARGS  )

Definition at line 2314 of file date.c.

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

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

◆ timetz_part()

Datum timetz_part ( PG_FUNCTION_ARGS  )

Definition at line 3032 of file date.c.

3033 {
3034  return timetz_part_common(fcinfo, false);
3035 }

References timetz_part_common().

◆ timetz_part_common()

static Datum timetz_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2915 of file date.c.

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

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

Referenced by extract_timetz(), and timetz_part().

◆ timetz_pl_interval()

Datum timetz_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 2596 of file date.c.

2597 {
2598  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2599  Interval *span = PG_GETARG_INTERVAL_P(1);
2600  TimeTzADT *result;
2601 
2602  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2603 
2604  result->time = time->time + span->time;
2605  result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2606  if (result->time < INT64CONST(0))
2607  result->time += USECS_PER_DAY;
2608 
2609  result->zone = time->zone;
2610 
2611  PG_RETURN_TIMETZADT_P(result);
2612 }

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

◆ timetz_recv()

Datum timetz_recv ( PG_FUNCTION_ARGS  )

Definition at line 2335 of file date.c.

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

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

◆ timetz_scale()

Datum timetz_scale ( PG_FUNCTION_ARGS  )

Definition at line 2425 of file date.c.

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

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

◆ timetz_send()

Datum timetz_send ( PG_FUNCTION_ARGS  )

◆ timetz_smaller()

Datum timetz_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2579 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

◆ timetz_time()

Datum timetz_time ( PG_FUNCTION_ARGS  )

Definition at line 2803 of file date.c.

2804 {
2805  TimeTzADT *timetz = PG_GETARG_TIMETZADT_P(0);
2806  TimeADT result;
2807 
2808  /* swallow the time zone and just return the time */
2809  result = timetz->time;
2810 
2811  PG_RETURN_TIMEADT(result);
2812 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMEADT, and TimeTzADT::time.

◆ timetz_zone()

Datum timetz_zone ( PG_FUNCTION_ARGS  )

Definition at line 3048 of file date.c.

3049 {
3050  text *zone = PG_GETARG_TEXT_PP(0);
3052  TimeTzADT *result;
3053  int tz;
3054  char tzname[TZ_STRLEN_MAX + 1];
3055  char *lowzone;
3056  int dterr,
3057  type,
3058  val;
3059  pg_tz *tzp;
3060  DateTimeErrorExtra extra;
3061 
3062  /*
3063  * Look up the requested timezone. First we look in the timezone
3064  * abbreviation table (to handle cases like "EST"), and if that fails, we
3065  * look in the timezone database (to handle cases like
3066  * "America/New_York"). (This matches the order in which timestamp input
3067  * checks the cases; it's important because the timezone database unwisely
3068  * uses a few zone names that are identical to offset abbreviations.)
3069  */
3070  text_to_cstring_buffer(zone, tzname, sizeof(tzname));
3071 
3072  /* DecodeTimezoneAbbrev requires lowercase input */
3073  lowzone = downcase_truncate_identifier(tzname,
3074  strlen(tzname),
3075  false);
3076 
3077  dterr = DecodeTimezoneAbbrev(0, lowzone, &type, &val, &tzp, &extra);
3078  if (dterr)
3079  DateTimeParseError(dterr, &extra, NULL, NULL, NULL);
3080 
3081  if (type == TZ || type == DTZ)
3082  {
3083  /* fixed-offset abbreviation */
3084  tz = -val;
3085  }
3086  else if (type == DYNTZ)
3087  {
3088  /* dynamic-offset abbreviation, resolve using transaction start time */
3090  int isdst;
3091 
3092  tz = DetermineTimeZoneAbbrevOffsetTS(now, tzname, tzp, &isdst);
3093  }
3094  else
3095  {
3096  /* try it as a full zone name */
3097  tzp = pg_tzset(tzname);
3098  if (tzp)
3099  {
3100  /* Get the offset-from-GMT that is valid now for the zone */
3102  struct pg_tm tm;
3103  fsec_t fsec;
3104 
3105  if (timestamp2tm(now, &tz, &tm, &fsec, NULL, tzp) != 0)
3106  ereport(ERROR,
3107  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3108  errmsg("timestamp out of range")));
3109  }
3110  else
3111  {
3112  ereport(ERROR,
3113  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3114  errmsg("time zone \"%s\" not recognized", tzname)));
3115  tz = 0; /* keep compiler quiet */
3116  }
3117  }
3118 
3119  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
3120 
3121  result->time = t->time + (t->zone - tz) * USECS_PER_SEC;
3122  while (result->time < INT64CONST(0))
3123  result->time += USECS_PER_DAY;
3124  while (result->time >= USECS_PER_DAY)
3125  result->time -= USECS_PER_DAY;
3126 
3127  result->zone = tz;
3128 
3129  PG_RETURN_TIMETZADT_P(result);
3130 }
int DetermineTimeZoneAbbrevOffsetTS(TimestampTz ts, const char *abbr, pg_tz *tzp, int *isdst)
Definition: datetime.c:1849
int DecodeTimezoneAbbrev(int field, const char *lowtoken, int *ftype, int *offset, pg_tz **tz, DateTimeErrorExtra *extra)
Definition: datetime.c:3234
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1549
#define TZ
Definition: datetime.h:95
#define DTZ
Definition: datetime.h:96
#define DYNTZ
Definition: datetime.h:97
#define TZ_STRLEN_MAX
Definition: pgtime.h:54
pg_tz * pg_tzset(const char *tzname)
Definition: pgtz.c:234
Definition: pgtz.h:66
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition: varlena.c:253
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:855

References DateTimeParseError(), DecodeTimezoneAbbrev(), DetermineTimeZoneAbbrevOffsetTS(), downcase_truncate_identifier(), DTZ, DYNTZ, ereport, errcode(), errmsg(), ERROR, GetCurrentTransactionStartTimestamp(), now(), palloc(), PG_GETARG_TEXT_PP, PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, pg_tzset(), text_to_cstring_buffer(), TimeTzADT::time, timestamp2tm(), tm, generate_unaccent_rules::type, TZ, TZ_STRLEN_MAX, USECS_PER_DAY, USECS_PER_SEC, val, while(), and TimeTzADT::zone.

◆ timetztypmodin()

Datum timetztypmodin ( PG_FUNCTION_ARGS  )

Definition at line 2383 of file date.c.

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

References anytime_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timetztypmodout()

Datum timetztypmodout ( PG_FUNCTION_ARGS  )

Definition at line 2391 of file date.c.

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

References anytime_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

◆ tm2time()

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

Definition at line 1424 of file date.c.

1425 {
1426  *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1427  * USECS_PER_SEC) + fsec;
1428  return 0;
1429 }

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 parse_datetime(), sql_localtime(), and time_in().

◆ tm2timetz()

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

Definition at line 2263 of file date.c.

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

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

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