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

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

1634 {
1635  static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1636  INT64CONST(1000000),
1637  INT64CONST(100000),
1638  INT64CONST(10000),
1639  INT64CONST(1000),
1640  INT64CONST(100),
1641  INT64CONST(10),
1642  INT64CONST(1)
1643  };
1644 
1645  static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1646  INT64CONST(500000),
1647  INT64CONST(50000),
1648  INT64CONST(5000),
1649  INT64CONST(500),
1650  INT64CONST(50),
1651  INT64CONST(5),
1652  INT64CONST(0)
1653  };
1654 
1655  if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1656  {
1657  if (*time >= INT64CONST(0))
1658  *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1659  TimeScales[typmod];
1660  else
1661  *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1662  TimeScales[typmod]);
1663  }
1664 }
#define MAX_TIME_PRECISION
Definition: date.h:51

References MAX_TIME_PRECISION.

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

◆ anytime_typmod_check()

int32 anytime_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 70 of file date.c.

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

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

Referenced by anytime_typmodin(), and transformSQLValueFunction().

◆ anytime_typmodin()

static int32 anytime_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 49 of file date.c.

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

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

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

References psprintf().

Referenced by timetypmodout(), and timetztypmodout().

◆ date2timestamp()

static TimestampTz date2timestamp ( DateADT  dateVal)
static

Definition at line 601 of file date.c.

602 {
603  return date2timestamp_opt_overflow(dateVal, NULL);
604 }
Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:557

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

714 {
715  double result;
716 
717  if (DATE_IS_NOBEGIN(dateVal))
718  result = -DBL_MAX;
719  else if (DATE_IS_NOEND(dateVal))
720  result = DBL_MAX;
721  else
722  {
723  /* date is days since 2000, timestamp is microseconds since same... */
724  result = dateVal * (double) USECS_PER_DAY;
725  }
726 
727  return result;
728 }
#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 557 of file date.c.

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

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

698 {
699  return date2timestamptz_opt_overflow(dateVal, NULL);
700 }
TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:617

References date2timestamptz_opt_overflow().

Referenced by date_timestamptz().

◆ date2timestamptz_opt_overflow()

TimestampTz date2timestamptz_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 617 of file date.c.

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

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

Referenced by date2timestamptz(), and date_cmp_timestamptz_internal().

◆ date_cmp()

Datum date_cmp ( PG_FUNCTION_ARGS  )

Definition at line 430 of file date.c.

431 {
432  DateADT dateVal1 = PG_GETARG_DATEADT(0);
433  DateADT dateVal2 = PG_GETARG_DATEADT(1);
434 
435  if (dateVal1 < dateVal2)
436  PG_RETURN_INT32(-1);
437  else if (dateVal1 > dateVal2)
438  PG_RETURN_INT32(1);
439  PG_RETURN_INT32(0);
440 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#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 807 of file date.c.

808 {
809  DateADT dateVal = PG_GETARG_DATEADT(0);
811 
813 }
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:736
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35

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

737 {
738  Timestamp dt1;
739  int overflow;
740 
741  dt1 = date2timestamp_opt_overflow(dateVal, &overflow);
742  if (overflow > 0)
743  {
744  /* dt1 is larger than any finite timestamp, but less than infinity */
745  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
746  }
747  Assert(overflow == 0); /* -1 case cannot occur */
748 
749  return timestamp_cmp_internal(dt1, dt2);
750 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2111
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:159
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 891 of file date.c.

892 {
893  DateADT dateVal = PG_GETARG_DATEADT(0);
895 
897 }
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:816
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

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

817 {
818  TimestampTz dt1;
819  int overflow;
820 
821  dt1 = date2timestamptz_opt_overflow(dateVal, &overflow);
822  if (overflow > 0)
823  {
824  /* dt1 is larger than any finite timestamp, but less than infinity */
825  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
826  }
827  if (overflow < 0)
828  {
829  /* dt1 is less than any finite timestamp, but more than -infinity */
830  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
831  }
832 
833  return timestamptz_cmp_internal(dt1, dt2);
834 }
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:154
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:101

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

377 {
378  DateADT dateVal1 = PG_GETARG_DATEADT(0);
379  DateADT dateVal2 = PG_GETARG_DATEADT(1);
380 
381  PG_RETURN_BOOL(dateVal1 == dateVal2);
382 }
#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 753 of file date.c.

754 {
755  DateADT dateVal = PG_GETARG_DATEADT(0);
757 
758  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) == 0);
759 }

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

838 {
839  DateADT dateVal = PG_GETARG_DATEADT(0);
841 
842  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) == 0);
843 }

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

453 {
455 
457 }
#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 421 of file date.c.

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

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

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

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

883 {
884  DateADT dateVal = PG_GETARG_DATEADT(0);
886 
887  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) >= 0);
888 }

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

413 {
414  DateADT dateVal1 = PG_GETARG_DATEADT(0);
415  DateADT dateVal2 = PG_GETARG_DATEADT(1);
416 
417  PG_RETURN_BOOL(dateVal1 > dateVal2);
418 }

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

781 {
782  DateADT dateVal = PG_GETARG_DATEADT(0);
784 
785  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) > 0);
786 }

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

865 {
866  DateADT dateVal = PG_GETARG_DATEADT(0);
868 
870 }

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

113 {
114  char *str = PG_GETARG_CSTRING(0);
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 
127  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
128  field, ftype, MAXDATEFIELDS, &nf);
129  if (dterr == 0)
130  dterr = DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp);
131  if (dterr != 0)
132  DateTimeParseError(dterr, str, "date");
133 
134  switch (dtype)
135  {
136  case DTK_DATE:
137  break;
138 
139  case DTK_EPOCH:
140  GetEpochTime(tm);
141  break;
142 
143  case DTK_LATE:
144  DATE_NOEND(date);
146 
147  case DTK_EARLY:
150 
151  default:
153  break;
154  }
155 
156  /* Prevent overflow in Julian-day routines */
158  ereport(ERROR,
159  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
160  errmsg("date out of range: \"%s\"", str)));
161 
163 
164  /* Now check for just-out-of-range dates */
165  if (!IS_VALID_DATE(date))
166  ereport(ERROR,
167  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
168  errmsg("date out of range: \"%s\"", str)));
169 
171 }
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:752
int date2j(int y, int m, int d)
Definition: datetime.c:284
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:973
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:4024
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2069
#define IS_VALID_DATE(d)
Definition: timestamp.h:229
int32 fsec_t
Definition: timestamp.h:41
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:194
#define PG_RETURN_DATEADT(x)
Definition: date.h:65
#define DATE_NOEND(j)
Definition: date.h:41
#define DATE_NOBEGIN(j)
Definition: date.h:39
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define MAXDATEFIELDS
Definition: datetime.h:203
#define DTK_EPOCH
Definition: datetime.h:153
#define DTK_LATE
Definition: datetime.h:152
#define DTK_DATE
Definition: datetime.h:145
#define DTERR_BAD_FORMAT
Definition: datetime.h:281
#define DTK_EARLY
Definition: datetime.h:151
#define MAXDATELEN
Definition: datetime.h:201

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

◆ date_larger()

Datum date_larger ( PG_FUNCTION_ARGS  )

Definition at line 460 of file date.c.

461 {
462  DateADT dateVal1 = PG_GETARG_DATEADT(0);
463  DateADT dateVal2 = PG_GETARG_DATEADT(1);
464 
465  PG_RETURN_DATEADT((dateVal1 > dateVal2) ? dateVal1 : dateVal2);
466 }

References PG_GETARG_DATEADT, and PG_RETURN_DATEADT.

◆ date_le()

Datum date_le ( PG_FUNCTION_ARGS  )

Definition at line 403 of file date.c.

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

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

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

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

874 {
875  DateADT dateVal = PG_GETARG_DATEADT(0);
877 
878  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) <= 0);
879 }

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

395 {
396  DateADT dateVal1 = PG_GETARG_DATEADT(0);
397  DateADT dateVal2 = PG_GETARG_DATEADT(1);
398 
399  PG_RETURN_BOOL(dateVal1 < dateVal2);
400 }

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

772 {
773  DateADT dateVal = PG_GETARG_DATEADT(0);
775 
776  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) < 0);
777 }

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

856 {
857  DateADT dateVal = PG_GETARG_DATEADT(0);
859 
861 }

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

481 {
482  DateADT dateVal1 = PG_GETARG_DATEADT(0);
483  DateADT dateVal2 = PG_GETARG_DATEADT(1);
484 
485  if (DATE_NOT_FINITE(dateVal1) || DATE_NOT_FINITE(dateVal2))
486  ereport(ERROR,
487  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
488  errmsg("cannot subtract infinite dates")));
489 
490  PG_RETURN_INT32((int32) (dateVal1 - dateVal2));
491 }

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

1260 {
1261  DateADT dateVal = PG_GETARG_DATEADT(0);
1262  Interval *span = PG_GETARG_INTERVAL_P(1);
1263  Timestamp dateStamp;
1264 
1265  dateStamp = date2timestamp(dateVal);
1266 
1268  TimestampGetDatum(dateStamp),
1269  PointerGetDatum(span));
1270 }
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2976
static TimestampTz date2timestamp(DateADT dateVal)
Definition: date.c:601
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:633
#define PointerGetDatum(X)
Definition: postgres.h:600
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define TimestampGetDatum(X)
Definition: timestamp.h:31

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

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

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

◆ date_ne()

Datum date_ne ( PG_FUNCTION_ARGS  )

Definition at line 385 of file date.c.

386 {
387  DateADT dateVal1 = PG_GETARG_DATEADT(0);
388  DateADT dateVal2 = PG_GETARG_DATEADT(1);
389 
390  PG_RETURN_BOOL(dateVal1 != dateVal2);
391 }

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

◆ date_ne_timestamp()

Datum date_ne_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 762 of file date.c.

763 {
764  DateADT dateVal = PG_GETARG_DATEADT(0);
766 
767  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt2) != 0);
768 }

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

847 {
848  DateADT dateVal = PG_GETARG_DATEADT(0);
850 
851  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt2) != 0);
852 }

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

178 {
180  char *result;
181  struct pg_tm tt,
182  *tm = &tt;
183  char buf[MAXDATELEN + 1];
184 
185  if (DATE_NOT_FINITE(date))
187  else
188  {
190  &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
192  }
193 
194  result = pstrdup(buf);
195  PG_RETURN_CSTRING(result);
196 }
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4143
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:287
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
int DateStyle
Definition: globals.c:119
char * pstrdup(const char *in)
Definition: mcxt.c:1305
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 1239 of file date.c.

1240 {
1241  DateADT dateVal = PG_GETARG_DATEADT(0);
1242  Interval *span = PG_GETARG_INTERVAL_P(1);
1243  Timestamp dateStamp;
1244 
1245  dateStamp = date2timestamp(dateVal);
1246 
1248  TimestampGetDatum(dateStamp),
1249  PointerGetDatum(span));
1250 }
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2897

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

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

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

Referenced by daterange_canonical().

◆ date_recv()

Datum date_recv ( PG_FUNCTION_ARGS  )

Definition at line 202 of file date.c.

203 {
205  DateADT result;
206 
207  result = (DateADT) pq_getmsgint(buf, sizeof(DateADT));
208 
209  /* Limit to the same range that date_in() accepts. */
210  if (DATE_NOT_FINITE(result))
211  /* ok */ ;
212  else if (!IS_VALID_DATE(result))
213  ereport(ERROR,
214  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
215  errmsg("date out of range")));
216 
217  PG_RETURN_DATEADT(result);
218 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
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 224 of file date.c.

225 {
228 
230  pq_sendint32(&buf, date);
232 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
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 469 of file date.c.

470 {
471  DateADT dateVal1 = PG_GETARG_DATEADT(0);
472  DateADT dateVal2 = PG_GETARG_DATEADT(1);
473 
474  PG_RETURN_DATEADT((dateVal1 < dateVal2) ? dateVal1 : dateVal2);
475 }

References PG_GETARG_DATEADT, and PG_RETURN_DATEADT.

◆ date_sortsupport()

Datum date_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 443 of file date.c.

444 {
446 
448  PG_RETURN_VOID();
449 }
#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:4926

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

1277 {
1278  DateADT dateVal = PG_GETARG_DATEADT(0);
1279  Timestamp result;
1280 
1281  result = date2timestamp(dateVal);
1282 
1283  PG_RETURN_TIMESTAMP(result);
1284 }
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39

References date2timestamp(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

◆ date_timestamptz()

Datum date_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 1320 of file date.c.

1321 {
1322  DateADT dateVal = PG_GETARG_DATEADT(0);
1323  TimestampTz result;
1324 
1325  result = date2timestamptz(dateVal);
1326 
1327  PG_RETURN_TIMESTAMP(result);
1328 }
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:697

References date2timestamptz(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

◆ datetime_timestamp()

Datum datetime_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1954 of file date.c.

1955 {
1957  TimeADT time = PG_GETARG_TIMEADT(1);
1958  Timestamp result;
1959 
1960  result = date2timestamp(date);
1961  if (!TIMESTAMP_NOT_FINITE(result))
1962  {
1963  result += time;
1964  if (!IS_VALID_TIMESTAMP(result))
1965  ereport(ERROR,
1966  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1967  errmsg("timestamp out of range")));
1968  }
1969 
1970  PG_RETURN_TIMESTAMP(result);
1971 }
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:161
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
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 2848 of file date.c.

2849 {
2851  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2852  TimestampTz result;
2853 
2854  if (DATE_IS_NOBEGIN(date))
2855  TIMESTAMP_NOBEGIN(result);
2856  else if (DATE_IS_NOEND(date))
2857  TIMESTAMP_NOEND(result);
2858  else
2859  {
2860  /*
2861  * Date's range is wider than timestamp's, so check for boundaries.
2862  * Since dates have the same minimum values as timestamps, only upper
2863  * boundary need be checked for overflow.
2864  */
2866  ereport(ERROR,
2867  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2868  errmsg("date out of range for timestamp")));
2869  result = date * USECS_PER_DAY + time->time + time->zone * USECS_PER_SEC;
2870 
2871  /*
2872  * Since it is possible to go beyond allowed timestamptz range because
2873  * of time zone, check for allowed timestamp range after adding tz.
2874  */
2875  if (!IS_VALID_TIMESTAMP(result))
2876  ereport(ERROR,
2877  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2878  errmsg("date out of range for timestamp")));
2879  }
2880 
2881  PG_RETURN_TIMESTAMP(result);
2882 }
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
Definition: date.h:28
TimeADT time
Definition: date.h:29
int32 zone
Definition: date.h:30

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

◆ EncodeSpecialDate()

void EncodeSpecialDate ( DateADT  dt,
char *  str 
)

Definition at line 287 of file date.c.

288 {
289  if (DATE_IS_NOBEGIN(dt))
290  strcpy(str, EARLY);
291  else if (DATE_IS_NOEND(dt))
292  strcpy(str, LATE);
293  else /* shouldn't happen */
294  elog(ERROR, "invalid argument for EncodeSpecialDate");
295 }
#define elog(elevel,...)
Definition: elog.h:218
#define EARLY
Definition: datetime.h:40
#define LATE
Definition: datetime.h:41

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

1060 {
1061  text *units = PG_GETARG_TEXT_PP(0);
1063  int64 intresult;
1064  int type,
1065  val;
1066  char *lowunits;
1067  int year,
1068  mon,
1069  mday;
1070 
1071  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
1072  VARSIZE_ANY_EXHDR(units),
1073  false);
1074 
1075  type = DecodeUnits(0, lowunits, &val);
1076  if (type == UNKNOWN_FIELD)
1077  type = DecodeSpecial(0, lowunits, &val);
1078 
1079  if (DATE_NOT_FINITE(date) && (type == UNITS || type == RESERV))
1080  {
1081  switch (val)
1082  {
1083  /* Oscillating units */
1084  case DTK_DAY:
1085  case DTK_MONTH:
1086  case DTK_QUARTER:
1087  case DTK_WEEK:
1088  case DTK_DOW:
1089  case DTK_ISODOW:
1090  case DTK_DOY:
1091  PG_RETURN_NULL();
1092  break;
1093 
1094  /* Monotonically-increasing units */
1095  case DTK_YEAR:
1096  case DTK_DECADE:
1097  case DTK_CENTURY:
1098  case DTK_MILLENNIUM:
1099  case DTK_JULIAN:
1100  case DTK_ISOYEAR:
1101  case DTK_EPOCH:
1102  if (DATE_IS_NOBEGIN(date))
1104  CStringGetDatum("-Infinity"),
1106  Int32GetDatum(-1))));
1107  else
1109  CStringGetDatum("Infinity"),
1111  Int32GetDatum(-1))));
1112  default:
1113  ereport(ERROR,
1114  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1115  errmsg("unit \"%s\" not supported for type %s",
1116  lowunits, format_type_be(DATEOID))));
1117  }
1118  }
1119  else if (type == UNITS)
1120  {
1121  j2date(date + POSTGRES_EPOCH_JDATE, &year, &mon, &mday);
1122 
1123  switch (val)
1124  {
1125  case DTK_DAY:
1126  intresult = mday;
1127  break;
1128 
1129  case DTK_MONTH:
1130  intresult = mon;
1131  break;
1132 
1133  case DTK_QUARTER:
1134  intresult = (mon - 1) / 3 + 1;
1135  break;
1136 
1137  case DTK_WEEK:
1138  intresult = date2isoweek(year, mon, mday);
1139  break;
1140 
1141  case DTK_YEAR:
1142  if (year > 0)
1143  intresult = year;
1144  else
1145  /* there is no year 0, just 1 BC and 1 AD */
1146  intresult = year - 1;
1147  break;
1148 
1149  case DTK_DECADE:
1150  /* see comments in timestamp_part */
1151  if (year >= 0)
1152  intresult = year / 10;
1153  else
1154  intresult = -((8 - (year - 1)) / 10);
1155  break;
1156 
1157  case DTK_CENTURY:
1158  /* see comments in timestamp_part */
1159  if (year > 0)
1160  intresult = (year + 99) / 100;
1161  else
1162  intresult = -((99 - (year - 1)) / 100);
1163  break;
1164 
1165  case DTK_MILLENNIUM:
1166  /* see comments in timestamp_part */
1167  if (year > 0)
1168  intresult = (year + 999) / 1000;
1169  else
1170  intresult = -((999 - (year - 1)) / 1000);
1171  break;
1172 
1173  case DTK_JULIAN:
1174  intresult = date + POSTGRES_EPOCH_JDATE;
1175  break;
1176 
1177  case DTK_ISOYEAR:
1178  intresult = date2isoyear(year, mon, mday);
1179  /* Adjust BC years */
1180  if (intresult <= 0)
1181  intresult -= 1;
1182  break;
1183 
1184  case DTK_DOW:
1185  case DTK_ISODOW:
1186  intresult = j2day(date + POSTGRES_EPOCH_JDATE);
1187  if (val == DTK_ISODOW && intresult == 0)
1188  intresult = 7;
1189  break;
1190 
1191  case DTK_DOY:
1192  intresult = date2j(year, mon, mday) - date2j(year, 1, 1) + 1;
1193  break;
1194 
1195  default:
1196  ereport(ERROR,
1197  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1198  errmsg("unit \"%s\" not supported for type %s",
1199  lowunits, format_type_be(DATEOID))));
1200  intresult = 0;
1201  }
1202  }
1203  else if (type == RESERV)
1204  {
1205  switch (val)
1206  {
1207  case DTK_EPOCH:
1208  intresult = ((int64) date + POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY;
1209  break;
1210 
1211  default:
1212  ereport(ERROR,
1213  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1214  errmsg("unit \"%s\" not supported for type %s",
1215  lowunits, format_type_be(DATEOID))));
1216  intresult = 0;
1217  }
1218  }
1219  else
1220  {
1221  ereport(ERROR,
1222  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1223  errmsg("unit \"%s\" not recognized for type %s",
1224  lowunits, format_type_be(DATEOID))));
1225  intresult = 0;
1226  }
1227 
1228  PG_RETURN_NUMERIC(int64_to_numeric(intresult));
1229 }
int j2day(int date)
Definition: datetime.c:342
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3280
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3987
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4137
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:617
int date2isoweek(int year, int mon, int mday)
Definition: timestamp.c:4475
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4530
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:201
#define SECS_PER_DAY
Definition: timestamp.h:125
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:635
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#define UNKNOWN_FIELD
Definition: datetime.h:125
#define DTK_DECADE
Definition: datetime.h:169
#define DTK_QUARTER
Definition: datetime.h:167
#define DTK_JULIAN
Definition: datetime.h:174
#define DTK_CENTURY
Definition: datetime.h:170
#define DTK_ISODOW
Definition: datetime.h:181
#define DTK_DAY
Definition: datetime.h:164
#define RESERV
Definition: datetime.h:91
#define DTK_MILLENNIUM
Definition: datetime.h:171
#define DTK_ISOYEAR
Definition: datetime.h:180
#define DTK_DOY
Definition: datetime.h:177
#define DTK_WEEK
Definition: datetime.h:165
#define DTK_DOW
Definition: datetime.h:176
#define DTK_YEAR
Definition: datetime.h:168
#define DTK_MONTH
Definition: datetime.h:166
#define UNITS
Definition: datetime.h:108
long val
Definition: informix.c:664
#define DatumGetNumeric(X)
Definition: numeric.h:59
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:64
#define CStringGetDatum(X)
Definition: postgres.h:622
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define Int32GetDatum(X)
Definition: postgres.h:523
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
#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:622

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

2230 {
2231  return time_part_common(fcinfo, true);
2232 }
static Datum time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2120

References time_part_common().

◆ extract_timetz()

Datum extract_timetz ( PG_FUNCTION_ARGS  )

Definition at line 3012 of file date.c.

3013 {
3014  return timetz_part_common(fcinfo, true);
3015 }
static Datum timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2889

References timetz_part_common().

◆ float_time_overflows()

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

Definition at line 1439 of file date.c.

1440 {
1441  /* Range-check the fields individually. */
1442  if (hour < 0 || hour > HOURS_PER_DAY ||
1443  min < 0 || min >= MINS_PER_HOUR)
1444  return true;
1445 
1446  /*
1447  * "sec", being double, requires extra care. Cope with NaN, and round off
1448  * before applying the range check to avoid unexpected errors due to
1449  * imprecise input. (We assume rint() behaves sanely with infinities.)
1450  */
1451  if (isnan(sec))
1452  return true;
1453  sec = rint(sec * USECS_PER_SEC);
1454  if (sec < 0 || sec > SECS_PER_MINUTE * USECS_PER_SEC)
1455  return true;
1456 
1457  /*
1458  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1459  * that the total time value doesn't exceed 24:00:00. This must match the
1460  * way that callers will convert the fields to a time.
1461  */
1462  if (((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1463  * USECS_PER_SEC) + (int64) sec) > USECS_PER_DAY)
1464  return true;
1465 
1466  return false;
1467 }
#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().

◆ GetSQLCurrentDate()

DateADT GetSQLCurrentDate ( void  )

Definition at line 302 of file date.c.

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

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

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLCurrentTime()

TimeTzADT* GetSQLCurrentTime ( int32  typmod)

Definition at line 335 of file date.c.

336 {
337  TimeTzADT *result;
338  struct pg_tm tt,
339  *tm = &tt;
340  fsec_t fsec;
341  int tz;
342 
343  GetCurrentTimeUsec(tm, &fsec, &tz);
344 
345  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
346  tm2timetz(tm, fsec, tz, result);
347  AdjustTimeForTypmod(&(result->time), typmod);
348  return result;
349 }
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:385
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2243
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1633
void * palloc(Size size)
Definition: mcxt.c:1068

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

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLLocalTime()

TimeADT GetSQLLocalTime ( int32  typmod)

Definition at line 355 of file date.c.

356 {
357  TimeADT result;
358  struct pg_tm tt,
359  *tm = &tt;
360  fsec_t fsec;
361  int tz;
362 
363  GetCurrentTimeUsec(tm, &fsec, &tz);
364 
365  tm2time(tm, fsec, &result);
366  AdjustTimeForTypmod(&result, typmod);
367  return result;
368 }
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1404

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

Referenced by ExecEvalSQLValueFunction().

◆ in_range_date_interval()

Datum in_range_date_interval ( PG_FUNCTION_ARGS  )

Definition at line 1032 of file date.c.

1033 {
1035  DateADT base = PG_GETARG_DATEADT(1);
1036  Interval *offset = PG_GETARG_INTERVAL_P(2);
1037  bool sub = PG_GETARG_BOOL(3);
1038  bool less = PG_GETARG_BOOL(4);
1039  Timestamp valStamp;
1040  Timestamp baseStamp;
1041 
1042  /* XXX we could support out-of-range cases here, perhaps */
1043  valStamp = date2timestamp(val);
1044  baseStamp = date2timestamp(base);
1045 
1047  TimestampGetDatum(valStamp),
1048  TimestampGetDatum(baseStamp),
1049  IntervalPGetDatum(offset),
1050  BoolGetDatum(sub),
1051  BoolGetDatum(less));
1052 }
Datum in_range_timestamp_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3405
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:639
#define BoolGetDatum(X)
Definition: postgres.h:446
#define IntervalPGetDatum(X)
Definition: timestamp.h:33

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

2081 {
2083  TimeADT base = PG_GETARG_TIMEADT(1);
2084  Interval *offset = PG_GETARG_INTERVAL_P(2);
2085  bool sub = PG_GETARG_BOOL(3);
2086  bool less = PG_GETARG_BOOL(4);
2087  TimeADT sum;
2088 
2089  /*
2090  * Like time_pl_interval/time_mi_interval, we disregard the month and day
2091  * fields of the offset. So our test for negative should too.
2092  */
2093  if (offset->time < 0)
2094  ereport(ERROR,
2095  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2096  errmsg("invalid preceding or following size in window function")));
2097 
2098  /*
2099  * We can't use time_pl_interval/time_mi_interval here, because their
2100  * wraparound behavior would give wrong (or at least undesirable) answers.
2101  * Fortunately the equivalent non-wrapping behavior is trivial, especially
2102  * since we don't worry about integer overflow.
2103  */
2104  if (sub)
2105  sum = base - offset->time;
2106  else
2107  sum = base + offset->time;
2108 
2109  if (less)
2110  PG_RETURN_BOOL(val <= sum);
2111  else
2112  PG_RETURN_BOOL(val >= sum);
2113 }
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 2614 of file date.c.

2615 {
2617  TimeTzADT *base = PG_GETARG_TIMETZADT_P(1);
2618  Interval *offset = PG_GETARG_INTERVAL_P(2);
2619  bool sub = PG_GETARG_BOOL(3);
2620  bool less = PG_GETARG_BOOL(4);
2621  TimeTzADT sum;
2622 
2623  /*
2624  * Like timetz_pl_interval/timetz_mi_interval, we disregard the month and
2625  * day fields of the offset. So our test for negative should too.
2626  */
2627  if (offset->time < 0)
2628  ereport(ERROR,
2629  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2630  errmsg("invalid preceding or following size in window function")));
2631 
2632  /*
2633  * We can't use timetz_pl_interval/timetz_mi_interval here, because their
2634  * wraparound behavior would give wrong (or at least undesirable) answers.
2635  * Fortunately the equivalent non-wrapping behavior is trivial, especially
2636  * since we don't worry about integer overflow.
2637  */
2638  if (sub)
2639  sum.time = base->time - offset->time;
2640  else
2641  sum.time = base->time + offset->time;
2642  sum.zone = base->zone;
2643 
2644  if (less)
2645  PG_RETURN_BOOL(timetz_cmp_internal(val, &sum) <= 0);
2646  else
2647  PG_RETURN_BOOL(timetz_cmp_internal(val, &sum) >= 0);
2648 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2417

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

2001 {
2002  Interval *span = PG_GETARG_INTERVAL_P(0);
2003  TimeADT result;
2004  int64 days;
2005 
2006  result = span->time;
2007  if (result >= USECS_PER_DAY)
2008  {
2009  days = result / USECS_PER_DAY;
2010  result -= days * USECS_PER_DAY;
2011  }
2012  else if (result < 0)
2013  {
2014  days = (-result + USECS_PER_DAY - 1) / USECS_PER_DAY;
2015  result += days * USECS_PER_DAY;
2016  }
2017 
2018  PG_RETURN_TIMEADT(result);
2019 }
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66

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

239 {
240  struct pg_tm tm;
241  DateADT date;
242  int dterr;
243  bool bc = false;
244 
248 
249  /* Handle negative years as BC */
250  if (tm.tm_year < 0)
251  {
252  bc = true;
253  tm.tm_year = -tm.tm_year;
254  }
255 
256  dterr = ValidateDate(DTK_DATE_M, false, false, bc, &tm);
257 
258  if (dterr != 0)
259  ereport(ERROR,
260  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
261  errmsg("date field value out of range: %d-%02d-%02d",
262  tm.tm_year, tm.tm_mon, tm.tm_mday)));
263 
264  /* Prevent overflow in Julian-day routines */
266  ereport(ERROR,
267  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
268  errmsg("date out of range: %d-%02d-%02d",
269  tm.tm_year, tm.tm_mon, tm.tm_mday)));
270 
272 
273  /* Now check for just-out-of-range dates */
274  if (!IS_VALID_DATE(date))
275  ereport(ERROR,
276  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
277  errmsg("date out of range: %d-%02d-%02d",
278  tm.tm_year, tm.tm_mon, tm.tm_mday)));
279 
281 }
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2648
#define DTK_DATE_M
Definition: datetime.h:192

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

1566 {
1567  int tm_hour = PG_GETARG_INT32(0);
1568  int tm_min = PG_GETARG_INT32(1);
1569  double sec = PG_GETARG_FLOAT8(2);
1570  TimeADT time;
1571 
1572  /* Check for time overflow */
1573  if (float_time_overflows(tm_hour, tm_min, sec))
1574  ereport(ERROR,
1575  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
1576  errmsg("time field value out of range: %d:%02d:%02g",
1577  tm_hour, tm_min, sec)));
1578 
1579  /* This should match tm2time */
1580  time = (((tm_hour * MINS_PER_HOUR + tm_min) * SECS_PER_MINUTE)
1581  * USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
1582 
1583  PG_RETURN_TIMEADT(time);
1584 }
bool float_time_overflows(int hour, int min, double sec)
Definition: date.c:1439
#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 1771 of file date.c.

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

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

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

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

◆ time2tm()

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

Definition at line 1476 of file date.c.

1477 {
1478  tm->tm_hour = time / USECS_PER_HOUR;
1479  time -= tm->tm_hour * USECS_PER_HOUR;
1480  tm->tm_min = time / USECS_PER_MINUTE;
1481  time -= tm->tm_min * USECS_PER_MINUTE;
1482  tm->tm_sec = time / USECS_PER_SEC;
1483  time -= tm->tm_sec * USECS_PER_SEC;
1484  *fsec = time;
1485  return 0;
1486 }
#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 1722 of file date.c.

1723 {
1724  TimeADT time1 = PG_GETARG_TIMEADT(0);
1725  TimeADT time2 = PG_GETARG_TIMEADT(1);
1726 
1727  if (time1 < time2)
1728  PG_RETURN_INT32(-1);
1729  if (time1 > time2)
1730  PG_RETURN_INT32(1);
1731  PG_RETURN_INT32(0);
1732 }

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

1669 {
1670  TimeADT time1 = PG_GETARG_TIMEADT(0);
1671  TimeADT time2 = PG_GETARG_TIMEADT(1);
1672 
1673  PG_RETURN_BOOL(time1 == time2);
1674 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timeeq().

◆ time_ge()

Datum time_ge ( PG_FUNCTION_ARGS  )

Definition at line 1713 of file date.c.

1714 {
1715  TimeADT time1 = PG_GETARG_TIMEADT(0);
1716  TimeADT time2 = PG_GETARG_TIMEADT(1);
1717 
1718  PG_RETURN_BOOL(time1 >= time2);
1719 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timege().

◆ time_gt()

Datum time_gt ( PG_FUNCTION_ARGS  )

Definition at line 1704 of file date.c.

1705 {
1706  TimeADT time1 = PG_GETARG_TIMEADT(0);
1707  TimeADT time2 = PG_GETARG_TIMEADT(1);
1708 
1709  PG_RETURN_BOOL(time1 > time2);
1710 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timegt().

◆ time_hash()

Datum time_hash ( PG_FUNCTION_ARGS  )

Definition at line 1735 of file date.c.

1736 {
1737  return hashint8(fcinfo);
1738 }
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:84

References hashint8().

◆ time_hash_extended()

Datum time_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 1741 of file date.c.

1742 {
1743  return hashint8extended(fcinfo);
1744 }
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:104

References hashint8extended().

◆ time_in()

Datum time_in ( PG_FUNCTION_ARGS  )

Definition at line 1367 of file date.c.

1368 {
1369  char *str = PG_GETARG_CSTRING(0);
1370 
1371 #ifdef NOT_USED
1372  Oid typelem = PG_GETARG_OID(1);
1373 #endif
1374  int32 typmod = PG_GETARG_INT32(2);
1375  TimeADT result;
1376  fsec_t fsec;
1377  struct pg_tm tt,
1378  *tm = &tt;
1379  int tz;
1380  int nf;
1381  int dterr;
1382  char workbuf[MAXDATELEN + 1];
1383  char *field[MAXDATEFIELDS];
1384  int dtype;
1385  int ftype[MAXDATEFIELDS];
1386 
1387  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
1388  field, ftype, MAXDATEFIELDS, &nf);
1389  if (dterr == 0)
1390  dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
1391  if (dterr != 0)
1392  DateTimeParseError(dterr, str, "time");
1393 
1394  tm2time(tm, fsec, &result);
1395  AdjustTimeForTypmod(&result, typmod);
1396 
1397  PG_RETURN_TIMEADT(result);
1398 }
int DecodeTimeOnly(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:1923
#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_TIMEADT, generate_unaccent_rules::str, tm, and tm2time().

◆ time_interval()

Datum time_interval ( PG_FUNCTION_ARGS  )

Definition at line 1977 of file date.c.

1978 {
1979  TimeADT time = PG_GETARG_TIMEADT(0);
1980  Interval *result;
1981 
1982  result = (Interval *) palloc(sizeof(Interval));
1983 
1984  result->time = time;
1985  result->day = 0;
1986  result->month = 0;
1987 
1988  PG_RETURN_INTERVAL_P(result);
1989 }
int32 day
Definition: timestamp.h:51
int32 month
Definition: timestamp.h:52
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41

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

1748 {
1749  TimeADT time1 = PG_GETARG_TIMEADT(0);
1750  TimeADT time2 = PG_GETARG_TIMEADT(1);
1751 
1752  PG_RETURN_TIMEADT((time1 > time2) ? time1 : time2);
1753 }

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

◆ time_le()

Datum time_le ( PG_FUNCTION_ARGS  )

Definition at line 1695 of file date.c.

1696 {
1697  TimeADT time1 = PG_GETARG_TIMEADT(0);
1698  TimeADT time2 = PG_GETARG_TIMEADT(1);
1699 
1700  PG_RETURN_BOOL(time1 <= time2);
1701 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timele().

◆ time_lt()

Datum time_lt ( PG_FUNCTION_ARGS  )

Definition at line 1686 of file date.c.

1687 {
1688  TimeADT time1 = PG_GETARG_TIMEADT(0);
1689  TimeADT time2 = PG_GETARG_TIMEADT(1);
1690 
1691  PG_RETURN_BOOL(time1 < time2);
1692 }

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

2063 {
2064  TimeADT time = PG_GETARG_TIMEADT(0);
2065  Interval *span = PG_GETARG_INTERVAL_P(1);
2066  TimeADT result;
2067 
2068  result = time - span->time;
2069  result -= result / USECS_PER_DAY * USECS_PER_DAY;
2070  if (result < INT64CONST(0))
2071  result += USECS_PER_DAY;
2072 
2073  PG_RETURN_TIMEADT(result);
2074 }

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

2026 {
2027  TimeADT time1 = PG_GETARG_TIMEADT(0);
2028  TimeADT time2 = PG_GETARG_TIMEADT(1);
2029  Interval *result;
2030 
2031  result = (Interval *) palloc(sizeof(Interval));
2032 
2033  result->month = 0;
2034  result->day = 0;
2035  result->time = time1 - time2;
2036 
2037  PG_RETURN_INTERVAL_P(result);
2038 }

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

1678 {
1679  TimeADT time1 = PG_GETARG_TIMEADT(0);
1680  TimeADT time2 = PG_GETARG_TIMEADT(1);
1681 
1682  PG_RETURN_BOOL(time1 != time2);
1683 }

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

◆ time_out()

Datum time_out ( PG_FUNCTION_ARGS  )

Definition at line 1489 of file date.c.

1490 {
1491  TimeADT time = PG_GETARG_TIMEADT(0);
1492  char *result;
1493  struct pg_tm tt,
1494  *tm = &tt;
1495  fsec_t fsec;
1496  char buf[MAXDATELEN + 1];
1497 
1498  time2tm(time, tm, &fsec);
1499  EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf);
1500 
1501  result = pstrdup(buf);
1502  PG_RETURN_CSTRING(result);
1503 }
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:4228
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1476

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

1416 {
1417  /* Range-check the fields individually. */
1418  if (hour < 0 || hour > HOURS_PER_DAY ||
1419  min < 0 || min >= MINS_PER_HOUR ||
1420  sec < 0 || sec > SECS_PER_MINUTE ||
1421  fsec < 0 || fsec > USECS_PER_SEC)
1422  return true;
1423 
1424  /*
1425  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1426  * that the total time value doesn't exceed 24:00:00.
1427  */
1428  if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1429  + sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1430  return true;
1431 
1432  return false;
1433 }

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

2224 {
2225  return time_part_common(fcinfo, false);
2226 }

References time_part_common().

◆ time_part_common()

static Datum time_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2120 of file date.c.

2121 {
2122  text *units = PG_GETARG_TEXT_PP(0);
2123  TimeADT time = PG_GETARG_TIMEADT(1);
2124  int64 intresult;
2125  int type,
2126  val;
2127  char *lowunits;
2128 
2129  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
2130  VARSIZE_ANY_EXHDR(units),
2131  false);
2132 
2133  type = DecodeUnits(0, lowunits, &val);
2134  if (type == UNKNOWN_FIELD)
2135  type = DecodeSpecial(0, lowunits, &val);
2136 
2137  if (type == UNITS)
2138  {
2139  fsec_t fsec;
2140  struct pg_tm tt,
2141  *tm = &tt;
2142 
2143  time2tm(time, tm, &fsec);
2144 
2145  switch (val)
2146  {
2147  case DTK_MICROSEC:
2148  intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
2149  break;
2150 
2151  case DTK_MILLISEC:
2152  if (retnumeric)
2153  /*---
2154  * tm->tm_sec * 1000 + fsec / 1000
2155  * = (tm->tm_sec * 1'000'000 + fsec) / 1000
2156  */
2157  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
2158  else
2159  PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
2160  break;
2161 
2162  case DTK_SECOND:
2163  if (retnumeric)
2164  /*---
2165  * tm->tm_sec + fsec / 1'000'000
2166  * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
2167  */
2168  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
2169  else
2170  PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
2171  break;
2172 
2173  case DTK_MINUTE:
2174  intresult = tm->tm_min;
2175  break;
2176 
2177  case DTK_HOUR:
2178  intresult = tm->tm_hour;
2179  break;
2180 
2181  case DTK_TZ:
2182  case DTK_TZ_MINUTE:
2183  case DTK_TZ_HOUR:
2184  case DTK_DAY:
2185  case DTK_MONTH:
2186  case DTK_QUARTER:
2187  case DTK_YEAR:
2188  case DTK_DECADE:
2189  case DTK_CENTURY:
2190  case DTK_MILLENNIUM:
2191  case DTK_ISOYEAR:
2192  default:
2193  ereport(ERROR,
2194  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2195  errmsg("unit \"%s\" not supported for type %s",
2196  lowunits, format_type_be(TIMEOID))));
2197  intresult = 0;
2198  }
2199  }
2200  else if (type == RESERV && val == DTK_EPOCH)
2201  {
2202  if (retnumeric)
2204  else
2205  PG_RETURN_FLOAT8(time / 1000000.0);
2206  }
2207  else
2208  {
2209  ereport(ERROR,
2210  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2211  errmsg("unit \"%s\" not recognized for type %s",
2212  lowunits, format_type_be(TIMEOID))));
2213  intresult = 0;
2214  }
2215 
2216  if (retnumeric)
2217  PG_RETURN_NUMERIC(int64_to_numeric(intresult));
2218  else
2219  PG_RETURN_FLOAT8(intresult);
2220 }
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4158
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define DTK_SECOND
Definition: datetime.h:161
#define DTK_TZ_HOUR
Definition: datetime.h:178
#define DTK_TZ_MINUTE
Definition: datetime.h:179
#define DTK_TZ
Definition: datetime.h:147
#define DTK_HOUR
Definition: datetime.h:163
#define DTK_MICROSEC
Definition: datetime.h:173
#define DTK_MILLISEC
Definition: datetime.h:172
#define DTK_MINUTE
Definition: datetime.h:162

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

2045 {
2046  TimeADT time = PG_GETARG_TIMEADT(0);
2047  Interval *span = PG_GETARG_INTERVAL_P(1);
2048  TimeADT result;
2049 
2050  result = time + span->time;
2051  result -= result / USECS_PER_DAY * USECS_PER_DAY;
2052  if (result < INT64CONST(0))
2053  result += USECS_PER_DAY;
2054 
2055  PG_RETURN_TIMEADT(result);
2056 }

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

1510 {
1512 
1513 #ifdef NOT_USED
1514  Oid typelem = PG_GETARG_OID(1);
1515 #endif
1516  int32 typmod = PG_GETARG_INT32(2);
1517  TimeADT result;
1518 
1519  result = pq_getmsgint64(buf);
1520 
1521  if (result < INT64CONST(0) || result > USECS_PER_DAY)
1522  ereport(ERROR,
1523  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1524  errmsg("time out of range")));
1525 
1526  AdjustTimeForTypmod(&result, typmod);
1527 
1528  PG_RETURN_TIMEADT(result);
1529 }
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455

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

1614 {
1615  TimeADT time = PG_GETARG_TIMEADT(0);
1616  int32 typmod = PG_GETARG_INT32(1);
1617  TimeADT result;
1618 
1619  result = time;
1620  AdjustTimeForTypmod(&result, typmod);
1621 
1622  PG_RETURN_TIMEADT(result);
1623 }

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

◆ time_send()

Datum time_send ( PG_FUNCTION_ARGS  )

Definition at line 1535 of file date.c.

1536 {
1537  TimeADT time = PG_GETARG_TIMEADT(0);
1539 
1540  pq_begintypsend(&buf);
1541  pq_sendint64(&buf, time);
1543 }
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 1756 of file date.c.

1757 {
1758  TimeADT time1 = PG_GETARG_TIMEADT(0);
1759  TimeADT time2 = PG_GETARG_TIMEADT(1);
1760 
1761  PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
1762 }

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

◆ time_support()

Datum time_support ( PG_FUNCTION_ARGS  )

Definition at line 1593 of file date.c.

1594 {
1595  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1596  Node *ret = NULL;
1597 
1598  if (IsA(rawreq, SupportRequestSimplify))
1599  {
1601 
1602  ret = TemporalSimplify(MAX_TIME_PRECISION, (Node *) req->fcall);
1603  }
1604 
1605  PG_RETURN_POINTER(ret);
1606 }
Node * TemporalSimplify(int32 max_precis, Node *node)
Definition: datetime.c:4756
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define IsA(nodeptr, _type_)
Definition: nodes.h:624
Definition: nodes.h:574

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

2791 {
2792  TimeADT time = PG_GETARG_TIMEADT(0);
2793  TimeTzADT *result;
2794  struct pg_tm tt,
2795  *tm = &tt;
2796  fsec_t fsec;
2797  int tz;
2798 
2800  time2tm(time, tm, &fsec);
2802 
2803  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2804 
2805  result->time = time;
2806  result->zone = tz;
2807 
2808  PG_RETURN_TIMETZADT_P(result);
2809 }
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67

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

955 {
957  DateADT dateVal = PG_GETARG_DATEADT(1);
958 
960 }

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

1291 {
1293  DateADT result;
1294  struct pg_tm tt,
1295  *tm = &tt;
1296  fsec_t fsec;
1297 
1299  DATE_NOBEGIN(result);
1300  else if (TIMESTAMP_IS_NOEND(timestamp))
1301  DATE_NOEND(result);
1302  else
1303  {
1304  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1305  ereport(ERROR,
1306  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1307  errmsg("timestamp out of range")));
1308 
1310  }
1311 
1312  PG_RETURN_DATEADT(result);
1313 }
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1816
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 900 of file date.c.

901 {
903  DateADT dateVal = PG_GETARG_DATEADT(1);
904 
905  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) == 0);
906 }

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

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

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

928 {
930  DateADT dateVal = PG_GETARG_DATEADT(1);
931 
932  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) < 0);
933 }

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

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

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

919 {
921  DateADT dateVal = PG_GETARG_DATEADT(1);
922 
923  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) > 0);
924 }

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

910 {
912  DateADT dateVal = PG_GETARG_DATEADT(1);
913 
914  PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) != 0);
915 }

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

1894 {
1896  TimeADT result;
1897  struct pg_tm tt,
1898  *tm = &tt;
1899  fsec_t fsec;
1900 
1902  PG_RETURN_NULL();
1903 
1904  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1905  ereport(ERROR,
1906  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1907  errmsg("timestamp out of range")));
1908 
1909  /*
1910  * Could also do this with time = (timestamp / USECS_PER_DAY *
1911  * USECS_PER_DAY) - timestamp;
1912  */
1913  result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1914  USECS_PER_SEC) + fsec;
1915 
1916  PG_RETURN_TIMEADT(result);
1917 }

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

1018 {
1020  DateADT dateVal = PG_GETARG_DATEADT(1);
1021 
1023 }

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

1336 {
1338  DateADT result;
1339  struct pg_tm tt,
1340  *tm = &tt;
1341  fsec_t fsec;
1342  int tz;
1343 
1345  DATE_NOBEGIN(result);
1346  else if (TIMESTAMP_IS_NOEND(timestamp))
1347  DATE_NOEND(result);
1348  else
1349  {
1350  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1351  ereport(ERROR,
1352  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1353  errmsg("timestamp out of range")));
1354 
1356  }
1357 
1358  PG_RETURN_DATEADT(result);
1359 }

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

964 {
966  DateADT dateVal = PG_GETARG_DATEADT(1);
967 
968  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) == 0);
969 }

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

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

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

991 {
993  DateADT dateVal = PG_GETARG_DATEADT(1);
994 
996 }

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

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

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

982 {
984  DateADT dateVal = PG_GETARG_DATEADT(1);
985 
987 }

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

973 {
975  DateADT dateVal = PG_GETARG_DATEADT(1);
976 
977  PG_RETURN_BOOL(date_cmp_timestamptz_internal(dateVal, dt1) != 0);
978 }

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

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

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

2817 {
2819  TimeTzADT *result;
2820  struct pg_tm tt,
2821  *tm = &tt;
2822  int tz;
2823  fsec_t fsec;
2824 
2826  PG_RETURN_NULL();
2827 
2828  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
2829  ereport(ERROR,
2830  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2831  errmsg("timestamp out of range")));
2832 
2833  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2834 
2835  tm2timetz(tm, fsec, tz, result);
2836 
2837  PG_RETURN_TIMETZADT_P(result);
2838 }

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

1547 {
1549 
1550  PG_RETURN_INT32(anytime_typmodin(false, ta));
1551 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
static int32 anytime_typmodin(bool istz, ArrayType *ta)
Definition: date.c:49

References anytime_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timetypmodout()

Datum timetypmodout ( PG_FUNCTION_ARGS  )

Definition at line 1554 of file date.c.

1555 {
1556  int32 typmod = PG_GETARG_INT32(0);
1557 
1558  PG_RETURN_CSTRING(anytime_typmodout(false, typmod));
1559 }
static char * anytime_typmodout(bool istz, int32 typmod)
Definition: date.c:92

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

2378 {
2379  TimeOffset trem = time->time;
2380 
2381  tm->tm_hour = trem / USECS_PER_HOUR;
2382  trem -= tm->tm_hour * USECS_PER_HOUR;
2383  tm->tm_min = trem / USECS_PER_MINUTE;
2384  trem -= tm->tm_min * USECS_PER_MINUTE;
2385  tm->tm_sec = trem / USECS_PER_SEC;
2386  *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2387 
2388  if (tzp != NULL)
2389  *tzp = time->zone;
2390 
2391  return 0;
2392 }
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 2498 of file date.c.

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

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

2418 {
2419  TimeOffset t1,
2420  t2;
2421 
2422  /* Primary sort is by true (GMT-equivalent) time */
2423  t1 = time1->time + (time1->zone * USECS_PER_SEC);
2424  t2 = time2->time + (time2->zone * USECS_PER_SEC);
2425 
2426  if (t1 > t2)
2427  return 1;
2428  if (t1 < t2)
2429  return -1;
2430 
2431  /*
2432  * If same GMT time, sort by timezone; we only want to say that two
2433  * timetz's are equal if both the time and zone parts are equal.
2434  */
2435  if (time1->zone > time2->zone)
2436  return 1;
2437  if (time1->zone < time2->zone)
2438  return -1;
2439 
2440  return 0;
2441 }

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

2445 {
2446  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2447  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2448 
2449  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) == 0);
2450 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_ge()

Datum timetz_ge ( PG_FUNCTION_ARGS  )

Definition at line 2489 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_gt()

Datum timetz_gt ( PG_FUNCTION_ARGS  )

Definition at line 2480 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_hash()

Datum timetz_hash ( PG_FUNCTION_ARGS  )

Definition at line 2507 of file date.c.

2508 {
2510  uint32 thash;
2511 
2512  /*
2513  * To avoid any problems with padding bytes in the struct, we figure the
2514  * field hashes separately and XOR them.
2515  */
2517  Int64GetDatumFast(key->time)));
2518  thash ^= DatumGetUInt32(hash_uint32(key->zone));
2519  PG_RETURN_UINT32(thash);
2520 }
unsigned int uint32
Definition: c.h:441
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:355
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:631
static Datum hash_uint32(uint32 k)
Definition: hashfn.h:43
#define Int64GetDatumFast(X)
Definition: postgres.h:804
#define DatumGetUInt32(X)
Definition: postgres.h:530

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

2524 {
2526  Datum seed = PG_GETARG_DATUM(1);
2527  uint64 thash;
2528 
2529  /* Same approach as timetz_hash */
2531  Int64GetDatumFast(key->time),
2532  seed));
2533  thash ^= DatumGetUInt64(hash_uint32_extended(key->zone,
2534  DatumGetInt64(seed)));
2535  PG_RETURN_UINT64(thash);
2536 }
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:369
static Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfn.h:49
#define DatumGetUInt64(X)
Definition: postgres.h:678
#define DatumGetInt64(X)
Definition: postgres.h:651

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

2254 {
2255  char *str = PG_GETARG_CSTRING(0);
2256 
2257 #ifdef NOT_USED
2258  Oid typelem = PG_GETARG_OID(1);
2259 #endif
2260  int32 typmod = PG_GETARG_INT32(2);
2261  TimeTzADT *result;
2262  fsec_t fsec;
2263  struct pg_tm tt,
2264  *tm = &tt;
2265  int tz;
2266  int nf;
2267  int dterr;
2268  char workbuf[MAXDATELEN + 1];
2269  char *field[MAXDATEFIELDS];
2270  int dtype;
2271  int ftype[MAXDATEFIELDS];
2272 
2273  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
2274  field, ftype, MAXDATEFIELDS, &nf);
2275  if (dterr == 0)
2276  dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
2277  if (dterr != 0)
2278  DateTimeParseError(dterr, str, "time with time zone");
2279 
2280  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2281  tm2timetz(tm, fsec, tz, result);
2282  AdjustTimeForTypmod(&(result->time), typmod);
2283 
2284  PG_RETURN_TIMETZADT_P(result);
2285 }

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

◆ timetz_izone()

Datum timetz_izone ( PG_FUNCTION_ARGS  )

Definition at line 3106 of file date.c.

3107 {
3109  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
3110  TimeTzADT *result;
3111  int tz;
3112 
3113  if (zone->month != 0 || zone->day != 0)
3114  ereport(ERROR,
3115  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3116  errmsg("interval time zone \"%s\" must not include months or days",
3118  PointerGetDatum(zone))))));
3119 
3120  tz = -(zone->time / USECS_PER_SEC);
3121 
3122  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
3123 
3124  result->time = time->time + (time->zone - tz) * USECS_PER_SEC;
3125  while (result->time < INT64CONST(0))
3126  result->time += USECS_PER_DAY;
3127  while (result->time >= USECS_PER_DAY)
3128  result->time -= USECS_PER_DAY;
3129 
3130  result->zone = tz;
3131 
3132  PG_RETURN_TIMETZADT_P(result);
3133 }
Datum interval_out(PG_FUNCTION_ARGS)
Definition: timestamp.c:955
while(p+4<=pend)
#define DatumGetCString(X)
Definition: postgres.h:610
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 2539 of file date.c.

2540 {
2541  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2542  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2543  TimeTzADT *result;
2544 
2545  if (timetz_cmp_internal(time1, time2) > 0)
2546  result = time1;
2547  else
2548  result = time2;
2549  PG_RETURN_TIMETZADT_P(result);
2550 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

◆ timetz_le()

Datum timetz_le ( PG_FUNCTION_ARGS  )

Definition at line 2471 of file date.c.

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

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_lt()

Datum timetz_lt ( PG_FUNCTION_ARGS  )

Definition at line 2462 of file date.c.

2463 {
2464  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2465  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2466 
2467  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) < 0);
2468 }

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

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

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

2454 {
2455  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2456  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2457 
2458  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) != 0);
2459 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_out()

Datum timetz_out ( PG_FUNCTION_ARGS  )

Definition at line 2288 of file date.c.

2289 {
2290  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2291  char *result;
2292  struct pg_tm tt,
2293  *tm = &tt;
2294  fsec_t fsec;
2295  int tz;
2296  char buf[MAXDATELEN + 1];
2297 
2298  timetz2tm(time, tm, &fsec, &tz);
2299  EncodeTimeOnly(tm, fsec, true, tz, DateStyle, buf);
2300 
2301  result = pstrdup(buf);
2302  PG_RETURN_CSTRING(result);
2303 }
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: date.c:2377

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

3007 {
3008  return timetz_part_common(fcinfo, false);
3009 }

References timetz_part_common().

◆ timetz_part_common()

static Datum timetz_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2889 of file date.c.

2890 {
2891  text *units = PG_GETARG_TEXT_PP(0);
2892  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2893  int64 intresult;
2894  int type,
2895  val;
2896  char *lowunits;
2897 
2898  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
2899  VARSIZE_ANY_EXHDR(units),
2900  false);
2901 
2902  type = DecodeUnits(0, lowunits, &val);
2903  if (type == UNKNOWN_FIELD)
2904  type = DecodeSpecial(0, lowunits, &val);
2905 
2906  if (type == UNITS)
2907  {
2908  int tz;
2909  fsec_t fsec;
2910  struct pg_tm tt,
2911  *tm = &tt;
2912 
2913  timetz2tm(time, tm, &fsec, &tz);
2914 
2915  switch (val)
2916  {
2917  case DTK_TZ:
2918  intresult = -tz;
2919  break;
2920 
2921  case DTK_TZ_MINUTE:
2922  intresult = (-tz / SECS_PER_MINUTE) % MINS_PER_HOUR;
2923  break;
2924 
2925  case DTK_TZ_HOUR:
2926  intresult = -tz / SECS_PER_HOUR;
2927  break;
2928 
2929  case DTK_MICROSEC:
2930  intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
2931  break;
2932 
2933  case DTK_MILLISEC:
2934  if (retnumeric)
2935  /*---
2936  * tm->tm_sec * 1000 + fsec / 1000
2937  * = (tm->tm_sec * 1'000'000 + fsec) / 1000
2938  */
2939  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
2940  else
2941  PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
2942  break;
2943 
2944  case DTK_SECOND:
2945  if (retnumeric)
2946  /*---
2947  * tm->tm_sec + fsec / 1'000'000
2948  * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
2949  */
2950  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
2951  else
2952  PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
2953  break;
2954 
2955  case DTK_MINUTE:
2956  intresult = tm->tm_min;
2957  break;
2958 
2959  case DTK_HOUR:
2960  intresult = tm->tm_hour;
2961  break;
2962 
2963  case DTK_DAY:
2964  case DTK_MONTH:
2965  case DTK_QUARTER:
2966  case DTK_YEAR:
2967  case DTK_DECADE:
2968  case DTK_CENTURY:
2969  case DTK_MILLENNIUM:
2970  default:
2971  ereport(ERROR,
2972  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2973  errmsg("unit \"%s\" not supported for type %s",
2974  lowunits, format_type_be(TIMETZOID))));
2975  intresult = 0;
2976  }
2977  }
2978  else if (type == RESERV && val == DTK_EPOCH)
2979  {
2980  if (retnumeric)
2981  /*---
2982  * time->time / 1'000'000 + time->zone
2983  * = (time->time + time->zone * 1'000'000) / 1'000'000
2984  */
2985  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(time->time + time->zone * INT64CONST(1000000), 6));
2986  else
2987  PG_RETURN_FLOAT8(time->time / 1000000.0 + time->zone);
2988  }
2989  else
2990  {
2991  ereport(ERROR,
2992  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2993  errmsg("unit \"%s\" not recognized for type %s",
2994  lowunits, format_type_be(TIMETZOID))));
2995  intresult = 0;
2996  }
2997 
2998  if (retnumeric)
2999  PG_RETURN_NUMERIC(int64_to_numeric(intresult));
3000  else
3001  PG_RETURN_FLOAT8(intresult);
3002 }
#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 2570 of file date.c.

2571 {
2572  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2573  Interval *span = PG_GETARG_INTERVAL_P(1);
2574  TimeTzADT *result;
2575 
2576  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2577 
2578  result->time = time->time + span->time;
2579  result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2580  if (result->time < INT64CONST(0))
2581  result->time += USECS_PER_DAY;
2582 
2583  result->zone = time->zone;
2584 
2585  PG_RETURN_TIMETZADT_P(result);
2586 }

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

2310 {
2312 
2313 #ifdef NOT_USED
2314  Oid typelem = PG_GETARG_OID(1);
2315 #endif
2316  int32 typmod = PG_GETARG_INT32(2);
2317  TimeTzADT *result;
2318 
2319  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2320 
2321  result->time = pq_getmsgint64(buf);
2322 
2323  if (result->time < INT64CONST(0) || result->time > USECS_PER_DAY)
2324  ereport(ERROR,
2325  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2326  errmsg("time out of range")));
2327 
2328  result->zone = pq_getmsgint(buf, sizeof(result->zone));
2329 
2330  /* Check for sane GMT displacement; see notes in datatype/timestamp.h */
2331  if (result->zone <= -TZDISP_LIMIT || result->zone >= TZDISP_LIMIT)
2332  ereport(ERROR,
2333  (errcode(ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE),
2334  errmsg("time zone displacement out of range")));
2335 
2336  AdjustTimeForTypmod(&(result->time), typmod);
2337 
2338  PG_RETURN_TIMETZADT_P(result);
2339 }
#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 2399 of file date.c.

2400 {
2401  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2402  int32 typmod = PG_GETARG_INT32(1);
2403  TimeTzADT *result;
2404 
2405  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2406 
2407  result->time = time->time;
2408  result->zone = time->zone;
2409 
2410  AdjustTimeForTypmod(&(result->time), typmod);
2411 
2412  PG_RETURN_TIMETZADT_P(result);
2413 }

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

2554 {
2555  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2556  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2557  TimeTzADT *result;
2558 
2559  if (timetz_cmp_internal(time1, time2) < 0)
2560  result = time1;
2561  else
2562  result = time2;
2563  PG_RETURN_TIMETZADT_P(result);
2564 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

◆ timetz_time()

Datum timetz_time ( PG_FUNCTION_ARGS  )

Definition at line 2777 of file date.c.

2778 {
2779  TimeTzADT *timetz = PG_GETARG_TIMETZADT_P(0);
2780  TimeADT result;
2781 
2782  /* swallow the time zone and just return the time */
2783  result = timetz->time;
2784 
2785  PG_RETURN_TIMEADT(result);
2786 }

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMEADT, and TimeTzADT::time.

◆ timetz_zone()

Datum timetz_zone ( PG_FUNCTION_ARGS  )

Definition at line 3022 of file date.c.

3023 {
3024  text *zone = PG_GETARG_TEXT_PP(0);
3026  TimeTzADT *result;
3027  int tz;
3028  char tzname[TZ_STRLEN_MAX + 1];
3029  char *lowzone;
3030  int type,
3031  val;
3032  pg_tz *tzp;
3033 
3034  /*
3035  * Look up the requested timezone. First we look in the timezone
3036  * abbreviation table (to handle cases like "EST"), and if that fails, we
3037  * look in the timezone database (to handle cases like
3038  * "America/New_York"). (This matches the order in which timestamp input
3039  * checks the cases; it's important because the timezone database unwisely
3040  * uses a few zone names that are identical to offset abbreviations.)
3041  */
3042  text_to_cstring_buffer(zone, tzname, sizeof(tzname));
3043 
3044  /* DecodeTimezoneAbbrev requires lowercase input */
3045  lowzone = downcase_truncate_identifier(tzname,
3046  strlen(tzname),
3047  false);
3048 
3049  type = DecodeTimezoneAbbrev(0, lowzone, &val, &tzp);
3050 
3051  if (type == TZ || type == DTZ)
3052  {
3053  /* fixed-offset abbreviation */
3054  tz = -val;
3055  }
3056  else if (type == DYNTZ)
3057  {
3058  /* dynamic-offset abbreviation, resolve using transaction start time */
3060  int isdst;
3061 
3062  tz = DetermineTimeZoneAbbrevOffsetTS(now, tzname, tzp, &isdst);
3063  }
3064  else
3065  {
3066  /* try it as a full zone name */
3067  tzp = pg_tzset(tzname);
3068  if (tzp)
3069  {
3070  /* Get the offset-from-GMT that is valid now for the zone */
3072  struct pg_tm tm;
3073  fsec_t fsec;
3074 
3075  if (timestamp2tm(now, &tz, &tm, &fsec, NULL, tzp) != 0)
3076  ereport(ERROR,
3077  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3078  errmsg("timestamp out of range")));
3079  }
3080  else
3081  {
3082  ereport(ERROR,
3083  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3084  errmsg("time zone \"%s\" not recognized", tzname)));
3085  tz = 0; /* keep compiler quiet */
3086  }
3087  }
3088 
3089  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
3090 
3091  result->time = t->time + (t->zone - tz) * USECS_PER_SEC;
3092  while (result->time < INT64CONST(0))
3093  result->time += USECS_PER_DAY;
3094  while (result->time >= USECS_PER_DAY)
3095  result->time -= USECS_PER_DAY;
3096 
3097  result->zone = tz;
3098 
3099  PG_RETURN_TIMETZADT_P(result);
3100 }
int DecodeTimezoneAbbrev(int field, char *lowtoken, int *offset, pg_tz **tz)
Definition: datetime.c:3225
int DetermineTimeZoneAbbrevOffsetTS(TimestampTz ts, const char *abbr, pg_tz *tzp, int *isdst)
Definition: datetime.c:1846
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1538
#define TZ
Definition: datetime.h:96
#define DTZ
Definition: datetime.h:97
#define DYNTZ
Definition: datetime.h:98
#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:252
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:851

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

2358 {
2360 
2361  PG_RETURN_INT32(anytime_typmodin(true, ta));
2362 }

References anytime_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timetztypmodout()

Datum timetztypmodout ( PG_FUNCTION_ARGS  )

Definition at line 2365 of file date.c.

2366 {
2367  int32 typmod = PG_GETARG_INT32(0);
2368 
2369  PG_RETURN_CSTRING(anytime_typmodout(true, typmod));
2370 }

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

1405 {
1406  *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1407  * USECS_PER_SEC) + fsec;
1408  return 0;
1409 }

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

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

◆ tm2timetz()

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

Definition at line 2243 of file date.c.

2244 {
2245  result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2246  USECS_PER_SEC) + fsec;
2247  result->zone = tz;
2248 
2249  return 0;
2250 }

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

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