OSDN Git Service

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