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-2000, PostgreSQL, Inc
10 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Id: datetime.h,v 1.15 2000/06/08 22:37:58 momjian Exp $
14 *-------------------------------------------------------------------------
22 #include "utils/timestamp.h"
25 /* ----------------------------------------------------------------
26 * time types + support macros
28 * String definitions for standard time quantities.
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 * ----------------------------------------------------------------
36 #define DCURRENT "current"
38 #define INVALID "invalid"
39 #define EARLY "-infinity"
40 #define LATE "infinity"
43 #define TOMORROW "tomorrow"
44 #define YESTERDAY "yesterday"
47 #define DMICROSEC "usecond"
48 #define DMILLISEC "msecond"
49 #define DSECOND "second"
50 #define DMINUTE "minute"
54 #define DMONTH "month"
55 #define DQUARTER "quarter"
57 #define DDECADE "decade"
58 #define DCENTURY "century"
59 #define DMILLENNIUM "millennium"
62 #define DTIMEZONE "timezone"
65 * Fundamental time field definitions for parsing.
67 * Meridian: am, pm, or 24-hour style.
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.
88 #define TIMES 4 /* not used - thomas 1997-07-14 */
101 /* these are only for relative dates */
103 #define ABS_BEFORE 18
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.
113 * Not all of these fields are used for masks in DecodeDateDelta
114 * so allow some larger than 31. - thomas 1997-11-17
125 #define DTK_SPECIAL 6
126 #define DTK_INVALID 7
127 #define DTK_CURRENT 8
132 #define DTK_YESTERDAY 13
134 #define DTK_TOMORROW 15
138 #define DTK_SECOND 18
139 #define DTK_MINUTE 19
144 #define DTK_QUARTER 24
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
154 #define DTK_TZ_HOUR 34
155 #define DTK_TZ_MINUTE 35
158 * Bit mask definitions for time parsing.
161 #define DTK_M(t) (0x01 << (t))
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))
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
170 #define TOKMAXLEN 10 /* only this many chars are stored in
173 /* keep this struct small; it gets used a lot */
179 char token[TOKMAXLEN];
182 char value; /* this may be unsigned, alas */
187 * Macro to replace modf(), which is broken on some platforms.
189 #define TMODULO(t,q,u) \
191 q = ((t < 0)? ceil(t / u): floor(t / u)); \
198 * Date/time validation
199 * Include check for leap year.
202 extern int day_tab[2][13];
204 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
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
211 #define JULIAN_MINYEAR (-4713)
212 #define JULIAN_MINMONTH (11)
213 #define JULIAN_MINDAY (23)
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)))))
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)
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))))))
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);
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,
243 struct tm * tm, double *fsec, int *tzp);
245 extern int DecodeTimeOnly(char **field, int *ftype,
247 struct tm * tm, double *fsec, int *tzp);
249 extern int DecodeDateDelta(char **field, int *ftype,
251 struct tm * tm, double *fsec);
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);
258 extern int DecodeSpecial(int field, char *lowtoken, int *val);
259 extern int DecodeUnits(int field, char *lowtoken, int *val);
261 extern int j2day(int jd);
263 #endif /* DATETIME_H */