PostgreSQL Source Code
git master
timestamp.h
Go to the documentation of this file.
1
/*-------------------------------------------------------------------------
2
*
3
* timestamp.h
4
* Timestamp and Interval typedefs and related macros.
5
*
6
* Note: this file must be includable in both frontend and backend contexts.
7
*
8
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
9
* Portions Copyright (c) 1994, Regents of the University of California
10
*
11
* src/include/datatype/timestamp.h
12
*
13
*-------------------------------------------------------------------------
14
*/
15
#ifndef DATATYPE_TIMESTAMP_H
16
#define DATATYPE_TIMESTAMP_H
17
18
/*
19
* Timestamp represents absolute time.
20
*
21
* Interval represents delta time. Keep track of months (and years), days,
22
* and hours/minutes/seconds separately since the elapsed time spanned is
23
* unknown until instantiated relative to an absolute time.
24
*
25
* Note that Postgres uses "time interval" to mean a bounded interval,
26
* consisting of a beginning and ending time, not a time span - thomas 97/03/20
27
*
28
* Timestamps, as well as the h/m/s fields of intervals, are stored as
29
* int64 values with units of microseconds. (Once upon a time they were
30
* double values with units of seconds.)
31
*
32
* TimeOffset and fsec_t are convenience typedefs for temporary variables.
33
* Do not use fsec_t in values stored on-disk.
34
* Also, fsec_t is only meant for *fractional* seconds; beware of overflow
35
* if the value you need to store could be many seconds.
36
*/
37
38
typedef
int64
Timestamp
;
39
typedef
int64
TimestampTz
;
40
typedef
int64
TimeOffset
;
41
typedef
int32
fsec_t
;
/* fractional seconds (in microseconds) */
42
43
44
/*
45
* Storage format for type interval.
46
*/
47
typedef
struct
48
{
49
TimeOffset
time
;
/* all time units other than days, months and
50
* years */
51
int32
day
;
/* days, after time for alignment */
52
int32
month
;
/* months and years, after time for alignment */
53
}
Interval
;
54
55
/*
56
* Data structure representing a broken-down interval.
57
*
58
* For historical reasons, this is modeled on struct pg_tm for timestamps.
59
* Unlike the situation for timestamps, there's no magic interpretation
60
* needed for months or years: they're just zero or not. Note that fields
61
* can be negative; however, because of the divisions done while converting
62
* from struct Interval, only tm_mday could be INT_MIN. This is important
63
* because we may need to negate the values in some code paths.
64
*/
65
struct
pg_itm
66
{
67
int
tm_usec
;
68
int
tm_sec
;
69
int
tm_min
;
70
int64
tm_hour
;
/* needs to be wide */
71
int
tm_mday
;
72
int
tm_mon
;
73
int
tm_year
;
74
};
75
76
/*
77
* Data structure for decoding intervals. We could just use struct pg_itm,
78
* but then the requirement for tm_usec to be 64 bits would propagate to
79
* places where it's not really needed. Also, omitting the fields that
80
* aren't used during decoding seems like a good error-prevention measure.
81
*/
82
struct
pg_itm_in
83
{
84
int64
tm_usec
;
/* needs to be wide */
85
int
tm_mday
;
86
int
tm_mon
;
87
int
tm_year
;
88
};
89
90
91
/* Limits on the "precision" option (typmod) for these data types */
92
#define MAX_TIMESTAMP_PRECISION 6
93
#define MAX_INTERVAL_PRECISION 6
94
95
/*
96
* Round off to MAX_TIMESTAMP_PRECISION decimal places.
97
* Note: this is also used for rounding off intervals.
98
*/
99
#define TS_PREC_INV 1000000.0
100
#define TSROUND(j) (rint(((double) (j)) * TS_PREC_INV) / TS_PREC_INV)
101
102
103
/*
104
* Assorted constants for datetime-related calculations
105
*/
106
107
#define DAYS_PER_YEAR 365.25
/* assumes leap year every four years */
108
#define MONTHS_PER_YEAR 12
109
/*
110
* DAYS_PER_MONTH is very imprecise. The more accurate value is
111
* 365.2425/12 = 30.436875, or '30 days 10:29:06'. Right now we only
112
* return an integral number of days, but someday perhaps we should
113
* also return a 'time' value to be used as well. ISO 8601 suggests
114
* 30 days.
115
*/
116
#define DAYS_PER_MONTH 30
/* assumes exactly 30 days per month */
117
#define HOURS_PER_DAY 24
/* assume no daylight savings time changes */
118
119
/*
120
* This doesn't adjust for uneven daylight savings time intervals or leap
121
* seconds, and it crudely estimates leap years. A more accurate value
122
* for days per years is 365.2422.
123
*/
124
#define SECS_PER_YEAR (36525 * 864)
/* avoid floating-point computation */
125
#define SECS_PER_DAY 86400
126
#define SECS_PER_HOUR 3600
127
#define SECS_PER_MINUTE 60
128
#define MINS_PER_HOUR 60
129
130
#define USECS_PER_DAY INT64CONST(86400000000)
131
#define USECS_PER_HOUR INT64CONST(3600000000)
132
#define USECS_PER_MINUTE INT64CONST(60000000)
133
#define USECS_PER_SEC INT64CONST(1000000)
134
135
/*
136
* We allow numeric timezone offsets up to 15:59:59 either way from Greenwich.
137
* Currently, the record holders for wackiest offsets in actual use are zones
138
* Asia/Manila, at -15:56:00 until 1844, and America/Metlakatla, at +15:13:42
139
* until 1867. If we were to reject such values we would fail to dump and
140
* restore old timestamptz values with these zone settings.
141
*/
142
#define MAX_TZDISP_HOUR 15
/* maximum allowed hour part */
143
#define TZDISP_LIMIT ((MAX_TZDISP_HOUR + 1) * SECS_PER_HOUR)
144
145
/*
146
* We reserve the minimum and maximum integer values to represent
147
* timestamp (or timestamptz) -infinity and +infinity.
148
*/
149
#define TIMESTAMP_MINUS_INFINITY PG_INT64_MIN
150
#define TIMESTAMP_INFINITY PG_INT64_MAX
151
152
/*
153
* Historically these alias for infinity have been used.
154
*/
155
#define DT_NOBEGIN TIMESTAMP_MINUS_INFINITY
156
#define DT_NOEND TIMESTAMP_INFINITY
157
158
#define TIMESTAMP_NOBEGIN(j) \
159
do {(j) = DT_NOBEGIN;} while (0)
160
161
#define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN)
162
163
#define TIMESTAMP_NOEND(j) \
164
do {(j) = DT_NOEND;} while (0)
165
166
#define TIMESTAMP_IS_NOEND(j) ((j) == DT_NOEND)
167
168
#define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
169
170
171
/*
172
* Julian date support.
173
*
174
* date2j() and j2date() nominally handle the Julian date range 0..INT_MAX,
175
* or 4714-11-24 BC to 5874898-06-03 AD. In practice, date2j() will work and
176
* give correct negative Julian dates for dates before 4714-11-24 BC as well.
177
* We rely on it to do so back to 4714-11-01 BC. Allowing at least one day's
178
* slop is necessary so that timestamp rotation doesn't produce dates that
179
* would be rejected on input. For example, '4714-11-24 00:00 GMT BC' is a
180
* legal timestamptz value, but in zones east of Greenwich it would print as
181
* sometime in the afternoon of 4714-11-23 BC; if we couldn't process such a
182
* date we'd have a dump/reload failure. So the idea is for IS_VALID_JULIAN
183
* to accept a slightly wider range of dates than we really support, and
184
* then we apply the exact checks in IS_VALID_DATE or IS_VALID_TIMESTAMP,
185
* after timezone rotation if any. To save a few cycles, we can make
186
* IS_VALID_JULIAN check only to the month boundary, since its exact cutoffs
187
* are not very critical in this scheme.
188
*
189
* It is correct that JULIAN_MINYEAR is -4713, not -4714; it is defined to
190
* allow easy comparison to tm_year values, in which we follow the convention
191
* that tm_year <= 0 represents abs(tm_year)+1 BC.
192
*/
193
194
#define JULIAN_MINYEAR (-4713)
195
#define JULIAN_MINMONTH (11)
196
#define JULIAN_MINDAY (24)
197
#define JULIAN_MAXYEAR (5874898)
198
#define JULIAN_MAXMONTH (6)
199
#define JULIAN_MAXDAY (3)
200
201
#define IS_VALID_JULIAN(y,m,d) \
202
(((y) > JULIAN_MINYEAR || \
203
((y) == JULIAN_MINYEAR && ((m) >= JULIAN_MINMONTH))) && \
204
((y) < JULIAN_MAXYEAR || \
205
((y) == JULIAN_MAXYEAR && ((m) < JULIAN_MAXMONTH))))
206
207
/* Julian-date equivalents of Day 0 in Unix and Postgres reckoning */
208
#define UNIX_EPOCH_JDATE 2440588
/* == date2j(1970, 1, 1) */
209
#define POSTGRES_EPOCH_JDATE 2451545
/* == date2j(2000, 1, 1) */
210
211
/*
212
* Range limits for dates and timestamps.
213
*
214
* We have traditionally allowed Julian day zero as a valid datetime value,
215
* so that is the lower bound for both dates and timestamps.
216
*
217
* The upper limit for dates is 5874897-12-31, which is a bit less than what
218
* the Julian-date code can allow. For timestamps, the upper limit is
219
* 294276-12-31. The int64 overflow limit would be a few days later; again,
220
* leaving some slop avoids worries about corner-case overflow, and provides
221
* a simpler user-visible definition.
222
*/
223
224
/* First allowed date, and first disallowed date, in Julian-date form */
225
#define DATETIME_MIN_JULIAN (0)
226
#define DATE_END_JULIAN (2147483494)
/* == date2j(JULIAN_MAXYEAR, 1, 1) */
227
#define TIMESTAMP_END_JULIAN (109203528)
/* == date2j(294277, 1, 1) */
228
229
/* Timestamp limits */
230
#define MIN_TIMESTAMP INT64CONST(-211813488000000000)
231
/* == (DATETIME_MIN_JULIAN - POSTGRES_EPOCH_JDATE) * USECS_PER_DAY */
232
#define END_TIMESTAMP INT64CONST(9223371331200000000)
233
/* == (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE) * USECS_PER_DAY */
234
235
/* Range-check a date (given in Postgres, not Julian, numbering) */
236
#define IS_VALID_DATE(d) \
237
((DATETIME_MIN_JULIAN - POSTGRES_EPOCH_JDATE) <= (d) && \
238
(d) < (DATE_END_JULIAN - POSTGRES_EPOCH_JDATE))
239
240
/* Range-check a timestamp */
241
#define IS_VALID_TIMESTAMP(t) (MIN_TIMESTAMP <= (t) && (t) < END_TIMESTAMP)
242
243
#endif
/* DATATYPE_TIMESTAMP_H */
int32
signed int int32
Definition:
c.h:483
Timestamp
int64 Timestamp
Definition:
timestamp.h:38
TimestampTz
int64 TimestampTz
Definition:
timestamp.h:39
fsec_t
int32 fsec_t
Definition:
timestamp.h:41
TimeOffset
int64 TimeOffset
Definition:
timestamp.h:40
Interval
Definition:
timestamp.h:48
Interval::day
int32 day
Definition:
timestamp.h:51
Interval::month
int32 month
Definition:
timestamp.h:52
Interval::time
TimeOffset time
Definition:
timestamp.h:49
pg_itm_in
Definition:
timestamp.h:83
pg_itm_in::tm_mon
int tm_mon
Definition:
timestamp.h:86
pg_itm_in::tm_year
int tm_year
Definition:
timestamp.h:87
pg_itm_in::tm_mday
int tm_mday
Definition:
timestamp.h:85
pg_itm_in::tm_usec
int64 tm_usec
Definition:
timestamp.h:84
pg_itm
Definition:
timestamp.h:66
pg_itm::tm_hour
int64 tm_hour
Definition:
timestamp.h:70
pg_itm::tm_year
int tm_year
Definition:
timestamp.h:73
pg_itm::tm_mon
int tm_mon
Definition:
timestamp.h:72
pg_itm::tm_mday
int tm_mday
Definition:
timestamp.h:71
pg_itm::tm_sec
int tm_sec
Definition:
timestamp.h:68
pg_itm::tm_min
int tm_min
Definition:
timestamp.h:69
pg_itm::tm_usec
int tm_usec
Definition:
timestamp.h:67
src
include
datatype
timestamp.h
Generated on Wed Sep 27 2023 18:13:23 for PostgreSQL Source Code by
1.9.1