Update FSM on WAL replay. This is a bit limited; the FSM is only updated
[PostgreSQL.git] / src / include / utils / timestamp.h
blobd1b3c161fcec19a8f7c3951ec1be6f2a305113eb
1 /*-------------------------------------------------------------------------
3 * timestamp.h
4 * Definitions for the SQL92 "timestamp" and "interval" types.
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $PostgreSQL$
11 *-------------------------------------------------------------------------
13 #ifndef TIMESTAMP_H
14 #define TIMESTAMP_H
16 #include <math.h>
17 #include <limits.h>
18 #include <float.h>
20 #include "fmgr.h"
21 #include "pgtime.h"
22 #ifdef HAVE_INT64_TIMESTAMP
23 #include "utils/int8.h"
24 #endif
27 * Timestamp represents absolute time.
29 * Interval represents delta time. Keep track of months (and years), days,
30 * and hours/minutes/seconds separately since the elapsed time spanned is
31 * unknown until instantiated relative to an absolute time.
33 * Note that Postgres uses "time interval" to mean a bounded interval,
34 * consisting of a beginning and ending time, not a time span - thomas 97/03/20
36 * We have two implementations, one that uses int64 values with units of
37 * microseconds, and one that uses double values with units of seconds.
39 * TimeOffset and fsec_t are convenience typedefs for temporary variables
40 * that are of different types in the two cases. Do not use fsec_t in values
41 * stored on-disk, since it is not the same size in both implementations.
44 #ifdef HAVE_INT64_TIMESTAMP
46 typedef int64 Timestamp;
47 typedef int64 TimestampTz;
48 typedef int64 TimeOffset;
49 typedef int32 fsec_t; /* fractional seconds (in microseconds) */
51 #else
53 typedef double Timestamp;
54 typedef double TimestampTz;
55 typedef double TimeOffset;
56 typedef double fsec_t; /* fractional seconds (in seconds) */
58 #endif
60 typedef struct
62 TimeOffset time; /* all time units other than days, months and
63 * years */
64 int32 day; /* days, after time for alignment */
65 int32 month; /* months and years, after time for alignment */
66 } Interval;
69 #define MAX_TIMESTAMP_PRECISION 6
70 #define MAX_INTERVAL_PRECISION 6
72 /* in both timestamp.h and ecpg/dt.h */
73 #define DAYS_PER_YEAR 365.25 /* assumes leap year every four years */
74 #define MONTHS_PER_YEAR 12
76 * DAYS_PER_MONTH is very imprecise. The more accurate value is
77 * 365.2425/12 = 30.436875, or '30 days 10:29:06'. Right now we only
78 * return an integral number of days, but someday perhaps we should
79 * also return a 'time' value to be used as well. ISO 8601 suggests
80 * 30 days.
82 #define DAYS_PER_MONTH 30 /* assumes exactly 30 days per month */
83 #define HOURS_PER_DAY 24 /* assume no daylight savings time changes */
86 * This doesn't adjust for uneven daylight savings time intervals or leap
87 * seconds, and it crudely estimates leap years. A more accurate value
88 * for days per years is 365.2422.
90 #define SECS_PER_YEAR (36525 * 864) /* avoid floating-point computation */
91 #define SECS_PER_DAY 86400
92 #define SECS_PER_HOUR 3600
93 #define SECS_PER_MINUTE 60
94 #define MINS_PER_HOUR 60
96 #ifdef HAVE_INT64_TIMESTAMP
97 #define USECS_PER_DAY INT64CONST(86400000000)
98 #define USECS_PER_HOUR INT64CONST(3600000000)
99 #define USECS_PER_MINUTE INT64CONST(60000000)
100 #define USECS_PER_SEC INT64CONST(1000000)
101 #endif
104 * Macros for fmgr-callable functions.
106 * For Timestamp, we make use of the same support routines as for int64
107 * or float8. Therefore Timestamp is pass-by-reference if and only if
108 * int64 or float8 is!
110 #ifdef HAVE_INT64_TIMESTAMP
112 #define DatumGetTimestamp(X) ((Timestamp) DatumGetInt64(X))
113 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetInt64(X))
114 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
116 #define TimestampGetDatum(X) Int64GetDatum(X)
117 #define TimestampTzGetDatum(X) Int64GetDatum(X)
118 #define IntervalPGetDatum(X) PointerGetDatum(X)
120 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
121 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
122 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
124 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
125 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
126 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
128 #define DT_NOBEGIN (-INT64CONST(0x7fffffffffffffff) - 1)
129 #define DT_NOEND (INT64CONST(0x7fffffffffffffff))
131 #else /* !HAVE_INT64_TIMESTAMP */
133 #define DatumGetTimestamp(X) ((Timestamp) DatumGetFloat8(X))
134 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetFloat8(X))
135 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
137 #define TimestampGetDatum(X) Float8GetDatum(X)
138 #define TimestampTzGetDatum(X) Float8GetDatum(X)
139 #define IntervalPGetDatum(X) PointerGetDatum(X)
141 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
142 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
143 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
145 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
146 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
147 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
149 #ifdef HUGE_VAL
150 #define DT_NOBEGIN (-HUGE_VAL)
151 #define DT_NOEND (HUGE_VAL)
152 #else
153 #define DT_NOBEGIN (-DBL_MAX)
154 #define DT_NOEND (DBL_MAX)
155 #endif
157 #endif /* HAVE_INT64_TIMESTAMP */
160 #define TIMESTAMP_NOBEGIN(j) \
161 do {(j) = DT_NOBEGIN;} while (0)
162 #define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN)
164 #define TIMESTAMP_NOEND(j) \
165 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))
171 * Round off to MAX_TIMESTAMP_PRECISION decimal places.
172 * Note: this is also used for rounding off intervals.
174 #define TS_PREC_INV 1000000.0
175 #define TSROUND(j) (rint(((double) (j)) * TS_PREC_INV) / TS_PREC_INV)
177 #define TIMESTAMP_MASK(b) (1 << (b))
178 #define INTERVAL_MASK(b) (1 << (b))
180 /* Macros to handle packing and unpacking the typmod field for intervals */
181 #define INTERVAL_FULL_RANGE (0x7FFF)
182 #define INTERVAL_RANGE_MASK (0x7FFF)
183 #define INTERVAL_FULL_PRECISION (0xFFFF)
184 #define INTERVAL_PRECISION_MASK (0xFFFF)
185 #define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
186 #define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
187 #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
189 #ifdef HAVE_INT64_TIMESTAMP
190 #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * (int64) 1000))
191 #else
192 #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) / 1000.0))
193 #endif
196 /* Set at postmaster start */
197 extern TimestampTz PgStartTime;
198 /* Set at configuration reload */
199 extern TimestampTz PgReloadTime;
203 * timestamp.c prototypes
206 extern Datum timestamp_in(PG_FUNCTION_ARGS);
207 extern Datum timestamp_out(PG_FUNCTION_ARGS);
208 extern Datum timestamp_recv(PG_FUNCTION_ARGS);
209 extern Datum timestamp_send(PG_FUNCTION_ARGS);
210 extern Datum timestamptypmodin(PG_FUNCTION_ARGS);
211 extern Datum timestamptypmodout(PG_FUNCTION_ARGS);
212 extern Datum timestamp_scale(PG_FUNCTION_ARGS);
213 extern Datum timestamp_eq(PG_FUNCTION_ARGS);
214 extern Datum timestamp_ne(PG_FUNCTION_ARGS);
215 extern Datum timestamp_lt(PG_FUNCTION_ARGS);
216 extern Datum timestamp_le(PG_FUNCTION_ARGS);
217 extern Datum timestamp_ge(PG_FUNCTION_ARGS);
218 extern Datum timestamp_gt(PG_FUNCTION_ARGS);
219 extern Datum timestamp_finite(PG_FUNCTION_ARGS);
220 extern Datum timestamp_cmp(PG_FUNCTION_ARGS);
221 extern Datum timestamp_hash(PG_FUNCTION_ARGS);
222 extern Datum timestamp_smaller(PG_FUNCTION_ARGS);
223 extern Datum timestamp_larger(PG_FUNCTION_ARGS);
225 extern Datum timestamp_eq_timestamptz(PG_FUNCTION_ARGS);
226 extern Datum timestamp_ne_timestamptz(PG_FUNCTION_ARGS);
227 extern Datum timestamp_lt_timestamptz(PG_FUNCTION_ARGS);
228 extern Datum timestamp_le_timestamptz(PG_FUNCTION_ARGS);
229 extern Datum timestamp_gt_timestamptz(PG_FUNCTION_ARGS);
230 extern Datum timestamp_ge_timestamptz(PG_FUNCTION_ARGS);
231 extern Datum timestamp_cmp_timestamptz(PG_FUNCTION_ARGS);
233 extern Datum timestamptz_eq_timestamp(PG_FUNCTION_ARGS);
234 extern Datum timestamptz_ne_timestamp(PG_FUNCTION_ARGS);
235 extern Datum timestamptz_lt_timestamp(PG_FUNCTION_ARGS);
236 extern Datum timestamptz_le_timestamp(PG_FUNCTION_ARGS);
237 extern Datum timestamptz_gt_timestamp(PG_FUNCTION_ARGS);
238 extern Datum timestamptz_ge_timestamp(PG_FUNCTION_ARGS);
239 extern Datum timestamptz_cmp_timestamp(PG_FUNCTION_ARGS);
241 extern Datum interval_in(PG_FUNCTION_ARGS);
242 extern Datum interval_out(PG_FUNCTION_ARGS);
243 extern Datum interval_recv(PG_FUNCTION_ARGS);
244 extern Datum interval_send(PG_FUNCTION_ARGS);
245 extern Datum intervaltypmodin(PG_FUNCTION_ARGS);
246 extern Datum intervaltypmodout(PG_FUNCTION_ARGS);
247 extern Datum interval_scale(PG_FUNCTION_ARGS);
248 extern Datum interval_eq(PG_FUNCTION_ARGS);
249 extern Datum interval_ne(PG_FUNCTION_ARGS);
250 extern Datum interval_lt(PG_FUNCTION_ARGS);
251 extern Datum interval_le(PG_FUNCTION_ARGS);
252 extern Datum interval_ge(PG_FUNCTION_ARGS);
253 extern Datum interval_gt(PG_FUNCTION_ARGS);
254 extern Datum interval_finite(PG_FUNCTION_ARGS);
255 extern Datum interval_cmp(PG_FUNCTION_ARGS);
256 extern Datum interval_hash(PG_FUNCTION_ARGS);
257 extern Datum interval_smaller(PG_FUNCTION_ARGS);
258 extern Datum interval_larger(PG_FUNCTION_ARGS);
259 extern Datum interval_justify_interval(PG_FUNCTION_ARGS);
260 extern Datum interval_justify_hours(PG_FUNCTION_ARGS);
261 extern Datum interval_justify_days(PG_FUNCTION_ARGS);
263 extern Datum timestamp_trunc(PG_FUNCTION_ARGS);
264 extern Datum interval_trunc(PG_FUNCTION_ARGS);
265 extern Datum timestamp_part(PG_FUNCTION_ARGS);
266 extern Datum interval_part(PG_FUNCTION_ARGS);
267 extern Datum timestamp_zone(PG_FUNCTION_ARGS);
268 extern Datum timestamp_izone(PG_FUNCTION_ARGS);
269 extern Datum timestamp_timestamptz(PG_FUNCTION_ARGS);
271 extern Datum timestamptz_in(PG_FUNCTION_ARGS);
272 extern Datum timestamptz_out(PG_FUNCTION_ARGS);
273 extern Datum timestamptz_recv(PG_FUNCTION_ARGS);
274 extern Datum timestamptz_send(PG_FUNCTION_ARGS);
275 extern Datum timestamptztypmodin(PG_FUNCTION_ARGS);
276 extern Datum timestamptztypmodout(PG_FUNCTION_ARGS);
277 extern Datum timestamptz_scale(PG_FUNCTION_ARGS);
278 extern Datum timestamptz_timestamp(PG_FUNCTION_ARGS);
279 extern Datum timestamptz_zone(PG_FUNCTION_ARGS);
280 extern Datum timestamptz_izone(PG_FUNCTION_ARGS);
281 extern Datum timestamptz_timestamptz(PG_FUNCTION_ARGS);
283 extern Datum interval_um(PG_FUNCTION_ARGS);
284 extern Datum interval_pl(PG_FUNCTION_ARGS);
285 extern Datum interval_mi(PG_FUNCTION_ARGS);
286 extern Datum interval_mul(PG_FUNCTION_ARGS);
287 extern Datum mul_d_interval(PG_FUNCTION_ARGS);
288 extern Datum interval_div(PG_FUNCTION_ARGS);
289 extern Datum interval_accum(PG_FUNCTION_ARGS);
290 extern Datum interval_avg(PG_FUNCTION_ARGS);
292 extern Datum timestamp_mi(PG_FUNCTION_ARGS);
293 extern Datum timestamp_pl_interval(PG_FUNCTION_ARGS);
294 extern Datum timestamp_mi_interval(PG_FUNCTION_ARGS);
295 extern Datum timestamp_age(PG_FUNCTION_ARGS);
296 extern Datum overlaps_timestamp(PG_FUNCTION_ARGS);
298 extern Datum timestamptz_pl_interval(PG_FUNCTION_ARGS);
299 extern Datum timestamptz_mi_interval(PG_FUNCTION_ARGS);
300 extern Datum timestamptz_age(PG_FUNCTION_ARGS);
301 extern Datum timestamptz_trunc(PG_FUNCTION_ARGS);
302 extern Datum timestamptz_part(PG_FUNCTION_ARGS);
304 extern Datum now(PG_FUNCTION_ARGS);
305 extern Datum statement_timestamp(PG_FUNCTION_ARGS);
306 extern Datum clock_timestamp(PG_FUNCTION_ARGS);
308 extern Datum pg_postmaster_start_time(PG_FUNCTION_ARGS);
309 extern Datum pg_conf_load_time(PG_FUNCTION_ARGS);
311 extern Datum generate_series_timestamp(PG_FUNCTION_ARGS);
312 extern Datum generate_series_timestamptz(PG_FUNCTION_ARGS);
314 /* Internal routines (not fmgr-callable) */
316 extern TimestampTz GetCurrentTimestamp(void);
318 extern void TimestampDifference(TimestampTz start_time, TimestampTz stop_time,
319 long *secs, int *microsecs);
320 extern bool TimestampDifferenceExceeds(TimestampTz start_time,
321 TimestampTz stop_time,
322 int msec);
324 extern TimestampTz time_t_to_timestamptz(pg_time_t tm);
325 extern pg_time_t timestamptz_to_time_t(TimestampTz t);
327 extern const char *timestamptz_to_str(TimestampTz t);
329 extern int tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *dt);
330 extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm,
331 fsec_t *fsec, char **tzn, pg_tz *attimezone);
332 extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec);
334 extern int interval2tm(Interval span, struct pg_tm * tm, fsec_t *fsec);
335 extern int tm2interval(struct pg_tm * tm, fsec_t fsec, Interval *span);
337 extern Timestamp SetEpochTimestamp(void);
338 extern void GetEpochTime(struct pg_tm * tm);
340 extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
342 /* timestamp comparison works for timestamptz also */
343 #define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2)
345 extern int isoweek2j(int year, int week);
346 extern void isoweek2date(int woy, int *year, int *mon, int *mday);
347 extern void isoweekdate2date(int isoweek, int isowday, int *year, int *mon, int *mday);
348 extern int date2isoweek(int year, int mon, int mday);
349 extern int date2isoyear(int year, int mon, int mday);
350 extern int date2isoyearday(int year, int mon, int mday);
352 #endif /* TIMESTAMP_H */