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.
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.
97 * ISDIGIT differs from isdigit, as follows:
98 * - Its arg may be any int or unsigned int; it need not be an unsigned char
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.
105 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
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.
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.
122 : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0))
124 #define EPOCH_YEAR 1970
125 #define TM_YEAR_BASE 1900
127 #define HOUR(x) ((x) * 60)
129 #define STREQ(a, b) (strcmp (a, b) == 0)
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.
136 static unsigned char to_uchar (char ch) { return ch; }
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.
144 * An integer value, and the number of digits in its textual
153 /* An entry in the lexical lookup table. */
160 /* Meridian: am, pm, or 24-hour style. */
161 enum { MERam, MERpm, MER24 };
163 enum { BILLION = 1000000000, LOG10_BILLION = 9 };
165 /* Relative year, month, day, hour, minutes, seconds, and nanoseconds. */
176 #if HAVE_COMPOUND_LITERALS
177 # define RELATIVE_TIME_0 ((relative_time) { 0, 0, 0, 0, 0, 0, 0 })
179 static relative_time const RELATIVE_TIME_0;
182 /* Information passed to and from the parser. */
184 /* The input string remaining to be parsed. */
187 /* N, if this is the Nth Tuesday. */
188 long int day_ordinal;
190 /* Day of week; Sunday is 0. */
193 /* tm_isdst flag for the local zone. */
196 /* Time zone, in minutes east of UTC. */
199 /* Style used for time. */
202 /* Gregorian year, month, day, hour, minutes, seconds, and ns. */
208 struct timespec seconds; /* includes nanoseconds */
210 /* Relative year, month, day, hour, minutes, seconds, and ns. */
213 /* Presence or counts of some nonterminals parsed so far. */
218 size_t local_zones_seen;
224 /* 1 if the user specified explicit ordinal day value, */
225 int ordinal_day_seen;
227 /* Table of local time zone abbreviations, null terminated. */
228 table local_time_zone_table[3];
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);
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,
241 static void digits_to_date_time(parser_control *pc, textint text_int)
243 if (pc->dates_seen && ! pc->year.digits
244 && ! pc->rels_seen && (pc->times_seen || 2 < text_int.digits)) {
248 if (4 < text_int.digits) {
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;
256 if (text_int.digits <= 2) {
257 pc->hour = text_int.value;
261 pc->hour = text_int.value / 100;
262 pc->minutes = text_int.value % 100;
264 pc->seconds.tv_sec = 0;
265 pc->seconds.tv_nsec = 0;
266 pc->meridian = MER24;
271 /* Increment PC->rel by FACTOR * REL (FACTOR is 1 or -1). */
272 static void apply_relative_time(parser_control *pc, relative_time rel,
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;
285 /* Set PC-> hour, minutes, seconds and nanoseconds members from arguments. */
287 set_hhmmss(parser_control *pc, long int hour, long int minutes,
288 time_t sec, long int nsec)
291 pc->minutes = minutes;
292 pc->seconds.tv_sec = sec;
293 pc->seconds.tv_nsec = nsec;
299 * We want a reentrant parser, even if the TZ manipulation and the calls to
300 * localtime and gmtime are not reentrant.
303 %parse-param { parser_control *pc }
304 %lex-param { parser_control *pc }
306 /* This grammar has 31 shift/reduce conflicts. */
312 struct timespec timespec;
319 %token tYEAR_UNIT tMONTH_UNIT tHOUR_UNIT tMINUTE_UNIT tSEC_UNIT
320 %token <intval> tDAY_UNIT tDAY_SHIFT
322 %token <intval> tDAY tDAYZONE tLOCAL_ZONE tMERIDIAN
323 %token <intval> tMONTH tORDINAL tZONE
325 %token <textintval> tSNUMBER tUNUMBER
326 %token <timespec> tSDECIMAL_NUMBER tUDECIMAL_NUMBER
328 %type <intval> o_colon_minutes
329 %type <timespec> seconds signed_seconds unsigned_seconds
331 %type <rel> relunit relunit_snumber dayshift
343 pc->timespec_seen = 1;
354 pc->times_seen++; pc->dates_seen++;
360 pc->local_zones_seen++;
381 iso_8601_date 'T' iso_8601_time
386 set_hhmmss (pc, $1.value, 0, 0, 0);
389 | tUNUMBER ':' tUNUMBER tMERIDIAN {
390 set_hhmmss (pc, $1.value, $3.value, 0, 0);
393 | tUNUMBER ':' tUNUMBER ':' unsigned_seconds tMERIDIAN {
394 set_hhmmss (pc, $1.value, $3.value, $5.tv_sec, $5.tv_nsec);
401 tUNUMBER zone_offset {
402 set_hhmmss (pc, $1.value, 0, 0, 0);
403 pc->meridian = MER24;
405 | tUNUMBER ':' tUNUMBER o_zone_offset {
406 set_hhmmss (pc, $1.value, $3.value, 0, 0);
407 pc->meridian = MER24;
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;
421 tSNUMBER o_colon_minutes {
423 pc->time_zone = time_zone_hhmm (pc, $1, $2);
428 * Local zone strings only affect DST setting,
429 * and only take affect if the current TZ setting is relevant.
432 * 'EEST' is parsed as tLOCAL_ZONE, as it relates to the effective TZ:
433 * TZ=Europe/Helsinki date -d '2016-12-30 EEST'
436 * 'EEST' is parsed as 'zone' (TZ=+03:00):
437 * TZ=Asia/Tokyo ./src/date --debug -d '2011-06-11 EEST'
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').
447 pc->local_isdst = $1;
448 pc->dsts_seen += (0 < $1);
452 pc->dsts_seen += (0 < $1) + 1;
457 * Note 'T' is a special case, as it is used as the separator in ISO
458 * 8601 date and time of day representation.
465 pc->time_zone = HOUR(7);
467 | tZONE relunit_snumber {
469 apply_relative_time (pc, $2, 1);
471 | 'T' relunit_snumber {
472 pc->time_zone = HOUR(7);
473 apply_relative_time (pc, $2, 1);
475 | tZONE tSNUMBER o_colon_minutes {
476 pc->time_zone = $1 + time_zone_hhmm (pc, $2, $3);
479 pc->time_zone = $1 + 60;
482 pc->time_zone = $1 + 60;
496 pc->day_ordinal = $1;
498 pc->ordinal_day_seen = 1;
501 pc->day_ordinal = $1.value;
503 pc->ordinal_day_seen = 1;
508 tUNUMBER '/' tUNUMBER {
509 pc->month = $1.value;
512 | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
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.
520 if (4 <= $1.digits) {
522 pc->month = $3.value;
525 pc->month = $1.value;
530 | tUNUMBER tMONTH tSNUMBER {
531 /* e.g. 17-JUN-1992. */
534 pc->year.value = -$3.value;
535 pc->year.digits = $3.digits;
537 | tMONTH tSNUMBER tSNUMBER {
538 /* e.g. JUN-17-1992. */
541 pc->year.value = -$3.value;
542 pc->year.digits = $3.digits;
548 | tMONTH tUNUMBER ',' tUNUMBER {
557 | tUNUMBER tMONTH tUNUMBER {
566 tUNUMBER tSNUMBER tSNUMBER {
567 /* ISO 8601 format.YYYY-MM-DD. */
569 pc->month = -$2.value;
576 { apply_relative_time (pc, $1, $2); }
578 { apply_relative_time (pc, $1, 1); }
580 { apply_relative_time (pc, $1, 1); }
585 { $$ = RELATIVE_TIME_0; $$.year = $1; }
586 | tUNUMBER tYEAR_UNIT
587 { $$ = RELATIVE_TIME_0; $$.year = $1.value; }
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; }
595 { $$ = RELATIVE_TIME_0; $$.month = 1; }
597 { $$ = RELATIVE_TIME_0; $$.day = $1 * $2; }
599 { $$ = RELATIVE_TIME_0; $$.day = $1.value * $2; }
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; }
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; }
613 { $$ = RELATIVE_TIME_0; $$.minutes = 1; }
615 { $$ = RELATIVE_TIME_0; $$.seconds = $1; }
617 { $$ = RELATIVE_TIME_0; $$.seconds = $1.value; }
618 | tSDECIMAL_NUMBER tSEC_UNIT {
619 $$ = RELATIVE_TIME_0;
620 $$.seconds = $1.tv_sec;
623 | tUDECIMAL_NUMBER tSEC_UNIT {
624 $$ = RELATIVE_TIME_0;
625 $$.seconds = $1.tv_sec;
629 { $$ = RELATIVE_TIME_0; $$.seconds = 1; }
635 { $$ = RELATIVE_TIME_0; $$.year = $1.value; }
636 | tSNUMBER tMONTH_UNIT
637 { $$ = RELATIVE_TIME_0; $$.month = $1.value; }
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; }
645 { $$ = RELATIVE_TIME_0; $$.seconds = $1.value; }
650 { $$ = RELATIVE_TIME_0; $$.day = $1; }
653 seconds: signed_seconds | unsigned_seconds;
658 { $$.tv_sec = $1.value; $$.tv_nsec = 0; }
664 { $$.tv_sec = $1.value; $$.tv_nsec = 0; }
669 { digits_to_date_time (pc, $1); }
673 tUNUMBER relunit_snumber {
675 * Hybrid all-digit and relative offset, so that we accept e.g.,
676 * "YYYYMMDD +N days" as well as "YYYYMMDD N days".
678 digits_to_date_time (pc, $1);
679 apply_relative_time (pc, $2, 1);
692 static table const meridian_table[] = {
693 { "AM", tMERIDIAN, MERam },
694 { "A.M.", tMERIDIAN, MERam },
695 { "PM", tMERIDIAN, MERpm },
696 { "P.M.", tMERIDIAN, MERpm },
700 static table const dst_table[] = {
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 },
722 { "WEDNESDAY",tDAY, 3 },
723 { "WEDNES", tDAY, 3 },
724 { "THURSDAY", tDAY, 4 },
726 { "THURS", tDAY, 4 },
727 { "FRIDAY", tDAY, 5 },
728 { "SATURDAY", tDAY, 6 },
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 },
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 },
768 { "HENCE", tAGO, 1 },
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.
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) },
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.
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 */
843 * Military time zone table.
845 * Note 'T' is a special case, as it is used as the separator in ISO
846 * 8601 date and time of day representation.
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) },
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) },
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
886 static long int time_zone_hhmm(parser_control *pc, textint s, long int mm)
891 * If the length of S is 1 or 2 and no minutes are specified,
892 * interpret it as a number of hours.
894 if (s.digits <= 2 && mm < 0)
898 n_minutes = (s.value / 100) * 60 + s.value % 100;
900 n_minutes = s.value * 60 + (s.negative ? -mm : mm);
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.
907 if (24 * 60 < abs (n_minutes))
913 static int to_hour(long int hours, int meridian)
916 default: /* Pacify GCC. */
918 return 0 <= hours && hours < 24 ? hours : -1;
920 return 0 < hours && hours < 12 ? hours : hours == 12 ? 0 : -1;
922 return 0 < hours && hours < 12 ? hours + 12 : hours == 12 ? 12 : -1;
926 static long int to_year(textint textyear)
928 long int year = textyear.value;
934 * XPG4 suggests that years 00-68 map to 2000-2068, and
935 * years 69-99 map to 1969-1999.
937 else if (textyear.digits == 2)
938 year += year < 69 ? 2000 : 1900;
943 static table const * lookup_zone(parser_control const *pc, char const *name)
947 for (tp = universal_time_zone_table; tp->name; tp++)
948 if (strcmp (name, tp->name) == 0)
952 * Try local zone abbreviations before those in time_zone_table, as
953 * the local ones are more likely to be right.
955 for (tp = pc->local_time_zone_table; tp->name; tp++)
956 if (strcmp (name, tp->name) == 0)
959 for (tp = time_zone_table; tp->name; tp++)
960 if (strcmp (name, tp->name) == 0)
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.
973 static long int tm_diff(struct tm const *a, struct tm const *b)
976 * Compute intervening leap days correctly even if year is negative.
977 * Take care to avoid int overflow in leap day calculations.
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));
994 #endif /* ! HAVE_TM_GMTOFF */
996 static table const * lookup_word(parser_control const *pc, char *word)
1005 /* Make it uppercase. */
1006 for (p = word; *p; p++) {
1007 unsigned char ch = *p;
1008 *p = c_toupper (ch);
1011 for (tp = meridian_table; tp->name; tp++)
1012 if (strcmp (word, tp->name) == 0)
1015 /* See if we have an abbreviation for a month. */
1016 wordlen = strlen (word);
1017 abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.');
1019 for (tp = month_and_day_table; tp->name; tp++)
1020 if ((abbrev ? strncmp (word, tp->name, 3) :
1021 strcmp (word, tp->name)) == 0)
1024 if ((tp = lookup_zone (pc, word)))
1027 if (strcmp (word, dst_table[0].name) == 0)
1030 for (tp = time_units_table; tp->name; tp++)
1031 if (strcmp (word, tp->name) == 0)
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)
1040 word[wordlen - 1] = 'S'; /* For "this" in relative_time_table. */
1043 for (tp = relative_time_table; tp->name; tp++)
1044 if (strcmp (word, tp->name) == 0)
1047 /* Military time zones. */
1049 for (tp = military_table; tp->name; tp++)
1050 if (word[0] == tp->name[0])
1053 /* Drop out any periods and try the time zone table again. */
1054 for (period_found = 0, p = q = word; (*p = *q); q++)
1059 if (period_found && (tp = lookup_zone (pc, word)))
1065 static int yylex (union YYSTYPE *lvalp, parser_control *pc)
1071 while (c = *pc->input, c_isspace (c))
1074 if (ISDIGIT (c) || c == '-' || c == '+') {
1077 unsigned long int value;
1078 if (c == '-' || c == '+') {
1079 sign = c == '-' ? -1 : 1;
1080 while (c = *++pc->input, c_isspace (c))
1083 /* skip the '-' sign */
1088 for (value = 0; ; value *= 10) {
1089 unsigned long int value1 = value + (c - '0');
1096 if (ULONG_MAX / 10 < value)
1099 if ((c == '.' || c == ',') && ISDIGIT (p[1])) {
1103 unsigned long int value1;
1105 /* Check for overflow when converting value to
1119 if (value != value1)
1122 /* Accumulate fraction, to ns precision. */
1126 digits <= LOG10_BILLION; digits++) {
1132 /* Skip excess digits, truncating toward
1136 for (; ISDIGIT (*p); p++)
1141 while (ISDIGIT (*p))
1144 /* Adjust to the timespec convention, which is
1145 * that tv_nsec is always a positive offset even
1146 * if tv_sec is negative.
1148 if (sign < 0 && ns) {
1155 lvalp->timespec.tv_sec = s;
1156 lvalp->timespec.tv_nsec = ns;
1159 sign ? tSDECIMAL_NUMBER : tUDECIMAL_NUMBER;
1161 lvalp->textintval.negative = sign < 0;
1163 lvalp->textintval.value = - value;
1164 if (0 < lvalp->textintval.value)
1167 lvalp->textintval.value = value;
1168 if (lvalp->textintval.value < 0)
1171 lvalp->textintval.digits = p - pc->input;
1173 return sign ? tSNUMBER : tUNUMBER;
1177 if (c_isalpha (c)) {
1183 if (p < buff + sizeof buff - 1)
1187 while (c_isalpha (c) || c == '.');
1190 tp = lookup_word (pc, buff);
1194 lvalp->intval = tp->value;
1199 return to_uchar (*pc->input++);
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__)))
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.
1228 static int mktime_ok(struct tm const *tm0, struct tm const *tm1, time_t t)
1230 if (t == (time_t) -1) {
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.
1236 tm1 = localtime (&t);
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));
1250 * A reasonable upper bound for the size of ordinary TZ strings.
1251 * Use heap allocation if TZ's length exceeds this.
1253 enum { TZBUFSIZE = 100 };
1256 * Return a copy of TZ, stored in TZBUF if it fits, and heap-allocated
1259 static char * get_tz(char tzbuf[TZBUFSIZE])
1261 char *tz = getenv ("TZ");
1263 size_t tzsize = strlen (tz) + 1;
1264 tz = (tzsize <= TZBUFSIZE
1265 ? memcpy (tzbuf, tz, tzsize)
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.
1277 int parse_date(struct timespec *result, char const *p,
1278 struct timespec const *now)
1282 struct tm const *tmp;
1286 struct timespec gettime_buffer;
1288 int tz_was_altered = 0;
1290 char tz0buf[TZBUFSIZE];
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;
1301 Start = now->tv_sec;
1302 Start_ns = now->tv_nsec;
1304 tmp = localtime (&now->tv_sec);
1308 while (c = *p, c_isspace (c))
1311 if (strncmp (p, "TZ=\"", 4) == 0) {
1312 char const *tzbase = p + 4;
1316 for (s = tzbase; *s; s++, tzsize++)
1319 if (! (*s == '\\' || *s == '"'))
1321 } else if (*s == '"') {
1324 char tz1buf[TZBUFSIZE];
1325 int large_tz = TZBUFSIZE < tzsize;
1327 tz0 = get_tz (tz0buf);
1328 z = tz1 = large_tz ? malloc (tzsize) : tz1buf;
1329 for (s = tzbase; *s != '"'; s++)
1330 *z++ = *(s += *s == '\\');
1332 setenv_ok = setenv ("TZ", tz1, 1) == 0;
1340 while (c = *p, c_isspace (c))
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.
1356 pc.year.value = tmp->tm_year;
1357 pc.year.value += TM_YEAR_BASE;
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;
1367 pc.meridian = MER24;
1368 pc.rel = RELATIVE_TIME_0;
1369 pc.timespec_seen = 0;
1374 pc.local_zones_seen = 0;
1378 pc.ordinal_day_seen = 0;
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;
1387 * Probe the names used in the next three calendar quarters, looking
1388 * for a tm_isdst different from the one we already have.
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) {
1399 pc.local_time_zone_table[1].name
1400 = probe_tm->tm_zone;
1401 pc.local_time_zone_table[1].type
1403 pc.local_time_zone_table[1].value
1404 = probe_tm->tm_isdst;
1405 pc.local_time_zone_table[2].name
1415 # if !HAVE_DECL_TZNAME
1416 extern char *tzname[];
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;
1424 pc.local_time_zone_table[i].name = NULL;
1427 pc.local_time_zone_table[0].name = NULL;
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)) {
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.
1439 pc.local_time_zone_table[0].value = -1;
1440 pc.local_time_zone_table[1].name = NULL;
1443 if (yyparse (&pc) != 0) {
1447 if (pc.timespec_seen)
1448 *result = pc.seconds;
1450 if (1 < (pc.times_seen | pc.dates_seen | pc.days_seen
1452 | (pc.local_zones_seen + pc.zones_seen))) {
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) {
1465 tm.tm_min = pc.minutes;
1466 tm.tm_sec = pc.seconds.tv_sec;
1468 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
1469 pc.seconds.tv_nsec = 0;
1473 * Let mktime deduce tm_isdst if we have an absolute timestamp.
1475 if (pc.dates_seen | pc.days_seen | pc.times_seen)
1479 * But if the input explicitly specifies local time with or
1480 * without DST, give mktime that information.
1482 if (pc.local_zones_seen)
1483 tm.tm_isdst = pc.local_isdst;
1487 Start = mktime (&tm);
1489 if (! mktime_ok (&tm0, &tm, Start)) {
1490 if (! pc.zones_seen) {
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.
1507 long int time_zone = pc.time_zone;
1509 long int abs_time_zone = time_zone < 0
1510 ? - time_zone : time_zone;
1512 long int abs_time_zone_hour
1513 = abs_time_zone / 60;
1515 int abs_time_zone_min = abs_time_zone % 60;
1517 char tz1buf[sizeof "XXX+0:00"
1518 + sizeof pc.time_zone
1521 if (!tz_was_altered)
1522 tz0 = get_tz (tz0buf);
1523 sprintf (tz1buf, "XXX%s%ld:%02d",
1524 &"-"[time_zone < 0],
1527 if (setenv ("TZ", tz1buf, 1) != 0) {
1532 Start = mktime (&tm);
1533 if (! mktime_ok (&tm0, &tm, Start)) {
1539 if (pc.days_seen && ! pc.dates_seen) {
1540 tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7 + 7
1542 - (0 < pc.day_ordinal
1543 && tm.tm_wday != pc.day_number)));
1545 Start = mktime (&tm);
1546 if (Start == (time_t) -1) {
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))) {
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) {
1574 * The only "output" of this if-block is an updated Start value,
1575 * so this block must follow others that clobber Start.
1577 if (pc.zones_seen) {
1578 long int delta = pc.time_zone * 60;
1580 #ifdef HAVE_TM_GMTOFF
1581 delta -= tm.tm_gmtoff;
1584 struct tm const *gmt = gmtime (&t);
1588 delta -= tm_diff (&tm, gmt);
1591 if ((Start < t1) != (delta < 0)) {
1592 goto fail; /* time_t overflow */
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.
1606 long int sum_ns = pc.seconds.tv_nsec + pc.rel.ns;
1607 long int normalized_ns = (sum_ns % BILLION + BILLION) % BILLION;
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;
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))
1628 result->tv_sec = t5;
1629 result->tv_nsec = normalized_ns;
1638 ok &= (tz0 ? setenv ("TZ", tz0, 1)
1639 : unsetenv ("TZ")) == 0;