timestamp.h 11.0 KB
Newer Older
1 2 3 4 5
/*-------------------------------------------------------------------------
 *
 * timestamp.h
 *	  Definitions for the SQL92 "timestamp" and "interval" types.
 *
P
 
PostgreSQL Daemon 已提交
6
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7 8
 * Portions Copyright (c) 1994, Regents of the University of California
 *
B
Bruce Momjian 已提交
9
 * $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.53 2005/07/22 05:08:26 momjian Exp $
10 11 12 13 14 15 16 17
 *
 *-------------------------------------------------------------------------
 */
#ifndef TIMESTAMP_H
#define TIMESTAMP_H

#include <math.h>
#include <limits.h>
B
Bruce Momjian 已提交
18
#include <float.h>
19

20
#include "fmgr.h"
21
#include "pgtime.h"
22 23 24
#ifdef HAVE_INT64_TIMESTAMP
#include "utils/int8.h"
#endif
25

26 27
/*
 * Timestamp represents absolute time.
28 29 30
 * Interval represents delta time. Keep track of months (and years), days,
 *	and time separately since the elapsed time spanned is unknown until
 *	instantiated 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 37 38
#ifdef HAVE_INT64_TIMESTAMP
typedef int64 Timestamp;
typedef int64 TimestampTz;
B
Bruce Momjian 已提交
39

40
#else
41
typedef double Timestamp;
42
typedef double TimestampTz;
43
#endif
44

45 46
typedef struct
{
47
#ifdef HAVE_INT64_TIMESTAMP
48 49
	int64		time;			/* all time units other than days, 
								 * months and years */
50
#else
51 52
	double		time;			/* all time units other than days,
								 * months and years */
53
#endif
54
	int32		day;		    /* days, after time for alignment */
B
Bruce Momjian 已提交
55 56
	int32		month;			/* months and years, after time for
								 * alignment */
57 58 59
} Interval;


60 61 62
#define MAX_TIMESTAMP_PRECISION 6
#define MAX_INTERVAL_PRECISION 6

63
/* in both timestamp.h and ecpg/dt.h */
64
#define DAYS_PER_YEAR	365.25	/* assumes leap year every four years */
65
#define MONTHS_PER_YEAR	12
66 67
/*
 *	DAYS_PER_MONTH is very imprecise.  The more accurate value is
B
Bruce Momjian 已提交
68
 *	365.2425/12 = 30.436875, or '30 days 10:29:06'.  Right now we only
69
 *	return an integral number of days, but someday perhaps we should
B
Bruce Momjian 已提交
70 71
 *	also return a 'time' value to be used as well.  ISO 8601 suggests
 *	30 days.
72
 */
73 74
#define DAYS_PER_MONTH	30		/* assumes exactly 30 days per month */
#define HOURS_PER_DAY	24		/* assume no daylight savings time changes */
75

B
Bruce Momjian 已提交
76
/*
77
 *	This doesn't adjust for uneven daylight savings time intervals or leap
B
Bruce Momjian 已提交
78 79
 *	seconds, and it crudely estimates leap years.  A more accurate value
 *	for days per years is 365.2422.
B
Bruce Momjian 已提交
80 81 82
 */
#define SECS_PER_YEAR	(36525 * 864)	/* avoid floating-point computation */
#define SECS_PER_DAY	86400
83
#define SECS_PER_HOUR   3600
84
#define SECS_PER_MINUTE 60
B
Bruce Momjian 已提交
85
#define MINS_PER_HOUR	60
86

87
#ifdef HAVE_INT64_TIMESTAMP
88 89 90 91
#define USECS_PER_DAY	INT64CONST(86400000000)
#define USECS_PER_HOUR	INT64CONST(3600000000)
#define USECS_PER_MINUTE INT64CONST(60000000)
#define USECS_PER_SEC	INT64CONST(1000000)
92
#endif
93

94 95 96
/*
 * Macros for fmgr-callable functions.
 *
97 98 99
 * For Timestamp, we make use of the same support routines as for int64
 * or float8.  Therefore Timestamp is pass-by-reference if and only if
 * int64 or float8 is!
100
 */
101
#ifdef HAVE_INT64_TIMESTAMP
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#define DatumGetTimestamp(X)  ((Timestamp) DatumGetInt64(X))
#define DatumGetTimestampTz(X)	((TimestampTz) DatumGetInt64(X))
#define DatumGetIntervalP(X)  ((Interval *) DatumGetPointer(X))

#define TimestampGetDatum(X) Int64GetDatum(X)
#define TimestampTzGetDatum(X) Int64GetDatum(X)
#define IntervalPGetDatum(X) PointerGetDatum(X)

#define PG_GETARG_TIMESTAMP(n) PG_GETARG_INT64(n)
#define PG_GETARG_TIMESTAMPTZ(n) PG_GETARG_INT64(n)
#define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))

#define PG_RETURN_TIMESTAMP(x) PG_RETURN_INT64(x)
#define PG_RETURN_TIMESTAMPTZ(x) PG_RETURN_INT64(x)
#define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)

#define DT_NOBEGIN		(-INT64CONST(0x7fffffffffffffff) - 1)
#define DT_NOEND		(INT64CONST(0x7fffffffffffffff))

#else
123

124
#define DatumGetTimestamp(X)  ((Timestamp) DatumGetFloat8(X))
125
#define DatumGetTimestampTz(X)	((TimestampTz) DatumGetFloat8(X))
126 127
#define DatumGetIntervalP(X)  ((Interval *) DatumGetPointer(X))

128 129 130
#define TimestampGetDatum(X) Float8GetDatum(X)
#define TimestampTzGetDatum(X) Float8GetDatum(X)
#define IntervalPGetDatum(X) PointerGetDatum(X)
131

132 133
#define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
#define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
134 135
#define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))

136 137
#define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
#define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
138 139
#define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)

140 141 142 143 144 145 146
#ifdef HUGE_VAL
#define DT_NOBEGIN		(-HUGE_VAL)
#define DT_NOEND		(HUGE_VAL)
#else
#define DT_NOBEGIN		(-DBL_MAX)
#define DT_NOEND		(DBL_MAX)
#endif
B
Bruce Momjian 已提交
147
#endif   /* HAVE_INT64_TIMESTAMP */
148

149

150
#define TIMESTAMP_NOBEGIN(j)	do {(j) = DT_NOBEGIN;} while (0)
151
#define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN)
152

153
#define TIMESTAMP_NOEND(j)		do {(j) = DT_NOEND;} while (0)
154
#define TIMESTAMP_IS_NOEND(j)	((j) == DT_NOEND)
155

156
#define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
157

158
#ifdef HAVE_INT64_TIMESTAMP
159

160 161 162
typedef int32 fsec_t;

#else
163

164 165
typedef double fsec_t;

166 167
#define TIME_PREC_INV 1000000.0
#define JROUND(j) (rint(((double) (j))*TIME_PREC_INV)/TIME_PREC_INV)
168
#endif
169

170 171 172 173 174 175 176 177 178 179 180 181
#define TIMESTAMP_MASK(b) (1 << (b))
#define INTERVAL_MASK(b) (1 << (b))

/* Macros to handle packing and unpacking the typmod field for intervals */
#define INTERVAL_FULL_RANGE (0x7FFF)
#define INTERVAL_RANGE_MASK (0x7FFF)
#define INTERVAL_FULL_PRECISION (0xFFFF)
#define INTERVAL_PRECISION_MASK (0xFFFF)
#define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
#define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
#define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)

182

183 184 185 186
/* Set at postmaster start */
extern TimestampTz PgStartTime;


187 188 189 190
/*
 * timestamp.c prototypes
 */

191 192
extern Datum timestamp_in(PG_FUNCTION_ARGS);
extern Datum timestamp_out(PG_FUNCTION_ARGS);
193 194
extern Datum timestamp_recv(PG_FUNCTION_ARGS);
extern Datum timestamp_send(PG_FUNCTION_ARGS);
195
extern Datum timestamp_scale(PG_FUNCTION_ARGS);
196 197 198 199 200 201 202 203 204 205 206
extern Datum timestamp_eq(PG_FUNCTION_ARGS);
extern Datum timestamp_ne(PG_FUNCTION_ARGS);
extern Datum timestamp_lt(PG_FUNCTION_ARGS);
extern Datum timestamp_le(PG_FUNCTION_ARGS);
extern Datum timestamp_ge(PG_FUNCTION_ARGS);
extern Datum timestamp_gt(PG_FUNCTION_ARGS);
extern Datum timestamp_finite(PG_FUNCTION_ARGS);
extern Datum timestamp_cmp(PG_FUNCTION_ARGS);
extern Datum timestamp_smaller(PG_FUNCTION_ARGS);
extern Datum timestamp_larger(PG_FUNCTION_ARGS);

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
extern Datum timestamp_eq_timestamptz(PG_FUNCTION_ARGS);
extern Datum timestamp_ne_timestamptz(PG_FUNCTION_ARGS);
extern Datum timestamp_lt_timestamptz(PG_FUNCTION_ARGS);
extern Datum timestamp_le_timestamptz(PG_FUNCTION_ARGS);
extern Datum timestamp_gt_timestamptz(PG_FUNCTION_ARGS);
extern Datum timestamp_ge_timestamptz(PG_FUNCTION_ARGS);
extern Datum timestamp_cmp_timestamptz(PG_FUNCTION_ARGS);

extern Datum timestamptz_eq_timestamp(PG_FUNCTION_ARGS);
extern Datum timestamptz_ne_timestamp(PG_FUNCTION_ARGS);
extern Datum timestamptz_lt_timestamp(PG_FUNCTION_ARGS);
extern Datum timestamptz_le_timestamp(PG_FUNCTION_ARGS);
extern Datum timestamptz_gt_timestamp(PG_FUNCTION_ARGS);
extern Datum timestamptz_ge_timestamp(PG_FUNCTION_ARGS);
extern Datum timestamptz_cmp_timestamp(PG_FUNCTION_ARGS);

223 224
extern Datum interval_in(PG_FUNCTION_ARGS);
extern Datum interval_out(PG_FUNCTION_ARGS);
225 226
extern Datum interval_recv(PG_FUNCTION_ARGS);
extern Datum interval_send(PG_FUNCTION_ARGS);
227
extern Datum interval_scale(PG_FUNCTION_ARGS);
228 229 230 231 232 233 234 235
extern Datum interval_eq(PG_FUNCTION_ARGS);
extern Datum interval_ne(PG_FUNCTION_ARGS);
extern Datum interval_lt(PG_FUNCTION_ARGS);
extern Datum interval_le(PG_FUNCTION_ARGS);
extern Datum interval_ge(PG_FUNCTION_ARGS);
extern Datum interval_gt(PG_FUNCTION_ARGS);
extern Datum interval_finite(PG_FUNCTION_ARGS);
extern Datum interval_cmp(PG_FUNCTION_ARGS);
236
extern Datum interval_hash(PG_FUNCTION_ARGS);
237 238
extern Datum interval_smaller(PG_FUNCTION_ARGS);
extern Datum interval_larger(PG_FUNCTION_ARGS);
239 240
extern Datum interval_justify_hours(PG_FUNCTION_ARGS);
extern Datum interval_justify_days(PG_FUNCTION_ARGS);
241 242 243 244 245 246 247 248 249 250

extern Datum timestamp_text(PG_FUNCTION_ARGS);
extern Datum text_timestamp(PG_FUNCTION_ARGS);
extern Datum interval_text(PG_FUNCTION_ARGS);
extern Datum text_interval(PG_FUNCTION_ARGS);
extern Datum timestamp_trunc(PG_FUNCTION_ARGS);
extern Datum interval_trunc(PG_FUNCTION_ARGS);
extern Datum timestamp_part(PG_FUNCTION_ARGS);
extern Datum interval_part(PG_FUNCTION_ARGS);
extern Datum timestamp_zone(PG_FUNCTION_ARGS);
251
extern Datum timestamp_izone(PG_FUNCTION_ARGS);
252 253 254 255
extern Datum timestamp_timestamptz(PG_FUNCTION_ARGS);

extern Datum timestamptz_in(PG_FUNCTION_ARGS);
extern Datum timestamptz_out(PG_FUNCTION_ARGS);
256 257
extern Datum timestamptz_recv(PG_FUNCTION_ARGS);
extern Datum timestamptz_send(PG_FUNCTION_ARGS);
258
extern Datum timestamptz_scale(PG_FUNCTION_ARGS);
259 260 261 262
extern Datum timestamptz_timestamp(PG_FUNCTION_ARGS);
extern Datum timestamptz_zone(PG_FUNCTION_ARGS);
extern Datum timestamptz_izone(PG_FUNCTION_ARGS);
extern Datum timestamptz_timestamptz(PG_FUNCTION_ARGS);
263 264 265 266 267 268 269

extern Datum interval_um(PG_FUNCTION_ARGS);
extern Datum interval_pl(PG_FUNCTION_ARGS);
extern Datum interval_mi(PG_FUNCTION_ARGS);
extern Datum interval_mul(PG_FUNCTION_ARGS);
extern Datum mul_d_interval(PG_FUNCTION_ARGS);
extern Datum interval_div(PG_FUNCTION_ARGS);
270 271
extern Datum interval_accum(PG_FUNCTION_ARGS);
extern Datum interval_avg(PG_FUNCTION_ARGS);
272 273

extern Datum timestamp_mi(PG_FUNCTION_ARGS);
274 275
extern Datum timestamp_pl_interval(PG_FUNCTION_ARGS);
extern Datum timestamp_mi_interval(PG_FUNCTION_ARGS);
276 277 278
extern Datum timestamp_age(PG_FUNCTION_ARGS);
extern Datum overlaps_timestamp(PG_FUNCTION_ARGS);

279 280
extern Datum timestamptz_text(PG_FUNCTION_ARGS);
extern Datum text_timestamptz(PG_FUNCTION_ARGS);
281 282
extern Datum timestamptz_pl_interval(PG_FUNCTION_ARGS);
extern Datum timestamptz_mi_interval(PG_FUNCTION_ARGS);
283 284 285 286
extern Datum timestamptz_age(PG_FUNCTION_ARGS);
extern Datum timestamptz_trunc(PG_FUNCTION_ARGS);
extern Datum timestamptz_part(PG_FUNCTION_ARGS);

287 288
extern Datum now(PG_FUNCTION_ARGS);

289 290
extern Datum pgsql_postmaster_start_time(PG_FUNCTION_ARGS);

291
/* Internal routines (not fmgr-callable) */
292

293 294
extern TimestampTz GetCurrentTimestamp(void);

295 296
extern int	tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *dt);
extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm,
297
			 fsec_t *fsec, char **tzn, pg_tz *attimezone);
298
extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec);
299

300 301
extern int	interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec);
extern int	tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span);
B
Bruce Momjian 已提交
302

303
extern Timestamp SetEpochTimestamp(void);
304
extern void GetEpochTime(struct pg_tm *tm);
305

306
extern int	timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
B
Bruce Momjian 已提交
307

308 309 310
/* timestamp comparison works for timestamptz also */
#define timestamptz_cmp_internal(dt1,dt2)	timestamp_cmp_internal(dt1, dt2)

B
Bruce Momjian 已提交
311
extern void isoweek2date(int woy, int *year, int *mon, int *mday);
B
Bruce Momjian 已提交
312
extern int	date2isoweek(int year, int mon, int mday);
313
extern int	date2isoyear(int year, int mon, int mday);
314

315
#endif   /* TIMESTAMP_H */