1 /*-------------------------------------------------------------------------
4 * Definitions for the SQL "timestamp" and "interval" types.
6 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * src/include/utils/timestamp.h
11 *-------------------------------------------------------------------------
16 #include "datatype/timestamp.h"
22 * Macros for fmgr-callable functions.
24 * For Timestamp, we make use of the same support routines as for int64
25 * or float8. Therefore Timestamp is pass-by-reference if and only if
28 #ifdef HAVE_INT64_TIMESTAMP
30 #define DatumGetTimestamp(X) ((Timestamp) DatumGetInt64(X))
31 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetInt64(X))
32 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
34 #define TimestampGetDatum(X) Int64GetDatum(X)
35 #define TimestampTzGetDatum(X) Int64GetDatum(X)
36 #define IntervalPGetDatum(X) PointerGetDatum(X)
38 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
39 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
40 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
42 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
43 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
44 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
45 #else /* !HAVE_INT64_TIMESTAMP */
47 #define DatumGetTimestamp(X) ((Timestamp) DatumGetFloat8(X))
48 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetFloat8(X))
49 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
51 #define TimestampGetDatum(X) Float8GetDatum(X)
52 #define TimestampTzGetDatum(X) Float8GetDatum(X)
53 #define IntervalPGetDatum(X) PointerGetDatum(X)
55 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
56 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
57 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
59 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
60 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
61 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
62 #endif /* HAVE_INT64_TIMESTAMP */
65 #define TIMESTAMP_MASK(b) (1 << (b))
66 #define INTERVAL_MASK(b) (1 << (b))
68 /* Macros to handle packing and unpacking the typmod field for intervals */
69 #define INTERVAL_FULL_RANGE (0x7FFF)
70 #define INTERVAL_RANGE_MASK (0x7FFF)
71 #define INTERVAL_FULL_PRECISION (0xFFFF)
72 #define INTERVAL_PRECISION_MASK (0xFFFF)
73 #define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
74 #define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
75 #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
77 #ifdef HAVE_INT64_TIMESTAMP
78 #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * (int64) 1000))
80 #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) / 1000.0))
84 /* Set at postmaster start */
85 extern TimestampTz PgStartTime;
87 /* Set at configuration reload */
88 extern TimestampTz PgReloadTime;
92 * timestamp.c prototypes
95 extern Datum timestamp_in(PG_FUNCTION_ARGS);
96 extern Datum timestamp_out(PG_FUNCTION_ARGS);
97 extern Datum timestamp_recv(PG_FUNCTION_ARGS);
98 extern Datum timestamp_send(PG_FUNCTION_ARGS);
99 extern Datum timestamptypmodin(PG_FUNCTION_ARGS);
100 extern Datum timestamptypmodout(PG_FUNCTION_ARGS);
101 extern Datum timestamp_transform(PG_FUNCTION_ARGS);
102 extern Datum timestamp_scale(PG_FUNCTION_ARGS);
103 extern Datum timestamp_eq(PG_FUNCTION_ARGS);
104 extern Datum timestamp_ne(PG_FUNCTION_ARGS);
105 extern Datum timestamp_lt(PG_FUNCTION_ARGS);
106 extern Datum timestamp_le(PG_FUNCTION_ARGS);
107 extern Datum timestamp_ge(PG_FUNCTION_ARGS);
108 extern Datum timestamp_gt(PG_FUNCTION_ARGS);
109 extern Datum timestamp_finite(PG_FUNCTION_ARGS);
110 extern Datum timestamp_cmp(PG_FUNCTION_ARGS);
111 extern Datum timestamp_sortsupport(PG_FUNCTION_ARGS);
112 extern Datum timestamp_hash(PG_FUNCTION_ARGS);
113 extern Datum timestamp_smaller(PG_FUNCTION_ARGS);
114 extern Datum timestamp_larger(PG_FUNCTION_ARGS);
116 extern Datum timestamp_eq_timestamptz(PG_FUNCTION_ARGS);
117 extern Datum timestamp_ne_timestamptz(PG_FUNCTION_ARGS);
118 extern Datum timestamp_lt_timestamptz(PG_FUNCTION_ARGS);
119 extern Datum timestamp_le_timestamptz(PG_FUNCTION_ARGS);
120 extern Datum timestamp_gt_timestamptz(PG_FUNCTION_ARGS);
121 extern Datum timestamp_ge_timestamptz(PG_FUNCTION_ARGS);
122 extern Datum timestamp_cmp_timestamptz(PG_FUNCTION_ARGS);
124 extern Datum make_timestamp(PG_FUNCTION_ARGS);
125 extern Datum make_timestamptz(PG_FUNCTION_ARGS);
126 extern Datum make_timestamptz_at_timezone(PG_FUNCTION_ARGS);
128 extern Datum timestamptz_eq_timestamp(PG_FUNCTION_ARGS);
129 extern Datum timestamptz_ne_timestamp(PG_FUNCTION_ARGS);
130 extern Datum timestamptz_lt_timestamp(PG_FUNCTION_ARGS);
131 extern Datum timestamptz_le_timestamp(PG_FUNCTION_ARGS);
132 extern Datum timestamptz_gt_timestamp(PG_FUNCTION_ARGS);
133 extern Datum timestamptz_ge_timestamp(PG_FUNCTION_ARGS);
134 extern Datum timestamptz_cmp_timestamp(PG_FUNCTION_ARGS);
136 extern Datum interval_in(PG_FUNCTION_ARGS);
137 extern Datum interval_out(PG_FUNCTION_ARGS);
138 extern Datum interval_recv(PG_FUNCTION_ARGS);
139 extern Datum interval_send(PG_FUNCTION_ARGS);
140 extern Datum intervaltypmodin(PG_FUNCTION_ARGS);
141 extern Datum intervaltypmodout(PG_FUNCTION_ARGS);
142 extern Datum interval_transform(PG_FUNCTION_ARGS);
143 extern Datum interval_scale(PG_FUNCTION_ARGS);
144 extern Datum interval_eq(PG_FUNCTION_ARGS);
145 extern Datum interval_ne(PG_FUNCTION_ARGS);
146 extern Datum interval_lt(PG_FUNCTION_ARGS);
147 extern Datum interval_le(PG_FUNCTION_ARGS);
148 extern Datum interval_ge(PG_FUNCTION_ARGS);
149 extern Datum interval_gt(PG_FUNCTION_ARGS);
150 extern Datum interval_finite(PG_FUNCTION_ARGS);
151 extern Datum interval_cmp(PG_FUNCTION_ARGS);
152 extern Datum interval_hash(PG_FUNCTION_ARGS);
153 extern Datum interval_smaller(PG_FUNCTION_ARGS);
154 extern Datum interval_larger(PG_FUNCTION_ARGS);
155 extern Datum interval_justify_interval(PG_FUNCTION_ARGS);
156 extern Datum interval_justify_hours(PG_FUNCTION_ARGS);
157 extern Datum interval_justify_days(PG_FUNCTION_ARGS);
158 extern Datum make_interval(PG_FUNCTION_ARGS);
160 extern Datum timestamp_trunc(PG_FUNCTION_ARGS);
161 extern Datum interval_trunc(PG_FUNCTION_ARGS);
162 extern Datum timestamp_part(PG_FUNCTION_ARGS);
163 extern Datum interval_part(PG_FUNCTION_ARGS);
164 extern Datum timestamp_zone(PG_FUNCTION_ARGS);
165 extern Datum timestamp_izone(PG_FUNCTION_ARGS);
166 extern Datum timestamp_timestamptz(PG_FUNCTION_ARGS);
168 extern Datum timestamptz_in(PG_FUNCTION_ARGS);
169 extern Datum timestamptz_out(PG_FUNCTION_ARGS);
170 extern Datum timestamptz_recv(PG_FUNCTION_ARGS);
171 extern Datum timestamptz_send(PG_FUNCTION_ARGS);
172 extern Datum timestamptztypmodin(PG_FUNCTION_ARGS);
173 extern Datum timestamptztypmodout(PG_FUNCTION_ARGS);
174 extern Datum timestamptz_scale(PG_FUNCTION_ARGS);
175 extern Datum timestamptz_timestamp(PG_FUNCTION_ARGS);
176 extern Datum timestamptz_zone(PG_FUNCTION_ARGS);
177 extern Datum timestamptz_izone(PG_FUNCTION_ARGS);
178 extern Datum timestamptz_timestamptz(PG_FUNCTION_ARGS);
180 extern Datum interval_um(PG_FUNCTION_ARGS);
181 extern Datum interval_pl(PG_FUNCTION_ARGS);
182 extern Datum interval_mi(PG_FUNCTION_ARGS);
183 extern Datum interval_mul(PG_FUNCTION_ARGS);
184 extern Datum mul_d_interval(PG_FUNCTION_ARGS);
185 extern Datum interval_div(PG_FUNCTION_ARGS);
186 extern Datum interval_accum(PG_FUNCTION_ARGS);
187 extern Datum interval_accum_inv(PG_FUNCTION_ARGS);
188 extern Datum interval_avg(PG_FUNCTION_ARGS);
190 extern Datum timestamp_mi(PG_FUNCTION_ARGS);
191 extern Datum timestamp_pl_interval(PG_FUNCTION_ARGS);
192 extern Datum timestamp_mi_interval(PG_FUNCTION_ARGS);
193 extern Datum timestamp_age(PG_FUNCTION_ARGS);
194 extern Datum overlaps_timestamp(PG_FUNCTION_ARGS);
196 extern Datum timestamptz_pl_interval(PG_FUNCTION_ARGS);
197 extern Datum timestamptz_mi_interval(PG_FUNCTION_ARGS);
198 extern Datum timestamptz_age(PG_FUNCTION_ARGS);
199 extern Datum timestamptz_trunc(PG_FUNCTION_ARGS);
200 extern Datum timestamptz_part(PG_FUNCTION_ARGS);
202 extern Datum now(PG_FUNCTION_ARGS);
203 extern Datum statement_timestamp(PG_FUNCTION_ARGS);
204 extern Datum clock_timestamp(PG_FUNCTION_ARGS);
206 extern Datum pg_postmaster_start_time(PG_FUNCTION_ARGS);
207 extern Datum pg_conf_load_time(PG_FUNCTION_ARGS);
209 extern Datum generate_series_timestamp(PG_FUNCTION_ARGS);
210 extern Datum generate_series_timestamptz(PG_FUNCTION_ARGS);
212 /* Internal routines (not fmgr-callable) */
214 extern TimestampTz GetCurrentTimestamp(void);
215 extern void TimestampDifference(TimestampTz start_time, TimestampTz stop_time,
216 long *secs, int *microsecs);
217 extern bool TimestampDifferenceExceeds(TimestampTz start_time,
218 TimestampTz stop_time,
222 * Prototypes for functions to deal with integer timestamps, when the native
223 * format is float timestamps.
225 #ifndef HAVE_INT64_TIMESTAMP
226 extern int64 GetCurrentIntegerTimestamp(void);
227 extern TimestampTz IntegerTimestampToTimestampTz(int64 timestamp);
229 #define GetCurrentIntegerTimestamp() GetCurrentTimestamp()
230 #define IntegerTimestampToTimestampTz(timestamp) (timestamp)
233 extern TimestampTz time_t_to_timestamptz(pg_time_t tm);
234 extern pg_time_t timestamptz_to_time_t(TimestampTz t);
236 extern const char *timestamptz_to_str(TimestampTz t);
238 extern int tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *dt);
239 extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm,
240 fsec_t *fsec, const char **tzn, pg_tz *attimezone);
241 extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec);
243 extern int interval2tm(Interval span, struct pg_tm * tm, fsec_t *fsec);
244 extern int tm2interval(struct pg_tm * tm, fsec_t fsec, Interval *span);
246 extern Timestamp SetEpochTimestamp(void);
247 extern void GetEpochTime(struct pg_tm * tm);
249 extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
251 /* timestamp comparison works for timestamptz also */
252 #define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2)
254 extern int isoweek2j(int year, int week);
255 extern void isoweek2date(int woy, int *year, int *mon, int *mday);
256 extern void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday);
257 extern int date2isoweek(int year, int mon, int mday);
258 extern int date2isoyear(int year, int mon, int mday);
259 extern int date2isoyearday(int year, int mon, int mday);
261 #endif /* TIMESTAMP_H */