OSDN Git Service

Another pgindent run. Fixes enum indenting, and improves #endif
[pg-rex/syncrep.git] / src / include / utils / datetime.h
1 /*-------------------------------------------------------------------------
2  *
3  * datetime.h
4  *        Definitions for the date/time and other date/time support code.
5  *        The support code is shared with other date data types,
6  *         including abstime, reltime, date, and time.
7  *
8  *
9  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  * $Id: datetime.h,v 1.25 2001/10/28 06:26:09 momjian Exp $
13  *
14  *-------------------------------------------------------------------------
15  */
16 #ifndef DATETIME_H
17 #define DATETIME_H
18
19 #include <time.h>
20 #include <math.h>
21 #include <limits.h>
22
23 #include "utils/timestamp.h"
24
25
26 /* ----------------------------------------------------------------
27  *                              time types + support macros
28  *
29  * String definitions for standard time quantities.
30  *
31  * These strings are the defaults used to form output time strings.
32  * Other alternate forms are hardcoded into token tables in datetime.c.
33  * ----------------------------------------------------------------
34  */
35
36 #define DAGO                    "ago"
37 #define DCURRENT                "current"
38 #define EPOCH                   "epoch"
39 #define INVALID                 "invalid"
40 #define EARLY                   "-infinity"
41 #define LATE                    "infinity"
42 #define NOW                             "now"
43 #define TODAY                   "today"
44 #define TOMORROW                "tomorrow"
45 #define YESTERDAY               "yesterday"
46 #define ZULU                    "zulu"
47
48 #define DMICROSEC               "usecond"
49 #define DMILLISEC               "msecond"
50 #define DSECOND                 "second"
51 #define DMINUTE                 "minute"
52 #define DHOUR                   "hour"
53 #define DDAY                    "day"
54 #define DWEEK                   "week"
55 #define DMONTH                  "month"
56 #define DQUARTER                "quarter"
57 #define DYEAR                   "year"
58 #define DDECADE                 "decade"
59 #define DCENTURY                "century"
60 #define DMILLENNIUM             "millennium"
61 #define DA_D                    "ad"
62 #define DB_C                    "bc"
63 #define DTIMEZONE               "timezone"
64
65 /*
66  * Fundamental time field definitions for parsing.
67  *
68  *      Meridian:  am, pm, or 24-hour style.
69  *      Millennium: ad, bc
70  */
71
72 #define AM              0
73 #define PM              1
74 #define HR24    2
75
76 #define AD              0
77 #define BC              1
78
79 /*
80  * Fields for time decoding.
81  *
82  * Can't have more of these than there are bits in an unsigned int
83  * since these are turned into bit masks during parsing and decoding.
84  *
85  * Furthermore, the values for YEAR, MONTH, DAY, HOUR, MINUTE, SECOND
86  * must be in the range 0..14 so that the associated bitmasks can fit
87  * into the left half of an INTERVAL's typmod value.
88  */
89
90 #define RESERV  0
91 #define MONTH   1
92 #define YEAR    2
93 #define DAY             3
94 #define JULIAN  4
95 #define TZ              5
96 #define DTZ             6
97 #define DTZMOD  7
98 #define IGNORE  8
99 #define AMPM    9
100 #define HOUR    10
101 #define MINUTE  11
102 #define SECOND  12
103 #define DOY             13
104 #define DOW             14
105 #define UNITS   15
106 #define ADBC    16
107 /* these are only for relative dates */
108 #define AGO             17
109 #define ABS_BEFORE              18
110 #define ABS_AFTER               19
111 /* reserved for unrecognized string values */
112 #define UNKNOWN_FIELD   31
113
114 /*
115  * Token field definitions for time parsing and decoding.
116  * These need to fit into the datetkn table type.
117  * At the moment, that means keep them within [-127,127].
118  * These are also used for bit masks in DecodeDateDelta()
119  *      so actually restrict them to within [0,31] for now.
120  * - thomas 97/06/19
121  * Not all of these fields are used for masks in DecodeDateDelta
122  *      so allow some larger than 31. - thomas 1997-11-17
123  */
124
125 #define DTK_NUMBER              0
126 #define DTK_STRING              1
127
128 #define DTK_DATE                2
129 #define DTK_TIME                3
130 #define DTK_TZ                  4
131 #define DTK_AGO                 5
132
133 #define DTK_SPECIAL             6
134 #define DTK_INVALID             7
135 #define DTK_CURRENT             8
136 #define DTK_EARLY               9
137 #define DTK_LATE                10
138 #define DTK_EPOCH               11
139 #define DTK_NOW                 12
140 #define DTK_YESTERDAY   13
141 #define DTK_TODAY               14
142 #define DTK_TOMORROW    15
143 #define DTK_ZULU                16
144
145 #define DTK_DELTA               17
146 #define DTK_SECOND              18
147 #define DTK_MINUTE              19
148 #define DTK_HOUR                20
149 #define DTK_DAY                 21
150 #define DTK_WEEK                22
151 #define DTK_MONTH               23
152 #define DTK_QUARTER             24
153 #define DTK_YEAR                25
154 #define DTK_DECADE              26
155 #define DTK_CENTURY             27
156 #define DTK_MILLENNIUM  28
157 #define DTK_MILLISEC    29
158 #define DTK_MICROSEC    30
159 #define DTK_JULIAN              31
160
161 #define DTK_DOW                 32
162 #define DTK_DOY                 33
163 #define DTK_TZ_HOUR             34
164 #define DTK_TZ_MINUTE   35
165
166 #define DTK_ISO_DATE    36
167 #define DTK_ISO_TIME    37
168
169
170 /*
171  * Bit mask definitions for time parsing.
172  */
173
174 #define DTK_M(t)                (0x01 << (t))
175
176 #define DTK_DATE_M              (DTK_M(YEAR) | DTK_M(MONTH) | DTK_M(DAY))
177 #define DTK_TIME_M              (DTK_M(HOUR) | DTK_M(MINUTE) | DTK_M(SECOND))
178
179 #define MAXDATELEN              51              /* maximum possible length of an input
180                                                                  * date string (not counting tr. null) */
181 #define MAXDATEFIELDS   25              /* maximum possible number of fields in a
182                                                                  * date string */
183 #define TOKMAXLEN               10              /* only this many chars are stored in
184                                                                  * datetktbl */
185
186 /* keep this struct small; it gets used a lot */
187 typedef struct
188 {
189 #if defined(_AIX)
190         char       *token;
191 #else
192         char            token[TOKMAXLEN];
193 #endif   /* _AIX */
194         char            type;
195         char            value;                  /* this may be unsigned, alas */
196 } datetkn;
197
198
199 /* TMODULO()
200  * Macro to replace modf(), which is broken on some platforms.
201  */
202 #define TMODULO(t,q,u) \
203 do { \
204         q = ((t < 0)? ceil(t / u): floor(t / u)); \
205         if (q != 0) \
206                 t -= rint(q * u); \
207 } while(0)
208
209 #ifdef __CYGWIN__
210 #define TIMEZONE_GLOBAL _timezone
211 #else
212 #define TIMEZONE_GLOBAL timezone
213 #endif
214
215 /*
216  * Date/time validation
217  * Include check for leap year.
218  */
219
220 extern int      day_tab[2][13];
221
222 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
223
224 /* Julian date support for date2j() and j2date()
225  * Set the minimum year to one greater than the year of the first valid day
226  *      to avoid having to check year and day both. - tgl 97/05/08
227  */
228
229 #define JULIAN_MINYEAR (-4713)
230 #define JULIAN_MINMONTH (11)
231 #define JULIAN_MINDAY (23)
232
233 #define IS_VALID_JULIAN(y,m,d) (((y) > JULIAN_MINYEAR) \
234  || (((y) == JULIAN_MINYEAR) && (((m) > JULIAN_MINMONTH) \
235   || (((m) == JULIAN_MINMONTH) && ((d) >= JULIAN_MINDAY)))))
236
237 #define UTIME_MINYEAR (1901)
238 #define UTIME_MINMONTH (12)
239 #define UTIME_MINDAY (14)
240 #define UTIME_MAXYEAR (2038)
241 #define UTIME_MAXMONTH (01)
242 #define UTIME_MAXDAY (18)
243
244 #define IS_VALID_UTIME(y,m,d) ((((y) > UTIME_MINYEAR) \
245  || (((y) == UTIME_MINYEAR) && (((m) > UTIME_MINMONTH) \
246   || (((m) == UTIME_MINMONTH) && ((d) >= UTIME_MINDAY))))) \
247  && (((y) < UTIME_MAXYEAR) \
248  || (((y) == UTIME_MAXYEAR) && (((m) < UTIME_MAXMONTH) \
249   || (((m) == UTIME_MAXMONTH) && ((d) <= UTIME_MAXDAY))))))
250
251
252 extern void GetCurrentTime(struct tm * tm);
253 extern void GetCurrentTimeUsec(struct tm * tm, double *fsec);
254 extern void j2date(int jd, int *year, int *month, int *day);
255 extern int      date2j(int year, int month, int day);
256
257 extern int ParseDateTime(char *timestr, char *lowstr,
258                           char **field, int *ftype,
259                           int maxfields, int *numfields);
260 extern int DecodeDateTime(char **field, int *ftype,
261                            int nf, int *dtype,
262                            struct tm * tm, double *fsec, int *tzp);
263
264 extern int DecodeTimeOnly(char **field, int *ftype,
265                            int nf, int *dtype,
266                            struct tm * tm, double *fsec, int *tzp);
267
268 extern int DecodeDateDelta(char **field, int *ftype,
269                                 int nf, int *dtype,
270                                 struct tm * tm, double *fsec);
271
272 extern int      DetermineLocalTimeZone(struct tm * tm);
273
274 extern int      EncodeDateOnly(struct tm * tm, int style, char *str);
275 extern int      EncodeTimeOnly(struct tm * tm, double fsec, int *tzp, int style, char *str);
276 extern int      EncodeDateTime(struct tm * tm, double fsec, int *tzp, char **tzn, int style, char *str);
277 extern int      EncodeTimeSpan(struct tm * tm, double fsec, int style, char *str);
278
279 extern int      DecodeSpecial(int field, char *lowtoken, int *val);
280 extern int      DecodeUnits(int field, char *lowtoken, int *val);
281 extern void ClearDateCache(bool);
282
283 extern int      j2day(int jd);
284
285 #endif   /* DATETIME_H */