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