1 /*-------------------------------------------------------------------------
4 * Definitions for the SQL92 "timestamp" and "interval" types.
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 *-------------------------------------------------------------------------
22 #ifdef HAVE_INT64_TIMESTAMP
23 #include "utils/int8.h"
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) */
52 typedef double Timestamp
;
53 typedef double TimestampTz
;
54 typedef double TimeOffset
;
55 typedef double fsec_t
; /* fractional seconds (in seconds) */
60 TimeOffset time
; /* all time units other than days, months and
62 int32 day
; /* days, after time for alignment */
63 int32 month
; /* months and years, after time for alignment */
67 #define MAX_TIMESTAMP_PRECISION 6
68 #define MAX_INTERVAL_PRECISION 6
70 /* in both timestamp.h and ecpg/dt.h */
71 #define DAYS_PER_YEAR 365.25 /* assumes leap year every four years */
72 #define MONTHS_PER_YEAR 12
74 * DAYS_PER_MONTH is very imprecise. The more accurate value is
75 * 365.2425/12 = 30.436875, or '30 days 10:29:06'. Right now we only
76 * return an integral number of days, but someday perhaps we should
77 * also return a 'time' value to be used as well. ISO 8601 suggests
80 #define DAYS_PER_MONTH 30 /* assumes exactly 30 days per month */
81 #define HOURS_PER_DAY 24 /* assume no daylight savings time changes */
84 * This doesn't adjust for uneven daylight savings time intervals or leap
85 * seconds, and it crudely estimates leap years. A more accurate value
86 * for days per years is 365.2422.
88 #define SECS_PER_YEAR (36525 * 864) /* avoid floating-point computation */
89 #define SECS_PER_DAY 86400
90 #define SECS_PER_HOUR 3600
91 #define SECS_PER_MINUTE 60
92 #define MINS_PER_HOUR 60
94 #ifdef HAVE_INT64_TIMESTAMP
95 #define USECS_PER_DAY INT64CONST(86400000000)
96 #define USECS_PER_HOUR INT64CONST(3600000000)
97 #define USECS_PER_MINUTE INT64CONST(60000000)
98 #define USECS_PER_SEC INT64CONST(1000000)
102 * Macros for fmgr-callable functions.
104 * For Timestamp, we make use of the same support routines as for int64
105 * or float8. Therefore Timestamp is pass-by-reference if and only if
106 * int64 or float8 is!
108 #ifdef HAVE_INT64_TIMESTAMP
110 #define DatumGetTimestamp(X) ((Timestamp) DatumGetInt64(X))
111 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetInt64(X))
112 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
114 #define TimestampGetDatum(X) Int64GetDatum(X)
115 #define TimestampTzGetDatum(X) Int64GetDatum(X)
116 #define IntervalPGetDatum(X) PointerGetDatum(X)
118 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
119 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
120 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
122 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
123 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
124 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
126 #define DT_NOBEGIN (-INT64CONST(0x7fffffffffffffff) - 1)
127 #define DT_NOEND (INT64CONST(0x7fffffffffffffff))
128 #else /* !HAVE_INT64_TIMESTAMP */
130 #define DatumGetTimestamp(X) ((Timestamp) DatumGetFloat8(X))
131 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetFloat8(X))
132 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
134 #define TimestampGetDatum(X) Float8GetDatum(X)
135 #define TimestampTzGetDatum(X) Float8GetDatum(X)
136 #define IntervalPGetDatum(X) PointerGetDatum(X)
138 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
139 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
140 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
142 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
143 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
144 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
147 #define DT_NOBEGIN (-HUGE_VAL)
148 #define DT_NOEND (HUGE_VAL)
150 #define DT_NOBEGIN (-DBL_MAX)
151 #define DT_NOEND (DBL_MAX)
153 #endif /* HAVE_INT64_TIMESTAMP */
156 #define TIMESTAMP_NOBEGIN(j) \
157 do {(j) = DT_NOBEGIN;} while (0)
158 #define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN)
160 #define TIMESTAMP_NOEND(j) \
161 do {(j) = DT_NOEND;} while (0)
162 #define TIMESTAMP_IS_NOEND(j) ((j) == DT_NOEND)
164 #define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
167 * Round off to MAX_TIMESTAMP_PRECISION decimal places.
168 * Note: this is also used for rounding off intervals.
170 #define TS_PREC_INV 1000000.0
171 #define TSROUND(j) (rint(((double) (j)) * TS_PREC_INV) / TS_PREC_INV)
173 #define TIMESTAMP_MASK(b) (1 << (b))
174 #define INTERVAL_MASK(b) (1 << (b))
176 /* Macros to handle packing and unpacking the typmod field for intervals */
177 #define INTERVAL_FULL_RANGE (0x7FFF)
178 #define INTERVAL_RANGE_MASK (0x7FFF)
179 #define INTERVAL_FULL_PRECISION (0xFFFF)
180 #define INTERVAL_PRECISION_MASK (0xFFFF)
181 #define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
182 #define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
183 #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
185 #ifdef HAVE_INT64_TIMESTAMP
186 #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * (int64) 1000))
188 #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) / 1000.0))
192 /* Set at postmaster start */
193 extern TimestampTz PgStartTime
;
195 /* Set at configuration reload */
196 extern TimestampTz PgReloadTime
;
200 * timestamp.c prototypes
203 extern Datum
timestamp_in(PG_FUNCTION_ARGS
);
204 extern Datum
timestamp_out(PG_FUNCTION_ARGS
);
205 extern Datum
timestamp_recv(PG_FUNCTION_ARGS
);
206 extern Datum
timestamp_send(PG_FUNCTION_ARGS
);
207 extern Datum
timestamptypmodin(PG_FUNCTION_ARGS
);
208 extern Datum
timestamptypmodout(PG_FUNCTION_ARGS
);
209 extern Datum
timestamp_scale(PG_FUNCTION_ARGS
);
210 extern Datum
timestamp_eq(PG_FUNCTION_ARGS
);
211 extern Datum
timestamp_ne(PG_FUNCTION_ARGS
);
212 extern Datum
timestamp_lt(PG_FUNCTION_ARGS
);
213 extern Datum
timestamp_le(PG_FUNCTION_ARGS
);
214 extern Datum
timestamp_ge(PG_FUNCTION_ARGS
);
215 extern Datum
timestamp_gt(PG_FUNCTION_ARGS
);
216 extern Datum
timestamp_finite(PG_FUNCTION_ARGS
);
217 extern Datum
timestamp_cmp(PG_FUNCTION_ARGS
);
218 extern Datum
timestamp_hash(PG_FUNCTION_ARGS
);
219 extern Datum
timestamp_smaller(PG_FUNCTION_ARGS
);
220 extern Datum
timestamp_larger(PG_FUNCTION_ARGS
);
222 extern Datum
timestamp_eq_timestamptz(PG_FUNCTION_ARGS
);
223 extern Datum
timestamp_ne_timestamptz(PG_FUNCTION_ARGS
);
224 extern Datum
timestamp_lt_timestamptz(PG_FUNCTION_ARGS
);
225 extern Datum
timestamp_le_timestamptz(PG_FUNCTION_ARGS
);
226 extern Datum
timestamp_gt_timestamptz(PG_FUNCTION_ARGS
);
227 extern Datum
timestamp_ge_timestamptz(PG_FUNCTION_ARGS
);
228 extern Datum
timestamp_cmp_timestamptz(PG_FUNCTION_ARGS
);
230 extern Datum
timestamptz_eq_timestamp(PG_FUNCTION_ARGS
);
231 extern Datum
timestamptz_ne_timestamp(PG_FUNCTION_ARGS
);
232 extern Datum
timestamptz_lt_timestamp(PG_FUNCTION_ARGS
);
233 extern Datum
timestamptz_le_timestamp(PG_FUNCTION_ARGS
);
234 extern Datum
timestamptz_gt_timestamp(PG_FUNCTION_ARGS
);
235 extern Datum
timestamptz_ge_timestamp(PG_FUNCTION_ARGS
);
236 extern Datum
timestamptz_cmp_timestamp(PG_FUNCTION_ARGS
);
238 extern Datum
interval_in(PG_FUNCTION_ARGS
);
239 extern Datum
interval_out(PG_FUNCTION_ARGS
);
240 extern Datum
interval_recv(PG_FUNCTION_ARGS
);
241 extern Datum
interval_send(PG_FUNCTION_ARGS
);
242 extern Datum
intervaltypmodin(PG_FUNCTION_ARGS
);
243 extern Datum
intervaltypmodout(PG_FUNCTION_ARGS
);
244 extern Datum
interval_scale(PG_FUNCTION_ARGS
);
245 extern Datum
interval_eq(PG_FUNCTION_ARGS
);
246 extern Datum
interval_ne(PG_FUNCTION_ARGS
);
247 extern Datum
interval_lt(PG_FUNCTION_ARGS
);
248 extern Datum
interval_le(PG_FUNCTION_ARGS
);
249 extern Datum
interval_ge(PG_FUNCTION_ARGS
);
250 extern Datum
interval_gt(PG_FUNCTION_ARGS
);
251 extern Datum
interval_finite(PG_FUNCTION_ARGS
);
252 extern Datum
interval_cmp(PG_FUNCTION_ARGS
);
253 extern Datum
interval_hash(PG_FUNCTION_ARGS
);
254 extern Datum
interval_smaller(PG_FUNCTION_ARGS
);
255 extern Datum
interval_larger(PG_FUNCTION_ARGS
);
256 extern Datum
interval_justify_interval(PG_FUNCTION_ARGS
);
257 extern Datum
interval_justify_hours(PG_FUNCTION_ARGS
);
258 extern Datum
interval_justify_days(PG_FUNCTION_ARGS
);
260 extern Datum
timestamp_trunc(PG_FUNCTION_ARGS
);
261 extern Datum
interval_trunc(PG_FUNCTION_ARGS
);
262 extern Datum
timestamp_part(PG_FUNCTION_ARGS
);
263 extern Datum
interval_part(PG_FUNCTION_ARGS
);
264 extern Datum
timestamp_zone(PG_FUNCTION_ARGS
);
265 extern Datum
timestamp_izone(PG_FUNCTION_ARGS
);
266 extern Datum
timestamp_timestamptz(PG_FUNCTION_ARGS
);
268 extern Datum
timestamptz_in(PG_FUNCTION_ARGS
);
269 extern Datum
timestamptz_out(PG_FUNCTION_ARGS
);
270 extern Datum
timestamptz_recv(PG_FUNCTION_ARGS
);
271 extern Datum
timestamptz_send(PG_FUNCTION_ARGS
);
272 extern Datum
timestamptztypmodin(PG_FUNCTION_ARGS
);
273 extern Datum
timestamptztypmodout(PG_FUNCTION_ARGS
);
274 extern Datum
timestamptz_scale(PG_FUNCTION_ARGS
);
275 extern Datum
timestamptz_timestamp(PG_FUNCTION_ARGS
);
276 extern Datum
timestamptz_zone(PG_FUNCTION_ARGS
);
277 extern Datum
timestamptz_izone(PG_FUNCTION_ARGS
);
278 extern Datum
timestamptz_timestamptz(PG_FUNCTION_ARGS
);
280 extern Datum
interval_um(PG_FUNCTION_ARGS
);
281 extern Datum
interval_pl(PG_FUNCTION_ARGS
);
282 extern Datum
interval_mi(PG_FUNCTION_ARGS
);
283 extern Datum
interval_mul(PG_FUNCTION_ARGS
);
284 extern Datum
mul_d_interval(PG_FUNCTION_ARGS
);
285 extern Datum
interval_div(PG_FUNCTION_ARGS
);
286 extern Datum
interval_accum(PG_FUNCTION_ARGS
);
287 extern Datum
interval_avg(PG_FUNCTION_ARGS
);
289 extern Datum
timestamp_mi(PG_FUNCTION_ARGS
);
290 extern Datum
timestamp_pl_interval(PG_FUNCTION_ARGS
);
291 extern Datum
timestamp_mi_interval(PG_FUNCTION_ARGS
);
292 extern Datum
timestamp_age(PG_FUNCTION_ARGS
);
293 extern Datum
overlaps_timestamp(PG_FUNCTION_ARGS
);
295 extern Datum
timestamptz_pl_interval(PG_FUNCTION_ARGS
);
296 extern Datum
timestamptz_mi_interval(PG_FUNCTION_ARGS
);
297 extern Datum
timestamptz_age(PG_FUNCTION_ARGS
);
298 extern Datum
timestamptz_trunc(PG_FUNCTION_ARGS
);
299 extern Datum
timestamptz_part(PG_FUNCTION_ARGS
);
301 extern Datum
now(PG_FUNCTION_ARGS
);
302 extern Datum
statement_timestamp(PG_FUNCTION_ARGS
);
303 extern Datum
clock_timestamp(PG_FUNCTION_ARGS
);
305 extern Datum
pg_postmaster_start_time(PG_FUNCTION_ARGS
);
306 extern Datum
pg_conf_load_time(PG_FUNCTION_ARGS
);
308 extern Datum
generate_series_timestamp(PG_FUNCTION_ARGS
);
309 extern Datum
generate_series_timestamptz(PG_FUNCTION_ARGS
);
311 /* Internal routines (not fmgr-callable) */
313 extern TimestampTz
GetCurrentTimestamp(void);
315 extern void TimestampDifference(TimestampTz start_time
, TimestampTz stop_time
,
316 long *secs
, int *microsecs
);
317 extern bool TimestampDifferenceExceeds(TimestampTz start_time
,
318 TimestampTz stop_time
,
321 extern TimestampTz
time_t_to_timestamptz(pg_time_t tm
);
322 extern pg_time_t
timestamptz_to_time_t(TimestampTz t
);
324 extern const char *timestamptz_to_str(TimestampTz t
);
326 extern int tm2timestamp(struct pg_tm
* tm
, fsec_t fsec
, int *tzp
, Timestamp
*dt
);
327 extern int timestamp2tm(Timestamp dt
, int *tzp
, struct pg_tm
* tm
,
328 fsec_t
*fsec
, char **tzn
, pg_tz
*attimezone
);
329 extern void dt2time(Timestamp dt
, int *hour
, int *min
, int *sec
, fsec_t
*fsec
);
331 extern int interval2tm(Interval span
, struct pg_tm
* tm
, fsec_t
*fsec
);
332 extern int tm2interval(struct pg_tm
* tm
, fsec_t fsec
, Interval
*span
);
334 extern Timestamp
SetEpochTimestamp(void);
335 extern void GetEpochTime(struct pg_tm
* tm
);
337 extern int timestamp_cmp_internal(Timestamp dt1
, Timestamp dt2
);
339 /* timestamp comparison works for timestamptz also */
340 #define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2)
342 extern int isoweek2j(int year
, int week
);
343 extern void isoweek2date(int woy
, int *year
, int *mon
, int *mday
);
344 extern void isoweekdate2date(int isoweek
, int isowday
, int *year
, int *mon
, int *mday
);
345 extern int date2isoweek(int year
, int mon
, int mday
);
346 extern int date2isoyear(int year
, int mon
, int mday
);
347 extern int date2isoyearday(int year
, int mon
, int mday
);
349 #endif /* TIMESTAMP_H */