Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * date.h
4 : * Definitions for the SQL "date" and "time" types.
5 : *
6 : *
7 : * Portions Copyright (c) 1996-2025, 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 <math.h>
18 :
19 : #include "datatype/timestamp.h"
20 : #include "fmgr.h"
21 : #include "pgtime.h"
22 :
23 : typedef int32 DateADT;
24 :
25 : typedef int64 TimeADT;
26 :
27 : typedef struct
28 : {
29 : TimeADT time; /* all time units other than months and years */
30 : int32 zone; /* numeric time zone, in seconds */
31 : } TimeTzADT;
32 :
33 : /*
34 : * sizeof(TimeTzADT) will be 16 on most platforms due to alignment padding.
35 : * However, timetz's typlen is 12 according to pg_type. In most places
36 : * we can get away with using sizeof(TimeTzADT), but where it's important
37 : * to match the declared typlen, use TIMETZ_TYPLEN.
38 : */
39 : #define TIMETZ_TYPLEN 12
40 :
41 : /*
42 : * Infinity and minus infinity must be the max and min values of DateADT.
43 : */
44 : #define DATEVAL_NOBEGIN ((DateADT) PG_INT32_MIN)
45 : #define DATEVAL_NOEND ((DateADT) PG_INT32_MAX)
46 :
47 : #define DATE_NOBEGIN(j) ((j) = DATEVAL_NOBEGIN)
48 : #define DATE_IS_NOBEGIN(j) ((j) == DATEVAL_NOBEGIN)
49 : #define DATE_NOEND(j) ((j) = DATEVAL_NOEND)
50 : #define DATE_IS_NOEND(j) ((j) == DATEVAL_NOEND)
51 : #define DATE_NOT_FINITE(j) (DATE_IS_NOBEGIN(j) || DATE_IS_NOEND(j))
52 :
53 : #define MAX_TIME_PRECISION 6
54 :
55 : /*
56 : * Functions for fmgr-callable functions.
57 : *
58 : * For TimeADT, we make use of the same support routines as for int64.
59 : * Therefore TimeADT is pass-by-reference if and only if int64 is!
60 : */
61 : static inline DateADT
1176 peter@eisentraut.org 62 CBC 327138 : DatumGetDateADT(Datum X)
63 : {
64 327138 : return (DateADT) DatumGetInt32(X);
65 : }
66 :
67 : static inline TimeADT
68 214107 : DatumGetTimeADT(Datum X)
69 : {
70 214107 : return (TimeADT) DatumGetInt64(X);
71 : }
72 :
73 : static inline TimeTzADT *
74 227510 : DatumGetTimeTzADTP(Datum X)
75 : {
76 227510 : return (TimeTzADT *) DatumGetPointer(X);
77 : }
78 :
79 : static inline Datum
80 35177 : DateADTGetDatum(DateADT X)
81 : {
82 35177 : return Int32GetDatum(X);
83 : }
84 :
85 : static inline Datum
86 52663 : TimeADTGetDatum(TimeADT X)
87 : {
88 52663 : return Int64GetDatum(X);
89 : }
90 :
91 : static inline Datum
92 4318 : TimeTzADTPGetDatum(const TimeTzADT *X)
93 : {
94 4318 : return PointerGetDatum(X);
95 : }
96 :
97 : #define PG_GETARG_DATEADT(n) DatumGetDateADT(PG_GETARG_DATUM(n))
98 : #define PG_GETARG_TIMEADT(n) DatumGetTimeADT(PG_GETARG_DATUM(n))
99 : #define PG_GETARG_TIMETZADT_P(n) DatumGetTimeTzADTP(PG_GETARG_DATUM(n))
100 :
101 : #define PG_RETURN_DATEADT(x) return DateADTGetDatum(x)
102 : #define PG_RETURN_TIMEADT(x) return TimeADTGetDatum(x)
103 : #define PG_RETURN_TIMETZADT_P(x) return TimeTzADTPGetDatum(x)
104 :
105 :
106 : /* date.c */
107 : extern int32 anytime_typmod_check(bool istz, int32 typmod);
108 : extern double date2timestamp_no_overflow(DateADT dateVal);
109 : extern Timestamp date2timestamp_safe(DateADT dateVal, Node *escontext);
110 : extern TimestampTz date2timestamptz_safe(DateADT dateVal, Node *escontext);
111 : extern DateADT timestamp2date_safe(Timestamp timestamp, Node *escontext);
112 : extern DateADT timestamptz2date_safe(TimestampTz timestamp, Node *escontext);
113 : extern int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2);
114 : extern int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2);
115 :
116 : extern void EncodeSpecialDate(DateADT dt, char *str);
117 : extern DateADT GetSQLCurrentDate(void);
118 : extern TimeTzADT *GetSQLCurrentTime(int32 typmod);
119 : extern TimeADT GetSQLLocalTime(int32 typmod);
120 : extern int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec);
121 : extern int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp);
122 : extern int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result);
123 : extern int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result);
124 : extern bool time_overflows(int hour, int min, int sec, fsec_t fsec);
125 : extern bool float_time_overflows(int hour, int min, double sec);
126 : extern void AdjustTimeForTypmod(TimeADT *time, int32 typmod);
127 :
128 : #endif /* DATE_H */
|