]> granicus.if.org Git - postgresql/blob - src/include/utils/timestamp.h
#include cleanups
[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-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * $Id: timestamp.h,v 1.7 2000/06/15 00:52:26 momjian 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
23
24 /*
25  * Timestamp represents absolute time.
26  * Interval represents delta time. Keep track of months (and years)
27  *      separately since the elapsed time spanned is unknown until instantiated
28  *      relative to an absolute time.
29  *
30  * Note that Postgres uses "time interval" to mean a bounded interval,
31  *      consisting of a beginning and ending time, not a time span - thomas 97/03/20
32  */
33
34 typedef double Timestamp;
35
36 typedef struct
37 {
38         double          time;                   /* all time units other than months and
39                                                                  * years */
40         int4            month;                  /* months and years, after time for
41                                                                  * alignment */
42 } Interval;
43
44
45 /*
46  * Macros for fmgr-callable functions.
47  *
48  * For Timestamp, we make use of the same support routines as for float8.
49  * Therefore Timestamp is pass-by-reference if and only if float8 is!
50  */
51 #define DatumGetTimestamp(X)  ((Timestamp) DatumGetFloat8(X))
52 #define DatumGetIntervalP(X)  ((Interval *) DatumGetPointer(X))
53
54 #define TimestampGetDatum(X)  Float8GetDatum(X)
55 #define IntervalPGetDatum(X)  PointerGetDatum(X)
56
57 #define PG_GETARG_TIMESTAMP(n)  DatumGetTimestamp(PG_GETARG_DATUM(n))
58 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
59
60 #define PG_RETURN_TIMESTAMP(x)  return TimestampGetDatum(x)
61 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
62
63
64 #ifdef NAN
65 #define DT_INVALID              (NAN)
66 #else
67 #define DT_INVALID              (DBL_MIN+DBL_MIN)
68 #endif
69 #ifdef HUGE_VAL
70 #define DT_NOBEGIN              (-HUGE_VAL)
71 #define DT_NOEND                (HUGE_VAL)
72 #else
73 #define DT_NOBEGIN              (-DBL_MAX)
74 #define DT_NOEND                (DBL_MAX)
75 #endif
76 #define DT_CURRENT              (DBL_MIN)
77 #define DT_EPOCH                (-DBL_MIN)
78
79 #define TIMESTAMP_INVALID(j)            do {j = DT_INVALID;} while (0)
80 #ifdef NAN
81 #define TIMESTAMP_IS_INVALID(j) (isnan(j))
82 #else
83 #define TIMESTAMP_IS_INVALID(j) (j == DT_INVALID)
84 #endif
85
86 #define TIMESTAMP_NOBEGIN(j)            do {j = DT_NOBEGIN;} while (0)
87 #define TIMESTAMP_IS_NOBEGIN(j) (j == DT_NOBEGIN)
88
89 #define TIMESTAMP_NOEND(j)              do {j = DT_NOEND;} while (0)
90 #define TIMESTAMP_IS_NOEND(j)   (j == DT_NOEND)
91
92 #define TIMESTAMP_CURRENT(j)            do {j = DT_CURRENT;} while (0)
93 #if defined(linux) && defined(__powerpc__)
94 extern int      timestamp_is_current(double j);
95
96 #define TIMESTAMP_IS_CURRENT(j) timestamp_is_current(j)
97 #else
98 #define TIMESTAMP_IS_CURRENT(j) (j == DT_CURRENT)
99 #endif
100
101 #define TIMESTAMP_EPOCH(j)              do {j = DT_EPOCH;} while (0)
102 #if defined(linux) && defined(__powerpc__)
103 extern int      timestamp_is_epoch(double j);
104
105 #define TIMESTAMP_IS_EPOCH(j)   timestamp_is_epoch(j)
106 #else
107 #define TIMESTAMP_IS_EPOCH(j)   (j == DT_EPOCH)
108 #endif
109
110 #define TIMESTAMP_IS_RELATIVE(j) (TIMESTAMP_IS_CURRENT(j) || TIMESTAMP_IS_EPOCH(j))
111 #define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_INVALID(j) \
112                                                                 || TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
113 #define TIMESTAMP_IS_RESERVED(j) (TIMESTAMP_IS_RELATIVE(j) || TIMESTAMP_NOT_FINITE(j))
114
115 #define INTERVAL_INVALID(j)             do {(j).time = DT_INVALID;} while (0)
116 #ifdef NAN
117 #define INTERVAL_IS_INVALID(j)  (isnan((j).time))
118 #else
119 #define INTERVAL_IS_INVALID(j)  ((j).time == DT_INVALID)
120 #endif
121 #define INTERVAL_NOT_FINITE(j)  INTERVAL_IS_INVALID(j)
122
123 #define TIME_PREC_INV 1000000.0
124 #define JROUND(j) (rint(((double) (j))*TIME_PREC_INV)/TIME_PREC_INV)
125
126
127 /*
128  * timestamp.c prototypes
129  */
130
131 extern Datum timestamp_in(PG_FUNCTION_ARGS);
132 extern Datum timestamp_out(PG_FUNCTION_ARGS);
133 extern Datum timestamp_eq(PG_FUNCTION_ARGS);
134 extern Datum timestamp_ne(PG_FUNCTION_ARGS);
135 extern Datum timestamp_lt(PG_FUNCTION_ARGS);
136 extern Datum timestamp_le(PG_FUNCTION_ARGS);
137 extern Datum timestamp_ge(PG_FUNCTION_ARGS);
138 extern Datum timestamp_gt(PG_FUNCTION_ARGS);
139 extern Datum timestamp_finite(PG_FUNCTION_ARGS);
140 extern Datum timestamp_cmp(PG_FUNCTION_ARGS);
141 extern Datum timestamp_smaller(PG_FUNCTION_ARGS);
142 extern Datum timestamp_larger(PG_FUNCTION_ARGS);
143
144 extern Datum interval_in(PG_FUNCTION_ARGS);
145 extern Datum interval_out(PG_FUNCTION_ARGS);
146 extern Datum interval_eq(PG_FUNCTION_ARGS);
147 extern Datum interval_ne(PG_FUNCTION_ARGS);
148 extern Datum interval_lt(PG_FUNCTION_ARGS);
149 extern Datum interval_le(PG_FUNCTION_ARGS);
150 extern Datum interval_ge(PG_FUNCTION_ARGS);
151 extern Datum interval_gt(PG_FUNCTION_ARGS);
152 extern Datum interval_finite(PG_FUNCTION_ARGS);
153 extern Datum interval_cmp(PG_FUNCTION_ARGS);
154 extern Datum interval_smaller(PG_FUNCTION_ARGS);
155 extern Datum interval_larger(PG_FUNCTION_ARGS);
156
157 extern Datum timestamp_text(PG_FUNCTION_ARGS);
158 extern Datum text_timestamp(PG_FUNCTION_ARGS);
159 extern Datum interval_text(PG_FUNCTION_ARGS);
160 extern Datum text_interval(PG_FUNCTION_ARGS);
161 extern Datum timestamp_trunc(PG_FUNCTION_ARGS);
162 extern Datum interval_trunc(PG_FUNCTION_ARGS);
163 extern Datum timestamp_part(PG_FUNCTION_ARGS);
164 extern Datum interval_part(PG_FUNCTION_ARGS);
165 extern Datum timestamp_zone(PG_FUNCTION_ARGS);
166
167 extern Datum interval_um(PG_FUNCTION_ARGS);
168 extern Datum interval_pl(PG_FUNCTION_ARGS);
169 extern Datum interval_mi(PG_FUNCTION_ARGS);
170 extern Datum interval_mul(PG_FUNCTION_ARGS);
171 extern Datum mul_d_interval(PG_FUNCTION_ARGS);
172 extern Datum interval_div(PG_FUNCTION_ARGS);
173
174 extern Datum timestamp_mi(PG_FUNCTION_ARGS);
175 extern Datum timestamp_pl_span(PG_FUNCTION_ARGS);
176 extern Datum timestamp_mi_span(PG_FUNCTION_ARGS);
177 extern Datum timestamp_age(PG_FUNCTION_ARGS);
178 extern Datum overlaps_timestamp(PG_FUNCTION_ARGS);
179
180 extern Datum now(PG_FUNCTION_ARGS);
181
182 /* Internal routines (not fmgr-callable) */
183
184 extern int      tm2timestamp(struct tm * tm, double fsec, int *tzp, Timestamp *dt);
185 extern int      timestamp2tm(Timestamp dt, int *tzp, struct tm * tm,
186                                                  double *fsec, char **tzn);
187
188 extern Timestamp SetTimestamp(Timestamp timestamp);
189
190 #endif   /* TIMESTAMP_H */