/src/postgres/src/include/utils/date.h
Line | Count | Source |
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 | | * Infinity and minus infinity must be the max and min values of DateADT. |
35 | | */ |
36 | 0 | #define DATEVAL_NOBEGIN ((DateADT) PG_INT32_MIN) |
37 | 0 | #define DATEVAL_NOEND ((DateADT) PG_INT32_MAX) |
38 | | |
39 | 0 | #define DATE_NOBEGIN(j) ((j) = DATEVAL_NOBEGIN) |
40 | 0 | #define DATE_IS_NOBEGIN(j) ((j) == DATEVAL_NOBEGIN) |
41 | 0 | #define DATE_NOEND(j) ((j) = DATEVAL_NOEND) |
42 | 0 | #define DATE_IS_NOEND(j) ((j) == DATEVAL_NOEND) |
43 | 0 | #define DATE_NOT_FINITE(j) (DATE_IS_NOBEGIN(j) || DATE_IS_NOEND(j)) |
44 | | |
45 | 0 | #define MAX_TIME_PRECISION 6 |
46 | | |
47 | | /* |
48 | | * Functions for fmgr-callable functions. |
49 | | * |
50 | | * For TimeADT, we make use of the same support routines as for int64. |
51 | | * Therefore TimeADT is pass-by-reference if and only if int64 is! |
52 | | */ |
53 | | static inline DateADT |
54 | | DatumGetDateADT(Datum X) |
55 | 0 | { |
56 | 0 | return (DateADT) DatumGetInt32(X); |
57 | 0 | } Unexecuted instantiation: brin_minmax_multi.c:DatumGetDateADT Unexecuted instantiation: parse_expr.c:DatumGetDateADT Unexecuted instantiation: execExprInterp.c:DatumGetDateADT Unexecuted instantiation: date.c:DatumGetDateADT Unexecuted instantiation: datetime.c:DatumGetDateADT Unexecuted instantiation: formatting.c:DatumGetDateADT Unexecuted instantiation: json.c:DatumGetDateADT Unexecuted instantiation: jsonpath_exec.c:DatumGetDateADT Unexecuted instantiation: pseudorandomfuncs.c:DatumGetDateADT Unexecuted instantiation: rangetypes.c:DatumGetDateADT Unexecuted instantiation: selfuncs.c:DatumGetDateADT Unexecuted instantiation: timestamp.c:DatumGetDateADT Unexecuted instantiation: xml.c:DatumGetDateADT |
58 | | |
59 | | static inline TimeADT |
60 | | DatumGetTimeADT(Datum X) |
61 | 0 | { |
62 | 0 | return (TimeADT) DatumGetInt64(X); |
63 | 0 | } Unexecuted instantiation: brin_minmax_multi.c:DatumGetTimeADT Unexecuted instantiation: parse_expr.c:DatumGetTimeADT Unexecuted instantiation: execExprInterp.c:DatumGetTimeADT Unexecuted instantiation: date.c:DatumGetTimeADT Unexecuted instantiation: datetime.c:DatumGetTimeADT Unexecuted instantiation: formatting.c:DatumGetTimeADT Unexecuted instantiation: json.c:DatumGetTimeADT Unexecuted instantiation: jsonpath_exec.c:DatumGetTimeADT Unexecuted instantiation: pseudorandomfuncs.c:DatumGetTimeADT Unexecuted instantiation: rangetypes.c:DatumGetTimeADT Unexecuted instantiation: selfuncs.c:DatumGetTimeADT Unexecuted instantiation: timestamp.c:DatumGetTimeADT Unexecuted instantiation: xml.c:DatumGetTimeADT |
64 | | |
65 | | static inline TimeTzADT * |
66 | | DatumGetTimeTzADTP(Datum X) |
67 | 0 | { |
68 | 0 | return (TimeTzADT *) DatumGetPointer(X); |
69 | 0 | } Unexecuted instantiation: brin_minmax_multi.c:DatumGetTimeTzADTP Unexecuted instantiation: parse_expr.c:DatumGetTimeTzADTP Unexecuted instantiation: execExprInterp.c:DatumGetTimeTzADTP Unexecuted instantiation: date.c:DatumGetTimeTzADTP Unexecuted instantiation: datetime.c:DatumGetTimeTzADTP Unexecuted instantiation: formatting.c:DatumGetTimeTzADTP Unexecuted instantiation: json.c:DatumGetTimeTzADTP Unexecuted instantiation: jsonpath_exec.c:DatumGetTimeTzADTP Unexecuted instantiation: pseudorandomfuncs.c:DatumGetTimeTzADTP Unexecuted instantiation: rangetypes.c:DatumGetTimeTzADTP Unexecuted instantiation: selfuncs.c:DatumGetTimeTzADTP Unexecuted instantiation: timestamp.c:DatumGetTimeTzADTP Unexecuted instantiation: xml.c:DatumGetTimeTzADTP |
70 | | |
71 | | static inline Datum |
72 | | DateADTGetDatum(DateADT X) |
73 | 0 | { |
74 | 0 | return Int32GetDatum(X); |
75 | 0 | } Unexecuted instantiation: brin_minmax_multi.c:DateADTGetDatum Unexecuted instantiation: parse_expr.c:DateADTGetDatum Unexecuted instantiation: execExprInterp.c:DateADTGetDatum Unexecuted instantiation: date.c:DateADTGetDatum Unexecuted instantiation: datetime.c:DateADTGetDatum Unexecuted instantiation: formatting.c:DateADTGetDatum Unexecuted instantiation: json.c:DateADTGetDatum Unexecuted instantiation: jsonpath_exec.c:DateADTGetDatum Unexecuted instantiation: pseudorandomfuncs.c:DateADTGetDatum Unexecuted instantiation: rangetypes.c:DateADTGetDatum Unexecuted instantiation: selfuncs.c:DateADTGetDatum Unexecuted instantiation: timestamp.c:DateADTGetDatum Unexecuted instantiation: xml.c:DateADTGetDatum |
76 | | |
77 | | static inline Datum |
78 | | TimeADTGetDatum(TimeADT X) |
79 | 0 | { |
80 | 0 | return Int64GetDatum(X); |
81 | 0 | } Unexecuted instantiation: brin_minmax_multi.c:TimeADTGetDatum Unexecuted instantiation: parse_expr.c:TimeADTGetDatum Unexecuted instantiation: execExprInterp.c:TimeADTGetDatum Unexecuted instantiation: date.c:TimeADTGetDatum Unexecuted instantiation: datetime.c:TimeADTGetDatum Unexecuted instantiation: formatting.c:TimeADTGetDatum Unexecuted instantiation: json.c:TimeADTGetDatum Unexecuted instantiation: jsonpath_exec.c:TimeADTGetDatum Unexecuted instantiation: pseudorandomfuncs.c:TimeADTGetDatum Unexecuted instantiation: rangetypes.c:TimeADTGetDatum Unexecuted instantiation: selfuncs.c:TimeADTGetDatum Unexecuted instantiation: timestamp.c:TimeADTGetDatum Unexecuted instantiation: xml.c:TimeADTGetDatum |
82 | | |
83 | | static inline Datum |
84 | | TimeTzADTPGetDatum(const TimeTzADT *X) |
85 | 0 | { |
86 | 0 | return PointerGetDatum(X); |
87 | 0 | } Unexecuted instantiation: brin_minmax_multi.c:TimeTzADTPGetDatum Unexecuted instantiation: parse_expr.c:TimeTzADTPGetDatum Unexecuted instantiation: execExprInterp.c:TimeTzADTPGetDatum Unexecuted instantiation: date.c:TimeTzADTPGetDatum Unexecuted instantiation: datetime.c:TimeTzADTPGetDatum Unexecuted instantiation: formatting.c:TimeTzADTPGetDatum Unexecuted instantiation: json.c:TimeTzADTPGetDatum Unexecuted instantiation: jsonpath_exec.c:TimeTzADTPGetDatum Unexecuted instantiation: pseudorandomfuncs.c:TimeTzADTPGetDatum Unexecuted instantiation: rangetypes.c:TimeTzADTPGetDatum Unexecuted instantiation: selfuncs.c:TimeTzADTPGetDatum Unexecuted instantiation: timestamp.c:TimeTzADTPGetDatum Unexecuted instantiation: xml.c:TimeTzADTPGetDatum |
88 | | |
89 | 0 | #define PG_GETARG_DATEADT(n) DatumGetDateADT(PG_GETARG_DATUM(n)) |
90 | 0 | #define PG_GETARG_TIMEADT(n) DatumGetTimeADT(PG_GETARG_DATUM(n)) |
91 | 0 | #define PG_GETARG_TIMETZADT_P(n) DatumGetTimeTzADTP(PG_GETARG_DATUM(n)) |
92 | | |
93 | 0 | #define PG_RETURN_DATEADT(x) return DateADTGetDatum(x) |
94 | 0 | #define PG_RETURN_TIMEADT(x) return TimeADTGetDatum(x) |
95 | 0 | #define PG_RETURN_TIMETZADT_P(x) return TimeTzADTPGetDatum(x) |
96 | | |
97 | | |
98 | | /* date.c */ |
99 | | extern int32 anytime_typmod_check(bool istz, int32 typmod); |
100 | | extern double date2timestamp_no_overflow(DateADT dateVal); |
101 | | extern Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow); |
102 | | extern TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow); |
103 | | extern DateADT timestamp2date_opt_overflow(Timestamp timestamp, int *overflow); |
104 | | extern DateADT timestamptz2date_opt_overflow(TimestampTz timestamp, int *overflow); |
105 | | extern int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2); |
106 | | extern int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2); |
107 | | |
108 | | extern void EncodeSpecialDate(DateADT dt, char *str); |
109 | | extern DateADT GetSQLCurrentDate(void); |
110 | | extern TimeTzADT *GetSQLCurrentTime(int32 typmod); |
111 | | extern TimeADT GetSQLLocalTime(int32 typmod); |
112 | | extern int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec); |
113 | | extern int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp); |
114 | | extern int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result); |
115 | | extern int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result); |
116 | | extern bool time_overflows(int hour, int min, int sec, fsec_t fsec); |
117 | | extern bool float_time_overflows(int hour, int min, double sec); |
118 | | extern void AdjustTimeForTypmod(TimeADT *time, int32 typmod); |
119 | | |
120 | | #endif /* DATE_H */ |