1 /*-------------------------------------------------------------------------
4 * Definitions for the SQL92 "timestamp" and "interval" types.
6 * Portions Copyright (c) 1996-2005, 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.50 2005/07/21 15:16:27 momjian 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), days,
29 * and time separately since the elapsed time spanned is unknown until
30 * instantiated 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 days,
51 double time; /* all time units other than days,
54 int32 day; /* days, after time for alignment */
55 int32 month; /* months and years, after time for
60 #define MAX_TIMESTAMP_PRECISION 6
61 #define MAX_INTERVAL_PRECISION 6
63 /* in both timestamp.h and ecpg/dt.h */
64 #define DAYS_PER_YEAR 365.25 /* assumes leap year every four years */
65 #define MONTHS_PER_YEAR 12
67 * DAYS_PER_MONTH is very imprecise. The more accurate value is
68 * 365.25/12 = 30.4375, or '30 days 10:30:00'. Right now we only
69 * return an integral number of days, but someday perhaps we should
70 * also return a 'time' value to be used as well.
72 #define DAYS_PER_MONTH 30 /* assumes exactly 30 days per month */
73 #define HOURS_PER_DAY 24 /* assume no daylight savings time changes */
75 #define SECS_PER_DAY 86400 /* assumes no leap second */
76 #define SECS_PER_HOUR 3600
77 #define SECS_PER_MINUTE 60
79 #ifdef HAVE_INT64_TIMESTAMP
80 #define USECS_PER_DAY INT64CONST(86400000000)
81 #define USECS_PER_HOUR INT64CONST(3600000000)
82 #define USECS_PER_MINUTE INT64CONST(60000000)
83 #define USECS_PER_SEC INT64CONST(1000000)
87 * Macros for fmgr-callable functions.
89 * For Timestamp, we make use of the same support routines as for int64
90 * or float8. Therefore Timestamp is pass-by-reference if and only if
93 #ifdef HAVE_INT64_TIMESTAMP
95 #define DatumGetTimestamp(X) ((Timestamp) DatumGetInt64(X))
96 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetInt64(X))
97 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
99 #define TimestampGetDatum(X) Int64GetDatum(X)
100 #define TimestampTzGetDatum(X) Int64GetDatum(X)
101 #define IntervalPGetDatum(X) PointerGetDatum(X)
103 #define PG_GETARG_TIMESTAMP(n) PG_GETARG_INT64(n)
104 #define PG_GETARG_TIMESTAMPTZ(n) PG_GETARG_INT64(n)
105 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
107 #define PG_RETURN_TIMESTAMP(x) PG_RETURN_INT64(x)
108 #define PG_RETURN_TIMESTAMPTZ(x) PG_RETURN_INT64(x)
109 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
111 #define DT_NOBEGIN (-INT64CONST(0x7fffffffffffffff) - 1)
112 #define DT_NOEND (INT64CONST(0x7fffffffffffffff))
116 #define DatumGetTimestamp(X) ((Timestamp) DatumGetFloat8(X))
117 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetFloat8(X))
118 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
120 #define TimestampGetDatum(X) Float8GetDatum(X)
121 #define TimestampTzGetDatum(X) Float8GetDatum(X)
122 #define IntervalPGetDatum(X) PointerGetDatum(X)
124 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
125 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
126 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
128 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
129 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
130 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
133 #define DT_NOBEGIN (-HUGE_VAL)
134 #define DT_NOEND (HUGE_VAL)
136 #define DT_NOBEGIN (-DBL_MAX)
137 #define DT_NOEND (DBL_MAX)
139 #endif /* HAVE_INT64_TIMESTAMP */
142 #define TIMESTAMP_NOBEGIN(j) do {(j) = DT_NOBEGIN;} while (0)
143 #define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN)
145 #define TIMESTAMP_NOEND(j) do {(j) = DT_NOEND;} while (0)
146 #define TIMESTAMP_IS_NOEND(j) ((j) == DT_NOEND)
148 #define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
150 #ifdef HAVE_INT64_TIMESTAMP
152 typedef int32 fsec_t;
156 typedef double fsec_t;
158 #define TIME_PREC_INV 1000000.0
159 #define JROUND(j) (rint(((double) (j))*TIME_PREC_INV)/TIME_PREC_INV)
162 #define TIMESTAMP_MASK(b) (1 << (b))
163 #define INTERVAL_MASK(b) (1 << (b))
165 /* Macros to handle packing and unpacking the typmod field for intervals */
166 #define INTERVAL_FULL_RANGE (0x7FFF)
167 #define INTERVAL_RANGE_MASK (0x7FFF)
168 #define INTERVAL_FULL_PRECISION (0xFFFF)
169 #define INTERVAL_PRECISION_MASK (0xFFFF)
170 #define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
171 #define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
172 #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
175 /* Set at postmaster start */
176 extern TimestampTz PgStartTime;
180 * timestamp.c prototypes
183 extern Datum timestamp_in(PG_FUNCTION_ARGS);
184 extern Datum timestamp_out(PG_FUNCTION_ARGS);
185 extern Datum timestamp_recv(PG_FUNCTION_ARGS);
186 extern Datum timestamp_send(PG_FUNCTION_ARGS);
187 extern Datum timestamp_scale(PG_FUNCTION_ARGS);
188 extern Datum timestamp_eq(PG_FUNCTION_ARGS);
189 extern Datum timestamp_ne(PG_FUNCTION_ARGS);
190 extern Datum timestamp_lt(PG_FUNCTION_ARGS);
191 extern Datum timestamp_le(PG_FUNCTION_ARGS);
192 extern Datum timestamp_ge(PG_FUNCTION_ARGS);
193 extern Datum timestamp_gt(PG_FUNCTION_ARGS);
194 extern Datum timestamp_finite(PG_FUNCTION_ARGS);
195 extern Datum timestamp_cmp(PG_FUNCTION_ARGS);
196 extern Datum timestamp_smaller(PG_FUNCTION_ARGS);
197 extern Datum timestamp_larger(PG_FUNCTION_ARGS);
199 extern Datum timestamp_eq_timestamptz(PG_FUNCTION_ARGS);
200 extern Datum timestamp_ne_timestamptz(PG_FUNCTION_ARGS);
201 extern Datum timestamp_lt_timestamptz(PG_FUNCTION_ARGS);
202 extern Datum timestamp_le_timestamptz(PG_FUNCTION_ARGS);
203 extern Datum timestamp_gt_timestamptz(PG_FUNCTION_ARGS);
204 extern Datum timestamp_ge_timestamptz(PG_FUNCTION_ARGS);
205 extern Datum timestamp_cmp_timestamptz(PG_FUNCTION_ARGS);
207 extern Datum timestamptz_eq_timestamp(PG_FUNCTION_ARGS);
208 extern Datum timestamptz_ne_timestamp(PG_FUNCTION_ARGS);
209 extern Datum timestamptz_lt_timestamp(PG_FUNCTION_ARGS);
210 extern Datum timestamptz_le_timestamp(PG_FUNCTION_ARGS);
211 extern Datum timestamptz_gt_timestamp(PG_FUNCTION_ARGS);
212 extern Datum timestamptz_ge_timestamp(PG_FUNCTION_ARGS);
213 extern Datum timestamptz_cmp_timestamp(PG_FUNCTION_ARGS);
215 extern Datum interval_in(PG_FUNCTION_ARGS);
216 extern Datum interval_out(PG_FUNCTION_ARGS);
217 extern Datum interval_recv(PG_FUNCTION_ARGS);
218 extern Datum interval_send(PG_FUNCTION_ARGS);
219 extern Datum interval_scale(PG_FUNCTION_ARGS);
220 extern Datum interval_eq(PG_FUNCTION_ARGS);
221 extern Datum interval_ne(PG_FUNCTION_ARGS);
222 extern Datum interval_lt(PG_FUNCTION_ARGS);
223 extern Datum interval_le(PG_FUNCTION_ARGS);
224 extern Datum interval_ge(PG_FUNCTION_ARGS);
225 extern Datum interval_gt(PG_FUNCTION_ARGS);
226 extern Datum interval_finite(PG_FUNCTION_ARGS);
227 extern Datum interval_cmp(PG_FUNCTION_ARGS);
228 extern Datum interval_hash(PG_FUNCTION_ARGS);
229 extern Datum interval_smaller(PG_FUNCTION_ARGS);
230 extern Datum interval_larger(PG_FUNCTION_ARGS);
231 extern Datum interval_justify_hours(PG_FUNCTION_ARGS);
232 extern Datum interval_justify_days(PG_FUNCTION_ARGS);
234 extern Datum timestamp_text(PG_FUNCTION_ARGS);
235 extern Datum text_timestamp(PG_FUNCTION_ARGS);
236 extern Datum interval_text(PG_FUNCTION_ARGS);
237 extern Datum text_interval(PG_FUNCTION_ARGS);
238 extern Datum timestamp_trunc(PG_FUNCTION_ARGS);
239 extern Datum interval_trunc(PG_FUNCTION_ARGS);
240 extern Datum timestamp_part(PG_FUNCTION_ARGS);
241 extern Datum interval_part(PG_FUNCTION_ARGS);
242 extern Datum timestamp_zone(PG_FUNCTION_ARGS);
243 extern Datum timestamp_izone(PG_FUNCTION_ARGS);
244 extern Datum timestamp_timestamptz(PG_FUNCTION_ARGS);
246 extern Datum timestamptz_in(PG_FUNCTION_ARGS);
247 extern Datum timestamptz_out(PG_FUNCTION_ARGS);
248 extern Datum timestamptz_recv(PG_FUNCTION_ARGS);
249 extern Datum timestamptz_send(PG_FUNCTION_ARGS);
250 extern Datum timestamptz_scale(PG_FUNCTION_ARGS);
251 extern Datum timestamptz_timestamp(PG_FUNCTION_ARGS);
252 extern Datum timestamptz_zone(PG_FUNCTION_ARGS);
253 extern Datum timestamptz_izone(PG_FUNCTION_ARGS);
254 extern Datum timestamptz_timestamptz(PG_FUNCTION_ARGS);
256 extern Datum interval_um(PG_FUNCTION_ARGS);
257 extern Datum interval_pl(PG_FUNCTION_ARGS);
258 extern Datum interval_mi(PG_FUNCTION_ARGS);
259 extern Datum interval_mul(PG_FUNCTION_ARGS);
260 extern Datum mul_d_interval(PG_FUNCTION_ARGS);
261 extern Datum interval_div(PG_FUNCTION_ARGS);
262 extern Datum interval_accum(PG_FUNCTION_ARGS);
263 extern Datum interval_avg(PG_FUNCTION_ARGS);
265 extern Datum timestamp_mi(PG_FUNCTION_ARGS);
266 extern Datum timestamp_pl_interval(PG_FUNCTION_ARGS);
267 extern Datum timestamp_mi_interval(PG_FUNCTION_ARGS);
268 extern Datum timestamp_age(PG_FUNCTION_ARGS);
269 extern Datum overlaps_timestamp(PG_FUNCTION_ARGS);
271 extern Datum timestamptz_text(PG_FUNCTION_ARGS);
272 extern Datum text_timestamptz(PG_FUNCTION_ARGS);
273 extern Datum timestamptz_pl_interval(PG_FUNCTION_ARGS);
274 extern Datum timestamptz_mi_interval(PG_FUNCTION_ARGS);
275 extern Datum timestamptz_age(PG_FUNCTION_ARGS);
276 extern Datum timestamptz_trunc(PG_FUNCTION_ARGS);
277 extern Datum timestamptz_part(PG_FUNCTION_ARGS);
279 extern Datum now(PG_FUNCTION_ARGS);
281 extern Datum pgsql_postmaster_start_time(PG_FUNCTION_ARGS);
283 /* Internal routines (not fmgr-callable) */
285 extern TimestampTz GetCurrentTimestamp(void);
287 extern int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *dt);
288 extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm,
289 fsec_t *fsec, char **tzn, pg_tz *attimezone);
290 extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec);
292 extern int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec);
293 extern int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span);
295 extern Timestamp SetEpochTimestamp(void);
296 extern void GetEpochTime(struct pg_tm *tm);
298 extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
300 /* timestamp comparison works for timestamptz also */
301 #define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2)
303 extern void isoweek2date(int woy, int *year, int *mon, int *mday);
304 extern int date2isoweek(int year, int mon, int mday);
305 extern int date2isoyear(int year, int mon, int mday);
307 #endif /* TIMESTAMP_H */