Code review for commit 05a7be935.
[pgsql.git] / src / include / datatype / timestamp.h
blobab8ccf89ca9eaf6c5d223dd5454d4b9b17272888
1 /*-------------------------------------------------------------------------
3 * timestamp.h
4 * Timestamp and Interval typedefs and related macros.
6 * Note: this file must be includable in both frontend and backend contexts.
8 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/include/datatype/timestamp.h
13 *-------------------------------------------------------------------------
15 #ifndef DATATYPE_TIMESTAMP_H
16 #define DATATYPE_TIMESTAMP_H
19 * Timestamp represents absolute time.
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.
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
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.)
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.
38 typedef int64 Timestamp;
39 typedef int64 TimestampTz;
40 typedef int64 TimeOffset;
41 typedef int32 fsec_t; /* fractional seconds (in microseconds) */
45 * Storage format for type interval.
47 typedef struct
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;
56 * Data structure representing a broken-down interval.
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.
65 struct pg_itm
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;
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.
82 struct pg_itm_in
84 int64 tm_usec; /* needs to be wide */
85 int tm_mday;
86 int tm_mon;
87 int tm_year;
91 /* Limits on the "precision" option (typmod) for these data types */
92 #define MAX_TIMESTAMP_PRECISION 6
93 #define MAX_INTERVAL_PRECISION 6
96 * Round off to MAX_TIMESTAMP_PRECISION decimal places.
97 * Note: this is also used for rounding off intervals.
99 #define TS_PREC_INV 1000000.0
100 #define TSROUND(j) (rint(((double) (j)) * TS_PREC_INV) / TS_PREC_INV)
104 * Assorted constants for datetime-related calculations
107 #define DAYS_PER_YEAR 365.25 /* assumes leap year every four years */
108 #define MONTHS_PER_YEAR 12
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.
116 #define DAYS_PER_MONTH 30 /* assumes exactly 30 days per month */
117 #define HOURS_PER_DAY 24 /* assume no daylight savings time changes */
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.
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
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)
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.
142 #define MAX_TZDISP_HOUR 15 /* maximum allowed hour part */
143 #define TZDISP_LIMIT ((MAX_TZDISP_HOUR + 1) * SECS_PER_HOUR)
146 * We reserve the minimum and maximum integer values to represent
147 * timestamp (or timestamptz) -infinity and +infinity.
149 #define TIMESTAMP_MINUS_INFINITY PG_INT64_MIN
150 #define TIMESTAMP_INFINITY PG_INT64_MAX
153 * Historically these alias for infinity have been used.
155 #define DT_NOBEGIN TIMESTAMP_MINUS_INFINITY
156 #define DT_NOEND TIMESTAMP_INFINITY
158 #define TIMESTAMP_NOBEGIN(j) \
159 do {(j) = DT_NOBEGIN;} while (0)
161 #define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN)
163 #define TIMESTAMP_NOEND(j) \
164 do {(j) = DT_NOEND;} while (0)
166 #define TIMESTAMP_IS_NOEND(j) ((j) == DT_NOEND)
168 #define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
172 * Julian date support.
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.
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.
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)
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))))
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) */
212 * Range limits for dates and timestamps.
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.
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.
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) */
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 */
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))
240 /* Range-check a timestamp */
241 #define IS_VALID_TIMESTAMP(t) (MIN_TIMESTAMP <= (t) && (t) < END_TIMESTAMP)
243 #endif /* DATATYPE_TIMESTAMP_H */