PostgreSQL Source Code  git master
dt.h
Go to the documentation of this file.
1 /* src/interfaces/ecpg/pgtypeslib/dt.h */
2 
3 #ifndef DT_H
4 #define DT_H
5 
6 #include <pgtypes_timestamp.h>
7 
8 #include <time.h>
9 
10 #define MAXTZLEN 10
11 
12 typedef int32 fsec_t;
13 
14 #define USE_POSTGRES_DATES 0
15 #define USE_ISO_DATES 1
16 #define USE_SQL_DATES 2
17 #define USE_GERMAN_DATES 3
18 
19 #define INTSTYLE_POSTGRES 0
20 #define INTSTYLE_POSTGRES_VERBOSE 1
21 #define INTSTYLE_SQL_STANDARD 2
22 #define INTSTYLE_ISO_8601 3
23 
24 #define INTERVAL_FULL_RANGE (0x7FFF)
25 #define INTERVAL_MASK(b) (1 << (b))
26 #define MAX_INTERVAL_PRECISION 6
27 
28 #define DTERR_BAD_FORMAT (-1)
29 #define DTERR_FIELD_OVERFLOW (-2)
30 #define DTERR_MD_FIELD_OVERFLOW (-3) /* triggers hint about DateStyle */
31 #define DTERR_INTERVAL_OVERFLOW (-4)
32 #define DTERR_TZDISP_OVERFLOW (-5)
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  * Field types for time decoding.
80  *
81  * Can't have more of these than there are bits in an unsigned int
82  * since these are turned into bit masks during parsing and decoding.
83  *
84  * Furthermore, the values for YEAR, MONTH, DAY, HOUR, MINUTE, SECOND
85  * must be in the range 0..14 so that the associated bitmasks can fit
86  * into the left half of an INTERVAL's typmod value.
87  *
88  * Copy&pasted these values from src/include/utils/datetime.h
89  * 2008-11-20, changing a number of their values.
90  */
91 
92 #define RESERV 0
93 #define MONTH 1
94 #define YEAR 2
95 #define DAY 3
96 #define JULIAN 4
97 #define TZ 5 /* fixed-offset timezone abbreviation */
98 #define DTZ 6 /* fixed-offset timezone abbrev, DST */
99 #define DYNTZ 7 /* dynamic timezone abbr (unimplemented) */
100 #define IGNORE_DTF 8
101 #define AMPM 9
102 #define HOUR 10
103 #define MINUTE 11
104 #define SECOND 12
105 #define MILLISECOND 13
106 #define MICROSECOND 14
107 #define DOY 15
108 #define DOW 16
109 #define UNITS 17
110 #define ADBC 18
111 /* these are only for relative dates */
112 #define AGO 19
113 #define ABS_BEFORE 20
114 #define ABS_AFTER 21
115 /* generic fields to help with parsing */
116 #define ISODATE 22
117 #define ISOTIME 23
118 /* hack for parsing two-word timezone specs "MET DST" etc */
119 #define DTZMOD 28 /* "DST" as a separate word */
120 /* reserved for unrecognized string values */
121 #define UNKNOWN_FIELD 31
122 
123 
124 /*
125  * Token field definitions for time parsing and decoding.
126  *
127  * Some field type codes (see above) use these as the "value" in datetktbl[].
128  * These are also used for bit masks in DecodeDateTime and friends
129  * so actually restrict them to within [0,31] for now.
130  * - thomas 97/06/19
131  * Not all of these fields are used for masks in DecodeDateTime
132  * so allow some larger than 31. - thomas 1997-11-17
133  *
134  * Caution: there are undocumented assumptions in the code that most of these
135  * values are not equal to IGNORE_DTF nor RESERV. Be very careful when
136  * renumbering values in either of these apparently-independent lists :-(
137  */
138 
139 #define DTK_NUMBER 0
140 #define DTK_STRING 1
141 
142 #define DTK_DATE 2
143 #define DTK_TIME 3
144 #define DTK_TZ 4
145 #define DTK_AGO 5
146 
147 #define DTK_SPECIAL 6
148 #define DTK_EARLY 9
149 #define DTK_LATE 10
150 #define DTK_EPOCH 11
151 #define DTK_NOW 12
152 #define DTK_YESTERDAY 13
153 #define DTK_TODAY 14
154 #define DTK_TOMORROW 15
155 #define DTK_ZULU 16
156 
157 #define DTK_DELTA 17
158 #define DTK_SECOND 18
159 #define DTK_MINUTE 19
160 #define DTK_HOUR 20
161 #define DTK_DAY 21
162 #define DTK_WEEK 22
163 #define DTK_MONTH 23
164 #define DTK_QUARTER 24
165 #define DTK_YEAR 25
166 #define DTK_DECADE 26
167 #define DTK_CENTURY 27
168 #define DTK_MILLENNIUM 28
169 #define DTK_MILLISEC 29
170 #define DTK_MICROSEC 30
171 #define DTK_JULIAN 31
172 
173 #define DTK_DOW 32
174 #define DTK_DOY 33
175 #define DTK_TZ_HOUR 34
176 #define DTK_TZ_MINUTE 35
177 #define DTK_ISOYEAR 36
178 #define DTK_ISODOW 37
179 
180 
181 /*
182  * Bit mask definitions for time parsing.
183  */
184 /* Copy&pasted these values from src/include/utils/datetime.h */
185 #define DTK_M(t) (0x01 << (t))
186 #define DTK_ALL_SECS_M (DTK_M(SECOND) | DTK_M(MILLISECOND) | DTK_M(MICROSECOND))
187 #define DTK_DATE_M (DTK_M(YEAR) | DTK_M(MONTH) | DTK_M(DAY))
188 #define DTK_TIME_M (DTK_M(HOUR) | DTK_M(MINUTE) | DTK_M(SECOND))
189 
190 /*
191  * Working buffer size for input and output of interval, timestamp, etc.
192  * Inputs that need more working space will be rejected early. Longer outputs
193  * will overrun buffers, so this must suffice for all possible output. As of
194  * this writing, PGTYPESinterval_to_asc() needs the most space at ~90 bytes.
195  */
196 #define MAXDATELEN 128
197 /* maximum possible number of fields in a date string */
198 #define MAXDATEFIELDS 25
199 /* only this many chars are stored in datetktbl */
200 #define TOKMAXLEN 10
201 
202 /* keep this struct small; it gets used a lot */
203 typedef struct
204 {
205  char token[TOKMAXLEN + 1]; /* always NUL-terminated */
206  char type; /* see field type codes above */
207  int32 value; /* meaning depends on type */
208 } datetkn;
209 
210 
211 /* FMODULO()
212  * Macro to replace modf(), which is broken on some platforms.
213  * t = input and remainder
214  * q = integer part
215  * u = divisor
216  */
217 #define FMODULO(t,q,u) \
218 do { \
219  (q) = (((t) < 0) ? ceil((t) / (u)): floor((t) / (u))); \
220  if ((q) != 0) (t) -= rint((q) * (u)); \
221 } while(0)
222 
223 /* TMODULO()
224  * Like FMODULO(), but work on the timestamp datatype (now always int64).
225  * We assume that int64 follows the C99 semantics for division (negative
226  * quotients truncate towards zero).
227  */
228 #define TMODULO(t,q,u) \
229 do { \
230  (q) = ((t) / (u)); \
231  if ((q) != 0) (t) -= ((q) * (u)); \
232 } while(0)
233 
234 /* in both timestamp.h and ecpg/dt.h */
235 #define DAYS_PER_YEAR 365.25 /* assumes leap year every four years */
236 #define MONTHS_PER_YEAR 12
237 /*
238  * DAYS_PER_MONTH is very imprecise. The more accurate value is
239  * 365.2425/12 = 30.436875, or '30 days 10:29:06'. Right now we only
240  * return an integral number of days, but someday perhaps we should
241  * also return a 'time' value to be used as well. ISO 8601 suggests
242  * 30 days.
243  */
244 #define DAYS_PER_MONTH 30 /* assumes exactly 30 days per month */
245 #define HOURS_PER_DAY 24 /* assume no daylight savings time changes */
246 
247 /*
248  * This doesn't adjust for uneven daylight savings time intervals or leap
249  * seconds, and it crudely estimates leap years. A more accurate value
250  * for days per years is 365.2422.
251  */
252 #define SECS_PER_YEAR (36525 * 864) /* avoid floating-point computation */
253 #define SECS_PER_DAY 86400
254 #define SECS_PER_HOUR 3600
255 #define SECS_PER_MINUTE 60
256 #define MINS_PER_HOUR 60
257 
258 #define USECS_PER_DAY INT64CONST(86400000000)
259 #define USECS_PER_HOUR INT64CONST(3600000000)
260 #define USECS_PER_MINUTE INT64CONST(60000000)
261 #define USECS_PER_SEC INT64CONST(1000000)
262 
263 /*
264  * Date/time validation
265  * Include check for leap year.
266  */
267 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
268 
269 /*
270  * Julian date support --- see comments in backend's timestamp.h.
271  */
272 
273 #define JULIAN_MINYEAR (-4713)
274 #define JULIAN_MINMONTH (11)
275 #define JULIAN_MINDAY (24)
276 #define JULIAN_MAXYEAR (5874898)
277 #define JULIAN_MAXMONTH (6)
278 #define JULIAN_MAXDAY (3)
279 
280 #define IS_VALID_JULIAN(y,m,d) \
281  (((y) > JULIAN_MINYEAR || \
282  ((y) == JULIAN_MINYEAR && ((m) >= JULIAN_MINMONTH))) && \
283  ((y) < JULIAN_MAXYEAR || \
284  ((y) == JULIAN_MAXYEAR && ((m) < JULIAN_MAXMONTH))))
285 
286 #define MIN_TIMESTAMP INT64CONST(-211813488000000000)
287 #define END_TIMESTAMP INT64CONST(9223371331200000000)
288 
289 #define IS_VALID_TIMESTAMP(t) (MIN_TIMESTAMP <= (t) && (t) < END_TIMESTAMP)
290 
291 #define UTIME_MINYEAR (1901)
292 #define UTIME_MINMONTH (12)
293 #define UTIME_MINDAY (14)
294 #define UTIME_MAXYEAR (2038)
295 #define UTIME_MAXMONTH (01)
296 #define UTIME_MAXDAY (18)
297 
298 #define IS_VALID_UTIME(y,m,d) ((((y) > UTIME_MINYEAR) \
299  || (((y) == UTIME_MINYEAR) && (((m) > UTIME_MINMONTH) \
300  || (((m) == UTIME_MINMONTH) && ((d) >= UTIME_MINDAY))))) \
301  && (((y) < UTIME_MAXYEAR) \
302  || (((y) == UTIME_MAXYEAR) && (((m) < UTIME_MAXMONTH) \
303  || (((m) == UTIME_MAXMONTH) && ((d) <= UTIME_MAXDAY))))))
304 
305 #define DT_NOBEGIN (-INT64CONST(0x7fffffffffffffff) - 1)
306 #define DT_NOEND (INT64CONST(0x7fffffffffffffff))
307 
308 #define TIMESTAMP_NOBEGIN(j) do {(j) = DT_NOBEGIN;} while (0)
309 #define TIMESTAMP_NOEND(j) do {(j) = DT_NOEND;} while (0)
310 #define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN)
311 #define TIMESTAMP_IS_NOEND(j) ((j) == DT_NOEND)
312 #define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
313 
314 int DecodeInterval(char **field, int *ftype, int nf, int *dtype, struct tm *tm, fsec_t *fsec);
315 int DecodeTime(char *str, int *tmask, struct tm *tm, fsec_t *fsec);
316 void EncodeDateTime(struct tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str, bool EuroDates);
317 void EncodeInterval(struct tm *tm, fsec_t fsec, int style, char *str);
318 int tm2timestamp(struct tm *tm, fsec_t fsec, int *tzp, timestamp * result);
319 int DecodeUnits(int field, char *lowtoken, int *val);
320 bool CheckDateTokenTables(void);
321 void EncodeDateOnly(struct tm *tm, int style, char *str, bool EuroDates);
322 int GetEpochTime(struct tm *tm);
323 int ParseDateTime(char *timestr, char *lowstr, char **field, int *ftype, int *numfields, char **endstr);
324 int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct tm *tm, fsec_t *fsec, bool EuroDates);
325 void j2date(int jd, int *year, int *month, int *day);
326 void GetCurrentDateTime(struct tm *tm);
327 int date2j(int y, int m, int d);
328 void TrimTrailingZeros(char *str);
329 void dt2time(double jd, int *hour, int *min, int *sec, fsec_t *fsec);
330 int PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp * d,
331  int *year, int *month, int *day,
332  int *hour, int *minute, int *second,
333  int *tz);
334 
335 extern char *pgtypes_date_weekdays_short[];
336 extern char *pgtypes_date_months[];
337 extern char *months[];
338 extern char *days[];
339 extern const int day_tab[2][13];
340 
341 #endif /* DT_H */
signed int int32
Definition: c.h:482
int32 fsec_t
Definition: timestamp.h:41
void EncodeDateOnly(struct tm *tm, int style, char *str, bool EuroDates)
Definition: dt_common.c:669
int GetEpochTime(struct tm *tm)
Definition: dt_common.c:949
int PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp *d, int *year, int *month, int *day, int *hour, int *minute, int *second, int *tz)
Definition: dt_common.c:2521
const int day_tab[2][13]
Definition: datetime.c:75
char * days[]
Definition: datetime.c:84
int32 fsec_t
Definition: dt.h:12
void EncodeDateTime(struct tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str, bool EuroDates)
Definition: dt_common.c:753
void TrimTrailingZeros(char *str)
Definition: dt_common.c:722
void GetCurrentDateTime(struct tm *tm)
Definition: dt_common.c:1060
int DecodeTime(char *str, int *tmask, struct tm *tm, fsec_t *fsec)
Definition: dt_common.c:1437
int date2j(int y, int m, int d)
Definition: datetime.c:286
void EncodeInterval(struct tm *tm, fsec_t fsec, int style, char *str)
Definition: interval.c:759
int ParseDateTime(char *timestr, char *lowstr, char **field, int *ftype, int *numfields, char **endstr)
Definition: dt_common.c:1600
bool CheckDateTokenTables(void)
Definition: datetime.c:4811
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:311
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct tm *tm, fsec_t *fsec, bool EuroDates)
Definition: dt_common.c:1782
char * months[]
Definition: datetime.c:81
int DecodeInterval(char **field, int *ftype, int nf, int *dtype, struct tm *tm, fsec_t *fsec)
Definition: interval.c:326
char * pgtypes_date_months[]
Definition: dt_common.c:499
char * pgtypes_date_weekdays_short[]
Definition: dt_common.c:497
void dt2time(double jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: dt_common.c:1068
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: dt_common.c:536
#define TOKMAXLEN
Definition: dt.h:200
int tm2timestamp(struct tm *tm, fsec_t fsec, int *tzp, timestamp *result)
Definition: timestamp.c:41
const char * str
#define token
Definition: indent_globs.h:126
static struct @160 value
long val
Definition: informix.c:689
int y
Definition: isn.c:71
static void const char * fmt
static struct pg_tm tm
Definition: localtime.c:104
int64 timestamp
const char * type