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-2024, 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 DAYS_PER_WEEK 7
118 #define HOURS_PER_DAY 24 /* assume no daylight savings time changes */
119 
120 /*
121  * This doesn't adjust for uneven daylight savings time intervals or leap
122  * seconds, and it crudely estimates leap years. A more accurate value
123  * for days per years is 365.2422.
124  */
125 #define SECS_PER_YEAR (36525 * 864) /* avoid floating-point computation */
126 #define SECS_PER_DAY 86400
127 #define SECS_PER_HOUR 3600
128 #define SECS_PER_MINUTE 60
129 #define MINS_PER_HOUR 60
130 
131 #define USECS_PER_DAY INT64CONST(86400000000)
132 #define USECS_PER_HOUR INT64CONST(3600000000)
133 #define USECS_PER_MINUTE INT64CONST(60000000)
134 #define USECS_PER_SEC INT64CONST(1000000)
135 
136 /*
137  * We allow numeric timezone offsets up to 15:59:59 either way from Greenwich.
138  * Currently, the record holders for wackiest offsets in actual use are zones
139  * Asia/Manila, at -15:56:00 until 1844, and America/Metlakatla, at +15:13:42
140  * until 1867. If we were to reject such values we would fail to dump and
141  * restore old timestamptz values with these zone settings.
142  */
143 #define MAX_TZDISP_HOUR 15 /* maximum allowed hour part */
144 #define TZDISP_LIMIT ((MAX_TZDISP_HOUR + 1) * SECS_PER_HOUR)
145 
146 /*
147  * We reserve the minimum and maximum integer values to represent
148  * timestamp (or timestamptz) -infinity and +infinity.
149  */
150 #define TIMESTAMP_MINUS_INFINITY PG_INT64_MIN
151 #define TIMESTAMP_INFINITY PG_INT64_MAX
152 
153 /*
154  * Historically these aliases for infinity have been used.
155  */
156 #define DT_NOBEGIN TIMESTAMP_MINUS_INFINITY
157 #define DT_NOEND TIMESTAMP_INFINITY
158 
159 #define TIMESTAMP_NOBEGIN(j) \
160  do {(j) = DT_NOBEGIN;} while (0)
161 
162 #define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN)
163 
164 #define TIMESTAMP_NOEND(j) \
165  do {(j) = DT_NOEND;} while (0)
166 
167 #define TIMESTAMP_IS_NOEND(j) ((j) == DT_NOEND)
168 
169 #define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
170 
171 /*
172  * Infinite intervals are represented by setting all fields to the minimum or
173  * maximum integer values.
174  */
175 #define INTERVAL_NOBEGIN(i) \
176  do { \
177  (i)->time = PG_INT64_MIN; \
178  (i)->day = PG_INT32_MIN; \
179  (i)->month = PG_INT32_MIN; \
180  } while (0)
181 
182 #define INTERVAL_IS_NOBEGIN(i) \
183  ((i)->month == PG_INT32_MIN && (i)->day == PG_INT32_MIN && (i)->time == PG_INT64_MIN)
184 
185 #define INTERVAL_NOEND(i) \
186  do { \
187  (i)->time = PG_INT64_MAX; \
188  (i)->day = PG_INT32_MAX; \
189  (i)->month = PG_INT32_MAX; \
190  } while (0)
191 
192 #define INTERVAL_IS_NOEND(i) \
193  ((i)->month == PG_INT32_MAX && (i)->day == PG_INT32_MAX && (i)->time == PG_INT64_MAX)
194 
195 #define INTERVAL_NOT_FINITE(i) (INTERVAL_IS_NOBEGIN(i) || INTERVAL_IS_NOEND(i))
196 
197 /*
198  * Julian date support.
199  *
200  * date2j() and j2date() nominally handle the Julian date range 0..INT_MAX,
201  * or 4714-11-24 BC to 5874898-06-03 AD. In practice, date2j() will work and
202  * give correct negative Julian dates for dates before 4714-11-24 BC as well.
203  * We rely on it to do so back to 4714-11-01 BC. Allowing at least one day's
204  * slop is necessary so that timestamp rotation doesn't produce dates that
205  * would be rejected on input. For example, '4714-11-24 00:00 GMT BC' is a
206  * legal timestamptz value, but in zones east of Greenwich it would print as
207  * sometime in the afternoon of 4714-11-23 BC; if we couldn't process such a
208  * date we'd have a dump/reload failure. So the idea is for IS_VALID_JULIAN
209  * to accept a slightly wider range of dates than we really support, and
210  * then we apply the exact checks in IS_VALID_DATE or IS_VALID_TIMESTAMP,
211  * after timezone rotation if any. To save a few cycles, we can make
212  * IS_VALID_JULIAN check only to the month boundary, since its exact cutoffs
213  * are not very critical in this scheme.
214  *
215  * It is correct that JULIAN_MINYEAR is -4713, not -4714; it is defined to
216  * allow easy comparison to tm_year values, in which we follow the convention
217  * that tm_year <= 0 represents abs(tm_year)+1 BC.
218  */
219 
220 #define JULIAN_MINYEAR (-4713)
221 #define JULIAN_MINMONTH (11)
222 #define JULIAN_MINDAY (24)
223 #define JULIAN_MAXYEAR (5874898)
224 #define JULIAN_MAXMONTH (6)
225 #define JULIAN_MAXDAY (3)
226 
227 #define IS_VALID_JULIAN(y,m,d) \
228  (((y) > JULIAN_MINYEAR || \
229  ((y) == JULIAN_MINYEAR && ((m) >= JULIAN_MINMONTH))) && \
230  ((y) < JULIAN_MAXYEAR || \
231  ((y) == JULIAN_MAXYEAR && ((m) < JULIAN_MAXMONTH))))
232 
233 /* Julian-date equivalents of Day 0 in Unix and Postgres reckoning */
234 #define UNIX_EPOCH_JDATE 2440588 /* == date2j(1970, 1, 1) */
235 #define POSTGRES_EPOCH_JDATE 2451545 /* == date2j(2000, 1, 1) */
236 
237 /*
238  * Range limits for dates and timestamps.
239  *
240  * We have traditionally allowed Julian day zero as a valid datetime value,
241  * so that is the lower bound for both dates and timestamps.
242  *
243  * The upper limit for dates is 5874897-12-31, which is a bit less than what
244  * the Julian-date code can allow. For timestamps, the upper limit is
245  * 294276-12-31. The int64 overflow limit would be a few days later; again,
246  * leaving some slop avoids worries about corner-case overflow, and provides
247  * a simpler user-visible definition.
248  */
249 
250 /* First allowed date, and first disallowed date, in Julian-date form */
251 #define DATETIME_MIN_JULIAN (0)
252 #define DATE_END_JULIAN (2147483494) /* == date2j(JULIAN_MAXYEAR, 1, 1) */
253 #define TIMESTAMP_END_JULIAN (109203528) /* == date2j(294277, 1, 1) */
254 
255 /* Timestamp limits */
256 #define MIN_TIMESTAMP INT64CONST(-211813488000000000)
257 /* == (DATETIME_MIN_JULIAN - POSTGRES_EPOCH_JDATE) * USECS_PER_DAY */
258 #define END_TIMESTAMP INT64CONST(9223371331200000000)
259 /* == (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE) * USECS_PER_DAY */
260 
261 /* Range-check a date (given in Postgres, not Julian, numbering) */
262 #define IS_VALID_DATE(d) \
263  ((DATETIME_MIN_JULIAN - POSTGRES_EPOCH_JDATE) <= (d) && \
264  (d) < (DATE_END_JULIAN - POSTGRES_EPOCH_JDATE))
265 
266 /* Range-check a timestamp */
267 #define IS_VALID_TIMESTAMP(t) (MIN_TIMESTAMP <= (t) && (t) < END_TIMESTAMP)
268 
269 #endif /* DATATYPE_TIMESTAMP_H */
signed int int32
Definition: c.h:494
int64 Timestamp
Definition: timestamp.h:38
int64 TimestampTz
Definition: timestamp.h:39
int32 fsec_t
Definition: timestamp.h:41
int64 TimeOffset
Definition: timestamp.h:40
int32 day
Definition: timestamp.h:51
int32 month
Definition: timestamp.h:52
TimeOffset time
Definition: timestamp.h:49
int tm_mon
Definition: timestamp.h:86
int tm_year
Definition: timestamp.h:87
int tm_mday
Definition: timestamp.h:85
int64 tm_usec
Definition: timestamp.h:84
int64 tm_hour
Definition: timestamp.h:70
int tm_year
Definition: timestamp.h:73
int tm_mon
Definition: timestamp.h:72
int tm_mday
Definition: timestamp.h:71
int tm_sec
Definition: timestamp.h:68
int tm_min
Definition: timestamp.h:69
int tm_usec
Definition: timestamp.h:67