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_safe (DateADT dateVal, Node *escontext)
 
TimestampTz date2timestamptz_safe (DateADT dateVal, Node *escontext)
 
DateADT timestamp2date_safe (Timestamp timestamp, Node *escontext)
 
DateADT timestamptz2date_safe (TimestampTz timestamp, Node *escontext)
 
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 1741 of file date.c.

1742{
1743 static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1744 INT64CONST(1000000),
1745 INT64CONST(100000),
1746 INT64CONST(10000),
1747 INT64CONST(1000),
1748 INT64CONST(100),
1749 INT64CONST(10),
1750 INT64CONST(1)
1751 };
1752
1753 static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1754 INT64CONST(500000),
1755 INT64CONST(50000),
1756 INT64CONST(5000),
1757 INT64CONST(500),
1758 INT64CONST(50),
1759 INT64CONST(5),
1760 INT64CONST(0)
1761 };
1762
1763 if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1764 {
1765 if (*time >= INT64CONST(0))
1766 *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1767 TimeScales[typmod];
1768 else
1769 *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1770 TimeScales[typmod]);
1771 }
1772}
#define INT64CONST(x)
Definition: c.h:555
int64_t int64
Definition: c.h:538
#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 73 of file date.c.

74{
75 if (typmod < 0)
77 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
78 errmsg("TIME(%d)%s precision must not be negative",
79 typmod, (istz ? " WITH TIME ZONE" : ""))));
80 if (typmod > MAX_TIME_PRECISION)
81 {
83 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
84 errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
85 typmod, (istz ? " WITH TIME ZONE" : ""),
87 typmod = MAX_TIME_PRECISION;
88 }
89
90 return typmod;
91}
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#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 753 of file date.c.

754{
755 double result;
756
757 if (DATE_IS_NOBEGIN(dateVal))
758 result = -DBL_MAX;
759 else if (DATE_IS_NOEND(dateVal))
760 result = DBL_MAX;
761 else
762 {
763 /* date is days since 2000, timestamp is microseconds since same... */
764 result = dateVal * (double) USECS_PER_DAY;
765 }
766
767 return result;
768}
#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_safe()

Timestamp date2timestamp_safe ( DateADT  dateVal,
Node escontext 
)

Definition at line 630 of file date.c.

631{
632 Timestamp result;
633
634 if (DATE_IS_NOBEGIN(dateVal))
635 TIMESTAMP_NOBEGIN(result);
636 else if (DATE_IS_NOEND(dateVal))
637 TIMESTAMP_NOEND(result);
638 else
639 {
640 /*
641 * Since dates have the same minimum values as timestamps, only upper
642 * boundary need be checked for overflow.
643 */
645 {
646 TIMESTAMP_NOEND(result);
647 ereturn(escontext, result,
648 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
649 errmsg("date out of range for timestamp")));
650 }
651
652 /* date is days since 2000, timestamp is microseconds since same... */
653 result = dateVal * USECS_PER_DAY;
654 }
655
656 return result;
657}
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
#define ereturn(context, dummy_value,...)
Definition: elog.h:278

References DATE_IS_NOBEGIN, DATE_IS_NOEND, ereturn, errcode(), errmsg(), 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_safe()

TimestampTz date2timestamptz_safe ( DateADT  dateVal,
Node escontext 
)

Definition at line 679 of file date.c.

680{
681 TimestampTz result;
682 struct pg_tm tt,
683 *tm = &tt;
684 int tz;
685
686 if (DATE_IS_NOBEGIN(dateVal))
687 TIMESTAMP_NOBEGIN(result);
688 else if (DATE_IS_NOEND(dateVal))
689 TIMESTAMP_NOEND(result);
690 else
691 {
692 /*
693 * Since dates have the same minimum values as timestamps, only upper
694 * boundary need be checked for overflow.
695 */
697 {
698 TIMESTAMP_NOEND(result);
699 ereturn(escontext, result,
700 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
701 errmsg("date out of range for timestamp")));
702 }
703
705 &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
706 tm->tm_hour = 0;
707 tm->tm_min = 0;
708 tm->tm_sec = 0;
710
711 result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
712
713 /*
714 * Since it is possible to go beyond allowed timestamptz range because
715 * of time zone, check for allowed timestamp range after adding tz.
716 */
717 if (!IS_VALID_TIMESTAMP(result))
718 {
719 if (result < MIN_TIMESTAMP)
720 TIMESTAMP_NOBEGIN(result);
721 else
722 TIMESTAMP_NOEND(result);
723
724 ereturn(escontext, result,
725 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
726 errmsg("date out of range for timestamp")));
727 }
728 }
729
730 return result;
731}
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(), ereturn, errcode(), errmsg(), 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 776 of file date.c.

777{
778 Timestamp dt1;
779 ErrorSaveContext escontext = {T_ErrorSaveContext};
780
781 dt1 = date2timestamp_safe(dateVal, (Node *) &escontext);
782 if (escontext.error_occurred)
783 {
784 Assert(TIMESTAMP_IS_NOEND(dt1)); /* NOBEGIN case cannot occur */
785
786 /* dt1 is larger than any finite timestamp, but less than infinity */
787 return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
788 }
789
790 return timestamp_cmp_internal(dt1, dt2);
791}
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2210
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:167
Timestamp date2timestamp_safe(DateADT dateVal, Node *escontext)
Definition: date.c:630
Assert(PointerIsAligned(start, uint64))
bool error_occurred
Definition: miscnodes.h:47
Definition: nodes.h:135

References Assert(), date2timestamp_safe(), ErrorSaveContext::error_occurred, 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 857 of file date.c.

858{
859 TimestampTz dt1;
860 ErrorSaveContext escontext = {T_ErrorSaveContext};
861
862 dt1 = date2timestamptz_safe(dateVal, (Node *) &escontext);
863
864 if (escontext.error_occurred)
865 {
866 if (TIMESTAMP_IS_NOEND(dt1))
867 {
868 /* dt1 is larger than any finite timestamp, but less than infinity */
869 return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
870 }
871 if (TIMESTAMP_IS_NOBEGIN(dt1))
872 {
873 /* dt1 is less than any finite timestamp, but more than -infinity */
874 return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
875 }
876 }
877
878 return timestamptz_cmp_internal(dt1, dt2);
879}
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:162
TimestampTz date2timestamptz_safe(DateADT dateVal, Node *escontext)
Definition: date.c:679
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:143

References date2timestamptz_safe(), ErrorSaveContext::error_occurred, 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 303 of file date.c.

304{
305 if (DATE_IS_NOBEGIN(dt))
306 strcpy(str, EARLY);
307 else if (DATE_IS_NOEND(dt))
308 strcpy(str, LATE);
309 else /* shouldn't happen */
310 elog(ERROR, "invalid argument for EncodeSpecialDate");
311}
#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 1547 of file date.c.

1548{
1549 /* Range-check the fields individually. */
1550 if (hour < 0 || hour > HOURS_PER_DAY ||
1551 min < 0 || min >= MINS_PER_HOUR)
1552 return true;
1553
1554 /*
1555 * "sec", being double, requires extra care. Cope with NaN, and round off
1556 * before applying the range check to avoid unexpected errors due to
1557 * imprecise input. (We assume rint() behaves sanely with infinities.)
1558 */
1559 if (isnan(sec))
1560 return true;
1561 sec = rint(sec * USECS_PER_SEC);
1562 if (sec < 0 || sec > SECS_PER_MINUTE * USECS_PER_SEC)
1563 return true;
1564
1565 /*
1566 * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1567 * that the total time value doesn't exceed 24:00:00. This must match the
1568 * way that callers will convert the fields to a time.
1569 */
1570 if (((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1571 * USECS_PER_SEC) + (int64) sec) > USECS_PER_DAY)
1572 return true;
1573
1574 return false;
1575}
#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 318 of file date.c.

319{
320 struct pg_tm tm;
321
322 static int cache_year = 0;
323 static int cache_mon = 0;
324 static int cache_mday = 0;
325 static DateADT cache_date;
326
328
329 /*
330 * date2j involves several integer divisions; moreover, unless our session
331 * lives across local midnight, we don't really have to do it more than
332 * once. So it seems worth having a separate cache here.
333 */
334 if (tm.tm_year != cache_year ||
335 tm.tm_mon != cache_mon ||
336 tm.tm_mday != cache_mday)
337 {
339 cache_year = tm.tm_year;
340 cache_mon = tm.tm_mon;
341 cache_mday = tm.tm_mday;
342 }
343
344 return cache_date;
345}
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 351 of file date.c.

352{
353 TimeTzADT *result;
354 struct pg_tm tt,
355 *tm = &tt;
356 fsec_t fsec;
357 int tz;
358
359 GetCurrentTimeUsec(tm, &fsec, &tz);
360
361 result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
362 tm2timetz(tm, fsec, tz, result);
363 AdjustTimeForTypmod(&(result->time), typmod);
364 return result;
365}
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:2359
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1741
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 371 of file date.c.

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

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

Referenced by ExecEvalSQLValueFunction().

◆ time2tm()

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

Definition at line 1584 of file date.c.

1585{
1586 tm->tm_hour = time / USECS_PER_HOUR;
1587 time -= tm->tm_hour * USECS_PER_HOUR;
1588 tm->tm_min = time / USECS_PER_MINUTE;
1589 time -= tm->tm_min * USECS_PER_MINUTE;
1590 tm->tm_sec = time / USECS_PER_SEC;
1591 time -= tm->tm_sec * USECS_PER_SEC;
1592 *fsec = time;
1593 return 0;
1594}
#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 1523 of file date.c.

1524{
1525 /* Range-check the fields individually. */
1526 if (hour < 0 || hour > HOURS_PER_DAY ||
1527 min < 0 || min >= MINS_PER_HOUR ||
1528 sec < 0 || sec > SECS_PER_MINUTE ||
1529 fsec < 0 || fsec > USECS_PER_SEC)
1530 return true;
1531
1532 /*
1533 * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1534 * that the total time value doesn't exceed 24:00:00.
1535 */
1536 if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1537 + sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1538 return true;
1539
1540 return false;
1541}

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

DateADT timestamp2date_safe ( Timestamp  timestamp,
Node escontext 
)

Definition at line 1358 of file date.c.

1359{
1360 DateADT result;
1361 struct pg_tm tt,
1362 *tm = &tt;
1363 fsec_t fsec;
1364
1366 DATE_NOBEGIN(result);
1367 else if (TIMESTAMP_IS_NOEND(timestamp))
1368 DATE_NOEND(result);
1369 else
1370 {
1371 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1372 {
1373 if (timestamp < 0)
1374 DATE_NOBEGIN(result);
1375 else
1376 DATE_NOEND(result); /* not actually reachable */
1377
1378 ereturn(escontext, result,
1379 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1380 errmsg("timestamp out of range")));
1381 }
1382
1384 }
1385
1386 return result;
1387}
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, ereturn, errcode(), errmsg(), 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_safe()

DateADT timestamptz2date_safe ( TimestampTz  timestamp,
Node escontext 
)

Definition at line 1432 of file date.c.

1433{
1434 DateADT result;
1435 struct pg_tm tt,
1436 *tm = &tt;
1437 fsec_t fsec;
1438 int tz;
1439
1441 DATE_NOBEGIN(result);
1442 else if (TIMESTAMP_IS_NOEND(timestamp))
1443 DATE_NOEND(result);
1444 else
1445 {
1446 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1447 {
1448 if (timestamp < 0)
1449 DATE_NOBEGIN(result);
1450 else
1451 DATE_NOEND(result); /* not actually reachable */
1452
1453 ereturn(escontext, result,
1454 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1455 errmsg("timestamp out of range")));
1456 }
1457
1459 }
1460
1461 return result;
1462}

References date2j(), DATE_NOBEGIN, DATE_NOEND, ereturn, errcode(), errmsg(), 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 2499 of file date.c.

2500{
2501 TimeOffset trem = time->time;
2502
2503 tm->tm_hour = trem / USECS_PER_HOUR;
2504 trem -= tm->tm_hour * USECS_PER_HOUR;
2505 tm->tm_min = trem / USECS_PER_MINUTE;
2506 trem -= tm->tm_min * USECS_PER_MINUTE;
2507 tm->tm_sec = trem / USECS_PER_SEC;
2508 *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2509
2510 if (tzp != NULL)
2511 *tzp = time->zone;
2512
2513 return 0;
2514}
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 1512 of file date.c.

1513{
1514 *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1515 * USECS_PER_SEC) + fsec;
1516 return 0;
1517}

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

2360{
2361 result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2362 USECS_PER_SEC) + fsec;
2363 result->zone = tz;
2364
2365 return 0;
2366}

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