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

#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_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(), 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

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

◆ 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:570
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ date2timestamp_no_overflow()

double date2timestamp_no_overflow ( DateADT  dateVal)

Definition at line 648 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, and USECS_PER_DAY.

Referenced by convert_timevalue_to_scalar().

649 {
650  double result;
651 
652  if (DATE_IS_NOBEGIN(dateVal))
653  result = -DBL_MAX;
654  else if (DATE_IS_NOEND(dateVal))
655  result = DBL_MAX;
656  else
657  {
658  /* date is days since 2000, timestamp is microseconds since same... */
659  result = dateVal * (double) USECS_PER_DAY;
660  }
661 
662  return result;
663 }
#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 289 of file date.c.

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

Referenced by date_out(), and JsonEncodeDateTime().

290 {
291  if (DATE_IS_NOBEGIN(dt))
292  strcpy(str, EARLY);
293  else if (DATE_IS_NOEND(dt))
294  strcpy(str, LATE);
295  else /* shouldn't happen */
296  elog(ERROR, "invalid argument for EncodeSpecialDate");
297 }
#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:226
#define EARLY
Definition: datetime.h:40

◆ GetSQLCurrentDate()

DateADT GetSQLCurrentDate ( void  )

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

305 {
306  TimestampTz ts;
307  struct pg_tm tt,
308  *tm = &tt;
309  fsec_t fsec;
310  int tz;
311 
313 
314  if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
315  ereport(ERROR,
316  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
317  errmsg("timestamp out of range")));
318 
319  return date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
320 }
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:1776
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#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:141
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:783
int date2j(int y, int m, int d)
Definition: datetime.c:270
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ GetSQLCurrentTime()

TimeTzADT* GetSQLCurrentTime ( int32  typmod)

Definition at line 326 of file date.c.

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

Referenced by ExecEvalSQLValueFunction().

327 {
328  TimeTzADT *result;
329  TimestampTz ts;
330  struct pg_tm tt,
331  *tm = &tt;
332  fsec_t fsec;
333  int tz;
334 
336 
337  if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
338  ereport(ERROR,
339  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
340  errmsg("timestamp out of range")));
341 
342  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
343  tm2timetz(tm, fsec, tz, result);
344  AdjustTimeForTypmod(&(result->time), typmod);
345  return result;
346 }
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:1776
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define ERROR
Definition: elog.h:43
int32 fsec_t
Definition: timestamp.h:41
static void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1383
#define ereport(elevel, rest)
Definition: elog.h:141
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:783
static int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:1961
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: date.h:27

◆ GetSQLLocalTime()

TimeADT GetSQLLocalTime ( int32  typmod)

Definition at line 352 of file date.c.

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

Referenced by ExecEvalSQLValueFunction().

353 {
354  TimeADT result;
355  TimestampTz ts;
356  struct pg_tm tt,
357  *tm = &tt;
358  fsec_t fsec;
359  int tz;
360 
362 
363  if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
364  ereport(ERROR,
365  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
366  errmsg("timestamp out of range")));
367 
368  tm2time(tm, fsec, &result);
369  AdjustTimeForTypmod(&result, typmod);
370  return result;
371 }
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:1776
static int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1207
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#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:1383
#define ereport(elevel, rest)
Definition: elog.h:141
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:783
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ time2tm()

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

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

1223 {
1224  tm->tm_hour = time / USECS_PER_HOUR;
1225  time -= tm->tm_hour * USECS_PER_HOUR;
1226  tm->tm_min = time / USECS_PER_MINUTE;
1227  time -= tm->tm_min * USECS_PER_MINUTE;
1228  tm->tm_sec = time / USECS_PER_SEC;
1229  time -= tm->tm_sec * USECS_PER_SEC;
1230  *fsec = time;
1231  return 0;
1232 }
#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 2095 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().

2096 {
2097  TimeOffset trem = time->time;
2098 
2099  tm->tm_hour = trem / USECS_PER_HOUR;
2100  trem -= tm->tm_hour * USECS_PER_HOUR;
2101  tm->tm_min = trem / USECS_PER_MINUTE;
2102  trem -= tm->tm_min * USECS_PER_MINUTE;
2103  tm->tm_sec = trem / USECS_PER_SEC;
2104  *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2105 
2106  if (tzp != NULL)
2107  *tzp = time->zone;
2108 
2109  return 0;
2110 }
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