date.h 7.63 KB
Newer Older
1 2 3 4 5 6
/*-------------------------------------------------------------------------
 *
 * date.h
 *	  Definitions for the SQL92 "date" and "time" types.
 *
 *
7
 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 9
 * Portions Copyright (c) 1994, Regents of the University of California
 *
10
 * $PostgreSQL: pgsql/src/include/utils/date.h,v 1.41 2008/10/14 17:12:33 tgl Exp $
11 12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */
#ifndef DATE_H
#define DATE_H

17 18
#include <math.h>

19 20 21
#include "fmgr.h"


22 23
typedef int32 DateADT;

24 25 26
#ifdef HAVE_INT64_TIMESTAMP
typedef int64 TimeADT;
#else
27
typedef float8 TimeADT;
28
#endif
29

30 31
typedef struct
{
32
	TimeADT		time;			/* all time units other than months and years */
Bruce Momjian's avatar
Bruce Momjian committed
33
	int32		zone;			/* numeric time zone, in seconds */
34 35
} TimeTzADT;

36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*
 * Infinity and minus infinity must be the max and min values of DateADT.
 * We could use INT_MIN and INT_MAX here, but seems better to not assume that
 * int32 == int.
 */
#define DATEVAL_NOBEGIN		((DateADT) (-0x7fffffff - 1))
#define DATEVAL_NOEND		((DateADT) 0x7fffffff)

#define DATE_NOBEGIN(j)		((j) = DATEVAL_NOBEGIN)
#define DATE_IS_NOBEGIN(j)	((j) == DATEVAL_NOBEGIN)
#define DATE_NOEND(j)		((j) = DATEVAL_NOEND)
#define DATE_IS_NOEND(j)	((j) == DATEVAL_NOEND)
#define DATE_NOT_FINITE(j)	(DATE_IS_NOBEGIN(j) || DATE_IS_NOEND(j))

Teodor Sigaev's avatar
Teodor Sigaev committed
50 51 52 53 54 55
/*
 * Macros for fmgr-callable functions.
 *
 * For TimeADT, we make use of the same support routines as for float8 or int64.
 * Therefore TimeADT is pass-by-reference if and only if float8 or int64 is!
 */
56
#ifdef HAVE_INT64_TIMESTAMP
Teodor Sigaev's avatar
Teodor Sigaev committed
57

58
#define MAX_TIME_PRECISION 6
Teodor Sigaev's avatar
Teodor Sigaev committed
59 60 61 62 63 64 65 66

#define DatumGetDateADT(X)	  ((DateADT) DatumGetInt32(X))
#define DatumGetTimeADT(X)	  ((TimeADT) DatumGetInt64(X))
#define DatumGetTimeTzADTP(X) ((TimeTzADT *) DatumGetPointer(X))

#define DateADTGetDatum(X)	  Int32GetDatum(X)
#define TimeADTGetDatum(X)	  Int64GetDatum(X)
#define TimeTzADTPGetDatum(X) PointerGetDatum(X)
67 68

#else  /* !HAVE_INT64_TIMESTAMP */
Teodor Sigaev's avatar
Teodor Sigaev committed
69

70
#define MAX_TIME_PRECISION 10
71

72 73 74 75
/* round off to MAX_TIME_PRECISION decimal places */
#define TIME_PREC_INV 10000000000.0
#define TIMEROUND(j) (rint(((double) (j)) * TIME_PREC_INV) / TIME_PREC_INV)

76 77
#define DatumGetDateADT(X)	  ((DateADT) DatumGetInt32(X))
#define DatumGetTimeADT(X)	  ((TimeADT) DatumGetFloat8(X))
78 79
#define DatumGetTimeTzADTP(X) ((TimeTzADT *) DatumGetPointer(X))

80 81
#define DateADTGetDatum(X)	  Int32GetDatum(X)
#define TimeADTGetDatum(X)	  Float8GetDatum(X)
82
#define TimeTzADTPGetDatum(X) PointerGetDatum(X)
83

84
#endif   /* HAVE_INT64_TIMESTAMP */
Teodor Sigaev's avatar
Teodor Sigaev committed
85

86 87
#define PG_GETARG_DATEADT(n)	 DatumGetDateADT(PG_GETARG_DATUM(n))
#define PG_GETARG_TIMEADT(n)	 DatumGetTimeADT(PG_GETARG_DATUM(n))
88 89
#define PG_GETARG_TIMETZADT_P(n) DatumGetTimeTzADTP(PG_GETARG_DATUM(n))

90 91
#define PG_RETURN_DATEADT(x)	 return DateADTGetDatum(x)
#define PG_RETURN_TIMEADT(x)	 return TimeADTGetDatum(x)
92 93 94
#define PG_RETURN_TIMETZADT_P(x) return TimeTzADTPGetDatum(x)


95
/* date.c */
96 97
extern Datum date_in(PG_FUNCTION_ARGS);
extern Datum date_out(PG_FUNCTION_ARGS);
98 99
extern Datum date_recv(PG_FUNCTION_ARGS);
extern Datum date_send(PG_FUNCTION_ARGS);
100 101 102 103 104 105 106
extern Datum date_eq(PG_FUNCTION_ARGS);
extern Datum date_ne(PG_FUNCTION_ARGS);
extern Datum date_lt(PG_FUNCTION_ARGS);
extern Datum date_le(PG_FUNCTION_ARGS);
extern Datum date_gt(PG_FUNCTION_ARGS);
extern Datum date_ge(PG_FUNCTION_ARGS);
extern Datum date_cmp(PG_FUNCTION_ARGS);
107
extern Datum date_finite(PG_FUNCTION_ARGS);
108 109 110 111 112
extern Datum date_larger(PG_FUNCTION_ARGS);
extern Datum date_smaller(PG_FUNCTION_ARGS);
extern Datum date_mi(PG_FUNCTION_ARGS);
extern Datum date_pli(PG_FUNCTION_ARGS);
extern Datum date_mii(PG_FUNCTION_ARGS);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
extern Datum date_eq_timestamp(PG_FUNCTION_ARGS);
extern Datum date_ne_timestamp(PG_FUNCTION_ARGS);
extern Datum date_lt_timestamp(PG_FUNCTION_ARGS);
extern Datum date_le_timestamp(PG_FUNCTION_ARGS);
extern Datum date_gt_timestamp(PG_FUNCTION_ARGS);
extern Datum date_ge_timestamp(PG_FUNCTION_ARGS);
extern Datum date_cmp_timestamp(PG_FUNCTION_ARGS);
extern Datum date_eq_timestamptz(PG_FUNCTION_ARGS);
extern Datum date_ne_timestamptz(PG_FUNCTION_ARGS);
extern Datum date_lt_timestamptz(PG_FUNCTION_ARGS);
extern Datum date_le_timestamptz(PG_FUNCTION_ARGS);
extern Datum date_gt_timestamptz(PG_FUNCTION_ARGS);
extern Datum date_ge_timestamptz(PG_FUNCTION_ARGS);
extern Datum date_cmp_timestamptz(PG_FUNCTION_ARGS);
extern Datum timestamp_eq_date(PG_FUNCTION_ARGS);
extern Datum timestamp_ne_date(PG_FUNCTION_ARGS);
extern Datum timestamp_lt_date(PG_FUNCTION_ARGS);
extern Datum timestamp_le_date(PG_FUNCTION_ARGS);
extern Datum timestamp_gt_date(PG_FUNCTION_ARGS);
extern Datum timestamp_ge_date(PG_FUNCTION_ARGS);
extern Datum timestamp_cmp_date(PG_FUNCTION_ARGS);
extern Datum timestamptz_eq_date(PG_FUNCTION_ARGS);
extern Datum timestamptz_ne_date(PG_FUNCTION_ARGS);
extern Datum timestamptz_lt_date(PG_FUNCTION_ARGS);
extern Datum timestamptz_le_date(PG_FUNCTION_ARGS);
extern Datum timestamptz_gt_date(PG_FUNCTION_ARGS);
extern Datum timestamptz_ge_date(PG_FUNCTION_ARGS);
extern Datum timestamptz_cmp_date(PG_FUNCTION_ARGS);
extern Datum date_pl_interval(PG_FUNCTION_ARGS);
extern Datum date_mi_interval(PG_FUNCTION_ARGS);
143 144
extern Datum date_timestamp(PG_FUNCTION_ARGS);
extern Datum timestamp_date(PG_FUNCTION_ARGS);
145 146
extern Datum date_timestamptz(PG_FUNCTION_ARGS);
extern Datum timestamptz_date(PG_FUNCTION_ARGS);
147 148
extern Datum datetime_timestamp(PG_FUNCTION_ARGS);
extern Datum abstime_date(PG_FUNCTION_ARGS);
149

150 151
extern Datum time_in(PG_FUNCTION_ARGS);
extern Datum time_out(PG_FUNCTION_ARGS);
152 153
extern Datum time_recv(PG_FUNCTION_ARGS);
extern Datum time_send(PG_FUNCTION_ARGS);
154 155
extern Datum timetypmodin(PG_FUNCTION_ARGS);
extern Datum timetypmodout(PG_FUNCTION_ARGS);
156
extern Datum time_scale(PG_FUNCTION_ARGS);
157 158 159 160 161 162 163
extern Datum time_eq(PG_FUNCTION_ARGS);
extern Datum time_ne(PG_FUNCTION_ARGS);
extern Datum time_lt(PG_FUNCTION_ARGS);
extern Datum time_le(PG_FUNCTION_ARGS);
extern Datum time_gt(PG_FUNCTION_ARGS);
extern Datum time_ge(PG_FUNCTION_ARGS);
extern Datum time_cmp(PG_FUNCTION_ARGS);
164
extern Datum time_hash(PG_FUNCTION_ARGS);
165 166 167
extern Datum overlaps_time(PG_FUNCTION_ARGS);
extern Datum time_larger(PG_FUNCTION_ARGS);
extern Datum time_smaller(PG_FUNCTION_ARGS);
168
extern Datum time_mi_time(PG_FUNCTION_ARGS);
169
extern Datum timestamp_time(PG_FUNCTION_ARGS);
170
extern Datum timestamptz_time(PG_FUNCTION_ARGS);
171
extern Datum time_interval(PG_FUNCTION_ARGS);
172 173 174
extern Datum interval_time(PG_FUNCTION_ARGS);
extern Datum time_pl_interval(PG_FUNCTION_ARGS);
extern Datum time_mi_interval(PG_FUNCTION_ARGS);
175
extern Datum time_part(PG_FUNCTION_ARGS);
176

177 178
extern Datum timetz_in(PG_FUNCTION_ARGS);
extern Datum timetz_out(PG_FUNCTION_ARGS);
179 180
extern Datum timetz_recv(PG_FUNCTION_ARGS);
extern Datum timetz_send(PG_FUNCTION_ARGS);
181 182
extern Datum timetztypmodin(PG_FUNCTION_ARGS);
extern Datum timetztypmodout(PG_FUNCTION_ARGS);
183
extern Datum timetz_scale(PG_FUNCTION_ARGS);
184 185 186 187 188 189 190
extern Datum timetz_eq(PG_FUNCTION_ARGS);
extern Datum timetz_ne(PG_FUNCTION_ARGS);
extern Datum timetz_lt(PG_FUNCTION_ARGS);
extern Datum timetz_le(PG_FUNCTION_ARGS);
extern Datum timetz_gt(PG_FUNCTION_ARGS);
extern Datum timetz_ge(PG_FUNCTION_ARGS);
extern Datum timetz_cmp(PG_FUNCTION_ARGS);
191
extern Datum timetz_hash(PG_FUNCTION_ARGS);
192 193 194
extern Datum overlaps_timetz(PG_FUNCTION_ARGS);
extern Datum timetz_larger(PG_FUNCTION_ARGS);
extern Datum timetz_smaller(PG_FUNCTION_ARGS);
195 196 197 198
extern Datum timetz_time(PG_FUNCTION_ARGS);
extern Datum time_timetz(PG_FUNCTION_ARGS);
extern Datum timestamptz_timetz(PG_FUNCTION_ARGS);
extern Datum datetimetz_timestamptz(PG_FUNCTION_ARGS);
199
extern Datum timetz_part(PG_FUNCTION_ARGS);
200 201
extern Datum timetz_zone(PG_FUNCTION_ARGS);
extern Datum timetz_izone(PG_FUNCTION_ARGS);
202 203
extern Datum timetz_pl_interval(PG_FUNCTION_ARGS);
extern Datum timetz_mi_interval(PG_FUNCTION_ARGS);
204

205
#endif   /* DATE_H */