]>
Commit | Line | Data |
---|---|---|
7088bd88 WP |
1 | %{ |
2 | /** | |
3 | * Parse a string into an internal timestamp. | |
4 | * | |
5 | * This file is based on gnulib parse-datetime.y-dd7a871 with | |
6 | * the other gnulib dependencies removed for use in util-linux. | |
7 | * | |
8 | * Copyright (C) 1999-2000, 2002-2017 Free Software Foundation, Inc. | |
9 | * | |
10 | * This program is free software: you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 3 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 | * | |
23 | * Originally written by Steven M. Bellovin <smb@research.att.com> while | |
24 | * at the University of North Carolina at Chapel Hill. Later tweaked by | |
25 | * a couple of people on Usenet. Completely overhauled by Rich $alz | |
26 | * <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990. | |
27 | * | |
28 | * Modified by Paul Eggert <eggert@twinsun.com> in August 1999 to do | |
29 | * the right thing about local DST. Also modified by Paul Eggert | |
30 | * <eggert@cs.ucla.edu> in February 2004 to support | |
31 | * nanosecond-resolution timestamps, and in October 2004 to support | |
32 | * TZ strings in dates. | |
33 | */ | |
34 | ||
35 | /** | |
36 | * FIXME: Check for arithmetic overflow in all cases, not just | |
37 | * some of them. | |
38 | */ | |
39 | ||
40 | #include <sys/time.h> | |
41 | #include <time.h> | |
42 | ||
43 | #include "c.h" | |
44 | #include "timeutils.h" | |
45 | ||
46 | /** | |
47 | * There's no need to extend the stack, so there's no need to involve | |
48 | * alloca. | |
49 | */ | |
50 | #define YYSTACK_USE_ALLOCA 0 | |
51 | ||
52 | /** | |
53 | * Tell Bison how much stack space is needed. 20 should be plenty for | |
54 | * this grammar, which is not right recursive. Beware setting it too | |
55 | * high, since that might cause problems on machines whose | |
56 | * implementations have lame stack-overflow checking. | |
57 | */ | |
58 | #define YYMAXDEPTH 20 | |
59 | #define YYINITDEPTH YYMAXDEPTH | |
60 | ||
61 | /** | |
62 | * Since the code of parse-datetime.y is not included in the Emacs executable | |
63 | * itself, there is no need to #define static in this file. Even if | |
64 | * the code were included in the Emacs executable, it probably | |
65 | * wouldn't do any harm to #undef it here; this will only cause | |
66 | * problems if we try to write to a static variable, which I don't | |
67 | * think this code needs to do. | |
68 | */ | |
69 | #ifdef emacs | |
70 | # undef static | |
71 | #endif | |
72 | ||
73 | #include <inttypes.h> | |
74 | #include <limits.h> | |
75 | #include <stdio.h> | |
76 | #include <stdlib.h> | |
77 | #include <string.h> | |
78 | ||
79 | ||
80 | #include <stdarg.h> | |
81 | #include "cctype.h" | |
82 | #include "nls.h" | |
83 | #include "xalloc.h" | |
84 | ||
85 | /** | |
86 | * Bison's skeleton tests _STDLIB_H, while some stdlib.h headers | |
87 | * use _STDLIB_H_ as witness. Map the latter to the one bison uses. | |
88 | * FIXME: this is temporary. Remove when we have a mechanism to ensure | |
89 | * that the version we're using is fixed, too. | |
90 | */ | |
91 | #ifdef _STDLIB_H_ | |
92 | # undef _STDLIB_H | |
93 | # define _STDLIB_H 1 | |
94 | #endif | |
95 | ||
96 | /** | |
97 | * ISDIGIT differs from isdigit, as follows: | |
98 | * - Its arg may be any int or unsigned int; it need not be an unsigned char | |
99 | * or EOF. | |
100 | * - It's typically faster. | |
101 | * POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to | |
102 | * isdigit unless it's important to use the locale's definition | |
103 | * of "digit" even when the host does not conform to POSIX. | |
104 | */ | |
105 | #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9) | |
106 | ||
107 | /** | |
108 | * Shift A right by B bits portably, by dividing A by 2**B and | |
109 | * truncating towards minus infinity. A and B should be free of side | |
110 | * effects, and B should be in the range 0 <= B <= INT_BITS - 2, where | |
111 | * INT_BITS is the number of useful bits in an int. GNU code can | |
112 | * assume that INT_BITS is at least 32. | |
113 | * | |
114 | * ISO C99 says that A >> B is implementation-defined if A < 0. Some | |
115 | * implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift | |
116 | * right in the usual way when A < 0, so SHR falls back on division if | |
117 | * ordinary A >> B doesn't seem to be the usual signed shift. | |
118 | */ | |
119 | #define SHR(a, b) \ | |
120 | (-1 >> 1 == -1 \ | |
121 | ? (a) >> (b) \ | |
122 | : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0)) | |
123 | ||
124 | #define EPOCH_YEAR 1970 | |
125 | #define TM_YEAR_BASE 1900 | |
126 | ||
127 | #define HOUR(x) ((x) * 60) | |
128 | ||
129 | #define STREQ(a, b) (strcmp (a, b) == 0) | |
130 | ||
131 | /** | |
132 | * Convert a possibly-signed character to an unsigned character. This is | |
133 | * a bit safer than casting to unsigned char, since it catches some type | |
134 | * errors that the cast doesn't. | |
135 | */ | |
136 | static unsigned char to_uchar (char ch) { return ch; } | |
137 | ||
138 | /** | |
139 | * FIXME: It also assumes that signed integer overflow silently wraps around, | |
140 | * but this is not true any more with recent versions of GCC 4. | |
141 | */ | |
142 | ||
143 | /** | |
144 | * An integer value, and the number of digits in its textual | |
145 | * representation. | |
146 | */ | |
147 | typedef struct { | |
148 | int negative; | |
149 | long int value; | |
150 | size_t digits; | |
151 | } textint; | |
152 | ||
153 | /* An entry in the lexical lookup table. */ | |
154 | typedef struct { | |
155 | char const *name; | |
156 | int type; | |
157 | int value; | |
158 | } table; | |
159 | ||
160 | /* Meridian: am, pm, or 24-hour style. */ | |
161 | enum { MERam, MERpm, MER24 }; | |
162 | ||
163 | enum { BILLION = 1000000000, LOG10_BILLION = 9 }; | |
164 | ||
165 | /* Relative year, month, day, hour, minutes, seconds, and nanoseconds. */ | |
166 | typedef struct { | |
167 | long int year; | |
168 | long int month; | |
169 | long int day; | |
170 | long int hour; | |
171 | long int minutes; | |
172 | time_t seconds; | |
173 | long int ns; | |
174 | } relative_time; | |
175 | ||
176 | #if HAVE_COMPOUND_LITERALS | |
177 | # define RELATIVE_TIME_0 ((relative_time) { 0, 0, 0, 0, 0, 0, 0 }) | |
178 | #else | |
179 | static relative_time const RELATIVE_TIME_0; | |
180 | #endif | |
181 | ||
182 | /* Information passed to and from the parser. */ | |
183 | typedef struct { | |
184 | /* The input string remaining to be parsed. */ | |
185 | const char *input; | |
186 | ||
187 | /* N, if this is the Nth Tuesday. */ | |
188 | long int day_ordinal; | |
189 | ||
190 | /* Day of week; Sunday is 0. */ | |
191 | int day_number; | |
192 | ||
193 | /* tm_isdst flag for the local zone. */ | |
194 | int local_isdst; | |
195 | ||
196 | /* Time zone, in minutes east of UTC. */ | |
197 | long int time_zone; | |
198 | ||
199 | /* Style used for time. */ | |
200 | int meridian; | |
201 | ||
202 | /* Gregorian year, month, day, hour, minutes, seconds, and ns. */ | |
203 | textint year; | |
204 | long int month; | |
205 | long int day; | |
206 | long int hour; | |
207 | long int minutes; | |
208 | struct timespec seconds; /* includes nanoseconds */ | |
209 | ||
210 | /* Relative year, month, day, hour, minutes, seconds, and ns. */ | |
211 | relative_time rel; | |
212 | ||
213 | /* Presence or counts of some nonterminals parsed so far. */ | |
214 | int timespec_seen; | |
215 | int rels_seen; | |
216 | size_t dates_seen; | |
217 | size_t days_seen; | |
218 | size_t local_zones_seen; | |
219 | size_t dsts_seen; | |
220 | size_t times_seen; | |
221 | size_t zones_seen; | |
222 | size_t year_seen; | |
223 | ||
224 | /* 1 if the user specified explicit ordinal day value, */ | |
225 | int ordinal_day_seen; | |
226 | ||
227 | /* Table of local time zone abbreviations, null terminated. */ | |
228 | table local_time_zone_table[3]; | |
229 | } parser_control; | |
230 | ||
231 | union YYSTYPE; | |
232 | static int yylex (union YYSTYPE *, parser_control *); | |
233 | static int yyerror (parser_control const *, char const *); | |
234 | static long int time_zone_hhmm (parser_control *, textint, long int); | |
235 | ||
236 | /** | |
237 | * Extract into *PC any date and time info from a string of digits | |
238 | * of the form e.g., YYYYMMDD, YYMMDD, HHMM, HH (and sometimes YYY, | |
239 | * YYYY, ...). | |
240 | */ | |
241 | static void digits_to_date_time(parser_control *pc, textint text_int) | |
242 | { | |
243 | if (pc->dates_seen && ! pc->year.digits | |
244 | && ! pc->rels_seen && (pc->times_seen || 2 < text_int.digits)) { | |
245 | pc->year_seen++; | |
246 | pc->year = text_int; | |
247 | } else { | |
248 | if (4 < text_int.digits) { | |
249 | pc->dates_seen++; | |
250 | pc->day = text_int.value % 100; | |
251 | pc->month = (text_int.value / 100) % 100; | |
252 | pc->year.value = text_int.value / 10000; | |
253 | pc->year.digits = text_int.digits - 4; | |
254 | } else { | |
255 | pc->times_seen++; | |
256 | if (text_int.digits <= 2) { | |
257 | pc->hour = text_int.value; | |
258 | pc->minutes = 0; | |
259 | } | |
260 | else { | |
261 | pc->hour = text_int.value / 100; | |
262 | pc->minutes = text_int.value % 100; | |
263 | } | |
264 | pc->seconds.tv_sec = 0; | |
265 | pc->seconds.tv_nsec = 0; | |
266 | pc->meridian = MER24; | |
267 | } | |
268 | } | |
269 | } | |
270 | ||
271 | /* Increment PC->rel by FACTOR * REL (FACTOR is 1 or -1). */ | |
272 | static void apply_relative_time(parser_control *pc, relative_time rel, | |
273 | int factor) | |
274 | { | |
275 | pc->rel.ns += factor * rel.ns; | |
276 | pc->rel.seconds += factor * rel.seconds; | |
277 | pc->rel.minutes += factor * rel.minutes; | |
278 | pc->rel.hour += factor * rel.hour; | |
279 | pc->rel.day += factor * rel.day; | |
280 | pc->rel.month += factor * rel.month; | |
281 | pc->rel.year += factor * rel.year; | |
282 | pc->rels_seen = 1; | |
283 | } | |
284 | ||
285 | /* Set PC-> hour, minutes, seconds and nanoseconds members from arguments. */ | |
286 | static void | |
287 | set_hhmmss(parser_control *pc, long int hour, long int minutes, | |
288 | time_t sec, long int nsec) | |
289 | { | |
290 | pc->hour = hour; | |
291 | pc->minutes = minutes; | |
292 | pc->seconds.tv_sec = sec; | |
293 | pc->seconds.tv_nsec = nsec; | |
294 | } | |
295 | ||
296 | %} | |
297 | ||
298 | /** | |
299 | * We want a reentrant parser, even if the TZ manipulation and the calls to | |
300 | * localtime and gmtime are not reentrant. | |
301 | */ | |
302 | %pure-parser | |
303 | %parse-param { parser_control *pc } | |
304 | %lex-param { parser_control *pc } | |
305 | ||
306 | /* This grammar has 31 shift/reduce conflicts. */ | |
307 | %expect 31 | |
308 | ||
309 | %union { | |
310 | long int intval; | |
311 | textint textintval; | |
312 | struct timespec timespec; | |
313 | relative_time rel; | |
314 | } | |
315 | ||
316 | %token <intval> tAGO | |
317 | %token tDST | |
318 | ||
319 | %token tYEAR_UNIT tMONTH_UNIT tHOUR_UNIT tMINUTE_UNIT tSEC_UNIT | |
320 | %token <intval> tDAY_UNIT tDAY_SHIFT | |
321 | ||
322 | %token <intval> tDAY tDAYZONE tLOCAL_ZONE tMERIDIAN | |
323 | %token <intval> tMONTH tORDINAL tZONE | |
324 | ||
325 | %token <textintval> tSNUMBER tUNUMBER | |
326 | %token <timespec> tSDECIMAL_NUMBER tUDECIMAL_NUMBER | |
327 | ||
328 | %type <intval> o_colon_minutes | |
329 | %type <timespec> seconds signed_seconds unsigned_seconds | |
330 | ||
331 | %type <rel> relunit relunit_snumber dayshift | |
332 | ||
333 | %% | |
334 | ||
335 | spec: | |
336 | timespec | |
337 | | items | |
338 | ; | |
339 | ||
340 | timespec: | |
341 | '@' seconds { | |
342 | pc->seconds = $2; | |
343 | pc->timespec_seen = 1; | |
344 | } | |
345 | ; | |
346 | ||
347 | items: | |
348 | /* empty */ | |
349 | | items item | |
350 | ; | |
351 | ||
352 | item: | |
353 | datetime { | |
354 | pc->times_seen++; pc->dates_seen++; | |
355 | } | |
356 | | time { | |
357 | pc->times_seen++; | |
358 | } | |
359 | | local_zone { | |
360 | pc->local_zones_seen++; | |
361 | } | |
362 | | zone { | |
363 | pc->zones_seen++; | |
364 | } | |
365 | | date { | |
366 | pc->dates_seen++; | |
367 | } | |
368 | | day { | |
369 | pc->days_seen++; | |
370 | } | |
371 | | rel | |
372 | | number | |
373 | | hybrid | |
374 | ; | |
375 | ||
376 | datetime: | |
377 | iso_8601_datetime | |
378 | ; | |
379 | ||
380 | iso_8601_datetime: | |
381 | iso_8601_date 'T' iso_8601_time | |
382 | ; | |
383 | ||
384 | time: | |
385 | tUNUMBER tMERIDIAN { | |
386 | set_hhmmss (pc, $1.value, 0, 0, 0); | |
387 | pc->meridian = $2; | |
388 | } | |
389 | | tUNUMBER ':' tUNUMBER tMERIDIAN { | |
390 | set_hhmmss (pc, $1.value, $3.value, 0, 0); | |
391 | pc->meridian = $4; | |
392 | } | |
393 | | tUNUMBER ':' tUNUMBER ':' unsigned_seconds tMERIDIAN { | |
394 | set_hhmmss (pc, $1.value, $3.value, $5.tv_sec, $5.tv_nsec); | |
395 | pc->meridian = $6; | |
396 | } | |
397 | | iso_8601_time | |
398 | ; | |
399 | ||
400 | iso_8601_time: | |
401 | tUNUMBER zone_offset { | |
402 | set_hhmmss (pc, $1.value, 0, 0, 0); | |
403 | pc->meridian = MER24; | |
404 | } | |
405 | | tUNUMBER ':' tUNUMBER o_zone_offset { | |
406 | set_hhmmss (pc, $1.value, $3.value, 0, 0); | |
407 | pc->meridian = MER24; | |
408 | } | |
409 | | tUNUMBER ':' tUNUMBER ':' unsigned_seconds o_zone_offset { | |
410 | set_hhmmss (pc, $1.value, $3.value, $5.tv_sec, $5.tv_nsec); | |
411 | pc->meridian = MER24; | |
412 | } | |
413 | ; | |
414 | ||
415 | o_zone_offset: | |
416 | /* empty */ | |
417 | | zone_offset | |
418 | ; | |
419 | ||
420 | zone_offset: | |
421 | tSNUMBER o_colon_minutes { | |
422 | pc->zones_seen++; | |
423 | pc->time_zone = time_zone_hhmm (pc, $1, $2); | |
424 | } | |
425 | ; | |
426 | ||
427 | /** | |
428 | * Local zone strings only affect DST setting, | |
429 | * and only take affect if the current TZ setting is relevant. | |
430 | * | |
431 | * Example 1: | |
432 | * 'EEST' is parsed as tLOCAL_ZONE, as it relates to the effective TZ: | |
433 | * TZ=Europe/Helsinki date -d '2016-12-30 EEST' | |
434 | * | |
435 | * Example 2: | |
436 | * 'EEST' is parsed as 'zone' (TZ=+03:00): | |
437 | * TZ=Asia/Tokyo ./src/date --debug -d '2011-06-11 EEST' | |
438 | * | |
439 | * This is implemented by probing the next three calendar quarters | |
440 | * of the effective timezone and looking for DST changes - | |
441 | * if found, the timezone name (EEST) is inserted into | |
442 | * the lexical lookup table with type tLOCAL_ZONE. | |
443 | * (Search for 'quarter' comment in 'parse_date'). | |
444 | */ | |
445 | local_zone: | |
446 | tLOCAL_ZONE { | |
447 | pc->local_isdst = $1; | |
448 | pc->dsts_seen += (0 < $1); | |
449 | } | |
450 | | tLOCAL_ZONE tDST { | |
451 | pc->local_isdst = 1; | |
452 | pc->dsts_seen += (0 < $1) + 1; | |
453 | } | |
454 | ; | |
455 | ||
456 | /** | |
457 | * Note 'T' is a special case, as it is used as the separator in ISO | |
458 | * 8601 date and time of day representation. | |
459 | */ | |
460 | zone: | |
461 | tZONE { | |
462 | pc->time_zone = $1; | |
463 | } | |
464 | | 'T' { | |
465 | pc->time_zone = HOUR(7); | |
466 | } | |
467 | | tZONE relunit_snumber { | |
468 | pc->time_zone = $1; | |
469 | apply_relative_time (pc, $2, 1); | |
470 | } | |
471 | | 'T' relunit_snumber { | |
472 | pc->time_zone = HOUR(7); | |
473 | apply_relative_time (pc, $2, 1); | |
474 | } | |
475 | | tZONE tSNUMBER o_colon_minutes { | |
476 | pc->time_zone = $1 + time_zone_hhmm (pc, $2, $3); | |
477 | } | |
478 | | tDAYZONE { | |
479 | pc->time_zone = $1 + 60; | |
480 | } | |
481 | | tZONE tDST { | |
482 | pc->time_zone = $1 + 60; | |
483 | } | |
484 | ; | |
485 | ||
486 | day: | |
487 | tDAY { | |
488 | pc->day_ordinal = 0; | |
489 | pc->day_number = $1; | |
490 | } | |
491 | | tDAY ',' { | |
492 | pc->day_ordinal = 0; | |
493 | pc->day_number = $1; | |
494 | } | |
495 | | tORDINAL tDAY { | |
496 | pc->day_ordinal = $1; | |
497 | pc->day_number = $2; | |
498 | pc->ordinal_day_seen = 1; | |
499 | } | |
500 | | tUNUMBER tDAY { | |
501 | pc->day_ordinal = $1.value; | |
502 | pc->day_number = $2; | |
503 | pc->ordinal_day_seen = 1; | |
504 | } | |
505 | ; | |
506 | ||
507 | date: | |
508 | tUNUMBER '/' tUNUMBER { | |
509 | pc->month = $1.value; | |
510 | pc->day = $3.value; | |
511 | } | |
512 | | tUNUMBER '/' tUNUMBER '/' tUNUMBER { | |
513 | /** | |
514 | * Interpret as YYYY/MM/DD if the first value has 4 or more digits, | |
515 | * otherwise as MM/DD/YY. | |
516 | * The goal in recognizing YYYY/MM/DD is solely to support legacy | |
517 | * machine-generated dates like those in an RCS log listing. If | |
518 | * you want portability, use the ISO 8601 format. | |
519 | */ | |
520 | if (4 <= $1.digits) { | |
521 | pc->year = $1; | |
522 | pc->month = $3.value; | |
523 | pc->day = $5.value; | |
524 | } else { | |
525 | pc->month = $1.value; | |
526 | pc->day = $3.value; | |
527 | pc->year = $5; | |
528 | } | |
529 | } | |
530 | | tUNUMBER tMONTH tSNUMBER { | |
531 | /* e.g. 17-JUN-1992. */ | |
532 | pc->day = $1.value; | |
533 | pc->month = $2; | |
534 | pc->year.value = -$3.value; | |
535 | pc->year.digits = $3.digits; | |
536 | } | |
537 | | tMONTH tSNUMBER tSNUMBER { | |
538 | /* e.g. JUN-17-1992. */ | |
539 | pc->month = $1; | |
540 | pc->day = -$2.value; | |
541 | pc->year.value = -$3.value; | |
542 | pc->year.digits = $3.digits; | |
543 | } | |
544 | | tMONTH tUNUMBER { | |
545 | pc->month = $1; | |
546 | pc->day = $2.value; | |
547 | } | |
548 | | tMONTH tUNUMBER ',' tUNUMBER { | |
549 | pc->month = $1; | |
550 | pc->day = $2.value; | |
551 | pc->year = $4; | |
552 | } | |
553 | | tUNUMBER tMONTH { | |
554 | pc->day = $1.value; | |
555 | pc->month = $2; | |
556 | } | |
557 | | tUNUMBER tMONTH tUNUMBER { | |
558 | pc->day = $1.value; | |
559 | pc->month = $2; | |
560 | pc->year = $3; | |
561 | } | |
562 | | iso_8601_date | |
563 | ; | |
564 | ||
565 | iso_8601_date: | |
566 | tUNUMBER tSNUMBER tSNUMBER { | |
567 | /* ISO 8601 format.YYYY-MM-DD. */ | |
568 | pc->year = $1; | |
569 | pc->month = -$2.value; | |
570 | pc->day = -$3.value; | |
571 | } | |
572 | ; | |
573 | ||
574 | rel: | |
575 | relunit tAGO | |
576 | { apply_relative_time (pc, $1, $2); } | |
577 | | relunit | |
578 | { apply_relative_time (pc, $1, 1); } | |
579 | | dayshift | |
580 | { apply_relative_time (pc, $1, 1); } | |
581 | ; | |
582 | ||
583 | relunit: | |
584 | tORDINAL tYEAR_UNIT | |
585 | { $$ = RELATIVE_TIME_0; $$.year = $1; } | |
586 | | tUNUMBER tYEAR_UNIT | |
587 | { $$ = RELATIVE_TIME_0; $$.year = $1.value; } | |
588 | | tYEAR_UNIT | |
589 | { $$ = RELATIVE_TIME_0; $$.year = 1; } | |
590 | | tORDINAL tMONTH_UNIT | |
591 | { $$ = RELATIVE_TIME_0; $$.month = $1; } | |
592 | | tUNUMBER tMONTH_UNIT | |
593 | { $$ = RELATIVE_TIME_0; $$.month = $1.value; } | |
594 | | tMONTH_UNIT | |
595 | { $$ = RELATIVE_TIME_0; $$.month = 1; } | |
596 | | tORDINAL tDAY_UNIT | |
597 | { $$ = RELATIVE_TIME_0; $$.day = $1 * $2; } | |
598 | | tUNUMBER tDAY_UNIT | |
599 | { $$ = RELATIVE_TIME_0; $$.day = $1.value * $2; } | |
600 | | tDAY_UNIT | |
601 | { $$ = RELATIVE_TIME_0; $$.day = $1; } | |
602 | | tORDINAL tHOUR_UNIT | |
603 | { $$ = RELATIVE_TIME_0; $$.hour = $1; } | |
604 | | tUNUMBER tHOUR_UNIT | |
605 | { $$ = RELATIVE_TIME_0; $$.hour = $1.value; } | |
606 | | tHOUR_UNIT | |
607 | { $$ = RELATIVE_TIME_0; $$.hour = 1; } | |
608 | | tORDINAL tMINUTE_UNIT | |
609 | { $$ = RELATIVE_TIME_0; $$.minutes = $1; } | |
610 | | tUNUMBER tMINUTE_UNIT | |
611 | { $$ = RELATIVE_TIME_0; $$.minutes = $1.value; } | |
612 | | tMINUTE_UNIT | |
613 | { $$ = RELATIVE_TIME_0; $$.minutes = 1; } | |
614 | | tORDINAL tSEC_UNIT | |
615 | { $$ = RELATIVE_TIME_0; $$.seconds = $1; } | |
616 | | tUNUMBER tSEC_UNIT | |
617 | { $$ = RELATIVE_TIME_0; $$.seconds = $1.value; } | |
618 | | tSDECIMAL_NUMBER tSEC_UNIT { | |
619 | $$ = RELATIVE_TIME_0; | |
620 | $$.seconds = $1.tv_sec; | |
621 | $$.ns = $1.tv_nsec; | |
622 | } | |
623 | | tUDECIMAL_NUMBER tSEC_UNIT { | |
624 | $$ = RELATIVE_TIME_0; | |
625 | $$.seconds = $1.tv_sec; | |
626 | $$.ns = $1.tv_nsec; | |
627 | } | |
628 | | tSEC_UNIT | |
629 | { $$ = RELATIVE_TIME_0; $$.seconds = 1; } | |
630 | | relunit_snumber | |
631 | ; | |
632 | ||
633 | relunit_snumber: | |
634 | tSNUMBER tYEAR_UNIT | |
635 | { $$ = RELATIVE_TIME_0; $$.year = $1.value; } | |
636 | | tSNUMBER tMONTH_UNIT | |
637 | { $$ = RELATIVE_TIME_0; $$.month = $1.value; } | |
638 | | tSNUMBER tDAY_UNIT | |
639 | { $$ = RELATIVE_TIME_0; $$.day = $1.value * $2; } | |
640 | | tSNUMBER tHOUR_UNIT | |
641 | { $$ = RELATIVE_TIME_0; $$.hour = $1.value; } | |
642 | | tSNUMBER tMINUTE_UNIT | |
643 | { $$ = RELATIVE_TIME_0; $$.minutes = $1.value; } | |
644 | | tSNUMBER tSEC_UNIT | |
645 | { $$ = RELATIVE_TIME_0; $$.seconds = $1.value; } | |
646 | ; | |
647 | ||
648 | dayshift: | |
649 | tDAY_SHIFT | |
650 | { $$ = RELATIVE_TIME_0; $$.day = $1; } | |
651 | ; | |
652 | ||
653 | seconds: signed_seconds | unsigned_seconds; | |
654 | ||
655 | signed_seconds: | |
656 | tSDECIMAL_NUMBER | |
657 | | tSNUMBER | |
658 | { $$.tv_sec = $1.value; $$.tv_nsec = 0; } | |
659 | ; | |
660 | ||
661 | unsigned_seconds: | |
662 | tUDECIMAL_NUMBER | |
663 | | tUNUMBER | |
664 | { $$.tv_sec = $1.value; $$.tv_nsec = 0; } | |
665 | ; | |
666 | ||
667 | number: | |
668 | tUNUMBER | |
669 | { digits_to_date_time (pc, $1); } | |
670 | ; | |
671 | ||
672 | hybrid: | |
673 | tUNUMBER relunit_snumber { | |
674 | /** | |
675 | * Hybrid all-digit and relative offset, so that we accept e.g., | |
676 | * "YYYYMMDD +N days" as well as "YYYYMMDD N days". | |
677 | */ | |
678 | digits_to_date_time (pc, $1); | |
679 | apply_relative_time (pc, $2, 1); | |
680 | } | |
681 | ; | |
682 | ||
683 | o_colon_minutes: | |
684 | /* empty */ | |
685 | { $$ = -1; } | |
686 | | ':' tUNUMBER | |
687 | { $$ = $2.value; } | |
688 | ; | |
689 | ||
690 | %% | |
691 | ||
692 | static table const meridian_table[] = { | |
693 | { "AM", tMERIDIAN, MERam }, | |
694 | { "A.M.", tMERIDIAN, MERam }, | |
695 | { "PM", tMERIDIAN, MERpm }, | |
696 | { "P.M.", tMERIDIAN, MERpm }, | |
697 | { NULL, 0, 0 } | |
698 | }; | |
699 | ||
700 | static table const dst_table[] = { | |
701 | { "DST", tDST, 0 } | |
702 | }; | |
703 | ||
704 | static table const month_and_day_table[] = { | |
705 | { "JANUARY", tMONTH, 1 }, | |
706 | { "FEBRUARY", tMONTH, 2 }, | |
707 | { "MARCH", tMONTH, 3 }, | |
708 | { "APRIL", tMONTH, 4 }, | |
709 | { "MAY", tMONTH, 5 }, | |
710 | { "JUNE", tMONTH, 6 }, | |
711 | { "JULY", tMONTH, 7 }, | |
712 | { "AUGUST", tMONTH, 8 }, | |
713 | { "SEPTEMBER",tMONTH, 9 }, | |
714 | { "SEPT", tMONTH, 9 }, | |
715 | { "OCTOBER", tMONTH, 10 }, | |
716 | { "NOVEMBER", tMONTH, 11 }, | |
717 | { "DECEMBER", tMONTH, 12 }, | |
718 | { "SUNDAY", tDAY, 0 }, | |
719 | { "MONDAY", tDAY, 1 }, | |
720 | { "TUESDAY", tDAY, 2 }, | |
721 | { "TUES", tDAY, 2 }, | |
722 | { "WEDNESDAY",tDAY, 3 }, | |
723 | { "WEDNES", tDAY, 3 }, | |
724 | { "THURSDAY", tDAY, 4 }, | |
725 | { "THUR", tDAY, 4 }, | |
726 | { "THURS", tDAY, 4 }, | |
727 | { "FRIDAY", tDAY, 5 }, | |
728 | { "SATURDAY", tDAY, 6 }, | |
729 | { NULL, 0, 0 } | |
730 | }; | |
731 | ||
732 | static table const time_units_table[] = { | |
733 | { "YEAR", tYEAR_UNIT, 1 }, | |
734 | { "MONTH", tMONTH_UNIT, 1 }, | |
735 | { "FORTNIGHT",tDAY_UNIT, 14 }, | |
736 | { "WEEK", tDAY_UNIT, 7 }, | |
737 | { "DAY", tDAY_UNIT, 1 }, | |
738 | { "HOUR", tHOUR_UNIT, 1 }, | |
739 | { "MINUTE", tMINUTE_UNIT, 1 }, | |
740 | { "MIN", tMINUTE_UNIT, 1 }, | |
741 | { "SECOND", tSEC_UNIT, 1 }, | |
742 | { "SEC", tSEC_UNIT, 1 }, | |
743 | { NULL, 0, 0 } | |
744 | }; | |
745 | ||
746 | /* Assorted relative-time words. */ | |
747 | static table const relative_time_table[] = { | |
748 | { "TOMORROW", tDAY_SHIFT, 1 }, | |
749 | { "YESTERDAY",tDAY_SHIFT, -1 }, | |
750 | { "TODAY", tDAY_SHIFT, 0 }, | |
751 | { "NOW", tDAY_SHIFT, 0 }, | |
752 | { "LAST", tORDINAL, -1 }, | |
753 | { "THIS", tORDINAL, 0 }, | |
754 | { "NEXT", tORDINAL, 1 }, | |
755 | { "FIRST", tORDINAL, 1 }, | |
756 | /*{ "SECOND", tORDINAL, 2 }, */ | |
757 | { "THIRD", tORDINAL, 3 }, | |
758 | { "FOURTH", tORDINAL, 4 }, | |
759 | { "FIFTH", tORDINAL, 5 }, | |
760 | { "SIXTH", tORDINAL, 6 }, | |
761 | { "SEVENTH", tORDINAL, 7 }, | |
762 | { "EIGHTH", tORDINAL, 8 }, | |
763 | { "NINTH", tORDINAL, 9 }, | |
764 | { "TENTH", tORDINAL, 10 }, | |
765 | { "ELEVENTH", tORDINAL, 11 }, | |
766 | { "TWELFTH", tORDINAL, 12 }, | |
767 | { "AGO", tAGO, -1 }, | |
768 | { "HENCE", tAGO, 1 }, | |
769 | { NULL, 0, 0 } | |
770 | }; | |
771 | ||
772 | /** | |
773 | * The universal time zone table. These labels can be used even for | |
774 | * timestamps that would not otherwise be valid, e.g., GMT timestamps | |
775 | * in London during summer. | |
776 | */ | |
777 | static table const universal_time_zone_table[] = { | |
778 | { "GMT", tZONE, HOUR ( 0) }, /* Greenwich Mean */ | |
779 | { "UT", tZONE, HOUR ( 0) }, /* Universal (Coordinated) */ | |
780 | { "UTC", tZONE, HOUR ( 0) }, | |
781 | { NULL, 0, 0 } | |
782 | }; | |
783 | ||
784 | /** | |
785 | * The time zone table. This table is necessarily incomplete, as time | |
786 | * zone abbreviations are ambiguous; e.g. Australians interpret "EST" | |
787 | * as Eastern time in Australia, not as US Eastern Standard Time. | |
788 | * You cannot rely on parse_date to handle arbitrary time zone | |
789 | * abbreviations; use numeric abbreviations like "-0500" instead. | |
790 | */ | |
791 | static table const time_zone_table[] = { | |
792 | { "WET", tZONE, HOUR ( 0) }, /* Western European */ | |
793 | { "WEST", tDAYZONE, HOUR ( 0) }, /* Western European Summer */ | |
794 | { "BST", tDAYZONE, HOUR ( 0) }, /* British Summer */ | |
795 | { "ART", tZONE, -HOUR ( 3) }, /* Argentina */ | |
796 | { "BRT", tZONE, -HOUR ( 3) }, /* Brazil */ | |
797 | { "BRST", tDAYZONE, -HOUR ( 3) }, /* Brazil Summer */ | |
798 | { "NST", tZONE, -(HOUR ( 3) + 30) }, /* Newfoundland Standard */ | |
799 | { "NDT", tDAYZONE,-(HOUR ( 3) + 30) }, /* Newfoundland Daylight */ | |
800 | { "AST", tZONE, -HOUR ( 4) }, /* Atlantic Standard */ | |
801 | { "ADT", tDAYZONE, -HOUR ( 4) }, /* Atlantic Daylight */ | |
802 | { "CLT", tZONE, -HOUR ( 4) }, /* Chile */ | |
803 | { "CLST", tDAYZONE, -HOUR ( 4) }, /* Chile Summer */ | |
804 | { "EST", tZONE, -HOUR ( 5) }, /* Eastern Standard */ | |
805 | { "EDT", tDAYZONE, -HOUR ( 5) }, /* Eastern Daylight */ | |
806 | { "CST", tZONE, -HOUR ( 6) }, /* Central Standard */ | |
807 | { "CDT", tDAYZONE, -HOUR ( 6) }, /* Central Daylight */ | |
808 | { "MST", tZONE, -HOUR ( 7) }, /* Mountain Standard */ | |
809 | { "MDT", tDAYZONE, -HOUR ( 7) }, /* Mountain Daylight */ | |
810 | { "PST", tZONE, -HOUR ( 8) }, /* Pacific Standard */ | |
811 | { "PDT", tDAYZONE, -HOUR ( 8) }, /* Pacific Daylight */ | |
812 | { "AKST", tZONE, -HOUR ( 9) }, /* Alaska Standard */ | |
813 | { "AKDT", tDAYZONE, -HOUR ( 9) }, /* Alaska Daylight */ | |
814 | { "HST", tZONE, -HOUR (10) }, /* Hawaii Standard */ | |
815 | { "HAST", tZONE, -HOUR (10) }, /* Hawaii-Aleutian Standard */ | |
816 | { "HADT", tDAYZONE, -HOUR (10) }, /* Hawaii-Aleutian Daylight */ | |
817 | { "SST", tZONE, -HOUR (12) }, /* Samoa Standard */ | |
818 | { "WAT", tZONE, HOUR ( 1) }, /* West Africa */ | |
819 | { "CET", tZONE, HOUR ( 1) }, /* Central European */ | |
820 | { "CEST", tDAYZONE, HOUR ( 1) }, /* Central European Summer */ | |
821 | { "MET", tZONE, HOUR ( 1) }, /* Middle European */ | |
822 | { "MEZ", tZONE, HOUR ( 1) }, /* Middle European */ | |
823 | { "MEST", tDAYZONE, HOUR ( 1) }, /* Middle European Summer */ | |
824 | { "MESZ", tDAYZONE, HOUR ( 1) }, /* Middle European Summer */ | |
825 | { "EET", tZONE, HOUR ( 2) }, /* Eastern European */ | |
826 | { "EEST", tDAYZONE, HOUR ( 2) }, /* Eastern European Summer */ | |
827 | { "CAT", tZONE, HOUR ( 2) }, /* Central Africa */ | |
828 | { "SAST", tZONE, HOUR ( 2) }, /* South Africa Standard */ | |
829 | { "EAT", tZONE, HOUR ( 3) }, /* East Africa */ | |
830 | { "MSK", tZONE, HOUR ( 3) }, /* Moscow */ | |
831 | { "MSD", tDAYZONE, HOUR ( 3) }, /* Moscow Daylight */ | |
832 | { "IST", tZONE, (HOUR ( 5) + 30) }, /* India Standard */ | |
833 | { "SGT", tZONE, HOUR ( 8) }, /* Singapore */ | |
834 | { "KST", tZONE, HOUR ( 9) }, /* Korea Standard */ | |
835 | { "JST", tZONE, HOUR ( 9) }, /* Japan Standard */ | |
836 | { "GST", tZONE, HOUR (10) }, /* Guam Standard */ | |
837 | { "NZST", tZONE, HOUR (12) }, /* New Zealand Standard */ | |
838 | { "NZDT", tDAYZONE, HOUR (12) }, /* New Zealand Daylight */ | |
839 | { NULL, 0, 0 } | |
840 | }; | |
841 | ||
842 | /** | |
843 | * Military time zone table. | |
844 | * | |
845 | * Note 'T' is a special case, as it is used as the separator in ISO | |
846 | * 8601 date and time of day representation. | |
847 | */ | |
848 | static table const military_table[] = { | |
849 | { "A", tZONE, -HOUR ( 1) }, | |
850 | { "B", tZONE, -HOUR ( 2) }, | |
851 | { "C", tZONE, -HOUR ( 3) }, | |
852 | { "D", tZONE, -HOUR ( 4) }, | |
853 | { "E", tZONE, -HOUR ( 5) }, | |
854 | { "F", tZONE, -HOUR ( 6) }, | |
855 | { "G", tZONE, -HOUR ( 7) }, | |
856 | { "H", tZONE, -HOUR ( 8) }, | |
857 | { "I", tZONE, -HOUR ( 9) }, | |
858 | { "K", tZONE, -HOUR (10) }, | |
859 | { "L", tZONE, -HOUR (11) }, | |
860 | { "M", tZONE, -HOUR (12) }, | |
861 | { "N", tZONE, HOUR ( 1) }, | |
862 | { "O", tZONE, HOUR ( 2) }, | |
863 | { "P", tZONE, HOUR ( 3) }, | |
864 | { "Q", tZONE, HOUR ( 4) }, | |
865 | { "R", tZONE, HOUR ( 5) }, | |
866 | { "S", tZONE, HOUR ( 6) }, | |
867 | { "T", 'T', 0 }, | |
868 | { "U", tZONE, HOUR ( 8) }, | |
869 | { "V", tZONE, HOUR ( 9) }, | |
870 | { "W", tZONE, HOUR (10) }, | |
871 | { "X", tZONE, HOUR (11) }, | |
872 | { "Y", tZONE, HOUR (12) }, | |
873 | { "Z", tZONE, HOUR ( 0) }, | |
874 | { NULL, 0, 0 } | |
875 | }; | |
876 | ||
877 | \f | |
878 | ||
879 | /** | |
880 | * Convert a time zone expressed as HH:MM into an integer count of | |
881 | * minutes. If MM is negative, then S is of the form HHMM and needs | |
882 | * to be picked apart; otherwise, S is of the form HH. As specified in | |
883 | * http://www.opengroup.org/susv3xbd/xbd_chap08.html#tag_08_03, allow | |
884 | * only valid TZ range, and consider first two digits as hours, if no | |
885 | * minutes specified. | |
886 | */ | |
887 | ||
888 | static long int time_zone_hhmm(parser_control *pc, textint s, long int mm) | |
889 | { | |
890 | long int n_minutes; | |
891 | ||
892 | /** | |
893 | * If the length of S is 1 or 2 and no minutes are specified, | |
894 | * interpret it as a number of hours. | |
895 | */ | |
896 | if (s.digits <= 2 && mm < 0) | |
897 | s.value *= 100; | |
898 | ||
899 | if (mm < 0) | |
900 | n_minutes = (s.value / 100) * 60 + s.value % 100; | |
901 | else | |
902 | n_minutes = s.value * 60 + (s.negative ? -mm : mm); | |
903 | ||
904 | /** | |
905 | * If the absolute number of minutes is larger than 24 hours, | |
906 | * arrange to reject it by incrementing pc->zones_seen. Thus, | |
907 | * we allow only values in the range UTC-24:00 to UTC+24:00. | |
908 | */ | |
909 | if (24 * 60 < abs (n_minutes)) | |
910 | pc->zones_seen++; | |
911 | ||
912 | return n_minutes; | |
913 | } | |
914 | ||
915 | static int to_hour(long int hours, int meridian) | |
916 | { | |
917 | switch (meridian) { | |
918 | default: /* Pacify GCC. */ | |
919 | case MER24: | |
920 | return 0 <= hours && hours < 24 ? hours : -1; | |
921 | case MERam: | |
922 | return 0 < hours && hours < 12 ? hours : hours == 12 ? 0 : -1; | |
923 | case MERpm: | |
924 | return 0 < hours && hours < 12 ? hours + 12 : hours == 12 ? 12 : -1; | |
925 | } | |
926 | } | |
927 | ||
928 | static long int to_year(textint textyear) | |
929 | { | |
930 | long int year = textyear.value; | |
931 | ||
932 | if (year < 0) | |
933 | year = -year; | |
934 | ||
935 | /** | |
936 | * XPG4 suggests that years 00-68 map to 2000-2068, and | |
937 | * years 69-99 map to 1969-1999. | |
938 | */ | |
939 | else if (textyear.digits == 2) | |
940 | year += year < 69 ? 2000 : 1900; | |
941 | ||
942 | return year; | |
943 | } | |
944 | ||
945 | static table const * lookup_zone(parser_control const *pc, char const *name) | |
946 | { | |
947 | table const *tp; | |
948 | ||
949 | for (tp = universal_time_zone_table; tp->name; tp++) | |
950 | if (strcmp (name, tp->name) == 0) | |
951 | return tp; | |
952 | ||
953 | /** | |
954 | * Try local zone abbreviations before those in time_zone_table, as | |
955 | * the local ones are more likely to be right. | |
956 | */ | |
957 | for (tp = pc->local_time_zone_table; tp->name; tp++) | |
958 | if (strcmp (name, tp->name) == 0) | |
959 | return tp; | |
960 | ||
961 | for (tp = time_zone_table; tp->name; tp++) | |
962 | if (strcmp (name, tp->name) == 0) | |
963 | return tp; | |
964 | ||
965 | return NULL; | |
966 | } | |
967 | ||
968 | #if ! HAVE_TM_GMTOFF | |
969 | /** | |
970 | * Yield the difference between *A and *B, | |
971 | * measured in seconds, ignoring leap seconds. | |
972 | * The body of this function is taken directly from the GNU C Library; | |
973 | * see src/strftime.c. | |
974 | */ | |
975 | static long int tm_diff(struct tm const *a, struct tm const *b) | |
976 | { | |
977 | /** | |
978 | * Compute intervening leap days correctly even if year is negative. | |
979 | * Take care to avoid int overflow in leap day calculations. | |
980 | */ | |
981 | int a4 = SHR (a->tm_year, 2) + SHR (TM_YEAR_BASE, 2) - ! (a->tm_year & 3); | |
982 | int b4 = SHR (b->tm_year, 2) + SHR (TM_YEAR_BASE, 2) - ! (b->tm_year & 3); | |
983 | int a100 = a4 / 25 - (a4 % 25 < 0); | |
984 | int b100 = b4 / 25 - (b4 % 25 < 0); | |
985 | int a400 = SHR (a100, 2); | |
986 | int b400 = SHR (b100, 2); | |
987 | int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); | |
988 | long int ayear = a->tm_year; | |
989 | long int years = ayear - b->tm_year; | |
990 | long int days = (365 * years + intervening_leap_days | |
991 | + (a->tm_yday - b->tm_yday)); | |
992 | return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour)) | |
993 | + (a->tm_min - b->tm_min)) | |
994 | + (a->tm_sec - b->tm_sec)); | |
995 | } | |
996 | #endif /* ! HAVE_TM_GMTOFF */ | |
997 | ||
998 | static table const * lookup_word(parser_control const *pc, char *word) | |
999 | { | |
1000 | char *p; | |
1001 | char *q; | |
1002 | size_t wordlen; | |
1003 | table const *tp; | |
1004 | int period_found; | |
1005 | int abbrev; | |
1006 | ||
1007 | /* Make it uppercase. */ | |
1008 | for (p = word; *p; p++) { | |
1009 | unsigned char ch = *p; | |
1010 | *p = c_toupper (ch); | |
1011 | } | |
1012 | ||
1013 | for (tp = meridian_table; tp->name; tp++) | |
1014 | if (strcmp (word, tp->name) == 0) | |
1015 | return tp; | |
1016 | ||
1017 | /* See if we have an abbreviation for a month. */ | |
1018 | wordlen = strlen (word); | |
1019 | abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.'); | |
1020 | ||
1021 | for (tp = month_and_day_table; tp->name; tp++) | |
1022 | if ((abbrev ? strncmp (word, tp->name, 3) : | |
1023 | strcmp (word, tp->name)) == 0) | |
1024 | return tp; | |
1025 | ||
1026 | if ((tp = lookup_zone (pc, word))) | |
1027 | return tp; | |
1028 | ||
1029 | if (strcmp (word, dst_table[0].name) == 0) | |
1030 | return dst_table; | |
1031 | ||
1032 | for (tp = time_units_table; tp->name; tp++) | |
1033 | if (strcmp (word, tp->name) == 0) | |
1034 | return tp; | |
1035 | ||
1036 | /* Strip off any plural and try the units table again. */ | |
1037 | if (word[wordlen - 1] == 'S') { | |
1038 | word[wordlen - 1] = '\0'; | |
1039 | for (tp = time_units_table; tp->name; tp++) | |
1040 | if (strcmp (word, tp->name) == 0) | |
1041 | return tp; | |
1042 | word[wordlen - 1] = 'S'; /* For "this" in relative_time_table. */ | |
1043 | } | |
1044 | ||
1045 | for (tp = relative_time_table; tp->name; tp++) | |
1046 | if (strcmp (word, tp->name) == 0) | |
1047 | return tp; | |
1048 | ||
1049 | /* Military time zones. */ | |
1050 | if (wordlen == 1) | |
1051 | for (tp = military_table; tp->name; tp++) | |
1052 | if (word[0] == tp->name[0]) | |
1053 | return tp; | |
1054 | ||
1055 | /* Drop out any periods and try the time zone table again. */ | |
1056 | for (period_found = 0, p = q = word; (*p = *q); q++) | |
1057 | if (*q == '.') | |
1058 | period_found = 1; | |
1059 | else | |
1060 | p++; | |
1061 | if (period_found && (tp = lookup_zone (pc, word))) | |
1062 | return tp; | |
1063 | ||
1064 | return NULL; | |
1065 | } | |
1066 | ||
1067 | static int yylex (union YYSTYPE *lvalp, parser_control *pc) | |
1068 | { | |
1069 | unsigned char c; | |
1070 | size_t count; | |
1071 | ||
1072 | for (;;) { | |
1073 | while (c = *pc->input, c_isspace (c)) | |
1074 | pc->input++; | |
1075 | ||
1076 | if (ISDIGIT (c) || c == '-' || c == '+') { | |
1077 | char const *p; | |
1078 | int sign; | |
1079 | unsigned long int value; | |
1080 | if (c == '-' || c == '+') { | |
1081 | sign = c == '-' ? -1 : 1; | |
1082 | while (c = *++pc->input, c_isspace (c)) | |
1083 | continue; | |
1084 | if (! ISDIGIT (c)) | |
1085 | /* skip the '-' sign */ | |
1086 | continue; | |
1087 | } else | |
1088 | sign = 0; | |
1089 | p = pc->input; | |
1090 | for (value = 0; ; value *= 10) { | |
1091 | unsigned long int value1 = value + (c - '0'); | |
1092 | if (value1 < value) | |
1093 | return '?'; | |
1094 | value = value1; | |
1095 | c = *++p; | |
1096 | if (! ISDIGIT (c)) | |
1097 | break; | |
1098 | if (ULONG_MAX / 10 < value) | |
1099 | return '?'; | |
1100 | } | |
1101 | if ((c == '.' || c == ',') && ISDIGIT (p[1])) { | |
1102 | time_t s; | |
1103 | int ns; | |
1104 | int digits; | |
1105 | unsigned long int value1; | |
1106 | ||
1107 | /* Check for overflow when converting value to | |
1108 | * time_t. | |
1109 | */ | |
1110 | if (sign < 0) { | |
1111 | s = - value; | |
1112 | if (0 < s) | |
1113 | return '?'; | |
1114 | value1 = -s; | |
1115 | } else { | |
1116 | s = value; | |
1117 | if (s < 0) | |
1118 | return '?'; | |
1119 | value1 = s; | |
1120 | } | |
1121 | if (value != value1) | |
1122 | return '?'; | |
1123 | ||
1124 | /* Accumulate fraction, to ns precision. */ | |
1125 | p++; | |
1126 | ns = *p++ - '0'; | |
1127 | for (digits = 2; | |
1128 | digits <= LOG10_BILLION; digits++) { | |
1129 | ns *= 10; | |
1130 | if (ISDIGIT (*p)) | |
1131 | ns += *p++ - '0'; | |
1132 | } | |
1133 | ||
1134 | /* Skip excess digits, truncating toward | |
1135 | * -Infinity. | |
1136 | */ | |
1137 | if (sign < 0) | |
1138 | for (; ISDIGIT (*p); p++) | |
1139 | if (*p != '0') { | |
1140 | ns++; | |
1141 | break; | |
1142 | } | |
1143 | while (ISDIGIT (*p)) | |
1144 | p++; | |
1145 | ||
1146 | /* Adjust to the timespec convention, which is | |
1147 | * that tv_nsec is always a positive offset even | |
1148 | * if tv_sec is negative. | |
1149 | */ | |
1150 | if (sign < 0 && ns) { | |
1151 | s--; | |
1152 | if (! (s < 0)) | |
1153 | return '?'; | |
1154 | ns = BILLION - ns; | |
1155 | } | |
1156 | ||
1157 | lvalp->timespec.tv_sec = s; | |
1158 | lvalp->timespec.tv_nsec = ns; | |
1159 | pc->input = p; | |
1160 | return | |
1161 | sign ? tSDECIMAL_NUMBER : tUDECIMAL_NUMBER; | |
1162 | } else { | |
1163 | lvalp->textintval.negative = sign < 0; | |
1164 | if (sign < 0) { | |
1165 | lvalp->textintval.value = - value; | |
1166 | if (0 < lvalp->textintval.value) | |
1167 | return '?'; | |
1168 | } else { | |
1169 | lvalp->textintval.value = value; | |
1170 | if (lvalp->textintval.value < 0) | |
1171 | return '?'; | |
1172 | } | |
1173 | lvalp->textintval.digits = p - pc->input; | |
1174 | pc->input = p; | |
1175 | return sign ? tSNUMBER : tUNUMBER; | |
1176 | } | |
1177 | } | |
1178 | ||
1179 | if (c_isalpha (c)) { | |
1180 | char buff[20]; | |
1181 | char *p = buff; | |
1182 | table const *tp; | |
1183 | ||
1184 | do { | |
1185 | if (p < buff + sizeof buff - 1) | |
1186 | *p++ = c; | |
1187 | c = *++pc->input; | |
1188 | } | |
1189 | while (c_isalpha (c) || c == '.'); | |
1190 | ||
1191 | *p = '\0'; | |
1192 | tp = lookup_word (pc, buff); | |
1193 | if (! tp) { | |
1194 | return '?'; | |
1195 | } | |
1196 | lvalp->intval = tp->value; | |
1197 | return tp->type; | |
1198 | } | |
1199 | ||
1200 | if (c != '(') | |
1201 | return to_uchar (*pc->input++); | |
1202 | ||
1203 | count = 0; | |
1204 | do { | |
1205 | c = *pc->input++; | |
1206 | if (c == '\0') | |
1207 | return c; | |
1208 | if (c == '(') | |
1209 | count++; | |
1210 | else if (c == ')') | |
1211 | count--; | |
1212 | } | |
1213 | while (count != 0); | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | /* Do nothing if the parser reports an error. */ | |
1218 | static int yyerror(parser_control const *pc __attribute__((__unused__)), | |
1219 | char const *s __attribute__((__unused__))) | |
1220 | { | |
1221 | return 0; | |
1222 | } | |
1223 | ||
1224 | /** | |
1225 | * If *TM0 is the old and *TM1 is the new value of a struct tm after | |
1226 | * passing it to mktime, return 1 if it's OK that mktime returned T. | |
1227 | * It's not OK if *TM0 has out-of-range members. | |
1228 | */ | |
1229 | ||
1230 | static int mktime_ok(struct tm const *tm0, struct tm const *tm1, time_t t) | |
1231 | { | |
1232 | if (t == (time_t) -1) { | |
1233 | /** | |
1234 | * Guard against falsely reporting an error when parsing a | |
1235 | * timestamp that happens to equal (time_t) -1, on a host that | |
1236 | * supports such a timestamp. | |
1237 | */ | |
1238 | tm1 = localtime (&t); | |
1239 | if (!tm1) | |
1240 | return 0; | |
1241 | } | |
1242 | ||
1243 | return ! ((tm0->tm_sec ^ tm1->tm_sec) | |
1244 | | (tm0->tm_min ^ tm1->tm_min) | |
1245 | | (tm0->tm_hour ^ tm1->tm_hour) | |
1246 | | (tm0->tm_mday ^ tm1->tm_mday) | |
1247 | | (tm0->tm_mon ^ tm1->tm_mon) | |
1248 | | (tm0->tm_year ^ tm1->tm_year)); | |
1249 | } | |
1250 | ||
1251 | /** | |
1252 | * A reasonable upper bound for the size of ordinary TZ strings. | |
1253 | * Use heap allocation if TZ's length exceeds this. | |
1254 | */ | |
1255 | enum { TZBUFSIZE = 100 }; | |
1256 | ||
1257 | /** | |
1258 | * Return a copy of TZ, stored in TZBUF if it fits, and heap-allocated | |
1259 | * otherwise. | |
1260 | */ | |
1261 | static char * get_tz(char tzbuf[TZBUFSIZE]) | |
1262 | { | |
1263 | char *tz = getenv ("TZ"); | |
1264 | if (tz) { | |
1265 | size_t tzsize = strlen (tz) + 1; | |
1266 | tz = (tzsize <= TZBUFSIZE | |
1267 | ? memcpy (tzbuf, tz, tzsize) | |
1268 | : xstrdup (tz)); | |
1269 | } | |
1270 | return tz; | |
1271 | } | |
1272 | ||
1273 | /** | |
1274 | * Parse a date/time string, storing the resulting time value into *result. | |
1275 | * The string itself is pointed to by *p. Return 1 if successful. | |
1276 | * *p can be an incomplete or relative time specification; if so, use | |
1277 | * *now as the basis for the returned time. | |
1278 | */ | |
1279 | int parse_date(struct timespec *result, char const *p, | |
1280 | struct timespec const *now) | |
1281 | { | |
1282 | time_t Start; | |
1283 | long int Start_ns; | |
1284 | struct tm const *tmp; | |
1285 | struct tm tm; | |
1286 | struct tm tm0; | |
1287 | parser_control pc; | |
1288 | struct timespec gettime_buffer; | |
1289 | unsigned char c; | |
1290 | int tz_was_altered = 0; | |
1291 | char *tz0 = NULL; | |
1292 | char tz0buf[TZBUFSIZE]; | |
1293 | int ok = 1; | |
1294 | struct timeval tv; | |
1295 | ||
1296 | if (! now) { | |
1297 | gettimeofday (&tv, NULL); | |
1298 | gettime_buffer.tv_sec = tv.tv_sec; | |
1299 | gettime_buffer.tv_nsec = tv.tv_usec * 1000; | |
1300 | now = &gettime_buffer; | |
1301 | } | |
1302 | ||
1303 | Start = now->tv_sec; | |
1304 | Start_ns = now->tv_nsec; | |
1305 | ||
1306 | tmp = localtime (&now->tv_sec); | |
1307 | if (! tmp) | |
1308 | return 0; | |
1309 | ||
1310 | while (c = *p, c_isspace (c)) | |
1311 | p++; | |
1312 | ||
1313 | if (strncmp (p, "TZ=\"", 4) == 0) { | |
1314 | char const *tzbase = p + 4; | |
1315 | size_t tzsize = 1; | |
1316 | char const *s; | |
1317 | ||
1318 | for (s = tzbase; *s; s++, tzsize++) | |
1319 | if (*s == '\\') { | |
1320 | s++; | |
1321 | if (! (*s == '\\' || *s == '"')) | |
1322 | break; | |
1323 | } else if (*s == '"') { | |
1324 | char *z; | |
1325 | char *tz1; | |
1326 | char tz1buf[TZBUFSIZE]; | |
1327 | int large_tz = TZBUFSIZE < tzsize; | |
1328 | int setenv_ok; | |
1329 | tz0 = get_tz (tz0buf); | |
1330 | z = tz1 = large_tz ? malloc (tzsize) : tz1buf; | |
1331 | for (s = tzbase; *s != '"'; s++) | |
1332 | *z++ = *(s += *s == '\\'); | |
1333 | *z = '\0'; | |
1334 | setenv_ok = setenv ("TZ", tz1, 1) == 0; | |
1335 | if (large_tz) | |
1336 | free (tz1); | |
1337 | if (!setenv_ok) | |
1338 | goto fail; | |
1339 | tz_was_altered = 1; | |
1340 | ||
1341 | p = s + 1; | |
1342 | while (c = *p, c_isspace (c)) | |
1343 | p++; | |
1344 | ||
1345 | break; | |
1346 | } | |
1347 | } | |
1348 | ||
1349 | /** | |
1350 | * As documented, be careful to treat the empty string just like | |
1351 | * a date string of "0". Without this, an empty string would be | |
1352 | * declared invalid when parsed during a DST transition. | |
1353 | */ | |
1354 | if (*p == '\0') | |
1355 | p = "0"; | |
1356 | ||
1357 | pc.input = p; | |
1358 | pc.year.value = tmp->tm_year; | |
1359 | pc.year.value += TM_YEAR_BASE; | |
1360 | pc.year.digits = 0; | |
1361 | pc.month = tmp->tm_mon + 1; | |
1362 | pc.day = tmp->tm_mday; | |
1363 | pc.hour = tmp->tm_hour; | |
1364 | pc.minutes = tmp->tm_min; | |
1365 | pc.seconds.tv_sec = tmp->tm_sec; | |
1366 | pc.seconds.tv_nsec = Start_ns; | |
1367 | tm.tm_isdst = tmp->tm_isdst; | |
1368 | ||
1369 | pc.meridian = MER24; | |
1370 | pc.rel = RELATIVE_TIME_0; | |
1371 | pc.timespec_seen = 0; | |
1372 | pc.rels_seen = 0; | |
1373 | pc.dates_seen = 0; | |
1374 | pc.days_seen = 0; | |
1375 | pc.times_seen = 0; | |
1376 | pc.local_zones_seen = 0; | |
1377 | pc.dsts_seen = 0; | |
1378 | pc.zones_seen = 0; | |
1379 | pc.year_seen = 0; | |
1380 | pc.ordinal_day_seen = 0; | |
1381 | ||
1382 | #if HAVE_STRUCT_TM_TM_ZONE | |
1383 | pc.local_time_zone_table[0].name = tmp->tm_zone; | |
1384 | pc.local_time_zone_table[0].type = tLOCAL_ZONE; | |
1385 | pc.local_time_zone_table[0].value = tmp->tm_isdst; | |
1386 | pc.local_time_zone_table[1].name = NULL; | |
1387 | ||
1388 | /** | |
1389 | * Probe the names used in the next three calendar quarters, looking | |
1390 | * for a tm_isdst different from the one we already have. | |
1391 | */ | |
1392 | { | |
1393 | int quarter; | |
1394 | for (quarter = 1; quarter <= 3; quarter++) { | |
1395 | time_t probe = Start + quarter * (90 * 24 * 60 * 60); | |
1396 | struct tm const *probe_tm = localtime (&probe); | |
1397 | if (probe_tm && probe_tm->tm_zone | |
1398 | && probe_tm->tm_isdst | |
1399 | != pc.local_time_zone_table[0].value) { | |
1400 | { | |
1401 | pc.local_time_zone_table[1].name | |
1402 | = probe_tm->tm_zone; | |
1403 | pc.local_time_zone_table[1].type | |
1404 | = tLOCAL_ZONE; | |
1405 | pc.local_time_zone_table[1].value | |
1406 | = probe_tm->tm_isdst; | |
1407 | pc.local_time_zone_table[2].name | |
1408 | = NULL; | |
1409 | } | |
1410 | break; | |
1411 | } | |
1412 | } | |
1413 | } | |
1414 | #else | |
1415 | #if HAVE_TZNAME | |
1416 | { | |
1417 | # if !HAVE_DECL_TZNAME | |
1418 | extern char *tzname[]; | |
1419 | # endif | |
1420 | int i; | |
1421 | for (i = 0; i < 2; i++) { | |
1422 | pc.local_time_zone_table[i].name = tzname[i]; | |
1423 | pc.local_time_zone_table[i].type = tLOCAL_ZONE; | |
1424 | pc.local_time_zone_table[i].value = i; | |
1425 | } | |
1426 | pc.local_time_zone_table[i].name = NULL; | |
1427 | } | |
1428 | #else | |
1429 | pc.local_time_zone_table[0].name = NULL; | |
1430 | #endif | |
1431 | #endif | |
1432 | ||
1433 | if (pc.local_time_zone_table[0].name && pc.local_time_zone_table[1].name | |
1434 | && ! strcmp (pc.local_time_zone_table[0].name, | |
1435 | pc.local_time_zone_table[1].name)) { | |
1436 | /** | |
1437 | * This locale uses the same abbreviation for standard and | |
1438 | * daylight times. So if we see that abbreviation, we don't | |
1439 | * know whether it's daylight time. | |
1440 | */ | |
1441 | pc.local_time_zone_table[0].value = -1; | |
1442 | pc.local_time_zone_table[1].name = NULL; | |
1443 | } | |
1444 | ||
1445 | if (yyparse (&pc) != 0) { | |
1446 | goto fail; | |
1447 | } | |
1448 | ||
1449 | if (pc.timespec_seen) | |
1450 | *result = pc.seconds; | |
1451 | else { | |
1452 | if (1 < (pc.times_seen | pc.dates_seen | pc.days_seen | |
1453 | | pc.dsts_seen | |
1454 | | (pc.local_zones_seen + pc.zones_seen))) { | |
1455 | goto fail; | |
1456 | } | |
1457 | ||
1458 | tm.tm_year = to_year (pc.year) - TM_YEAR_BASE; | |
1459 | tm.tm_mon = pc.month - 1; | |
1460 | tm.tm_mday = pc.day; | |
1461 | if (pc.times_seen || (pc.rels_seen && | |
1462 | ! pc.dates_seen && ! pc.days_seen)) { | |
1463 | tm.tm_hour = to_hour (pc.hour, pc.meridian); | |
1464 | if (tm.tm_hour < 0) { | |
1465 | goto fail; | |
1466 | } | |
1467 | tm.tm_min = pc.minutes; | |
1468 | tm.tm_sec = pc.seconds.tv_sec; | |
1469 | } else { | |
1470 | tm.tm_hour = tm.tm_min = tm.tm_sec = 0; | |
1471 | pc.seconds.tv_nsec = 0; | |
1472 | } | |
1473 | ||
1474 | /** | |
1475 | * Let mktime deduce tm_isdst if we have an absolute timestamp. | |
1476 | */ | |
1477 | if (pc.dates_seen | pc.days_seen | pc.times_seen) | |
1478 | tm.tm_isdst = -1; | |
1479 | ||
1480 | /** | |
1481 | * But if the input explicitly specifies local time with or | |
1482 | * without DST, give mktime that information. | |
1483 | */ | |
1484 | if (pc.local_zones_seen) | |
1485 | tm.tm_isdst = pc.local_isdst; | |
1486 | ||
1487 | tm0 = tm; | |
1488 | ||
1489 | Start = mktime (&tm); | |
1490 | ||
1491 | if (! mktime_ok (&tm0, &tm, Start)) { | |
1492 | if (! pc.zones_seen) { | |
1493 | goto fail; | |
1494 | } else { | |
1495 | /** Guard against falsely reporting errors near | |
1496 | * the time_t boundaries when parsing times in | |
1497 | * other time zones. For example, suppose the | |
1498 | * input string "1969-12-31 23:00:00 -0100", the | |
1499 | * current time zone is 8 hours ahead of UTC, | |
1500 | * and the min time_t value is 1970-01-01 | |
1501 | * 00:00:00 UTC. Then the min localtime value | |
1502 | * is 1970-01-01 08:00:00, and mktime will | |
1503 | * therefore fail on 1969-12-31 23:00:00. To | |
1504 | * work around the problem, set the time zone to | |
1505 | * 1 hour behind UTC temporarily by setting | |
1506 | * TZ="XXX1:00" and try mktime again. | |
1507 | */ | |
1508 | ||
1509 | long int time_zone = pc.time_zone; | |
1510 | ||
1511 | long int abs_time_zone = time_zone < 0 | |
1512 | ? - time_zone : time_zone; | |
1513 | ||
1514 | long int abs_time_zone_hour | |
1515 | = abs_time_zone / 60; | |
1516 | ||
1517 | int abs_time_zone_min = abs_time_zone % 60; | |
1518 | ||
1519 | char tz1buf[sizeof "XXX+0:00" | |
1520 | + sizeof pc.time_zone | |
1521 | * CHAR_BIT / 3]; | |
1522 | ||
1523 | if (!tz_was_altered) | |
1524 | tz0 = get_tz (tz0buf); | |
1525 | sprintf (tz1buf, "XXX%s%ld:%02d", | |
1526 | &"-"[time_zone < 0], | |
1527 | abs_time_zone_hour, | |
1528 | abs_time_zone_min); | |
1529 | if (setenv ("TZ", tz1buf, 1) != 0) { | |
1530 | goto fail; | |
1531 | } | |
1532 | tz_was_altered = 1; | |
1533 | tm = tm0; | |
1534 | Start = mktime (&tm); | |
1535 | if (! mktime_ok (&tm0, &tm, Start)) { | |
1536 | goto fail; | |
1537 | } | |
1538 | } | |
1539 | } | |
1540 | ||
1541 | if (pc.days_seen && ! pc.dates_seen) { | |
1542 | tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7 + 7 | |
1543 | * (pc.day_ordinal | |
1544 | - (0 < pc.day_ordinal | |
1545 | && tm.tm_wday != pc.day_number))); | |
1546 | tm.tm_isdst = -1; | |
1547 | Start = mktime (&tm); | |
1548 | if (Start == (time_t) -1) { | |
1549 | goto fail; | |
1550 | } | |
1551 | } | |
1552 | /* Add relative date. */ | |
1553 | if (pc.rel.year | pc.rel.month | pc.rel.day) { | |
1554 | int year = tm.tm_year + pc.rel.year; | |
1555 | int month = tm.tm_mon + pc.rel.month; | |
1556 | int day = tm.tm_mday + pc.rel.day; | |
1557 | if (((year < tm.tm_year) ^ (pc.rel.year < 0)) | |
1558 | | ((month < tm.tm_mon) ^ (pc.rel.month < 0)) | |
1559 | | ((day < tm.tm_mday) ^ (pc.rel.day < 0))) { | |
1560 | goto fail; | |
1561 | } | |
1562 | tm.tm_year = year; | |
1563 | tm.tm_mon = month; | |
1564 | tm.tm_mday = day; | |
1565 | tm.tm_hour = tm0.tm_hour; | |
1566 | tm.tm_min = tm0.tm_min; | |
1567 | tm.tm_sec = tm0.tm_sec; | |
1568 | tm.tm_isdst = tm0.tm_isdst; | |
1569 | Start = mktime (&tm); | |
1570 | if (Start == (time_t) -1) { | |
1571 | goto fail; | |
1572 | } | |
1573 | } | |
1574 | ||
1575 | /** | |
1576 | * The only "output" of this if-block is an updated Start value, | |
1577 | * so this block must follow others that clobber Start. | |
1578 | */ | |
1579 | if (pc.zones_seen) { | |
1580 | long int delta = pc.time_zone * 60; | |
1581 | time_t t1; | |
1582 | #ifdef HAVE_TM_GMTOFF | |
1583 | delta -= tm.tm_gmtoff; | |
1584 | #else | |
1585 | time_t t = Start; | |
1586 | struct tm const *gmt = gmtime (&t); | |
1587 | if (! gmt) { | |
1588 | goto fail; | |
1589 | } | |
1590 | delta -= tm_diff (&tm, gmt); | |
1591 | #endif | |
1592 | t1 = Start - delta; | |
1593 | if ((Start < t1) != (delta < 0)) { | |
1594 | goto fail; /* time_t overflow */ | |
1595 | } | |
1596 | Start = t1; | |
1597 | } | |
1598 | ||
1599 | /** | |
1600 | * Add relative hours, minutes, and seconds. On hosts that | |
1601 | * support leap seconds, ignore the possibility of leap seconds; | |
1602 | * e.g., "+ 10 minutes" adds 600 seconds, even if one of them is | |
1603 | * a leap second. Typically this is not what the user wants, | |
1604 | * but it's too hard to do it the other way, because the time | |
1605 | * zone indicator must be applied before relative times, and if | |
1606 | * mktime is applied again the time zone will be lost. | |
1607 | */ | |
1608 | long int sum_ns = pc.seconds.tv_nsec + pc.rel.ns; | |
1609 | long int normalized_ns = (sum_ns % BILLION + BILLION) % BILLION; | |
1610 | time_t t0 = Start; | |
1611 | long int d1 = 60 * 60 * pc.rel.hour; | |
1612 | time_t t1 = t0 + d1; | |
1613 | long int d2 = 60 * pc.rel.minutes; | |
1614 | time_t t2 = t1 + d2; | |
1615 | time_t d3 = pc.rel.seconds; | |
1616 | time_t t3 = t2 + d3; | |
1617 | long int d4 = (sum_ns - normalized_ns) / BILLION; | |
1618 | time_t t4 = t3 + d4; | |
1619 | time_t t5 = t4; | |
1620 | ||
1621 | if ((d1 / (60 * 60) ^ pc.rel.hour) | |
1622 | | (d2 / 60 ^ pc.rel.minutes) | |
1623 | | ((t1 < t0) ^ (d1 < 0)) | |
1624 | | ((t2 < t1) ^ (d2 < 0)) | |
1625 | | ((t3 < t2) ^ (d3 < 0)) | |
1626 | | ((t4 < t3) ^ (d4 < 0)) | |
1627 | | (t5 != t4)) { | |
1628 | goto fail; | |
1629 | } | |
1630 | result->tv_sec = t5; | |
1631 | result->tv_nsec = normalized_ns; | |
1632 | } | |
1633 | ||
1634 | goto done; | |
1635 | ||
1636 | fail: | |
1637 | ok = 0; | |
1638 | done: | |
1639 | if (tz_was_altered) | |
1640 | ok &= (tz0 ? setenv ("TZ", tz0, 1) | |
1641 | : unsetenv ("TZ")) == 0; | |
1642 | if (tz0 != tz0buf) | |
1643 | free (tz0); | |
1644 | return ok; | |
1645 | } |