PostgreSQL Source Code git master
Loading...
Searching...
No Matches
date.h File Reference
#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 TIMETZ_TYPLEN   12
 
#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 46 of file date.h.

◆ DATE_IS_NOEND

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

Definition at line 48 of file date.h.

◆ DATE_NOBEGIN

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

Definition at line 45 of file date.h.

◆ DATE_NOEND

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

Definition at line 47 of file date.h.

◆ DATE_NOT_FINITE

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

Definition at line 49 of file date.h.

◆ DATEVAL_NOBEGIN

#define DATEVAL_NOBEGIN   ((DateADT) PG_INT32_MIN)

Definition at line 42 of file date.h.

◆ DATEVAL_NOEND

#define DATEVAL_NOEND   ((DateADT) PG_INT32_MAX)

Definition at line 43 of file date.h.

◆ MAX_TIME_PRECISION

#define MAX_TIME_PRECISION   6

Definition at line 51 of file date.h.

◆ PG_GETARG_DATEADT

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

Definition at line 95 of file date.h.

◆ PG_GETARG_TIMEADT

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

Definition at line 96 of file date.h.

◆ PG_GETARG_TIMETZADT_P

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

Definition at line 97 of file date.h.

◆ PG_RETURN_DATEADT

#define PG_RETURN_DATEADT (   x)    return DateADTGetDatum(x)

Definition at line 99 of file date.h.

◆ PG_RETURN_TIMEADT

#define PG_RETURN_TIMEADT (   x)    return TimeADTGetDatum(x)

Definition at line 100 of file date.h.

◆ PG_RETURN_TIMETZADT_P

#define PG_RETURN_TIMETZADT_P (   x)    return TimeTzADTPGetDatum(x)

Definition at line 101 of file date.h.

◆ TIMETZ_TYPLEN

#define TIMETZ_TYPLEN   12

Definition at line 37 of file date.h.

Typedef Documentation

◆ DateADT

Definition at line 21 of file date.h.

◆ TimeADT

Definition at line 23 of file date.h.

Function Documentation

◆ AdjustTimeForTypmod()

void AdjustTimeForTypmod ( TimeADT time,
int32  typmod 
)
extern

Definition at line 1733 of file date.c.

1734{
1735 static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1736 INT64CONST(1000000),
1737 INT64CONST(100000),
1738 INT64CONST(10000),
1739 INT64CONST(1000),
1740 INT64CONST(100),
1741 INT64CONST(10),
1742 INT64CONST(1)
1743 };
1744
1745 static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1746 INT64CONST(500000),
1747 INT64CONST(50000),
1748 INT64CONST(5000),
1749 INT64CONST(500),
1750 INT64CONST(50),
1751 INT64CONST(5),
1752 INT64CONST(0)
1753 };
1754
1755 if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1756 {
1757 if (*time >= INT64CONST(0))
1758 *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1759 TimeScales[typmod];
1760 else
1761 *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1762 TimeScales[typmod]);
1763 }
1764}
#define INT64CONST(x)
Definition c.h:632
int64_t int64
Definition c.h:615
#define MAX_TIME_PRECISION
Definition date.h:51
static int fb(int x)

References fb(), 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 
)
extern

Definition at line 65 of file date.c.

66{
67 if (typmod < 0)
70 errmsg("TIME(%d)%s precision must not be negative",
71 typmod, (istz ? " WITH TIME ZONE" : ""))));
72 if (typmod > MAX_TIME_PRECISION)
73 {
76 errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
77 typmod, (istz ? " WITH TIME ZONE" : ""),
79 typmod = MAX_TIME_PRECISION;
80 }
81
82 return typmod;
83}
int errcode(int sqlerrcode)
Definition elog.c:874
#define WARNING
Definition elog.h:36
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
static char * errmsg

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

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

◆ date2timestamp_no_overflow()

double date2timestamp_no_overflow ( DateADT  dateVal)
extern

Definition at line 745 of file date.c.

746{
747 double result;
748
750 result = -DBL_MAX;
751 else if (DATE_IS_NOEND(dateVal))
752 result = DBL_MAX;
753 else
754 {
755 /* date is days since 2000, timestamp is microseconds since same... */
756 result = dateVal * (double) USECS_PER_DAY;
757 }
758
759 return result;
760}
#define USECS_PER_DAY
Definition timestamp.h:131
#define DATE_IS_NOEND(j)
Definition date.h:48
#define DATE_IS_NOBEGIN(j)
Definition date.h:46

References DATE_IS_NOBEGIN, DATE_IS_NOEND, fb(), and USECS_PER_DAY.

Referenced by convert_timevalue_to_scalar().

◆ date2timestamp_safe()

Timestamp date2timestamp_safe ( DateADT  dateVal,
Node escontext 
)
extern

Definition at line 622 of file date.c.

623{
624 Timestamp result;
625
627 TIMESTAMP_NOBEGIN(result);
628 else if (DATE_IS_NOEND(dateVal))
629 TIMESTAMP_NOEND(result);
630 else
631 {
632 /*
633 * Since dates have the same minimum values as timestamps, only upper
634 * boundary need be checked for overflow.
635 */
637 {
638 TIMESTAMP_NOEND(result);
639 ereturn(escontext, result,
641 errmsg("date out of range for timestamp")));
642 }
643
644 /* date is days since 2000, timestamp is microseconds since same... */
645 result = dateVal * USECS_PER_DAY;
646 }
647
648 return result;
649}
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, fb(), 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 
)
extern

Definition at line 671 of file date.c.

672{
673 TimestampTz result;
674 struct pg_tm tt,
675 *tm = &tt;
676 int tz;
677
679 TIMESTAMP_NOBEGIN(result);
680 else if (DATE_IS_NOEND(dateVal))
681 TIMESTAMP_NOEND(result);
682 else
683 {
684 /*
685 * Since dates have the same minimum values as timestamps, only upper
686 * boundary need be checked for overflow.
687 */
689 {
690 TIMESTAMP_NOEND(result);
691 ereturn(escontext, result,
693 errmsg("date out of range for timestamp")));
694 }
695
697 &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
698 tm->tm_hour = 0;
699 tm->tm_min = 0;
700 tm->tm_sec = 0;
702
703 result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
704
705 /*
706 * Since it is possible to go beyond allowed timestamptz range because
707 * of time zone, check for allowed timestamp range after adding tz.
708 */
709 if (!IS_VALID_TIMESTAMP(result))
710 {
711 if (result < MIN_TIMESTAMP)
712 TIMESTAMP_NOBEGIN(result);
713 else
714 TIMESTAMP_NOEND(result);
715
716 ereturn(escontext, result,
718 errmsg("date out of range for timestamp")));
719 }
720 }
721
722 return result;
723}
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition datetime.c:1605
void j2date(int jd, int *year, int *month, int *day)
Definition datetime.c:322
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, fb(), 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 
)
extern

Definition at line 768 of file date.c.

769{
772
773 dt1 = date2timestamp_safe(dateVal, (Node *) &escontext);
774 if (escontext.error_occurred)
775 {
776 Assert(TIMESTAMP_IS_NOEND(dt1)); /* NOBEGIN case cannot occur */
777
778 /* dt1 is larger than any finite timestamp, but less than infinity */
779 return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
780 }
781
783}
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition timestamp.c:2201
#define Assert(condition)
Definition c.h:945
#define TIMESTAMP_IS_NOEND(j)
Definition timestamp.h:167
Timestamp date2timestamp_safe(DateADT dateVal, Node *escontext)
Definition date.c:622
Definition nodes.h:135

References Assert, date2timestamp_safe(), ErrorSaveContext::error_occurred, fb(), 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 
)
extern

Definition at line 849 of file date.c.

850{
853
854 dt1 = date2timestamptz_safe(dateVal, (Node *) &escontext);
855
856 if (escontext.error_occurred)
857 {
859 {
860 /* dt1 is larger than any finite timestamp, but less than infinity */
861 return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
862 }
864 {
865 /* dt1 is less than any finite timestamp, but more than -infinity */
866 return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
867 }
868 }
869
871}
#define TIMESTAMP_IS_NOBEGIN(j)
Definition timestamp.h:162
TimestampTz date2timestamptz_safe(DateADT dateVal, Node *escontext)
Definition date.c:671
#define timestamptz_cmp_internal(dt1, dt2)
Definition timestamp.h:143

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

67{
68 return (TimeADT) DatumGetInt64(X);
69}
int64 TimeADT
Definition date.h:23
static int64 DatumGetInt64(Datum X)
Definition postgres.h:403

References DatumGetInt64(), and fb().

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

◆ DatumGetTimeTzADTP()

static TimeTzADT * DatumGetTimeTzADTP ( Datum  X)
inlinestatic

Definition at line 72 of file date.h.

73{
74 return (TimeTzADT *) DatumGetPointer(X);
75}
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332

References DatumGetPointer(), and fb().

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

◆ EncodeSpecialDate()

void EncodeSpecialDate ( DateADT  dt,
char str 
)
extern

Definition at line 295 of file date.c.

296{
297 if (DATE_IS_NOBEGIN(dt))
298 strcpy(str, EARLY);
299 else if (DATE_IS_NOEND(dt))
300 strcpy(str, LATE);
301 else /* shouldn't happen */
302 elog(ERROR, "invalid argument for EncodeSpecialDate");
303}
#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, fb(), LATE, and str.

Referenced by date_out(), and JsonEncodeDateTime().

◆ float_time_overflows()

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

Definition at line 1539 of file date.c.

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

Definition at line 310 of file date.c.

311{
312 struct pg_tm tm;
313
314 static int cache_year = 0;
315 static int cache_mon = 0;
316 static int cache_mday = 0;
317 static DateADT cache_date;
318
320
321 /*
322 * date2j involves several integer divisions; moreover, unless our session
323 * lives across local midnight, we don't really have to do it more than
324 * once. So it seems worth having a separate cache here.
325 */
326 if (tm.tm_year != cache_year ||
327 tm.tm_mon != cache_mon ||
329 {
334 }
335
336 return cache_date;
337}
void GetCurrentDateTime(struct pg_tm *tm)
Definition datetime.c:377
int date2j(int year, int month, int day)
Definition datetime.c:297

References date2j(), fb(), 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)
extern

Definition at line 343 of file date.c.

344{
345 TimeTzADT *result;
346 struct pg_tm tt,
347 *tm = &tt;
348 fsec_t fsec;
349 int tz;
350
351 GetCurrentTimeUsec(tm, &fsec, &tz);
352
353 result = palloc_object(TimeTzADT);
354 tm2timetz(tm, fsec, tz, result);
355 AdjustTimeForTypmod(&(result->time), typmod);
356 return result;
357}
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition datetime.c:398
int32 fsec_t
Definition timestamp.h:41
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition date.c:2351
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition date.c:1733
#define palloc_object(type)
Definition fe_memutils.h:74
TimeADT time
Definition date.h:27

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

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLLocalTime()

TimeADT GetSQLLocalTime ( int32  typmod)
extern

Definition at line 363 of file date.c.

364{
365 TimeADT result;
366 struct pg_tm tt,
367 *tm = &tt;
368 fsec_t fsec;
369 int tz;
370
371 GetCurrentTimeUsec(tm, &fsec, &tz);
372
373 tm2time(tm, fsec, &result);
374 AdjustTimeForTypmod(&result, typmod);
375 return result;
376}
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition date.c:1504

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

Referenced by ExecEvalSQLValueFunction().

◆ time2tm()

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

Definition at line 1576 of file date.c.

1577{
1578 tm->tm_hour = time / USECS_PER_HOUR;
1579 time -= tm->tm_hour * USECS_PER_HOUR;
1580 tm->tm_min = time / USECS_PER_MINUTE;
1581 time -= tm->tm_min * USECS_PER_MINUTE;
1582 tm->tm_sec = time / USECS_PER_SEC;
1583 time -= tm->tm_sec * USECS_PER_SEC;
1584 *fsec = time;
1585 return 0;
1586}
#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 
)
extern

Definition at line 1515 of file date.c.

1516{
1517 /* Range-check the fields individually. */
1522 return true;
1523
1524 /*
1525 * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1526 * that the total time value doesn't exceed 24:00:00.
1527 */
1528 if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1529 + sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1530 return true;
1531
1532 return false;
1533}

References fb(), HOURS_PER_DAY, MINS_PER_HOUR, SECS_PER_MINUTE, USECS_PER_DAY, and USECS_PER_SEC.

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ TimeADTGetDatum()

◆ timestamp2date_safe()

DateADT timestamp2date_safe ( Timestamp  timestamp,
Node escontext 
)
extern

Definition at line 1350 of file date.c.

1351{
1352 DateADT result;
1353 struct pg_tm tt,
1354 *tm = &tt;
1355 fsec_t fsec;
1356
1358 DATE_NOBEGIN(result);
1359 else if (TIMESTAMP_IS_NOEND(timestamp))
1360 DATE_NOEND(result);
1361 else
1362 {
1363 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
1364 {
1365 if (timestamp < 0)
1366 DATE_NOBEGIN(result);
1367 else
1368 DATE_NOEND(result); /* not actually reachable */
1369
1370 ereturn(escontext, result,
1372 errmsg("timestamp out of range")));
1373 }
1374
1376 }
1377
1378 return result;
1379}
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition timestamp.c:1901
#define DATE_NOEND(j)
Definition date.h:47
#define DATE_NOBEGIN(j)
Definition date.h:45
int64 timestamp

References date2j(), DATE_NOBEGIN, DATE_NOEND, ereturn, errcode(), errmsg, fb(), 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 
)
extern

Definition at line 1424 of file date.c.

1425{
1426 DateADT result;
1427 struct pg_tm tt,
1428 *tm = &tt;
1429 fsec_t fsec;
1430 int tz;
1431
1433 DATE_NOBEGIN(result);
1434 else if (TIMESTAMP_IS_NOEND(timestamp))
1435 DATE_NOEND(result);
1436 else
1437 {
1438 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
1439 {
1440 if (timestamp < 0)
1441 DATE_NOBEGIN(result);
1442 else
1443 DATE_NOEND(result); /* not actually reachable */
1444
1445 ereturn(escontext, result,
1447 errmsg("timestamp out of range")));
1448 }
1449
1451 }
1452
1453 return result;
1454}

References date2j(), DATE_NOBEGIN, DATE_NOEND, ereturn, errcode(), errmsg, fb(), 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 
)
extern

Definition at line 2491 of file date.c.

2492{
2493 TimeOffset trem = time->time;
2494
2500 *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2501
2502 if (tzp != NULL)
2503 *tzp = time->zone;
2504
2505 return 0;
2506}
int64 TimeOffset
Definition timestamp.h:40
int32 zone
Definition date.h:28

References fb(), 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 90 of file date.h.

91{
92 return PointerGetDatum(X);
93}
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342

References fb(), and PointerGetDatum().

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

◆ tm2time()

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

Definition at line 1504 of file date.c.

1505{
1506 *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1507 * USECS_PER_SEC) + fsec;
1508 return 0;
1509}

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 
)
extern

Definition at line 2351 of file date.c.

2352{
2353 result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2354 USECS_PER_SEC) + fsec;
2355 result->zone = tz;
2356
2357 return 0;
2358}

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