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

PostgreSQL Source Code git master
date.h File Reference
#include <math.h>
#include "datatype/timestamp.h"
#include "fmgr.h"
#include "pgtime.h"
Include dependency graph for date.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  TimeTzADT
 

Macros

#define DATEVAL_NOBEGIN   ((DateADT) PG_INT32_MIN)
 
#define DATEVAL_NOEND   ((DateADT) PG_INT32_MAX)
 
#define DATE_NOBEGIN(j)   ((j) = DATEVAL_NOBEGIN)
 
#define DATE_IS_NOBEGIN(j)   ((j) == DATEVAL_NOBEGIN)
 
#define DATE_NOEND(j)   ((j) = DATEVAL_NOEND)
 
#define DATE_IS_NOEND(j)   ((j) == DATEVAL_NOEND)
 
#define DATE_NOT_FINITE(j)   (DATE_IS_NOBEGIN(j) || DATE_IS_NOEND(j))
 
#define MAX_TIME_PRECISION   6
 
#define PG_GETARG_DATEADT(n)   DatumGetDateADT(PG_GETARG_DATUM(n))
 
#define PG_GETARG_TIMEADT(n)   DatumGetTimeADT(PG_GETARG_DATUM(n))
 
#define PG_GETARG_TIMETZADT_P(n)   DatumGetTimeTzADTP(PG_GETARG_DATUM(n))
 
#define PG_RETURN_DATEADT(x)   return DateADTGetDatum(x)
 
#define PG_RETURN_TIMEADT(x)   return TimeADTGetDatum(x)
 
#define PG_RETURN_TIMETZADT_P(x)   return TimeTzADTPGetDatum(x)
 

Typedefs

typedef int32 DateADT
 
typedef int64 TimeADT
 

Functions

static DateADT DatumGetDateADT (Datum X)
 
static TimeADT DatumGetTimeADT (Datum X)
 
static TimeTzADTDatumGetTimeTzADTP (Datum X)
 
static Datum DateADTGetDatum (DateADT X)
 
static Datum TimeADTGetDatum (TimeADT X)
 
static Datum TimeTzADTPGetDatum (const TimeTzADT *X)
 
int32 anytime_typmod_check (bool istz, int32 typmod)
 
double date2timestamp_no_overflow (DateADT dateVal)
 
Timestamp date2timestamp_opt_overflow (DateADT dateVal, int *overflow)
 
TimestampTz date2timestamptz_opt_overflow (DateADT dateVal, int *overflow)
 
DateADT timestamp2date_opt_overflow (Timestamp timestamp, int *overflow)
 
DateADT timestamptz2date_opt_overflow (TimestampTz timestamp, int *overflow)
 
int32 date_cmp_timestamp_internal (DateADT dateVal, Timestamp dt2)
 
int32 date_cmp_timestamptz_internal (DateADT dateVal, TimestampTz dt2)
 
void EncodeSpecialDate (DateADT dt, char *str)
 
DateADT GetSQLCurrentDate (void)
 
TimeTzADTGetSQLCurrentTime (int32 typmod)
 
TimeADT GetSQLLocalTime (int32 typmod)
 
int time2tm (TimeADT time, struct pg_tm *tm, fsec_t *fsec)
 
int timetz2tm (TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
 
int tm2time (struct pg_tm *tm, fsec_t fsec, TimeADT *result)
 
int tm2timetz (struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
 
bool time_overflows (int hour, int min, int sec, fsec_t fsec)
 
bool float_time_overflows (int hour, int min, double sec)
 
void AdjustTimeForTypmod (TimeADT *time, int32 typmod)
 

Macro Definition Documentation

◆ DATE_IS_NOBEGIN

#define DATE_IS_NOBEGIN (   j)    ((j) == DATEVAL_NOBEGIN)

Definition at line 40 of file date.h.

◆ DATE_IS_NOEND

#define DATE_IS_NOEND (   j)    ((j) == DATEVAL_NOEND)

Definition at line 42 of file date.h.

◆ DATE_NOBEGIN

#define DATE_NOBEGIN (   j)    ((j) = DATEVAL_NOBEGIN)

Definition at line 39 of file date.h.

◆ DATE_NOEND

#define DATE_NOEND (   j)    ((j) = DATEVAL_NOEND)

Definition at line 41 of file date.h.

◆ DATE_NOT_FINITE

#define DATE_NOT_FINITE (   j)    (DATE_IS_NOBEGIN(j) || DATE_IS_NOEND(j))

Definition at line 43 of file date.h.

◆ DATEVAL_NOBEGIN

#define DATEVAL_NOBEGIN   ((DateADT) PG_INT32_MIN)

Definition at line 36 of file date.h.

◆ DATEVAL_NOEND

#define DATEVAL_NOEND   ((DateADT) PG_INT32_MAX)

Definition at line 37 of file date.h.

◆ MAX_TIME_PRECISION

#define MAX_TIME_PRECISION   6

Definition at line 45 of file date.h.

◆ PG_GETARG_DATEADT

#define PG_GETARG_DATEADT (   n)    DatumGetDateADT(PG_GETARG_DATUM(n))

Definition at line 89 of file date.h.

◆ PG_GETARG_TIMEADT

#define PG_GETARG_TIMEADT (   n)    DatumGetTimeADT(PG_GETARG_DATUM(n))

Definition at line 90 of file date.h.

◆ PG_GETARG_TIMETZADT_P

#define PG_GETARG_TIMETZADT_P (   n)    DatumGetTimeTzADTP(PG_GETARG_DATUM(n))

Definition at line 91 of file date.h.

◆ PG_RETURN_DATEADT

#define PG_RETURN_DATEADT (   x)    return DateADTGetDatum(x)

Definition at line 93 of file date.h.

◆ PG_RETURN_TIMEADT

#define PG_RETURN_TIMEADT (   x)    return TimeADTGetDatum(x)

Definition at line 94 of file date.h.

◆ PG_RETURN_TIMETZADT_P

#define PG_RETURN_TIMETZADT_P (   x)    return TimeTzADTPGetDatum(x)

Definition at line 95 of file date.h.

Typedef Documentation

◆ DateADT

typedef int32 DateADT

Definition at line 23 of file date.h.

◆ TimeADT

typedef int64 TimeADT

Definition at line 25 of file date.h.

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

◆ 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_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_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
Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:629
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_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
TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:689
#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().

◆ DateADTGetDatum()

◆ DatumGetDateADT()

static DateADT DatumGetDateADT ( Datum  X)
inlinestatic

◆ DatumGetTimeADT()

static TimeADT DatumGetTimeADT ( Datum  X)
inlinestatic

Definition at line 60 of file date.h.

61{
62 return (TimeADT) DatumGetInt64(X);
63}
int64 TimeADT
Definition: date.h:25
static int64 DatumGetInt64(Datum X)
Definition: postgres.h:393

References DatumGetInt64().

Referenced by convert_timevalue_to_scalar(), executeDateTimeMethod(), gbt_num_compress(), and JsonEncodeDateTime().

◆ DatumGetTimeTzADTP()

static TimeTzADT * DatumGetTimeTzADTP ( Datum  X)
inlinestatic

Definition at line 66 of file date.h.

67{
68 return (TimeTzADT *) DatumGetPointer(X);
69}
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322
Definition: date.h:28

References DatumGetPointer().

Referenced by convert_timevalue_to_scalar(), executeDateTimeMethod(), gbt_timetz_compress(), and JsonEncodeDateTime().

◆ 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
const char * str
#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().

◆ 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
int date2j(int year, int month, int day)
Definition: datetime.c:296

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
int32 fsec_t
Definition: timestamp.h:41
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
TimeADT time
Definition: date.h:29

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

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

◆ TimeADTGetDatum()

static Datum TimeADTGetDatum ( TimeADT  X)
inlinestatic

◆ 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
#define DATE_NOEND(j)
Definition: date.h:41
#define DATE_NOBEGIN(j)
Definition: date.h:39
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().

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

◆ 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
int32 zone
Definition: date.h:30

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

◆ TimeTzADTPGetDatum()

static Datum TimeTzADTPGetDatum ( const TimeTzADT X)
inlinestatic

Definition at line 84 of file date.h.

85{
86 return PointerGetDatum(X);
87}
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332

References PointerGetDatum().

Referenced by ExecEvalSQLValueFunction(), executeDateTimeMethod(), leftmostvalue_timetz(), and parse_datetime().

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