1 #include "postgres_fe.h"
9 #include "pgtypes_timestamp.h"
11 static int day_tab[2][13] = {
12 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0},
13 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}};
15 typedef long AbsoluteTime;
17 #define ABS_SIGNBIT ((char) 0200)
19 #define NEG(n) ((n)|ABS_SIGNBIT)
20 #define FROMVAL(tp) (-SIGNEDCHAR((tp)->value) * 15) /* uncompress */
21 #define VALMASK ((char) 0177)
22 #define SIGNEDCHAR(c) ((c)&ABS_SIGNBIT? -((c)&VALMASK): (c))
24 static datetkn datetktbl[] = {
25 /* text, token, lexval */
26 {EARLY, RESERV, DTK_EARLY}, /* "-infinity" reserved for "early time" */
27 {"abstime", IGNORE_DTF, 0}, /* for pre-v6.1 "Invalid Abstime" */
28 {"acsst", DTZ, POS(42)}, /* Cent. Australia */
29 {"acst", DTZ, NEG(16)}, /* Atlantic/Porto Acre */
30 {"act", TZ, NEG(20)}, /* Atlantic/Porto Acre */
31 {DA_D, ADBC, AD}, /* "ad" for years >= 0 */
32 {"adt", DTZ, NEG(12)}, /* Atlantic Daylight Time */
33 {"aesst", DTZ, POS(44)}, /* E. Australia */
34 {"aest", TZ, POS(40)}, /* Australia Eastern Std Time */
35 {"aft", TZ, POS(18)}, /* Kabul */
36 {"ahst", TZ, NEG(40)}, /* Alaska-Hawaii Std Time */
37 {"akdt", DTZ, NEG(32)}, /* Alaska Daylight Time */
38 {"akst", DTZ, NEG(36)}, /* Alaska Standard Time */
39 {"allballs", RESERV, DTK_ZULU}, /* 00:00:00 */
40 {"almst", TZ, POS(28)}, /* Almaty Savings Time */
41 {"almt", TZ, POS(24)}, /* Almaty Time */
43 {"amst", DTZ, POS(20)}, /* Armenia Summer Time (Yerevan) */
45 {"amst", DTZ, NEG(12)}, /* Porto Velho */
47 {"amt", TZ, POS(16)}, /* Armenia Time (Yerevan) */
48 {"anast", DTZ, POS(52)}, /* Anadyr Summer Time (Russia) */
49 {"anat", TZ, POS(48)}, /* Anadyr Time (Russia) */
57 {"art", TZ, NEG(12)}, /* Argentina Time */
60 ast /* Atlantic Standard Time, Arabia Standard
61 * Time, Acre Standard Time */
63 {"ast", TZ, NEG(16)}, /* Atlantic Std Time (Canada) */
64 {"at", IGNORE_DTF, 0}, /* "at" (throwaway) */
67 {"awsst", DTZ, POS(36)}, /* W. Australia */
68 {"awst", TZ, POS(32)}, /* W. Australia */
69 {"awt", DTZ, NEG(12)},
70 {"azost", DTZ, POS(0)}, /* Azores Summer Time */
71 {"azot", TZ, NEG(4)}, /* Azores Time */
72 {"azst", DTZ, POS(20)}, /* Azerbaijan Summer Time */
73 {"azt", TZ, POS(16)}, /* Azerbaijan Time */
74 {DB_C, ADBC, BC}, /* "bc" for years < 0 */
75 {"bdst", TZ, POS(8)}, /* British Double Summer Time */
76 {"bdt", TZ, POS(24)}, /* Dacca */
77 {"bnt", TZ, POS(32)}, /* Brunei Darussalam Time */
78 {"bort", TZ, POS(32)}, /* Borneo Time (Indonesia) */
83 {"bot", TZ, NEG(16)}, /* Bolivia Time */
84 {"bra", TZ, NEG(12)}, /* Brazil Time */
89 {"bst", DTZ, POS(4)}, /* British Summer Time */
91 {"bst", TZ, NEG(12)}, /* Brazil Standard Time */
92 {"bst", DTZ, NEG(44)}, /* Bering Summer Time */
94 {"bt", TZ, POS(12)}, /* Baghdad Time */
95 {"btt", TZ, POS(24)}, /* Bhutan Time */
96 {"cadt", DTZ, POS(42)}, /* Central Australian DST */
97 {"cast", TZ, POS(38)}, /* Central Australian ST */
98 {"cat", TZ, NEG(40)}, /* Central Alaska Time */
99 {"cct", TZ, POS(32)}, /* China Coast Time */
101 {"cct", TZ, POS(26)}, /* Indian Cocos (Island) Time */
103 {"cdt", DTZ, NEG(20)}, /* Central Daylight Time */
104 {"cest", DTZ, POS(8)}, /* Central European Dayl.Time */
105 {"cet", TZ, POS(4)}, /* Central European Time */
106 {"cetdst", DTZ, POS(8)}, /* Central European Dayl.Time */
107 {"chadt", DTZ, POS(55)}, /* Chatham Island Daylight Time (13:45) */
108 {"chast", TZ, POS(51)}, /* Chatham Island Time (12:45) */
112 {"ckt", TZ, POS(48)}, /* Cook Islands Time */
113 {"clst", DTZ, NEG(12)}, /* Chile Summer Time */
114 {"clt", TZ, NEG(16)}, /* Chile Time */
118 {"cot", TZ, NEG(20)}, /* Columbia Time */
119 {"cst", TZ, NEG(24)}, /* Central Standard Time */
120 {DCURRENT, RESERV, DTK_CURRENT}, /* "current" is always now */
124 {"cvt", TZ, POS(28)}, /* Christmas Island Time (Indian Ocean) */
125 {"cxt", TZ, POS(28)}, /* Christmas Island Time (Indian Ocean) */
126 {"d", UNITS, DTK_DAY}, /* "day of month" for ISO input */
127 {"davt", TZ, POS(28)}, /* Davis Time (Antarctica) */
128 {"ddut", TZ, POS(40)}, /* Dumont-d'Urville Time (Antarctica) */
130 {"december", MONTH, 12},
131 {"dnt", TZ, POS(4)}, /* Dansk Normal Tid */
132 {"dow", RESERV, DTK_DOW}, /* day of week */
133 {"doy", RESERV, DTK_DOY}, /* day of year */
136 {"dusst", DTZ, POS(24)}, /* Dushanbe Summer Time */
138 {"easst", DTZ, NEG(20)}, /* Easter Island Summer Time */
139 {"east", TZ, NEG(24)}, /* Easter Island Time */
140 {"eat", TZ, POS(12)}, /* East Africa Time */
142 {"east", DTZ, POS(16)}, /* Indian Antananarivo Savings Time */
143 {"eat", TZ, POS(12)}, /* Indian Antananarivo Time */
144 {"ect", TZ, NEG(16)}, /* Eastern Caribbean Time */
145 {"ect", TZ, NEG(20)}, /* Ecuador Time */
147 {"edt", DTZ, NEG(16)}, /* Eastern Daylight Time */
148 {"eest", DTZ, POS(12)}, /* Eastern Europe Summer Time */
149 {"eet", TZ, POS(8)}, /* East. Europe, USSR Zone 1 */
150 {"eetdst", DTZ, POS(12)}, /* Eastern Europe Daylight Time */
151 {"egst", DTZ, POS(0)}, /* East Greenland Summer Time */
152 {"egt", TZ, NEG(4)}, /* East Greenland Time */
156 {EPOCH, RESERV, DTK_EPOCH}, /* "epoch" reserved for system epoch time */
157 {"est", TZ, NEG(20)}, /* Eastern Standard Time */
159 {"february", MONTH, 2},
160 {"fjst", DTZ, NEG(52)}, /* Fiji Summer Time (13 hour offset!) */
161 {"fjt", TZ, NEG(48)}, /* Fiji Time */
162 {"fkst", DTZ, NEG(12)}, /* Falkland Islands Summer Time */
163 {"fkt", TZ, NEG(8)}, /* Falkland Islands Time */
170 {"fst", TZ, POS(4)}, /* French Summer Time */
171 {"fwt", DTZ, POS(8)}, /* French Winter Time */
172 {"galt", TZ, NEG(24)}, /* Galapagos Time */
173 {"gamt", TZ, NEG(36)}, /* Gambier Time */
174 {"gest", DTZ, POS(20)}, /* Georgia Summer Time */
175 {"get", TZ, POS(16)}, /* Georgia Time */
176 {"gft", TZ, NEG(12)}, /* French Guiana Time */
180 {"gilt", TZ, POS(48)}, /* Gilbert Islands Time */
181 {"gmt", TZ, POS(0)}, /* Greenwish Mean Time */
182 {"gst", TZ, POS(40)}, /* Guam Std Time, USSR Zone 9 */
183 {"gyt", TZ, NEG(16)}, /* Guyana Time */
184 {"h", UNITS, DTK_HOUR}, /* "hour" */
189 {"hdt", DTZ, NEG(36)}, /* Hawaii/Alaska Daylight Time */
193 {"hkt", TZ, POS(32)}, /* Hong Kong Time */
195 {"hmt", TZ, POS(12)}, /* Hellas ? ? */
199 {"hst", TZ, NEG(40)}, /* Hawaii Std Time */
203 {"ict", TZ, POS(28)}, /* Indochina Time */
204 {"idle", TZ, POS(48)}, /* Intl. Date Line, East */
205 {"idlw", TZ, NEG(48)}, /* Intl. Date Line, West */
207 idt /* Israeli, Iran, Indian Daylight Time */
209 {LATE, RESERV, DTK_LATE}, /* "infinity" reserved for "late time" */
210 {INVALID, RESERV, DTK_INVALID}, /* "invalid" reserved for bad time */
211 {"iot", TZ, POS(20)}, /* Indian Chagos Time */
212 {"irkst", DTZ, POS(36)}, /* Irkutsk Summer Time */
213 {"irkt", TZ, POS(32)}, /* Irkutsk Time */
214 {"irt", TZ, POS(14)}, /* Iran Time */
218 {"ist", TZ, POS(8)}, /* Israel */
219 {"it", TZ, POS(14)}, /* Iran Time */
220 {"j", UNITS, DTK_JULIAN},
222 {"january", MONTH, 1},
223 {"javt", TZ, POS(28)}, /* Java Time (07:00? see JT) */
224 {"jayt", TZ, POS(36)}, /* Jayapura Time (Indonesia) */
225 {"jd", UNITS, DTK_JULIAN},
226 {"jst", TZ, POS(36)}, /* Japan Std Time,USSR Zone 8 */
227 {"jt", TZ, POS(30)}, /* Java Time (07:30? see JAVT) */
229 {"julian", UNITS, DTK_JULIAN},
233 {"kdt", DTZ, POS(40)}, /* Korea Daylight Time */
234 {"kgst", DTZ, POS(24)}, /* Kyrgyzstan Summer Time */
235 {"kgt", TZ, POS(20)}, /* Kyrgyzstan Time */
236 {"kost", TZ, POS(48)}, /* Kosrae Time */
237 {"krast", DTZ, POS(28)}, /* Krasnoyarsk Summer Time */
238 {"krat", TZ, POS(32)}, /* Krasnoyarsk Standard Time */
239 {"kst", TZ, POS(36)}, /* Korea Standard Time */
240 {"lhdt", DTZ, POS(44)}, /* Lord Howe Daylight Time, Australia */
241 {"lhst", TZ, POS(42)}, /* Lord Howe Standard Time, Australia */
242 {"ligt", TZ, POS(40)}, /* From Melbourne, Australia */
243 {"lint", TZ, POS(56)}, /* Line Islands Time (Kiribati; +14
245 {"lkt", TZ, POS(24)}, /* Lanka Time */
246 {"m", UNITS, DTK_MONTH}, /* "month" for ISO input */
247 {"magst", DTZ, POS(48)}, /* Magadan Summer Time */
248 {"magt", TZ, POS(44)}, /* Magadan Time */
251 {"mart", TZ, NEG(38)}, /* Marquesas Time */
252 {"mawt", TZ, POS(24)}, /* Mawson, Antarctica */
254 {"mdt", DTZ, NEG(24)}, /* Mountain Daylight Time */
255 {"mest", DTZ, POS(8)}, /* Middle Europe Summer Time */
256 {"met", TZ, POS(4)}, /* Middle Europe Time */
257 {"metdst", DTZ, POS(8)}, /* Middle Europe Daylight Time */
258 {"mewt", TZ, POS(4)}, /* Middle Europe Winter Time */
259 {"mez", TZ, POS(4)}, /* Middle Europe Zone */
260 {"mht", TZ, POS(48)}, /* Kwajalein */
261 {"mm", UNITS, DTK_MINUTE}, /* "minute" for ISO input */
262 {"mmt", TZ, POS(26)}, /* Myannar Time */
268 {"mpt", TZ, POS(40)}, /* North Mariana Islands Time */
269 {"msd", DTZ, POS(16)}, /* Moscow Summer Time */
270 {"msk", TZ, POS(12)}, /* Moscow Time */
271 {"mst", TZ, NEG(28)}, /* Mountain Standard Time */
272 {"mt", TZ, POS(34)}, /* Moluccas Time */
273 {"mut", TZ, POS(16)}, /* Mauritius Island Time */
274 {"mvt", TZ, POS(20)}, /* Maldives Island Time */
275 {"myt", TZ, POS(32)}, /* Malaysia Time */
279 {"nct", TZ, POS(44)}, /* New Caledonia Time */
280 {"ndt", DTZ, NEG(10)}, /* Nfld. Daylight Time */
281 {"nft", TZ, NEG(14)}, /* Newfoundland Standard Time */
282 {"nor", TZ, POS(4)}, /* Norway Standard Time */
284 {"november", MONTH, 11},
285 {"novst", DTZ, POS(28)}, /* Novosibirsk Summer Time */
286 {"novt", TZ, POS(24)}, /* Novosibirsk Standard Time */
287 {NOW, RESERV, DTK_NOW}, /* current transaction time */
288 {"npt", TZ, POS(23)}, /* Nepal Standard Time (GMT-5:45) */
289 {"nst", TZ, NEG(14)}, /* Nfld. Standard Time */
290 {"nt", TZ, NEG(44)}, /* Nome Time */
291 {"nut", TZ, NEG(44)}, /* Niue Time */
292 {"nzdt", DTZ, POS(52)}, /* New Zealand Daylight Time */
293 {"nzst", TZ, POS(48)}, /* New Zealand Standard Time */
294 {"nzt", TZ, POS(48)}, /* New Zealand Time */
296 {"october", MONTH, 10},
297 {"omsst", DTZ, POS(28)}, /* Omsk Summer Time */
298 {"omst", TZ, POS(24)}, /* Omsk Time */
299 {"on", IGNORE_DTF, 0}, /* "on" (throwaway) */
300 {"pdt", DTZ, NEG(28)}, /* Pacific Daylight Time */
304 {"pet", TZ, NEG(20)}, /* Peru Time */
305 {"petst", DTZ, POS(52)}, /* Petropavlovsk-Kamchatski Summer Time */
306 {"pett", TZ, POS(48)}, /* Petropavlovsk-Kamchatski Time */
307 {"pgt", TZ, POS(40)}, /* Papua New Guinea Time */
308 {"phot", TZ, POS(52)}, /* Phoenix Islands (Kiribati) Time */
312 {"pht", TZ, POS(32)}, /* Phillipine Time */
313 {"pkt", TZ, POS(20)}, /* Pakistan Time */
315 {"pmdt", DTZ, NEG(8)}, /* Pierre & Miquelon Daylight Time */
319 {"pont", TZ, POS(44)}, /* Ponape Time (Micronesia) */
320 {"pst", TZ, NEG(32)}, /* Pacific Standard Time */
321 {"pwt", TZ, POS(36)}, /* Palau Time */
322 {"pyst", DTZ, NEG(12)}, /* Paraguay Summer Time */
323 {"pyt", TZ, NEG(16)}, /* Paraguay Time */
324 {"ret", DTZ, POS(16)}, /* Reunion Island Time */
325 {"s", UNITS, DTK_SECOND}, /* "seconds" for ISO input */
326 {"sadt", DTZ, POS(42)}, /* S. Australian Dayl. Time */
331 {"sast", TZ, POS(38)}, /* South Australian Std Time */
333 {"saturday", DOW, 6},
337 {"sct", DTZ, POS(16)}, /* Mahe Island Time */
340 {"september", MONTH, 9},
341 {"set", TZ, NEG(4)}, /* Seychelles Time ?? */
345 {"sst", DTZ, POS(8)}, /* Swedish Summer Time */
348 {"swt", TZ, POS(4)}, /* Swedish Winter Time */
352 {"t", ISOTIME, DTK_TIME}, /* Filler for ISO time fields */
353 {"tft", TZ, POS(20)}, /* Kerguelen Time */
354 {"that", TZ, NEG(40)}, /* Tahiti Time */
358 {"thursday", DOW, 4},
359 {"tjt", TZ, POS(20)}, /* Tajikistan Time */
360 {"tkt", TZ, NEG(40)}, /* Tokelau Time */
361 {"tmt", TZ, POS(20)}, /* Turkmenistan Time */
362 {TODAY, RESERV, DTK_TODAY}, /* midnight */
363 {TOMORROW, RESERV, DTK_TOMORROW}, /* tomorrow midnight */
367 {"tot", TZ, POS(52)}, /* Tonga Time */
371 {"truk", TZ, POS(40)}, /* Truk Time */
375 {"tvt", TZ, POS(48)}, /* Tuvalu Time */
379 {"ulast", DTZ, POS(36)}, /* Ulan Bator Summer Time */
380 {"ulat", TZ, POS(32)}, /* Ulan Bator Time */
381 {"undefined", RESERV, DTK_INVALID}, /* pre-v6.1 invalid time */
384 {"uyst", DTZ, NEG(8)}, /* Uruguay Summer Time */
385 {"uyt", TZ, NEG(12)}, /* Uruguay Time */
386 {"uzst", DTZ, POS(24)}, /* Uzbekistan Summer Time */
387 {"uzt", TZ, POS(20)}, /* Uzbekistan Time */
388 {"vet", TZ, NEG(16)}, /* Venezuela Time */
389 {"vlast", DTZ, POS(44)}, /* Vladivostok Summer Time */
390 {"vlat", TZ, POS(40)}, /* Vladivostok Time */
394 {"vut", TZ, POS(44)}, /* Vanuata Time */
395 {"wadt", DTZ, POS(32)}, /* West Australian DST */
396 {"wakt", TZ, POS(48)}, /* Wake Time */
400 {"wast", TZ, POS(28)}, /* West Australian Std Time */
401 {"wat", TZ, NEG(4)}, /* West Africa Time */
402 {"wdt", DTZ, POS(36)}, /* West Australian DST */
404 {"wednesday", DOW, 3},
406 {"west", DTZ, POS(4)}, /* Western Europe Summer Time */
407 {"wet", TZ, POS(0)}, /* Western Europe */
408 {"wetdst", DTZ, POS(4)}, /* Western Europe Daylight Savings Time */
409 {"wft", TZ, POS(48)}, /* Wallis and Futuna Time */
410 {"wgst", DTZ, NEG(8)}, /* West Greenland Summer Time */
411 {"wgt", TZ, NEG(12)}, /* West Greenland Time */
412 {"wst", TZ, POS(32)}, /* West Australian Standard Time */
413 {"y", UNITS, DTK_YEAR}, /* "year" for ISO input */
414 {"yakst", DTZ, POS(40)}, /* Yakutsk Summer Time */
415 {"yakt", TZ, POS(36)}, /* Yakutsk Time */
416 {"yapt", TZ, POS(40)}, /* Yap Time (Micronesia) */
417 {"ydt", DTZ, NEG(32)}, /* Yukon Daylight Time */
418 {"yekst", DTZ, POS(24)}, /* Yekaterinburg Summer Time */
419 {"yekt", TZ, POS(20)}, /* Yekaterinburg Time */
420 {YESTERDAY, RESERV, DTK_YESTERDAY}, /* yesterday midnight */
421 {"yst", TZ, NEG(36)}, /* Yukon Standard Time */
422 {"z", TZ, POS(0)}, /* time zone tag per ISO-8601 */
423 {"zp4", TZ, NEG(16)}, /* UTC +4 hours. */
424 {"zp5", TZ, NEG(20)}, /* UTC +5 hours. */
425 {"zp6", TZ, NEG(24)}, /* UTC +6 hours. */
426 {ZULU, TZ, POS(0)}, /* UTC */
429 static datetkn deltatktbl[] = {
430 /* text, token, lexval */
431 {"@", IGNORE_DTF, 0}, /* postgres relative prefix */
432 {DAGO, AGO, 0}, /* "ago" indicates negative time offset */
433 {"c", UNITS, DTK_CENTURY}, /* "century" relative */
434 {"cent", UNITS, DTK_CENTURY}, /* "century" relative */
435 {"centuries", UNITS, DTK_CENTURY}, /* "centuries" relative */
436 {DCENTURY, UNITS, DTK_CENTURY}, /* "century" relative */
437 {"d", UNITS, DTK_DAY}, /* "day" relative */
438 {DDAY, UNITS, DTK_DAY}, /* "day" relative */
439 {"days", UNITS, DTK_DAY}, /* "days" relative */
440 {"dec", UNITS, DTK_DECADE}, /* "decade" relative */
441 {DDECADE, UNITS, DTK_DECADE}, /* "decade" relative */
442 {"decades", UNITS, DTK_DECADE}, /* "decades" relative */
443 {"decs", UNITS, DTK_DECADE}, /* "decades" relative */
444 {"h", UNITS, DTK_HOUR}, /* "hour" relative */
445 {DHOUR, UNITS, DTK_HOUR}, /* "hour" relative */
446 {"hours", UNITS, DTK_HOUR}, /* "hours" relative */
447 {"hr", UNITS, DTK_HOUR}, /* "hour" relative */
448 {"hrs", UNITS, DTK_HOUR}, /* "hours" relative */
449 {INVALID, RESERV, DTK_INVALID}, /* reserved for invalid time */
450 {"m", UNITS, DTK_MINUTE}, /* "minute" relative */
451 {"microsecon", UNITS, DTK_MICROSEC}, /* "microsecond" relative */
452 {"mil", UNITS, DTK_MILLENNIUM}, /* "millennium" relative */
453 {"millennia", UNITS, DTK_MILLENNIUM}, /* "millennia" relative */
454 {DMILLENNIUM, UNITS, DTK_MILLENNIUM}, /* "millennium" relative */
455 {"millisecon", UNITS, DTK_MILLISEC}, /* relative */
456 {"mils", UNITS, DTK_MILLENNIUM}, /* "millennia" relative */
457 {"min", UNITS, DTK_MINUTE}, /* "minute" relative */
458 {"mins", UNITS, DTK_MINUTE}, /* "minutes" relative */
459 {DMINUTE, UNITS, DTK_MINUTE}, /* "minute" relative */
460 {"minutes", UNITS, DTK_MINUTE}, /* "minutes" relative */
461 {"mon", UNITS, DTK_MONTH}, /* "months" relative */
462 {"mons", UNITS, DTK_MONTH}, /* "months" relative */
463 {DMONTH, UNITS, DTK_MONTH}, /* "month" relative */
464 {"months", UNITS, DTK_MONTH},
465 {"ms", UNITS, DTK_MILLISEC},
466 {"msec", UNITS, DTK_MILLISEC},
467 {DMILLISEC, UNITS, DTK_MILLISEC},
468 {"mseconds", UNITS, DTK_MILLISEC},
469 {"msecs", UNITS, DTK_MILLISEC},
470 {"qtr", UNITS, DTK_QUARTER}, /* "quarter" relative */
471 {DQUARTER, UNITS, DTK_QUARTER}, /* "quarter" relative */
472 {"reltime", IGNORE_DTF, 0}, /* pre-v6.1 "Undefined Reltime" */
473 {"s", UNITS, DTK_SECOND},
474 {"sec", UNITS, DTK_SECOND},
475 {DSECOND, UNITS, DTK_SECOND},
476 {"seconds", UNITS, DTK_SECOND},
477 {"secs", UNITS, DTK_SECOND},
478 {DTIMEZONE, UNITS, DTK_TZ}, /* "timezone" time offset */
479 {"timezone_h", UNITS, DTK_TZ_HOUR}, /* timezone hour units */
480 {"timezone_m", UNITS, DTK_TZ_MINUTE}, /* timezone minutes units */
481 {"undefined", RESERV, DTK_INVALID}, /* pre-v6.1 invalid time */
482 {"us", UNITS, DTK_MICROSEC}, /* "microsecond" relative */
483 {"usec", UNITS, DTK_MICROSEC}, /* "microsecond" relative */
484 {DMICROSEC, UNITS, DTK_MICROSEC}, /* "microsecond" relative */
485 {"useconds", UNITS, DTK_MICROSEC}, /* "microseconds" relative */
486 {"usecs", UNITS, DTK_MICROSEC}, /* "microseconds" relative */
487 {"w", UNITS, DTK_WEEK}, /* "week" relative */
488 {DWEEK, UNITS, DTK_WEEK}, /* "week" relative */
489 {"weeks", UNITS, DTK_WEEK}, /* "weeks" relative */
490 {"y", UNITS, DTK_YEAR}, /* "year" relative */
491 {DYEAR, UNITS, DTK_YEAR}, /* "year" relative */
492 {"years", UNITS, DTK_YEAR}, /* "years" relative */
493 {"yr", UNITS, DTK_YEAR}, /* "year" relative */
494 {"yrs", UNITS, DTK_YEAR}, /* "years" relative */
497 static unsigned int szdatetktbl = sizeof datetktbl / sizeof datetktbl[0];
498 static unsigned int szdeltatktbl = sizeof deltatktbl / sizeof deltatktbl[0];
500 static datetkn *datecache[MAXDATEFIELDS] = {NULL};
502 static datetkn *deltacache[MAXDATEFIELDS] = {NULL};
504 char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL};
506 char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", NULL};
508 char *pgtypes_date_weekdays_short[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL};
510 char *pgtypes_date_months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", NULL};
513 datebsearch(char *key, datetkn *base, unsigned int nel)
515 datetkn *last = base + nel - 1,
521 position = base + ((last - base) >> 1);
522 result = key[0] - position->token[0];
525 result = strncmp(key, position->token, TOKMAXLEN);
538 * Decode text string using lookup table.
539 * This routine supports time interval decoding.
542 DecodeUnits(int field, char *lowtoken, int *val)
547 if ((deltacache[field] != NULL)
548 && (strncmp(lowtoken, deltacache[field]->token, TOKMAXLEN) == 0))
549 tp = deltacache[field];
551 tp = datebsearch(lowtoken, deltatktbl, szdeltatktbl);
552 deltacache[field] = tp;
555 type = UNKNOWN_FIELD;
561 if ((type == TZ) || (type == DTZ))
568 } /* DecodeUnits() */
571 * Calendar time to Julian date conversions.
572 * Julian date is commonly used in astronomical applications,
573 * since it is numerically accurate and computationally simple.
574 * The algorithms here will accurately convert between Julian day
575 * and calendar date for all non-negative Julian days
576 * (i.e. from Nov 24, -4713 on).
578 * These routines will be used by other date/time packages
581 * Rewritten to eliminate overflow problems. This now allows the
582 * routines to work correctly for all Julian day counts from
583 * 0 to 2147483647 (Nov 24, -4713 to Jun 3, 5874898) assuming
584 * a 32-bit integer. Longer types should also work to the limits
585 * of their precision.
589 date2j(int y, int m, int d)
606 julian = y * 365 - 32167;
607 julian += y / 4 - century + century / 4;
608 julian += 7834 * m / 256 + d;
614 j2date(int jd, int *year, int *month, int *day)
623 quad = julian / 146097;
624 extra = (julian - quad * 146097) * 4 + 3;
625 julian += 60 + quad * 3 + extra / 146097;
626 quad = julian / 1461;
627 julian -= quad * 1461;
628 y = julian * 4 / 1461;
629 julian = ((y != 0) ? ((julian + 305) % 365) : ((julian + 306) % 366))
633 quad = julian * 2141 / 65536;
634 *day = julian - 7834 * quad / 256;
635 *month = (quad + 10) % 12 + 1;
652 * Decode text string using lookup table.
653 * Implement a cache lookup since it is likely that dates
654 * will be related in format.
657 DecodeSpecial(int field, char *lowtoken, int *val)
662 if ((datecache[field] != NULL)
663 && (strncmp(lowtoken, datecache[field]->token, TOKMAXLEN) == 0))
664 tp = datecache[field];
669 tp = datebsearch(lowtoken, datetktbl, szdatetktbl);
671 datecache[field] = tp;
674 type = UNKNOWN_FIELD;
695 } /* DecodeSpecial() */
698 * Encode date as local time.
701 EncodeDateOnly(struct tm * tm, int style, char *str, bool EuroDates)
703 if ((tm->tm_mon < 1) || (tm->tm_mon > 12))
709 /* compatible with ISO date formats */
711 sprintf(str, "%04d-%02d-%02d",
712 tm->tm_year, tm->tm_mon, tm->tm_mday);
714 sprintf(str, "%04d-%02d-%02d %s",
715 -(tm->tm_year - 1), tm->tm_mon, tm->tm_mday, "BC");
719 /* compatible with Oracle/Ingres date formats */
721 sprintf(str, "%02d/%02d", tm->tm_mday, tm->tm_mon);
723 sprintf(str, "%02d/%02d", tm->tm_mon, tm->tm_mday);
725 sprintf((str + 5), "/%04d", tm->tm_year);
727 sprintf((str + 5), "/%04d %s", -(tm->tm_year - 1), "BC");
730 case USE_GERMAN_DATES:
731 /* German-style date format */
732 sprintf(str, "%02d.%02d", tm->tm_mday, tm->tm_mon);
734 sprintf((str + 5), ".%04d", tm->tm_year);
736 sprintf((str + 5), ".%04d %s", -(tm->tm_year - 1), "BC");
739 case USE_POSTGRES_DATES:
741 /* traditional date-only style for Postgres */
743 sprintf(str, "%02d-%02d", tm->tm_mday, tm->tm_mon);
745 sprintf(str, "%02d-%02d", tm->tm_mon, tm->tm_mday);
747 sprintf((str + 5), "-%04d", tm->tm_year);
749 sprintf((str + 5), "-%04d %s", -(tm->tm_year - 1), "BC");
754 } /* EncodeDateOnly() */
757 TrimTrailingZeros(char *str)
759 int len = strlen(str);
761 /* chop off trailing zeros... but leave at least 2 fractional digits */
762 while ((*(str + len - 1) == '0') && (*(str + len - 3) != '.'))
770 * Encode date and time interpreted as local time.
771 * Support several date styles:
772 * Postgres - day mon hh:mm:ss yyyy tz
773 * SQL - mm/dd/yyyy hh:mm:ss.ss tz
774 * ISO - yyyy-mm-dd hh:mm:ss+/-tz
775 * German - dd.mm.yyyy hh:mm:ss tz
776 * Variants (affects order of month and day for Postgres and SQL styles):
778 * European - dd/mm/yyyy
781 EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, char *str, bool EuroDates)
790 /* Compatible with ISO-8601 date formats */
792 sprintf(str, "%04d-%02d-%02d %02d:%02d",
793 ((tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1)),
794 tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min);
797 * Print fractional seconds if any. The field widths here
798 * should be at least equal to MAX_TIMESTAMP_PRECISION.
800 * In float mode, don't print fractional seconds before 1 AD,
801 * since it's unlikely there's any precision left ...
803 #ifdef HAVE_INT64_TIMESTAMP
806 sprintf((str + strlen(str)), ":%02d.%06d", tm->tm_sec, fsec);
808 if ((fsec != 0) && (tm->tm_year > 0))
810 sprintf((str + strlen(str)), ":%09.6f", tm->tm_sec + fsec);
812 TrimTrailingZeros(str);
815 sprintf((str + strlen(str)), ":%02d", tm->tm_sec);
817 if (tm->tm_year <= 0)
818 sprintf((str + strlen(str)), " BC");
821 * tzp == NULL indicates that we don't want *any* time zone
822 * info in the output string. *tzn != NULL indicates that we
823 * have alpha time zone info available. tm_isdst != -1
824 * indicates that we have a valid time zone translation.
826 if ((tzp != NULL) && (tm->tm_isdst >= 0))
828 hour = -(*tzp / 3600);
829 min = ((abs(*tzp) / 60) % 60);
830 sprintf((str + strlen(str)), ((min != 0) ? "%+03d:%02d" : "%+03d"), hour, min);
835 /* Compatible with Oracle/Ingres date formats */
838 sprintf(str, "%02d/%02d", tm->tm_mday, tm->tm_mon);
840 sprintf(str, "%02d/%02d", tm->tm_mon, tm->tm_mday);
842 sprintf((str + 5), "/%04d %02d:%02d",
843 ((tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1)),
844 tm->tm_hour, tm->tm_min);
847 * Print fractional seconds if any. The field widths here
848 * should be at least equal to MAX_TIMESTAMP_PRECISION.
850 * In float mode, don't print fractional seconds before 1 AD,
851 * since it's unlikely there's any precision left ...
853 #ifdef HAVE_INT64_TIMESTAMP
856 sprintf((str + strlen(str)), ":%02d.%06d", tm->tm_sec, fsec);
858 if ((fsec != 0) && (tm->tm_year > 0))
860 sprintf((str + strlen(str)), ":%09.6f", tm->tm_sec + fsec);
862 TrimTrailingZeros(str);
865 sprintf((str + strlen(str)), ":%02d", tm->tm_sec);
867 if (tm->tm_year <= 0)
868 sprintf((str + strlen(str)), " BC");
870 if ((tzp != NULL) && (tm->tm_isdst >= 0))
873 sprintf((str + strlen(str)), " %.*s", MAXTZLEN, *tzn);
876 hour = -(*tzp / 3600);
877 min = ((abs(*tzp) / 60) % 60);
878 sprintf((str + strlen(str)), ((min != 0) ? "%+03d:%02d" : "%+03d"), hour, min);
883 case USE_GERMAN_DATES:
884 /* German variant on European style */
886 sprintf(str, "%02d.%02d", tm->tm_mday, tm->tm_mon);
888 sprintf((str + 5), ".%04d %02d:%02d",
889 ((tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1)),
890 tm->tm_hour, tm->tm_min);
893 * Print fractional seconds if any. The field widths here
894 * should be at least equal to MAX_TIMESTAMP_PRECISION.
896 * In float mode, don't print fractional seconds before 1 AD,
897 * since it's unlikely there's any precision left ...
899 #ifdef HAVE_INT64_TIMESTAMP
902 sprintf((str + strlen(str)), ":%02d.%06d", tm->tm_sec, fsec);
904 if ((fsec != 0) && (tm->tm_year > 0))
906 sprintf((str + strlen(str)), ":%09.6f", tm->tm_sec + fsec);
908 TrimTrailingZeros(str);
911 sprintf((str + strlen(str)), ":%02d", tm->tm_sec);
913 if (tm->tm_year <= 0)
914 sprintf((str + strlen(str)), " BC");
916 if ((tzp != NULL) && (tm->tm_isdst >= 0))
919 sprintf((str + strlen(str)), " %.*s", MAXTZLEN, *tzn);
922 hour = -(*tzp / 3600);
923 min = ((abs(*tzp) / 60) % 60);
924 sprintf((str + strlen(str)), ((min != 0) ? "%+03d:%02d" : "%+03d"), hour, min);
929 case USE_POSTGRES_DATES:
931 /* Backward-compatible with traditional Postgres abstime dates */
933 day = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
934 tm->tm_wday = j2day(day);
936 strncpy(str, days[tm->tm_wday], 3);
937 strcpy((str + 3), " ");
940 sprintf((str + 4), "%02d %3s", tm->tm_mday, months[tm->tm_mon - 1]);
942 sprintf((str + 4), "%3s %02d", months[tm->tm_mon - 1], tm->tm_mday);
944 sprintf((str + 10), " %02d:%02d", tm->tm_hour, tm->tm_min);
947 * Print fractional seconds if any. The field widths here
948 * should be at least equal to MAX_TIMESTAMP_PRECISION.
950 * In float mode, don't print fractional seconds before 1 AD,
951 * since it's unlikely there's any precision left ...
953 #ifdef HAVE_INT64_TIMESTAMP
956 sprintf((str + strlen(str)), ":%02d.%06d", tm->tm_sec, fsec);
958 if ((fsec != 0) && (tm->tm_year > 0))
960 sprintf((str + strlen(str)), ":%09.6f", tm->tm_sec + fsec);
962 TrimTrailingZeros(str);
965 sprintf((str + strlen(str)), ":%02d", tm->tm_sec);
967 sprintf((str + strlen(str)), " %04d",
968 ((tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1)));
969 if (tm->tm_year <= 0)
970 sprintf((str + strlen(str)), " BC");
972 if ((tzp != NULL) && (tm->tm_isdst >= 0))
975 sprintf((str + strlen(str)), " %.*s", MAXTZLEN, *tzn);
979 * We have a time zone, but no string version. Use the
980 * numeric form, but be sure to include a leading
981 * space to avoid formatting something which would be
982 * rejected by the date/time parser later. - thomas
985 hour = -(*tzp / 3600);
986 min = ((abs(*tzp) / 60) % 60);
987 sprintf((str + strlen(str)), ((min != 0) ? " %+03d:%02d" : " %+03d"), hour, min);
994 } /* EncodeDateTime() */
997 GetEpochTime(struct tm * tm)
1002 t0 = gmtime(&epoch);
1004 tm->tm_year = t0->tm_year;
1005 tm->tm_mon = t0->tm_mon;
1006 tm->tm_mday = t0->tm_mday;
1007 tm->tm_hour = t0->tm_hour;
1008 tm->tm_min = t0->tm_min;
1009 tm->tm_sec = t0->tm_sec;
1011 if (tm->tm_year < 1900)
1012 tm->tm_year += 1900;
1016 } /* GetEpochTime() */
1019 abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char **tzn)
1021 time_t time = (time_t) _time;
1025 tx = localtime((time_t *) &time);
1027 tx = gmtime((time_t *) &time);
1029 tm->tm_year = tx->tm_year + 1900;
1030 tm->tm_mon = tx->tm_mon + 1;
1031 tm->tm_mday = tx->tm_mday;
1032 tm->tm_hour = tx->tm_hour;
1033 tm->tm_min = tx->tm_min;
1034 tm->tm_sec = tx->tm_sec;
1035 tm->tm_isdst = tx->tm_isdst;
1037 #if defined(HAVE_TM_ZONE)
1038 tm->tm_gmtoff = tx->tm_gmtoff;
1039 tm->tm_zone = tx->tm_zone;
1044 * We have a brute force time zone per SQL99? Then use it without
1045 * change since we have already rotated to the time zone.
1047 *tzp = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
1050 * XXX FreeBSD man pages indicate that this should work - tgl
1056 * Copy no more than MAXTZLEN bytes of timezone to tzn, in
1057 * case it contains an error message, which doesn't fit in the
1060 StrNCpy(*tzn, tm->tm_zone, MAXTZLEN + 1);
1061 if (strlen(tm->tm_zone) > MAXTZLEN)
1067 #elif defined(HAVE_INT_TIMEZONE)
1070 *tzp = ((tm->tm_isdst > 0) ? (TIMEZONE_GLOBAL - 3600) : TIMEZONE_GLOBAL);
1075 * Copy no more than MAXTZLEN bytes of timezone to tzn, in
1076 * case it contains an error message, which doesn't fit in the
1079 StrNCpy(*tzn, tzname[tm->tm_isdst], MAXTZLEN + 1);
1080 if (strlen(tzname[tm->tm_isdst]) > MAXTZLEN)
1086 #else /* not (HAVE_TM_ZONE || HAVE_INT_TIMEZONE) */
1089 /* default to UTC */
1100 GetCurrentDateTime(struct tm * tm)
1104 abstime2tm(time(NULL), &tz, tm, NULL);
1107 /* DetermineLocalTimeZone()
1109 * Given a struct tm in which tm_year, tm_mon, tm_mday, tm_hour, tm_min, and
1110 * tm_sec fields are set, attempt to determine the applicable local zone
1111 * (ie, regular or daylight-savings time) at that time. Set the struct tm's
1112 * tm_isdst field accordingly, and return the actual timezone offset.
1114 * This subroutine exists to centralize uses of mktime() and defend against
1115 * mktime() bugs/restrictions on various platforms. This should be
1116 * the *only* call of mktime() in the backend.
1119 DetermineLocalTimeZone(struct tm * tm)
1123 if (IS_VALID_UTIME(tm->tm_year, tm->tm_mon, tm->tm_mday))
1125 #if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
1128 * Some buggy mktime() implementations may change the
1129 * year/month/day when given a time right at a DST boundary. To
1130 * prevent corruption of the caller's data, give mktime() a
1137 /* change to Unix conventions for year/month */
1138 tmp->tm_year -= 1900;
1141 /* indicate timezone unknown */
1144 if (mktime(tmp) != ((time_t) -1) &&
1147 /* mktime() succeeded, trust its result */
1148 tm->tm_isdst = tmp->tm_isdst;
1150 #if defined(HAVE_TM_ZONE)
1151 /* tm_gmtoff is Sun/DEC-ism */
1152 tz = -(tmp->tm_gmtoff);
1153 #elif defined(HAVE_INT_TIMEZONE)
1154 tz = ((tmp->tm_isdst > 0) ? (TIMEZONE_GLOBAL - 3600) : TIMEZONE_GLOBAL);
1155 #endif /* HAVE_INT_TIMEZONE */
1160 * We have a buggy (not to say deliberately brain damaged)
1161 * mktime(). Work around it by using localtime() instead.
1163 * First, generate the time_t value corresponding to the given
1164 * y/m/d/h/m/s taken as GMT time. This will not overflow (at
1165 * least not for time_t taken as signed) because of the range
1166 * check we did above.
1175 day = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) -
1176 date2j(1970, 1, 1));
1177 mysec = tm->tm_sec + (tm->tm_min + (day * 24 + tm->tm_hour) * 60) * 60;
1178 mytime = (time_t) mysec;
1181 * Use localtime to convert that time_t to broken-down time,
1182 * and reassemble to get a representation of local time.
1184 tmp = localtime(&mytime);
1185 day = (date2j(tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday) -
1186 date2j(1970, 1, 1));
1187 locsec = tmp->tm_sec + (tmp->tm_min + (day * 24 + tmp->tm_hour) * 60) * 60;
1190 * The local time offset corresponding to that GMT time is now
1191 * computable as mysec - locsec.
1193 delta1 = mysec - locsec;
1196 * However, if that GMT time and the local time we are
1197 * actually interested in are on opposite sides of a
1198 * daylight-savings-time transition, then this is not the time
1199 * offset we want. So, adjust the time_t to be what we think
1200 * the GMT time corresponding to our target local time is, and
1201 * repeat the localtime() call and delta calculation. We may
1202 * have to do it twice before we have a trustworthy delta.
1204 * Note: think not to put a loop here, since if we've been given
1205 * an "impossible" local time (in the gap during a
1206 * spring-forward transition) we'd never get out of the loop.
1207 * Twice is enough to give the behavior we want, which is that
1208 * "impossible" times are taken as standard time, while at a
1209 * fall-back boundary ambiguous times are also taken as
1213 mytime = (time_t) mysec;
1214 tmp = localtime(&mytime);
1215 day = (date2j(tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday) -
1216 date2j(1970, 1, 1));
1217 locsec = tmp->tm_sec + (tmp->tm_min + (day * 24 + tmp->tm_hour) * 60) * 60;
1218 delta2 = mysec - locsec;
1219 if (delta2 != delta1)
1221 mysec += (delta2 - delta1);
1222 mytime = (time_t) mysec;
1223 tmp = localtime(&mytime);
1224 day = (date2j(tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday) -
1225 date2j(1970, 1, 1));
1226 locsec = tmp->tm_sec + (tmp->tm_min + (day * 24 + tmp->tm_hour) * 60) * 60;
1227 delta2 = mysec - locsec;
1229 tm->tm_isdst = tmp->tm_isdst;
1232 #else /* not (HAVE_TM_ZONE || HAVE_INT_TIMEZONE) */
1233 /* Assume UTC if no system timezone info available */
1240 /* Given date is out of range, so assume UTC */
1249 dt2time(double jd, int *hour, int *min, int *sec, fsec_t *fsec)
1251 #ifdef HAVE_INT64_TIMESTAMP
1259 #ifdef HAVE_INT64_TIMESTAMP
1260 *hour = (time / INT64CONST(3600000000));
1261 time -= ((*hour) * INT64CONST(3600000000));
1262 *min = (time / INT64CONST(60000000));
1263 time -= ((*min) * INT64CONST(60000000));
1264 *sec = (time / INT64CONST(1000000));
1265 *fsec = (time - (*sec * INT64CONST(1000000)));
1267 *hour = (time / 3600);
1268 time -= ((*hour) * 3600);
1270 time -= ((*min) * 60);
1272 *fsec = JROUND(time - *sec);
1279 /* DecodeNumberField()
1280 * Interpret numeric string as a concatenated date or time field.
1281 * Use the context of previously decoded fields to help with
1282 * the interpretation.
1285 DecodeNumberField(int len, char *str, int fmask,
1286 int *tmask, struct tm * tm, fsec_t *fsec, int *is2digits, bool EuroDates)
1291 * Have a decimal point? Then this is a date or something with a
1294 if ((cp = strchr(str, '.')) != NULL)
1296 #ifdef HAVE_INT64_TIMESTAMP
1297 char fstr[MAXDATELEN + 1];
1300 * OK, we have at most six digits to care about. Let's construct a
1301 * string and then do the conversion to an integer.
1303 strcpy(fstr, (cp + 1));
1304 strcpy((fstr + strlen(fstr)), "000000");
1306 *fsec = strtol(fstr, NULL, 10);
1308 *fsec = strtod(cp, NULL);
1313 /* No decimal point and no complete date yet? */
1314 else if ((fmask & DTK_DATE_M) != DTK_DATE_M)
1319 *tmask = DTK_DATE_M;
1321 tm->tm_mday = atoi(str + 6);
1323 tm->tm_mon = atoi(str + 4);
1325 tm->tm_year = atoi(str + 0);
1332 *tmask = DTK_DATE_M;
1333 tm->tm_mday = atoi(str + 4);
1335 tm->tm_mon = atoi(str + 2);
1337 tm->tm_year = atoi(str + 0);
1345 *tmask = DTK_DATE_M;
1346 tm->tm_mday = atoi(str + 2);
1349 tm->tm_year = atoi(str + 0);
1356 /* not all time fields are specified? */
1357 if ((fmask & DTK_TIME_M) != DTK_TIME_M)
1362 *tmask = DTK_TIME_M;
1363 tm->tm_sec = atoi(str + 4);
1365 tm->tm_min = atoi(str + 2);
1367 tm->tm_hour = atoi(str + 0);
1374 *tmask = DTK_TIME_M;
1376 tm->tm_min = atoi(str + 2);
1378 tm->tm_hour = atoi(str + 0);
1385 } /* DecodeNumberField() */
1389 * Interpret plain numeric field as a date value in context.
1392 DecodeNumber(int flen, char *str, int fmask,
1393 int *tmask, struct tm * tm, fsec_t *fsec, int *is2digits, bool EuroDates)
1400 val = strtol(str, &cp, 10);
1407 * More than two digits? Then could be a date or a run-together
1408 * time: 2001.360 20011225 040506.789
1411 return DecodeNumberField(flen, str, (fmask | DTK_DATE_M),
1412 tmask, tm, fsec, is2digits, EuroDates);
1414 *fsec = strtod(cp, &cp);
1418 else if (*cp != '\0')
1421 /* Special case day of year? */
1422 if ((flen == 3) && (fmask & DTK_M(YEAR))
1423 && ((val >= 1) && (val <= 366)))
1425 *tmask = (DTK_M(DOY) | DTK_M(MONTH) | DTK_M(DAY));
1427 j2date((date2j(tm->tm_year, 1, 1) + tm->tm_yday - 1),
1428 &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1432 * Enough digits to be unequivocal year? Used to test for 4 digits or
1433 * more, but we now test first for a three-digit doy so anything
1434 * bigger than two digits had better be an explicit year.
1435 * - thomas 1999-01-09
1436 * Back to requiring a 4 digit year. We accept a two digit
1437 * year farther down. - thomas 2000-03-28
1441 *tmask = DTK_M(YEAR);
1443 /* already have a year? then see if we can substitute... */
1444 if ((fmask & DTK_M(YEAR)) && (!(fmask & DTK_M(DAY)))
1445 && ((tm->tm_year >= 1) && (tm->tm_year <= 31)))
1447 tm->tm_mday = tm->tm_year;
1448 *tmask = DTK_M(DAY);
1454 /* already have year? then could be month */
1455 else if ((fmask & DTK_M(YEAR)) && (!(fmask & DTK_M(MONTH)))
1456 && ((val >= 1) && (val <= 12)))
1458 *tmask = DTK_M(MONTH);
1461 /* no year and EuroDates enabled? then could be day */
1462 else if ((EuroDates || (fmask & DTK_M(MONTH)))
1463 && (!(fmask & DTK_M(YEAR)) && !(fmask & DTK_M(DAY)))
1464 && ((val >= 1) && (val <= 31)))
1466 *tmask = DTK_M(DAY);
1469 else if ((!(fmask & DTK_M(MONTH)))
1470 && ((val >= 1) && (val <= 12)))
1472 *tmask = DTK_M(MONTH);
1475 else if ((!(fmask & DTK_M(DAY)))
1476 && ((val >= 1) && (val <= 31)))
1478 *tmask = DTK_M(DAY);
1483 * Check for 2 or 4 or more digits, but currently we reach here only
1484 * if two digits. - thomas 2000-03-28
1486 else if (!(fmask & DTK_M(YEAR))
1487 && ((flen >= 4) || (flen == 2)))
1489 *tmask = DTK_M(YEAR);
1492 /* adjust ONLY if exactly two digits... */
1493 *is2digits = (flen == 2);
1499 } /* DecodeNumber() */
1502 * Decode date string which includes delimiters.
1503 * Insist on a complete set of fields.
1506 DecodeDate(char *str, int fmask, int *tmask, struct tm * tm, bool EuroDates)
1514 int is2digits = FALSE;
1518 char *field[MAXDATEFIELDS];
1520 /* parse this string... */
1521 while ((*str != '\0') && (nf < MAXDATEFIELDS))
1523 /* skip field separators */
1524 while (!isalnum((unsigned char) *str))
1528 if (isdigit((unsigned char) *str))
1530 while (isdigit((unsigned char) *str))
1533 else if (isalpha((unsigned char) *str))
1535 while (isalpha((unsigned char) *str))
1539 /* Just get rid of any non-digit, non-alpha characters... */
1546 /* don't allow too many fields */
1553 /* look first for text fields, since that will be unambiguous month */
1554 for (i = 0; i < nf; i++)
1556 if (isalpha((unsigned char) *field[i]))
1558 type = DecodeSpecial(i, field[i], &val);
1559 if (type == IGNORE_DTF)
1562 dmask = DTK_M(type);
1582 /* mark this field as being completed */
1587 /* now pick up remaining numeric fields */
1588 for (i = 0; i < nf; i++)
1590 if (field[i] == NULL)
1593 if ((len = strlen(field[i])) <= 0)
1596 if (DecodeNumber(len, field[i], fmask, &dmask, tm, &fsec, &is2digits, EuroDates) != 0)
1606 if ((fmask & ~(DTK_M(DOY) | DTK_M(TZ))) != DTK_DATE_M)
1609 /* there is no year zero in AD/BC notation; i.e. "1 BC" == year 0 */
1612 if (tm->tm_year > 0)
1613 tm->tm_year = -(tm->tm_year - 1);
1619 if (tm->tm_year < 70)
1620 tm->tm_year += 2000;
1621 else if (tm->tm_year < 100)
1622 tm->tm_year += 1900;
1626 } /* DecodeDate() */
1630 * Decode time string which includes delimiters.
1631 * Only check the lower limit on hours, since this same code
1632 * can be used to represent time spans.
1635 DecodeTime(char *str, int fmask, int *tmask, struct tm * tm, fsec_t *fsec)
1639 *tmask = DTK_TIME_M;
1641 tm->tm_hour = strtol(str, &cp, 10);
1645 tm->tm_min = strtol(str, &cp, 10);
1651 else if (*cp != ':')
1656 tm->tm_sec = strtol(str, &cp, 10);
1659 else if (*cp == '.')
1661 #ifdef HAVE_INT64_TIMESTAMP
1662 char fstr[MAXDATELEN + 1];
1665 * OK, we have at most six digits to work with. Let's
1666 * construct a string and then do the conversion to an
1669 strncpy(fstr, (cp + 1), 7);
1670 strcpy((fstr + strlen(fstr)), "000000");
1672 *fsec = strtol(fstr, &cp, 10);
1675 *fsec = strtod(str, &cp);
1684 /* do a sanity check */
1685 #ifdef HAVE_INT64_TIMESTAMP
1686 if ((tm->tm_hour < 0)
1687 || (tm->tm_min < 0) || (tm->tm_min > 59)
1688 || (tm->tm_sec < 0) || (tm->tm_sec > 59)
1689 || (*fsec >= INT64CONST(1000000)))
1692 if ((tm->tm_hour < 0)
1693 || (tm->tm_min < 0) || (tm->tm_min > 59)
1694 || (tm->tm_sec < 0) || (tm->tm_sec > 59)
1700 } /* DecodeTime() */
1703 * Interpret string as a numeric timezone.
1705 * Note: we allow timezone offsets up to 13:59. There are places that
1706 * use +1300 summer time.
1709 DecodeTimezone(char *str, int *tzp)
1717 /* assume leading character is "+" or "-" */
1718 hr = strtol((str + 1), &cp, 10);
1720 /* explicit delimiter? */
1722 min = strtol((cp + 1), &cp, 10);
1723 /* otherwise, might have run things together... */
1724 else if ((*cp == '\0') && ((len = strlen(str)) > 3))
1726 min = strtol((str + len - 2), &cp, 10);
1727 if ((min < 0) || (min >= 60))
1730 *(str + len - 2) = '\0';
1731 hr = strtol((str + 1), &cp, 10);
1732 if ((hr < 0) || (hr > 13))
1738 tz = (hr * 60 + min) * 60;
1744 } /* DecodeTimezone() */
1747 /* DecodePosixTimezone()
1748 * Interpret string as a POSIX-compatible timezone:
1751 * - thomas 2000-03-15
1754 DecodePosixTimezone(char *str, int *tzp)
1763 while ((*cp != '\0') && isalpha((unsigned char) *cp))
1766 if (DecodeTimezone(cp, &tz) != 0)
1771 type = DecodeSpecial(MAXDATEFIELDS - 1, str, &val);
1778 *tzp = (val * 60) - tz;
1786 } /* DecodePosixTimezone() */
1789 * Break string into tokens based on a date/time context.
1790 * Several field types are assigned:
1791 * DTK_NUMBER - digits and (possibly) a decimal point
1792 * DTK_DATE - digits and two delimiters, or digits and text
1793 * DTK_TIME - digits, colon delimiters, and possibly a decimal point
1794 * DTK_STRING - text (no digits)
1795 * DTK_SPECIAL - leading "+" or "-" followed by text
1796 * DTK_TZ - leading "+" or "-" followed by digits
1797 * Note that some field types can hold unexpected items:
1798 * DTK_NUMBER can hold date fields (yy.ddd)
1799 * DTK_STRING can hold months (January) and time zones (PST)
1800 * DTK_DATE can hold Posix time zones (GMT-8)
1803 ParseDateTime(char *timestr, char *lowstr,
1804 char **field, int *ftype, int maxfields, int *numfields, char **endstr)
1810 /* outer loop through fields */
1811 while (*(*endstr) != '\0')
1815 /* leading digit? then date or time */
1816 if (isdigit((unsigned char) *(*endstr)))
1818 *lp++ = *(*endstr)++;
1819 while (isdigit((unsigned char) *(*endstr)))
1820 *lp++ = *(*endstr)++;
1823 if (*(*endstr) == ':')
1825 ftype[nf] = DTK_TIME;
1826 *lp++ = *(*endstr)++;
1827 while (isdigit((unsigned char) *(*endstr)) ||
1828 (*(*endstr) == ':') || (*(*endstr) == '.'))
1829 *lp++ = *(*endstr)++;
1831 /* date field? allow embedded text month */
1832 else if ((*(*endstr) == '-') || (*(*endstr) == '/') || (*(*endstr) == '.'))
1834 /* save delimiting character to use later */
1835 char *dp = (*endstr);
1837 *lp++ = *(*endstr)++;
1838 /* second field is all digits? then no embedded text month */
1839 if (isdigit((unsigned char) *(*endstr)))
1841 ftype[nf] = ((*dp == '.') ? DTK_NUMBER : DTK_DATE);
1842 while (isdigit((unsigned char) *(*endstr)))
1843 *lp++ = *(*endstr)++;
1846 * insist that the delimiters match to get a
1849 if (*(*endstr) == *dp)
1851 ftype[nf] = DTK_DATE;
1852 *lp++ = *(*endstr)++;
1853 while (isdigit((unsigned char) *(*endstr)) || (*(*endstr) == *dp))
1854 *lp++ = *(*endstr)++;
1859 ftype[nf] = DTK_DATE;
1860 while (isalnum((unsigned char) *(*endstr)) || (*(*endstr) == *dp))
1861 *lp++ = tolower((unsigned char) *(*endstr)++);
1866 * otherwise, number only and will determine year, month, day,
1867 * or concatenated fields later...
1870 ftype[nf] = DTK_NUMBER;
1872 /* Leading decimal point? Then fractional seconds... */
1873 else if (*(*endstr) == '.')
1875 *lp++ = *(*endstr)++;
1876 while (isdigit((unsigned char) *(*endstr)))
1877 *lp++ = *(*endstr)++;
1879 ftype[nf] = DTK_NUMBER;
1883 * text? then date string, month, day of week, special, or
1886 else if (isalpha((unsigned char) *(*endstr)))
1888 ftype[nf] = DTK_STRING;
1889 *lp++ = tolower((unsigned char) *(*endstr)++);
1890 while (isalpha((unsigned char) *(*endstr)))
1891 *lp++ = tolower((unsigned char) *(*endstr)++);
1894 * Full date string with leading text month? Could also be a
1895 * POSIX time zone...
1897 if ((*(*endstr) == '-') || (*(*endstr) == '/') || (*(*endstr) == '.'))
1899 char *dp = (*endstr);
1901 ftype[nf] = DTK_DATE;
1902 *lp++ = *(*endstr)++;
1903 while (isdigit((unsigned char) *(*endstr)) || (*(*endstr) == *dp))
1904 *lp++ = *(*endstr)++;
1907 /* skip leading spaces */
1908 else if (isspace((unsigned char) *(*endstr)))
1913 /* sign? then special or numeric timezone */
1914 else if ((*(*endstr) == '+') || (*(*endstr) == '-'))
1916 *lp++ = *(*endstr)++;
1917 /* soak up leading whitespace */
1918 while (isspace((unsigned char) *(*endstr)))
1920 /* numeric timezone? */
1921 if (isdigit((unsigned char) *(*endstr)))
1924 *lp++ = *(*endstr)++;
1925 while (isdigit((unsigned char) *(*endstr)) ||
1926 (*(*endstr) == ':') || (*(*endstr) == '.'))
1927 *lp++ = *(*endstr)++;
1930 else if (isalpha((unsigned char) *(*endstr)))
1932 ftype[nf] = DTK_SPECIAL;
1933 *lp++ = tolower((unsigned char) *(*endstr)++);
1934 while (isalpha((unsigned char) *(*endstr)))
1935 *lp++ = tolower((unsigned char) *(*endstr)++);
1937 /* otherwise something wrong... */
1941 /* ignore punctuation but use as delimiter */
1942 else if (ispunct((unsigned char) *(*endstr)))
1948 /* otherwise, something is not right... */
1952 /* force in a delimiter after each field */
1955 if (nf > MAXDATEFIELDS)
1962 } /* ParseDateTime() */
1966 * Interpret previously parsed fields for general date and time.
1967 * Return 0 if full date, 1 if only time, and -1 if problems.
1968 * External format(s):
1969 * "<weekday> <month>-<day>-<year> <hour>:<minute>:<second>"
1970 * "Fri Feb-7-1997 15:23:27"
1971 * "Feb-7-1997 15:23:27"
1972 * "2-7-1997 15:23:27"
1973 * "1997-2-7 15:23:27"
1974 * "1997.038 15:23:27" (day of year 1-366)
1975 * Also supports input in compact time:
1978 * "20011225T040506.789-07"
1980 * Use the system-provided functions to get the current time zone
1981 * if not specified in the input string.
1982 * If the date is outside the time_t system-supported time range,
1983 * then assume UTC time zone. - thomas 1997-05-27
1986 DecodeDateTime(char **field, int *ftype, int nf,
1987 int *dtype, struct tm * tm, fsec_t *fsec, int *tzp, bool EuroDates)
1992 int ptype = 0; /* "prefix type" for ISO y2001m02d04
1997 int haveTextMonth = FALSE;
1998 int is2digits = FALSE;
2002 * We'll insist on at least all of the date fields, but initialize the
2003 * remaining fields in case they are not set later...
2010 /* don't know daylight savings time status apriori */
2015 for (i = 0; i < nf; i++)
2021 * Integral julian day with attached time zone?
2022 * All other forms with JD will be separated into
2023 * distinct fields, so we handle just this case here.
2025 if (ptype == DTK_JULIAN)
2033 val = strtol(field[i], &cp, 10);
2037 j2date(val, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2038 /* Get the time zone from the end of the string */
2039 if (DecodeTimezone(cp, tzp) != 0)
2042 tmask = DTK_DATE_M | DTK_TIME_M | DTK_M(TZ);
2047 * Already have a date? Then this might be a POSIX time
2048 * zone with an embedded dash (e.g. "PST-3" == "EST") or
2049 * a run-together time with trailing time zone (e.g. hhmmss-zz).
2050 * - thomas 2001-12-25
2052 else if (((fmask & DTK_DATE_M) == DTK_DATE_M)
2055 /* No time zone accepted? Then quit... */
2059 if (isdigit((unsigned char) *field[i]) || ptype != 0)
2065 /* Sanity check; should not fail this test */
2066 if (ptype != DTK_TIME)
2072 * Starts with a digit but we already have a time
2073 * field? Then we are in trouble with a date and
2076 if ((fmask & DTK_TIME_M) == DTK_TIME_M)
2079 if ((cp = strchr(field[i], '-')) == NULL)
2082 /* Get the time zone from the end of the string */
2083 if (DecodeTimezone(cp, tzp) != 0)
2088 * Then read the rest of the field as a
2091 if ((ftype[i] = DecodeNumberField(strlen(field[i]), field[i], fmask,
2092 &tmask, tm, fsec, &is2digits, EuroDates)) < 0)
2096 * modify tmask after returning from
2097 * DecodeNumberField()
2103 if (DecodePosixTimezone(field[i], tzp) != 0)
2110 else if (DecodeDate(field[i], fmask, &tmask, tm, EuroDates) != 0)
2115 if (DecodeTime(field[i], fmask, &tmask, tm, fsec) != 0)
2119 * Check upper limit on hours; other limits checked in
2122 if (tm->tm_hour > 23)
2133 if (DecodeTimezone(field[i], &tz) != 0)
2137 * Already have a time zone? Then maybe this is the
2138 * second field of a POSIX time: EST+3 (equivalent to
2141 if ((i > 0) && ((fmask & DTK_M(TZ)) != 0)
2142 && (ftype[i - 1] == DTK_TZ)
2143 && (isalpha((unsigned char) *field[i - 1])))
2159 * Was this an "ISO date" with embedded field labels? An
2160 * example is "y2001m02d04" - thomas 2001-02-04
2167 val = strtol(field[i], &cp, 10);
2170 * only a few kinds are allowed to have an embedded
2184 else if (*cp != '\0')
2191 tmask = DTK_M(YEAR);
2197 * already have a month and hour? then assume
2200 if (((fmask & DTK_M(MONTH)) != 0)
2201 && ((fmask & DTK_M(HOUR)) != 0))
2204 tmask = DTK_M(MINUTE);
2209 tmask = DTK_M(MONTH);
2220 tmask = DTK_M(HOUR);
2225 tmask = DTK_M(MINUTE);
2230 tmask = DTK_M(SECOND);
2235 frac = strtod(cp, &cp);
2238 #ifdef HAVE_INT64_TIMESTAMP
2239 *fsec = frac * 1000000;
2248 if (DecodeTimezone(field[i], tzp) != 0)
2254 * previous field was a label for "julian date"?
2257 j2date(val, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2258 /* fractional Julian Day? */
2263 time = strtod(cp, &cp);
2267 tmask |= DTK_TIME_M;
2268 #ifdef HAVE_INT64_TIMESTAMP
2269 dt2time((time * 86400000000), &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
2271 dt2time((time * 86400), &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
2277 /* previous field was "t" for ISO time */
2278 if ((ftype[i] = DecodeNumberField(strlen(field[i]), field[i], (fmask | DTK_DATE_M),
2279 &tmask, tm, fsec, &is2digits, EuroDates)) < 0)
2282 if (tmask != DTK_TIME_M)
2299 flen = strlen(field[i]);
2300 cp = strchr(field[i], '.');
2302 /* Embedded decimal and no date yet? */
2303 if ((cp != NULL) && !(fmask & DTK_DATE_M))
2305 if (DecodeDate(field[i], fmask, &tmask, tm, EuroDates) != 0)
2308 /* embedded decimal and several digits before? */
2309 else if ((cp != NULL) && ((flen - strlen(cp)) > 2))
2312 * Interpret as a concatenated date or time Set
2313 * the type field to allow decoding other fields
2314 * later. Example: 20011223 or 040506
2316 if ((ftype[i] = DecodeNumberField(flen, field[i], fmask,
2317 &tmask, tm, fsec, &is2digits, EuroDates)) < 0)
2322 if ((ftype[i] = DecodeNumberField(flen, field[i], fmask,
2323 &tmask, tm, fsec, &is2digits, EuroDates)) < 0)
2326 /* otherwise it is a single date/time field... */
2327 else if (DecodeNumber(flen, field[i], fmask,
2328 &tmask, tm, fsec, &is2digits, EuroDates) != 0)
2335 type = DecodeSpecial(i, field[i], &val);
2336 if (type == IGNORE_DTF)
2339 tmask = DTK_M(type);
2346 tmask = (DTK_DATE_M | DTK_TIME_M | DTK_M(TZ));
2348 GetCurrentDateTime(tm);
2354 GetCurrentDateTime(tm);
2355 j2date((date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - 1),
2356 &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2365 GetCurrentDateTime(tm);
2374 GetCurrentDateTime(tm);
2375 j2date((date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) + 1),
2376 &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2383 tmask = (DTK_TIME_M | DTK_M(TZ));
2401 * already have a (numeric) month? then see if we
2404 if ((fmask & DTK_M(MONTH)) && (!haveTextMonth)
2405 && (!(fmask & DTK_M(DAY)))
2406 && ((tm->tm_mon >= 1) && (tm->tm_mon <= 31)))
2408 tm->tm_mday = tm->tm_mon;
2411 haveTextMonth = TRUE;
2418 * daylight savings time modifier (solves "MET
2421 tmask |= DTK_M(DTZ);
2431 * set mask for TZ here _or_ check for DTZ later
2432 * when getting default timezone
2473 * This is a filler field "t" indicating that the
2474 * next field is time. Try to verify that this is
2479 /* No preceeding date? Then quit... */
2480 if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2484 * We will need one of the following fields:
2485 * DTK_NUMBER should be hhmmss.fff
2486 * DTK_TIME should be hh:mm:ss.fff
2487 * DTK_DATE should be hhmmss-zz
2490 || ((ftype[i + 1] != DTK_NUMBER)
2491 && (ftype[i + 1] != DTK_TIME)
2492 && (ftype[i + 1] != DTK_DATE)))
2512 /* there is no year zero in AD/BC notation; i.e. "1 BC" == year 0 */
2515 if (tm->tm_year > 0)
2516 tm->tm_year = -(tm->tm_year - 1);
2522 if (tm->tm_year < 70)
2523 tm->tm_year += 2000;
2524 else if (tm->tm_year < 100)
2525 tm->tm_year += 1900;
2528 if ((mer != HR24) && (tm->tm_hour > 12))
2530 if ((mer == AM) && (tm->tm_hour == 12))
2532 else if ((mer == PM) && (tm->tm_hour != 12))
2535 /* do additional checking for full date specs... */
2536 if (*dtype == DTK_DATE)
2538 if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2539 return ((fmask & DTK_TIME_M) == DTK_TIME_M) ? 1 : -1;
2542 * check for valid day of month, now that we know for sure the
2545 if ((tm->tm_mday < 1)
2546 || (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]))
2549 /* timezone not specified? then find local timezone if possible */
2550 if (((fmask & DTK_DATE_M) == DTK_DATE_M)
2551 && (tzp != NULL) && (!(fmask & DTK_M(TZ))))
2554 * daylight savings time modifier but no standard timezone?
2557 if (fmask & DTK_M(DTZMOD))
2560 *tzp = DetermineLocalTimeZone(tm);
2565 } /* DecodeDateTime() */
2567 /* Function works as follows:
2573 find_end_token(char *str, char *fmt)
2576 * str: here is28the day12the hour fmt: here is%dthe day%hthe hour
2578 * we extract the 28, we read the percent sign and the type "d" then this
2579 * functions gets called as find_end_token("28the day12the hour", "the
2582 * fmt points to "the day%hthehour", next_percent points to %hthehour and
2583 * we have to find a match for everything between these positions
2584 * ("the day"). We look for "the day" in str and know that the pattern
2585 * we are about to scan ends where this string starts (right after the
2588 * At the end, *fmt is '\0' and *str isn't. end_position then is
2591 char *end_position = NULL;
2593 *subst_location = NULL;
2594 int scan_offset = 0;
2597 /* are we at the end? */
2601 return end_position;
2604 /* not at the end */
2605 while (fmt[scan_offset] == '%' && fmt[scan_offset + 1])
2608 * there is no delimiter, skip to the next delimiter if we're
2609 * reading a number and then something that is not a number
2610 * "9:15pm", we might be able to recover with the strtol end
2611 * pointer. Go for the next percent sign
2615 next_percent = strchr(fmt + scan_offset, '%');
2619 * we don't want to allocate extra memory, so we temporarily set
2620 * the '%' sign to '\0' and call strstr However since we allow
2621 * whitespace to float around everything, we have to shorten the
2622 * pattern until we reach a non-whitespace character
2625 subst_location = next_percent;
2626 while (*(subst_location - 1) == ' ' && subst_location - 1 > fmt + scan_offset)
2628 last_char = *subst_location;
2629 *subst_location = '\0';
2632 * the haystack is the str and the needle is the original fmt but
2633 * it ends at the position where the next percent sign would be
2637 * There is one special case. Imagine: str = " 2", fmt = "%d
2638 * %...", since we want to allow blanks as "dynamic" padding we
2639 * have to accept this. Now, we are called with a fmt of " %..."
2640 * and look for " " in str. We find it at the first position and
2641 * never read the 2...
2645 end_position = strstr(str, fmt + scan_offset);
2646 *subst_location = last_char;
2651 * there is no other percent sign. So everything up to the end has
2654 end_position = str + strlen(str);
2659 * maybe we have the following case:
2661 * str = "4:15am" fmt = "%M:%S %p"
2663 * at this place we could have
2665 * str = "15am" fmt = " %p"
2667 * and have set fmt to " " because overwrote the % sign with a NULL
2669 * In this case where we would have to match a space but can't find
2670 * it, set end_position to the end of the string
2672 if ((fmt + scan_offset)[0] == ' ' && fmt + scan_offset + 1 == subst_location)
2673 end_position = str + strlen(str);
2675 return end_position;
2679 pgtypes_defmt_scan(union un_fmt_comb * scan_val, int scan_type, char **pstr, char *pfmt)
2682 * scan everything between pstr and pstr_end. This is not including
2683 * the last character so we might set it to '\0' for the parsing
2689 char *strtol_end = NULL;
2691 while (**pstr == ' ')
2693 pstr_end = find_end_token(*pstr, pfmt);
2696 /* there was an error, no match */
2700 last_char = *pstr_end;
2705 case PGTYPES_TYPE_UINT:
2708 * numbers may be blank-padded, this is the only deviation
2709 * from the fmt-string we accept
2711 while (**pstr == ' ')
2714 scan_val->uint_val = (unsigned int) strtol(*pstr, &strtol_end, 10);
2718 case PGTYPES_TYPE_UINT_LONG:
2719 while (**pstr == ' ')
2722 scan_val->uint_val = (unsigned long int) strtol(*pstr, &strtol_end, 10);
2726 case PGTYPES_TYPE_STRING_MALLOCED:
2728 scan_val->str_val = pgtypes_strdup(*pstr);
2730 if (strtol_end && *strtol_end)
2734 *pstr_end = last_char;
2738 /* XXX range checking */
2739 int PGTYPEStimestamp_defmt_scan(char **, char *, timestamp *, int *, int *, int *,
2740 int *, int *, int *, int *);
2743 PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp *d,
2744 int *year, int *month, int *day,
2745 int *hour, int *minute, int *second,
2748 union un_fmt_comb scan_val;
2764 while (*pfmt == ' ')
2766 while (*pstr == ' ')
2777 /* XXX Error: no match */
2783 /* here *pfmt equals '%' */
2791 * we parse the day and see if it is a week day but we do
2792 * not check if the week day really matches the date
2796 while (pgtypes_date_weekdays_short[j])
2798 if (strncmp(pgtypes_date_weekdays_short[j], pstr,
2799 strlen(pgtypes_date_weekdays_short[j])) == 0)
2803 pstr += strlen(pgtypes_date_weekdays_short[j]);
2810 /* see note above */
2816 if (strncmp(days[j], pstr, strlen(days[j])) == 0)
2820 pstr += strlen(days[j]);
2833 if (strncmp(months[j], pstr, strlen(months[j])) == 0)
2837 pstr += strlen(months[j]);
2845 /* see note above */
2849 while (pgtypes_date_months[j])
2851 if (strncmp(pgtypes_date_months[j], pstr, strlen(pgtypes_date_months[j])) == 0)
2855 pstr += strlen(pgtypes_date_months[j]);
2867 scan_type = PGTYPES_TYPE_UINT;
2868 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
2869 *year = scan_val.uint_val * 100;
2874 scan_type = PGTYPES_TYPE_UINT;
2875 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
2876 *day = scan_val.uint_val;
2881 * we have to concatenate the strings in order to be able
2882 * to find the end of the substitution
2885 tmp = pgtypes_alloc(strlen("%m/%d/%y") + strlen(pstr) + 1);
2886 strcpy(tmp, "%m/%d/%y");
2888 err = PGTYPEStimestamp_defmt_scan(&pstr, tmp, d, year, month, day, hour, minute, second, tz);
2893 scan_type = PGTYPES_TYPE_UINT;
2894 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
2895 *month = scan_val.uint_val;
2898 case 'g': /* XXX difference to y (ISO) */
2900 scan_type = PGTYPES_TYPE_UINT;
2901 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
2905 *year = scan_val.uint_val;
2908 *year += scan_val.uint_val;
2913 /* XXX difference to %V (ISO) */
2915 scan_type = PGTYPES_TYPE_UINT;
2916 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
2917 *year = scan_val.uint_val;
2924 scan_type = PGTYPES_TYPE_UINT;
2925 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
2926 *hour += scan_val.uint_val;
2930 scan_type = PGTYPES_TYPE_UINT;
2931 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
2934 * XXX what should we do with that? We could say that it's
2935 * sufficient if we have the year and the day within the
2936 * year to get at least a specific day.
2941 scan_type = PGTYPES_TYPE_UINT;
2942 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
2943 *minute = scan_val.uint_val;
2955 if (strncmp(pstr, "am", 2) == 0)
2961 if (strncmp(pstr, "a.m.", 4) == 0)
2967 if (strncmp(pstr, "pm", 2) == 0)
2973 if (strncmp(pstr, "p.m.", 4) == 0)
2983 if (strncmp(pstr, "AM", 2) == 0)
2989 if (strncmp(pstr, "A.M.", 4) == 0)
2995 if (strncmp(pstr, "PM", 2) == 0)
3001 if (strncmp(pstr, "P.M.", 4) == 0)
3010 tmp = pgtypes_alloc(strlen("%I:%M:%S %p") + strlen(pstr) + 1);
3011 strcpy(tmp, "%I:%M:%S %p");
3013 err = PGTYPEStimestamp_defmt_scan(&pstr, tmp, d, year, month, day, hour, minute, second, tz);
3018 tmp = pgtypes_alloc(strlen("%H:%M") + strlen(pstr) + 1);
3019 strcpy(tmp, "%H:%M");
3021 err = PGTYPEStimestamp_defmt_scan(&pstr, tmp, d, year, month, day, hour, minute, second, tz);
3026 scan_type = PGTYPES_TYPE_UINT_LONG;
3027 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
3028 /* number of seconds in scan_val.luint_val */
3031 time_t et = (time_t) scan_val.luint_val;
3034 *year = tms->tm_year;
3035 *month = tms->tm_mon;
3036 *day = tms->tm_mday;
3037 *hour = tms->tm_hour;
3038 *minute = tms->tm_min;
3039 *second = tms->tm_sec;
3044 scan_type = PGTYPES_TYPE_UINT;
3045 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
3046 *second = scan_val.uint_val;
3057 tmp = pgtypes_alloc(strlen("%H:%M:%S") + strlen(pstr) + 1);
3058 strcpy(tmp, "%H:%M:%S");
3060 err = PGTYPEStimestamp_defmt_scan(&pstr, tmp, d, year, month, day, hour, minute, second, tz);
3065 scan_type = PGTYPES_TYPE_UINT;
3066 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
3067 if (scan_val.uint_val < 1 || scan_val.uint_val > 7)
3072 scan_type = PGTYPES_TYPE_UINT;
3073 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
3074 if (scan_val.uint_val < 0 || scan_val.uint_val > 53)
3079 scan_type = PGTYPES_TYPE_UINT;
3080 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
3081 if (scan_val.uint_val < 1 || scan_val.uint_val > 53)
3086 scan_type = PGTYPES_TYPE_UINT;
3087 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
3088 if (scan_val.uint_val < 0 || scan_val.uint_val > 6)
3093 scan_type = PGTYPES_TYPE_UINT;
3094 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
3095 if (scan_val.uint_val < 0 || scan_val.uint_val > 53)
3104 scan_type = PGTYPES_TYPE_UINT;
3105 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
3106 *year = scan_val.uint_val;
3110 scan_type = PGTYPES_TYPE_STRING_MALLOCED;
3111 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
3114 err = DecodeTimezone(scan_val.str_val, tz);
3115 free(scan_val.str_val);
3120 scan_type = PGTYPES_TYPE_STRING_MALLOCED;
3121 err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt);
3124 * XXX use DecodeSpecial instead ? - it's declared static
3125 * but the arrays as well. :-(
3127 for (j = 0; !err && j < szdatetktbl; j++)
3129 if (strcasecmp(datetktbl[j].token, scan_val.str_val) == 0)
3132 * tz calculates the offset for the seconds, the
3133 * timezone value of the datetktbl table is in
3136 *tz = -15 * 60 * datetktbl[j].value;
3140 free(scan_val.str_val);
3200 if (*day > day_tab[isleap(*year)][*month - 1])
3202 *day = day_tab[isleap(*year)][*month - 1];
3206 tm.tm_sec = *second;
3207 tm.tm_min = *minute;
3213 tm2timestamp(&tm, 0, tz, d);
3218 /* XXX: 1900 is compiled in as the base for years */