PostgreSQL Source Code  git master
date.h File Reference
#include <math.h>
#include "fmgr.h"
#include "pgtime.h"
#include "datatype/timestamp.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)
 
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)
 

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 abstime_date(), 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 abstime_date(), 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

#define DatumGetDateADT (   X)    ((DateADT) DatumGetInt32(X))

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

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

◆ TimeTzADTPGetDatum

#define TimeTzADTPGetDatum (   X)    PointerGetDatum(X)

Definition at line 59 of file date.h.

Referenced by ExecEvalSQLValueFunction(), and leftmostvalue_timetz().

Typedef Documentation

◆ DateADT

Definition at line 23 of file date.h.

◆ TimeADT

Definition at line 25 of file date.h.

Function Documentation

◆ anytime_typmod_check()

int32 anytime_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 72 of file date.c.

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

Referenced by anytime_typmodin(), and transformSQLValueFunction().

73 {
74  if (typmod < 0)
75  ereport(ERROR,
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:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ date2timestamp_no_overflow()

double date2timestamp_no_overflow ( DateADT  dateVal)

Definition at line 656 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, and USECS_PER_DAY.

Referenced by convert_timevalue_to_scalar().

657 {
658  double result;
659 
660  if (DATE_IS_NOBEGIN(dateVal))
661  result = -DBL_MAX;
662  else if (DATE_IS_NOEND(dateVal))
663  result = DBL_MAX;
664  else
665  {
666  /* date is days since 2000, timestamp is microseconds since same... */
667  result = dateVal * (double) USECS_PER_DAY;
668  }
669 
670  return result;
671 }
#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

◆ EncodeSpecialDate()

void EncodeSpecialDate ( DateADT  dt,
char *  str 
)

Definition at line 297 of file date.c.

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

Referenced by date_out(), and JsonEncodeDateTime().

298 {
299  if (DATE_IS_NOBEGIN(dt))
300  strcpy(str, EARLY);
301  else if (DATE_IS_NOEND(dt))
302  strcpy(str, LATE);
303  else /* shouldn't happen */
304  elog(ERROR, "invalid argument for EncodeSpecialDate");
305 }
#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
Definition: elog.h:219
#define EARLY
Definition: datetime.h:40

◆ GetSQLCurrentDate()

DateADT GetSQLCurrentDate ( void  )

Definition at line 312 of file date.c.

References date2j(), ereport, errcode(), errmsg(), ERROR, GetCurrentTransactionStartTimestamp(), POSTGRES_EPOCH_JDATE, timestamp2tm(), tm, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

Referenced by ExecEvalSQLValueFunction().

313 {
314  TimestampTz ts;
315  struct pg_tm tt,
316  *tm = &tt;
317  fsec_t fsec;
318  int tz;
319 
321 
322  if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
323  ereport(ERROR,
324  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
325  errmsg("timestamp out of range")));
326 
327  return date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
328 }
int64 TimestampTz
Definition: timestamp.h:39
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:107
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:122
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:700
int date2j(int y, int m, int d)
Definition: datetime.c:276
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ GetSQLCurrentTime()

TimeTzADT* GetSQLCurrentTime ( int32  typmod)

Definition at line 334 of file date.c.

References AdjustTimeForTypmod(), ereport, errcode(), errmsg(), ERROR, GetCurrentTransactionStartTimestamp(), palloc(), TimeTzADT::time, timestamp2tm(), tm, and tm2timetz().

Referenced by ExecEvalSQLValueFunction().

335 {
336  TimeTzADT *result;
337  TimestampTz ts;
338  struct pg_tm tt,
339  *tm = &tt;
340  fsec_t fsec;
341  int tz;
342 
344 
345  if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
346  ereport(ERROR,
347  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
348  errmsg("timestamp out of range")));
349 
350  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
351  tm2timetz(tm, fsec, tz, result);
352  AdjustTimeForTypmod(&(result->time), typmod);
353  return result;
354 }
TimeADT time
Definition: date.h:29
int64 TimestampTz
Definition: timestamp.h:39
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:107
#define ERROR
Definition: elog.h:43
int32 fsec_t
Definition: timestamp.h:41
static void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1430
#define ereport(elevel, rest)
Definition: elog.h:122
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:700
static int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2008
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: date.h:27

◆ GetSQLLocalTime()

TimeADT GetSQLLocalTime ( int32  typmod)

Definition at line 360 of file date.c.

References AdjustTimeForTypmod(), ereport, errcode(), errmsg(), ERROR, GetCurrentTransactionStartTimestamp(), timestamp2tm(), tm, and tm2time().

Referenced by ExecEvalSQLValueFunction().

361 {
362  TimeADT result;
363  TimestampTz ts;
364  struct pg_tm tt,
365  *tm = &tt;
366  fsec_t fsec;
367  int tz;
368 
370 
371  if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
372  ereport(ERROR,
373  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
374  errmsg("timestamp out of range")));
375 
376  tm2time(tm, fsec, &result);
377  AdjustTimeForTypmod(&result, typmod);
378  return result;
379 }
int64 TimestampTz
Definition: timestamp.h:39
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758
static int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1264
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:107
#define ERROR
Definition: elog.h:43
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
static void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1430
#define ereport(elevel, rest)
Definition: elog.h:122
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:700
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ time2tm()

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

Definition at line 1279 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().

1280 {
1281  tm->tm_hour = time / USECS_PER_HOUR;
1282  time -= tm->tm_hour * USECS_PER_HOUR;
1283  tm->tm_min = time / USECS_PER_MINUTE;
1284  time -= tm->tm_min * USECS_PER_MINUTE;
1285  tm->tm_sec = time / USECS_PER_SEC;
1286  time -= tm->tm_sec * USECS_PER_SEC;
1287  *fsec = time;
1288  return 0;
1289 }
#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

◆ timetz2tm()

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

Definition at line 2142 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().

2143 {
2144  TimeOffset trem = time->time;
2145 
2146  tm->tm_hour = trem / USECS_PER_HOUR;
2147  trem -= tm->tm_hour * USECS_PER_HOUR;
2148  tm->tm_min = trem / USECS_PER_MINUTE;
2149  trem -= tm->tm_min * USECS_PER_MINUTE;
2150  tm->tm_sec = trem / USECS_PER_SEC;
2151  *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2152 
2153  if (tzp != NULL)
2154  *tzp = time->zone;
2155 
2156  return 0;
2157 }
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