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