3 * Parse a string into an internal timestamp.
5 * This file is based on gnulib parse-datetime.y-dd7a871 with
6 * the other gnulib dependencies removed for use in util-linux.
8 * Copyright (C) 1999-2000, 2002-2017 Free Software Foundation, Inc.
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.
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.
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/>.
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.
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.
36 * FIXME: Check for arithmetic overflow in all cases, not just
44 #include "timeutils.h"
47 * There's no need to extend the stack, so there's no need to involve
50 #define YYSTACK_USE_ALLOCA 0
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.
59 #define YYINITDEPTH YYMAXDEPTH
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.
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.
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.
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.
110 : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0))
112 #define TM_YEAR_BASE 1900
114 #define HOUR(x) ((x) * 60)
116 #define STREQ(a, b) (strcmp (a, b) == 0)
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.
123 static unsigned char to_uchar (char ch) { return ch; }
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.
131 * An integer value, and the number of digits in its textual
140 /* An entry in the lexical lookup table. */
147 /* Meridian: am, pm, or 24-hour style. */
148 enum { MERam, MERpm, MER24 };
150 enum { BILLION = 1000000000, LOG10_BILLION = 9 };
152 /* Relative year, month, day, hour, minutes, seconds, and nanoseconds. */
163 #if HAVE_COMPOUND_LITERALS
164 # define RELATIVE_TIME_0 ((relative_time) { 0, 0, 0, 0, 0, 0, 0 })
166 static relative_time const RELATIVE_TIME_0;
169 /* Information passed to and from the parser. */
171 /* The input string remaining to be parsed. */
174 /* N, if this is the Nth Tuesday. */
175 intmax_t day_ordinal;
177 /* Day of week; Sunday is 0. */
180 /* tm_isdst flag for the local zone. */
183 /* Time zone, in minutes east of UTC. */
186 /* Style used for time. */
189 /* Gregorian year, month, day, hour, minutes, seconds, and ns. */
195 struct timespec seconds; /* includes nanoseconds */
197 /* Relative year, month, day, hour, minutes, seconds, and ns. */
200 /* Presence or counts of some nonterminals parsed so far. */
205 size_t local_zones_seen;
210 /* Table of local time zone abbreviations, null terminated. */
211 table local_time_zone_table[3];
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);
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,
224 static void digits_to_date_time(parser_control *pc, textint text_int)
226 if (pc->dates_seen && ! pc->year.digits
227 && ! pc->rels_seen && (pc->times_seen || 2 < text_int.digits)) {
230 if (4 < text_int.digits) {
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;
238 if (text_int.digits <= 2) {
239 pc->hour = text_int.value;
243 pc->hour = text_int.value / 100;
244 pc->minutes = text_int.value % 100;
246 pc->seconds.tv_sec = 0;
247 pc->seconds.tv_nsec = 0;
248 pc->meridian = MER24;
253 /* Increment PC->rel by FACTOR * REL (FACTOR is 1 or -1). */
254 static void apply_relative_time(parser_control *pc, relative_time rel,
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;
267 /* Set PC-> hour, minutes, seconds and nanoseconds members from arguments. */
269 set_hhmmss(parser_control *pc, intmax_t hour, intmax_t minutes,
270 time_t sec, int nsec)
273 pc->minutes = minutes;
274 pc->seconds.tv_sec = sec;
275 pc->seconds.tv_nsec = nsec;
281 * We want a reentrant parser, even if the TZ manipulation and the calls to
282 * localtime and gmtime are not reentrant.
285 %parse-param { parser_control *pc }
286 %lex-param { parser_control *pc }
288 /* This grammar has 31 shift/reduce conflicts. */
294 struct timespec timespec;
301 %token tYEAR_UNIT tMONTH_UNIT tHOUR_UNIT tMINUTE_UNIT tSEC_UNIT
302 %token <intval> tDAY_UNIT tDAY_SHIFT
304 %token <intval> tDAY tDAYZONE tLOCAL_ZONE tMERIDIAN
305 %token <intval> tMONTH tORDINAL tZONE
307 %token <textintval> tSNUMBER tUNUMBER
308 %token <timespec> tSDECIMAL_NUMBER tUDECIMAL_NUMBER
310 %type <textintval> o_colon_minutes
311 %type <timespec> seconds signed_seconds unsigned_seconds
313 %type <rel> relunit relunit_snumber dayshift
325 pc->timespec_seen = 1;
336 pc->times_seen++; pc->dates_seen++;
342 pc->local_zones_seen++;
363 iso_8601_date 'T' iso_8601_time
368 set_hhmmss (pc, $1.value, 0, 0, 0);
371 | tUNUMBER ':' tUNUMBER tMERIDIAN {
372 set_hhmmss (pc, $1.value, $3.value, 0, 0);
375 | tUNUMBER ':' tUNUMBER ':' unsigned_seconds tMERIDIAN {
376 set_hhmmss (pc, $1.value, $3.value, $5.tv_sec, $5.tv_nsec);
383 tUNUMBER zone_offset {
384 set_hhmmss (pc, $1.value, 0, 0, 0);
385 pc->meridian = MER24;
387 | tUNUMBER ':' tUNUMBER o_zone_offset {
388 set_hhmmss (pc, $1.value, $3.value, 0, 0);
389 pc->meridian = MER24;
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;
403 tSNUMBER o_colon_minutes {
405 if (! time_zone_hhmm (pc, $1, $2)) YYABORT;
410 * Local zone strings only affect DST setting,
411 * and only take affect if the current TZ setting is relevant.
414 * 'EEST' is parsed as tLOCAL_ZONE, as it relates to the effective TZ:
415 * TZ=Europe/Helsinki date -d '2016-12-30 EEST'
418 * 'EEST' is parsed as 'zone' (TZ=+03:00):
419 * TZ=Asia/Tokyo ./src/date --debug -d '2011-06-11 EEST'
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').
429 pc->local_isdst = $1;
430 pc->dsts_seen += (0 < $1);
434 pc->dsts_seen += (0 < $1) + 1;
439 * Note 'T' is a special case, as it is used as the separator in ISO
440 * 8601 date and time of day representation.
447 pc->time_zone = HOUR(7);
449 | tZONE relunit_snumber {
451 apply_relative_time (pc, $2, 1);
453 | 'T' relunit_snumber {
454 pc->time_zone = HOUR(7);
455 apply_relative_time (pc, $2, 1);
457 | tZONE tSNUMBER o_colon_minutes {
458 if (! time_zone_hhmm (pc, $2, $3)) YYABORT;
462 pc->time_zone = $1 + 60;
465 pc->time_zone = $1 + 60;
479 pc->day_ordinal = $1;
483 pc->day_ordinal = $1.value;
489 tUNUMBER '/' tUNUMBER {
490 pc->month = $1.value;
493 | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
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.
501 if (4 <= $1.digits) {
503 pc->month = $3.value;
506 pc->month = $1.value;
511 | tUNUMBER tMONTH tSNUMBER {
512 /* e.g. 17-JUN-1992. */
515 pc->year.value = -$3.value;
516 pc->year.digits = $3.digits;
518 | tMONTH tSNUMBER tSNUMBER {
519 /* e.g. JUN-17-1992. */
522 pc->year.value = -$3.value;
523 pc->year.digits = $3.digits;
529 | tMONTH tUNUMBER ',' tUNUMBER {
538 | tUNUMBER tMONTH tUNUMBER {
547 tUNUMBER tSNUMBER tSNUMBER {
548 /* ISO 8601 format.YYYY-MM-DD. */
550 pc->month = -$2.value;
557 { apply_relative_time (pc, $1, $2); }
559 { apply_relative_time (pc, $1, 1); }
561 { apply_relative_time (pc, $1, 1); }
566 { $$ = RELATIVE_TIME_0; $$.year = $1; }
567 | tUNUMBER tYEAR_UNIT
568 { $$ = RELATIVE_TIME_0; $$.year = $1.value; }
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; }
576 { $$ = RELATIVE_TIME_0; $$.month = 1; }
578 { $$ = RELATIVE_TIME_0; $$.day = $1 * $2; }
580 { $$ = RELATIVE_TIME_0; $$.day = $1.value * $2; }
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; }
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; }
594 { $$ = RELATIVE_TIME_0; $$.minutes = 1; }
596 { $$ = RELATIVE_TIME_0; $$.seconds = $1; }
598 { $$ = RELATIVE_TIME_0; $$.seconds = $1.value; }
599 | tSDECIMAL_NUMBER tSEC_UNIT {
600 $$ = RELATIVE_TIME_0;
601 $$.seconds = $1.tv_sec;
604 | tUDECIMAL_NUMBER tSEC_UNIT {
605 $$ = RELATIVE_TIME_0;
606 $$.seconds = $1.tv_sec;
610 { $$ = RELATIVE_TIME_0; $$.seconds = 1; }
616 { $$ = RELATIVE_TIME_0; $$.year = $1.value; }
617 | tSNUMBER tMONTH_UNIT
618 { $$ = RELATIVE_TIME_0; $$.month = $1.value; }
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; }
626 { $$ = RELATIVE_TIME_0; $$.seconds = $1.value; }
631 { $$ = RELATIVE_TIME_0; $$.day = $1; }
634 seconds: signed_seconds | unsigned_seconds;
639 { $$.tv_sec = $1.value; $$.tv_nsec = 0; }
645 { $$.tv_sec = $1.value; $$.tv_nsec = 0; }
650 { digits_to_date_time (pc, $1); }
654 tUNUMBER relunit_snumber {
656 * Hybrid all-digit and relative offset, so that we accept e.g.,
657 * "YYYYMMDD +N days" as well as "YYYYMMDD N days".
659 digits_to_date_time (pc, $1);
660 apply_relative_time (pc, $2, 1);
666 { $$.value = $$.digits = 0; }
674 static table const meridian_table[] = {
675 { "AM", tMERIDIAN, MERam },
676 { "A.M.", tMERIDIAN, MERam },
677 { "PM", tMERIDIAN, MERpm },
678 { "P.M.", tMERIDIAN, MERpm },
682 static table const dst_table[] = {
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 },
704 { "WEDNESDAY",tDAY, 3 },
705 { "WEDNES", tDAY, 3 },
706 { "THURSDAY", tDAY, 4 },
708 { "THURS", tDAY, 4 },
709 { "FRIDAY", tDAY, 5 },
710 { "SATURDAY", tDAY, 6 },
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 },
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 },
750 { "HENCE", tAGO, 1 },
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.
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) },
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.
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 */
825 * Military time zone table.
827 * Note 'T' is a special case, as it is used as the separator in ISO
828 * 8601 date and time of day representation.
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) },
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) },
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).
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.
871 static int time_zone_hhmm(parser_control *pc, textint hh, textint mm)
875 if (hh.digits > 2 && hh.digits < 5 && mm.digits == 0) {
878 } else if (hh.digits < 3 && (mm.digits == 0 || mm.digits == 2)) {
880 m = hh.negative ? -mm.value : mm.value;
884 if (abs(h) > 24 || abs(m) > 59)
887 pc->time_zone = h * 60 + m;
891 static int to_hour(intmax_t hours, int meridian)
894 default: /* Pacify GCC. */
896 return 0 <= hours && hours < 24 ? hours : -1;
898 return 0 < hours && hours < 12 ? hours : hours == 12 ? 0 : -1;
900 return 0 < hours && hours < 12 ? hours + 12 : hours == 12 ? 12 : -1;
904 static long int to_year(textint textyear)
906 intmax_t year = textyear.value;
912 * XPG4 suggests that years 00-68 map to 2000-2068, and
913 * years 69-99 map to 1969-1999.
915 else if (textyear.digits == 2)
916 year += year < 69 ? 2000 : 1900;
921 static table const * lookup_zone(parser_control const *pc, char const *name)
925 for (tp = universal_time_zone_table; tp->name; tp++)
926 if (strcmp (name, tp->name) == 0)
930 * Try local zone abbreviations before those in time_zone_table, as
931 * the local ones are more likely to be right.
933 for (tp = pc->local_time_zone_table; tp->name; tp++)
934 if (strcmp (name, tp->name) == 0)
937 for (tp = time_zone_table; tp->name; tp++)
938 if (strcmp (name, tp->name) == 0)
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.
951 static int tm_diff(struct tm const *a, struct tm const *b)
954 * Compute intervening leap days correctly even if year is negative.
955 * Take care to avoid int overflow in leap day calculations.
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));
971 #endif /* ! HAVE_TM_GMTOFF */
973 static table const * lookup_word(parser_control const *pc, char *word)
982 /* Make it uppercase. */
983 for (p = word; *p; p++)
984 *p = c_toupper (to_uchar (*p));
986 for (tp = meridian_table; tp->name; tp++)
987 if (strcmp (word, tp->name) == 0)
990 /* See if we have an abbreviation for a month. */
991 wordlen = strlen (word);
992 abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.');
994 for (tp = month_and_day_table; tp->name; tp++)
995 if ((abbrev ? strncmp (word, tp->name, 3) :
996 strcmp (word, tp->name)) == 0)
999 if ((tp = lookup_zone (pc, word)))
1002 if (strcmp (word, dst_table[0].name) == 0)
1005 for (tp = time_units_table; tp->name; tp++)
1006 if (strcmp (word, tp->name) == 0)
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)
1015 word[wordlen - 1] = 'S'; /* For "this" in relative_time_table. */
1018 for (tp = relative_time_table; tp->name; tp++)
1019 if (strcmp (word, tp->name) == 0)
1022 /* Military time zones. */
1024 for (tp = military_table; tp->name; tp++)
1025 if (word[0] == tp->name[0])
1028 /* Drop out any periods and try the time zone table again. */
1029 for (period_found = 0, p = q = word; (*p = *q); q++)
1034 if (period_found && (tp = lookup_zone (pc, word)))
1040 static int yylex (union YYSTYPE *lvalp, parser_control *pc)
1046 while (c = *pc->input, c_isspace (c))
1049 if (c_isdigit (c) || c == '-' || c == '+') {
1053 if (c == '-' || c == '+') {
1054 sign = c == '-' ? -1 : 1;
1055 while (c = *++pc->input, c_isspace (c))
1057 if (! c_isdigit (c))
1058 /* skip the '-' sign */
1063 for (value = 0; ; value *= 10) {
1064 uintmax_t value1 = value + (c - '0');
1069 if (! c_isdigit (c))
1071 if (UINTMAX_MAX / 10 < value)
1074 if ((c == '.' || c == ',') && c_isdigit (p[1])) {
1080 /* Check for overflow when converting value to
1094 if (value != value1)
1097 /* Accumulate fraction, to ns precision. */
1101 digits <= LOG10_BILLION; digits++) {
1107 /* Skip excess digits, truncating toward
1111 for (; c_isdigit (*p); p++)
1116 while (c_isdigit (*p))
1119 /* Adjust to the timespec convention, which is
1120 * that tv_nsec is always a positive offset even
1121 * if tv_sec is negative.
1123 if (sign < 0 && ns) {
1130 lvalp->timespec.tv_sec = s;
1131 lvalp->timespec.tv_nsec = ns;
1134 sign ? tSDECIMAL_NUMBER : tUDECIMAL_NUMBER;
1136 lvalp->textintval.negative = sign < 0;
1138 lvalp->textintval.value = - value;
1139 if (0 < lvalp->textintval.value)
1142 lvalp->textintval.value = value;
1143 if (lvalp->textintval.value < 0)
1146 lvalp->textintval.digits = p - pc->input;
1148 return sign ? tSNUMBER : tUNUMBER;
1152 if (c_isalpha (c)) {
1158 if (p < buff + sizeof buff - 1)
1162 while (c_isalpha (c) || c == '.');
1165 tp = lookup_word (pc, buff);
1169 lvalp->intval = tp->value;
1174 return to_uchar (*pc->input++);
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__)))
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.
1203 static int mktime_ok(struct tm const *tm0, struct tm const *tm1, time_t t)
1205 if (t == (time_t) -1) {
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.
1211 tm1 = localtime (&t);
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));
1225 * A reasonable upper bound for the size of ordinary TZ strings.
1226 * Use heap allocation if TZ's length exceeds this.
1228 enum { TZBUFSIZE = 100 };
1231 * Return a copy of TZ, stored in TZBUF if it fits, and heap-allocated
1234 static char * get_tz(char tzbuf[TZBUFSIZE])
1236 char *tz = getenv ("TZ");
1238 size_t tzsize = strlen (tz) + 1;
1239 tz = (tzsize <= TZBUFSIZE
1240 ? memcpy (tzbuf, tz, tzsize)
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.
1252 int parse_date(struct timespec *result, char const *p,
1253 struct timespec const *now)
1257 struct tm const *tmp;
1261 struct timespec gettime_buffer;
1263 int tz_was_altered = 0;
1265 char tz0buf[TZBUFSIZE];
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;
1276 Start = now->tv_sec;
1277 Start_ns = now->tv_nsec;
1279 tmp = localtime (&now->tv_sec);
1283 while (c = *p, c_isspace (c))
1286 if (strncmp (p, "TZ=\"", 4) == 0) {
1287 char const *tzbase = p + 4;
1291 for (s = tzbase; *s; s++, tzsize++)
1294 if (! (*s == '\\' || *s == '"'))
1296 } else if (*s == '"') {
1299 char tz1buf[TZBUFSIZE];
1300 int large_tz = TZBUFSIZE < tzsize;
1303 tz0 = get_tz (tz0buf);
1308 z = tz1 = malloc (tzsize);
1314 for (s = tzbase; *s != '"'; s++)
1315 *z++ = *(s += *s == '\\');
1317 setenv_ok = setenv ("TZ", tz1, 1) == 0;
1325 while (c = *p, c_isspace (c))
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.
1341 pc.year.value = tmp->tm_year;
1342 pc.year.value += TM_YEAR_BASE;
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;
1352 pc.meridian = MER24;
1353 pc.rel = RELATIVE_TIME_0;
1354 pc.timespec_seen = 0;
1359 pc.local_zones_seen = 0;
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;
1370 * Probe the names used in the next three calendar quarters, looking
1371 * for a tm_isdst different from the one we already have.
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) {
1382 pc.local_time_zone_table[1].name
1383 = probe_tm->tm_zone;
1384 pc.local_time_zone_table[1].type
1386 pc.local_time_zone_table[1].value
1387 = probe_tm->tm_isdst;
1388 pc.local_time_zone_table[2].name
1398 # if !HAVE_DECL_TZNAME
1399 extern char *tzname[];
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;
1407 pc.local_time_zone_table[i].name = NULL;
1410 pc.local_time_zone_table[0].name = NULL;
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)) {
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.
1422 pc.local_time_zone_table[0].value = -1;
1423 pc.local_time_zone_table[1].name = NULL;
1426 if (yyparse (&pc) != 0) {
1430 if (pc.timespec_seen)
1431 *result = pc.seconds;
1433 if (1 < (pc.times_seen | pc.dates_seen | pc.days_seen
1435 | (pc.local_zones_seen + pc.zones_seen))) {
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) {
1448 tm.tm_min = pc.minutes;
1449 tm.tm_sec = pc.seconds.tv_sec;
1451 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
1452 pc.seconds.tv_nsec = 0;
1456 * Let mktime deduce tm_isdst if we have an absolute timestamp.
1458 if (pc.dates_seen | pc.days_seen | pc.times_seen)
1462 * But if the input explicitly specifies local time with or
1463 * without DST, give mktime that information.
1465 if (pc.local_zones_seen)
1466 tm.tm_isdst = pc.local_isdst;
1470 Start = mktime (&tm);
1472 if (! mktime_ok (&tm0, &tm, Start)) {
1473 if (! pc.zones_seen) {
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.
1490 intmax_t time_zone = pc.time_zone;
1492 intmax_t abs_time_zone = time_zone < 0
1493 ? - time_zone : time_zone;
1495 intmax_t abs_time_zone_hour
1496 = abs_time_zone / 60;
1498 int abs_time_zone_min = abs_time_zone % 60;
1500 char tz1buf[sizeof "XXX+0:00"
1501 + sizeof pc.time_zone
1504 if (!tz_was_altered)
1505 tz0 = get_tz (tz0buf);
1506 sprintf (tz1buf, "XXX%s%jd:%02d",
1507 &"-"[time_zone < 0],
1510 if (setenv ("TZ", tz1buf, 1) != 0) {
1515 Start = mktime (&tm);
1516 if (! mktime_ok (&tm0, &tm, Start)) {
1522 if (pc.days_seen && ! pc.dates_seen) {
1523 tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7 + 7
1525 - (0 < pc.day_ordinal
1526 && tm.tm_wday != pc.day_number)));
1528 Start = mktime (&tm);
1529 if (Start == (time_t) -1) {
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))) {
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) {
1557 * The only "output" of this if-block is an updated Start value,
1558 * so this block must follow others that clobber Start.
1560 if (pc.zones_seen) {
1561 intmax_t delta = pc.time_zone * 60;
1563 #ifdef HAVE_TM_GMTOFF
1564 delta -= tm.tm_gmtoff;
1567 struct tm const *gmt = gmtime (&t);
1571 delta -= tm_diff (&tm, gmt);
1574 if ((Start < t1) != (delta < 0)) {
1575 goto fail; /* time_t overflow */
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.
1589 intmax_t sum_ns = pc.seconds.tv_nsec + pc.rel.ns;
1590 intmax_t normalized_ns = (sum_ns % BILLION + BILLION) % BILLION;
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;
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))
1611 result->tv_sec = t5;
1612 result->tv_nsec = normalized_ns;
1621 ok &= (tz0 ? setenv ("TZ", tz0, 1)
1622 : unsetenv ("TZ")) == 0;