]> granicus.if.org Git - postgresql/blob - src/include/utils/timestamp.h
Neatnik fetishism.
[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-2001, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * $Id: timestamp.h,v 1.20 2001/10/03 15:42:12 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
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 double TimestampTz;
37
38 typedef struct
39 {
40         double          time;   /* all time units other than months and years */
41         int32           month;  /* months and years, after time for 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 DatumGetTimestampTz(X)  ((TimestampTz) DatumGetFloat8(X))
53 #define DatumGetIntervalP(X)  ((Interval *) DatumGetPointer(X))
54
55 #define TimestampGetDatum(X) Float8GetDatum(X)
56 #define TimestampTzGetDatum(X) Float8GetDatum(X)
57 #define IntervalPGetDatum(X) PointerGetDatum(X)
58
59 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
60 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
61 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
62
63 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
64 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
65 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
66
67
68 #ifdef HUGE_VAL
69 #define DT_NOBEGIN              (-HUGE_VAL)
70 #define DT_NOEND                (HUGE_VAL)
71 #else
72 #define DT_NOBEGIN              (-DBL_MAX)
73 #define DT_NOEND                (DBL_MAX)
74 #endif
75
76 #define TIMESTAMP_NOBEGIN(j)    do {j = DT_NOBEGIN;} while (0)
77 #define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN)
78
79 #define TIMESTAMP_NOEND(j)              do {j = DT_NOEND;} while (0)
80 #define TIMESTAMP_IS_NOEND(j)   ((j) == DT_NOEND)
81
82 #define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
83
84 #define TIME_PREC_INV 1000000.0
85 #define JROUND(j) (rint(((double) (j))*TIME_PREC_INV)/TIME_PREC_INV)
86
87
88 /*
89  * timestamp.c prototypes
90  */
91
92 extern Datum timestamp_in(PG_FUNCTION_ARGS);
93 extern Datum timestamp_out(PG_FUNCTION_ARGS);
94 extern Datum timestamp_scale(PG_FUNCTION_ARGS);
95 extern Datum timestamp_eq(PG_FUNCTION_ARGS);
96 extern Datum timestamp_ne(PG_FUNCTION_ARGS);
97 extern Datum timestamp_lt(PG_FUNCTION_ARGS);
98 extern Datum timestamp_le(PG_FUNCTION_ARGS);
99 extern Datum timestamp_ge(PG_FUNCTION_ARGS);
100 extern Datum timestamp_gt(PG_FUNCTION_ARGS);
101 extern Datum timestamp_finite(PG_FUNCTION_ARGS);
102 extern Datum timestamp_cmp(PG_FUNCTION_ARGS);
103 extern Datum timestamp_smaller(PG_FUNCTION_ARGS);
104 extern Datum timestamp_larger(PG_FUNCTION_ARGS);
105
106 extern Datum interval_in(PG_FUNCTION_ARGS);
107 extern Datum interval_out(PG_FUNCTION_ARGS);
108 extern Datum interval_eq(PG_FUNCTION_ARGS);
109 extern Datum interval_ne(PG_FUNCTION_ARGS);
110 extern Datum interval_lt(PG_FUNCTION_ARGS);
111 extern Datum interval_le(PG_FUNCTION_ARGS);
112 extern Datum interval_ge(PG_FUNCTION_ARGS);
113 extern Datum interval_gt(PG_FUNCTION_ARGS);
114 extern Datum interval_finite(PG_FUNCTION_ARGS);
115 extern Datum interval_cmp(PG_FUNCTION_ARGS);
116 extern Datum interval_hash(PG_FUNCTION_ARGS);
117 extern Datum interval_smaller(PG_FUNCTION_ARGS);
118 extern Datum interval_larger(PG_FUNCTION_ARGS);
119
120 extern Datum timestamp_text(PG_FUNCTION_ARGS);
121 extern Datum text_timestamp(PG_FUNCTION_ARGS);
122 extern Datum interval_text(PG_FUNCTION_ARGS);
123 extern Datum text_interval(PG_FUNCTION_ARGS);
124 extern Datum timestamp_trunc(PG_FUNCTION_ARGS);
125 extern Datum interval_trunc(PG_FUNCTION_ARGS);
126 extern Datum timestamp_part(PG_FUNCTION_ARGS);
127 extern Datum interval_part(PG_FUNCTION_ARGS);
128 extern Datum timestamp_zone(PG_FUNCTION_ARGS);
129 extern Datum timestamp_izone(PG_FUNCTION_ARGS);
130 extern Datum timestamp_timestamptz(PG_FUNCTION_ARGS);
131
132 extern Datum timestamptz_in(PG_FUNCTION_ARGS);
133 extern Datum timestamptz_out(PG_FUNCTION_ARGS);
134 extern Datum timestamptz_scale(PG_FUNCTION_ARGS);
135 extern Datum timestamptz_timestamp(PG_FUNCTION_ARGS);
136 extern Datum timestamptz_zone(PG_FUNCTION_ARGS);
137 extern Datum timestamptz_izone(PG_FUNCTION_ARGS);
138 extern Datum timestamptz_timestamptz(PG_FUNCTION_ARGS);
139
140 extern Datum interval_um(PG_FUNCTION_ARGS);
141 extern Datum interval_pl(PG_FUNCTION_ARGS);
142 extern Datum interval_mi(PG_FUNCTION_ARGS);
143 extern Datum interval_mul(PG_FUNCTION_ARGS);
144 extern Datum mul_d_interval(PG_FUNCTION_ARGS);
145 extern Datum interval_div(PG_FUNCTION_ARGS);
146 extern Datum interval_accum(PG_FUNCTION_ARGS);
147 extern Datum interval_avg(PG_FUNCTION_ARGS);
148
149 extern Datum timestamp_mi(PG_FUNCTION_ARGS);
150 extern Datum timestamp_pl_span(PG_FUNCTION_ARGS);
151 extern Datum timestamp_mi_span(PG_FUNCTION_ARGS);
152 extern Datum timestamp_age(PG_FUNCTION_ARGS);
153 extern Datum overlaps_timestamp(PG_FUNCTION_ARGS);
154
155 extern Datum timestamptz_text(PG_FUNCTION_ARGS);
156 extern Datum text_timestamptz(PG_FUNCTION_ARGS);
157 extern Datum timestamptz_pl_span(PG_FUNCTION_ARGS);
158 extern Datum timestamptz_mi_span(PG_FUNCTION_ARGS);
159 extern Datum timestamptz_age(PG_FUNCTION_ARGS);
160 extern Datum timestamptz_trunc(PG_FUNCTION_ARGS);
161 extern Datum timestamptz_part(PG_FUNCTION_ARGS);
162
163 extern Datum now(PG_FUNCTION_ARGS);
164
165 /* Internal routines (not fmgr-callable) */
166
167 extern int      tm2timestamp(struct tm * tm, double fsec, int *tzp, Timestamp *dt);
168 extern int      timestamp2tm(Timestamp dt, int *tzp, struct tm * tm,
169                                                  double *fsec, char **tzn);
170 extern void     dt2time(Timestamp dt, int *hour, int *min, double *sec);
171
172 extern int      interval2tm(Interval span, struct tm * tm, float8 *fsec);
173 extern int      tm2interval(struct tm * tm, double fsec, Interval *span);
174
175 extern Timestamp SetEpochTimestamp(void);
176 extern void GetEpochTime(struct tm * tm);
177
178 extern void isoweek2date(int woy, int *year, int *mon, int *mday);
179 extern int      date2isoweek(int year, int mon, int mday);
180
181 #endif   /* TIMESTAMP_H */