]> granicus.if.org Git - postgresql/blob - src/include/utils/timestamp.h
Standard pgindent run for 8.1.
[postgresql] / src / include / utils / timestamp.h
1 /*-------------------------------------------------------------------------
2  *
3  * timestamp.h
4  *        Definitions for the SQL92 "timestamp" and "interval" types.
5  *
6  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.57 2005/10/15 02:49:46 momjian Exp $
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef TIMESTAMP_H
14 #define TIMESTAMP_H
15
16 #include <math.h>
17 #include <limits.h>
18 #include <float.h>
19
20 #include "fmgr.h"
21 #include "pgtime.h"
22 #ifdef HAVE_INT64_TIMESTAMP
23 #include "utils/int8.h"
24 #endif
25
26 /*
27  * Timestamp represents absolute time.
28  * Interval represents delta time. Keep track of months (and years), days,
29  *      and time separately since the elapsed time spanned is unknown until
30  *      instantiated relative to an absolute time.
31  *
32  * Note that Postgres uses "time interval" to mean a bounded interval,
33  * consisting of a beginning and ending time, not a time span - thomas 97/03/20
34  */
35
36 #ifdef HAVE_INT64_TIMESTAMP
37 typedef int64 Timestamp;
38 typedef int64 TimestampTz;
39 #else
40 typedef double Timestamp;
41 typedef double TimestampTz;
42 #endif
43
44 typedef struct
45 {
46 #ifdef HAVE_INT64_TIMESTAMP
47         int64           time;                   /* all time units other than days, months and
48                                                                  * years */
49 #else
50         double          time;                   /* all time units other than days, months and
51                                                                  * years */
52 #endif
53         int32           day;                    /* days, after time for alignment */
54         int32           month;                  /* months and years, after time for alignment */
55 } Interval;
56
57
58 #define MAX_TIMESTAMP_PRECISION 6
59 #define MAX_INTERVAL_PRECISION 6
60
61 /* in both timestamp.h and ecpg/dt.h */
62 #define DAYS_PER_YEAR   365.25  /* assumes leap year every four years */
63 #define MONTHS_PER_YEAR 12
64 /*
65  *      DAYS_PER_MONTH is very imprecise.  The more accurate value is
66  *      365.2425/12 = 30.436875, or '30 days 10:29:06'.  Right now we only
67  *      return an integral number of days, but someday perhaps we should
68  *      also return a 'time' value to be used as well.  ISO 8601 suggests
69  *      30 days.
70  */
71 #define DAYS_PER_MONTH  30              /* assumes exactly 30 days per month */
72 #define HOURS_PER_DAY   24              /* assume no daylight savings time changes */
73
74 /*
75  *      This doesn't adjust for uneven daylight savings time intervals or leap
76  *      seconds, and it crudely estimates leap years.  A more accurate value
77  *      for days per years is 365.2422.
78  */
79 #define SECS_PER_YEAR   (36525 * 864)   /* avoid floating-point computation */
80 #define SECS_PER_DAY    86400
81 #define SECS_PER_HOUR   3600
82 #define SECS_PER_MINUTE 60
83 #define MINS_PER_HOUR   60
84
85 #ifdef HAVE_INT64_TIMESTAMP
86 #define USECS_PER_DAY   INT64CONST(86400000000)
87 #define USECS_PER_HOUR  INT64CONST(3600000000)
88 #define USECS_PER_MINUTE INT64CONST(60000000)
89 #define USECS_PER_SEC   INT64CONST(1000000)
90 #endif
91
92 /*
93  * Macros for fmgr-callable functions.
94  *
95  * For Timestamp, we make use of the same support routines as for int64
96  * or float8.  Therefore Timestamp is pass-by-reference if and only if
97  * int64 or float8 is!
98  */
99 #ifdef HAVE_INT64_TIMESTAMP
100
101 #define DatumGetTimestamp(X)  ((Timestamp) DatumGetInt64(X))
102 #define DatumGetTimestampTz(X)  ((TimestampTz) DatumGetInt64(X))
103 #define DatumGetIntervalP(X)  ((Interval *) DatumGetPointer(X))
104
105 #define TimestampGetDatum(X) Int64GetDatum(X)
106 #define TimestampTzGetDatum(X) Int64GetDatum(X)
107 #define IntervalPGetDatum(X) PointerGetDatum(X)
108
109 #define PG_GETARG_TIMESTAMP(n) PG_GETARG_INT64(n)
110 #define PG_GETARG_TIMESTAMPTZ(n) PG_GETARG_INT64(n)
111 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
112
113 #define PG_RETURN_TIMESTAMP(x) PG_RETURN_INT64(x)
114 #define PG_RETURN_TIMESTAMPTZ(x) PG_RETURN_INT64(x)
115 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
116
117 #define DT_NOBEGIN              (-INT64CONST(0x7fffffffffffffff) - 1)
118 #define DT_NOEND                (INT64CONST(0x7fffffffffffffff))
119 #else
120
121 #define DatumGetTimestamp(X)  ((Timestamp) DatumGetFloat8(X))
122 #define DatumGetTimestampTz(X)  ((TimestampTz) DatumGetFloat8(X))
123 #define DatumGetIntervalP(X)  ((Interval *) DatumGetPointer(X))
124
125 #define TimestampGetDatum(X) Float8GetDatum(X)
126 #define TimestampTzGetDatum(X) Float8GetDatum(X)
127 #define IntervalPGetDatum(X) PointerGetDatum(X)
128
129 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
130 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
131 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
132
133 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
134 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
135 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
136
137 #ifdef HUGE_VAL
138 #define DT_NOBEGIN              (-HUGE_VAL)
139 #define DT_NOEND                (HUGE_VAL)
140 #else
141 #define DT_NOBEGIN              (-DBL_MAX)
142 #define DT_NOEND                (DBL_MAX)
143 #endif
144 #endif   /* HAVE_INT64_TIMESTAMP */
145
146
147 #define TIMESTAMP_NOBEGIN(j)    do {(j) = DT_NOBEGIN;} while (0)
148 #define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN)
149
150 #define TIMESTAMP_NOEND(j)              do {(j) = DT_NOEND;} while (0)
151 #define TIMESTAMP_IS_NOEND(j)   ((j) == DT_NOEND)
152
153 #define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
154
155 #ifdef HAVE_INT64_TIMESTAMP
156
157 typedef int32 fsec_t;
158 #else
159
160 typedef double fsec_t;
161
162 /* round off to MAX_TIMESTAMP_PRECISION decimal places */
163 /* note: this is also used for rounding off intervals */
164 #define TS_PREC_INV 1000000.0
165 #define TSROUND(j) (rint(((double) (j)) * TS_PREC_INV) / TS_PREC_INV)
166 #endif
167
168 #define TIMESTAMP_MASK(b) (1 << (b))
169 #define INTERVAL_MASK(b) (1 << (b))
170
171 /* Macros to handle packing and unpacking the typmod field for intervals */
172 #define INTERVAL_FULL_RANGE (0x7FFF)
173 #define INTERVAL_RANGE_MASK (0x7FFF)
174 #define INTERVAL_FULL_PRECISION (0xFFFF)
175 #define INTERVAL_PRECISION_MASK (0xFFFF)
176 #define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
177 #define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
178 #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
179
180
181 /* Set at postmaster start */
182 extern TimestampTz PgStartTime;
183
184
185 /*
186  * timestamp.c prototypes
187  */
188
189 extern Datum timestamp_in(PG_FUNCTION_ARGS);
190 extern Datum timestamp_out(PG_FUNCTION_ARGS);
191 extern Datum timestamp_recv(PG_FUNCTION_ARGS);
192 extern Datum timestamp_send(PG_FUNCTION_ARGS);
193 extern Datum timestamp_scale(PG_FUNCTION_ARGS);
194 extern Datum timestamp_eq(PG_FUNCTION_ARGS);
195 extern Datum timestamp_ne(PG_FUNCTION_ARGS);
196 extern Datum timestamp_lt(PG_FUNCTION_ARGS);
197 extern Datum timestamp_le(PG_FUNCTION_ARGS);
198 extern Datum timestamp_ge(PG_FUNCTION_ARGS);
199 extern Datum timestamp_gt(PG_FUNCTION_ARGS);
200 extern Datum timestamp_finite(PG_FUNCTION_ARGS);
201 extern Datum timestamp_cmp(PG_FUNCTION_ARGS);
202 extern Datum timestamp_smaller(PG_FUNCTION_ARGS);
203 extern Datum timestamp_larger(PG_FUNCTION_ARGS);
204
205 extern Datum timestamp_eq_timestamptz(PG_FUNCTION_ARGS);
206 extern Datum timestamp_ne_timestamptz(PG_FUNCTION_ARGS);
207 extern Datum timestamp_lt_timestamptz(PG_FUNCTION_ARGS);
208 extern Datum timestamp_le_timestamptz(PG_FUNCTION_ARGS);
209 extern Datum timestamp_gt_timestamptz(PG_FUNCTION_ARGS);
210 extern Datum timestamp_ge_timestamptz(PG_FUNCTION_ARGS);
211 extern Datum timestamp_cmp_timestamptz(PG_FUNCTION_ARGS);
212
213 extern Datum timestamptz_eq_timestamp(PG_FUNCTION_ARGS);
214 extern Datum timestamptz_ne_timestamp(PG_FUNCTION_ARGS);
215 extern Datum timestamptz_lt_timestamp(PG_FUNCTION_ARGS);
216 extern Datum timestamptz_le_timestamp(PG_FUNCTION_ARGS);
217 extern Datum timestamptz_gt_timestamp(PG_FUNCTION_ARGS);
218 extern Datum timestamptz_ge_timestamp(PG_FUNCTION_ARGS);
219 extern Datum timestamptz_cmp_timestamp(PG_FUNCTION_ARGS);
220
221 extern Datum interval_in(PG_FUNCTION_ARGS);
222 extern Datum interval_out(PG_FUNCTION_ARGS);
223 extern Datum interval_recv(PG_FUNCTION_ARGS);
224 extern Datum interval_send(PG_FUNCTION_ARGS);
225 extern Datum interval_scale(PG_FUNCTION_ARGS);
226 extern Datum interval_eq(PG_FUNCTION_ARGS);
227 extern Datum interval_ne(PG_FUNCTION_ARGS);
228 extern Datum interval_lt(PG_FUNCTION_ARGS);
229 extern Datum interval_le(PG_FUNCTION_ARGS);
230 extern Datum interval_ge(PG_FUNCTION_ARGS);
231 extern Datum interval_gt(PG_FUNCTION_ARGS);
232 extern Datum interval_finite(PG_FUNCTION_ARGS);
233 extern Datum interval_cmp(PG_FUNCTION_ARGS);
234 extern Datum interval_hash(PG_FUNCTION_ARGS);
235 extern Datum interval_smaller(PG_FUNCTION_ARGS);
236 extern Datum interval_larger(PG_FUNCTION_ARGS);
237 extern Datum interval_justify_hours(PG_FUNCTION_ARGS);
238 extern Datum interval_justify_days(PG_FUNCTION_ARGS);
239
240 extern Datum timestamp_text(PG_FUNCTION_ARGS);
241 extern Datum text_timestamp(PG_FUNCTION_ARGS);
242 extern Datum interval_text(PG_FUNCTION_ARGS);
243 extern Datum text_interval(PG_FUNCTION_ARGS);
244 extern Datum timestamp_trunc(PG_FUNCTION_ARGS);
245 extern Datum interval_trunc(PG_FUNCTION_ARGS);
246 extern Datum timestamp_part(PG_FUNCTION_ARGS);
247 extern Datum interval_part(PG_FUNCTION_ARGS);
248 extern Datum timestamp_zone(PG_FUNCTION_ARGS);
249 extern Datum timestamp_izone(PG_FUNCTION_ARGS);
250 extern Datum timestamp_timestamptz(PG_FUNCTION_ARGS);
251
252 extern Datum timestamptz_in(PG_FUNCTION_ARGS);
253 extern Datum timestamptz_out(PG_FUNCTION_ARGS);
254 extern Datum timestamptz_recv(PG_FUNCTION_ARGS);
255 extern Datum timestamptz_send(PG_FUNCTION_ARGS);
256 extern Datum timestamptz_scale(PG_FUNCTION_ARGS);
257 extern Datum timestamptz_timestamp(PG_FUNCTION_ARGS);
258 extern Datum timestamptz_zone(PG_FUNCTION_ARGS);
259 extern Datum timestamptz_izone(PG_FUNCTION_ARGS);
260 extern Datum timestamptz_timestamptz(PG_FUNCTION_ARGS);
261
262 extern Datum interval_um(PG_FUNCTION_ARGS);
263 extern Datum interval_pl(PG_FUNCTION_ARGS);
264 extern Datum interval_mi(PG_FUNCTION_ARGS);
265 extern Datum interval_mul(PG_FUNCTION_ARGS);
266 extern Datum mul_d_interval(PG_FUNCTION_ARGS);
267 extern Datum interval_div(PG_FUNCTION_ARGS);
268 extern Datum interval_accum(PG_FUNCTION_ARGS);
269 extern Datum interval_avg(PG_FUNCTION_ARGS);
270
271 extern Datum timestamp_mi(PG_FUNCTION_ARGS);
272 extern Datum timestamp_pl_interval(PG_FUNCTION_ARGS);
273 extern Datum timestamp_mi_interval(PG_FUNCTION_ARGS);
274 extern Datum timestamp_age(PG_FUNCTION_ARGS);
275 extern Datum overlaps_timestamp(PG_FUNCTION_ARGS);
276
277 extern Datum timestamptz_text(PG_FUNCTION_ARGS);
278 extern Datum text_timestamptz(PG_FUNCTION_ARGS);
279 extern Datum timestamptz_pl_interval(PG_FUNCTION_ARGS);
280 extern Datum timestamptz_mi_interval(PG_FUNCTION_ARGS);
281 extern Datum timestamptz_age(PG_FUNCTION_ARGS);
282 extern Datum timestamptz_trunc(PG_FUNCTION_ARGS);
283 extern Datum timestamptz_part(PG_FUNCTION_ARGS);
284
285 extern Datum now(PG_FUNCTION_ARGS);
286
287 extern Datum pgsql_postmaster_start_time(PG_FUNCTION_ARGS);
288
289 /* Internal routines (not fmgr-callable) */
290
291 extern TimestampTz GetCurrentTimestamp(void);
292
293 extern TimestampTz time_t_to_timestamptz(time_t tm);
294
295 extern int      tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *dt);
296 extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm,
297                          fsec_t *fsec, char **tzn, pg_tz *attimezone);
298 extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec);
299
300 extern int      interval2tm(Interval span, struct pg_tm * tm, fsec_t *fsec);
301 extern int      tm2interval(struct pg_tm * tm, fsec_t fsec, Interval *span);
302
303 extern Timestamp SetEpochTimestamp(void);
304 extern void GetEpochTime(struct pg_tm * tm);
305
306 extern int      timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
307
308 /* timestamp comparison works for timestamptz also */
309 #define timestamptz_cmp_internal(dt1,dt2)       timestamp_cmp_internal(dt1, dt2)
310
311 extern void isoweek2date(int woy, int *year, int *mon, int *mday);
312 extern int      date2isoweek(int year, int mon, int mday);
313 extern int      date2isoyear(int year, int mon, int mday);
314
315 #endif   /* TIMESTAMP_H */