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-2009, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
14 *-------------------------------------------------------------------------
22 #include "utils/timestamp.h"
23 #include "utils/tzparser.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 alternative 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. Since those bits
88 * are stored in typmods, you can't change them without initdb!
104 #define MILLISECOND 13
105 #define MICROSECOND 14
110 /* these are only for relative dates */
112 #define ABS_BEFORE 20
114 /* generic fields to help with parsing */
117 /* these are only for parsing intervals */
121 #define MILLENNIUM 27
122 /* reserved for unrecognized string values */
123 #define UNKNOWN_FIELD 31
126 * Token field definitions for time parsing and decoding.
127 * These need to fit into the datetkn table type.
128 * At the moment, that means keep them within [-127,127].
129 * These are also used for bit masks in DecodeDateDelta()
130 * so actually restrict them to within [0,31] for now.
132 * Not all of these fields are used for masks in DecodeDateDelta
133 * so allow some larger than 31. - thomas 1997-11-17
144 #define DTK_SPECIAL 6
145 #define DTK_INVALID 7
146 #define DTK_CURRENT 8
151 #define DTK_YESTERDAY 13
153 #define DTK_TOMORROW 15
157 #define DTK_SECOND 18
158 #define DTK_MINUTE 19
163 #define DTK_QUARTER 24
165 #define DTK_DECADE 26
166 #define DTK_CENTURY 27
167 #define DTK_MILLENNIUM 28
168 #define DTK_MILLISEC 29
169 #define DTK_MICROSEC 30
170 #define DTK_JULIAN 31
174 #define DTK_TZ_HOUR 34
175 #define DTK_TZ_MINUTE 35
176 #define DTK_ISOYEAR 36
177 #define DTK_ISODOW 37
181 * Bit mask definitions for time parsing.
184 #define DTK_M(t) (0x01 << (t))
186 /* Convenience: a second, plus any fractional component */
187 #define DTK_ALL_SECS_M (DTK_M(SECOND) | DTK_M(MILLISECOND) | DTK_M(MICROSECOND))
188 #define DTK_DATE_M (DTK_M(YEAR) | DTK_M(MONTH) | DTK_M(DAY))
189 #define DTK_TIME_M (DTK_M(HOUR) | DTK_M(MINUTE) | DTK_ALL_SECS_M)
191 #define MAXDATELEN 63 /* maximum possible length of an input date
192 * string (not counting tr. null) */
193 #define MAXDATEFIELDS 25 /* maximum possible number of fields in a date
195 #define TOKMAXLEN 10 /* only this many chars are stored in
198 /* keep this struct small; it gets used a lot */
201 char token
[TOKMAXLEN
];
203 char value
; /* this may be unsigned, alas */
208 * Macro to replace modf(), which is broken on some platforms.
209 * t = input and remainder
213 #define FMODULO(t,q,u) \
215 (q) = (((t) < 0) ? ceil((t) / (u)) : floor((t) / (u))); \
216 if ((q) != 0) (t) -= rint((q) * (u)); \
220 * Like FMODULO(), but work on the timestamp datatype (either int64 or float8).
221 * We assume that int64 follows the C99 semantics for division (negative
222 * quotients truncate towards zero).
224 #ifdef HAVE_INT64_TIMESTAMP
225 #define TMODULO(t,q,u) \
228 if ((q) != 0) (t) -= ((q) * (u)); \
231 #define TMODULO(t,q,u) \
233 (q) = (((t) < 0) ? ceil((t) / (u)) : floor((t) / (u))); \
234 if ((q) != 0) (t) -= rint((q) * (u)); \
239 * Date/time validation
240 * Include check for leap year.
243 extern const int day_tab
[2][13];
245 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
248 /* Julian date support for date2j() and j2date()
250 * IS_VALID_JULIAN checks the minimum date exactly, but is a bit sloppy
251 * about the maximum, since it's far enough out to not be especially
255 #define JULIAN_MINYEAR (-4713)
256 #define JULIAN_MINMONTH (11)
257 #define JULIAN_MINDAY (24)
258 #define JULIAN_MAXYEAR (5874898)
260 #define IS_VALID_JULIAN(y,m,d) ((((y) > JULIAN_MINYEAR) \
261 || (((y) == JULIAN_MINYEAR) && (((m) > JULIAN_MINMONTH) \
262 || (((m) == JULIAN_MINMONTH) && ((d) >= JULIAN_MINDAY))))) \
263 && ((y) < JULIAN_MAXYEAR))
265 /* Julian-date equivalents of Day 0 in Unix and Postgres reckoning */
266 #define UNIX_EPOCH_JDATE 2440588 /* == date2j(1970, 1, 1) */
267 #define POSTGRES_EPOCH_JDATE 2451545 /* == date2j(2000, 1, 1) */
271 * Datetime input parsing routines (ParseDateTime, DecodeDateTime, etc)
272 * return zero or a positive value on success. On failure, they return
273 * one of these negative code values. DateTimeParseError may be used to
274 * produce a correct ereport.
276 #define DTERR_BAD_FORMAT (-1)
277 #define DTERR_FIELD_OVERFLOW (-2)
278 #define DTERR_MD_FIELD_OVERFLOW (-3) /* triggers hint about DateStyle */
279 #define DTERR_INTERVAL_OVERFLOW (-4)
280 #define DTERR_TZDISP_OVERFLOW (-5)
283 extern void GetCurrentDateTime(struct pg_tm
* tm
);
284 extern void GetCurrentTimeUsec(struct pg_tm
* tm
, fsec_t
*fsec
, int *tzp
);
285 extern void j2date(int jd
, int *year
, int *month
, int *day
);
286 extern int date2j(int year
, int month
, int day
);
288 extern int ParseDateTime(const char *timestr
, char *workbuf
, size_t buflen
,
289 char **field
, int *ftype
,
290 int maxfields
, int *numfields
);
291 extern int DecodeDateTime(char **field
, int *ftype
,
293 struct pg_tm
* tm
, fsec_t
*fsec
, int *tzp
);
294 extern int DecodeTimeOnly(char **field
, int *ftype
,
296 struct pg_tm
* tm
, fsec_t
*fsec
, int *tzp
);
297 extern int DecodeInterval(char **field
, int *ftype
, int nf
, int range
,
298 int *dtype
, struct pg_tm
* tm
, fsec_t
*fsec
);
299 extern int DecodeISO8601Interval(char *str
,
300 int *dtype
, struct pg_tm
* tm
, fsec_t
*fsec
);
302 extern void DateTimeParseError(int dterr
, const char *str
,
303 const char *datatype
);
305 extern int DetermineTimeZoneOffset(struct pg_tm
* tm
, pg_tz
*tzp
);
307 extern void EncodeDateOnly(struct pg_tm
* tm
, int style
, char *str
);
308 extern void EncodeTimeOnly(struct pg_tm
* tm
, fsec_t fsec
, int *tzp
, int style
, char *str
);
309 extern void EncodeDateTime(struct pg_tm
* tm
, fsec_t fsec
, int *tzp
, char **tzn
, int style
, char *str
);
310 extern void EncodeInterval(struct pg_tm
* tm
, fsec_t fsec
, int style
, char *str
);
312 extern int DecodeSpecial(int field
, char *lowtoken
, int *val
);
313 extern int DecodeUnits(int field
, char *lowtoken
, int *val
);
315 extern int j2day(int jd
);
317 extern bool CheckDateTokenTables(void);
318 extern void InstallTimeZoneAbbrevs(tzEntry
*abbrevs
, int n
);
320 extern Datum
pg_timezone_abbrevs(PG_FUNCTION_ARGS
);
321 extern Datum
pg_timezone_names(PG_FUNCTION_ARGS
);
323 #endif /* DATETIME_H */