1 /*-------------------------------------------------------------------------
4 * Definitions for the SQL92 "timestamp" and "interval" types.
6 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.37 2004/05/21 05:08:05 tgl Exp $
11 *-------------------------------------------------------------------------
22 #ifdef HAVE_INT64_TIMESTAMP
23 #include "utils/int8.h"
27 * Timestamp represents absolute time.
28 * Interval represents delta time. Keep track of months (and years)
29 * separately since the elapsed time spanned is unknown until instantiated
30 * relative to an absolute time.
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
36 #ifdef HAVE_INT64_TIMESTAMP
37 typedef int64 Timestamp;
38 typedef int64 TimestampTz;
41 typedef double Timestamp;
42 typedef double TimestampTz;
47 #ifdef HAVE_INT64_TIMESTAMP
48 int64 time; /* all time units other than months and
51 double time; /* all time units other than months and
54 int32 month; /* months and years, after time for
59 #define MAX_TIMESTAMP_PRECISION 6
60 #define MAX_INTERVAL_PRECISION 6
64 * Macros for fmgr-callable functions.
66 * For Timestamp, we make use of the same support routines as for int64
67 * or float8. Therefore Timestamp is pass-by-reference if and only if
70 #ifdef HAVE_INT64_TIMESTAMP
72 #define DatumGetTimestamp(X) ((Timestamp) DatumGetInt64(X))
73 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetInt64(X))
74 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
76 #define TimestampGetDatum(X) Int64GetDatum(X)
77 #define TimestampTzGetDatum(X) Int64GetDatum(X)
78 #define IntervalPGetDatum(X) PointerGetDatum(X)
80 #define PG_GETARG_TIMESTAMP(n) PG_GETARG_INT64(n)
81 #define PG_GETARG_TIMESTAMPTZ(n) PG_GETARG_INT64(n)
82 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
84 #define PG_RETURN_TIMESTAMP(x) PG_RETURN_INT64(x)
85 #define PG_RETURN_TIMESTAMPTZ(x) PG_RETURN_INT64(x)
86 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
88 #define DT_NOBEGIN (-INT64CONST(0x7fffffffffffffff) - 1)
89 #define DT_NOEND (INT64CONST(0x7fffffffffffffff))
93 #define DatumGetTimestamp(X) ((Timestamp) DatumGetFloat8(X))
94 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetFloat8(X))
95 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
97 #define TimestampGetDatum(X) Float8GetDatum(X)
98 #define TimestampTzGetDatum(X) Float8GetDatum(X)
99 #define IntervalPGetDatum(X) PointerGetDatum(X)
101 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
102 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
103 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
105 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
106 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
107 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
110 #define DT_NOBEGIN (-HUGE_VAL)
111 #define DT_NOEND (HUGE_VAL)
113 #define DT_NOBEGIN (-DBL_MAX)
114 #define DT_NOEND (DBL_MAX)
116 #endif /* HAVE_INT64_TIMESTAMP */
119 #define TIMESTAMP_NOBEGIN(j) do {j = DT_NOBEGIN;} while (0)
120 #define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN)
122 #define TIMESTAMP_NOEND(j) do {j = DT_NOEND;} while (0)
123 #define TIMESTAMP_IS_NOEND(j) ((j) == DT_NOEND)
125 #define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
127 #ifdef HAVE_INT64_TIMESTAMP
129 typedef int32 fsec_t;
133 typedef double fsec_t;
135 #define TIME_PREC_INV 1000000.0
136 #define JROUND(j) (rint(((double) (j))*TIME_PREC_INV)/TIME_PREC_INV)
139 #define TIMESTAMP_MASK(b) (1 << (b))
140 #define INTERVAL_MASK(b) (1 << (b))
142 /* Macros to handle packing and unpacking the typmod field for intervals */
143 #define INTERVAL_FULL_RANGE (0x7FFF)
144 #define INTERVAL_RANGE_MASK (0x7FFF)
145 #define INTERVAL_FULL_PRECISION (0xFFFF)
146 #define INTERVAL_PRECISION_MASK (0xFFFF)
147 #define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
148 #define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
149 #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
153 * timestamp.c prototypes
156 extern Datum timestamp_in(PG_FUNCTION_ARGS);
157 extern Datum timestamp_out(PG_FUNCTION_ARGS);
158 extern Datum timestamp_recv(PG_FUNCTION_ARGS);
159 extern Datum timestamp_send(PG_FUNCTION_ARGS);
160 extern Datum timestamp_scale(PG_FUNCTION_ARGS);
161 extern Datum timestamp_eq(PG_FUNCTION_ARGS);
162 extern Datum timestamp_ne(PG_FUNCTION_ARGS);
163 extern Datum timestamp_lt(PG_FUNCTION_ARGS);
164 extern Datum timestamp_le(PG_FUNCTION_ARGS);
165 extern Datum timestamp_ge(PG_FUNCTION_ARGS);
166 extern Datum timestamp_gt(PG_FUNCTION_ARGS);
167 extern Datum timestamp_finite(PG_FUNCTION_ARGS);
168 extern Datum timestamp_cmp(PG_FUNCTION_ARGS);
169 extern Datum timestamp_smaller(PG_FUNCTION_ARGS);
170 extern Datum timestamp_larger(PG_FUNCTION_ARGS);
172 extern Datum timestamp_eq_timestamptz(PG_FUNCTION_ARGS);
173 extern Datum timestamp_ne_timestamptz(PG_FUNCTION_ARGS);
174 extern Datum timestamp_lt_timestamptz(PG_FUNCTION_ARGS);
175 extern Datum timestamp_le_timestamptz(PG_FUNCTION_ARGS);
176 extern Datum timestamp_gt_timestamptz(PG_FUNCTION_ARGS);
177 extern Datum timestamp_ge_timestamptz(PG_FUNCTION_ARGS);
178 extern Datum timestamp_cmp_timestamptz(PG_FUNCTION_ARGS);
180 extern Datum timestamptz_eq_timestamp(PG_FUNCTION_ARGS);
181 extern Datum timestamptz_ne_timestamp(PG_FUNCTION_ARGS);
182 extern Datum timestamptz_lt_timestamp(PG_FUNCTION_ARGS);
183 extern Datum timestamptz_le_timestamp(PG_FUNCTION_ARGS);
184 extern Datum timestamptz_gt_timestamp(PG_FUNCTION_ARGS);
185 extern Datum timestamptz_ge_timestamp(PG_FUNCTION_ARGS);
186 extern Datum timestamptz_cmp_timestamp(PG_FUNCTION_ARGS);
188 extern Datum interval_in(PG_FUNCTION_ARGS);
189 extern Datum interval_out(PG_FUNCTION_ARGS);
190 extern Datum interval_recv(PG_FUNCTION_ARGS);
191 extern Datum interval_send(PG_FUNCTION_ARGS);
192 extern Datum interval_scale(PG_FUNCTION_ARGS);
193 extern Datum interval_eq(PG_FUNCTION_ARGS);
194 extern Datum interval_ne(PG_FUNCTION_ARGS);
195 extern Datum interval_lt(PG_FUNCTION_ARGS);
196 extern Datum interval_le(PG_FUNCTION_ARGS);
197 extern Datum interval_ge(PG_FUNCTION_ARGS);
198 extern Datum interval_gt(PG_FUNCTION_ARGS);
199 extern Datum interval_finite(PG_FUNCTION_ARGS);
200 extern Datum interval_cmp(PG_FUNCTION_ARGS);
201 extern Datum interval_hash(PG_FUNCTION_ARGS);
202 extern Datum interval_smaller(PG_FUNCTION_ARGS);
203 extern Datum interval_larger(PG_FUNCTION_ARGS);
205 extern Datum timestamp_text(PG_FUNCTION_ARGS);
206 extern Datum text_timestamp(PG_FUNCTION_ARGS);
207 extern Datum interval_text(PG_FUNCTION_ARGS);
208 extern Datum text_interval(PG_FUNCTION_ARGS);
209 extern Datum timestamp_trunc(PG_FUNCTION_ARGS);
210 extern Datum interval_trunc(PG_FUNCTION_ARGS);
211 extern Datum timestamp_part(PG_FUNCTION_ARGS);
212 extern Datum interval_part(PG_FUNCTION_ARGS);
213 extern Datum timestamp_zone(PG_FUNCTION_ARGS);
214 extern Datum timestamp_izone(PG_FUNCTION_ARGS);
215 extern Datum timestamp_timestamptz(PG_FUNCTION_ARGS);
217 extern Datum timestamptz_in(PG_FUNCTION_ARGS);
218 extern Datum timestamptz_out(PG_FUNCTION_ARGS);
219 extern Datum timestamptz_recv(PG_FUNCTION_ARGS);
220 extern Datum timestamptz_send(PG_FUNCTION_ARGS);
221 extern Datum timestamptz_scale(PG_FUNCTION_ARGS);
222 extern Datum timestamptz_timestamp(PG_FUNCTION_ARGS);
223 extern Datum timestamptz_zone(PG_FUNCTION_ARGS);
224 extern Datum timestamptz_izone(PG_FUNCTION_ARGS);
225 extern Datum timestamptz_timestamptz(PG_FUNCTION_ARGS);
227 extern Datum interval_um(PG_FUNCTION_ARGS);
228 extern Datum interval_pl(PG_FUNCTION_ARGS);
229 extern Datum interval_mi(PG_FUNCTION_ARGS);
230 extern Datum interval_mul(PG_FUNCTION_ARGS);
231 extern Datum mul_d_interval(PG_FUNCTION_ARGS);
232 extern Datum interval_div(PG_FUNCTION_ARGS);
233 extern Datum interval_accum(PG_FUNCTION_ARGS);
234 extern Datum interval_avg(PG_FUNCTION_ARGS);
236 extern Datum timestamp_mi(PG_FUNCTION_ARGS);
237 extern Datum timestamp_pl_interval(PG_FUNCTION_ARGS);
238 extern Datum timestamp_mi_interval(PG_FUNCTION_ARGS);
239 extern Datum timestamp_age(PG_FUNCTION_ARGS);
240 extern Datum overlaps_timestamp(PG_FUNCTION_ARGS);
242 extern Datum timestamptz_text(PG_FUNCTION_ARGS);
243 extern Datum text_timestamptz(PG_FUNCTION_ARGS);
244 extern Datum timestamptz_pl_interval(PG_FUNCTION_ARGS);
245 extern Datum timestamptz_mi_interval(PG_FUNCTION_ARGS);
246 extern Datum timestamptz_age(PG_FUNCTION_ARGS);
247 extern Datum timestamptz_trunc(PG_FUNCTION_ARGS);
248 extern Datum timestamptz_part(PG_FUNCTION_ARGS);
250 extern Datum now(PG_FUNCTION_ARGS);
252 /* Internal routines (not fmgr-callable) */
254 extern int tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *dt);
255 extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm,
256 fsec_t *fsec, char **tzn);
257 extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec);
259 extern int interval2tm(Interval span, struct pg_tm * tm, fsec_t *fsec);
260 extern int tm2interval(struct pg_tm * tm, fsec_t fsec, Interval *span);
262 extern Timestamp SetEpochTimestamp(void);
263 extern void GetEpochTime(struct pg_tm * tm);
265 extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
266 /* timestamp comparison works for timestamptz also */
267 #define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2)
269 extern void isoweek2date(int woy, int *year, int *mon, int *mday);
270 extern int date2isoweek(int year, int mon, int mday);
271 extern int date2isoyear(int year, int mon, int mday);
273 #endif /* TIMESTAMP_H */