summaryrefslogtreecommitdiff
path: root/src/include/utils/nabstime.h
blob: fde50d224c9ca965165e5aa6caa93f772e7f7b6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*-------------------------------------------------------------------------
 *
 * nabstime.h
 *	  Definitions for the "new" abstime code.
 *
 *
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * $Id: nabstime.h,v 1.28 2001/01/24 19:43:28 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
#ifndef NABSTIME_H
#define NABSTIME_H

#include <limits.h>
#include <time.h>

#include "fmgr.h"
#include "utils/timestamp.h"
#include "utils/datetime.h"


/* ----------------------------------------------------------------
 *
 *				time types + support macros
 *
 * ----------------------------------------------------------------
 */
/*
 * Although time_t generally is a long int on 64 bit systems, these two
 * types must be 4 bytes, because that's what the system assumes. They
 * should be yanked (long) before 2038 and be replaced by timestamp and
 * interval.
 */
typedef int32 AbsoluteTime;
typedef int32 RelativeTime;

typedef struct
{
	int32		status;
	AbsoluteTime data[2];
} TimeIntervalData;

typedef TimeIntervalData *TimeInterval;

/*
 * Macros for fmgr-callable functions.
 */
#define DatumGetAbsoluteTime(X)  ((AbsoluteTime) DatumGetInt32(X))
#define DatumGetRelativeTime(X)  ((RelativeTime) DatumGetInt32(X))
#define DatumGetTimeInterval(X)  ((TimeInterval) DatumGetPointer(X))

#define AbsoluteTimeGetDatum(X)  Int32GetDatum(X)
#define RelativeTimeGetDatum(X)  Int32GetDatum(X)
#define TimeIntervalGetDatum(X)  PointerGetDatum(X)

#define PG_GETARG_ABSOLUTETIME(n)  DatumGetAbsoluteTime(PG_GETARG_DATUM(n))
#define PG_GETARG_RELATIVETIME(n)  DatumGetRelativeTime(PG_GETARG_DATUM(n))
#define PG_GETARG_TIMEINTERVAL(n)  DatumGetTimeInterval(PG_GETARG_DATUM(n))

#define PG_RETURN_ABSOLUTETIME(x)  return AbsoluteTimeGetDatum(x)
#define PG_RETURN_RELATIVETIME(x)  return RelativeTimeGetDatum(x)
#define PG_RETURN_TIMEINTERVAL(x)  return TimeIntervalGetDatum(x)

/*
 * Reserved values
 * Epoch is Unix system time zero, but needs to be kept as a reserved
 *	value rather than converting to time since timezone calculations
 *	might move it away from 1970-01-01 00:00:00Z - tgl 97/02/20
 *
 * Pre-v6.1 code had large decimal numbers for reserved values.
 * These were chosen as special 32-bit bit patterns,
 *	so redefine them explicitly using these bit patterns. - tgl 97/02/24
 */
#define EPOCH_ABSTIME	((AbsoluteTime) 0)
#define INVALID_ABSTIME ((AbsoluteTime) 0x7FFFFFFE)		/* 2147483647 (2^31 - 1) */
#define CURRENT_ABSTIME ((AbsoluteTime) 0x7FFFFFFD)		/* 2147483646 (2^31 - 2) */
#define NOEND_ABSTIME	((AbsoluteTime) 0x7FFFFFFC)		/* 2147483645 (2^31 - 3) */
#define BIG_ABSTIME		((AbsoluteTime) 0x7FFFFFFB)		/* 2147483644 (2^31 - 4) */
#define NOSTART_ABSTIME	((AbsoluteTime) INT_MIN)		/* -2147483648 */

#define INVALID_RELTIME ((RelativeTime) 0x7FFFFFFE)		/* 2147483647 (2^31 - 1) */

#define AbsoluteTimeIsValid(time) \
	((bool) ((time) != INVALID_ABSTIME))

/*
 * Because NOSTART_ABSTIME is defined as INT_MIN, there can't be any
 * AbsoluteTime values less than it.  Therefore, we can code the test
 * "time > NOSTART_ABSTIME" as "time != NOSTART_ABSTIME", which avoids
 * compiler bugs on some platforms.  --- tgl & az, 11/2000
 */
#define AbsoluteTimeIsReal(time) \
	((bool) (((AbsoluteTime) (time)) < NOEND_ABSTIME && \
			  ((AbsoluteTime) (time)) != NOSTART_ABSTIME))

#define RelativeTimeIsValid(time) \
	((bool) (((RelativeTime) (time)) != INVALID_RELTIME))


/*
 * nabstime.c prototypes
 */
extern Datum nabstimein(PG_FUNCTION_ARGS);
extern Datum nabstimeout(PG_FUNCTION_ARGS);

extern Datum abstimeeq(PG_FUNCTION_ARGS);
extern Datum abstimene(PG_FUNCTION_ARGS);
extern Datum abstimelt(PG_FUNCTION_ARGS);
extern Datum abstimegt(PG_FUNCTION_ARGS);
extern Datum abstimele(PG_FUNCTION_ARGS);
extern Datum abstimege(PG_FUNCTION_ARGS);
extern Datum abstime_finite(PG_FUNCTION_ARGS);

extern Datum timestamp_abstime(PG_FUNCTION_ARGS);
extern Datum abstime_timestamp(PG_FUNCTION_ARGS);

extern Datum reltimein(PG_FUNCTION_ARGS);
extern Datum reltimeout(PG_FUNCTION_ARGS);
extern Datum tintervalin(PG_FUNCTION_ARGS);
extern Datum tintervalout(PG_FUNCTION_ARGS);
extern Datum interval_reltime(PG_FUNCTION_ARGS);
extern Datum reltime_interval(PG_FUNCTION_ARGS);
extern Datum mktinterval(PG_FUNCTION_ARGS);
extern Datum timepl(PG_FUNCTION_ARGS);
extern Datum timemi(PG_FUNCTION_ARGS);

extern Datum intinterval(PG_FUNCTION_ARGS);
extern Datum tintervalrel(PG_FUNCTION_ARGS);
extern Datum timenow(PG_FUNCTION_ARGS);
extern Datum reltimeeq(PG_FUNCTION_ARGS);
extern Datum reltimene(PG_FUNCTION_ARGS);
extern Datum reltimelt(PG_FUNCTION_ARGS);
extern Datum reltimegt(PG_FUNCTION_ARGS);
extern Datum reltimele(PG_FUNCTION_ARGS);
extern Datum reltimege(PG_FUNCTION_ARGS);
extern Datum tintervalsame(PG_FUNCTION_ARGS);
extern Datum tintervaleq(PG_FUNCTION_ARGS);
extern Datum tintervalne(PG_FUNCTION_ARGS);
extern Datum tintervallt(PG_FUNCTION_ARGS);
extern Datum tintervalgt(PG_FUNCTION_ARGS);
extern Datum tintervalle(PG_FUNCTION_ARGS);
extern Datum tintervalge(PG_FUNCTION_ARGS);
extern Datum tintervalleneq(PG_FUNCTION_ARGS);
extern Datum tintervallenne(PG_FUNCTION_ARGS);
extern Datum tintervallenlt(PG_FUNCTION_ARGS);
extern Datum tintervallengt(PG_FUNCTION_ARGS);
extern Datum tintervallenle(PG_FUNCTION_ARGS);
extern Datum tintervallenge(PG_FUNCTION_ARGS);
extern Datum tintervalct(PG_FUNCTION_ARGS);
extern Datum tintervalov(PG_FUNCTION_ARGS);
extern Datum tintervalstart(PG_FUNCTION_ARGS);
extern Datum tintervalend(PG_FUNCTION_ARGS);
extern Datum int4reltime(PG_FUNCTION_ARGS);
extern Datum timeofday(PG_FUNCTION_ARGS);

/* non-fmgr-callable support routines */
extern AbsoluteTime GetCurrentAbsoluteTime(void);
extern bool AbsoluteTimeIsBefore(AbsoluteTime time1, AbsoluteTime time2);
extern void abstime2tm(AbsoluteTime time, int *tzp, struct tm * tm, char *tzn);

#endif	 /* NABSTIME_H */