]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/parse-date.y
66ec722ce4a6d9a10a6eda3fef55ac663961f215
[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 #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 /**
878 * Convert a time zone expressed as HH:MM into an integer count of
879 * minutes. If MM is negative, then S is of the form HHMM and needs
880 * to be picked apart; otherwise, S is of the form HH. As specified in
881 * http://www.opengroup.org/susv3xbd/xbd_chap08.html#tag_08_03, allow
882 * only valid TZ range, and consider first two digits as hours, if no
883 * minutes specified.
884 */
885
886 static long int time_zone_hhmm(parser_control *pc, textint s, long int mm)
887 {
888 long int n_minutes;
889
890 /**
891 * If the length of S is 1 or 2 and no minutes are specified,
892 * interpret it as a number of hours.
893 */
894 if (s.digits <= 2 && mm < 0)
895 s.value *= 100;
896
897 if (mm < 0)
898 n_minutes = (s.value / 100) * 60 + s.value % 100;
899 else
900 n_minutes = s.value * 60 + (s.negative ? -mm : mm);
901
902 /**
903 * If the absolute number of minutes is larger than 24 hours,
904 * arrange to reject it by incrementing pc->zones_seen. Thus,
905 * we allow only values in the range UTC-24:00 to UTC+24:00.
906 */
907 if (24 * 60 < abs (n_minutes))
908 pc->zones_seen++;
909
910 return n_minutes;
911 }
912
913 static int to_hour(long int hours, int meridian)
914 {
915 switch (meridian) {
916 default: /* Pacify GCC. */
917 case MER24:
918 return 0 <= hours && hours < 24 ? hours : -1;
919 case MERam:
920 return 0 < hours && hours < 12 ? hours : hours == 12 ? 0 : -1;
921 case MERpm:
922 return 0 < hours && hours < 12 ? hours + 12 : hours == 12 ? 12 : -1;
923 }
924 }
925
926 static long int to_year(textint textyear)
927 {
928 long int year = textyear.value;
929
930 if (year < 0)
931 year = -year;
932
933 /**
934 * XPG4 suggests that years 00-68 map to 2000-2068, and
935 * years 69-99 map to 1969-1999.
936 */
937 else if (textyear.digits == 2)
938 year += year < 69 ? 2000 : 1900;
939
940 return year;
941 }
942
943 static table const * lookup_zone(parser_control const *pc, char const *name)
944 {
945 table const *tp;
946
947 for (tp = universal_time_zone_table; tp->name; tp++)
948 if (strcmp (name, tp->name) == 0)
949 return tp;
950
951 /**
952 * Try local zone abbreviations before those in time_zone_table, as
953 * the local ones are more likely to be right.
954 */
955 for (tp = pc->local_time_zone_table; tp->name; tp++)
956 if (strcmp (name, tp->name) == 0)
957 return tp;
958
959 for (tp = time_zone_table; tp->name; tp++)
960 if (strcmp (name, tp->name) == 0)
961 return tp;
962
963 return NULL;
964 }
965
966 #if ! HAVE_TM_GMTOFF
967 /**
968 * Yield the difference between *A and *B,
969 * measured in seconds, ignoring leap seconds.
970 * The body of this function is taken directly from the GNU C Library;
971 * see src/strftime.c.
972 */
973 static long int tm_diff(struct tm const *a, struct tm const *b)
974 {
975 /**
976 * Compute intervening leap days correctly even if year is negative.
977 * Take care to avoid int overflow in leap day calculations.
978 */
979 int a4 = SHR (a->tm_year, 2) + SHR (TM_YEAR_BASE, 2) - ! (a->tm_year & 3);
980 int b4 = SHR (b->tm_year, 2) + SHR (TM_YEAR_BASE, 2) - ! (b->tm_year & 3);
981 int a100 = a4 / 25 - (a4 % 25 < 0);
982 int b100 = b4 / 25 - (b4 % 25 < 0);
983 int a400 = SHR (a100, 2);
984 int b400 = SHR (b100, 2);
985 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
986 long int ayear = a->tm_year;
987 long int years = ayear - b->tm_year;
988 long int days = (365 * years + intervening_leap_days
989 + (a->tm_yday - b->tm_yday));
990 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
991 + (a->tm_min - b->tm_min))
992 + (a->tm_sec - b->tm_sec));
993 }
994 #endif /* ! HAVE_TM_GMTOFF */
995
996 static table const * lookup_word(parser_control const *pc, char *word)
997 {
998 char *p;
999 char *q;
1000 size_t wordlen;
1001 table const *tp;
1002 int period_found;
1003 int abbrev;
1004
1005 /* Make it uppercase. */
1006 for (p = word; *p; p++) {
1007 unsigned char ch = *p;
1008 *p = c_toupper (ch);
1009 }
1010
1011 for (tp = meridian_table; tp->name; tp++)
1012 if (strcmp (word, tp->name) == 0)
1013 return tp;
1014
1015 /* See if we have an abbreviation for a month. */
1016 wordlen = strlen (word);
1017 abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.');
1018
1019 for (tp = month_and_day_table; tp->name; tp++)
1020 if ((abbrev ? strncmp (word, tp->name, 3) :
1021 strcmp (word, tp->name)) == 0)
1022 return tp;
1023
1024 if ((tp = lookup_zone (pc, word)))
1025 return tp;
1026
1027 if (strcmp (word, dst_table[0].name) == 0)
1028 return dst_table;
1029
1030 for (tp = time_units_table; tp->name; tp++)
1031 if (strcmp (word, tp->name) == 0)
1032 return tp;
1033
1034 /* Strip off any plural and try the units table again. */
1035 if (word[wordlen - 1] == 'S') {
1036 word[wordlen - 1] = '\0';
1037 for (tp = time_units_table; tp->name; tp++)
1038 if (strcmp (word, tp->name) == 0)
1039 return tp;
1040 word[wordlen - 1] = 'S'; /* For "this" in relative_time_table. */
1041 }
1042
1043 for (tp = relative_time_table; tp->name; tp++)
1044 if (strcmp (word, tp->name) == 0)
1045 return tp;
1046
1047 /* Military time zones. */
1048 if (wordlen == 1)
1049 for (tp = military_table; tp->name; tp++)
1050 if (word[0] == tp->name[0])
1051 return tp;
1052
1053 /* Drop out any periods and try the time zone table again. */
1054 for (period_found = 0, p = q = word; (*p = *q); q++)
1055 if (*q == '.')
1056 period_found = 1;
1057 else
1058 p++;
1059 if (period_found && (tp = lookup_zone (pc, word)))
1060 return tp;
1061
1062 return NULL;
1063 }
1064
1065 static int yylex (union YYSTYPE *lvalp, parser_control *pc)
1066 {
1067 unsigned char c;
1068 size_t count;
1069
1070 for (;;) {
1071 while (c = *pc->input, c_isspace (c))
1072 pc->input++;
1073
1074 if (ISDIGIT (c) || c == '-' || c == '+') {
1075 char const *p;
1076 int sign;
1077 unsigned long int value;
1078 if (c == '-' || c == '+') {
1079 sign = c == '-' ? -1 : 1;
1080 while (c = *++pc->input, c_isspace (c))
1081 continue;
1082 if (! ISDIGIT (c))
1083 /* skip the '-' sign */
1084 continue;
1085 } else
1086 sign = 0;
1087 p = pc->input;
1088 for (value = 0; ; value *= 10) {
1089 unsigned long int value1 = value + (c - '0');
1090 if (value1 < value)
1091 return '?';
1092 value = value1;
1093 c = *++p;
1094 if (! ISDIGIT (c))
1095 break;
1096 if (ULONG_MAX / 10 < value)
1097 return '?';
1098 }
1099 if ((c == '.' || c == ',') && ISDIGIT (p[1])) {
1100 time_t s;
1101 int ns;
1102 int digits;
1103 unsigned long int value1;
1104
1105 /* Check for overflow when converting value to
1106 * time_t.
1107 */
1108 if (sign < 0) {
1109 s = - value;
1110 if (0 < s)
1111 return '?';
1112 value1 = -s;
1113 } else {
1114 s = value;
1115 if (s < 0)
1116 return '?';
1117 value1 = s;
1118 }
1119 if (value != value1)
1120 return '?';
1121
1122 /* Accumulate fraction, to ns precision. */
1123 p++;
1124 ns = *p++ - '0';
1125 for (digits = 2;
1126 digits <= LOG10_BILLION; digits++) {
1127 ns *= 10;
1128 if (ISDIGIT (*p))
1129 ns += *p++ - '0';
1130 }
1131
1132 /* Skip excess digits, truncating toward
1133 * -Infinity.
1134 */
1135 if (sign < 0)
1136 for (; ISDIGIT (*p); p++)
1137 if (*p != '0') {
1138 ns++;
1139 break;
1140 }
1141 while (ISDIGIT (*p))
1142 p++;
1143
1144 /* Adjust to the timespec convention, which is
1145 * that tv_nsec is always a positive offset even
1146 * if tv_sec is negative.
1147 */
1148 if (sign < 0 && ns) {
1149 s--;
1150 if (! (s < 0))
1151 return '?';
1152 ns = BILLION - ns;
1153 }
1154
1155 lvalp->timespec.tv_sec = s;
1156 lvalp->timespec.tv_nsec = ns;
1157 pc->input = p;
1158 return
1159 sign ? tSDECIMAL_NUMBER : tUDECIMAL_NUMBER;
1160 } else {
1161 lvalp->textintval.negative = sign < 0;
1162 if (sign < 0) {
1163 lvalp->textintval.value = - value;
1164 if (0 < lvalp->textintval.value)
1165 return '?';
1166 } else {
1167 lvalp->textintval.value = value;
1168 if (lvalp->textintval.value < 0)
1169 return '?';
1170 }
1171 lvalp->textintval.digits = p - pc->input;
1172 pc->input = p;
1173 return sign ? tSNUMBER : tUNUMBER;
1174 }
1175 }
1176
1177 if (c_isalpha (c)) {
1178 char buff[20];
1179 char *p = buff;
1180 table const *tp;
1181
1182 do {
1183 if (p < buff + sizeof buff - 1)
1184 *p++ = c;
1185 c = *++pc->input;
1186 }
1187 while (c_isalpha (c) || c == '.');
1188
1189 *p = '\0';
1190 tp = lookup_word (pc, buff);
1191 if (! tp) {
1192 return '?';
1193 }
1194 lvalp->intval = tp->value;
1195 return tp->type;
1196 }
1197
1198 if (c != '(')
1199 return to_uchar (*pc->input++);
1200
1201 count = 0;
1202 do {
1203 c = *pc->input++;
1204 if (c == '\0')
1205 return c;
1206 if (c == '(')
1207 count++;
1208 else if (c == ')')
1209 count--;
1210 }
1211 while (count != 0);
1212 }
1213 }
1214
1215 /* Do nothing if the parser reports an error. */
1216 static int yyerror(parser_control const *pc __attribute__((__unused__)),
1217 char const *s __attribute__((__unused__)))
1218 {
1219 return 0;
1220 }
1221
1222 /**
1223 * If *TM0 is the old and *TM1 is the new value of a struct tm after
1224 * passing it to mktime, return 1 if it's OK that mktime returned T.
1225 * It's not OK if *TM0 has out-of-range members.
1226 */
1227
1228 static int mktime_ok(struct tm const *tm0, struct tm const *tm1, time_t t)
1229 {
1230 if (t == (time_t) -1) {
1231 /**
1232 * Guard against falsely reporting an error when parsing a
1233 * timestamp that happens to equal (time_t) -1, on a host that
1234 * supports such a timestamp.
1235 */
1236 tm1 = localtime (&t);
1237 if (!tm1)
1238 return 0;
1239 }
1240
1241 return ! ((tm0->tm_sec ^ tm1->tm_sec)
1242 | (tm0->tm_min ^ tm1->tm_min)
1243 | (tm0->tm_hour ^ tm1->tm_hour)
1244 | (tm0->tm_mday ^ tm1->tm_mday)
1245 | (tm0->tm_mon ^ tm1->tm_mon)
1246 | (tm0->tm_year ^ tm1->tm_year));
1247 }
1248
1249 /**
1250 * A reasonable upper bound for the size of ordinary TZ strings.
1251 * Use heap allocation if TZ's length exceeds this.
1252 */
1253 enum { TZBUFSIZE = 100 };
1254
1255 /**
1256 * Return a copy of TZ, stored in TZBUF if it fits, and heap-allocated
1257 * otherwise.
1258 */
1259 static char * get_tz(char tzbuf[TZBUFSIZE])
1260 {
1261 char *tz = getenv ("TZ");
1262 if (tz) {
1263 size_t tzsize = strlen (tz) + 1;
1264 tz = (tzsize <= TZBUFSIZE
1265 ? memcpy (tzbuf, tz, tzsize)
1266 : xstrdup (tz));
1267 }
1268 return tz;
1269 }
1270
1271 /**
1272 * Parse a date/time string, storing the resulting time value into *result.
1273 * The string itself is pointed to by *p. Return 1 if successful.
1274 * *p can be an incomplete or relative time specification; if so, use
1275 * *now as the basis for the returned time.
1276 */
1277 int parse_date(struct timespec *result, char const *p,
1278 struct timespec const *now)
1279 {
1280 time_t Start;
1281 long int Start_ns;
1282 struct tm const *tmp;
1283 struct tm tm;
1284 struct tm tm0;
1285 parser_control pc;
1286 struct timespec gettime_buffer;
1287 unsigned char c;
1288 int tz_was_altered = 0;
1289 char *tz0 = NULL;
1290 char tz0buf[TZBUFSIZE];
1291 int ok = 1;
1292 struct timeval tv;
1293
1294 if (! now) {
1295 gettimeofday (&tv, NULL);
1296 gettime_buffer.tv_sec = tv.tv_sec;
1297 gettime_buffer.tv_nsec = tv.tv_usec * 1000;
1298 now = &gettime_buffer;
1299 }
1300
1301 Start = now->tv_sec;
1302 Start_ns = now->tv_nsec;
1303
1304 tmp = localtime (&now->tv_sec);
1305 if (! tmp)
1306 return 0;
1307
1308 while (c = *p, c_isspace (c))
1309 p++;
1310
1311 if (strncmp (p, "TZ=\"", 4) == 0) {
1312 char const *tzbase = p + 4;
1313 size_t tzsize = 1;
1314 char const *s;
1315
1316 for (s = tzbase; *s; s++, tzsize++)
1317 if (*s == '\\') {
1318 s++;
1319 if (! (*s == '\\' || *s == '"'))
1320 break;
1321 } else if (*s == '"') {
1322 char *z;
1323 char *tz1;
1324 char tz1buf[TZBUFSIZE];
1325 int large_tz = TZBUFSIZE < tzsize;
1326 int setenv_ok;
1327 tz0 = get_tz (tz0buf);
1328 z = tz1 = large_tz ? malloc (tzsize) : tz1buf;
1329 for (s = tzbase; *s != '"'; s++)
1330 *z++ = *(s += *s == '\\');
1331 *z = '\0';
1332 setenv_ok = setenv ("TZ", tz1, 1) == 0;
1333 if (large_tz)
1334 free (tz1);
1335 if (!setenv_ok)
1336 goto fail;
1337 tz_was_altered = 1;
1338
1339 p = s + 1;
1340 while (c = *p, c_isspace (c))
1341 p++;
1342
1343 break;
1344 }
1345 }
1346
1347 /**
1348 * As documented, be careful to treat the empty string just like
1349 * a date string of "0". Without this, an empty string would be
1350 * declared invalid when parsed during a DST transition.
1351 */
1352 if (*p == '\0')
1353 p = "0";
1354
1355 pc.input = p;
1356 pc.year.value = tmp->tm_year;
1357 pc.year.value += TM_YEAR_BASE;
1358 pc.year.digits = 0;
1359 pc.month = tmp->tm_mon + 1;
1360 pc.day = tmp->tm_mday;
1361 pc.hour = tmp->tm_hour;
1362 pc.minutes = tmp->tm_min;
1363 pc.seconds.tv_sec = tmp->tm_sec;
1364 pc.seconds.tv_nsec = Start_ns;
1365 tm.tm_isdst = tmp->tm_isdst;
1366
1367 pc.meridian = MER24;
1368 pc.rel = RELATIVE_TIME_0;
1369 pc.timespec_seen = 0;
1370 pc.rels_seen = 0;
1371 pc.dates_seen = 0;
1372 pc.days_seen = 0;
1373 pc.times_seen = 0;
1374 pc.local_zones_seen = 0;
1375 pc.dsts_seen = 0;
1376 pc.zones_seen = 0;
1377 pc.year_seen = 0;
1378 pc.ordinal_day_seen = 0;
1379
1380 #if HAVE_STRUCT_TM_TM_ZONE
1381 pc.local_time_zone_table[0].name = tmp->tm_zone;
1382 pc.local_time_zone_table[0].type = tLOCAL_ZONE;
1383 pc.local_time_zone_table[0].value = tmp->tm_isdst;
1384 pc.local_time_zone_table[1].name = NULL;
1385
1386 /**
1387 * Probe the names used in the next three calendar quarters, looking
1388 * for a tm_isdst different from the one we already have.
1389 */
1390 {
1391 int quarter;
1392 for (quarter = 1; quarter <= 3; quarter++) {
1393 time_t probe = Start + quarter * (90 * 24 * 60 * 60);
1394 struct tm const *probe_tm = localtime (&probe);
1395 if (probe_tm && probe_tm->tm_zone
1396 && probe_tm->tm_isdst
1397 != pc.local_time_zone_table[0].value) {
1398 {
1399 pc.local_time_zone_table[1].name
1400 = probe_tm->tm_zone;
1401 pc.local_time_zone_table[1].type
1402 = tLOCAL_ZONE;
1403 pc.local_time_zone_table[1].value
1404 = probe_tm->tm_isdst;
1405 pc.local_time_zone_table[2].name
1406 = NULL;
1407 }
1408 break;
1409 }
1410 }
1411 }
1412 #else
1413 #if HAVE_TZNAME
1414 {
1415 # if !HAVE_DECL_TZNAME
1416 extern char *tzname[];
1417 # endif
1418 int i;
1419 for (i = 0; i < 2; i++) {
1420 pc.local_time_zone_table[i].name = tzname[i];
1421 pc.local_time_zone_table[i].type = tLOCAL_ZONE;
1422 pc.local_time_zone_table[i].value = i;
1423 }
1424 pc.local_time_zone_table[i].name = NULL;
1425 }
1426 #else
1427 pc.local_time_zone_table[0].name = NULL;
1428 #endif
1429 #endif
1430
1431 if (pc.local_time_zone_table[0].name && pc.local_time_zone_table[1].name
1432 && ! strcmp (pc.local_time_zone_table[0].name,
1433 pc.local_time_zone_table[1].name)) {
1434 /**
1435 * This locale uses the same abbreviation for standard and
1436 * daylight times. So if we see that abbreviation, we don't
1437 * know whether it's daylight time.
1438 */
1439 pc.local_time_zone_table[0].value = -1;
1440 pc.local_time_zone_table[1].name = NULL;
1441 }
1442
1443 if (yyparse (&pc) != 0) {
1444 goto fail;
1445 }
1446
1447 if (pc.timespec_seen)
1448 *result = pc.seconds;
1449 else {
1450 if (1 < (pc.times_seen | pc.dates_seen | pc.days_seen
1451 | pc.dsts_seen
1452 | (pc.local_zones_seen + pc.zones_seen))) {
1453 goto fail;
1454 }
1455
1456 tm.tm_year = to_year (pc.year) - TM_YEAR_BASE;
1457 tm.tm_mon = pc.month - 1;
1458 tm.tm_mday = pc.day;
1459 if (pc.times_seen || (pc.rels_seen &&
1460 ! pc.dates_seen && ! pc.days_seen)) {
1461 tm.tm_hour = to_hour (pc.hour, pc.meridian);
1462 if (tm.tm_hour < 0) {
1463 goto fail;
1464 }
1465 tm.tm_min = pc.minutes;
1466 tm.tm_sec = pc.seconds.tv_sec;
1467 } else {
1468 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
1469 pc.seconds.tv_nsec = 0;
1470 }
1471
1472 /**
1473 * Let mktime deduce tm_isdst if we have an absolute timestamp.
1474 */
1475 if (pc.dates_seen | pc.days_seen | pc.times_seen)
1476 tm.tm_isdst = -1;
1477
1478 /**
1479 * But if the input explicitly specifies local time with or
1480 * without DST, give mktime that information.
1481 */
1482 if (pc.local_zones_seen)
1483 tm.tm_isdst = pc.local_isdst;
1484
1485 tm0 = tm;
1486
1487 Start = mktime (&tm);
1488
1489 if (! mktime_ok (&tm0, &tm, Start)) {
1490 if (! pc.zones_seen) {
1491 goto fail;
1492 } else {
1493 /** Guard against falsely reporting errors near
1494 * the time_t boundaries when parsing times in
1495 * other time zones. For example, suppose the
1496 * input string "1969-12-31 23:00:00 -0100", the
1497 * current time zone is 8 hours ahead of UTC,
1498 * and the min time_t value is 1970-01-01
1499 * 00:00:00 UTC. Then the min localtime value
1500 * is 1970-01-01 08:00:00, and mktime will
1501 * therefore fail on 1969-12-31 23:00:00. To
1502 * work around the problem, set the time zone to
1503 * 1 hour behind UTC temporarily by setting
1504 * TZ="XXX1:00" and try mktime again.
1505 */
1506
1507 long int time_zone = pc.time_zone;
1508
1509 long int abs_time_zone = time_zone < 0
1510 ? - time_zone : time_zone;
1511
1512 long int abs_time_zone_hour
1513 = abs_time_zone / 60;
1514
1515 int abs_time_zone_min = abs_time_zone % 60;
1516
1517 char tz1buf[sizeof "XXX+0:00"
1518 + sizeof pc.time_zone
1519 * CHAR_BIT / 3];
1520
1521 if (!tz_was_altered)
1522 tz0 = get_tz (tz0buf);
1523 sprintf (tz1buf, "XXX%s%ld:%02d",
1524 &"-"[time_zone < 0],
1525 abs_time_zone_hour,
1526 abs_time_zone_min);
1527 if (setenv ("TZ", tz1buf, 1) != 0) {
1528 goto fail;
1529 }
1530 tz_was_altered = 1;
1531 tm = tm0;
1532 Start = mktime (&tm);
1533 if (! mktime_ok (&tm0, &tm, Start)) {
1534 goto fail;
1535 }
1536 }
1537 }
1538
1539 if (pc.days_seen && ! pc.dates_seen) {
1540 tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7 + 7
1541 * (pc.day_ordinal
1542 - (0 < pc.day_ordinal
1543 && tm.tm_wday != pc.day_number)));
1544 tm.tm_isdst = -1;
1545 Start = mktime (&tm);
1546 if (Start == (time_t) -1) {
1547 goto fail;
1548 }
1549 }
1550 /* Add relative date. */
1551 if (pc.rel.year | pc.rel.month | pc.rel.day) {
1552 int year = tm.tm_year + pc.rel.year;
1553 int month = tm.tm_mon + pc.rel.month;
1554 int day = tm.tm_mday + pc.rel.day;
1555 if (((year < tm.tm_year) ^ (pc.rel.year < 0))
1556 | ((month < tm.tm_mon) ^ (pc.rel.month < 0))
1557 | ((day < tm.tm_mday) ^ (pc.rel.day < 0))) {
1558 goto fail;
1559 }
1560 tm.tm_year = year;
1561 tm.tm_mon = month;
1562 tm.tm_mday = day;
1563 tm.tm_hour = tm0.tm_hour;
1564 tm.tm_min = tm0.tm_min;
1565 tm.tm_sec = tm0.tm_sec;
1566 tm.tm_isdst = tm0.tm_isdst;
1567 Start = mktime (&tm);
1568 if (Start == (time_t) -1) {
1569 goto fail;
1570 }
1571 }
1572
1573 /**
1574 * The only "output" of this if-block is an updated Start value,
1575 * so this block must follow others that clobber Start.
1576 */
1577 if (pc.zones_seen) {
1578 long int delta = pc.time_zone * 60;
1579 time_t t1;
1580 #ifdef HAVE_TM_GMTOFF
1581 delta -= tm.tm_gmtoff;
1582 #else
1583 time_t t = Start;
1584 struct tm const *gmt = gmtime (&t);
1585 if (! gmt) {
1586 goto fail;
1587 }
1588 delta -= tm_diff (&tm, gmt);
1589 #endif
1590 t1 = Start - delta;
1591 if ((Start < t1) != (delta < 0)) {
1592 goto fail; /* time_t overflow */
1593 }
1594 Start = t1;
1595 }
1596
1597 /**
1598 * Add relative hours, minutes, and seconds. On hosts that
1599 * support leap seconds, ignore the possibility of leap seconds;
1600 * e.g., "+ 10 minutes" adds 600 seconds, even if one of them is
1601 * a leap second. Typically this is not what the user wants,
1602 * but it's too hard to do it the other way, because the time
1603 * zone indicator must be applied before relative times, and if
1604 * mktime is applied again the time zone will be lost.
1605 */
1606 long int sum_ns = pc.seconds.tv_nsec + pc.rel.ns;
1607 long int normalized_ns = (sum_ns % BILLION + BILLION) % BILLION;
1608 time_t t0 = Start;
1609 long int d1 = 60 * 60 * pc.rel.hour;
1610 time_t t1 = t0 + d1;
1611 long int d2 = 60 * pc.rel.minutes;
1612 time_t t2 = t1 + d2;
1613 time_t d3 = pc.rel.seconds;
1614 time_t t3 = t2 + d3;
1615 long int d4 = (sum_ns - normalized_ns) / BILLION;
1616 time_t t4 = t3 + d4;
1617 time_t t5 = t4;
1618
1619 if ((d1 / (60 * 60) ^ pc.rel.hour)
1620 | (d2 / 60 ^ pc.rel.minutes)
1621 | ((t1 < t0) ^ (d1 < 0))
1622 | ((t2 < t1) ^ (d2 < 0))
1623 | ((t3 < t2) ^ (d3 < 0))
1624 | ((t4 < t3) ^ (d4 < 0))
1625 | (t5 != t4)) {
1626 goto fail;
1627 }
1628 result->tv_sec = t5;
1629 result->tv_nsec = normalized_ns;
1630 }
1631
1632 goto done;
1633
1634 fail:
1635 ok = 0;
1636 done:
1637 if (tz_was_altered)
1638 ok &= (tz0 ? setenv ("TZ", tz0, 1)
1639 : unsetenv ("TZ")) == 0;
1640 if (tz0 != tz0buf)
1641 free (tz0);
1642 return ok;
1643 }