PostgreSQL Source Code  git master
date.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include <float.h>
#include <time.h>
#include "access/xact.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/hashutils.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 int tm2time (struct pg_tm *tm, fsec_t fsec, TimeADT *result)
 
static int tm2timetz (struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
 
static void AdjustTimeForTypmod (TimeADT *time, int32 typmod)
 
static int32 anytime_typmodin (bool istz, ArrayType *ta)
 
int32 anytime_typmod_check (bool istz, int32 typmod)
 
static char * anytime_typmodout (bool istz, int32 typmod)
 
Datum date_in (PG_FUNCTION_ARGS)
 
Datum date_out (PG_FUNCTION_ARGS)
 
Datum date_recv (PG_FUNCTION_ARGS)
 
Datum date_send (PG_FUNCTION_ARGS)
 
Datum make_date (PG_FUNCTION_ARGS)
 
void EncodeSpecialDate (DateADT dt, char *str)
 
DateADT GetSQLCurrentDate (void)
 
TimeTzADTGetSQLCurrentTime (int32 typmod)
 
TimeADT GetSQLLocalTime (int32 typmod)
 
Datum date_eq (PG_FUNCTION_ARGS)
 
Datum date_ne (PG_FUNCTION_ARGS)
 
Datum date_lt (PG_FUNCTION_ARGS)
 
Datum date_le (PG_FUNCTION_ARGS)
 
Datum date_gt (PG_FUNCTION_ARGS)
 
Datum date_ge (PG_FUNCTION_ARGS)
 
Datum date_cmp (PG_FUNCTION_ARGS)
 
static int date_fastcmp (Datum x, Datum y, SortSupport ssup)
 
Datum date_sortsupport (PG_FUNCTION_ARGS)
 
Datum date_finite (PG_FUNCTION_ARGS)
 
Datum date_larger (PG_FUNCTION_ARGS)
 
Datum date_smaller (PG_FUNCTION_ARGS)
 
Datum date_mi (PG_FUNCTION_ARGS)
 
Datum date_pli (PG_FUNCTION_ARGS)
 
Datum date_mii (PG_FUNCTION_ARGS)
 
static Timestamp date2timestamp (DateADT dateVal)
 
static TimestampTz date2timestamptz (DateADT dateVal)
 
double date2timestamp_no_overflow (DateADT dateVal)
 
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)
 
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 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 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)
 
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)
 
Datum time_part (PG_FUNCTION_ARGS)
 
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)
 
Datum timetz_part (PG_FUNCTION_ARGS)
 
Datum timetz_zone (PG_FUNCTION_ARGS)
 
Datum timetz_izone (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ TIMEADT_GT

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

Referenced by overlaps_time().

◆ TIMEADT_LT

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

Referenced by overlaps_time().

◆ TIMETZ_GT

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

Referenced by overlaps_timetz().

◆ TIMETZ_LT

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

Referenced by overlaps_timetz().

Function Documentation

◆ AdjustTimeForTypmod()

static void AdjustTimeForTypmod ( TimeADT time,
int32  typmod 
)
static

Definition at line 1383 of file date.c.

References MAX_TIME_PRECISION.

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

1384 {
1385  static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1386  INT64CONST(1000000),
1387  INT64CONST(100000),
1388  INT64CONST(10000),
1389  INT64CONST(1000),
1390  INT64CONST(100),
1391  INT64CONST(10),
1392  INT64CONST(1)
1393  };
1394 
1395  static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1396  INT64CONST(500000),
1397  INT64CONST(50000),
1398  INT64CONST(5000),
1399  INT64CONST(500),
1400  INT64CONST(50),
1401  INT64CONST(5),
1402  INT64CONST(0)
1403  };
1404 
1405  if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1406  {
1407  if (*time >= INT64CONST(0))
1408  *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1409  TimeScales[typmod];
1410  else
1411  *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1412  TimeScales[typmod]);
1413  }
1414 }
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ anytime_typmod_check()

int32 anytime_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 72 of file date.c.

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

Referenced by anytime_typmodin(), and transformSQLValueFunction().

73 {
74  if (typmod < 0)
75  ereport(ERROR,
76  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
77  errmsg("TIME(%d)%s precision must not be negative",
78  typmod, (istz ? " WITH TIME ZONE" : ""))));
79  if (typmod > MAX_TIME_PRECISION)
80  {
82  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
83  errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
84  typmod, (istz ? " WITH TIME ZONE" : ""),
86  typmod = MAX_TIME_PRECISION;
87  }
88 
89  return typmod;
90 }
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ anytime_typmodin()

static int32 anytime_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 51 of file date.c.

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

Referenced by timetypmodin(), and timetztypmodin().

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

◆ anytime_typmodout()

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

Definition at line 94 of file date.c.

References psprintf().

Referenced by timetypmodout(), and timetztypmodout().

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

◆ date2timestamp()

static Timestamp date2timestamp ( DateADT  dateVal)
static

Definition at line 564 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, ereport, errcode(), errmsg(), ERROR, POSTGRES_EPOCH_JDATE, TIMESTAMP_END_JULIAN, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, and USECS_PER_DAY.

Referenced by date_cmp_timestamp(), date_eq_timestamp(), date_ge_timestamp(), date_gt_timestamp(), date_le_timestamp(), date_lt_timestamp(), date_mi_interval(), date_ne_timestamp(), date_pl_interval(), date_timestamp(), datetime_timestamp(), in_range_date_interval(), timestamp_cmp_date(), timestamp_eq_date(), timestamp_ge_date(), timestamp_gt_date(), timestamp_le_date(), timestamp_lt_date(), and timestamp_ne_date().

565 {
566  Timestamp result;
567 
568  if (DATE_IS_NOBEGIN(dateVal))
569  TIMESTAMP_NOBEGIN(result);
570  else if (DATE_IS_NOEND(dateVal))
571  TIMESTAMP_NOEND(result);
572  else
573  {
574  /*
575  * Date's range is wider than timestamp's, so check for boundaries.
576  * Since dates have the same minimum values as timestamps, only upper
577  * boundary need be checked for overflow.
578  */
579  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
580  ereport(ERROR,
581  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
582  errmsg("date out of range for timestamp")));
583 
584  /* date is days since 2000, timestamp is microseconds since same... */
585  result = dateVal * USECS_PER_DAY;
586  }
587 
588  return result;
589 }
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define USECS_PER_DAY
Definition: timestamp.h:91
#define ereport(elevel, rest)
Definition: elog.h:141
int64 Timestamp
Definition: timestamp.h:38
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ date2timestamp_no_overflow()

double date2timestamp_no_overflow ( DateADT  dateVal)

Definition at line 648 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, and USECS_PER_DAY.

Referenced by convert_timevalue_to_scalar().

649 {
650  double result;
651 
652  if (DATE_IS_NOBEGIN(dateVal))
653  result = -DBL_MAX;
654  else if (DATE_IS_NOEND(dateVal))
655  result = DBL_MAX;
656  else
657  {
658  /* date is days since 2000, timestamp is microseconds since same... */
659  result = dateVal * (double) USECS_PER_DAY;
660  }
661 
662  return result;
663 }
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define USECS_PER_DAY
Definition: timestamp.h:91
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40

◆ date2timestamptz()

static TimestampTz date2timestamptz ( DateADT  dateVal)
static

Definition at line 592 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, DetermineTimeZoneOffset(), ereport, errcode(), errmsg(), ERROR, IS_VALID_TIMESTAMP, j2date(), 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 date_cmp_timestamptz(), date_eq_timestamptz(), date_ge_timestamptz(), date_gt_timestamptz(), date_le_timestamptz(), date_lt_timestamptz(), date_ne_timestamptz(), date_timestamptz(), timestamptz_cmp_date(), timestamptz_eq_date(), timestamptz_ge_date(), timestamptz_gt_date(), timestamptz_le_date(), timestamptz_lt_date(), and timestamptz_ne_date().

593 {
594  TimestampTz result;
595  struct pg_tm tt,
596  *tm = &tt;
597  int tz;
598 
599  if (DATE_IS_NOBEGIN(dateVal))
600  TIMESTAMP_NOBEGIN(result);
601  else if (DATE_IS_NOEND(dateVal))
602  TIMESTAMP_NOEND(result);
603  else
604  {
605  /*
606  * Date's range is wider than timestamp's, so check for boundaries.
607  * Since dates have the same minimum values as timestamps, only upper
608  * boundary need be checked for overflow.
609  */
610  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
611  ereport(ERROR,
612  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
613  errmsg("date out of range for timestamp")));
614 
615  j2date(dateVal + POSTGRES_EPOCH_JDATE,
616  &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
617  tm->tm_hour = 0;
618  tm->tm_min = 0;
619  tm->tm_sec = 0;
621 
622  result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
623 
624  /*
625  * Since it is possible to go beyond allowed timestamptz range because
626  * of time zone, check for allowed timestamp range after adding tz.
627  */
628  if (!IS_VALID_TIMESTAMP(result))
629  ereport(ERROR,
630  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
631  errmsg("date out of range for timestamp")));
632  }
633 
634  return result;
635 }
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
int tm_hour
Definition: pgtime.h:29
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1441
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define USECS_PER_DAY
Definition: timestamp.h:91
#define ereport(elevel, rest)
Definition: elog.h:141
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:295
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ date_cmp()

Datum date_cmp ( PG_FUNCTION_ARGS  )

Definition at line 433 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_INT32.

Referenced by gbt_datekey_cmp(), and leftmostvalue_date().

434 {
435  DateADT dateVal1 = PG_GETARG_DATEADT(0);
436  DateADT dateVal2 = PG_GETARG_DATEADT(1);
437 
438  if (dateVal1 < dateVal2)
439  PG_RETURN_INT32(-1);
440  else if (dateVal1 > dateVal2)
441  PG_RETURN_INT32(1);
442  PG_RETURN_INT32(0);
443 }
int32 DateADT
Definition: date.h:23
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_DATEADT(n)
Definition: date.h:61

◆ date_cmp_timestamp()

Datum date_cmp_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 743 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_INT32, and timestamp_cmp_internal().

744 {
745  DateADT dateVal = PG_GETARG_DATEADT(0);
747  Timestamp dt1;
748 
749  dt1 = date2timestamp(dateVal);
750 
752 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564

◆ date_cmp_timestamptz()

Datum date_cmp_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 827 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_INT32, and timestamptz_cmp_internal.

828 {
829  DateADT dateVal = PG_GETARG_DATEADT(0);
831  TimestampTz dt1;
832 
833  dt1 = date2timestamptz(dateVal);
834 
836 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ date_eq()

Datum date_eq ( PG_FUNCTION_ARGS  )

Definition at line 379 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_dateeq().

380 {
381  DateADT dateVal1 = PG_GETARG_DATEADT(0);
382  DateADT dateVal2 = PG_GETARG_DATEADT(1);
383 
384  PG_RETURN_BOOL(dateVal1 == dateVal2);
385 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_eq_timestamp()

Datum date_eq_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 671 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

672 {
673  DateADT dateVal = PG_GETARG_DATEADT(0);
675  Timestamp dt1;
676 
677  dt1 = date2timestamp(dateVal);
678 
679  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
680 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_eq_timestamptz()

Datum date_eq_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 755 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamptz_cmp_internal.

756 {
757  DateADT dateVal = PG_GETARG_DATEADT(0);
759  TimestampTz dt1;
760 
761  dt1 = date2timestamptz(dateVal);
762 
764 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ date_fastcmp()

static int date_fastcmp ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 446 of file date.c.

References DatumGetDateADT.

Referenced by date_sortsupport().

447 {
448  DateADT a = DatumGetDateADT(x);
449  DateADT b = DatumGetDateADT(y);
450 
451  if (a < b)
452  return -1;
453  else if (a > b)
454  return 1;
455  return 0;
456 }
#define DatumGetDateADT(X)
Definition: date.h:53
int32 DateADT
Definition: date.h:23

◆ date_finite()

Datum date_finite ( PG_FUNCTION_ARGS  )

Definition at line 468 of file date.c.

References DATE_NOT_FINITE, PG_GETARG_DATEADT, and PG_RETURN_BOOL.

469 {
471 
473 }
int32 DateADT
Definition: date.h:23
long date
Definition: pgtypes_date.h:9
#define DATE_NOT_FINITE(j)
Definition: date.h:43
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_ge()

Datum date_ge ( PG_FUNCTION_ARGS  )

Definition at line 424 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_datege().

425 {
426  DateADT dateVal1 = PG_GETARG_DATEADT(0);
427  DateADT dateVal2 = PG_GETARG_DATEADT(1);
428 
429  PG_RETURN_BOOL(dateVal1 >= dateVal2);
430 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_ge_timestamp()

Datum date_ge_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 731 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

732 {
733  DateADT dateVal = PG_GETARG_DATEADT(0);
735  Timestamp dt1;
736 
737  dt1 = date2timestamp(dateVal);
738 
739  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
740 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_ge_timestamptz()

Datum date_ge_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 815 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamptz_cmp_internal.

816 {
817  DateADT dateVal = PG_GETARG_DATEADT(0);
819  TimestampTz dt1;
820 
821  dt1 = date2timestamptz(dateVal);
822 
824 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ date_gt()

Datum date_gt ( PG_FUNCTION_ARGS  )

Definition at line 415 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_dategt().

416 {
417  DateADT dateVal1 = PG_GETARG_DATEADT(0);
418  DateADT dateVal2 = PG_GETARG_DATEADT(1);
419 
420  PG_RETURN_BOOL(dateVal1 > dateVal2);
421 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_gt_timestamp()

Datum date_gt_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 707 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

708 {
709  DateADT dateVal = PG_GETARG_DATEADT(0);
711  Timestamp dt1;
712 
713  dt1 = date2timestamp(dateVal);
714 
715  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
716 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_gt_timestamptz()

Datum date_gt_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 791 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamptz_cmp_internal.

792 {
793  DateADT dateVal = PG_GETARG_DATEADT(0);
795  TimestampTz dt1;
796 
797  dt1 = date2timestamptz(dateVal);
798 
800 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ date_in()

Datum date_in ( PG_FUNCTION_ARGS  )

Definition at line 114 of file date.c.

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

115 {
116  char *str = PG_GETARG_CSTRING(0);
117  DateADT date;
118  fsec_t fsec;
119  struct pg_tm tt,
120  *tm = &tt;
121  int tzp;
122  int dtype;
123  int nf;
124  int dterr;
125  char *field[MAXDATEFIELDS];
126  int ftype[MAXDATEFIELDS];
127  char workbuf[MAXDATELEN + 1];
128 
129  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
130  field, ftype, MAXDATEFIELDS, &nf);
131  if (dterr == 0)
132  dterr = DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp);
133  if (dterr != 0)
134  DateTimeParseError(dterr, str, "date");
135 
136  switch (dtype)
137  {
138  case DTK_DATE:
139  break;
140 
141  case DTK_EPOCH:
142  GetEpochTime(tm);
143  break;
144 
145  case DTK_LATE:
146  DATE_NOEND(date);
147  PG_RETURN_DATEADT(date);
148 
149  case DTK_EARLY:
150  DATE_NOBEGIN(date);
151  PG_RETURN_DATEADT(date);
152 
153  default:
154  DateTimeParseError(DTERR_BAD_FORMAT, str, "date");
155  break;
156  }
157 
158  /* Prevent overflow in Julian-day routines */
159  if (!IS_VALID_JULIAN(tm->tm_year, tm->tm_mon, tm->tm_mday))
160  ereport(ERROR,
161  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
162  errmsg("date out of range: \"%s\"", str)));
163 
164  date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
165 
166  /* Now check for just-out-of-range dates */
167  if (!IS_VALID_DATE(date))
168  ereport(ERROR,
169  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
170  errmsg("date out of range: \"%s\"", str)));
171 
172  PG_RETURN_DATEADT(date);
173 }
#define MAXDATELEN
Definition: datetime.h:201
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3739
int32 DateADT
Definition: date.h:23
int errcode(int sqlerrcode)
Definition: elog.c:570
long date
Definition: pgtypes_date.h:9
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define DATE_NOBEGIN(j)
Definition: date.h:39
#define IS_VALID_DATE(d)
Definition: timestamp.h:190
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:761
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:155
#define ereport(elevel, rest)
Definition: elog.h:141
#define DTK_LATE
Definition: datetime.h:152
int date2j(int y, int m, int d)
Definition: datetime.c:270
#define DTK_EARLY
Definition: datetime.h:151
#define MAXDATEFIELDS
Definition: datetime.h:203
#define DTK_EPOCH
Definition: datetime.h:153
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:784
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2003
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
#define PG_RETURN_DATEADT(x)
Definition: date.h:65
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:540
#define DTK_DATE
Definition: datetime.h:145
#define DATE_NOEND(j)
Definition: date.h:41

◆ date_larger()

Datum date_larger ( PG_FUNCTION_ARGS  )

Definition at line 476 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_DATEADT.

477 {
478  DateADT dateVal1 = PG_GETARG_DATEADT(0);
479  DateADT dateVal2 = PG_GETARG_DATEADT(1);
480 
481  PG_RETURN_DATEADT((dateVal1 > dateVal2) ? dateVal1 : dateVal2);
482 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_DATEADT(x)
Definition: date.h:65

◆ date_le()

Datum date_le ( PG_FUNCTION_ARGS  )

Definition at line 406 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_datele().

407 {
408  DateADT dateVal1 = PG_GETARG_DATEADT(0);
409  DateADT dateVal2 = PG_GETARG_DATEADT(1);
410 
411  PG_RETURN_BOOL(dateVal1 <= dateVal2);
412 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_le_timestamp()

Datum date_le_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 719 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

720 {
721  DateADT dateVal = PG_GETARG_DATEADT(0);
723  Timestamp dt1;
724 
725  dt1 = date2timestamp(dateVal);
726 
727  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
728 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_le_timestamptz()

Datum date_le_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 803 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamptz_cmp_internal.

804 {
805  DateADT dateVal = PG_GETARG_DATEADT(0);
807  TimestampTz dt1;
808 
809  dt1 = date2timestamptz(dateVal);
810 
812 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ date_lt()

Datum date_lt ( PG_FUNCTION_ARGS  )

Definition at line 397 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_datelt().

398 {
399  DateADT dateVal1 = PG_GETARG_DATEADT(0);
400  DateADT dateVal2 = PG_GETARG_DATEADT(1);
401 
402  PG_RETURN_BOOL(dateVal1 < dateVal2);
403 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_lt_timestamp()

Datum date_lt_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 695 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

696 {
697  DateADT dateVal = PG_GETARG_DATEADT(0);
699  Timestamp dt1;
700 
701  dt1 = date2timestamp(dateVal);
702 
703  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
704 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_lt_timestamptz()

Datum date_lt_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 779 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamptz_cmp_internal.

780 {
781  DateADT dateVal = PG_GETARG_DATEADT(0);
783  TimestampTz dt1;
784 
785  dt1 = date2timestamptz(dateVal);
786 
788 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ date_mi()

Datum date_mi ( PG_FUNCTION_ARGS  )

Definition at line 496 of file date.c.

References DATE_NOT_FINITE, ereport, errcode(), errmsg(), ERROR, PG_GETARG_DATEADT, and PG_RETURN_INT32.

Referenced by date_dist(), gbt_date_penalty(), and gdb_date_dist().

497 {
498  DateADT dateVal1 = PG_GETARG_DATEADT(0);
499  DateADT dateVal2 = PG_GETARG_DATEADT(1);
500 
501  if (DATE_NOT_FINITE(dateVal1) || DATE_NOT_FINITE(dateVal2))
502  ereport(ERROR,
503  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
504  errmsg("cannot subtract infinite dates")));
505 
506  PG_RETURN_INT32((int32) (dateVal1 - dateVal2));
507 }
int32 DateADT
Definition: date.h:23
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
int errcode(int sqlerrcode)
Definition: elog.c:570
signed int int32
Definition: c.h:346
#define ERROR
Definition: elog.h:43
#define DATE_NOT_FINITE(j)
Definition: date.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ date_mi_interval()

Datum date_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 1062 of file date.c.

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

1063 {
1064  DateADT dateVal = PG_GETARG_DATEADT(0);
1065  Interval *span = PG_GETARG_INTERVAL_P(1);
1066  Timestamp dateStamp;
1067 
1068  dateStamp = date2timestamp(dateVal);
1069 
1071  TimestampGetDatum(dateStamp),
1072  PointerGetDatum(span));
1073 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int32 DateADT
Definition: date.h:23
#define PointerGetDatum(X)
Definition: postgres.h:556
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2896
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618

◆ date_mii()

Datum date_mii ( PG_FUNCTION_ARGS  )

Definition at line 537 of file date.c.

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

538 {
539  DateADT dateVal = PG_GETARG_DATEADT(0);
541  DateADT result;
542 
543  if (DATE_NOT_FINITE(dateVal))
544  PG_RETURN_DATEADT(dateVal); /* can't change infinity */
545 
546  result = dateVal - days;
547 
548  /* Check for integer overflow and out-of-allowed-range */
549  if ((days >= 0 ? (result > dateVal) : (result < dateVal)) ||
550  !IS_VALID_DATE(result))
551  ereport(ERROR,
552  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
553  errmsg("date out of range")));
554 
555  PG_RETURN_DATEADT(result);
556 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
int32 DateADT
Definition: date.h:23
int errcode(int sqlerrcode)
Definition: elog.c:570
signed int int32
Definition: c.h:346
#define IS_VALID_DATE(d)
Definition: timestamp.h:190
#define ERROR
Definition: elog.h:43
#define DATE_NOT_FINITE(j)
Definition: date.h:43
const char *const days[]
Definition: datetime.c:69
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define PG_RETURN_DATEADT(x)
Definition: date.h:65

◆ date_ne()

Datum date_ne ( PG_FUNCTION_ARGS  )

Definition at line 388 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

389 {
390  DateADT dateVal1 = PG_GETARG_DATEADT(0);
391  DateADT dateVal2 = PG_GETARG_DATEADT(1);
392 
393  PG_RETURN_BOOL(dateVal1 != dateVal2);
394 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_ne_timestamp()

Datum date_ne_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 683 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

684 {
685  DateADT dateVal = PG_GETARG_DATEADT(0);
687  Timestamp dt1;
688 
689  dt1 = date2timestamp(dateVal);
690 
691  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
692 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ date_ne_timestamptz()

Datum date_ne_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 767 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamptz_cmp_internal.

768 {
769  DateADT dateVal = PG_GETARG_DATEADT(0);
771  TimestampTz dt1;
772 
773  dt1 = date2timestamptz(dateVal);
774 
776 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ date_out()

Datum date_out ( PG_FUNCTION_ARGS  )

Definition at line 179 of file date.c.

References buf, DATE_NOT_FINITE, DateStyle, EncodeDateOnly(), EncodeSpecialDate(), j2date(), MAXDATELEN, PG_GETARG_DATEADT, PG_RETURN_CSTRING, POSTGRES_EPOCH_JDATE, pstrdup(), tm, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

180 {
182  char *result;
183  struct pg_tm tt,
184  *tm = &tt;
185  char buf[MAXDATELEN + 1];
186 
187  if (DATE_NOT_FINITE(date))
188  EncodeSpecialDate(date, buf);
189  else
190  {
192  &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
193  EncodeDateOnly(tm, DateStyle, buf);
194  }
195 
196  result = pstrdup(buf);
197  PG_RETURN_CSTRING(result);
198 }
#define MAXDATELEN
Definition: datetime.h:201
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:3858
int32 DateADT
Definition: date.h:23
char * pstrdup(const char *in)
Definition: mcxt.c:1161
long date
Definition: pgtypes_date.h:9
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define DATE_NOT_FINITE(j)
Definition: date.h:43
int tm_mday
Definition: pgtime.h:30
static char * buf
Definition: pg_test_fsync.c:68
int tm_mon
Definition: pgtime.h:31
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:289
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:295
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
int DateStyle
Definition: globals.c:115
int tm_year
Definition: pgtime.h:32
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ date_pl_interval()

Datum date_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 1042 of file date.c.

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

1043 {
1044  DateADT dateVal = PG_GETARG_DATEADT(0);
1045  Interval *span = PG_GETARG_INTERVAL_P(1);
1046  Timestamp dateStamp;
1047 
1048  dateStamp = date2timestamp(dateVal);
1049 
1051  TimestampGetDatum(dateStamp),
1052  PointerGetDatum(span));
1053 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int32 DateADT
Definition: date.h:23
#define PointerGetDatum(X)
Definition: postgres.h:556
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2817
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618

◆ date_pli()

Datum date_pli ( PG_FUNCTION_ARGS  )

Definition at line 513 of file date.c.

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

Referenced by daterange_canonical().

514 {
515  DateADT dateVal = PG_GETARG_DATEADT(0);
517  DateADT result;
518 
519  if (DATE_NOT_FINITE(dateVal))
520  PG_RETURN_DATEADT(dateVal); /* can't change infinity */
521 
522  result = dateVal + days;
523 
524  /* Check for integer overflow and out-of-allowed-range */
525  if ((days >= 0 ? (result < dateVal) : (result > dateVal)) ||
526  !IS_VALID_DATE(result))
527  ereport(ERROR,
528  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
529  errmsg("date out of range")));
530 
531  PG_RETURN_DATEADT(result);
532 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
int32 DateADT
Definition: date.h:23
int errcode(int sqlerrcode)
Definition: elog.c:570
signed int int32
Definition: c.h:346
#define IS_VALID_DATE(d)
Definition: timestamp.h:190
#define ERROR
Definition: elog.h:43
#define DATE_NOT_FINITE(j)
Definition: date.h:43
const char *const days[]
Definition: datetime.c:69
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define PG_RETURN_DATEADT(x)
Definition: date.h:65

◆ date_recv()

Datum date_recv ( PG_FUNCTION_ARGS  )

Definition at line 204 of file date.c.

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

205 {
207  DateADT result;
208 
209  result = (DateADT) pq_getmsgint(buf, sizeof(DateADT));
210 
211  /* Limit to the same range that date_in() accepts. */
212  if (DATE_NOT_FINITE(result))
213  /* ok */ ;
214  else if (!IS_VALID_DATE(result))
215  ereport(ERROR,
216  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
217  errmsg("date out of range")));
218 
219  PG_RETURN_DATEADT(result);
220 }
int32 DateADT
Definition: date.h:23
StringInfoData * StringInfo
Definition: stringinfo.h:43
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define IS_VALID_DATE(d)
Definition: timestamp.h:190
#define ERROR
Definition: elog.h:43
#define DATE_NOT_FINITE(j)
Definition: date.h:43
static char * buf
Definition: pg_test_fsync.c:68
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define PG_RETURN_DATEADT(x)
Definition: date.h:65

◆ date_send()

Datum date_send ( PG_FUNCTION_ARGS  )

Definition at line 226 of file date.c.

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

227 {
230 
231  pq_begintypsend(&buf);
232  pq_sendint32(&buf, date);
234 }
int32 DateADT
Definition: date.h:23
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
long date
Definition: pgtypes_date.h:9
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_DATEADT(n)
Definition: date.h:61

◆ date_smaller()

Datum date_smaller ( PG_FUNCTION_ARGS  )

Definition at line 485 of file date.c.

References PG_GETARG_DATEADT, and PG_RETURN_DATEADT.

486 {
487  DateADT dateVal1 = PG_GETARG_DATEADT(0);
488  DateADT dateVal2 = PG_GETARG_DATEADT(1);
489 
490  PG_RETURN_DATEADT((dateVal1 < dateVal2) ? dateVal1 : dateVal2);
491 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_DATEADT(x)
Definition: date.h:65

◆ date_sortsupport()

Datum date_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 459 of file date.c.

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

460 {
462 
463  ssup->comparator = date_fastcmp;
464  PG_RETURN_VOID();
465 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
static int date_fastcmp(Datum x, Datum y, SortSupport ssup)
Definition: date.c:446
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
#define PG_RETURN_VOID()
Definition: fmgr.h:339

◆ date_timestamp()

Datum date_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1079 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

1080 {
1081  DateADT dateVal = PG_GETARG_DATEADT(0);
1082  Timestamp result;
1083 
1084  result = date2timestamp(dateVal);
1085 
1086  PG_RETURN_TIMESTAMP(result);
1087 }
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39

◆ date_timestamptz()

Datum date_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 1123 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

1124 {
1125  DateADT dateVal = PG_GETARG_DATEADT(0);
1126  TimestampTz result;
1127 
1128  result = date2timestamptz(dateVal);
1129 
1130  PG_RETURN_TIMESTAMP(result);
1131 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39

◆ datetime_timestamp()

Datum datetime_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1704 of file date.c.

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

1705 {
1707  TimeADT time = PG_GETARG_TIMEADT(1);
1708  Timestamp result;
1709 
1710  result = date2timestamp(date);
1711  if (!TIMESTAMP_NOT_FINITE(result))
1712  {
1713  result += time;
1714  if (!IS_VALID_TIMESTAMP(result))
1715  ereport(ERROR,
1716  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1717  errmsg("timestamp out of range")));
1718  }
1719 
1720  PG_RETURN_TIMESTAMP(result);
1721 }
int32 DateADT
Definition: date.h:23
int errcode(int sqlerrcode)
Definition: elog.c:570
long date
Definition: pgtypes_date.h:9
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:43
int64 TimeADT
Definition: date.h:25
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ datetimetz_timestamptz()

Datum datetimetz_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 2566 of file date.c.

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

2567 {
2569  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2570  TimestampTz result;
2571 
2572  if (DATE_IS_NOBEGIN(date))
2573  TIMESTAMP_NOBEGIN(result);
2574  else if (DATE_IS_NOEND(date))
2575  TIMESTAMP_NOEND(result);
2576  else
2577  {
2578  /*
2579  * Date's range is wider than timestamp's, so check for boundaries.
2580  * Since dates have the same minimum values as timestamps, only upper
2581  * boundary need be checked for overflow.
2582  */
2584  ereport(ERROR,
2585  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2586  errmsg("date out of range for timestamp")));
2587  result = date * USECS_PER_DAY + time->time + time->zone * USECS_PER_SEC;
2588 
2589  /*
2590  * Since it is possible to go beyond allowed timestamptz range because
2591  * of time zone, check for allowed timestamp range after adding tz.
2592  */
2593  if (!IS_VALID_TIMESTAMP(result))
2594  ereport(ERROR,
2595  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2596  errmsg("date out of range for timestamp")));
2597  }
2598 
2599  PG_RETURN_TIMESTAMP(result);
2600 }
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
int errcode(int sqlerrcode)
Definition: elog.c:570
long date
Definition: pgtypes_date.h:9
#define ERROR
Definition: elog.h:43
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define USECS_PER_DAY
Definition: timestamp.h:91
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
Definition: date.h:27

◆ EncodeSpecialDate()

void EncodeSpecialDate ( DateADT  dt,
char *  str 
)

Definition at line 289 of file date.c.

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

Referenced by date_out(), and JsonEncodeDateTime().

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

◆ GetSQLCurrentDate()

DateADT GetSQLCurrentDate ( void  )

Definition at line 304 of file date.c.

References date2j(), ereport, errcode(), errmsg(), ERROR, GetCurrentTransactionStartTimestamp(), POSTGRES_EPOCH_JDATE, timestamp2tm(), tm, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

Referenced by ExecEvalSQLValueFunction().

305 {
306  TimestampTz ts;
307  struct pg_tm tt,
308  *tm = &tt;
309  fsec_t fsec;
310  int tz;
311 
313 
314  if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
315  ereport(ERROR,
316  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
317  errmsg("timestamp out of range")));
318 
319  return date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
320 }
int64 TimestampTz
Definition: timestamp.h:39
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1777
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:141
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:783
int date2j(int y, int m, int d)
Definition: datetime.c:270
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ GetSQLCurrentTime()

TimeTzADT* GetSQLCurrentTime ( int32  typmod)

Definition at line 326 of file date.c.

References AdjustTimeForTypmod(), ereport, errcode(), errmsg(), ERROR, GetCurrentTransactionStartTimestamp(), palloc(), TimeTzADT::time, timestamp2tm(), tm, and tm2timetz().

Referenced by ExecEvalSQLValueFunction().

327 {
328  TimeTzADT *result;
329  TimestampTz ts;
330  struct pg_tm tt,
331  *tm = &tt;
332  fsec_t fsec;
333  int tz;
334 
336 
337  if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
338  ereport(ERROR,
339  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
340  errmsg("timestamp out of range")));
341 
342  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
343  tm2timetz(tm, fsec, tz, result);
344  AdjustTimeForTypmod(&(result->time), typmod);
345  return result;
346 }
TimeADT time
Definition: date.h:29
int64 TimestampTz
Definition: timestamp.h:39
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1777
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define ERROR
Definition: elog.h:43
int32 fsec_t
Definition: timestamp.h:41
static void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1383
#define ereport(elevel, rest)
Definition: elog.h:141
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:783
static int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:1961
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: date.h:27

◆ GetSQLLocalTime()

TimeADT GetSQLLocalTime ( int32  typmod)

Definition at line 352 of file date.c.

References AdjustTimeForTypmod(), ereport, errcode(), errmsg(), ERROR, GetCurrentTransactionStartTimestamp(), timestamp2tm(), tm, and tm2time().

Referenced by ExecEvalSQLValueFunction().

353 {
354  TimeADT result;
355  TimestampTz ts;
356  struct pg_tm tt,
357  *tm = &tt;
358  fsec_t fsec;
359  int tz;
360 
362 
363  if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
364  ereport(ERROR,
365  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
366  errmsg("timestamp out of range")));
367 
368  tm2time(tm, fsec, &result);
369  AdjustTimeForTypmod(&result, typmod);
370  return result;
371 }
int64 TimestampTz
Definition: timestamp.h:39
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1777
static int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1207
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define ERROR
Definition: elog.h:43
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
static void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1383
#define ereport(elevel, rest)
Definition: elog.h:141
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:783
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ in_range_date_interval()

Datum in_range_date_interval ( PG_FUNCTION_ARGS  )

Definition at line 1013 of file date.c.

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

1014 {
1016  DateADT base = PG_GETARG_DATEADT(1);
1017  Interval *offset = PG_GETARG_INTERVAL_P(2);
1018  bool sub = PG_GETARG_BOOL(3);
1019  bool less = PG_GETARG_BOOL(4);
1020  Timestamp valStamp;
1021  Timestamp baseStamp;
1022 
1023  valStamp = date2timestamp(val);
1024  baseStamp = date2timestamp(base);
1025 
1027  TimestampGetDatum(valStamp),
1028  TimestampGetDatum(baseStamp),
1029  IntervalPGetDatum(offset),
1030  BoolGetDatum(sub),
1031  BoolGetDatum(less));
1032 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int32 DateADT
Definition: date.h:23
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define TimestampGetDatum(X)
Definition: timestamp.h:31
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:624
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define BoolGetDatum(X)
Definition: postgres.h:402
Datum in_range_timestamp_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3325
long val
Definition: informix.c:684

◆ in_range_time_interval()

Datum in_range_time_interval ( PG_FUNCTION_ARGS  )

Definition at line 1830 of file date.c.

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

1831 {
1833  TimeADT base = PG_GETARG_TIMEADT(1);
1834  Interval *offset = PG_GETARG_INTERVAL_P(2);
1835  bool sub = PG_GETARG_BOOL(3);
1836  bool less = PG_GETARG_BOOL(4);
1837  TimeADT sum;
1838 
1839  /*
1840  * Like time_pl_interval/time_mi_interval, we disregard the month and day
1841  * fields of the offset. So our test for negative should too.
1842  */
1843  if (offset->time < 0)
1844  ereport(ERROR,
1845  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
1846  errmsg("invalid preceding or following size in window function")));
1847 
1848  /*
1849  * We can't use time_pl_interval/time_mi_interval here, because their
1850  * wraparound behavior would give wrong (or at least undesirable) answers.
1851  * Fortunately the equivalent non-wrapping behavior is trivial, especially
1852  * since we don't worry about integer overflow.
1853  */
1854  if (sub)
1855  sum = base - offset->time;
1856  else
1857  sum = base + offset->time;
1858 
1859  if (less)
1860  PG_RETURN_BOOL(val <= sum);
1861  else
1862  PG_RETURN_BOOL(val >= sum);
1863 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:45
int64 TimeADT
Definition: date.h:25
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
int errmsg(const char *fmt,...)
Definition: elog.c:784
long val
Definition: informix.c:684

◆ in_range_timetz_interval()

Datum in_range_timetz_interval ( PG_FUNCTION_ARGS  )

Definition at line 2332 of file date.c.

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

2333 {
2335  TimeTzADT *base = PG_GETARG_TIMETZADT_P(1);
2336  Interval *offset = PG_GETARG_INTERVAL_P(2);
2337  bool sub = PG_GETARG_BOOL(3);
2338  bool less = PG_GETARG_BOOL(4);
2339  TimeTzADT sum;
2340 
2341  /*
2342  * Like timetz_pl_interval/timetz_mi_interval, we disregard the month and
2343  * day fields of the offset. So our test for negative should too.
2344  */
2345  if (offset->time < 0)
2346  ereport(ERROR,
2347  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2348  errmsg("invalid preceding or following size in window function")));
2349 
2350  /*
2351  * We can't use timetz_pl_interval/timetz_mi_interval here, because their
2352  * wraparound behavior would give wrong (or at least undesirable) answers.
2353  * Fortunately the equivalent non-wrapping behavior is trivial, especially
2354  * since we don't worry about integer overflow.
2355  */
2356  if (sub)
2357  sum.time = base->time - offset->time;
2358  else
2359  sum.time = base->time + offset->time;
2360  sum.zone = base->zone;
2361 
2362  if (less)
2363  PG_RETURN_BOOL(timetz_cmp_internal(val, &sum) <= 0);
2364  else
2365  PG_RETURN_BOOL(timetz_cmp_internal(val, &sum) >= 0);
2366 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
TimeADT time
Definition: date.h:29
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2135
#define ERROR
Definition: elog.h:43
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
TimeOffset time
Definition: timestamp.h:45
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: date.h:27
long val
Definition: informix.c:684

◆ interval_time()

Datum interval_time ( PG_FUNCTION_ARGS  )

Definition at line 1750 of file date.c.

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

1751 {
1752  Interval *span = PG_GETARG_INTERVAL_P(0);
1753  TimeADT result;
1754  int64 days;
1755 
1756  result = span->time;
1757  if (result >= USECS_PER_DAY)
1758  {
1759  days = result / USECS_PER_DAY;
1760  result -= days * USECS_PER_DAY;
1761  }
1762  else if (result < 0)
1763  {
1764  days = (-result + USECS_PER_DAY - 1) / USECS_PER_DAY;
1765  result += days * USECS_PER_DAY;
1766  }
1767 
1768  PG_RETURN_TIMEADT(result);
1769 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
TimeOffset time
Definition: timestamp.h:45
const char *const days[]
Definition: datetime.c:69
int64 TimeADT
Definition: date.h:25
#define USECS_PER_DAY
Definition: timestamp.h:91

◆ make_date()

Datum make_date ( PG_FUNCTION_ARGS  )

Definition at line 240 of file date.c.

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

241 {
242  struct pg_tm tm;
243  DateADT date;
244  int dterr;
245  bool bc = false;
246 
250 
251  /* Handle negative years as BC */
252  if (tm.tm_year < 0)
253  {
254  bc = true;
255  tm.tm_year = -tm.tm_year;
256  }
257 
258  dterr = ValidateDate(DTK_DATE_M, false, false, bc, &tm);
259 
260  if (dterr != 0)
261  ereport(ERROR,
262  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
263  errmsg("date field value out of range: %d-%02d-%02d",
264  tm.tm_year, tm.tm_mon, tm.tm_mday)));
265 
266  /* Prevent overflow in Julian-day routines */
268  ereport(ERROR,
269  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
270  errmsg("date out of range: %d-%02d-%02d",
271  tm.tm_year, tm.tm_mon, tm.tm_mday)));
272 
274 
275  /* Now check for just-out-of-range dates */
276  if (!IS_VALID_DATE(date))
277  ereport(ERROR,
278  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
279  errmsg("date out of range: %d-%02d-%02d",
280  tm.tm_year, tm.tm_mon, tm.tm_mday)));
281 
282  PG_RETURN_DATEADT(date);
283 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
int32 DateADT
Definition: date.h:23
int errcode(int sqlerrcode)
Definition: elog.c:570
long date
Definition: pgtypes_date.h:9
Definition: pgtime.h:25
#define DTK_DATE_M
Definition: datetime.h:192
static struct pg_tm tm
Definition: localtime.c:108
#define IS_VALID_DATE(d)
Definition: timestamp.h:190
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:155
#define ereport(elevel, rest)
Definition: elog.h:141
int date2j(int y, int m, int d)
Definition: datetime.c:270
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
#define PG_RETURN_DATEADT(x)
Definition: date.h:65
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2451

◆ make_time()

Datum make_time ( PG_FUNCTION_ARGS  )

Definition at line 1311 of file date.c.

References ereport, errcode(), errmsg(), ERROR, HOURS_PER_DAY, MINS_PER_HOUR, PG_GETARG_FLOAT8, PG_GETARG_INT32, PG_RETURN_TIMEADT, rint(), SECS_PER_MINUTE, pg_tm::tm_hour, pg_tm::tm_min, and USECS_PER_SEC.

1312 {
1313  int tm_hour = PG_GETARG_INT32(0);
1314  int tm_min = PG_GETARG_INT32(1);
1315  double sec = PG_GETARG_FLOAT8(2);
1316  TimeADT time;
1317 
1318  /* This should match the checks in DecodeTimeOnly */
1319  if (tm_hour < 0 || tm_min < 0 || tm_min > MINS_PER_HOUR - 1 ||
1320  sec < 0 || sec > SECS_PER_MINUTE ||
1321  tm_hour > HOURS_PER_DAY ||
1322  /* test for > 24:00:00 */
1323  (tm_hour == HOURS_PER_DAY && (tm_min > 0 || sec > 0)))
1324  ereport(ERROR,
1325  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
1326  errmsg("time field value out of range: %d:%02d:%02g",
1327  tm_hour, tm_min, sec)));
1328 
1329  /* This should match tm2time */
1330  time = (((tm_hour * MINS_PER_HOUR + tm_min) * SECS_PER_MINUTE)
1331  * USECS_PER_SEC) + rint(sec * USECS_PER_SEC);
1332 
1333  PG_RETURN_TIMEADT(time);
1334 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
int errcode(int sqlerrcode)
Definition: elog.c:570
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define ERROR
Definition: elog.h:43
#define HOURS_PER_DAY
Definition: timestamp.h:78
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int64 TimeADT
Definition: date.h:25
double rint(double x)
Definition: rint.c:21
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784
int tm_min
Definition: pgtime.h:28

◆ overlaps_time()

Datum overlaps_time ( PG_FUNCTION_ARGS  )

Definition at line 1521 of file date.c.

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

1522 {
1523  /*
1524  * The arguments are TimeADT, but we leave them as generic Datums to avoid
1525  * dereferencing nulls (TimeADT is pass-by-reference!)
1526  */
1527  Datum ts1 = PG_GETARG_DATUM(0);
1528  Datum te1 = PG_GETARG_DATUM(1);
1529  Datum ts2 = PG_GETARG_DATUM(2);
1530  Datum te2 = PG_GETARG_DATUM(3);
1531  bool ts1IsNull = PG_ARGISNULL(0);
1532  bool te1IsNull = PG_ARGISNULL(1);
1533  bool ts2IsNull = PG_ARGISNULL(2);
1534  bool te2IsNull = PG_ARGISNULL(3);
1535 
1536 #define TIMEADT_GT(t1,t2) \
1537  (DatumGetTimeADT(t1) > DatumGetTimeADT(t2))
1538 #define TIMEADT_LT(t1,t2) \
1539  (DatumGetTimeADT(t1) < DatumGetTimeADT(t2))
1540 
1541  /*
1542  * If both endpoints of interval 1 are null, the result is null (unknown).
1543  * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
1544  * take ts1 as the lesser endpoint.
1545  */
1546  if (ts1IsNull)
1547  {
1548  if (te1IsNull)
1549  PG_RETURN_NULL();
1550  /* swap null for non-null */
1551  ts1 = te1;
1552  te1IsNull = true;
1553  }
1554  else if (!te1IsNull)
1555  {
1556  if (TIMEADT_GT(ts1, te1))
1557  {
1558  Datum tt = ts1;
1559 
1560  ts1 = te1;
1561  te1 = tt;
1562  }
1563  }
1564 
1565  /* Likewise for interval 2. */
1566  if (ts2IsNull)
1567  {
1568  if (te2IsNull)
1569  PG_RETURN_NULL();
1570  /* swap null for non-null */
1571  ts2 = te2;
1572  te2IsNull = true;
1573  }
1574  else if (!te2IsNull)
1575  {
1576  if (TIMEADT_GT(ts2, te2))
1577  {
1578  Datum tt = ts2;
1579 
1580  ts2 = te2;
1581  te2 = tt;
1582  }
1583  }
1584 
1585  /*
1586  * At this point neither ts1 nor ts2 is null, so we can consider three
1587  * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
1588  */
1589  if (TIMEADT_GT(ts1, ts2))
1590  {
1591  /*
1592  * This case is ts1 < te2 OR te1 < te2, which may look redundant but
1593  * in the presence of nulls it's not quite completely so.
1594  */
1595  if (te2IsNull)
1596  PG_RETURN_NULL();
1597  if (TIMEADT_LT(ts1, te2))
1598  PG_RETURN_BOOL(true);
1599  if (te1IsNull)
1600  PG_RETURN_NULL();
1601 
1602  /*
1603  * If te1 is not null then we had ts1 <= te1 above, and we just found
1604  * ts1 >= te2, hence te1 >= te2.
1605  */
1606  PG_RETURN_BOOL(false);
1607  }
1608  else if (TIMEADT_LT(ts1, ts2))
1609  {
1610  /* This case is ts2 < te1 OR te2 < te1 */
1611  if (te1IsNull)
1612  PG_RETURN_NULL();
1613  if (TIMEADT_LT(ts2, te1))
1614  PG_RETURN_BOOL(true);
1615  if (te2IsNull)
1616  PG_RETURN_NULL();
1617 
1618  /*
1619  * If te2 is not null then we had ts2 <= te2 above, and we just found
1620  * ts2 >= te1, hence te2 >= te1.
1621  */
1622  PG_RETURN_BOOL(false);
1623  }
1624  else
1625  {
1626  /*
1627  * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
1628  * rather silly way of saying "true if both are nonnull, else null".
1629  */
1630  if (te1IsNull || te2IsNull)
1631  PG_RETURN_NULL();
1632  PG_RETURN_BOOL(true);
1633  }
1634 
1635 #undef TIMEADT_GT
1636 #undef TIMEADT_LT
1637 }
#define TIMEADT_LT(t1, t2)
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
uintptr_t Datum
Definition: postgres.h:367
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define TIMEADT_GT(t1, t2)
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ overlaps_timetz()

Datum overlaps_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2375 of file date.c.

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

2376 {
2377  /*
2378  * The arguments are TimeTzADT *, but we leave them as generic Datums for
2379  * convenience of notation --- and to avoid dereferencing nulls.
2380  */
2381  Datum ts1 = PG_GETARG_DATUM(0);
2382  Datum te1 = PG_GETARG_DATUM(1);
2383  Datum ts2 = PG_GETARG_DATUM(2);
2384  Datum te2 = PG_GETARG_DATUM(3);
2385  bool ts1IsNull = PG_ARGISNULL(0);
2386  bool te1IsNull = PG_ARGISNULL(1);
2387  bool ts2IsNull = PG_ARGISNULL(2);
2388  bool te2IsNull = PG_ARGISNULL(3);
2389 
2390 #define TIMETZ_GT(t1,t2) \
2391  DatumGetBool(DirectFunctionCall2(timetz_gt,t1,t2))
2392 #define TIMETZ_LT(t1,t2) \
2393  DatumGetBool(DirectFunctionCall2(timetz_lt,t1,t2))
2394 
2395  /*
2396  * If both endpoints of interval 1 are null, the result is null (unknown).
2397  * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2398  * take ts1 as the lesser endpoint.
2399  */
2400  if (ts1IsNull)
2401  {
2402  if (te1IsNull)
2403  PG_RETURN_NULL();
2404  /* swap null for non-null */
2405  ts1 = te1;
2406  te1IsNull = true;
2407  }
2408  else if (!te1IsNull)
2409  {
2410  if (TIMETZ_GT(ts1, te1))
2411  {
2412  Datum tt = ts1;
2413 
2414  ts1 = te1;
2415  te1 = tt;
2416  }
2417  }
2418 
2419  /* Likewise for interval 2. */
2420  if (ts2IsNull)
2421  {
2422  if (te2IsNull)
2423  PG_RETURN_NULL();
2424  /* swap null for non-null */
2425  ts2 = te2;
2426  te2IsNull = true;
2427  }
2428  else if (!te2IsNull)
2429  {
2430  if (TIMETZ_GT(ts2, te2))
2431  {
2432  Datum tt = ts2;
2433 
2434  ts2 = te2;
2435  te2 = tt;
2436  }
2437  }
2438 
2439  /*
2440  * At this point neither ts1 nor ts2 is null, so we can consider three
2441  * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2442  */
2443  if (TIMETZ_GT(ts1, ts2))
2444  {
2445  /*
2446  * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2447  * in the presence of nulls it's not quite completely so.
2448  */
2449  if (te2IsNull)
2450  PG_RETURN_NULL();
2451  if (TIMETZ_LT(ts1, te2))
2452  PG_RETURN_BOOL(true);
2453  if (te1IsNull)
2454  PG_RETURN_NULL();
2455 
2456  /*
2457  * If te1 is not null then we had ts1 <= te1 above, and we just found
2458  * ts1 >= te2, hence te1 >= te2.
2459  */
2460  PG_RETURN_BOOL(false);
2461  }
2462  else if (TIMETZ_LT(ts1, ts2))
2463  {
2464  /* This case is ts2 < te1 OR te2 < te1 */
2465  if (te1IsNull)
2466  PG_RETURN_NULL();
2467  if (TIMETZ_LT(ts2, te1))
2468  PG_RETURN_BOOL(true);
2469  if (te2IsNull)
2470  PG_RETURN_NULL();
2471 
2472  /*
2473  * If te2 is not null then we had ts2 <= te2 above, and we just found
2474  * ts2 >= te1, hence te2 >= te1.
2475  */
2476  PG_RETURN_BOOL(false);
2477  }
2478  else
2479  {
2480  /*
2481  * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2482  * rather silly way of saying "true if both are nonnull, else null".
2483  */
2484  if (te1IsNull || te2IsNull)
2485  PG_RETURN_NULL();
2486  PG_RETURN_BOOL(true);
2487  }
2488 
2489 #undef TIMETZ_GT
2490 #undef TIMETZ_LT
2491 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define TIMETZ_LT(t1, t2)
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
uintptr_t Datum
Definition: postgres.h:367
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define PG_RETURN_NULL()
Definition: fmgr.h:335
#define TIMETZ_GT(t1, t2)

◆ time2tm()

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

Definition at line 1222 of file date.c.

References pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by JsonEncodeDateTime(), time_out(), time_part(), and time_timetz().

1223 {
1224  tm->tm_hour = time / USECS_PER_HOUR;
1225  time -= tm->tm_hour * USECS_PER_HOUR;
1226  tm->tm_min = time / USECS_PER_MINUTE;
1227  time -= tm->tm_min * USECS_PER_MINUTE;
1228  tm->tm_sec = time / USECS_PER_SEC;
1229  time -= tm->tm_sec * USECS_PER_SEC;
1230  *fsec = time;
1231  return 0;
1232 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define USECS_PER_MINUTE
Definition: timestamp.h:93
#define USECS_PER_HOUR
Definition: timestamp.h:92
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ time_cmp()

Datum time_cmp ( PG_FUNCTION_ARGS  )

Definition at line 1472 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_INT32.

Referenced by gbt_timekey_cmp(), and leftmostvalue_time().

1473 {
1474  TimeADT time1 = PG_GETARG_TIMEADT(0);
1475  TimeADT time2 = PG_GETARG_TIMEADT(1);
1476 
1477  if (time1 < time2)
1478  PG_RETURN_INT32(-1);
1479  if (time1 > time2)
1480  PG_RETURN_INT32(1);
1481  PG_RETURN_INT32(0);
1482 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_eq()

Datum time_eq ( PG_FUNCTION_ARGS  )

Definition at line 1418 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timeeq().

1419 {
1420  TimeADT time1 = PG_GETARG_TIMEADT(0);
1421  TimeADT time2 = PG_GETARG_TIMEADT(1);
1422 
1423  PG_RETURN_BOOL(time1 == time2);
1424 }
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_ge()

Datum time_ge ( PG_FUNCTION_ARGS  )

Definition at line 1463 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timege().

1464 {
1465  TimeADT time1 = PG_GETARG_TIMEADT(0);
1466  TimeADT time2 = PG_GETARG_TIMEADT(1);
1467 
1468  PG_RETURN_BOOL(time1 >= time2);
1469 }
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_gt()

Datum time_gt ( PG_FUNCTION_ARGS  )

Definition at line 1454 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timegt().

1455 {
1456  TimeADT time1 = PG_GETARG_TIMEADT(0);
1457  TimeADT time2 = PG_GETARG_TIMEADT(1);
1458 
1459  PG_RETURN_BOOL(time1 > time2);
1460 }
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_hash()

Datum time_hash ( PG_FUNCTION_ARGS  )

Definition at line 1485 of file date.c.

References hashint8().

1486 {
1487  return hashint8(fcinfo);
1488 }
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:83

◆ time_hash_extended()

Datum time_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 1491 of file date.c.

References hashint8extended().

1492 {
1493  return hashint8extended(fcinfo);
1494 }
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103

◆ time_in()

Datum time_in ( PG_FUNCTION_ARGS  )

Definition at line 1170 of file date.c.

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

1171 {
1172  char *str = PG_GETARG_CSTRING(0);
1173 
1174 #ifdef NOT_USED
1175  Oid typelem = PG_GETARG_OID(1);
1176 #endif
1177  int32 typmod = PG_GETARG_INT32(2);
1178  TimeADT result;
1179  fsec_t fsec;
1180  struct pg_tm tt,
1181  *tm = &tt;
1182  int tz;
1183  int nf;
1184  int dterr;
1185  char workbuf[MAXDATELEN + 1];
1186  char *field[MAXDATEFIELDS];
1187  int dtype;
1188  int ftype[MAXDATEFIELDS];
1189 
1190  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
1191  field, ftype, MAXDATEFIELDS, &nf);
1192  if (dterr == 0)
1193  dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
1194  if (dterr != 0)
1195  DateTimeParseError(dterr, str, "time");
1196 
1197  tm2time(tm, fsec, &result);
1198  AdjustTimeForTypmod(&result, typmod);
1199 
1200  PG_RETURN_TIMEADT(result);
1201 }
#define MAXDATELEN
Definition: datetime.h:201
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3739
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
static int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1207
Definition: pgtime.h:25
unsigned int Oid
Definition: postgres_ext.h:31
int DecodeTimeOnly(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:1717
signed int int32
Definition: c.h:346
static struct pg_tm tm
Definition: localtime.c:108
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
static void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1383
#define MAXDATEFIELDS
Definition: datetime.h:203
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:540

◆ time_interval()

Datum time_interval ( PG_FUNCTION_ARGS  )

Definition at line 1727 of file date.c.

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

1728 {
1729  TimeADT time = PG_GETARG_TIMEADT(0);
1730  Interval *result;
1731 
1732  result = (Interval *) palloc(sizeof(Interval));
1733 
1734  result->time = time;
1735  result->day = 0;
1736  result->month = 0;
1737 
1738  PG_RETURN_INTERVAL_P(result);
1739 }
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
int64 TimeADT
Definition: date.h:25
int32 month
Definition: timestamp.h:48
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
void * palloc(Size size)
Definition: mcxt.c:924

◆ time_larger()

Datum time_larger ( PG_FUNCTION_ARGS  )

Definition at line 1497 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

1498 {
1499  TimeADT time1 = PG_GETARG_TIMEADT(0);
1500  TimeADT time2 = PG_GETARG_TIMEADT(1);
1501 
1502  PG_RETURN_TIMEADT((time1 > time2) ? time1 : time2);
1503 }
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_le()

Datum time_le ( PG_FUNCTION_ARGS  )

Definition at line 1445 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timele().

1446 {
1447  TimeADT time1 = PG_GETARG_TIMEADT(0);
1448  TimeADT time2 = PG_GETARG_TIMEADT(1);
1449 
1450  PG_RETURN_BOOL(time1 <= time2);
1451 }
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_lt()

Datum time_lt ( PG_FUNCTION_ARGS  )

Definition at line 1436 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timelt().

1437 {
1438  TimeADT time1 = PG_GETARG_TIMEADT(0);
1439  TimeADT time2 = PG_GETARG_TIMEADT(1);
1440 
1441  PG_RETURN_BOOL(time1 < time2);
1442 }
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_mi_interval()

Datum time_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 1812 of file date.c.

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

1813 {
1814  TimeADT time = PG_GETARG_TIMEADT(0);
1815  Interval *span = PG_GETARG_INTERVAL_P(1);
1816  TimeADT result;
1817 
1818  result = time - span->time;
1819  result -= result / USECS_PER_DAY * USECS_PER_DAY;
1820  if (result < INT64CONST(0))
1821  result += USECS_PER_DAY;
1822 
1823  PG_RETURN_TIMEADT(result);
1824 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
TimeOffset time
Definition: timestamp.h:45
int64 TimeADT
Definition: date.h:25
#define USECS_PER_DAY
Definition: timestamp.h:91
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_mi_time()

Datum time_mi_time ( PG_FUNCTION_ARGS  )

Definition at line 1775 of file date.c.

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

Referenced by gbt_time_dist(), gbt_time_penalty(), and time_dist().

1776 {
1777  TimeADT time1 = PG_GETARG_TIMEADT(0);
1778  TimeADT time2 = PG_GETARG_TIMEADT(1);
1779  Interval *result;
1780 
1781  result = (Interval *) palloc(sizeof(Interval));
1782 
1783  result->month = 0;
1784  result->day = 0;
1785  result->time = time1 - time2;
1786 
1787  PG_RETURN_INTERVAL_P(result);
1788 }
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
int64 TimeADT
Definition: date.h:25
int32 month
Definition: timestamp.h:48
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
void * palloc(Size size)
Definition: mcxt.c:924

◆ time_ne()

Datum time_ne ( PG_FUNCTION_ARGS  )

Definition at line 1427 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

1428 {
1429  TimeADT time1 = PG_GETARG_TIMEADT(0);
1430  TimeADT time2 = PG_GETARG_TIMEADT(1);
1431 
1432  PG_RETURN_BOOL(time1 != time2);
1433 }
int64 TimeADT
Definition: date.h:25
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_out()

Datum time_out ( PG_FUNCTION_ARGS  )

Definition at line 1235 of file date.c.

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

1236 {
1237  TimeADT time = PG_GETARG_TIMEADT(0);
1238  char *result;
1239  struct pg_tm tt,
1240  *tm = &tt;
1241  fsec_t fsec;
1242  char buf[MAXDATELEN + 1];
1243 
1244  time2tm(time, tm, &fsec);
1245  EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf);
1246 
1247  result = pstrdup(buf);
1248  PG_RETURN_CSTRING(result);
1249 }
#define MAXDATELEN
Definition: datetime.h:201
char * pstrdup(const char *in)
Definition: mcxt.c:1161
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:3943
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1222
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
static char * buf
Definition: pg_test_fsync.c:68
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
int DateStyle
Definition: globals.c:115

◆ time_part()

Datum time_part ( PG_FUNCTION_ARGS  )

Definition at line 1870 of file date.c.

References DecodeSpecial(), DecodeUnits(), downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_EPOCH, DTK_HOUR, DTK_ISOYEAR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_TZ, DTK_TZ_HOUR, DTK_TZ_MINUTE, DTK_YEAR, ereport, errcode(), errmsg(), ERROR, PG_GETARG_TEXT_PP, PG_GETARG_TIMEADT, PG_RETURN_FLOAT8, 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.

1871 {
1872  text *units = PG_GETARG_TEXT_PP(0);
1873  TimeADT time = PG_GETARG_TIMEADT(1);
1874  float8 result;
1875  int type,
1876  val;
1877  char *lowunits;
1878 
1879  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
1880  VARSIZE_ANY_EXHDR(units),
1881  false);
1882 
1883  type = DecodeUnits(0, lowunits, &val);
1884  if (type == UNKNOWN_FIELD)
1885  type = DecodeSpecial(0, lowunits, &val);
1886 
1887  if (type == UNITS)
1888  {
1889  fsec_t fsec;
1890  struct pg_tm tt,
1891  *tm = &tt;
1892 
1893  time2tm(time, tm, &fsec);
1894 
1895  switch (val)
1896  {
1897  case DTK_MICROSEC:
1898  result = tm->tm_sec * 1000000.0 + fsec;
1899  break;
1900 
1901  case DTK_MILLISEC:
1902  result = tm->tm_sec * 1000.0 + fsec / 1000.0;
1903  break;
1904 
1905  case DTK_SECOND:
1906  result = tm->tm_sec + fsec / 1000000.0;
1907  break;
1908 
1909  case DTK_MINUTE:
1910  result = tm->tm_min;
1911  break;
1912 
1913  case DTK_HOUR:
1914  result = tm->tm_hour;
1915  break;
1916 
1917  case DTK_TZ:
1918  case DTK_TZ_MINUTE:
1919  case DTK_TZ_HOUR:
1920  case DTK_DAY:
1921  case DTK_MONTH:
1922  case DTK_QUARTER:
1923  case DTK_YEAR:
1924  case DTK_DECADE:
1925  case DTK_CENTURY:
1926  case DTK_MILLENNIUM:
1927  case DTK_ISOYEAR:
1928  default:
1929  ereport(ERROR,
1930  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1931  errmsg("\"time\" units \"%s\" not recognized",
1932  lowunits)));
1933  result = 0;
1934  }
1935  }
1936  else if (type == RESERV && val == DTK_EPOCH)
1937  {
1938  result = time / 1000000.0;
1939  }
1940  else
1941  {
1942  ereport(ERROR,
1943  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1944  errmsg("\"time\" units \"%s\" not recognized",
1945  lowunits)));
1946  result = 0;
1947  }
1948 
1949  PG_RETURN_FLOAT8(result);
1950 }
#define DTK_TZ_HOUR
Definition: datetime.h:178
#define DTK_CENTURY
Definition: datetime.h:170
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define DTK_YEAR
Definition: datetime.h:168
int tm_hour
Definition: pgtime.h:29
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:131
#define DTK_QUARTER
Definition: datetime.h:167
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:356
#define DTK_MILLENNIUM
Definition: datetime.h:171
int errcode(int sqlerrcode)
Definition: elog.c:570
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1222
#define UNKNOWN_FIELD
Definition: datetime.h:125
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3702
Definition: pgtime.h:25
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
static struct pg_tm tm
Definition: localtime.c:108
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_MILLISEC
Definition: datetime.h:172
#define DTK_DECADE
Definition: datetime.h:169
#define DTK_TZ
Definition: datetime.h:147
#define DTK_HOUR
Definition: datetime.h:163
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:491
#define DTK_SECOND
Definition: datetime.h:161
#define DTK_ISOYEAR
Definition: datetime.h:180
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
#define DTK_TZ_MINUTE
Definition: datetime.h:179
#define ereport(elevel, rest)
Definition: elog.h:141
#define DTK_MINUTE
Definition: datetime.h:162
#define DTK_MICROSEC
Definition: datetime.h:173
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
#define DTK_DAY
Definition: datetime.h:164
#define RESERV
Definition: datetime.h:91
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3011
#define DTK_EPOCH
Definition: datetime.h:153
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:684

◆ time_pl_interval()

Datum time_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 1794 of file date.c.

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

1795 {
1796  TimeADT time = PG_GETARG_TIMEADT(0);
1797  Interval *span = PG_GETARG_INTERVAL_P(1);
1798  TimeADT result;
1799 
1800  result = time + span->time;
1801  result -= result / USECS_PER_DAY * USECS_PER_DAY;
1802  if (result < INT64CONST(0))
1803  result += USECS_PER_DAY;
1804 
1805  PG_RETURN_TIMEADT(result);
1806 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
TimeOffset time
Definition: timestamp.h:45
int64 TimeADT
Definition: date.h:25
#define USECS_PER_DAY
Definition: timestamp.h:91
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_recv()

Datum time_recv ( PG_FUNCTION_ARGS  )

Definition at line 1255 of file date.c.

References AdjustTimeForTypmod(), buf, ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_TIMEADT, pq_getmsgint64(), and USECS_PER_DAY.

1256 {
1258 
1259 #ifdef NOT_USED
1260  Oid typelem = PG_GETARG_OID(1);
1261 #endif
1262  int32 typmod = PG_GETARG_INT32(2);
1263  TimeADT result;
1264 
1265  result = pq_getmsgint64(buf);
1266 
1267  if (result < INT64CONST(0) || result > USECS_PER_DAY)
1268  ereport(ERROR,
1269  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1270  errmsg("time out of range")));
1271 
1272  AdjustTimeForTypmod(&result, typmod);
1273 
1274  PG_RETURN_TIMEADT(result);
1275 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
StringInfoData * StringInfo
Definition: stringinfo.h:43
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:346
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
int64 TimeADT
Definition: date.h:25
#define USECS_PER_DAY
Definition: timestamp.h:91
static void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1383
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455

◆ time_scale()

Datum time_scale ( PG_FUNCTION_ARGS  )

Definition at line 1363 of file date.c.

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

1364 {
1365  TimeADT time = PG_GETARG_TIMEADT(0);
1366  int32 typmod = PG_GETARG_INT32(1);
1367  TimeADT result;
1368 
1369  result = time;
1370  AdjustTimeForTypmod(&result, typmod);
1371 
1372  PG_RETURN_TIMEADT(result);
1373 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
signed int int32
Definition: c.h:346
int64 TimeADT
Definition: date.h:25
static void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1383
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_send()

Datum time_send ( PG_FUNCTION_ARGS  )

Definition at line 1281 of file date.c.

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

1282 {
1283  TimeADT time = PG_GETARG_TIMEADT(0);
1285 
1286  pq_begintypsend(&buf);
1287  pq_sendint64(&buf, time);
1289 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static char * buf
Definition: pg_test_fsync.c:68
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_smaller()

Datum time_smaller ( PG_FUNCTION_ARGS  )

Definition at line 1506 of file date.c.

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

1507 {
1508  TimeADT time1 = PG_GETARG_TIMEADT(0);
1509  TimeADT time2 = PG_GETARG_TIMEADT(1);
1510 
1511  PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
1512 }
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62

◆ time_support()

Datum time_support ( PG_FUNCTION_ARGS  )

Definition at line 1343 of file date.c.

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

1344 {
1345  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1346  Node *ret = NULL;
1347 
1348  if (IsA(rawreq, SupportRequestSimplify))
1349  {
1351 
1352  ret = TemporalSimplify(MAX_TIME_PRECISION, (Node *) req->fcall);
1353  }
1354 
1355  PG_RETURN_POINTER(ret);
1356 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define IsA(nodeptr, _type_)
Definition: nodes.h:575
Definition: nodes.h:524
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
Node * TemporalSimplify(int32 max_precis, Node *node)
Definition: datetime.c:4467
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ time_timetz()

Datum time_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2508 of file date.c.

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

2509 {
2510  TimeADT time = PG_GETARG_TIMEADT(0);
2511  TimeTzADT *result;
2512  struct pg_tm tt,
2513  *tm = &tt;
2514  fsec_t fsec;
2515  int tz;
2516 
2517  GetCurrentDateTime(tm);
2518  time2tm(time, tm, &fsec);
2520 
2521  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2522 
2523  result->time = time;
2524  result->zone = tz;
2525 
2526  PG_RETURN_TIMETZADT_P(result);
2527 }
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:348
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
TimeADT time
Definition: date.h:29
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1222
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
int32 zone
Definition: date.h:30
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1441
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
#define PG_GETARG_TIMEADT(n)
Definition: date.h:62
void * palloc(Size size)
Definition: mcxt.c:924
Definition: date.h:27

◆ timestamp_cmp_date()

Datum timestamp_cmp_date ( PG_FUNCTION_ARGS  )

Definition at line 911 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_INT32, and timestamp_cmp_internal().

912 {
914  DateADT dateVal = PG_GETARG_DATEADT(1);
915  Timestamp dt2;
916 
917  dt2 = date2timestamp(dateVal);
918 
920 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564

◆ timestamp_date()

Datum timestamp_date ( PG_FUNCTION_ARGS  )

Definition at line 1093 of file date.c.

References date2j(), DATE_NOBEGIN, DATE_NOEND, ereport, errcode(), errmsg(), ERROR, PG_GETARG_TIMESTAMP, PG_RETURN_DATEADT, POSTGRES_EPOCH_JDATE, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, tm, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

1094 {
1096  DateADT result;
1097  struct pg_tm tt,
1098  *tm = &tt;
1099  fsec_t fsec;
1100 
1101  if (TIMESTAMP_IS_NOBEGIN(timestamp))
1102  DATE_NOBEGIN(result);
1103  else if (TIMESTAMP_IS_NOEND(timestamp))
1104  DATE_NOEND(result);
1105  else
1106  {
1107  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1108  ereport(ERROR,
1109  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1110  errmsg("timestamp out of range")));
1111 
1112  result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
1113  }
1114 
1115  PG_RETURN_DATEADT(result);
1116 }
int32 DateADT
Definition: date.h:23
int64 timestamp
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1777
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define DATE_NOBEGIN(j)
Definition: date.h:39
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:141
int64 Timestamp
Definition: timestamp.h:38
int date2j(int y, int m, int d)
Definition: datetime.c:270
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
#define PG_RETURN_DATEADT(x)
Definition: date.h:65
#define DATE_NOEND(j)
Definition: date.h:41

◆ timestamp_eq_date()

Datum timestamp_eq_date ( PG_FUNCTION_ARGS  )

Definition at line 839 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

840 {
842  DateADT dateVal = PG_GETARG_DATEADT(1);
843  Timestamp dt2;
844 
845  dt2 = date2timestamp(dateVal);
846 
847  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
848 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ timestamp_ge_date()

Datum timestamp_ge_date ( PG_FUNCTION_ARGS  )

Definition at line 899 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

900 {
902  DateADT dateVal = PG_GETARG_DATEADT(1);
903  Timestamp dt2;
904 
905  dt2 = date2timestamp(dateVal);
906 
907  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
908 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ timestamp_gt_date()

Datum timestamp_gt_date ( PG_FUNCTION_ARGS  )

Definition at line 875 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

876 {
878  DateADT dateVal = PG_GETARG_DATEADT(1);
879  Timestamp dt2;
880 
881  dt2 = date2timestamp(dateVal);
882 
883  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
884 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ timestamp_le_date()

Datum timestamp_le_date ( PG_FUNCTION_ARGS  )

Definition at line 887 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

888 {
890  DateADT dateVal = PG_GETARG_DATEADT(1);
891  Timestamp dt2;
892 
893  dt2 = date2timestamp(dateVal);
894 
895  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
896 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ timestamp_lt_date()

Datum timestamp_lt_date ( PG_FUNCTION_ARGS  )

Definition at line 863 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

864 {
866  DateADT dateVal = PG_GETARG_DATEADT(1);
867  Timestamp dt2;
868 
869  dt2 = date2timestamp(dateVal);
870 
871  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
872 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ timestamp_ne_date()

Datum timestamp_ne_date ( PG_FUNCTION_ARGS  )

Definition at line 851 of file date.c.

References date2timestamp(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

852 {
854  DateADT dateVal = PG_GETARG_DATEADT(1);
855  Timestamp dt2;
856 
857  dt2 = date2timestamp(dateVal);
858 
859  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
860 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int32 DateADT
Definition: date.h:23
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
int64 Timestamp
Definition: timestamp.h:38
static Timestamp date2timestamp(DateADT dateVal)
Definition: date.c:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ timestamp_time()

Datum timestamp_time ( PG_FUNCTION_ARGS  )

Definition at line 1643 of file date.c.

References ereport, errcode(), errmsg(), ERROR, MINS_PER_HOUR, PG_GETARG_TIMESTAMP, PG_RETURN_NULL, PG_RETURN_TIMEADT, SECS_PER_MINUTE, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, and USECS_PER_SEC.

1644 {
1646  TimeADT result;
1647  struct pg_tm tt,
1648  *tm = &tt;
1649  fsec_t fsec;
1650 
1651  if (TIMESTAMP_NOT_FINITE(timestamp))
1652  PG_RETURN_NULL();
1653 
1654  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1655  ereport(ERROR,
1656  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1657  errmsg("timestamp out of range")));
1658 
1659  /*
1660  * Could also do this with time = (timestamp / USECS_PER_DAY *
1661  * USECS_PER_DAY) - timestamp;
1662  */
1663  result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1664  USECS_PER_SEC) + fsec;
1665 
1666  PG_RETURN_TIMEADT(result);
1667 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 timestamp
int tm_hour
Definition: pgtime.h:29
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1777
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
#define MINS_PER_HOUR
Definition: timestamp.h:89
static struct pg_tm tm
Definition: localtime.c:108
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:43
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
#define ereport(elevel, rest)
Definition: elog.h:141
int64 Timestamp
Definition: timestamp.h:38
int errmsg(const char *fmt,...)
Definition: elog.c:784
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ timestamptz_cmp_date()

Datum timestamptz_cmp_date ( PG_FUNCTION_ARGS  )

Definition at line 995 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_INT32, and timestamptz_cmp_internal.

996 {
998  DateADT dateVal = PG_GETARG_DATEADT(1);
999  TimestampTz dt2;
1000 
1001  dt2 = date2timestamptz(dateVal);
1002 
1004 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ timestamptz_date()

Datum timestamptz_date ( PG_FUNCTION_ARGS  )

Definition at line 1138 of file date.c.

References date2j(), DATE_NOBEGIN, DATE_NOEND, ereport, errcode(), errmsg(), ERROR, PG_GETARG_TIMESTAMP, PG_RETURN_DATEADT, POSTGRES_EPOCH_JDATE, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, tm, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

1139 {
1141  DateADT result;
1142  struct pg_tm tt,
1143  *tm = &tt;
1144  fsec_t fsec;
1145  int tz;
1146 
1147  if (TIMESTAMP_IS_NOBEGIN(timestamp))
1148  DATE_NOBEGIN(result);
1149  else if (TIMESTAMP_IS_NOEND(timestamp))
1150  DATE_NOEND(result);
1151  else
1152  {
1153  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1154  ereport(ERROR,
1155  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1156  errmsg("timestamp out of range")));
1157 
1158  result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
1159  }
1160 
1161  PG_RETURN_DATEADT(result);
1162 }
int32 DateADT
Definition: date.h:23
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1777
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define DATE_NOBEGIN(j)
Definition: date.h:39
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:141
int date2j(int y, int m, int d)
Definition: datetime.c:270
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
#define PG_RETURN_DATEADT(x)
Definition: date.h:65
#define DATE_NOEND(j)
Definition: date.h:41

◆ timestamptz_eq_date()

Datum timestamptz_eq_date ( PG_FUNCTION_ARGS  )

Definition at line 923 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamptz_cmp_internal.

924 {
926  DateADT dateVal = PG_GETARG_DATEADT(1);
927  TimestampTz dt2;
928 
929  dt2 = date2timestamptz(dateVal);
930 
932 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ timestamptz_ge_date()

Datum timestamptz_ge_date ( PG_FUNCTION_ARGS  )

Definition at line 983 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamptz_cmp_internal.

984 {
986  DateADT dateVal = PG_GETARG_DATEADT(1);
987  TimestampTz dt2;
988 
989  dt2 = date2timestamptz(dateVal);
990 
992 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ timestamptz_gt_date()

Datum timestamptz_gt_date ( PG_FUNCTION_ARGS  )

Definition at line 959 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamptz_cmp_internal.

960 {
962  DateADT dateVal = PG_GETARG_DATEADT(1);
963  TimestampTz dt2;
964 
965  dt2 = date2timestamptz(dateVal);
966 
968 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ timestamptz_le_date()

Datum timestamptz_le_date ( PG_FUNCTION_ARGS  )

Definition at line 971 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamptz_cmp_internal.

972 {
974  DateADT dateVal = PG_GETARG_DATEADT(1);
975  TimestampTz dt2;
976 
977  dt2 = date2timestamptz(dateVal);
978 
980 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ timestamptz_lt_date()

Datum timestamptz_lt_date ( PG_FUNCTION_ARGS  )

Definition at line 947 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamptz_cmp_internal.

948 {
950  DateADT dateVal = PG_GETARG_DATEADT(1);
951  TimestampTz dt2;
952 
953  dt2 = date2timestamptz(dateVal);
954 
956 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ timestamptz_ne_date()

Datum timestamptz_ne_date ( PG_FUNCTION_ARGS  )

Definition at line 935 of file date.c.

References date2timestamptz(), PG_GETARG_DATEADT, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamptz_cmp_internal.

936 {
938  DateADT dateVal = PG_GETARG_DATEADT(1);
939  TimestampTz dt2;
940 
941  dt2 = date2timestamptz(dateVal);
942 
944 }
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:592
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ timestamptz_time()

Datum timestamptz_time ( PG_FUNCTION_ARGS  )

Definition at line 1673 of file date.c.

References ereport, errcode(), errmsg(), ERROR, MINS_PER_HOUR, PG_GETARG_TIMESTAMP, PG_RETURN_NULL, PG_RETURN_TIMEADT, SECS_PER_MINUTE, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, and USECS_PER_SEC.

1674 {
1676  TimeADT result;
1677  struct pg_tm tt,
1678  *tm = &tt;
1679  int tz;
1680  fsec_t fsec;
1681 
1682  if (TIMESTAMP_NOT_FINITE(timestamp))
1683  PG_RETURN_NULL();
1684 
1685  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1686  ereport(ERROR,
1687  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1688  errmsg("timestamp out of range")));
1689 
1690  /*
1691  * Could also do this with time = (timestamp / USECS_PER_DAY *
1692  * USECS_PER_DAY) - timestamp;
1693  */
1694  result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
1695  USECS_PER_SEC) + fsec;
1696 
1697  PG_RETURN_TIMEADT(result);
1698 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
int tm_hour
Definition: pgtime.h:29
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1777
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
#define MINS_PER_HOUR
Definition: timestamp.h:89
static struct pg_tm tm
Definition: localtime.c:108
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:43
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ timestamptz_timetz()

Datum timestamptz_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2534 of file date.c.

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

2535 {
2537  TimeTzADT *result;
2538  struct pg_tm tt,
2539  *tm = &tt;
2540  int tz;
2541  fsec_t fsec;
2542 
2543  if (TIMESTAMP_NOT_FINITE(timestamp))
2544  PG_RETURN_NULL();
2545 
2546  if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
2547  ereport(ERROR,
2548  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2549  errmsg("timestamp out of range")));
2550 
2551  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2552 
2553  tm2timetz(tm, fsec, tz, result);
2554 
2555  PG_RETURN_TIMETZADT_P(result);
2556 }
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1777
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:43
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:141
static int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:1961
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: date.h:27
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ timetypmodin()

Datum timetypmodin ( PG_FUNCTION_ARGS  )

Definition at line 1292 of file date.c.

References anytime_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

1293 {
1295 
1296  PG_RETURN_INT32(anytime_typmodin(false, ta));
1297 }
static int32 anytime_typmodin(bool istz, ArrayType *ta)
Definition: date.c:51
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251

◆ timetypmodout()

Datum timetypmodout ( PG_FUNCTION_ARGS  )

Definition at line 1300 of file date.c.

References anytime_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

1301 {
1302  int32 typmod = PG_GETARG_INT32(0);
1303 
1304  PG_RETURN_CSTRING(anytime_typmodout(false, typmod));
1305 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
signed int int32
Definition: c.h:346
static char * anytime_typmodout(bool istz, int32 typmod)
Definition: date.c:94
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352

◆ timetz2tm()

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

Definition at line 2095 of file date.c.

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

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

2096 {
2097  TimeOffset trem = time->time;
2098 
2099  tm->tm_hour = trem / USECS_PER_HOUR;
2100  trem -= tm->tm_hour * USECS_PER_HOUR;
2101  tm->tm_min = trem / USECS_PER_MINUTE;
2102  trem -= tm->tm_min * USECS_PER_MINUTE;
2103  tm->tm_sec = trem / USECS_PER_SEC;
2104  *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2105 
2106  if (tzp != NULL)
2107  *tzp = time->zone;
2108 
2109  return 0;
2110 }
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define USECS_PER_MINUTE
Definition: timestamp.h:93
int32 zone
Definition: date.h:30
#define USECS_PER_HOUR
Definition: timestamp.h:92
int64 TimeOffset
Definition: timestamp.h:40
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ timetz_cmp()

Datum timetz_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2216 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_INT32, and timetz_cmp_internal().

Referenced by leftmostvalue_timetz().

2217 {
2218  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2219  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2220 
2221  PG_RETURN_INT32(timetz_cmp_internal(time1, time2));
2222 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2135
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
Definition: date.h:27

◆ timetz_cmp_internal()

static int timetz_cmp_internal ( TimeTzADT time1,
TimeTzADT time2 
)
static

Definition at line 2135 of file date.c.

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

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

2136 {
2137  TimeOffset t1,
2138  t2;
2139 
2140  /* Primary sort is by true (GMT-equivalent) time */
2141  t1 = time1->time + (time1->zone * USECS_PER_SEC);
2142  t2 = time2->time + (time2->zone * USECS_PER_SEC);
2143 
2144  if (t1 > t2)
2145  return 1;
2146  if (t1 < t2)
2147  return -1;
2148 
2149  /*
2150  * If same GMT time, sort by timezone; we only want to say that two
2151  * timetz's are equal if both the time and zone parts are equal.
2152  */
2153  if (time1->zone > time2->zone)
2154  return 1;
2155  if (time1->zone < time2->zone)
2156  return -1;
2157 
2158  return 0;
2159 }
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
int32 zone
Definition: date.h:30
int64 TimeOffset
Definition: timestamp.h:40

◆ timetz_eq()

Datum timetz_eq ( PG_FUNCTION_ARGS  )

Definition at line 2162 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

2163 {
2164  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2165  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2166 
2167  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) == 0);
2168 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2135
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Definition: date.h:27

◆ timetz_ge()

Datum timetz_ge ( PG_FUNCTION_ARGS  )

Definition at line 2207 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

2208 {
2209  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2210  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2211 
2212  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) >= 0);
2213 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2135
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Definition: date.h:27

◆ timetz_gt()

Datum timetz_gt ( PG_FUNCTION_ARGS  )

Definition at line 2198 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

2199 {
2200  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2201  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2202 
2203  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) > 0);
2204 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2135
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Definition: date.h:27

◆ timetz_hash()

Datum timetz_hash ( PG_FUNCTION_ARGS  )

Definition at line 2225 of file date.c.

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

2226 {
2228  uint32 thash;
2229 
2230  /*
2231  * To avoid any problems with padding bytes in the struct, we figure the
2232  * field hashes separately and XOR them.
2233  */
2235  Int64GetDatumFast(key->time)));
2236  thash ^= DatumGetUInt32(hash_uint32(key->zone));
2237  PG_RETURN_UINT32(thash);
2238 }
#define DatumGetUInt32(X)
Definition: postgres.h:486
TimeADT time
Definition: date.h:29
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:83
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:616
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:345
Datum hash_uint32(uint32 k)
Definition: hashfn.c:613
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
unsigned int uint32
Definition: c.h:358
#define Int64GetDatumFast(X)
Definition: postgres.h:775
Definition: date.h:27

◆ timetz_hash_extended()

Datum timetz_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2241 of file date.c.

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

2242 {
2244  Datum seed = PG_GETARG_DATUM(1);
2245  uint64 thash;
2246 
2247  /* Same approach as timetz_hash */
2249  Int64GetDatumFast(key->time),
2250  seed));
2252  DatumGetInt64(seed)));
2253  PG_RETURN_UINT64(thash);
2254 }
TimeADT time
Definition: date.h:29
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:358
int32 zone
Definition: date.h:30
#define DatumGetInt64(X)
Definition: postgres.h:607
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
uintptr_t Datum
Definition: postgres.h:367
#define Int64GetDatumFast(X)
Definition: postgres.h:775
#define DatumGetUInt64(X)
Definition: postgres.h:634
Definition: date.h:27
Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfn.c:634
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618

◆ timetz_in()

Datum timetz_in ( PG_FUNCTION_ARGS  )

Definition at line 1971 of file date.c.

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

1972 {
1973  char *str = PG_GETARG_CSTRING(0);
1974 
1975 #ifdef NOT_USED
1976  Oid typelem = PG_GETARG_OID(1);
1977 #endif
1978  int32 typmod = PG_GETARG_INT32(2);
1979  TimeTzADT *result;
1980  fsec_t fsec;
1981  struct pg_tm tt,
1982  *tm = &tt;
1983  int tz;
1984  int nf;
1985  int dterr;
1986  char workbuf[MAXDATELEN + 1];
1987  char *field[MAXDATEFIELDS];
1988  int dtype;
1989  int ftype[MAXDATEFIELDS];
1990 
1991  dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
1992  field, ftype, MAXDATEFIELDS, &nf);
1993  if (dterr == 0)
1994  dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
1995  if (dterr != 0)
1996  DateTimeParseError(dterr, str, "time with time zone");
1997 
1998  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
1999  tm2timetz(tm, fsec, tz, result);
2000  AdjustTimeForTypmod(&(result->time), typmod);
2001 
2002  PG_RETURN_TIMETZADT_P(result);
2003 }
#define MAXDATELEN
Definition: datetime.h:201
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3739
TimeADT time
Definition: date.h:29
Definition: pgtime.h:25
unsigned int Oid
Definition: postgres_ext.h:31
int DecodeTimeOnly(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:1717
signed int int32
Definition: c.h:346
static struct pg_tm tm
Definition: localtime.c:108
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
int32 fsec_t
Definition: timestamp.h:41
static void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1383
#define MAXDATEFIELDS
Definition: datetime.h:203
static int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:1961
void * palloc(Size size)
Definition: mcxt.c:924
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:540
Definition: date.h:27

◆ timetz_izone()

Datum timetz_izone ( PG_FUNCTION_ARGS  )

Definition at line 2789 of file date.c.

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

2790 {
2792  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2793  TimeTzADT *result;
2794  int tz;
2795 
2796  if (zone->month != 0 || zone->day != 0)
2797  ereport(ERROR,
2798  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2799  errmsg("interval time zone \"%s\" must not include months or days",
2801  PointerGetDatum(zone))))));
2802 
2803  tz = -(zone->time / USECS_PER_SEC);
2804 
2805  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2806 
2807  result->time = time->time + (time->zone - tz) * USECS_PER_SEC;
2808  while (result->time < INT64CONST(0))
2809  result->time += USECS_PER_DAY;
2810  while (result->time >= USECS_PER_DAY)
2811  result->time -= USECS_PER_DAY;
2812 
2813  result->zone = tz;
2814 
2815  PG_RETURN_TIMETZADT_P(result);
2816 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
#define PointerGetDatum(X)
Definition: postgres.h:556
int errcode(int sqlerrcode)
Definition: elog.c:570
Datum interval_out(PG_FUNCTION_ARGS)
Definition: timestamp.c:947
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:616
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:43
#define DatumGetCString(X)
Definition: postgres.h:566
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
#define ereport(elevel, rest)
Definition: elog.h:141
int32 month
Definition: timestamp.h:48
Definition: zic.c:98
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: date.h:27

◆ timetz_larger()

Datum timetz_larger ( PG_FUNCTION_ARGS  )

Definition at line 2257 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

2258 {
2259  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2260  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2261  TimeTzADT *result;
2262 
2263  if (timetz_cmp_internal(time1, time2) > 0)
2264  result = time1;
2265  else
2266  result = time2;
2267  PG_RETURN_TIMETZADT_P(result);
2268 }
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2135
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
Definition: date.h:27

◆ timetz_le()

Datum timetz_le ( PG_FUNCTION_ARGS  )

Definition at line 2189 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

2190 {
2191  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2192  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2193 
2194  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) <= 0);
2195 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2135
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Definition: date.h:27

◆ timetz_lt()

Datum timetz_lt ( PG_FUNCTION_ARGS  )

Definition at line 2180 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

2181 {
2182  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2183  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2184 
2185  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) < 0);
2186 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2135
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Definition: date.h:27

◆ timetz_mi_interval()

Datum timetz_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 2310 of file date.c.

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

2311 {
2312  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2313  Interval *span = PG_GETARG_INTERVAL_P(1);
2314  TimeTzADT *result;
2315 
2316  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2317 
2318  result->time = time->time - span->time;
2319  result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2320  if (result->time < INT64CONST(0))
2321  result->time += USECS_PER_DAY;
2322 
2323  result->zone = time->zone;
2324 
2325  PG_RETURN_TIMETZADT_P(result);
2326 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
TimeADT time
Definition: date.h:29
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
void * palloc(Size size)
Definition: mcxt.c:924
Definition: date.h:27

◆ timetz_ne()

Datum timetz_ne ( PG_FUNCTION_ARGS  )

Definition at line 2171 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

2172 {
2173  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2174  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2175 
2176  PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) != 0);
2177 }
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2135
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Definition: date.h:27

◆ timetz_out()

Datum timetz_out ( PG_FUNCTION_ARGS  )

Definition at line 2006 of file date.c.

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

2007 {
2008  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2009  char *result;
2010  struct pg_tm tt,
2011  *tm = &tt;
2012  fsec_t fsec;
2013  int tz;
2014  char buf[MAXDATELEN + 1];
2015 
2016  timetz2tm(time, tm, &fsec, &tz);
2017  EncodeTimeOnly(tm, fsec, true, tz, DateStyle, buf);
2018 
2019  result = pstrdup(buf);
2020  PG_RETURN_CSTRING(result);
2021 }
#define MAXDATELEN
Definition: datetime.h:201
char * pstrdup(const char *in)
Definition: mcxt.c:1161
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:3943
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
int32 fsec_t
Definition: timestamp.h:41
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: date.c:2095
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
int DateStyle
Definition: globals.c:115
Definition: date.h:27

◆ timetz_part()

Datum timetz_part ( PG_FUNCTION_ARGS  )

Definition at line 2607 of file date.c.

References DecodeSpecial(), DecodeUnits(), downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_EPOCH, DTK_HOUR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_TZ, DTK_TZ_HOUR, DTK_TZ_MINUTE, DTK_YEAR, ereport, errcode(), errmsg(), ERROR, FMODULO, PG_GETARG_TEXT_PP, PG_GETARG_TIMETZADT_P, PG_RETURN_FLOAT8, 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.

2608 {
2609  text *units = PG_GETARG_TEXT_PP(0);
2610  TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
2611  float8 result;
2612  int type,
2613  val;
2614  char *lowunits;
2615 
2616  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
2617  VARSIZE_ANY_EXHDR(units),
2618  false);
2619 
2620  type = DecodeUnits(0, lowunits, &val);
2621  if (type == UNKNOWN_FIELD)
2622  type = DecodeSpecial(0, lowunits, &val);
2623 
2624  if (type == UNITS)
2625  {
2626  double dummy;
2627  int tz;
2628  fsec_t fsec;
2629  struct pg_tm tt,
2630  *tm = &tt;
2631 
2632  timetz2tm(time, tm, &fsec, &tz);
2633 
2634  switch (val)
2635  {
2636  case DTK_TZ:
2637  result = -tz;
2638  break;
2639 
2640  case DTK_TZ_MINUTE:
2641  result = -tz;
2642  result /= SECS_PER_MINUTE;
2643  FMODULO(result, dummy, (double) SECS_PER_MINUTE);
2644  break;
2645 
2646  case DTK_TZ_HOUR:
2647  dummy = -tz;
2648  FMODULO(dummy, result, (double) SECS_PER_HOUR);
2649  break;
2650 
2651  case DTK_MICROSEC:
2652  result = tm->tm_sec * 1000000.0 + fsec;
2653  break;
2654 
2655  case DTK_MILLISEC:
2656  result = tm->tm_sec * 1000.0 + fsec / 1000.0;
2657  break;
2658 
2659  case DTK_SECOND:
2660  result = tm->tm_sec + fsec / 1000000.0;
2661  break;
2662 
2663  case DTK_MINUTE:
2664  result = tm->tm_min;
2665  break;
2666 
2667  case DTK_HOUR:
2668  result = tm->tm_hour;
2669  break;
2670 
2671  case DTK_DAY:
2672  case DTK_MONTH:
2673  case DTK_QUARTER:
2674  case DTK_YEAR:
2675  case DTK_DECADE:
2676  case DTK_CENTURY:
2677  case DTK_MILLENNIUM:
2678  default:
2679  ereport(ERROR,
2680  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2681  errmsg("\"time with time zone\" units \"%s\" not recognized",
2682  lowunits)));
2683  result = 0;
2684  }
2685  }
2686  else if (type == RESERV && val == DTK_EPOCH)
2687  {
2688  result = time->time / 1000000.0 + time->zone;
2689  }
2690  else
2691  {
2692  ereport(ERROR,
2693  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2694  errmsg("\"time with time zone\" units \"%s\" not recognized",
2695  lowunits)));
2696  result = 0;
2697  }
2698 
2699  PG_RETURN_FLOAT8(result);
2700 }
#define DTK_TZ_HOUR
Definition: datetime.h:178
#define DTK_CENTURY
Definition: datetime.h:170
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
TimeADT time
Definition: date.h:29
#define DTK_YEAR
Definition: datetime.h:168
int tm_hour
Definition: pgtime.h:29
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:131
#define DTK_QUARTER
Definition: datetime.h:167
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:356
#define FMODULO(t, q, u)
Definition: datetime.h:238
#define DTK_MILLENNIUM
Definition: datetime.h:171
int errcode(int sqlerrcode)
Definition: elog.c:570
#define UNKNOWN_FIELD
Definition: datetime.h:125
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3702
Definition: pgtime.h:25
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
static struct pg_tm tm
Definition: localtime.c:108
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_MILLISEC
Definition: datetime.h:172
#define DTK_DECADE
Definition: datetime.h:169
#define DTK_TZ
Definition: datetime.h:147
#define DTK_HOUR
Definition: datetime.h:163
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:491
#define DTK_SECOND
Definition: datetime.h:161
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int32 fsec_t
Definition: timestamp.h:41
#define DTK_TZ_MINUTE
Definition: datetime.h:179
#define ereport(elevel, rest)
Definition: elog.h:141
#define SECS_PER_HOUR
Definition: timestamp.h:87
#define DTK_MINUTE
Definition: datetime.h:162
#define DTK_MICROSEC
Definition: datetime.h:173
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: date.c:2095
#define DTK_DAY
Definition: datetime.h:164
#define RESERV
Definition: datetime.h:91
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3011
#define DTK_EPOCH
Definition: datetime.h:153
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549
int tm_sec
Definition: pgtime.h:27
Definition: date.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:684

◆ timetz_pl_interval()

Datum timetz_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 2288 of file date.c.

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

2289 {
2290  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2291  Interval *span = PG_GETARG_INTERVAL_P(1);
2292  TimeTzADT *result;
2293 
2294  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2295 
2296  result->time = time->time + span->time;
2297  result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2298  if (result->time < INT64CONST(0))
2299  result->time += USECS_PER_DAY;
2300 
2301  result->zone = time->zone;
2302 
2303  PG_RETURN_TIMETZADT_P(result);
2304 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
TimeADT time
Definition: date.h:29
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
void * palloc(Size size)
Definition: mcxt.c:924
Definition: date.h:27

◆ timetz_recv()

Datum timetz_recv ( PG_FUNCTION_ARGS  )

Definition at line 2027 of file date.c.

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.

2028 {
2030 
2031 #ifdef NOT_USED
2032  Oid typelem = PG_GETARG_OID(1);
2033 #endif
2034  int32 typmod = PG_GETARG_INT32(2);
2035  TimeTzADT *result;
2036 
2037  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2038 
2039  result->time = pq_getmsgint64(buf);
2040 
2041  if (result->time < INT64CONST(0) || result->time > USECS_PER_DAY)
2042  ereport(ERROR,
2043  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2044  errmsg("time out of range")));
2045 
2046  result->zone = pq_getmsgint(buf, sizeof(result->zone));
2047 
2048  /* Check for sane GMT displacement; see notes in datatype/timestamp.h */
2049  if (result->zone <= -TZDISP_LIMIT || result->zone >= TZDISP_LIMIT)
2050  ereport(ERROR,
2051  (errcode(ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE),
2052  errmsg("time zone displacement out of range")));
2053 
2054  AdjustTimeForTypmod(&(result->time), typmod);
2055 
2056  PG_RETURN_TIMETZADT_P(result);
2057 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
TimeADT time
Definition: date.h:29
StringInfoData * StringInfo
Definition: stringinfo.h:43
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:346
#define ERROR
Definition: elog.h:43
int32 zone
Definition: date.h:30
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define USECS_PER_DAY
Definition: timestamp.h:91
static void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1383
#define ereport(elevel, rest)
Definition: elog.h:141
#define TZDISP_LIMIT
Definition: timestamp.h:104
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
Definition: date.h:27

◆ timetz_scale()

Datum timetz_scale ( PG_FUNCTION_ARGS  )

Definition at line 2117 of file date.c.

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

2118 {
2119  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2120  int32 typmod = PG_GETARG_INT32(1);
2121  TimeTzADT *result;
2122 
2123  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2124 
2125  result->time = time->time;
2126  result->zone = time->zone;
2127 
2128  AdjustTimeForTypmod(&(result->time), typmod);
2129 
2130  PG_RETURN_TIMETZADT_P(result);
2131 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
TimeADT time
Definition: date.h:29
signed int int32
Definition: c.h:346
int32 zone
Definition: date.h:30
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
static void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1383
void * palloc(Size size)
Definition: mcxt.c:924
Definition: date.h:27

◆ timetz_send()

Datum timetz_send ( PG_FUNCTION_ARGS  )

Definition at line 2063 of file date.c.

References buf, PG_GETARG_TIMETZADT_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendint32(), pq_sendint64(), TimeTzADT::time, and TimeTzADT::zone.

2064 {
2065  TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
2067 
2068  pq_begintypsend(&buf);
2069  pq_sendint64(&buf, time->time);
2070  pq_sendint32(&buf, time->zone);
2072 }
TimeADT time
Definition: date.h:29
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
int32 zone
Definition: date.h:30
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
Definition: date.h:27

◆ timetz_smaller()

Datum timetz_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2271 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

2272 {
2273  TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2274  TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2275  TimeTzADT *result;
2276 
2277  if (timetz_cmp_internal(time1, time2) < 0)
2278  result = time1;
2279  else
2280  result = time2;
2281  PG_RETURN_TIMETZADT_P(result);
2282 }
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:67
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2135
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
Definition: date.h:27

◆ timetz_time()

Datum timetz_time ( PG_FUNCTION_ARGS  )

Definition at line 2495 of file date.c.

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMEADT, and TimeTzADT::time.

2496 {
2497  TimeTzADT *timetz = PG_GETARG_TIMETZADT_P(0);
2498  TimeADT result;
2499 
2500  /* swallow the time zone and just return the time */
2501  result = timetz->time;
2502 
2503  PG_RETURN_TIMEADT(result);
2504 }
TimeADT time
Definition: date.h:29
#define PG_RETURN_TIMEADT(x)
Definition: date.h:66
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:63
int64 TimeADT
Definition: date.h:25
Definition: date.h:27

◆ timetz_zone()

Datum timetz_zone ( PG_FUNCTION_ARGS  )

Definition at line 2707 of file date.c.

References DecodeTimezoneAbbrev(), DetermineTimeZoneAbbrevOffset(), downcase_truncate_identifier(), DTZ, DYNTZ, ereport, errcode(), errmsg(), ERROR, now(), palloc(), PG_GETARG_TEXT_PP, PG_GETARG_TIMETZADT_P, pg_localtime(), PG_RETURN_TIMETZADT_P, pg_tzset(), text_to_cstring_buffer(), TimeTzADT::time, tm, pg_tm::tm_gmtoff, generate_unaccent_rules::type, TZ, TZ_STRLEN_MAX, USECS_PER_DAY, USECS_PER_SEC, val, and TimeTzADT::zone.

2708 {
2709  text *zone = PG_GETARG_TEXT_PP(0);
2711  TimeTzADT *result;
2712  int tz;
2713  char tzname[TZ_STRLEN_MAX + 1];
2714  char *lowzone;
2715  int type,
2716  val;
2717  pg_tz *tzp;
2718 
2719  /*
2720  * Look up the requested timezone. First we look in the timezone
2721  * abbreviation table (to handle cases like "EST"), and if that fails, we
2722  * look in the timezone database (to handle cases like
2723  * "America/New_York"). (This matches the order in which timestamp input
2724  * checks the cases; it's important because the timezone database unwisely
2725  * uses a few zone names that are identical to offset abbreviations.)
2726  */
2727  text_to_cstring_buffer(zone, tzname, sizeof(tzname));
2728 
2729  /* DecodeTimezoneAbbrev requires lowercase input */
2730  lowzone = downcase_truncate_identifier(tzname,
2731  strlen(tzname),
2732  false);
2733 
2734  type = DecodeTimezoneAbbrev(0, lowzone, &val, &tzp);
2735 
2736  if (type == TZ || type == DTZ)
2737  {
2738  /* fixed-offset abbreviation */
2739  tz = -val;
2740  }
2741  else if (type == DYNTZ)
2742  {
2743  /* dynamic-offset abbreviation, resolve using current time */
2744  pg_time_t now = (pg_time_t) time(NULL);
2745  struct pg_tm *tm;
2746 
2747  tm = pg_localtime(&now, tzp);
2748  tz = DetermineTimeZoneAbbrevOffset(tm, tzname, tzp);