PostgreSQL Source Code git master
Loading...
Searching...
No Matches
date.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * date.h
4 * Definitions for the SQL "date" and "time" types.
5 *
6 *
7 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/include/utils/date.h
11 *
12 *-------------------------------------------------------------------------
13 */
14#ifndef DATE_H
15#define DATE_H
16
17#include "datatype/timestamp.h"
18#include "fmgr.h"
19#include "pgtime.h"
20
21typedef int32 DateADT;
22
23typedef int64 TimeADT;
24
25typedef struct
26{
27 TimeADT time; /* all time units other than months and years */
28 int32 zone; /* numeric time zone, in seconds */
29} TimeTzADT;
30
31/*
32 * sizeof(TimeTzADT) will be 16 on most platforms due to alignment padding.
33 * However, timetz's typlen is 12 according to pg_type. In most places
34 * we can get away with using sizeof(TimeTzADT), but where it's important
35 * to match the declared typlen, use TIMETZ_TYPLEN.
36 */
37#define TIMETZ_TYPLEN 12
38
39/*
40 * Infinity and minus infinity must be the max and min values of DateADT.
41 */
42#define DATEVAL_NOBEGIN ((DateADT) PG_INT32_MIN)
43#define DATEVAL_NOEND ((DateADT) PG_INT32_MAX)
44
45#define DATE_NOBEGIN(j) ((j) = DATEVAL_NOBEGIN)
46#define DATE_IS_NOBEGIN(j) ((j) == DATEVAL_NOBEGIN)
47#define DATE_NOEND(j) ((j) = DATEVAL_NOEND)
48#define DATE_IS_NOEND(j) ((j) == DATEVAL_NOEND)
49#define DATE_NOT_FINITE(j) (DATE_IS_NOBEGIN(j) || DATE_IS_NOEND(j))
50
51#define MAX_TIME_PRECISION 6
52
53/*
54 * Functions for fmgr-callable functions.
55 *
56 * For TimeADT, we make use of the same support routines as for int64.
57 * Therefore TimeADT is pass-by-reference if and only if int64 is!
58 */
59static inline DateADT
64
65static inline TimeADT
70
71static inline TimeTzADT *
76
77static inline Datum
82
83static inline Datum
88
89static inline Datum
91{
92 return PointerGetDatum(X);
93}
94
95#define PG_GETARG_DATEADT(n) DatumGetDateADT(PG_GETARG_DATUM(n))
96#define PG_GETARG_TIMEADT(n) DatumGetTimeADT(PG_GETARG_DATUM(n))
97#define PG_GETARG_TIMETZADT_P(n) DatumGetTimeTzADTP(PG_GETARG_DATUM(n))
98
99#define PG_RETURN_DATEADT(x) return DateADTGetDatum(x)
100#define PG_RETURN_TIMEADT(x) return TimeADTGetDatum(x)
101#define PG_RETURN_TIMETZADT_P(x) return TimeTzADTPGetDatum(x)
102
103
104/* date.c */
105extern int32 anytime_typmod_check(bool istz, int32 typmod);
113
114extern void EncodeSpecialDate(DateADT dt, char *str);
115extern DateADT GetSQLCurrentDate(void);
116extern TimeTzADT *GetSQLCurrentTime(int32 typmod);
117extern TimeADT GetSQLLocalTime(int32 typmod);
118extern int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec);
119extern int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp);
120extern int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result);
121extern int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result);
122extern bool time_overflows(int hour, int min, int sec, fsec_t fsec);
123extern bool float_time_overflows(int hour, int min, double sec);
124extern void AdjustTimeForTypmod(TimeADT *time, int32 typmod);
125
126#endif /* DATE_H */
int64_t int64
Definition c.h:543
int32_t int32
Definition c.h:542
int64 Timestamp
Definition timestamp.h:38
int64 TimestampTz
Definition timestamp.h:39
int32 fsec_t
Definition timestamp.h:41
int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2)
Definition date.c:776
Timestamp date2timestamp_safe(DateADT dateVal, Node *escontext)
Definition date.c:630
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition date.c:2499
static Datum DateADTGetDatum(DateADT X)
Definition date.h:78
TimestampTz date2timestamptz_safe(DateADT dateVal, Node *escontext)
Definition date.c:679
bool float_time_overflows(int hour, int min, double sec)
Definition date.c:1547
DateADT timestamp2date_safe(Timestamp timestamp, Node *escontext)
Definition date.c:1358
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition date.c:73
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition date.c:1512
TimeADT GetSQLLocalTime(int32 typmod)
Definition date.c:371
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition date.c:2359
DateADT timestamptz2date_safe(TimestampTz timestamp, Node *escontext)
Definition date.c:1432
static TimeTzADT * DatumGetTimeTzADTP(Datum X)
Definition date.h:72
bool time_overflows(int hour, int min, int sec, fsec_t fsec)
Definition date.c:1523
int32 DateADT
Definition date.h:21
static DateADT DatumGetDateADT(Datum X)
Definition date.h:60
DateADT GetSQLCurrentDate(void)
Definition date.c:318
static TimeADT DatumGetTimeADT(Datum X)
Definition date.h:66
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition date.c:1741
static Datum TimeTzADTPGetDatum(const TimeTzADT *X)
Definition date.h:90
int64 TimeADT
Definition date.h:23
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition date.c:1584
void EncodeSpecialDate(DateADT dt, char *str)
Definition date.c:303
static Datum TimeADTGetDatum(TimeADT X)
Definition date.h:84
int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2)
Definition date.c:857
double date2timestamp_no_overflow(DateADT dateVal)
Definition date.c:753
TimeTzADT * GetSQLCurrentTime(int32 typmod)
Definition date.c:351
const char * str
static struct pg_tm tm
Definition localtime.c:104
int64 timestamp
static Datum Int64GetDatum(int64 X)
Definition postgres.h:423
static int64 DatumGetInt64(Datum X)
Definition postgres.h:413
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
static int32 DatumGetInt32(Datum X)
Definition postgres.h:212
static int fb(int x)
Definition nodes.h:135
TimeADT time
Definition date.h:27
int32 zone
Definition date.h:28
Definition pgtime.h:35