1 /*-------------------------------------------------------------------------
2  *
3  * nabstime.h
4  *	  Definitions for the "new" abstime code.
5  *
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/utils/nabstime.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef NABSTIME_H
15 #define NABSTIME_H
16 
17 #include <limits.h>
18 
19 #include "fmgr.h"
20 #include "pgtime.h"
21 
22 
23 /* ----------------------------------------------------------------
24  *
25  *				time types + support macros
26  *
27  * ----------------------------------------------------------------
28  */
29 
30 /*
31  * Although time_t generally is a long int on 64 bit systems, these two
32  * types must be 4 bytes, because that's what pg_type.h assumes. They
33  * should be yanked (long) before 2038 and be replaced by timestamp and
34  * interval.
35  */
36 typedef int32 AbsoluteTime;
37 typedef int32 RelativeTime;
38 
39 typedef struct
40 {
41 	int32		status;
42 	AbsoluteTime data[2];
43 } TimeIntervalData;
44 
45 typedef TimeIntervalData *TimeInterval;
46 
47 /*
48  * Macros for fmgr-callable functions.
49  */
50 #define DatumGetAbsoluteTime(X)  ((AbsoluteTime) DatumGetInt32(X))
51 #define DatumGetRelativeTime(X)  ((RelativeTime) DatumGetInt32(X))
52 #define DatumGetTimeInterval(X)  ((TimeInterval) DatumGetPointer(X))
53 
54 #define AbsoluteTimeGetDatum(X)  Int32GetDatum(X)
55 #define RelativeTimeGetDatum(X)  Int32GetDatum(X)
56 #define TimeIntervalGetDatum(X)  PointerGetDatum(X)
57 
58 #define PG_GETARG_ABSOLUTETIME(n)  DatumGetAbsoluteTime(PG_GETARG_DATUM(n))
59 #define PG_GETARG_RELATIVETIME(n)  DatumGetRelativeTime(PG_GETARG_DATUM(n))
60 #define PG_GETARG_TIMEINTERVAL(n)  DatumGetTimeInterval(PG_GETARG_DATUM(n))
61 
62 #define PG_RETURN_ABSOLUTETIME(x)  return AbsoluteTimeGetDatum(x)
63 #define PG_RETURN_RELATIVETIME(x)  return RelativeTimeGetDatum(x)
64 #define PG_RETURN_TIMEINTERVAL(x)  return TimeIntervalGetDatum(x)
65 
66 /*
67  * Reserved values
68  * Epoch is Unix system time zero, but needs to be kept as a reserved
69  *	value rather than converting to time since timezone calculations
70  *	might move it away from 1970-01-01 00:00:00Z - tgl 97/02/20
71  *
72  * Pre-v6.1 code had large decimal numbers for reserved values.
73  * These were chosen as special 32-bit bit patterns,
74  *	so redefine them explicitly using these bit patterns. - tgl 97/02/24
75  */
76 #define INVALID_ABSTIME ((AbsoluteTime) 0x7FFFFFFE) /* 2147483647 (2^31 - 1) */
77 #define NOEND_ABSTIME	((AbsoluteTime) 0x7FFFFFFC) /* 2147483645 (2^31 - 3) */
78 #define NOSTART_ABSTIME ((AbsoluteTime) INT_MIN)	/* -2147483648 */
79 
80 #define INVALID_RELTIME ((RelativeTime) 0x7FFFFFFE) /* 2147483647 (2^31 - 1) */
81 
82 #define AbsoluteTimeIsValid(time) \
83 	((bool) ((time) != INVALID_ABSTIME))
84 
85 /*
86  * Because NOSTART_ABSTIME is defined as INT_MIN, there can't be any
87  * AbsoluteTime values less than it.  Therefore, we can code the test
88  * "time > NOSTART_ABSTIME" as "time != NOSTART_ABSTIME", which avoids
89  * compiler bugs on some platforms.  --- tgl & az, 11/2000
90  */
91 #define AbsoluteTimeIsReal(time) \
92 	((bool) (((AbsoluteTime) (time)) < NOEND_ABSTIME && \
93 			  ((AbsoluteTime) (time)) != NOSTART_ABSTIME))
94 
95 #define RelativeTimeIsValid(time) \
96 	((bool) (((RelativeTime) (time)) != INVALID_RELTIME))
97 
98 
99 /* non-fmgr-callable support routines */
100 extern AbsoluteTime GetCurrentAbsoluteTime(void);
101 extern void abstime2tm(AbsoluteTime time, int *tzp, struct pg_tm *tm, char **tzn);
102 
103 #endif							/* NABSTIME_H */
104