Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
date.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <time.h>
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "common/hashfn.h"
#include "common/int.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/supportnodes.h"
#include "parser/scansup.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/numeric.h"
#include "utils/skipsupport.h"
#include "utils/sortsupport.h"
Include dependency graph for date.c:

Go to the source code of this file.

Macros

#define TIMEADT_GT(t1, t2)    (DatumGetTimeADT(t1) > DatumGetTimeADT(t2))
 
#define TIMEADT_LT(t1, t2)    (DatumGetTimeADT(t1) < DatumGetTimeADT(t2))
 
#define TIMETZ_GT(t1, t2)    DatumGetBool(DirectFunctionCall2(timetz_gt,t1,t2))
 
#define TIMETZ_LT(t1, t2)    DatumGetBool(DirectFunctionCall2(timetz_lt,t1,t2))
 

Functions

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

Macro Definition Documentation

◆ TIMEADT_GT

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

◆ TIMEADT_LT

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

◆ TIMETZ_GT

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

◆ TIMETZ_LT

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

Function Documentation

◆ AdjustTimeForTypmod()

void AdjustTimeForTypmod ( TimeADT time,
int32  typmod 
)

Definition at line 1792 of file date.c.

1793{
1794 static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1795 INT64CONST(1000000),
1796 INT64CONST(100000),
1797 INT64CONST(10000),
1798 INT64CONST(1000),
1799 INT64CONST(100),
1800 INT64CONST(10),
1801 INT64CONST(1)
1802 };
1803
1804 static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1805 INT64CONST(500000),
1806 INT64CONST(50000),
1807 INT64CONST(5000),
1808 INT64CONST(500),
1809 INT64CONST(50),
1810 INT64CONST(5),
1811 INT64CONST(0)
1812 };
1813
1814 if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1815 {
1816 if (*time >= INT64CONST(0))
1817 *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1818 TimeScales[typmod];
1819 else
1820 *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1821 TimeScales[typmod]);
1822 }
1823}
#define INT64CONST(x)
Definition: c.h:553
int64_t int64
Definition: c.h:536
#define MAX_TIME_PRECISION
Definition: date.h:45

References INT64CONST, and MAX_TIME_PRECISION.

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

◆ anytime_typmod_check()

int32 anytime_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 72 of file date.c.

73{
74 if (typmod < 0)
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:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150

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

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

◆ anytime_typmodin()

static int32 anytime_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 51 of file date.c.

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)
64 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
65 errmsg("invalid type modifier")));
66
67 return anytime_typmod_check(istz, tl[0]);
68}
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:233
int32_t int32
Definition: c.h:535
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:72

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

Referenced by timetypmodin(), and timetztypmodin().

◆ anytime_typmodout()

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

Definition at line 94 of file date.c.

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 pstrdup(tz);
102}
char * pstrdup(const char *in)
Definition: mcxt.c:1759
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43

References psprintf(), and pstrdup().

Referenced by timetypmodout(), and timetztypmodout().

◆ date2timestamp()

static TimestampTz date2timestamp ( DateADT  dateVal)
static

Definition at line 673 of file date.c.

674{
675 return date2timestamp_opt_overflow(dateVal, NULL);
676}
Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:629

References date2timestamp_opt_overflow().

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

◆ date2timestamp_no_overflow()

double date2timestamp_no_overflow ( DateADT  dateVal)

Definition at line 785 of file date.c.

786{
787 double result;
788
789 if (DATE_IS_NOBEGIN(dateVal))
790 result = -DBL_MAX;
791 else if (DATE_IS_NOEND(dateVal))
792 result = DBL_MAX;
793 else
794 {
795 /* date is days since 2000, timestamp is microseconds since same... */
796 result = dateVal * (double) USECS_PER_DAY;
797 }
798
799 return result;
800}
#define USECS_PER_DAY
Definition: timestamp.h:131
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40

References DATE_IS_NOBEGIN, DATE_IS_NOEND, and USECS_PER_DAY.

Referenced by convert_timevalue_to_scalar().

◆ date2timestamp_opt_overflow()

Timestamp date2timestamp_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 629 of file date.c.

630{
631 Timestamp result;
632
633 if (overflow)
634 *overflow = 0;
635
636 if (DATE_IS_NOBEGIN(dateVal))
637 TIMESTAMP_NOBEGIN(result);
638 else if (DATE_IS_NOEND(dateVal))
639 TIMESTAMP_NOEND(result);
640 else
641 {
642 /*
643 * Since dates have the same minimum values as timestamps, only upper
644 * boundary need be checked for overflow.
645 */
647 {
648 if (overflow)
649 {
650 *overflow = 1;
651 TIMESTAMP_NOEND(result);
652 return result;
653 }
654 else
655 {
657 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
658 errmsg("date out of range for timestamp")));
659 }
660 }
661
662 /* date is days since 2000, timestamp is microseconds since same... */
663 result = dateVal * USECS_PER_DAY;
664 }
665
666 return result;
667}
int64 Timestamp
Definition: timestamp.h:38
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:159
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:253
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:235
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:164

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

Referenced by cvt_date_timestamp(), date2timestamp(), and date_cmp_timestamp_internal().

◆ date2timestamptz()

static TimestampTz date2timestamptz ( DateADT  dateVal)
static

Definition at line 769 of file date.c.

770{
771 return date2timestamptz_opt_overflow(dateVal, NULL);
772}
TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:689

References date2timestamptz_opt_overflow().

Referenced by date_timestamptz().

◆ date2timestamptz_opt_overflow()

TimestampTz date2timestamptz_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 689 of file date.c.

690{
691 TimestampTz result;
692 struct pg_tm tt,
693 *tm = &tt;
694 int tz;
695
696 if (overflow)
697 *overflow = 0;
698
699 if (DATE_IS_NOBEGIN(dateVal))
700 TIMESTAMP_NOBEGIN(result);
701 else if (DATE_IS_NOEND(dateVal))
702 TIMESTAMP_NOEND(result);
703 else
704 {
705 /*
706 * Since dates have the same minimum values as timestamps, only upper
707 * boundary need be checked for overflow.
708 */
710 {
711 if (overflow)
712 {
713 *overflow = 1;
714 TIMESTAMP_NOEND(result);
715 return result;
716 }
717 else
718 {
720 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
721 errmsg("date out of range for timestamp")));
722 }
723 }
724
726 &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
727 tm->tm_hour = 0;
728 tm->tm_min = 0;
729 tm->tm_sec = 0;
731
732 result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
733
734 /*
735 * Since it is possible to go beyond allowed timestamptz range because
736 * of time zone, check for allowed timestamp range after adding tz.
737 */
738 if (!IS_VALID_TIMESTAMP(result))
739 {
740 if (overflow)
741 {
742 if (result < MIN_TIMESTAMP)
743 {
744 *overflow = -1;
745 TIMESTAMP_NOBEGIN(result);
746 }
747 else
748 {
749 *overflow = 1;
750 TIMESTAMP_NOEND(result);
751 }
752 }
753 else
754 {
756 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
757 errmsg("date out of range for timestamp")));
758 }
759 }
760 }
761
762 return result;
763}
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1604
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:321
int64 TimestampTz
Definition: timestamp.h:39
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:267
#define USECS_PER_SEC
Definition: timestamp.h:134
#define MIN_TIMESTAMP
Definition: timestamp.h:256
static struct pg_tm tm
Definition: localtime.c:104
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
Definition: pgtime.h:35
int tm_hour
Definition: pgtime.h:38
int tm_mday
Definition: pgtime.h:39
int tm_mon
Definition: pgtime.h:40
int tm_min
Definition: pgtime.h:37
int tm_sec
Definition: pgtime.h:36
int tm_year
Definition: pgtime.h:41

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

Referenced by cvt_date_timestamptz(), date2timestamptz(), and date_cmp_timestamptz_internal().

◆ date_cmp()

Datum date_cmp ( PG_FUNCTION_ARGS  )

Definition at line 445 of file date.c.

446{
447 DateADT dateVal1 = PG_GETARG_DATEADT(0);
448 DateADT dateVal2 = PG_GETARG_DATEADT(1);
449
450 if (dateVal1 < dateVal2)
451 PG_RETURN_INT32(-1);
452 else if (dateVal1 > dateVal2)
455}
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:89
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354

References PG_GETARG_DATEADT, and PG_RETURN_INT32.

Referenced by compareDatetime(), gbt_date_ssup_cmp(), and gbt_datekey_cmp().

◆ date_cmp_timestamp()

Datum date_cmp_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 879 of file date.c.

880{
881 DateADT dateVal = PG_GETARG_DATEADT(0);
883
885}
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition: date.c:808
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:63

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

◆ date_cmp_timestamp_internal()

int32 date_cmp_timestamp_internal ( DateADT  dateVal,
Timestamp  dt2 
)

Definition at line 808 of file date.c.

809{
810 Timestamp dt1;
811 int overflow;
812
813 dt1 = date2timestamp_opt_overflow(dateVal, &overflow);
814 if (overflow > 0)
815 {
816 /* dt1 is larger than any finite timestamp, but less than infinity */
817 return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
818 }
819 Assert(overflow == 0); /* -1 case cannot occur */
820
821 return timestamp_cmp_internal(dt1, dt2);
822}
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2210
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:167
Assert(PointerIsAligned(start, uint64))

References Assert(), date2timestamp_opt_overflow(), timestamp_cmp_internal(), and TIMESTAMP_IS_NOEND.

Referenced by cmpDateToTimestamp(), date_cmp_timestamp(), date_eq_timestamp(), date_ge_timestamp(), date_gt_timestamp(), date_le_timestamp(), date_lt_timestamp(), date_ne_timestamp(), timestamp_cmp_date(), timestamp_eq_date(), timestamp_ge_date(), timestamp_gt_date(), timestamp_le_date(), timestamp_lt_date(), and timestamp_ne_date().

◆ date_cmp_timestamptz()

Datum date_cmp_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 963 of file date.c.

964{
965 DateADT dateVal = PG_GETARG_DATEADT(0);
967
969}
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition: date.c:888
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:64

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

◆ date_cmp_timestamptz_internal()

int32 date_cmp_timestamptz_internal ( DateADT  dateVal,
TimestampTz  dt2 
)

Definition at line 888 of file date.c.

889{
890 TimestampTz dt1;
891 int overflow;
892
893 dt1 = date2timestamptz_opt_overflow(dateVal, &overflow);
894 if (overflow > 0)
895 {
896 /* dt1 is larger than any finite timestamp, but less than infinity */
897 return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
898 }
899 if (overflow < 0)
900 {
901 /* dt1 is less than any finite timestamp, but more than -infinity */
902 return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
903 }
904
905 return timestamptz_cmp_internal(dt1, dt2);
906}
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:162
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:143

References date2timestamptz_opt_overflow(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, and timestamptz_cmp_internal.

Referenced by cmpDateToTimestampTz(), date_cmp_timestamptz(), date_eq_timestamptz(), date_ge_timestamptz(), date_gt_timestamptz(), date_le_timestamptz(), date_lt_timestamptz(), date_ne_timestamptz(), timestamptz_cmp_date(), timestamptz_eq_date(), timestamptz_ge_date(), timestamptz_gt_date(), timestamptz_le_date(), timestamptz_lt_date(), and timestamptz_ne_date().

◆ date_decrement()

static Datum date_decrement ( Relation  rel,
Datum  existing,
bool *  underflow 
)
static

Definition at line 467 of file date.c.

468{
469 DateADT dexisting = DatumGetDateADT(existing);
470
471 if (dexisting == DATEVAL_NOBEGIN)
472 {
473 /* return value is undefined */
474 *underflow = true;
475 return (Datum) 0;
476 }
477
478 *underflow = false;
479 return DateADTGetDatum(dexisting - 1);
480}
static Datum DateADTGetDatum(DateADT X)
Definition: date.h:72
static DateADT DatumGetDateADT(Datum X)
Definition: date.h:54
#define DATEVAL_NOBEGIN
Definition: date.h:36
uint64_t Datum
Definition: postgres.h:70

References DateADTGetDatum(), DATEVAL_NOBEGIN, and DatumGetDateADT().

Referenced by date_skipsupport().

◆ date_eq()

Datum date_eq ( PG_FUNCTION_ARGS  )

Definition at line 391 of file date.c.

392{
393 DateADT dateVal1 = PG_GETARG_DATEADT(0);
394 DateADT dateVal2 = PG_GETARG_DATEADT(1);
395
396 PG_RETURN_BOOL(dateVal1 == dateVal2);
397}
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_dateeq().

◆ date_eq_timestamp()

Datum date_eq_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 825 of file date.c.

826{
827 DateADT dateVal = PG_GETARG_DATEADT(0);
829
831}

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

◆ date_eq_timestamptz()

Datum date_eq_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 909 of file date.c.

910{
911 DateADT dateVal = PG_GETARG_DATEADT(0);
913
915}

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

◆ date_finite()

Datum date_finite ( PG_FUNCTION_ARGS  )

Definition at line 524 of file date.c.

525{
527
529}
#define DATE_NOT_FINITE(j)
Definition: date.h:43
long date
Definition: pgtypes_date.h:9

References DATE_NOT_FINITE, PG_GETARG_DATEADT, and PG_RETURN_BOOL.

◆ date_ge()

Datum date_ge ( PG_FUNCTION_ARGS  )

Definition at line 436 of file date.c.

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

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_datege().

◆ date_ge_timestamp()

Datum date_ge_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 870 of file date.c.

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

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

◆ date_ge_timestamptz()

Datum date_ge_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 954 of file date.c.

955{
956 DateADT dateVal = PG_GETARG_DATEADT(0);
958
960}

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

◆ date_gt()

Datum date_gt ( PG_FUNCTION_ARGS  )

Definition at line 427 of file date.c.

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

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_dategt().

◆ date_gt_timestamp()

Datum date_gt_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 852 of file date.c.

853{
854 DateADT dateVal = PG_GETARG_DATEADT(0);
856
858}

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

◆ date_gt_timestamptz()

Datum date_gt_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 936 of file date.c.

937{
938 DateADT dateVal = PG_GETARG_DATEADT(0);
940
942}

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

◆ date_in()

Datum date_in ( PG_FUNCTION_ARGS  )

Definition at line 114 of file date.c.

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

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

◆ date_increment()

static Datum date_increment ( Relation  rel,
Datum  existing,
bool *  overflow 
)
static

Definition at line 483 of file date.c.

484{
485 DateADT dexisting = DatumGetDateADT(existing);
486
487 if (dexisting == DATEVAL_NOEND)
488 {
489 /* return value is undefined */
490 *overflow = true;
491 return (Datum) 0;
492 }
493
494 *overflow = false;
495 return DateADTGetDatum(dexisting + 1);
496}
#define DATEVAL_NOEND
Definition: date.h:37

References DateADTGetDatum(), DATEVAL_NOEND, and DatumGetDateADT().

Referenced by date_skipsupport().

◆ date_larger()

Datum date_larger ( PG_FUNCTION_ARGS  )

Definition at line 532 of file date.c.

533{
534 DateADT dateVal1 = PG_GETARG_DATEADT(0);
535 DateADT dateVal2 = PG_GETARG_DATEADT(1);
536
537 PG_RETURN_DATEADT((dateVal1 > dateVal2) ? dateVal1 : dateVal2);
538}

References PG_GETARG_DATEADT, and PG_RETURN_DATEADT.

◆ date_le()

Datum date_le ( PG_FUNCTION_ARGS  )

Definition at line 418 of file date.c.

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

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_datele().

◆ date_le_timestamp()

Datum date_le_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 861 of file date.c.

862{
863 DateADT dateVal = PG_GETARG_DATEADT(0);
865
867}

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

◆ date_le_timestamptz()

Datum date_le_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 945 of file date.c.

946{
947 DateADT dateVal = PG_GETARG_DATEADT(0);
949
951}

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

◆ date_lt()

Datum date_lt ( PG_FUNCTION_ARGS  )

Definition at line 409 of file date.c.

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

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

Referenced by gbt_datelt().

◆ date_lt_timestamp()

Datum date_lt_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 843 of file date.c.

844{
845 DateADT dateVal = PG_GETARG_DATEADT(0);
847
849}

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

◆ date_lt_timestamptz()

Datum date_lt_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 927 of file date.c.

928{
929 DateADT dateVal = PG_GETARG_DATEADT(0);
931
933}

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

◆ date_mi()

Datum date_mi ( PG_FUNCTION_ARGS  )

Definition at line 552 of file date.c.

553{
554 DateADT dateVal1 = PG_GETARG_DATEADT(0);
555 DateADT dateVal2 = PG_GETARG_DATEADT(1);
556
557 if (DATE_NOT_FINITE(dateVal1) || DATE_NOT_FINITE(dateVal2))
559 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
560 errmsg("cannot subtract infinite dates")));
561
562 PG_RETURN_INT32((int32) (dateVal1 - dateVal2));
563}

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

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

◆ date_mi_interval()

Datum date_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 1331 of file date.c.

1332{
1333 DateADT dateVal = PG_GETARG_DATEADT(0);
1334 Interval *span = PG_GETARG_INTERVAL_P(1);
1335 Timestamp dateStamp;
1336
1337 dateStamp = date2timestamp(dateVal);
1338
1340 TimestampGetDatum(dateStamp),
1341 PointerGetDatum(span));
1342}
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3204
static TimestampTz date2timestamp(DateADT dateVal)
Definition: date.c:673
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:684
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
static Datum TimestampGetDatum(Timestamp X)
Definition: timestamp.h:46
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:65

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

◆ date_mii()

Datum date_mii ( PG_FUNCTION_ARGS  )

Definition at line 593 of file date.c.

594{
595 DateADT dateVal = PG_GETARG_DATEADT(0);
597 DateADT result;
598
599 if (DATE_NOT_FINITE(dateVal))
600 PG_RETURN_DATEADT(dateVal); /* can't change infinity */
601
602 result = dateVal - days;
603
604 /* Check for integer overflow and out-of-allowed-range */
605 if ((days >= 0 ? (result > dateVal) : (result < dateVal)) ||
606 !IS_VALID_DATE(result))
608 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
609 errmsg("date out of range")));
610
611 PG_RETURN_DATEADT(result);
612}
const char *const days[]
Definition: datetime.c:84
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269

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

◆ date_ne()

Datum date_ne ( PG_FUNCTION_ARGS  )

Definition at line 400 of file date.c.

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

References PG_GETARG_DATEADT, and PG_RETURN_BOOL.

◆ date_ne_timestamp()

Datum date_ne_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 834 of file date.c.

835{
836 DateADT dateVal = PG_GETARG_DATEADT(0);
838
840}

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

◆ date_ne_timestamptz()

Datum date_ne_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 918 of file date.c.

919{
920 DateADT dateVal = PG_GETARG_DATEADT(0);
922
924}

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

◆ date_out()

Datum date_out ( PG_FUNCTION_ARGS  )

Definition at line 185 of file date.c.

186{
188 char *result;
189 struct pg_tm tt,
190 *tm = &tt;
191 char buf[MAXDATELEN + 1];
192
195 else
196 {
198 &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
200 }
201
202 result = pstrdup(buf);
203 PG_RETURN_CSTRING(result);
204}
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4349
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:302
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
int DateStyle
Definition: globals.c:125
static char * buf
Definition: pg_test_fsync.c:72

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

Referenced by ExecGetJsonValueItemString().

◆ date_pl_interval()

Datum date_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 1311 of file date.c.

1312{
1313 DateADT dateVal = PG_GETARG_DATEADT(0);
1314 Interval *span = PG_GETARG_INTERVAL_P(1);
1315 Timestamp dateStamp;
1316
1317 dateStamp = date2timestamp(dateVal);
1318
1320 TimestampGetDatum(dateStamp),
1321 PointerGetDatum(span));
1322}
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3087

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

◆ date_pli()

Datum date_pli ( PG_FUNCTION_ARGS  )

Definition at line 569 of file date.c.

570{
571 DateADT dateVal = PG_GETARG_DATEADT(0);
573 DateADT result;
574
575 if (DATE_NOT_FINITE(dateVal))
576 PG_RETURN_DATEADT(dateVal); /* can't change infinity */
577
578 result = dateVal + days;
579
580 /* Check for integer overflow and out-of-allowed-range */
581 if ((days >= 0 ? (result < dateVal) : (result > dateVal)) ||
582 !IS_VALID_DATE(result))
584 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
585 errmsg("date out of range")));
586
587 PG_RETURN_DATEADT(result);
588}

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

◆ date_recv()

Datum date_recv ( PG_FUNCTION_ARGS  )

Definition at line 210 of file date.c.

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

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

◆ date_send()

Datum date_send ( PG_FUNCTION_ARGS  )

Definition at line 232 of file date.c.

233{
236
240}
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:144

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

◆ date_skipsupport()

Datum date_skipsupport ( PG_FUNCTION_ARGS  )

Definition at line 499 of file date.c.

500{
502
503 sksup->decrement = date_decrement;
504 sksup->increment = date_increment;
507
509}
static Datum date_decrement(Relation rel, Datum existing, bool *underflow)
Definition: date.c:467
static Datum date_increment(Relation rel, Datum existing, bool *overflow)
Definition: date.c:483
#define PG_RETURN_VOID()
Definition: fmgr.h:349
struct SkipSupportData * SkipSupport
Definition: skipsupport.h:50
SkipSupportIncDec decrement
Definition: skipsupport.h:91
SkipSupportIncDec increment
Definition: skipsupport.h:92

References date_decrement(), date_increment(), DateADTGetDatum(), DATEVAL_NOBEGIN, DATEVAL_NOEND, SkipSupportData::decrement, SkipSupportData::high_elem, SkipSupportData::increment, SkipSupportData::low_elem, PG_GETARG_POINTER, and PG_RETURN_VOID.

◆ date_smaller()

Datum date_smaller ( PG_FUNCTION_ARGS  )

Definition at line 541 of file date.c.

542{
543 DateADT dateVal1 = PG_GETARG_DATEADT(0);
544 DateADT dateVal2 = PG_GETARG_DATEADT(1);
545
546 PG_RETURN_DATEADT((dateVal1 < dateVal2) ? dateVal1 : dateVal2);
547}

References PG_GETARG_DATEADT, and PG_RETURN_DATEADT.

◆ date_sortsupport()

Datum date_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 458 of file date.c.

459{
461
464}
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
int ssup_datum_int32_cmp(Datum x, Datum y, SortSupport ssup)
Definition: tuplesort.c:3158

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

◆ date_timestamp()

Datum date_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1348 of file date.c.

1349{
1350 DateADT dateVal = PG_GETARG_DATEADT(0);
1351 Timestamp result;
1352
1353 result = date2timestamp(dateVal);
1354
1355 PG_RETURN_TIMESTAMP(result);
1356}
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:67

References date2timestamp(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

Referenced by executeDateTimeMethod().

◆ date_timestamptz()

Datum date_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 1433 of file date.c.

1434{
1435 DateADT dateVal = PG_GETARG_DATEADT(0);
1436 TimestampTz result;
1437
1438 result = date2timestamptz(dateVal);
1439
1440 PG_RETURN_TIMESTAMP(result);
1441}
static TimestampTz date2timestamptz(DateADT dateVal)
Definition: date.c:769

References date2timestamptz(), PG_GETARG_DATEADT, and PG_RETURN_TIMESTAMP.

Referenced by executeDateTimeMethod().

◆ datetime_timestamp()

Datum datetime_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2113 of file date.c.

2114{
2116 TimeADT time = PG_GETARG_TIMEADT(1);
2117 Timestamp result;
2118
2119 result = date2timestamp(date);
2120 if (!TIMESTAMP_NOT_FINITE(result))
2121 {
2122 result += time;
2123 if (!IS_VALID_TIMESTAMP(result))
2124 ereport(ERROR,
2125 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2126 errmsg("timestamp out of range")));
2127 }
2128
2129 PG_RETURN_TIMESTAMP(result);
2130}
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:169
#define PG_GETARG_TIMEADT(n)
Definition: date.h:90
int64 TimeADT
Definition: date.h:25

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

◆ datetimetz_timestamptz()

Datum datetimetz_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 3033 of file date.c.

3034{
3037 TimestampTz result;
3038
3039 if (DATE_IS_NOBEGIN(date))
3040 TIMESTAMP_NOBEGIN(result);
3041 else if (DATE_IS_NOEND(date))
3042 TIMESTAMP_NOEND(result);
3043 else
3044 {
3045 /*
3046 * Date's range is wider than timestamp's, so check for boundaries.
3047 * Since dates have the same minimum values as timestamps, only upper
3048 * boundary need be checked for overflow.
3049 */
3051 ereport(ERROR,
3052 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3053 errmsg("date out of range for timestamp")));
3054 result = date * USECS_PER_DAY + time->time + time->zone * USECS_PER_SEC;
3055
3056 /*
3057 * Since it is possible to go beyond allowed timestamptz range because
3058 * of time zone, check for allowed timestamp range after adding tz.
3059 */
3060 if (!IS_VALID_TIMESTAMP(result))
3061 ereport(ERROR,
3062 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3063 errmsg("date out of range for timestamp")));
3064 }
3065
3066 PG_RETURN_TIMESTAMP(result);
3067}
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:91
Definition: date.h:28
TimeADT time
Definition: date.h:29
int32 zone
Definition: date.h:30

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

◆ EncodeSpecialDate()

void EncodeSpecialDate ( DateADT  dt,
char *  str 
)

Definition at line 302 of file date.c.

303{
304 if (DATE_IS_NOBEGIN(dt))
305 strcpy(str, EARLY);
306 else if (DATE_IS_NOEND(dt))
307 strcpy(str, LATE);
308 else /* shouldn't happen */
309 elog(ERROR, "invalid argument for EncodeSpecialDate");
310}
#define elog(elevel,...)
Definition: elog.h:226
#define EARLY
Definition: datetime.h:39
#define LATE
Definition: datetime.h:40

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

Referenced by date_out(), and JsonEncodeDateTime().

◆ extract_date()

Datum extract_date ( PG_FUNCTION_ARGS  )

Definition at line 1131 of file date.c.

1132{
1133 text *units = PG_GETARG_TEXT_PP(0);
1135 int64 intresult;
1136 int type,
1137 val;
1138 char *lowunits;
1139 int year,
1140 mon,
1141 mday;
1142
1143 lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
1144 VARSIZE_ANY_EXHDR(units),
1145 false);
1146
1147 type = DecodeUnits(0, lowunits, &val);
1148 if (type == UNKNOWN_FIELD)
1149 type = DecodeSpecial(0, lowunits, &val);
1150
1151 if (DATE_NOT_FINITE(date) && (type == UNITS || type == RESERV))
1152 {
1153 switch (val)
1154 {
1155 /* Oscillating units */
1156 case DTK_DAY:
1157 case DTK_MONTH:
1158 case DTK_QUARTER:
1159 case DTK_WEEK:
1160 case DTK_DOW:
1161 case DTK_ISODOW:
1162 case DTK_DOY:
1164 break;
1165
1166 /* Monotonically-increasing units */
1167 case DTK_YEAR:
1168 case DTK_DECADE:
1169 case DTK_CENTURY:
1170 case DTK_MILLENNIUM:
1171 case DTK_JULIAN:
1172 case DTK_ISOYEAR:
1173 case DTK_EPOCH:
1174 if (DATE_IS_NOBEGIN(date))
1176 CStringGetDatum("-Infinity"),
1178 Int32GetDatum(-1))));
1179 else
1181 CStringGetDatum("Infinity"),
1183 Int32GetDatum(-1))));
1184 default:
1185 ereport(ERROR,
1186 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1187 errmsg("unit \"%s\" not supported for type %s",
1188 lowunits, format_type_be(DATEOID))));
1189 }
1190 }
1191 else if (type == UNITS)
1192 {
1193 j2date(date + POSTGRES_EPOCH_JDATE, &year, &mon, &mday);
1194
1195 switch (val)
1196 {
1197 case DTK_DAY:
1198 intresult = mday;
1199 break;
1200
1201 case DTK_MONTH:
1202 intresult = mon;
1203 break;
1204
1205 case DTK_QUARTER:
1206 intresult = (mon - 1) / 3 + 1;
1207 break;
1208
1209 case DTK_WEEK:
1210 intresult = date2isoweek(year, mon, mday);
1211 break;
1212
1213 case DTK_YEAR:
1214 if (year > 0)
1215 intresult = year;
1216 else
1217 /* there is no year 0, just 1 BC and 1 AD */
1218 intresult = year - 1;
1219 break;
1220
1221 case DTK_DECADE:
1222 /* see comments in timestamp_part */
1223 if (year >= 0)
1224 intresult = year / 10;
1225 else
1226 intresult = -((8 - (year - 1)) / 10);
1227 break;
1228
1229 case DTK_CENTURY:
1230 /* see comments in timestamp_part */
1231 if (year > 0)
1232 intresult = (year + 99) / 100;
1233 else
1234 intresult = -((99 - (year - 1)) / 100);
1235 break;
1236
1237 case DTK_MILLENNIUM:
1238 /* see comments in timestamp_part */
1239 if (year > 0)
1240 intresult = (year + 999) / 1000;
1241 else
1242 intresult = -((999 - (year - 1)) / 1000);
1243 break;
1244
1245 case DTK_JULIAN:
1246 intresult = date + POSTGRES_EPOCH_JDATE;
1247 break;
1248
1249 case DTK_ISOYEAR:
1250 intresult = date2isoyear(year, mon, mday);
1251 /* Adjust BC years */
1252 if (intresult <= 0)
1253 intresult -= 1;
1254 break;
1255
1256 case DTK_DOW:
1257 case DTK_ISODOW:
1258 intresult = j2day(date + POSTGRES_EPOCH_JDATE);
1259 if (val == DTK_ISODOW && intresult == 0)
1260 intresult = 7;
1261 break;
1262
1263 case DTK_DOY:
1264 intresult = date2j(year, mon, mday) - date2j(year, 1, 1) + 1;
1265 break;
1266
1267 default:
1268 ereport(ERROR,
1269 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1270 errmsg("unit \"%s\" not supported for type %s",
1271 lowunits, format_type_be(DATEOID))));
1272 intresult = 0;
1273 }
1274 }
1275 else if (type == RESERV)
1276 {
1277 switch (val)
1278 {
1279 case DTK_EPOCH:
1281 break;
1282
1283 default:
1284 ereport(ERROR,
1285 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1286 errmsg("unit \"%s\" not supported for type %s",
1287 lowunits, format_type_be(DATEOID))));
1288 intresult = 0;
1289 }
1290 }
1291 else
1292 {
1293 ereport(ERROR,
1294 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1295 errmsg("unit \"%s\" not recognized for type %s",
1296 lowunits, format_type_be(DATEOID))));
1297 intresult = 0;
1298 }
1299
1301}
int DecodeUnits(int field, const char *lowtoken, int *val)
Definition: datetime.c:4169
int j2day(int date)
Definition: datetime.c:354
int DecodeSpecial(int field, const char *lowtoken, int *val)
Definition: datetime.c:3246
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4260
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:626
int date2isoweek(int year, int mon, int mday)
Definition: timestamp.c:5292
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:5347
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:234
#define SECS_PER_DAY
Definition: timestamp.h:126
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:686
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#define UNKNOWN_FIELD
Definition: datetime.h:124
#define DTK_DECADE
Definition: datetime.h:168
#define DTK_QUARTER
Definition: datetime.h:166
#define DTK_JULIAN
Definition: datetime.h:173
#define DTK_CENTURY
Definition: datetime.h:169
#define DTK_ISODOW
Definition: datetime.h:180
#define DTK_DAY
Definition: datetime.h:163
#define RESERV
Definition: datetime.h:90
#define DTK_MILLENNIUM
Definition: datetime.h:170
#define DTK_ISOYEAR
Definition: datetime.h:179
#define DTK_DOY
Definition: datetime.h:176
#define DTK_WEEK
Definition: datetime.h:164
#define DTK_DOW
Definition: datetime.h:175
#define DTK_YEAR
Definition: datetime.h:167
#define DTK_MONTH
Definition: datetime.h:165
#define UNITS
Definition: datetime.h:107
long val
Definition: informix.c:689
static Numeric DatumGetNumeric(Datum X)
Definition: numeric.h:64
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:83
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:360
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:222
#define InvalidOid
Definition: postgres_ext.h:37
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:37
Definition: c.h:693
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition: varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition: varatt.h:486
const char * type

References CStringGetDatum(), date2isoweek(), date2isoyear(), date2j(), DATE_IS_NOBEGIN, DATE_NOT_FINITE, DatumGetNumeric(), DecodeSpecial(), DecodeUnits(), DirectFunctionCall3, downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_DOW, DTK_DOY, DTK_EPOCH, DTK_ISODOW, DTK_ISOYEAR, DTK_JULIAN, DTK_MILLENNIUM, DTK_MONTH, DTK_QUARTER, DTK_WEEK, DTK_YEAR, ereport, errcode(), errmsg(), ERROR, format_type_be(), Int32GetDatum(), int64_to_numeric(), InvalidOid, j2date(), j2day(), numeric_in(), ObjectIdGetDatum(), PG_GETARG_DATEADT, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_NUMERIC, POSTGRES_EPOCH_JDATE, RESERV, SECS_PER_DAY, type, UNITS, UNIX_EPOCH_JDATE, UNKNOWN_FIELD, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ extract_time()

Datum extract_time ( PG_FUNCTION_ARGS  )

Definition at line 2396 of file date.c.

2397{
2398 return time_part_common(fcinfo, true);
2399}
static Datum time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:2287

References time_part_common().

◆ extract_timetz()

Datum extract_timetz ( PG_FUNCTION_ARGS  )

Definition at line 3197 of file date.c.

3198{
3199 return timetz_part_common(fcinfo, true);
3200}
static Datum timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: date.c:3074

References timetz_part_common().

◆ float_time_overflows()

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

Definition at line 1598 of file date.c.

1599{
1600 /* Range-check the fields individually. */
1601 if (hour < 0 || hour > HOURS_PER_DAY ||
1602 min < 0 || min >= MINS_PER_HOUR)
1603 return true;
1604
1605 /*
1606 * "sec", being double, requires extra care. Cope with NaN, and round off
1607 * before applying the range check to avoid unexpected errors due to
1608 * imprecise input. (We assume rint() behaves sanely with infinities.)
1609 */
1610 if (isnan(sec))
1611 return true;
1612 sec = rint(sec * USECS_PER_SEC);
1613 if (sec < 0 || sec > SECS_PER_MINUTE * USECS_PER_SEC)
1614 return true;
1615
1616 /*
1617 * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1618 * that the total time value doesn't exceed 24:00:00. This must match the
1619 * way that callers will convert the fields to a time.
1620 */
1621 if (((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1622 * USECS_PER_SEC) + (int64) sec) > USECS_PER_DAY)
1623 return true;
1624
1625 return false;
1626}
#define MINS_PER_HOUR
Definition: timestamp.h:129
#define SECS_PER_MINUTE
Definition: timestamp.h:128
#define HOURS_PER_DAY
Definition: timestamp.h:118

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

Referenced by make_time(), and make_timestamp_internal().

◆ GetSQLCurrentDate()

DateADT GetSQLCurrentDate ( void  )

Definition at line 317 of file date.c.

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

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

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLCurrentTime()

TimeTzADT * GetSQLCurrentTime ( int32  typmod)

Definition at line 350 of file date.c.

351{
352 TimeTzADT *result;
353 struct pg_tm tt,
354 *tm = &tt;
355 fsec_t fsec;
356 int tz;
357
358 GetCurrentTimeUsec(tm, &fsec, &tz);
359
360 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
361 tm2timetz(tm, fsec, tz, result);
362 AdjustTimeForTypmod(&(result->time), typmod);
363 return result;
364}
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:397
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2410
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1792
void * palloc(Size size)
Definition: mcxt.c:1365

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

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLLocalTime()

TimeADT GetSQLLocalTime ( int32  typmod)

Definition at line 370 of file date.c.

371{
372 TimeADT result;
373 struct pg_tm tt,
374 *tm = &tt;
375 fsec_t fsec;
376 int tz;
377
378 GetCurrentTimeUsec(tm, &fsec, &tz);
379
380 tm2time(tm, fsec, &result);
381 AdjustTimeForTypmod(&result, typmod);
382 return result;
383}
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1563

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

Referenced by ExecEvalSQLValueFunction().

◆ hashdate()

Datum hashdate ( PG_FUNCTION_ARGS  )

Definition at line 512 of file date.c.

513{
515}
static Datum hash_uint32(uint32 k)
Definition: hashfn.h:43

References hash_uint32(), and PG_GETARG_DATEADT.

◆ hashdateextended()

Datum hashdateextended ( PG_FUNCTION_ARGS  )

Definition at line 518 of file date.c.

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

References hash_uint32_extended(), PG_GETARG_DATEADT, and PG_GETARG_INT64.

◆ in_range_date_interval()

Datum in_range_date_interval ( PG_FUNCTION_ARGS  )

Definition at line 1104 of file date.c.

1105{
1107 DateADT base = PG_GETARG_DATEADT(1);
1108 Interval *offset = PG_GETARG_INTERVAL_P(2);
1109 bool sub = PG_GETARG_BOOL(3);
1110 bool less = PG_GETARG_BOOL(4);
1111 Timestamp valStamp;
1112 Timestamp baseStamp;
1113
1114 /* XXX we could support out-of-range cases here, perhaps */
1115 valStamp = date2timestamp(val);
1116 baseStamp = date2timestamp(base);
1117
1119 TimestampGetDatum(valStamp),
1120 TimestampGetDatum(baseStamp),
1121 IntervalPGetDatum(offset),
1122 BoolGetDatum(sub),
1123 BoolGetDatum(less));
1124}
Datum in_range_timestamp_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3873
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:690
static Datum BoolGetDatum(bool X)
Definition: postgres.h:112
static Datum IntervalPGetDatum(const Interval *X)
Definition: timestamp.h:58

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

◆ in_range_time_interval()

Datum in_range_time_interval ( PG_FUNCTION_ARGS  )

Definition at line 2245 of file date.c.

2246{
2248 TimeADT base = PG_GETARG_TIMEADT(1);
2249 Interval *offset = PG_GETARG_INTERVAL_P(2);
2250 bool sub = PG_GETARG_BOOL(3);
2251 bool less = PG_GETARG_BOOL(4);
2252 TimeADT sum;
2253
2254 /*
2255 * Like time_pl_interval/time_mi_interval, we disregard the month and day
2256 * fields of the offset. So our test for negative should too. This also
2257 * catches -infinity, so we only need worry about +infinity below.
2258 */
2259 if (offset->time < 0)
2260 ereport(ERROR,
2261 (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2262 errmsg("invalid preceding or following size in window function")));
2263
2264 /*
2265 * We can't use time_pl_interval/time_mi_interval here, because their
2266 * wraparound behavior would give wrong (or at least undesirable) answers.
2267 * Fortunately the equivalent non-wrapping behavior is trivial, except
2268 * that adding an infinite (or very large) interval might cause integer
2269 * overflow. Subtraction cannot overflow here.
2270 */
2271 if (sub)
2272 sum = base - offset->time;
2273 else if (pg_add_s64_overflow(base, offset->time, &sum))
2274 PG_RETURN_BOOL(less);
2275
2276 if (less)
2277 PG_RETURN_BOOL(val <= sum);
2278 else
2279 PG_RETURN_BOOL(val >= sum);
2280}
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:235
TimeOffset time
Definition: timestamp.h:49

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

◆ in_range_timetz_interval()

Datum in_range_timetz_interval ( PG_FUNCTION_ARGS  )

Definition at line 2797 of file date.c.

2798{
2801 Interval *offset = PG_GETARG_INTERVAL_P(2);
2802 bool sub = PG_GETARG_BOOL(3);
2803 bool less = PG_GETARG_BOOL(4);
2804 TimeTzADT sum;
2805
2806 /*
2807 * Like timetz_pl_interval/timetz_mi_interval, we disregard the month and
2808 * day fields of the offset. So our test for negative should too. This
2809 * also catches -infinity, so we only need worry about +infinity below.
2810 */
2811 if (offset->time < 0)
2812 ereport(ERROR,
2813 (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2814 errmsg("invalid preceding or following size in window function")));
2815
2816 /*
2817 * We can't use timetz_pl_interval/timetz_mi_interval here, because their
2818 * wraparound behavior would give wrong (or at least undesirable) answers.
2819 * Fortunately the equivalent non-wrapping behavior is trivial, except
2820 * that adding an infinite (or very large) interval might cause integer
2821 * overflow. Subtraction cannot overflow here.
2822 */
2823 if (sub)
2824 sum.time = base->time - offset->time;
2825 else if (pg_add_s64_overflow(base->time, offset->time, &sum.time))
2826 PG_RETURN_BOOL(less);
2827 sum.zone = base->zone;
2828
2829 if (less)
2831 else
2833}
static int timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
Definition: date.c:2590

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

◆ interval_time()

Datum interval_time ( PG_FUNCTION_ARGS  )

Definition at line 2159 of file date.c.

2160{
2161 Interval *span = PG_GETARG_INTERVAL_P(0);
2162 TimeADT result;
2163
2164 if (INTERVAL_NOT_FINITE(span))
2165 ereport(ERROR,
2166 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2167 errmsg("cannot convert infinite interval to time")));
2168
2169 result = span->time % USECS_PER_DAY;
2170 if (result < 0)
2171 result += USECS_PER_DAY;
2172
2173 PG_RETURN_TIMEADT(result);
2174}
#define INTERVAL_NOT_FINITE(i)
Definition: timestamp.h:195
#define PG_RETURN_TIMEADT(x)
Definition: date.h:94

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

◆ make_date()

Datum make_date ( PG_FUNCTION_ARGS  )

Definition at line 246 of file date.c.

247{
248 struct pg_tm tm;
250 int dterr;
251 bool bc = false;
252
256
257 /* Handle negative years as BC */
258 if (tm.tm_year < 0)
259 {
260 int year = tm.tm_year;
261
262 bc = true;
263 if (pg_neg_s32_overflow(year, &year))
265 (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
266 errmsg("date field value out of range: %d-%02d-%02d",
268 tm.tm_year = year;
269 }
270
271 dterr = ValidateDate(DTK_DATE_M, false, false, bc, &tm);
272
273 if (dterr != 0)
275 (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
276 errmsg("date field value out of range: %d-%02d-%02d",
278
279 /* Prevent overflow in Julian-day routines */
282 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
283 errmsg("date out of range: %d-%02d-%02d",
285
287
288 /* Now check for just-out-of-range dates */
289 if (!IS_VALID_DATE(date))
291 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
292 errmsg("date out of range: %d-%02d-%02d",
294
296}
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2561
#define DTK_DATE_M
Definition: datetime.h:191
static bool pg_neg_s32_overflow(int32 a, int32 *result)
Definition: int.h:205

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

◆ make_time()

Datum make_time ( PG_FUNCTION_ARGS  )

Definition at line 1724 of file date.c.

1725{
1726 int tm_hour = PG_GETARG_INT32(0);
1727 int tm_min = PG_GETARG_INT32(1);
1728 double sec = PG_GETARG_FLOAT8(2);
1729 TimeADT time;
1730
1731 /* Check for time overflow */
1732 if (float_time_overflows(tm_hour, tm_min, sec))
1733 ereport(ERROR,
1734 (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
1735 errmsg("time field value out of range: %d:%02d:%02g",
1736 tm_hour, tm_min, sec)));
1737
1738 /* This should match tm2time */
1739 time = (((tm_hour * MINS_PER_HOUR + tm_min) * SECS_PER_MINUTE)
1740 * USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
1741
1742 PG_RETURN_TIMEADT(time);
1743}
bool float_time_overflows(int hour, int min, double sec)
Definition: date.c:1598
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282

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

◆ overlaps_time()

Datum overlaps_time ( PG_FUNCTION_ARGS  )

Definition at line 1930 of file date.c.

1931{
1932 /*
1933 * The arguments are TimeADT, but we leave them as generic Datums to avoid
1934 * dereferencing nulls (TimeADT is pass-by-reference!)
1935 */
1936 Datum ts1 = PG_GETARG_DATUM(0);
1937 Datum te1 = PG_GETARG_DATUM(1);
1938 Datum ts2 = PG_GETARG_DATUM(2);
1939 Datum te2 = PG_GETARG_DATUM(3);
1940 bool ts1IsNull = PG_ARGISNULL(0);
1941 bool te1IsNull = PG_ARGISNULL(1);
1942 bool ts2IsNull = PG_ARGISNULL(2);
1943 bool te2IsNull = PG_ARGISNULL(3);
1944
1945#define TIMEADT_GT(t1,t2) \
1946 (DatumGetTimeADT(t1) > DatumGetTimeADT(t2))
1947#define TIMEADT_LT(t1,t2) \
1948 (DatumGetTimeADT(t1) < DatumGetTimeADT(t2))
1949
1950 /*
1951 * If both endpoints of interval 1 are null, the result is null (unknown).
1952 * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
1953 * take ts1 as the lesser endpoint.
1954 */
1955 if (ts1IsNull)
1956 {
1957 if (te1IsNull)
1959 /* swap null for non-null */
1960 ts1 = te1;
1961 te1IsNull = true;
1962 }
1963 else if (!te1IsNull)
1964 {
1965 if (TIMEADT_GT(ts1, te1))
1966 {
1967 Datum tt = ts1;
1968
1969 ts1 = te1;
1970 te1 = tt;
1971 }
1972 }
1973
1974 /* Likewise for interval 2. */
1975 if (ts2IsNull)
1976 {
1977 if (te2IsNull)
1979 /* swap null for non-null */
1980 ts2 = te2;
1981 te2IsNull = true;
1982 }
1983 else if (!te2IsNull)
1984 {
1985 if (TIMEADT_GT(ts2, te2))
1986 {
1987 Datum tt = ts2;
1988
1989 ts2 = te2;
1990 te2 = tt;
1991 }
1992 }
1993
1994 /*
1995 * At this point neither ts1 nor ts2 is null, so we can consider three
1996 * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
1997 */
1998 if (TIMEADT_GT(ts1, ts2))
1999 {
2000 /*
2001 * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2002 * in the presence of nulls it's not quite completely so.
2003 */
2004 if (te2IsNull)
2006 if (TIMEADT_LT(ts1, te2))
2007 PG_RETURN_BOOL(true);
2008 if (te1IsNull)
2010
2011 /*
2012 * If te1 is not null then we had ts1 <= te1 above, and we just found
2013 * ts1 >= te2, hence te1 >= te2.
2014 */
2015 PG_RETURN_BOOL(false);
2016 }
2017 else if (TIMEADT_LT(ts1, ts2))
2018 {
2019 /* This case is ts2 < te1 OR te2 < te1 */
2020 if (te1IsNull)
2022 if (TIMEADT_LT(ts2, te1))
2023 PG_RETURN_BOOL(true);
2024 if (te2IsNull)
2026
2027 /*
2028 * If te2 is not null then we had ts2 <= te2 above, and we just found
2029 * ts2 >= te1, hence te2 >= te1.
2030 */
2031 PG_RETURN_BOOL(false);
2032 }
2033 else
2034 {
2035 /*
2036 * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2037 * rather silly way of saying "true if both are nonnull, else null".
2038 */
2039 if (te1IsNull || te2IsNull)
2041 PG_RETURN_BOOL(true);
2042 }
2043
2044#undef TIMEADT_GT
2045#undef TIMEADT_LT
2046}
#define TIMEADT_GT(t1, t2)
#define TIMEADT_LT(t1, t2)
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268

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

◆ overlaps_timetz()

Datum overlaps_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2842 of file date.c.

2843{
2844 /*
2845 * The arguments are TimeTzADT *, but we leave them as generic Datums for
2846 * convenience of notation --- and to avoid dereferencing nulls.
2847 */
2848 Datum ts1 = PG_GETARG_DATUM(0);
2849 Datum te1 = PG_GETARG_DATUM(1);
2850 Datum ts2 = PG_GETARG_DATUM(2);
2851 Datum te2 = PG_GETARG_DATUM(3);
2852 bool ts1IsNull = PG_ARGISNULL(0);
2853 bool te1IsNull = PG_ARGISNULL(1);
2854 bool ts2IsNull = PG_ARGISNULL(2);
2855 bool te2IsNull = PG_ARGISNULL(3);
2856
2857#define TIMETZ_GT(t1,t2) \
2858 DatumGetBool(DirectFunctionCall2(timetz_gt,t1,t2))
2859#define TIMETZ_LT(t1,t2) \
2860 DatumGetBool(DirectFunctionCall2(timetz_lt,t1,t2))
2861
2862 /*
2863 * If both endpoints of interval 1 are null, the result is null (unknown).
2864 * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2865 * take ts1 as the lesser endpoint.
2866 */
2867 if (ts1IsNull)
2868 {
2869 if (te1IsNull)
2871 /* swap null for non-null */
2872 ts1 = te1;
2873 te1IsNull = true;
2874 }
2875 else if (!te1IsNull)
2876 {
2877 if (TIMETZ_GT(ts1, te1))
2878 {
2879 Datum tt = ts1;
2880
2881 ts1 = te1;
2882 te1 = tt;
2883 }
2884 }
2885
2886 /* Likewise for interval 2. */
2887 if (ts2IsNull)
2888 {
2889 if (te2IsNull)
2891 /* swap null for non-null */
2892 ts2 = te2;
2893 te2IsNull = true;
2894 }
2895 else if (!te2IsNull)
2896 {
2897 if (TIMETZ_GT(ts2, te2))
2898 {
2899 Datum tt = ts2;
2900
2901 ts2 = te2;
2902 te2 = tt;
2903 }
2904 }
2905
2906 /*
2907 * At this point neither ts1 nor ts2 is null, so we can consider three
2908 * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2909 */
2910 if (TIMETZ_GT(ts1, ts2))
2911 {
2912 /*
2913 * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2914 * in the presence of nulls it's not quite completely so.
2915 */
2916 if (te2IsNull)
2918 if (TIMETZ_LT(ts1, te2))
2919 PG_RETURN_BOOL(true);
2920 if (te1IsNull)
2922
2923 /*
2924 * If te1 is not null then we had ts1 <= te1 above, and we just found
2925 * ts1 >= te2, hence te1 >= te2.
2926 */
2927 PG_RETURN_BOOL(false);
2928 }
2929 else if (TIMETZ_LT(ts1, ts2))
2930 {
2931 /* This case is ts2 < te1 OR te2 < te1 */
2932 if (te1IsNull)
2934 if (TIMETZ_LT(ts2, te1))
2935 PG_RETURN_BOOL(true);
2936 if (te2IsNull)
2938
2939 /*
2940 * If te2 is not null then we had ts2 <= te2 above, and we just found
2941 * ts2 >= te1, hence te2 >= te1.
2942 */
2943 PG_RETURN_BOOL(false);
2944 }
2945 else
2946 {
2947 /*
2948 * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2949 * rather silly way of saying "true if both are nonnull, else null".
2950 */
2951 if (te1IsNull || te2IsNull)
2953 PG_RETURN_BOOL(true);
2954 }
2955
2956#undef TIMETZ_GT
2957#undef TIMETZ_LT
2958}
#define TIMETZ_GT(t1, t2)
#define TIMETZ_LT(t1, t2)

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

◆ time2tm()

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

Definition at line 1635 of file date.c.

1636{
1637 tm->tm_hour = time / USECS_PER_HOUR;
1638 time -= tm->tm_hour * USECS_PER_HOUR;
1639 tm->tm_min = time / USECS_PER_MINUTE;
1640 time -= tm->tm_min * USECS_PER_MINUTE;
1641 tm->tm_sec = time / USECS_PER_SEC;
1642 time -= tm->tm_sec * USECS_PER_SEC;
1643 *fsec = time;
1644 return 0;
1645}
#define USECS_PER_HOUR
Definition: timestamp.h:132
#define USECS_PER_MINUTE
Definition: timestamp.h:133

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

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

◆ time_cmp()

Datum time_cmp ( PG_FUNCTION_ARGS  )

Definition at line 1881 of file date.c.

1882{
1883 TimeADT time1 = PG_GETARG_TIMEADT(0);
1884 TimeADT time2 = PG_GETARG_TIMEADT(1);
1885
1886 if (time1 < time2)
1887 PG_RETURN_INT32(-1);
1888 if (time1 > time2)
1889 PG_RETURN_INT32(1);
1890 PG_RETURN_INT32(0);
1891}

References PG_GETARG_TIMEADT, and PG_RETURN_INT32.

Referenced by compareDatetime(), gbt_timekey_cmp(), and gbt_timekey_ssup_cmp().

◆ time_eq()

Datum time_eq ( PG_FUNCTION_ARGS  )

Definition at line 1827 of file date.c.

1828{
1829 TimeADT time1 = PG_GETARG_TIMEADT(0);
1830 TimeADT time2 = PG_GETARG_TIMEADT(1);
1831
1832 PG_RETURN_BOOL(time1 == time2);
1833}

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timeeq().

◆ time_ge()

Datum time_ge ( PG_FUNCTION_ARGS  )

Definition at line 1872 of file date.c.

1873{
1874 TimeADT time1 = PG_GETARG_TIMEADT(0);
1875 TimeADT time2 = PG_GETARG_TIMEADT(1);
1876
1877 PG_RETURN_BOOL(time1 >= time2);
1878}

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timege().

◆ time_gt()

Datum time_gt ( PG_FUNCTION_ARGS  )

Definition at line 1863 of file date.c.

1864{
1865 TimeADT time1 = PG_GETARG_TIMEADT(0);
1866 TimeADT time2 = PG_GETARG_TIMEADT(1);
1867
1868 PG_RETURN_BOOL(time1 > time2);
1869}

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timegt().

◆ time_hash()

Datum time_hash ( PG_FUNCTION_ARGS  )

Definition at line 1894 of file date.c.

1895{
1896 return hashint8(fcinfo);
1897}
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:83

References hashint8().

◆ time_hash_extended()

Datum time_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 1900 of file date.c.

1901{
1902 return hashint8extended(fcinfo);
1903}
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103

References hashint8extended().

◆ time_in()

Datum time_in ( PG_FUNCTION_ARGS  )

Definition at line 1521 of file date.c.

1522{
1523 char *str = PG_GETARG_CSTRING(0);
1524#ifdef NOT_USED
1525 Oid typelem = PG_GETARG_OID(1);
1526#endif
1527 int32 typmod = PG_GETARG_INT32(2);
1528 Node *escontext = fcinfo->context;
1529 TimeADT result;
1530 fsec_t fsec;
1531 struct pg_tm tt,
1532 *tm = &tt;
1533 int tz;
1534 int nf;
1535 int dterr;
1536 char workbuf[MAXDATELEN + 1];
1537 char *field[MAXDATEFIELDS];
1538 int dtype;
1539 int ftype[MAXDATEFIELDS];
1540 DateTimeErrorExtra extra;
1541
1542 dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
1543 field, ftype, MAXDATEFIELDS, &nf);
1544 if (dterr == 0)
1545 dterr = DecodeTimeOnly(field, ftype, nf,
1546 &dtype, tm, &fsec, &tz, &extra);
1547 if (dterr != 0)
1548 {
1549 DateTimeParseError(dterr, &extra, str, "time", escontext);
1551 }
1552
1553 tm2time(tm, fsec, &result);
1554 AdjustTimeForTypmod(&result, typmod);
1555
1556 PG_RETURN_TIMEADT(result);
1557}
int DecodeTimeOnly(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp, DateTimeErrorExtra *extra)
Definition: datetime.c:1917
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
unsigned int Oid
Definition: postgres_ext.h:32

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

◆ time_interval()

Datum time_interval ( PG_FUNCTION_ARGS  )

Definition at line 2136 of file date.c.

2137{
2138 TimeADT time = PG_GETARG_TIMEADT(0);
2139 Interval *result;
2140
2141 result = (Interval *) palloc(sizeof(Interval));
2142
2143 result->time = time;
2144 result->day = 0;
2145 result->month = 0;
2146
2147 PG_RETURN_INTERVAL_P(result);
2148}
int32 day
Definition: timestamp.h:51
int32 month
Definition: timestamp.h:52
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:69

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

◆ time_larger()

Datum time_larger ( PG_FUNCTION_ARGS  )

Definition at line 1906 of file date.c.

1907{
1908 TimeADT time1 = PG_GETARG_TIMEADT(0);
1909 TimeADT time2 = PG_GETARG_TIMEADT(1);
1910
1911 PG_RETURN_TIMEADT((time1 > time2) ? time1 : time2);
1912}

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

◆ time_le()

Datum time_le ( PG_FUNCTION_ARGS  )

Definition at line 1854 of file date.c.

1855{
1856 TimeADT time1 = PG_GETARG_TIMEADT(0);
1857 TimeADT time2 = PG_GETARG_TIMEADT(1);
1858
1859 PG_RETURN_BOOL(time1 <= time2);
1860}

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timele().

◆ time_lt()

Datum time_lt ( PG_FUNCTION_ARGS  )

Definition at line 1845 of file date.c.

1846{
1847 TimeADT time1 = PG_GETARG_TIMEADT(0);
1848 TimeADT time2 = PG_GETARG_TIMEADT(1);
1849
1850 PG_RETURN_BOOL(time1 < time2);
1851}

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

Referenced by gbt_timelt().

◆ time_mi_interval()

Datum time_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 2222 of file date.c.

2223{
2224 TimeADT time = PG_GETARG_TIMEADT(0);
2225 Interval *span = PG_GETARG_INTERVAL_P(1);
2226 TimeADT result;
2227
2228 if (INTERVAL_NOT_FINITE(span))
2229 ereport(ERROR,
2230 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2231 errmsg("cannot subtract infinite interval from time")));
2232
2233 result = time - span->time;
2234 result -= result / USECS_PER_DAY * USECS_PER_DAY;
2235 if (result < INT64CONST(0))
2236 result += USECS_PER_DAY;
2237
2238 PG_RETURN_TIMEADT(result);
2239}

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

◆ time_mi_time()

Datum time_mi_time ( PG_FUNCTION_ARGS  )

Definition at line 2180 of file date.c.

2181{
2182 TimeADT time1 = PG_GETARG_TIMEADT(0);
2183 TimeADT time2 = PG_GETARG_TIMEADT(1);
2184 Interval *result;
2185
2186 result = (Interval *) palloc(sizeof(Interval));
2187
2188 result->month = 0;
2189 result->day = 0;
2190 result->time = time1 - time2;
2191
2192 PG_RETURN_INTERVAL_P(result);
2193}

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

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

◆ time_ne()

Datum time_ne ( PG_FUNCTION_ARGS  )

Definition at line 1836 of file date.c.

1837{
1838 TimeADT time1 = PG_GETARG_TIMEADT(0);
1839 TimeADT time2 = PG_GETARG_TIMEADT(1);
1840
1841 PG_RETURN_BOOL(time1 != time2);
1842}

References PG_GETARG_TIMEADT, and PG_RETURN_BOOL.

◆ time_out()

Datum time_out ( PG_FUNCTION_ARGS  )

Definition at line 1648 of file date.c.

1649{
1650 TimeADT time = PG_GETARG_TIMEADT(0);
1651 char *result;
1652 struct pg_tm tt,
1653 *tm = &tt;
1654 fsec_t fsec;
1655 char buf[MAXDATELEN + 1];
1656
1657 time2tm(time, tm, &fsec);
1658 EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf);
1659
1660 result = pstrdup(buf);
1661 PG_RETURN_CSTRING(result);
1662}
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:4434
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1635

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

Referenced by ExecGetJsonValueItemString().

◆ time_overflows()

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

Definition at line 1574 of file date.c.

1575{
1576 /* Range-check the fields individually. */
1577 if (hour < 0 || hour > HOURS_PER_DAY ||
1578 min < 0 || min >= MINS_PER_HOUR ||
1579 sec < 0 || sec > SECS_PER_MINUTE ||
1580 fsec < 0 || fsec > USECS_PER_SEC)
1581 return true;
1582
1583 /*
1584 * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1585 * that the total time value doesn't exceed 24:00:00.
1586 */
1587 if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1588 + sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1589 return true;
1590
1591 return false;
1592}

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

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ time_part()

Datum time_part ( PG_FUNCTION_ARGS  )

Definition at line 2390 of file date.c.

2391{
2392 return time_part_common(fcinfo, false);
2393}

References time_part_common().

◆ time_part_common()

static Datum time_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 2287 of file date.c.

2288{
2289 text *units = PG_GETARG_TEXT_PP(0);
2290 TimeADT time = PG_GETARG_TIMEADT(1);
2291 int64 intresult;
2292 int type,
2293 val;
2294 char *lowunits;
2295
2296 lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
2297 VARSIZE_ANY_EXHDR(units),
2298 false);
2299
2300 type = DecodeUnits(0, lowunits, &val);
2301 if (type == UNKNOWN_FIELD)
2302 type = DecodeSpecial(0, lowunits, &val);
2303
2304 if (type == UNITS)
2305 {
2306 fsec_t fsec;
2307 struct pg_tm tt,
2308 *tm = &tt;
2309
2310 time2tm(time, tm, &fsec);
2311
2312 switch (val)
2313 {
2314 case DTK_MICROSEC:
2315 intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
2316 break;
2317
2318 case DTK_MILLISEC:
2319 if (retnumeric)
2320 /*---
2321 * tm->tm_sec * 1000 + fsec / 1000
2322 * = (tm->tm_sec * 1'000'000 + fsec) / 1000
2323 */
2325 else
2326 PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
2327 break;
2328
2329 case DTK_SECOND:
2330 if (retnumeric)
2331 /*---
2332 * tm->tm_sec + fsec / 1'000'000
2333 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
2334 */
2336 else
2337 PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
2338 break;
2339
2340 case DTK_MINUTE:
2341 intresult = tm->tm_min;
2342 break;
2343
2344 case DTK_HOUR:
2345 intresult = tm->tm_hour;
2346 break;
2347
2348 case DTK_TZ:
2349 case DTK_TZ_MINUTE:
2350 case DTK_TZ_HOUR:
2351 case DTK_DAY:
2352 case DTK_MONTH:
2353 case DTK_QUARTER:
2354 case DTK_YEAR:
2355 case DTK_DECADE:
2356 case DTK_CENTURY:
2357 case DTK_MILLENNIUM:
2358 case DTK_ISOYEAR:
2359 default:
2360 ereport(ERROR,
2361 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2362 errmsg("unit \"%s\" not supported for type %s",
2363 lowunits, format_type_be(TIMEOID))));
2364 intresult = 0;
2365 }
2366 }
2367 else if (type == RESERV && val == DTK_EPOCH)
2368 {
2369 if (retnumeric)
2371 else
2372 PG_RETURN_FLOAT8(time / 1000000.0);
2373 }
2374 else
2375 {
2376 ereport(ERROR,
2377 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2378 errmsg("unit \"%s\" not recognized for type %s",
2379 lowunits, format_type_be(TIMEOID))));
2380 intresult = 0;
2381 }
2382
2383 if (retnumeric)
2385 else
2386 PG_RETURN_FLOAT8(intresult);
2387}
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4281
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define DTK_SECOND
Definition: datetime.h:160
#define DTK_TZ_HOUR
Definition: datetime.h:177
#define DTK_TZ_MINUTE
Definition: datetime.h:178
#define DTK_TZ
Definition: datetime.h:146
#define DTK_HOUR
Definition: datetime.h:162
#define DTK_MICROSEC
Definition: datetime.h:172
#define DTK_MILLISEC
Definition: datetime.h:171
#define DTK_MINUTE
Definition: datetime.h:161

References DecodeSpecial(), DecodeUnits(), downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_EPOCH, DTK_HOUR, DTK_ISOYEAR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_TZ, DTK_TZ_HOUR, DTK_TZ_MINUTE, DTK_YEAR, ereport, errcode(), errmsg(), ERROR, format_type_be(), int64_div_fast_to_numeric(), int64_to_numeric(), INT64CONST, PG_GETARG_TEXT_PP, PG_GETARG_TIMEADT, PG_RETURN_FLOAT8, PG_RETURN_NUMERIC, RESERV, time2tm(), tm, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, type, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by extract_time(), and time_part().

◆ time_pl_interval()

Datum time_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 2199 of file date.c.

2200{
2201 TimeADT time = PG_GETARG_TIMEADT(0);
2202 Interval *span = PG_GETARG_INTERVAL_P(1);
2203 TimeADT result;
2204
2205 if (INTERVAL_NOT_FINITE(span))
2206 ereport(ERROR,
2207 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2208 errmsg("cannot add infinite interval to time")));
2209
2210 result = time + span->time;
2211 result -= result / USECS_PER_DAY * USECS_PER_DAY;
2212 if (result < INT64CONST(0))
2213 result += USECS_PER_DAY;
2214
2215 PG_RETURN_TIMEADT(result);
2216}

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

◆ time_recv()

Datum time_recv ( PG_FUNCTION_ARGS  )

Definition at line 1668 of file date.c.

1669{
1671
1672#ifdef NOT_USED
1673 Oid typelem = PG_GETARG_OID(1);
1674#endif
1675 int32 typmod = PG_GETARG_INT32(2);
1676 TimeADT result;
1677
1678 result = pq_getmsgint64(buf);
1679
1680 if (result < INT64CONST(0) || result > USECS_PER_DAY)
1681 ereport(ERROR,
1682 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1683 errmsg("time out of range")));
1684
1685 AdjustTimeForTypmod(&result, typmod);
1686
1687 PG_RETURN_TIMEADT(result);
1688}
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:453

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

◆ time_scale()

Datum time_scale ( PG_FUNCTION_ARGS  )

Definition at line 1772 of file date.c.

1773{
1774 TimeADT time = PG_GETARG_TIMEADT(0);
1775 int32 typmod = PG_GETARG_INT32(1);
1776 TimeADT result;
1777
1778 result = time;
1779 AdjustTimeForTypmod(&result, typmod);
1780
1781 PG_RETURN_TIMEADT(result);
1782}

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

◆ time_send()

Datum time_send ( PG_FUNCTION_ARGS  )

Definition at line 1694 of file date.c.

1695{
1696 TimeADT time = PG_GETARG_TIMEADT(0);
1698
1700 pq_sendint64(&buf, time);
1702}
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:152

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

◆ time_smaller()

Datum time_smaller ( PG_FUNCTION_ARGS  )

Definition at line 1915 of file date.c.

1916{
1917 TimeADT time1 = PG_GETARG_TIMEADT(0);
1918 TimeADT time2 = PG_GETARG_TIMEADT(1);
1919
1920 PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
1921}

References PG_GETARG_TIMEADT, and PG_RETURN_TIMEADT.

◆ time_support()

Datum time_support ( PG_FUNCTION_ARGS  )

Definition at line 1752 of file date.c.

1753{
1754 Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1755 Node *ret = NULL;
1756
1757 if (IsA(rawreq, SupportRequestSimplify))
1758 {
1760
1762 }
1763
1764 PG_RETURN_POINTER(ret);
1765}
Node * TemporalSimplify(int32 max_precis, Node *node)
Definition: datetime.c:4962
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define IsA(nodeptr, _type_)
Definition: nodes.h:164

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

◆ time_timetz()

Datum time_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2975 of file date.c.

2976{
2977 TimeADT time = PG_GETARG_TIMEADT(0);
2978 TimeTzADT *result;
2979 struct pg_tm tt,
2980 *tm = &tt;
2981 fsec_t fsec;
2982 int tz;
2983
2985 time2tm(time, tm, &fsec);
2987
2988 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2989
2990 result->time = time;
2991 result->zone = tz;
2992
2993 PG_RETURN_TIMETZADT_P(result);
2994}
#define PG_RETURN_TIMETZADT_P(x)
Definition: date.h:95

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

Referenced by castTimeToTimeTz(), and executeDateTimeMethod().

◆ timestamp2date_opt_overflow()

DateADT timestamp2date_opt_overflow ( Timestamp  timestamp,
int *  overflow 
)

Definition at line 1385 of file date.c.

1386{
1387 DateADT result;
1388 struct pg_tm tt,
1389 *tm = &tt;
1390 fsec_t fsec;
1391
1392 if (overflow)
1393 *overflow = 0;
1394
1396 DATE_NOBEGIN(result);
1397 else if (TIMESTAMP_IS_NOEND(timestamp))
1398 DATE_NOEND(result);
1399 else
1400 {
1401 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1402 {
1403 if (overflow)
1404 {
1405 if (timestamp < 0)
1406 {
1407 *overflow = -1;
1408 DATE_NOBEGIN(result);
1409 }
1410 else
1411 {
1412 *overflow = 1; /* not actually reachable */
1413 DATE_NOEND(result);
1414 }
1415 return result;
1416 }
1417 ereport(ERROR,
1418 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1419 errmsg("timestamp out of range")));
1420 }
1421
1423 }
1424
1425 return result;
1426}
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1910
int64 timestamp

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

Referenced by cvt_timestamp_date(), and timestamp_date().

◆ timestamp_cmp_date()

Datum timestamp_cmp_date ( PG_FUNCTION_ARGS  )

Definition at line 1026 of file date.c.

1027{
1029 DateADT dateVal = PG_GETARG_DATEADT(1);
1030
1032}

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

◆ timestamp_date()

Datum timestamp_date ( PG_FUNCTION_ARGS  )

Definition at line 1362 of file date.c.

1363{
1365 DateADT result;
1366
1367 result = timestamp2date_opt_overflow(timestamp, NULL);
1368 PG_RETURN_DATEADT(result);
1369}
DateADT timestamp2date_opt_overflow(Timestamp timestamp, int *overflow)
Definition: date.c:1385

References PG_GETARG_TIMESTAMP, PG_RETURN_DATEADT, and timestamp2date_opt_overflow().

Referenced by executeDateTimeMethod().

◆ timestamp_eq_date()

Datum timestamp_eq_date ( PG_FUNCTION_ARGS  )

Definition at line 972 of file date.c.

973{
975 DateADT dateVal = PG_GETARG_DATEADT(1);
976
978}

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

◆ timestamp_ge_date()

Datum timestamp_ge_date ( PG_FUNCTION_ARGS  )

Definition at line 1017 of file date.c.

1018{
1020 DateADT dateVal = PG_GETARG_DATEADT(1);
1021
1022 PG_RETURN_BOOL(date_cmp_timestamp_internal(dateVal, dt1) <= 0);
1023}

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

◆ timestamp_gt_date()

Datum timestamp_gt_date ( PG_FUNCTION_ARGS  )

Definition at line 999 of file date.c.

1000{
1002 DateADT dateVal = PG_GETARG_DATEADT(1);
1003
1005}

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

◆ timestamp_le_date()

Datum timestamp_le_date ( PG_FUNCTION_ARGS  )

Definition at line 1008 of file date.c.

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

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

◆ timestamp_lt_date()

Datum timestamp_lt_date ( PG_FUNCTION_ARGS  )

Definition at line 990 of file date.c.

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

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

◆ timestamp_ne_date()

Datum timestamp_ne_date ( PG_FUNCTION_ARGS  )

Definition at line 981 of file date.c.

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

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

◆ timestamp_time()

Datum timestamp_time ( PG_FUNCTION_ARGS  )

Definition at line 2052 of file date.c.

2053{
2055 TimeADT result;
2056 struct pg_tm tt,
2057 *tm = &tt;
2058 fsec_t fsec;
2059
2062
2063 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
2064 ereport(ERROR,
2065 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2066 errmsg("timestamp out of range")));
2067
2068 /*
2069 * Could also do this with time = (timestamp / USECS_PER_DAY *
2070 * USECS_PER_DAY) - timestamp;
2071 */
2072 result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2073 USECS_PER_SEC) + fsec;
2074
2075 PG_RETURN_TIMEADT(result);
2076}

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

Referenced by executeDateTimeMethod().

◆ timestamptz2date_opt_overflow()

DateADT timestamptz2date_opt_overflow ( TimestampTz  timestamp,
int *  overflow 
)

Definition at line 1471 of file date.c.

1472{
1473 DateADT result;
1474 struct pg_tm tt,
1475 *tm = &tt;
1476 fsec_t fsec;
1477 int tz;
1478
1479 if (overflow)
1480 *overflow = 0;
1481
1483 DATE_NOBEGIN(result);
1484 else if (TIMESTAMP_IS_NOEND(timestamp))
1485 DATE_NOEND(result);
1486 else
1487 {
1488 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1489 {
1490 if (overflow)
1491 {
1492 if (timestamp < 0)
1493 {
1494 *overflow = -1;
1495 DATE_NOBEGIN(result);
1496 }
1497 else
1498 {
1499 *overflow = 1; /* not actually reachable */
1500 DATE_NOEND(result);
1501 }
1502 return result;
1503 }
1504 ereport(ERROR,
1505 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1506 errmsg("timestamp out of range")));
1507 }
1508
1510 }
1511
1512 return result;
1513}

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

Referenced by cvt_timestamptz_date(), and timestamptz_date().

◆ timestamptz_cmp_date()

Datum timestamptz_cmp_date ( PG_FUNCTION_ARGS  )

Definition at line 1089 of file date.c.

1090{
1092 DateADT dateVal = PG_GETARG_DATEADT(1);
1093
1095}

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

◆ timestamptz_date()

Datum timestamptz_date ( PG_FUNCTION_ARGS  )

Definition at line 1448 of file date.c.

1449{
1451 DateADT result;
1452
1454 PG_RETURN_DATEADT(result);
1455}
DateADT timestamptz2date_opt_overflow(TimestampTz timestamp, int *overflow)
Definition: date.c:1471

References PG_GETARG_TIMESTAMP, PG_RETURN_DATEADT, and timestamptz2date_opt_overflow().

Referenced by executeDateTimeMethod().

◆ timestamptz_eq_date()

Datum timestamptz_eq_date ( PG_FUNCTION_ARGS  )

Definition at line 1035 of file date.c.

1036{
1038 DateADT dateVal = PG_GETARG_DATEADT(1);
1039
1041}

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

◆ timestamptz_ge_date()

Datum timestamptz_ge_date ( PG_FUNCTION_ARGS  )

Definition at line 1080 of file date.c.

1081{
1083 DateADT dateVal = PG_GETARG_DATEADT(1);
1084
1086}

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

◆ timestamptz_gt_date()

Datum timestamptz_gt_date ( PG_FUNCTION_ARGS  )

Definition at line 1062 of file date.c.

1063{
1065 DateADT dateVal = PG_GETARG_DATEADT(1);
1066
1068}

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

◆ timestamptz_le_date()

Datum timestamptz_le_date ( PG_FUNCTION_ARGS  )

Definition at line 1071 of file date.c.

1072{
1074 DateADT dateVal = PG_GETARG_DATEADT(1);
1075
1077}

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

◆ timestamptz_lt_date()

Datum timestamptz_lt_date ( PG_FUNCTION_ARGS  )

Definition at line 1053 of file date.c.

1054{
1056 DateADT dateVal = PG_GETARG_DATEADT(1);
1057
1059}

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

◆ timestamptz_ne_date()

Datum timestamptz_ne_date ( PG_FUNCTION_ARGS  )

Definition at line 1044 of file date.c.

1045{
1047 DateADT dateVal = PG_GETARG_DATEADT(1);
1048
1050}

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

◆ timestamptz_time()

Datum timestamptz_time ( PG_FUNCTION_ARGS  )

Definition at line 2082 of file date.c.

2083{
2085 TimeADT result;
2086 struct pg_tm tt,
2087 *tm = &tt;
2088 int tz;
2089 fsec_t fsec;
2090
2093
2094 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
2095 ereport(ERROR,
2096 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2097 errmsg("timestamp out of range")));
2098
2099 /*
2100 * Could also do this with time = (timestamp / USECS_PER_DAY *
2101 * USECS_PER_DAY) - timestamp;
2102 */
2103 result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2104 USECS_PER_SEC) + fsec;
2105
2106 PG_RETURN_TIMEADT(result);
2107}

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

Referenced by executeDateTimeMethod().

◆ timestamptz_timetz()

Datum timestamptz_timetz ( PG_FUNCTION_ARGS  )

Definition at line 3001 of file date.c.

3002{
3004 TimeTzADT *result;
3005 struct pg_tm tt,
3006 *tm = &tt;
3007 int tz;
3008 fsec_t fsec;
3009
3012
3013 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
3014 ereport(ERROR,
3015 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3016 errmsg("timestamp out of range")));
3017
3018 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
3019
3020 tm2timetz(tm, fsec, tz, result);
3021
3022 PG_RETURN_TIMETZADT_P(result);
3023}

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

Referenced by executeDateTimeMethod().

◆ timetypmodin()

Datum timetypmodin ( PG_FUNCTION_ARGS  )

Definition at line 1705 of file date.c.

1706{
1708
1710}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
static int32 anytime_typmodin(bool istz, ArrayType *ta)
Definition: date.c:51

References anytime_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timetypmodout()

Datum timetypmodout ( PG_FUNCTION_ARGS  )

Definition at line 1713 of file date.c.

1714{
1715 int32 typmod = PG_GETARG_INT32(0);
1716
1717 PG_RETURN_CSTRING(anytime_typmodout(false, typmod));
1718}
static char * anytime_typmodout(bool istz, int32 typmod)
Definition: date.c:94

References anytime_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

◆ timetz2tm()

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

Definition at line 2550 of file date.c.

2551{
2552 TimeOffset trem = time->time;
2553
2554 tm->tm_hour = trem / USECS_PER_HOUR;
2555 trem -= tm->tm_hour * USECS_PER_HOUR;
2556 tm->tm_min = trem / USECS_PER_MINUTE;
2557 trem -= tm->tm_min * USECS_PER_MINUTE;
2558 tm->tm_sec = trem / USECS_PER_SEC;
2559 *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2560
2561 if (tzp != NULL)
2562 *tzp = time->zone;
2563
2564 return 0;
2565}
int64 TimeOffset
Definition: timestamp.h:40

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

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

◆ timetz_at_local()

Datum timetz_at_local ( PG_FUNCTION_ARGS  )

Definition at line 3312 of file date.c.

3313{
3314 Datum time = PG_GETARG_DATUM(0);
3315 const char *tzn = pg_get_timezone_name(session_timezone);
3317
3318 return DirectFunctionCall2(timetz_zone, zone, time);
3319}
Datum timetz_zone(PG_FUNCTION_ARGS)
Definition: date.c:3207
const char * pg_get_timezone_name(pg_tz *tz)
Definition: localtime.c:1989
Definition: zic.c:94
text * cstring_to_text(const char *s)
Definition: varlena.c:181

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

◆ timetz_cmp()

Datum timetz_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2671 of file date.c.

2672{
2673 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2674 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2675
2676 PG_RETURN_INT32(timetz_cmp_internal(time1, time2));
2677}

References PG_GETARG_TIMETZADT_P, PG_RETURN_INT32, and timetz_cmp_internal().

Referenced by compareDatetime().

◆ timetz_cmp_internal()

static int timetz_cmp_internal ( TimeTzADT time1,
TimeTzADT time2 
)
static

Definition at line 2590 of file date.c.

2591{
2592 TimeOffset t1,
2593 t2;
2594
2595 /* Primary sort is by true (GMT-equivalent) time */
2596 t1 = time1->time + (time1->zone * USECS_PER_SEC);
2597 t2 = time2->time + (time2->zone * USECS_PER_SEC);
2598
2599 if (t1 > t2)
2600 return 1;
2601 if (t1 < t2)
2602 return -1;
2603
2604 /*
2605 * If same GMT time, sort by timezone; we only want to say that two
2606 * timetz's are equal if both the time and zone parts are equal.
2607 */
2608 if (time1->zone > time2->zone)
2609 return 1;
2610 if (time1->zone < time2->zone)
2611 return -1;
2612
2613 return 0;
2614}

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

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

◆ timetz_eq()

Datum timetz_eq ( PG_FUNCTION_ARGS  )

Definition at line 2617 of file date.c.

2618{
2619 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2620 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2621
2622 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) == 0);
2623}

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_ge()

Datum timetz_ge ( PG_FUNCTION_ARGS  )

Definition at line 2662 of file date.c.

2663{
2664 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2665 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2666
2667 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) >= 0);
2668}

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_gt()

Datum timetz_gt ( PG_FUNCTION_ARGS  )

Definition at line 2653 of file date.c.

2654{
2655 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2656 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2657
2658 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) > 0);
2659}

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_hash()

Datum timetz_hash ( PG_FUNCTION_ARGS  )

Definition at line 2680 of file date.c.

2681{
2683 uint32 thash;
2684
2685 /*
2686 * To avoid any problems with padding bytes in the struct, we figure the
2687 * field hashes separately and XOR them.
2688 */
2690 Int64GetDatumFast(key->time)));
2691 thash ^= DatumGetUInt32(hash_uint32(key->zone));
2692 PG_RETURN_UINT32(thash);
2693}
uint32_t uint32
Definition: c.h:539
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:355
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:682
static uint32 DatumGetUInt32(Datum X)
Definition: postgres.h:232
#define Int64GetDatumFast(X)
Definition: postgres.h:515

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

◆ timetz_hash_extended()

Datum timetz_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2696 of file date.c.

2697{
2699 Datum seed = PG_GETARG_DATUM(1);
2700 uint64 thash;
2701
2702 /* Same approach as timetz_hash */
2704 Int64GetDatumFast(key->time),
2705 seed));
2707 DatumGetInt64(seed)));
2708 PG_RETURN_UINT64(thash);
2709}
uint64_t uint64
Definition: c.h:540
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:369
static uint64 DatumGetUInt64(Datum X)
Definition: postgres.h:413
static int64 DatumGetInt64(Datum X)
Definition: postgres.h:393

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

◆ timetz_in()

Datum timetz_in ( PG_FUNCTION_ARGS  )

Definition at line 2420 of file date.c.

2421{
2422 char *str = PG_GETARG_CSTRING(0);
2423#ifdef NOT_USED
2424 Oid typelem = PG_GETARG_OID(1);
2425#endif
2426 int32 typmod = PG_GETARG_INT32(2);
2427 Node *escontext = fcinfo->context;
2428 TimeTzADT *result;
2429 fsec_t fsec;
2430 struct pg_tm tt,
2431 *tm = &tt;
2432 int tz;
2433 int nf;
2434 int dterr;
2435 char workbuf[MAXDATELEN + 1];
2436 char *field[MAXDATEFIELDS];
2437 int dtype;
2438 int ftype[MAXDATEFIELDS];
2439 DateTimeErrorExtra extra;
2440
2441 dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
2442 field, ftype, MAXDATEFIELDS, &nf);
2443 if (dterr == 0)
2444 dterr = DecodeTimeOnly(field, ftype, nf,
2445 &dtype, tm, &fsec, &tz, &extra);
2446 if (dterr != 0)
2447 {
2448 DateTimeParseError(dterr, &extra, str, "time with time zone",
2449 escontext);
2451 }
2452
2453 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2454 tm2timetz(tm, fsec, tz, result);
2455 AdjustTimeForTypmod(&(result->time), typmod);
2456
2457 PG_RETURN_TIMETZADT_P(result);
2458}

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

◆ timetz_izone()

Datum timetz_izone ( PG_FUNCTION_ARGS  )

Definition at line 3269 of file date.c.

3270{
3273 TimeTzADT *result;
3274 int tz;
3275
3277 ereport(ERROR,
3278 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3279 errmsg("interval time zone \"%s\" must be finite",
3281 PointerGetDatum(zone))))));
3282
3283 if (zone->month != 0 || zone->day != 0)
3284 ereport(ERROR,
3285 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3286 errmsg("interval time zone \"%s\" must not include months or days",
3288 PointerGetDatum(zone))))));
3289
3290 tz = -(zone->time / USECS_PER_SEC);
3291
3292 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
3293
3294 result->time = time->time + (time->zone - tz) * USECS_PER_SEC;
3295 /* C99 modulo has the wrong sign convention for negative input */
3296 while (result->time < INT64CONST(0))
3297 result->time += USECS_PER_DAY;
3298 if (result->time >= USECS_PER_DAY)
3299 result->time %= USECS_PER_DAY;
3300
3301 result->zone = tz;
3302
3303 PG_RETURN_TIMETZADT_P(result);
3304}
Datum interval_out(PG_FUNCTION_ARGS)
Definition: timestamp.c:973
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
static char * DatumGetCString(Datum X)
Definition: postgres.h:345

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

◆ timetz_larger()

Datum timetz_larger ( PG_FUNCTION_ARGS  )

Definition at line 2712 of file date.c.

2713{
2714 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2715 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2716 TimeTzADT *result;
2717
2718 if (timetz_cmp_internal(time1, time2) > 0)
2719 result = time1;
2720 else
2721 result = time2;
2722 PG_RETURN_TIMETZADT_P(result);
2723}

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

◆ timetz_le()

Datum timetz_le ( PG_FUNCTION_ARGS  )

Definition at line 2644 of file date.c.

2645{
2646 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2647 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2648
2649 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) <= 0);
2650}

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_lt()

Datum timetz_lt ( PG_FUNCTION_ARGS  )

Definition at line 2635 of file date.c.

2636{
2637 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2638 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2639
2640 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) < 0);
2641}

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_mi_interval()

Datum timetz_mi_interval ( PG_FUNCTION_ARGS  )

Definition at line 2770 of file date.c.

2771{
2773 Interval *span = PG_GETARG_INTERVAL_P(1);
2774 TimeTzADT *result;
2775
2776 if (INTERVAL_NOT_FINITE(span))
2777 ereport(ERROR,
2778 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2779 errmsg("cannot subtract infinite interval from time")));
2780
2781 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2782
2783 result->time = time->time - span->time;
2784 result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2785 if (result->time < INT64CONST(0))
2786 result->time += USECS_PER_DAY;
2787
2788 result->zone = time->zone;
2789
2790 PG_RETURN_TIMETZADT_P(result);
2791}

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

◆ timetz_ne()

Datum timetz_ne ( PG_FUNCTION_ARGS  )

Definition at line 2626 of file date.c.

2627{
2628 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2629 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2630
2631 PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) != 0);
2632}

References PG_GETARG_TIMETZADT_P, PG_RETURN_BOOL, and timetz_cmp_internal().

◆ timetz_out()

Datum timetz_out ( PG_FUNCTION_ARGS  )

Definition at line 2461 of file date.c.

2462{
2464 char *result;
2465 struct pg_tm tt,
2466 *tm = &tt;
2467 fsec_t fsec;
2468 int tz;
2469 char buf[MAXDATELEN + 1];
2470
2471 timetz2tm(time, tm, &fsec, &tz);
2472 EncodeTimeOnly(tm, fsec, true, tz, DateStyle, buf);
2473
2474 result = pstrdup(buf);
2475 PG_RETURN_CSTRING(result);
2476}
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: date.c:2550

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

Referenced by ExecGetJsonValueItemString().

◆ timetz_part()

Datum timetz_part ( PG_FUNCTION_ARGS  )

Definition at line 3191 of file date.c.

3192{
3193 return timetz_part_common(fcinfo, false);
3194}

References timetz_part_common().

◆ timetz_part_common()

static Datum timetz_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 3074 of file date.c.

3075{
3076 text *units = PG_GETARG_TEXT_PP(0);
3078 int64 intresult;
3079 int type,
3080 val;
3081 char *lowunits;
3082
3083 lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
3084 VARSIZE_ANY_EXHDR(units),
3085 false);
3086
3087 type = DecodeUnits(0, lowunits, &val);
3088 if (type == UNKNOWN_FIELD)
3089 type = DecodeSpecial(0, lowunits, &val);
3090
3091 if (type == UNITS)
3092 {
3093 int tz;
3094 fsec_t fsec;
3095 struct pg_tm tt,
3096 *tm = &tt;
3097
3098 timetz2tm(time, tm, &fsec, &tz);
3099
3100 switch (val)
3101 {
3102 case DTK_TZ:
3103 intresult = -tz;
3104 break;
3105
3106 case DTK_TZ_MINUTE:
3107 intresult = (-tz / SECS_PER_MINUTE) % MINS_PER_HOUR;
3108 break;
3109
3110 case DTK_TZ_HOUR:
3111 intresult = -tz / SECS_PER_HOUR;
3112 break;
3113
3114 case DTK_MICROSEC:
3115 intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
3116 break;
3117
3118 case DTK_MILLISEC:
3119 if (retnumeric)
3120 /*---
3121 * tm->tm_sec * 1000 + fsec / 1000
3122 * = (tm->tm_sec * 1'000'000 + fsec) / 1000
3123 */
3125 else
3126 PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
3127 break;
3128
3129 case DTK_SECOND:
3130 if (retnumeric)
3131 /*---
3132 * tm->tm_sec + fsec / 1'000'000
3133 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
3134 */
3136 else
3137 PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
3138 break;
3139
3140 case DTK_MINUTE:
3141 intresult = tm->tm_min;
3142 break;
3143
3144 case DTK_HOUR:
3145 intresult = tm->tm_hour;
3146 break;
3147
3148 case DTK_DAY:
3149 case DTK_MONTH:
3150 case DTK_QUARTER:
3151 case DTK_YEAR:
3152 case DTK_DECADE:
3153 case DTK_CENTURY:
3154 case DTK_MILLENNIUM:
3155 default:
3156 ereport(ERROR,
3157 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3158 errmsg("unit \"%s\" not supported for type %s",
3159 lowunits, format_type_be(TIMETZOID))));
3160 intresult = 0;
3161 }
3162 }
3163 else if (type == RESERV && val == DTK_EPOCH)
3164 {
3165 if (retnumeric)
3166 /*---
3167 * time->time / 1'000'000 + time->zone
3168 * = (time->time + time->zone * 1'000'000) / 1'000'000
3169 */
3170 PG_RETURN_NUMERIC(int64_div_fast_to_numeric(time->time + time->zone * INT64CONST(1000000), 6));
3171 else
3172 PG_RETURN_FLOAT8(time->time / 1000000.0 + time->zone);
3173 }
3174 else
3175 {
3176 ereport(ERROR,
3177 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3178 errmsg("unit \"%s\" not recognized for type %s",
3179 lowunits, format_type_be(TIMETZOID))));
3180 intresult = 0;
3181 }
3182
3183 if (retnumeric)
3185 else
3186 PG_RETURN_FLOAT8(intresult);
3187}
#define SECS_PER_HOUR
Definition: timestamp.h:127

References DecodeSpecial(), DecodeUnits(), downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_EPOCH, DTK_HOUR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_TZ, DTK_TZ_HOUR, DTK_TZ_MINUTE, DTK_YEAR, ereport, errcode(), errmsg(), ERROR, format_type_be(), int64_div_fast_to_numeric(), int64_to_numeric(), INT64CONST, MINS_PER_HOUR, PG_GETARG_TEXT_PP, PG_GETARG_TIMETZADT_P, PG_RETURN_FLOAT8, PG_RETURN_NUMERIC, RESERV, SECS_PER_HOUR, SECS_PER_MINUTE, TimeTzADT::time, timetz2tm(), tm, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, type, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY(), VARSIZE_ANY_EXHDR(), and TimeTzADT::zone.

Referenced by extract_timetz(), and timetz_part().

◆ timetz_pl_interval()

Datum timetz_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 2743 of file date.c.

2744{
2746 Interval *span = PG_GETARG_INTERVAL_P(1);
2747 TimeTzADT *result;
2748
2749 if (INTERVAL_NOT_FINITE(span))
2750 ereport(ERROR,
2751 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2752 errmsg("cannot add infinite interval to time")));
2753
2754 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2755
2756 result->time = time->time + span->time;
2757 result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
2758 if (result->time < INT64CONST(0))
2759 result->time += USECS_PER_DAY;
2760
2761 result->zone = time->zone;
2762
2763 PG_RETURN_TIMETZADT_P(result);
2764}

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

◆ timetz_recv()

Datum timetz_recv ( PG_FUNCTION_ARGS  )

Definition at line 2482 of file date.c.

2483{
2485
2486#ifdef NOT_USED
2487 Oid typelem = PG_GETARG_OID(1);
2488#endif
2489 int32 typmod = PG_GETARG_INT32(2);
2490 TimeTzADT *result;
2491
2492 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2493
2494 result->time = pq_getmsgint64(buf);
2495
2496 if (result->time < INT64CONST(0) || result->time > USECS_PER_DAY)
2497 ereport(ERROR,
2498 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2499 errmsg("time out of range")));
2500
2501 result->zone = pq_getmsgint(buf, sizeof(result->zone));
2502
2503 /* Check for sane GMT displacement; see notes in datatype/timestamp.h */
2504 if (result->zone <= -TZDISP_LIMIT || result->zone >= TZDISP_LIMIT)
2505 ereport(ERROR,
2506 (errcode(ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE),
2507 errmsg("time zone displacement out of range")));
2508
2509 AdjustTimeForTypmod(&(result->time), typmod);
2510
2511 PG_RETURN_TIMETZADT_P(result);
2512}
#define TZDISP_LIMIT
Definition: timestamp.h:144

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

◆ timetz_scale()

Datum timetz_scale ( PG_FUNCTION_ARGS  )

Definition at line 2572 of file date.c.

2573{
2575 int32 typmod = PG_GETARG_INT32(1);
2576 TimeTzADT *result;
2577
2578 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
2579
2580 result->time = time->time;
2581 result->zone = time->zone;
2582
2583 AdjustTimeForTypmod(&(result->time), typmod);
2584
2585 PG_RETURN_TIMETZADT_P(result);
2586}

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

◆ timetz_send()

◆ timetz_smaller()

Datum timetz_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2726 of file date.c.

2727{
2728 TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
2729 TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
2730 TimeTzADT *result;
2731
2732 if (timetz_cmp_internal(time1, time2) < 0)
2733 result = time1;
2734 else
2735 result = time2;
2736 PG_RETURN_TIMETZADT_P(result);
2737}

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMETZADT_P, and timetz_cmp_internal().

◆ timetz_time()

Datum timetz_time ( PG_FUNCTION_ARGS  )

Definition at line 2962 of file date.c.

2963{
2964 TimeTzADT *timetz = PG_GETARG_TIMETZADT_P(0);
2965 TimeADT result;
2966
2967 /* swallow the time zone and just return the time */
2968 result = timetz->time;
2969
2970 PG_RETURN_TIMEADT(result);
2971}

References PG_GETARG_TIMETZADT_P, PG_RETURN_TIMEADT, and TimeTzADT::time.

Referenced by executeDateTimeMethod().

◆ timetz_zone()

Datum timetz_zone ( PG_FUNCTION_ARGS  )

Definition at line 3207 of file date.c.

3208{
3211 TimeTzADT *result;
3212 int tz;
3213 char tzname[TZ_STRLEN_MAX + 1];
3214 int type,
3215 val;
3216 pg_tz *tzp;
3217
3218 /*
3219 * Look up the requested timezone.
3220 */
3221 text_to_cstring_buffer(zone, tzname, sizeof(tzname));
3222
3223 type = DecodeTimezoneName(tzname, &val, &tzp);
3224
3226 {
3227 /* fixed-offset abbreviation */
3228 tz = -val;
3229 }
3230 else if (type == TZNAME_DYNTZ)
3231 {
3232 /* dynamic-offset abbreviation, resolve using transaction start time */
3234 int isdst;
3235
3236 tz = DetermineTimeZoneAbbrevOffsetTS(now, tzname, tzp, &isdst);
3237 }
3238 else
3239 {
3240 /* Get the offset-from-GMT that is valid now for the zone name */
3242 struct pg_tm tm;
3243 fsec_t fsec;
3244
3245 if (timestamp2tm(now, &tz, &tm, &fsec, NULL, tzp) != 0)
3246 ereport(ERROR,
3247 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3248 errmsg("timestamp out of range")));
3249 }
3250
3251 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
3252
3253 result->time = t->time + (t->zone - tz) * USECS_PER_SEC;
3254 /* C99 modulo has the wrong sign convention for negative input */
3255 while (result->time < INT64CONST(0))
3256 result->time += USECS_PER_DAY;
3257 if (result->time >= USECS_PER_DAY)
3258 result->time %= USECS_PER_DAY;
3259
3260 result->zone = tz;
3261
3262 PG_RETURN_TIMETZADT_P(result);
3263}
int DetermineTimeZoneAbbrevOffsetTS(TimestampTz ts, const char *abbr, pg_tz *tzp, int *isdst)
Definition: datetime.c:1803
int DecodeTimezoneName(const char *tzname, int *offset, pg_tz **tz)
Definition: datetime.c:3288
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
#define TZNAME_FIXED_OFFSET
Definition: datetime.h:299
#define TZNAME_DYNTZ
Definition: datetime.h:300
#define TZ_STRLEN_MAX
Definition: pgtime.h:54
Definition: pgtz.h:66
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition: varlena.c:245
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:870

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

Referenced by timetz_at_local().

◆ timetztypmodin()

Datum timetztypmodin ( PG_FUNCTION_ARGS  )

Definition at line 2530 of file date.c.

2531{
2533
2535}

References anytime_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timetztypmodout()

Datum timetztypmodout ( PG_FUNCTION_ARGS  )

Definition at line 2538 of file date.c.

2539{
2540 int32 typmod = PG_GETARG_INT32(0);
2541
2542 PG_RETURN_CSTRING(anytime_typmodout(true, typmod));
2543}

References anytime_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

◆ tm2time()

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

Definition at line 1563 of file date.c.

1564{
1565 *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1566 * USECS_PER_SEC) + fsec;
1567 return 0;
1568}

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

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

◆ tm2timetz()

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

Definition at line 2410 of file date.c.

2411{
2412 result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2413 USECS_PER_SEC) + fsec;
2414 result->zone = tz;
2415
2416 return 0;
2417}

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

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