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 DatumGetDateADT(X)   ((DateADT) DatumGetInt32(X))
 
#define DatumGetTimeADT(X)   ((TimeADT) DatumGetInt64(X))
 
#define DatumGetTimeTzADTP(X)   ((TimeTzADT *) DatumGetPointer(X))
 
#define DateADTGetDatum(X)   Int32GetDatum(X)
 
#define TimeADTGetDatum(X)   Int64GetDatum(X)
 
#define TimeTzADTPGetDatum(X)   PointerGetDatum(X)
 
#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

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

◆ DATE_IS_NOEND

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

◆ DATE_NOBEGIN

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

Definition at line 39 of file date.h.

Referenced by date_in(), timestamp_date(), and timestamptz_date().

◆ DATE_NOEND

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

Definition at line 41 of file date.h.

Referenced by date_in(), timestamp_date(), and timestamptz_date().

◆ DATE_NOT_FINITE

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

◆ DateADTGetDatum

◆ DATEVAL_NOBEGIN

#define DATEVAL_NOBEGIN   ((DateADT) PG_INT32_MIN)

Definition at line 36 of file date.h.

Referenced by leftmostvalue_date().

◆ DATEVAL_NOEND

#define DATEVAL_NOEND   ((DateADT) PG_INT32_MAX)

Definition at line 37 of file date.h.

◆ DatumGetDateADT

◆ DatumGetTimeADT

#define DatumGetTimeADT (   X)    ((TimeADT) DatumGetInt64(X))

Definition at line 54 of file date.h.

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

◆ DatumGetTimeTzADTP

#define DatumGetTimeTzADTP (   X)    ((TimeTzADT *) DatumGetPointer(X))

Definition at line 55 of file date.h.

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

◆ MAX_TIME_PRECISION

#define MAX_TIME_PRECISION   6

Definition at line 51 of file date.h.

Referenced by AdjustTimeForTypmod(), anytime_typmod_check(), EncodeTimeOnly(), and time_support().

◆ PG_GETARG_DATEADT

◆ PG_GETARG_TIMEADT

◆ PG_GETARG_TIMETZADT_P

◆ PG_RETURN_DATEADT

#define PG_RETURN_DATEADT (   x)    return DateADTGetDatum(x)

◆ PG_RETURN_TIMEADT

◆ PG_RETURN_TIMETZADT_P

◆ TimeADTGetDatum

#define TimeADTGetDatum (   X)    Int64GetDatum(X)

Definition at line 58 of file date.h.

Referenced by ExecEvalSQLValueFunction(), gbt_num_fetch(), leftmostvalue_time(), and parse_datetime().

◆ TimeTzADTPGetDatum

#define TimeTzADTPGetDatum (   X)    PointerGetDatum(X)

Definition at line 59 of file date.h.

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

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

References MAX_TIME_PRECISION.

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

1470 {
1471  static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1472  INT64CONST(1000000),
1473  INT64CONST(100000),
1474  INT64CONST(10000),
1475  INT64CONST(1000),
1476  INT64CONST(100),
1477  INT64CONST(10),
1478  INT64CONST(1)
1479  };
1480 
1481  static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1482  INT64CONST(500000),
1483  INT64CONST(50000),
1484  INT64CONST(5000),
1485  INT64CONST(500),
1486  INT64CONST(50),
1487  INT64CONST(5),
1488  INT64CONST(0)
1489  };
1490 
1491  if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1492  {
1493  if (*time >= INT64CONST(0))
1494  *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1495  TimeScales[typmod];
1496  else
1497  *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1498  TimeScales[typmod]);
1499  }
1500 }
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ anytime_typmod_check()

int32 anytime_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 68 of file date.c.

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

Referenced by anytime_typmodin(), and transformSQLValueFunction().

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

◆ date2timestamp_no_overflow()

double date2timestamp_no_overflow ( DateADT  dateVal)

Definition at line 724 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, and USECS_PER_DAY.

Referenced by convert_timevalue_to_scalar().

725 {
726  double result;
727 
728  if (DATE_IS_NOBEGIN(dateVal))
729  result = -DBL_MAX;
730  else if (DATE_IS_NOEND(dateVal))
731  result = DBL_MAX;
732  else
733  {
734  /* date is days since 2000, timestamp is microseconds since same... */
735  result = dateVal * (double) USECS_PER_DAY;
736  }
737 
738  return result;
739 }
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define USECS_PER_DAY
Definition: timestamp.h:91
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40

◆ date2timestamp_opt_overflow()

Timestamp date2timestamp_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 568 of file date.c.

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

Referenced by date2timestamp(), and date_cmp_timestamp_internal().

569 {
570  Timestamp result;
571 
572  if (overflow)
573  *overflow = 0;
574 
575  if (DATE_IS_NOBEGIN(dateVal))
576  TIMESTAMP_NOBEGIN(result);
577  else if (DATE_IS_NOEND(dateVal))
578  TIMESTAMP_NOEND(result);
579  else
580  {
581  /*
582  * Since dates have the same minimum values as timestamps, only upper
583  * boundary need be checked for overflow.
584  */
585  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
586  {
587  if (overflow)
588  {
589  *overflow = 1;
590  TIMESTAMP_NOEND(result);
591  return result;
592  }
593  else
594  {
595  ereport(ERROR,
596  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
597  errmsg("date out of range for timestamp")));
598  }
599  }
600 
601  /* date is days since 2000, timestamp is microseconds since same... */
602  result = dateVal * USECS_PER_DAY;
603  }
604 
605  return result;
606 }
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define USECS_PER_DAY
Definition: timestamp.h:91
int64 Timestamp
Definition: timestamp.h:38
#define ereport(elevel,...)
Definition: elog.h:144
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ date2timestamptz_opt_overflow()

TimestampTz date2timestamptz_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 628 of file date.c.

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 date2timestamptz(), and date_cmp_timestamptz_internal().

629 {
630  TimestampTz result;
631  struct pg_tm tt,
632  *tm = &tt;
633  int tz;
634 
635  if (overflow)
636  *overflow = 0;
637 
638  if (DATE_IS_NOBEGIN(dateVal))
639  TIMESTAMP_NOBEGIN(result);
640  else if (DATE_IS_NOEND(dateVal))
641  TIMESTAMP_NOEND(result);
642  else
643  {
644  /*
645  * Since dates have the same minimum values as timestamps, only upper
646  * boundary need be checked for overflow.
647  */
648  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
649  {
650  if (overflow)
651  {
652  *overflow = 1;
653  TIMESTAMP_NOEND(result);
654  return result;
655  }
656  else
657  {
658  ereport(ERROR,
659  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
660  errmsg("date out of range for timestamp")));
661  }
662  }
663 
664  j2date(dateVal + POSTGRES_EPOCH_JDATE,
665  &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
666  tm->tm_hour = 0;
667  tm->tm_min = 0;
668  tm->tm_sec = 0;
670 
671  result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
672 
673  /*
674  * Since it is possible to go beyond allowed timestamptz range because
675  * of time zone, check for allowed timestamp range after adding tz.
676  */
677  if (!IS_VALID_TIMESTAMP(result))
678  {
679  if (overflow)
680  {
681  if (result < MIN_TIMESTAMP)
682  {
683  *overflow = -1;
684  TIMESTAMP_NOBEGIN(result);
685  }
686  else
687  {
688  *overflow = 1;
689  TIMESTAMP_NOEND(result);
690  }
691  }
692  else
693  {
694  ereport(ERROR,
695  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
696  errmsg("date out of range for timestamp")));
697  }
698  }
699  }
700 
701  return result;
702 }
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
int tm_hour
Definition: pgtime.h:29
int errcode(int sqlerrcode)
Definition: elog.c:610
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1478
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define MIN_TIMESTAMP
Definition: timestamp.h:184
#define USECS_PER_DAY
Definition: timestamp.h:91
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
#define ereport(elevel,...)
Definition: elog.h:144
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ date_cmp_timestamp_internal()

int32 date_cmp_timestamp_internal ( DateADT  dateVal,
Timestamp  dt2 
)

Definition at line 747 of file date.c.

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

748 {
749  Timestamp dt1;
750  int overflow;
751 
752  dt1 = date2timestamp_opt_overflow(dateVal, &overflow);
753  if (overflow > 0)
754  {
755  /* dt1 is larger than any finite timestamp, but less than infinity */
756  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
757  }
758  Assert(overflow == 0); /* -1 case cannot occur */
759 
760  return timestamp_cmp_internal(dt1, dt2);
761 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2056
Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:568
int64 Timestamp
Definition: timestamp.h:38
#define Assert(condition)
Definition: c.h:746
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120

◆ date_cmp_timestamptz_internal()

int32 date_cmp_timestamptz_internal ( DateADT  dateVal,
TimestampTz  dt2 
)

Definition at line 827 of file date.c.

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

828 {
829  TimestampTz dt1;
830  int overflow;
831 
832  dt1 = date2timestamptz_opt_overflow(dateVal, &overflow);
833  if (overflow > 0)
834  {
835  /* dt1 is larger than any finite timestamp, but less than infinity */
836  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
837  }
838  if (overflow < 0)
839  {
840  /* dt1 is less than any finite timestamp, but more than -infinity */
841  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
842  }
843 
844  return timestamptz_cmp_internal(dt1, dt2);
845 }
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:628
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:98
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115

◆ EncodeSpecialDate()

void EncodeSpecialDate ( DateADT  dt,
char *  str 
)

Definition at line 285 of file date.c.

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

Referenced by date_out(), and JsonEncodeDateTime().

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

◆ float_time_overflows()

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

Definition at line 1273 of file date.c.

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

1274 {
1275  /* Range-check the fields individually. */
1276  if (hour < 0 || hour > HOURS_PER_DAY ||
1277  min < 0 || min >= MINS_PER_HOUR)
1278  return true;
1279 
1280  /*
1281  * "sec", being double, requires extra care. Cope with NaN, and round off
1282  * before applying the range check to avoid unexpected errors due to
1283  * imprecise input. (We assume rint() behaves sanely with infinities.)
1284  */
1285  if (isnan(sec))
1286  return true;
1287  sec = rint(sec * USECS_PER_SEC);
1288  if (sec < 0 || sec > SECS_PER_MINUTE * USECS_PER_SEC)
1289  return true;
1290 
1291  /*
1292  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1293  * that the total time value doesn't exceed 24:00:00. This must match the
1294  * way that callers will convert the fields to a time.
1295  */
1296  if (((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1297  * USECS_PER_SEC) + (int64) sec) > USECS_PER_DAY)
1298  return true;
1299 
1300  return false;
1301 }
#define USECS_PER_SEC
Definition: timestamp.h:94
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define HOURS_PER_DAY
Definition: timestamp.h:78
#define SECS_PER_MINUTE
Definition: timestamp.h:88
#define USECS_PER_DAY
Definition: timestamp.h:91

◆ GetSQLCurrentDate()

DateADT GetSQLCurrentDate ( void  )

Definition at line 300 of file date.c.

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

Referenced by ExecEvalSQLValueFunction().

301 {
302  struct pg_tm tm;
303 
304  static int cache_year = 0;
305  static int cache_mon = 0;
306  static int cache_mday = 0;
307  static DateADT cache_date;
308 
310 
311  /*
312  * date2j involves several integer divisions; moreover, unless our session
313  * lives across local midnight, we don't really have to do it more than
314  * once. So it seems worth having a separate cache here.
315  */
316  if (tm.tm_year != cache_year ||
317  tm.tm_mon != cache_mon ||
318  tm.tm_mday != cache_mday)
319  {
321  cache_year = tm.tm_year;
322  cache_mon = tm.tm_mon;
323  cache_mday = tm.tm_mday;
324  }
325 
326  return cache_date;
327 }
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:349
int32 DateADT
Definition: date.h:23
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int date2j(int y, int m, int d)
Definition: datetime.c:269
int tm_year
Definition: pgtime.h:32
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ GetSQLCurrentTime()

TimeTzADT* GetSQLCurrentTime ( int32  typmod)

Definition at line 333 of file date.c.

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

Referenced by ExecEvalSQLValueFunction().

334 {
335  TimeTzADT *result;
336  struct pg_tm tt,
337  *tm = &tt;
338  fsec_t fsec;
339  int tz;
340 
341  GetCurrentTimeUsec(tm, &fsec, &tz);
342 
343  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
344  tm2timetz(tm, fsec, tz, result);
345  AdjustTimeForTypmod(&(result->time), typmod);
346  return result;
347 }
TimeADT time
Definition: date.h:29
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2047
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:370
int32 fsec_t
Definition: timestamp.h:41
void * palloc(Size size)
Definition: mcxt.c:950
Definition: date.h:27
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1469

◆ GetSQLLocalTime()

TimeADT GetSQLLocalTime ( int32  typmod)

Definition at line 353 of file date.c.

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

Referenced by ExecEvalSQLValueFunction().

354 {
355  TimeADT result;
356  struct pg_tm tt,
357  *tm = &tt;
358  fsec_t fsec;
359  int tz;
360 
361  GetCurrentTimeUsec(tm, &fsec, &tz);
362 
363  tm2time(tm, fsec, &result);
364  AdjustTimeForTypmod(&result, typmod);
365  return result;
366 }
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:370
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1238
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1469

◆ time2tm()

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

Definition at line 1312 of file date.c.

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

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

1313 {
1314  tm->tm_hour = time / USECS_PER_HOUR;
1315  time -= tm->tm_hour * USECS_PER_HOUR;
1316  tm->tm_min = time / USECS_PER_MINUTE;
1317  time -= tm->tm_min * USECS_PER_MINUTE;
1318  tm->tm_sec = time / USECS_PER_SEC;
1319  time -= tm->tm_sec * USECS_PER_SEC;
1320  *fsec = time;
1321  return 0;
1322 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define USECS_PER_MINUTE
Definition: timestamp.h:93
#define USECS_PER_HOUR
Definition: timestamp.h:92
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ time_overflows()

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

Definition at line 1249 of file date.c.

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

Referenced by DecodeDateTime(), and DecodeTimeOnly().

1250 {
1251  /* Range-check the fields individually. */
1252  if (hour < 0 || hour > HOURS_PER_DAY ||
1253  min < 0 || min >= MINS_PER_HOUR ||
1254  sec < 0 || sec > SECS_PER_MINUTE ||
1255  fsec < 0 || fsec > USECS_PER_SEC)
1256  return true;
1257 
1258  /*
1259  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1260  * that the total time value doesn't exceed 24:00:00.
1261  */
1262  if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1263  + sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1264  return true;
1265 
1266  return false;
1267 }
#define USECS_PER_SEC
Definition: timestamp.h:94
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define HOURS_PER_DAY
Definition: timestamp.h:78
#define SECS_PER_MINUTE
Definition: timestamp.h:88
#define USECS_PER_DAY
Definition: timestamp.h:91

◆ timetz2tm()

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

Definition at line 2181 of file date.c.

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

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

2182 {
2183  TimeOffset trem = time->time;
2184 
2185  tm->tm_hour = trem / USECS_PER_HOUR;
2186  trem -= tm->tm_hour * USECS_PER_HOUR;
2187  tm->tm_min = trem / USECS_PER_MINUTE;
2188  trem -= tm->tm_min * USECS_PER_MINUTE;
2189  tm->tm_sec = trem / USECS_PER_SEC;
2190  *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2191 
2192  if (tzp != NULL)
2193  *tzp = time->zone;
2194 
2195  return 0;
2196 }
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define USECS_PER_MINUTE
Definition: timestamp.h:93
int32 zone
Definition: date.h:30
#define USECS_PER_HOUR
Definition: timestamp.h:92
int64 TimeOffset
Definition: timestamp.h:40
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ tm2time()

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

Definition at line 1238 of file date.c.

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

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

1239 {
1240  *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1241  * USECS_PER_SEC) + fsec;
1242  return 0;
1243 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ tm2timetz()

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

Definition at line 2047 of file date.c.

References MINS_PER_HOUR, SECS_PER_MINUTE, TimeTzADT::time, 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().

2048 {
2049  result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2050  USECS_PER_SEC) + fsec;
2051  result->zone = tz;
2052 
2053  return 0;
2054 }
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define MINS_PER_HOUR
Definition: timestamp.h:89
int32 zone
Definition: date.h:30
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28