1 /*-------------------------------------------------------------------------
4 * Definitions for the SQL92 "timestamp" and "interval" types.
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $Id: timestamp.h,v 1.7 2000/06/15 00:52:26 momjian Exp $
11 *-------------------------------------------------------------------------
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.
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
34 typedef double Timestamp;
38 double time; /* all time units other than months and
40 int4 month; /* months and years, after time for
46 * Macros for fmgr-callable functions.
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!
51 #define DatumGetTimestamp(X) ((Timestamp) DatumGetFloat8(X))
52 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
54 #define TimestampGetDatum(X) Float8GetDatum(X)
55 #define IntervalPGetDatum(X) PointerGetDatum(X)
57 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
58 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
60 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
61 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
65 #define DT_INVALID (NAN)
67 #define DT_INVALID (DBL_MIN+DBL_MIN)
70 #define DT_NOBEGIN (-HUGE_VAL)
71 #define DT_NOEND (HUGE_VAL)
73 #define DT_NOBEGIN (-DBL_MAX)
74 #define DT_NOEND (DBL_MAX)
76 #define DT_CURRENT (DBL_MIN)
77 #define DT_EPOCH (-DBL_MIN)
79 #define TIMESTAMP_INVALID(j) do {j = DT_INVALID;} while (0)
81 #define TIMESTAMP_IS_INVALID(j) (isnan(j))
83 #define TIMESTAMP_IS_INVALID(j) (j == DT_INVALID)
86 #define TIMESTAMP_NOBEGIN(j) do {j = DT_NOBEGIN;} while (0)
87 #define TIMESTAMP_IS_NOBEGIN(j) (j == DT_NOBEGIN)
89 #define TIMESTAMP_NOEND(j) do {j = DT_NOEND;} while (0)
90 #define TIMESTAMP_IS_NOEND(j) (j == DT_NOEND)
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);
96 #define TIMESTAMP_IS_CURRENT(j) timestamp_is_current(j)
98 #define TIMESTAMP_IS_CURRENT(j) (j == DT_CURRENT)
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);
105 #define TIMESTAMP_IS_EPOCH(j) timestamp_is_epoch(j)
107 #define TIMESTAMP_IS_EPOCH(j) (j == DT_EPOCH)
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))
115 #define INTERVAL_INVALID(j) do {(j).time = DT_INVALID;} while (0)
117 #define INTERVAL_IS_INVALID(j) (isnan((j).time))
119 #define INTERVAL_IS_INVALID(j) ((j).time == DT_INVALID)
121 #define INTERVAL_NOT_FINITE(j) INTERVAL_IS_INVALID(j)
123 #define TIME_PREC_INV 1000000.0
124 #define JROUND(j) (rint(((double) (j))*TIME_PREC_INV)/TIME_PREC_INV)
128 * timestamp.c prototypes
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);
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);
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);
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);
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);
180 extern Datum now(PG_FUNCTION_ARGS);
182 /* Internal routines (not fmgr-callable) */
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);
188 extern Timestamp SetTimestamp(Timestamp timestamp);
190 #endif /* TIMESTAMP_H */