PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
date.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * date.h
4  * Definitions for the SQL "date" and "time" types.
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/date.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef DATE_H
15 #define DATE_H
16 
17 #include <math.h>
18 
19 #include "fmgr.h"
20 
21 
22 typedef int32 DateADT;
23 
24 #ifdef HAVE_INT64_TIMESTAMP
25 typedef int64 TimeADT;
26 #else
27 typedef float8 TimeADT;
28 #endif
29 
30 typedef struct
31 {
32  TimeADT time; /* all time units other than months and years */
33  int32 zone; /* numeric time zone, in seconds */
34 } TimeTzADT;
35 
36 /*
37  * Infinity and minus infinity must be the max and min values of DateADT.
38  */
39 #define DATEVAL_NOBEGIN ((DateADT) PG_INT32_MIN)
40 #define DATEVAL_NOEND ((DateADT) PG_INT32_MAX)
41 
42 #define DATE_NOBEGIN(j) ((j) = DATEVAL_NOBEGIN)
43 #define DATE_IS_NOBEGIN(j) ((j) == DATEVAL_NOBEGIN)
44 #define DATE_NOEND(j) ((j) = DATEVAL_NOEND)
45 #define DATE_IS_NOEND(j) ((j) == DATEVAL_NOEND)
46 #define DATE_NOT_FINITE(j) (DATE_IS_NOBEGIN(j) || DATE_IS_NOEND(j))
47 
48 /*
49  * Macros for fmgr-callable functions.
50  *
51  * For TimeADT, we make use of the same support routines as for float8 or int64.
52  * Therefore TimeADT is pass-by-reference if and only if float8 or int64 is!
53  */
54 #ifdef HAVE_INT64_TIMESTAMP
55 
56 #define MAX_TIME_PRECISION 6
57 
58 #define DatumGetDateADT(X) ((DateADT) DatumGetInt32(X))
59 #define DatumGetTimeADT(X) ((TimeADT) DatumGetInt64(X))
60 #define DatumGetTimeTzADTP(X) ((TimeTzADT *) DatumGetPointer(X))
61 
62 #define DateADTGetDatum(X) Int32GetDatum(X)
63 #define TimeADTGetDatum(X) Int64GetDatum(X)
64 #define TimeTzADTPGetDatum(X) PointerGetDatum(X)
65 #else /* !HAVE_INT64_TIMESTAMP */
66 
67 #define MAX_TIME_PRECISION 10
68 
69 /* round off to MAX_TIME_PRECISION decimal places */
70 #define TIME_PREC_INV 10000000000.0
71 #define TIMEROUND(j) (rint(((double) (j)) * TIME_PREC_INV) / TIME_PREC_INV)
72 
73 #define DatumGetDateADT(X) ((DateADT) DatumGetInt32(X))
74 #define DatumGetTimeADT(X) ((TimeADT) DatumGetFloat8(X))
75 #define DatumGetTimeTzADTP(X) ((TimeTzADT *) DatumGetPointer(X))
76 
77 #define DateADTGetDatum(X) Int32GetDatum(X)
78 #define TimeADTGetDatum(X) Float8GetDatum(X)
79 #define TimeTzADTPGetDatum(X) PointerGetDatum(X)
80 #endif /* HAVE_INT64_TIMESTAMP */
81 
82 #define PG_GETARG_DATEADT(n) DatumGetDateADT(PG_GETARG_DATUM(n))
83 #define PG_GETARG_TIMEADT(n) DatumGetTimeADT(PG_GETARG_DATUM(n))
84 #define PG_GETARG_TIMETZADT_P(n) DatumGetTimeTzADTP(PG_GETARG_DATUM(n))
85 
86 #define PG_RETURN_DATEADT(x) return DateADTGetDatum(x)
87 #define PG_RETURN_TIMEADT(x) return TimeADTGetDatum(x)
88 #define PG_RETURN_TIMETZADT_P(x) return TimeTzADTPGetDatum(x)
89 
90 
91 /* date.c */
92 extern int32 anytime_typmod_check(bool istz, int32 typmod);
93 extern double date2timestamp_no_overflow(DateADT dateVal);
94 extern void EncodeSpecialDate(DateADT dt, char *str);
95 extern DateADT GetSQLCurrentDate(void);
96 extern TimeTzADT *GetSQLCurrentTime(int32 typmod);
97 extern TimeADT GetSQLLocalTime(int32 typmod);
98 
99 #endif /* DATE_H */
TimeADT time
Definition: date.h:32
int32 DateADT
Definition: date.h:22
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:299
double date2timestamp_no_overflow(DateADT dateVal)
Definition: date.c:666
DateADT GetSQLCurrentDate(void)
Definition: date.c:314
signed int int32
Definition: c.h:253
double float8
Definition: c.h:377
int32 zone
Definition: date.h:33
TimeTzADT * GetSQLCurrentTime(int32 typmod)
Definition: date.c:336
float8 TimeADT
Definition: date.h:27
TimeADT GetSQLLocalTime(int32 typmod)
Definition: date.c:362
Definition: date.h:30
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:74