]> granicus.if.org Git - postgresql/blob - src/include/utils/timestamp.h
Add binary I/O routines for a bunch more datatypes. Still a few to go,
[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-2002, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * $Id: timestamp.h,v 1.30 2003/05/12 23:08:52 tgl Exp $
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef TIMESTAMP_H
14 #define TIMESTAMP_H
15
16 #include <time.h>
17 #include <math.h>
18 #include <limits.h>
19 #include <float.h>
20
21 #include "fmgr.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)
29  *      separately since the elapsed time spanned is unknown until instantiated
30  *      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
40 #else
41 typedef double Timestamp;
42 typedef double TimestampTz;
43 #endif
44
45 typedef struct
46 {
47 #ifdef HAVE_INT64_TIMESTAMP
48         int64           time;                   /* all time units other than months and
49                                                                  * years */
50 #else
51         double          time;                   /* all time units other than months and
52                                                                  * years */
53 #endif
54         int32           month;                  /* months and years, after time for
55                                                                  * alignment */
56 } Interval;
57
58
59 #define MAX_TIMESTAMP_PRECISION 6
60 #define MAX_INTERVAL_PRECISION 6
61
62
63 /*
64  * Macros for fmgr-callable functions.
65  *
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
68  * int64 or float8 is!
69  */
70 #ifdef HAVE_INT64_TIMESTAMP
71
72 #define DatumGetTimestamp(X)  ((Timestamp) DatumGetInt64(X))
73 #define DatumGetTimestampTz(X)  ((TimestampTz) DatumGetInt64(X))
74 #define DatumGetIntervalP(X)  ((Interval *) DatumGetPointer(X))
75
76 #define TimestampGetDatum(X) Int64GetDatum(X)
77 #define TimestampTzGetDatum(X) Int64GetDatum(X)
78 #define IntervalPGetDatum(X) PointerGetDatum(X)
79
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))
83
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)
87
88 #define DT_NOBEGIN              (-INT64CONST(0x7fffffffffffffff) - 1)
89 #define DT_NOEND                (INT64CONST(0x7fffffffffffffff))
90
91 #else
92
93 #define DatumGetTimestamp(X)  ((Timestamp) DatumGetFloat8(X))
94 #define DatumGetTimestampTz(X)  ((TimestampTz) DatumGetFloat8(X))
95 #define DatumGetIntervalP(X)  ((Interval *) DatumGetPointer(X))
96
97 #define TimestampGetDatum(X) Float8GetDatum(X)
98 #define TimestampTzGetDatum(X) Float8GetDatum(X)
99 #define IntervalPGetDatum(X) PointerGetDatum(X)
100
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))
104
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)
108
109 #ifdef HUGE_VAL
110 #define DT_NOBEGIN              (-HUGE_VAL)
111 #define DT_NOEND                (HUGE_VAL)
112 #else
113 #define DT_NOBEGIN              (-DBL_MAX)
114 #define DT_NOEND                (DBL_MAX)
115 #endif
116 #endif   /* HAVE_INT64_TIMESTAMP */
117
118
119 #define TIMESTAMP_NOBEGIN(j)    do {j = DT_NOBEGIN;} while (0)
120 #define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN)
121
122 #define TIMESTAMP_NOEND(j)              do {j = DT_NOEND;} while (0)
123 #define TIMESTAMP_IS_NOEND(j)   ((j) == DT_NOEND)
124
125 #define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
126
127 #ifdef HAVE_INT64_TIMESTAMP
128
129 typedef int32 fsec_t;
130
131 #else
132
133 typedef double fsec_t;
134
135 #define TIME_PREC_INV 1000000.0
136 #define JROUND(j) (rint(((double) (j))*TIME_PREC_INV)/TIME_PREC_INV)
137 #endif
138
139 #define TIMESTAMP_MASK(b) (1 << (b))
140 #define INTERVAL_MASK(b) (1 << (b))
141
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)
150
151
152 /*
153  * timestamp.c prototypes
154  */
155
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);
171
172 extern Datum interval_in(PG_FUNCTION_ARGS);
173 extern Datum interval_out(PG_FUNCTION_ARGS);
174 extern Datum interval_recv(PG_FUNCTION_ARGS);
175 extern Datum interval_send(PG_FUNCTION_ARGS);
176 extern Datum interval_scale(PG_FUNCTION_ARGS);
177 extern Datum interval_eq(PG_FUNCTION_ARGS);
178 extern Datum interval_ne(PG_FUNCTION_ARGS);
179 extern Datum interval_lt(PG_FUNCTION_ARGS);
180 extern Datum interval_le(PG_FUNCTION_ARGS);
181 extern Datum interval_ge(PG_FUNCTION_ARGS);
182 extern Datum interval_gt(PG_FUNCTION_ARGS);
183 extern Datum interval_finite(PG_FUNCTION_ARGS);
184 extern Datum interval_cmp(PG_FUNCTION_ARGS);
185 extern Datum interval_hash(PG_FUNCTION_ARGS);
186 extern Datum interval_smaller(PG_FUNCTION_ARGS);
187 extern Datum interval_larger(PG_FUNCTION_ARGS);
188
189 extern Datum timestamp_text(PG_FUNCTION_ARGS);
190 extern Datum text_timestamp(PG_FUNCTION_ARGS);
191 extern Datum interval_text(PG_FUNCTION_ARGS);
192 extern Datum text_interval(PG_FUNCTION_ARGS);
193 extern Datum timestamp_trunc(PG_FUNCTION_ARGS);
194 extern Datum interval_trunc(PG_FUNCTION_ARGS);
195 extern Datum timestamp_part(PG_FUNCTION_ARGS);
196 extern Datum interval_part(PG_FUNCTION_ARGS);
197 extern Datum timestamp_zone(PG_FUNCTION_ARGS);
198 extern Datum timestamp_izone(PG_FUNCTION_ARGS);
199 extern Datum timestamp_timestamptz(PG_FUNCTION_ARGS);
200
201 extern Datum timestamptz_in(PG_FUNCTION_ARGS);
202 extern Datum timestamptz_out(PG_FUNCTION_ARGS);
203 extern Datum timestamptz_recv(PG_FUNCTION_ARGS);
204 extern Datum timestamptz_send(PG_FUNCTION_ARGS);
205 extern Datum timestamptz_scale(PG_FUNCTION_ARGS);
206 extern Datum timestamptz_timestamp(PG_FUNCTION_ARGS);
207 extern Datum timestamptz_zone(PG_FUNCTION_ARGS);
208 extern Datum timestamptz_izone(PG_FUNCTION_ARGS);
209 extern Datum timestamptz_timestamptz(PG_FUNCTION_ARGS);
210
211 extern Datum interval_um(PG_FUNCTION_ARGS);
212 extern Datum interval_pl(PG_FUNCTION_ARGS);
213 extern Datum interval_mi(PG_FUNCTION_ARGS);
214 extern Datum interval_mul(PG_FUNCTION_ARGS);
215 extern Datum mul_d_interval(PG_FUNCTION_ARGS);
216 extern Datum interval_div(PG_FUNCTION_ARGS);
217 extern Datum interval_accum(PG_FUNCTION_ARGS);
218 extern Datum interval_avg(PG_FUNCTION_ARGS);
219
220 extern Datum timestamp_mi(PG_FUNCTION_ARGS);
221 extern Datum timestamp_pl_span(PG_FUNCTION_ARGS);
222 extern Datum timestamp_mi_span(PG_FUNCTION_ARGS);
223 extern Datum timestamp_age(PG_FUNCTION_ARGS);
224 extern Datum overlaps_timestamp(PG_FUNCTION_ARGS);
225
226 extern Datum timestamptz_text(PG_FUNCTION_ARGS);
227 extern Datum text_timestamptz(PG_FUNCTION_ARGS);
228 extern Datum timestamptz_pl_span(PG_FUNCTION_ARGS);
229 extern Datum timestamptz_mi_span(PG_FUNCTION_ARGS);
230 extern Datum timestamptz_age(PG_FUNCTION_ARGS);
231 extern Datum timestamptz_trunc(PG_FUNCTION_ARGS);
232 extern Datum timestamptz_part(PG_FUNCTION_ARGS);
233
234 extern Datum now(PG_FUNCTION_ARGS);
235
236 /* Internal routines (not fmgr-callable) */
237
238 extern int      tm2timestamp(struct tm * tm, fsec_t fsec, int *tzp, Timestamp *dt);
239 extern int timestamp2tm(Timestamp dt, int *tzp, struct tm * tm,
240                          fsec_t *fsec, char **tzn);
241 extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec);
242
243 extern int      interval2tm(Interval span, struct tm * tm, fsec_t *fsec);
244 extern int      tm2interval(struct tm * tm, fsec_t fsec, Interval *span);
245
246 extern Timestamp SetEpochTimestamp(void);
247 extern void GetEpochTime(struct tm * tm);
248
249 extern void isoweek2date(int woy, int *year, int *mon, int *mday);
250 extern int      date2isoweek(int year, int mon, int mday);
251
252 #endif   /* TIMESTAMP_H */