1 /*-------------------------------------------------------------------------
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.
9 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Id: datetime.h,v 1.32 2002/06/20 20:29:52 momjian Exp $
14 *-------------------------------------------------------------------------
23 #include "utils/timestamp.h"
26 /* ----------------------------------------------------------------
27 * time types + support macros
29 * String definitions for standard time quantities.
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 * ----------------------------------------------------------------
37 #define DCURRENT "current"
39 #define INVALID "invalid"
40 #define EARLY "-infinity"
41 #define LATE "infinity"
44 #define TOMORROW "tomorrow"
45 #define YESTERDAY "yesterday"
48 #define DMICROSEC "usecond"
49 #define DMILLISEC "msecond"
50 #define DSECOND "second"
51 #define DMINUTE "minute"
55 #define DMONTH "month"
56 #define DQUARTER "quarter"
58 #define DDECADE "decade"
59 #define DCENTURY "century"
60 #define DMILLENNIUM "millennium"
63 #define DTIMEZONE "timezone"
66 * Fundamental time field definitions for parsing.
68 * Meridian: am, pm, or 24-hour style.
80 * Fields for time decoding.
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.
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.
107 /* these are only for relative dates */
109 #define ABS_BEFORE 18
111 /* generic fields to help with parsing */
114 /* reserved for unrecognized string values */
115 #define UNKNOWN_FIELD 31
118 * Token field definitions for time parsing and decoding.
119 * These need to fit into the datetkn table type.
120 * At the moment, that means keep them within [-127,127].
121 * These are also used for bit masks in DecodeDateDelta()
122 * so actually restrict them to within [0,31] for now.
124 * Not all of these fields are used for masks in DecodeDateDelta
125 * so allow some larger than 31. - thomas 1997-11-17
136 #define DTK_SPECIAL 6
137 #define DTK_INVALID 7
138 #define DTK_CURRENT 8
143 #define DTK_YESTERDAY 13
145 #define DTK_TOMORROW 15
149 #define DTK_SECOND 18
150 #define DTK_MINUTE 19
155 #define DTK_QUARTER 24
157 #define DTK_DECADE 26
158 #define DTK_CENTURY 27
159 #define DTK_MILLENNIUM 28
160 #define DTK_MILLISEC 29
161 #define DTK_MICROSEC 30
162 #define DTK_JULIAN 31
166 #define DTK_TZ_HOUR 34
167 #define DTK_TZ_MINUTE 35
171 * Bit mask definitions for time parsing.
174 #define DTK_M(t) (0x01 << (t))
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))
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
183 #define TOKMAXLEN 10 /* only this many chars are stored in
186 /* keep this struct small; it gets used a lot */
192 char token[TOKMAXLEN];
195 char value; /* this may be unsigned, alas */
200 * Macro to replace modf(), which is broken on some platforms.
201 * t = input and remainder
205 #ifdef HAVE_INT64_TIMESTAMP
206 #define TMODULO(t,q,u) \
209 if (q != 0) t -= (q * u); \
212 #define TMODULO(t,q,u) \
214 q = ((t < 0)? ceil(t / u): floor(t / u)); \
215 if (q != 0) t -= rint(q * u); \
219 /* Global variable holding time zone information. */
220 #if defined(__CYGWIN__) || defined(N_PLAT_NLM)
221 #define TIMEZONE_GLOBAL _timezone
223 #define TIMEZONE_GLOBAL timezone
227 * Date/time validation
228 * Include check for leap year.
231 extern int day_tab[2][13];
233 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
235 /* Julian date support for date2j() and j2date()
236 * Set the minimum year to one greater than the year of the first valid day
237 * to avoid having to check year and day both. - tgl 97/05/08
240 #define JULIAN_MINYEAR (-4713)
241 #define JULIAN_MINMONTH (11)
242 #define JULIAN_MINDAY (23)
244 #define IS_VALID_JULIAN(y,m,d) (((y) > JULIAN_MINYEAR) \
245 || (((y) == JULIAN_MINYEAR) && (((m) > JULIAN_MINMONTH) \
246 || (((m) == JULIAN_MINMONTH) && ((d) >= JULIAN_MINDAY)))))
248 #define UTIME_MINYEAR (1901)
249 #define UTIME_MINMONTH (12)
250 #define UTIME_MINDAY (14)
251 #define UTIME_MAXYEAR (2038)
252 #define UTIME_MAXMONTH (01)
253 #define UTIME_MAXDAY (18)
255 #define IS_VALID_UTIME(y,m,d) ((((y) > UTIME_MINYEAR) \
256 || (((y) == UTIME_MINYEAR) && (((m) > UTIME_MINMONTH) \
257 || (((m) == UTIME_MINMONTH) && ((d) >= UTIME_MINDAY))))) \
258 && (((y) < UTIME_MAXYEAR) \
259 || (((y) == UTIME_MAXYEAR) && (((m) < UTIME_MAXMONTH) \
260 || (((m) == UTIME_MAXMONTH) && ((d) <= UTIME_MAXDAY))))))
263 extern void GetCurrentDateTime(struct tm * tm);
264 extern void GetCurrentTimeUsec(struct tm * tm, fsec_t *fsec);
265 extern void j2date(int jd, int *year, int *month, int *day);
266 extern int date2j(int year, int month, int day);
268 extern int ParseDateTime(char *timestr, char *lowstr,
269 char **field, int *ftype,
270 int maxfields, int *numfields);
271 extern int DecodeDateTime(char **field, int *ftype,
273 struct tm * tm, fsec_t *fsec, int *tzp);
275 extern int DecodeTimeOnly(char **field, int *ftype,
277 struct tm * tm, fsec_t *fsec, int *tzp);
279 extern int DecodeInterval(char **field, int *ftype,
281 struct tm * tm, fsec_t *fsec);
283 extern int DetermineLocalTimeZone(struct tm * tm);
285 extern int EncodeDateOnly(struct tm * tm, int style, char *str);
286 extern int EncodeTimeOnly(struct tm * tm, fsec_t fsec, int *tzp, int style, char *str);
287 extern int EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, char *str);
288 extern int EncodeInterval(struct tm * tm, fsec_t fsec, int style, char *str);
290 extern int DecodeSpecial(int field, char *lowtoken, int *val);
291 extern int DecodeUnits(int field, char *lowtoken, int *val);
292 extern bool ClearDateCache(bool, bool, bool);
294 extern int j2day(int jd);
296 #endif /* DATETIME_H */