3 * SPDX-License-Identifier: GPL-3.0-or-later
5 * Parse a string into an internal timestamp.
7 * This file is based on gnulib parse-datetime.y-dd7a871 with
8 * the other gnulib dependencies removed for use in util-linux.
10 * Copyright (C) 1999-2000, 2002-2017 Free Software Foundation, Inc.
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.
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.
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/>.
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.
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.
38 * FIXME: Check for arithmetic overflow in all cases, not just
46 #include "timeutils.h"
50 * There's no need to extend the stack, so there's no need to involve
53 #define YYSTACK_USE_ALLOCA 0
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.
62 #define YYINITDEPTH YYMAXDEPTH
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.
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.
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.
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.
113 : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0))
115 #define TM_YEAR_BASE 1900
117 #define HOUR(x) ((x) * 60)
119 #define STREQ(a, b) (strcmp (a, b) == 0)
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.
126 static unsigned char to_uchar (char ch) { return ch; }
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.
134 * An integer value, and the number of digits in its textual
143 /* An entry in the lexical lookup table. */
150 /* Meridian: am, pm, or 24-hour style. */
151 enum { MERam, MERpm, MER24 };
153 enum { BILLION = 1000000000, LOG10_BILLION = 9 };
155 /* Relative year, month, day, hour, minutes, seconds, and nanoseconds. */
166 #if HAVE_COMPOUND_LITERALS
167 # define RELATIVE_TIME_0 ((relative_time) { 0, 0, 0, 0, 0, 0, 0 })
169 static relative_time const RELATIVE_TIME_0;
172 /* Information passed to and from the parser. */
174 /* The input string remaining to be parsed. */
177 /* N, if this is the Nth Tuesday. */
178 intmax_t day_ordinal;
180 /* Day of week; Sunday is 0. */
183 /* tm_isdst flag for the local zone. */
186 /* Time zone, in minutes east of UTC. */
189 /* Style used for time. */
192 /* Gregorian year, month, day, hour, minutes, seconds, and ns. */
198 struct timespec seconds; /* includes nanoseconds */
200 /* Relative year, month, day, hour, minutes, seconds, and ns. */
203 /* Presence or counts of some nonterminals parsed so far. */
208 size_t local_zones_seen;
213 /* Table of local time zone abbreviations, null terminated. */
214 table local_time_zone_table[3];
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);
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,
227 static void digits_to_date_time(parser_control *pc, textint text_int)
229 if (pc->dates_seen && ! pc->year.digits
230 && ! pc->rels_seen && (pc->times_seen || 2 < text_int.digits)) {
233 if (4 < text_int.digits) {
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;
241 if (text_int.digits <= 2) {
242 pc->hour = text_int.value;
246 pc->hour = text_int.value / 100;
247 pc->minutes = text_int.value % 100;
249 pc->seconds.tv_sec = 0;
250 pc->seconds.tv_nsec = 0;
251 pc->meridian = MER24;
256 /* Increment PC->rel by FACTOR * REL (FACTOR is 1 or -1). */
257 static void apply_relative_time(parser_control *pc, relative_time rel,
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;
270 /* Set PC-> hour, minutes, seconds and nanoseconds members from arguments. */
272 set_hhmmss(parser_control *pc, intmax_t hour, intmax_t minutes,
273 time_t sec, int nsec)
276 pc->minutes = minutes;
277 pc->seconds.tv_sec = sec;
278 pc->seconds.tv_nsec = nsec;
284 * We want a reentrant parser, even if the TZ manipulation and the calls to
285 * localtime and gmtime are not reentrant.
288 %parse-param { parser_control *pc }
289 %lex-param { parser_control *pc }
291 /* This grammar has 31 shift/reduce conflicts. */
297 struct timespec timespec;
304 %token tYEAR_UNIT tMONTH_UNIT tHOUR_UNIT tMINUTE_UNIT tSEC_UNIT
305 %token <intval> tDAY_UNIT tDAY_SHIFT
307 %token <intval> tDAY tDAYZONE tLOCAL_ZONE tMERIDIAN
308 %token <intval> tMONTH tORDINAL tZONE
310 %token <textintval> tSNUMBER tUNUMBER
311 %token <timespec> tSDECIMAL_NUMBER tUDECIMAL_NUMBER
313 %type <textintval> o_colon_minutes
314 %type <timespec> seconds signed_seconds unsigned_seconds
316 %type <rel> relunit relunit_snumber dayshift
328 pc->timespec_seen = 1;
339 pc->times_seen++; pc->dates_seen++;
345 pc->local_zones_seen++;
366 iso_8601_date 'T' iso_8601_time
371 set_hhmmss (pc, $1.value, 0, 0, 0);
374 | tUNUMBER ':' tUNUMBER tMERIDIAN {
375 set_hhmmss (pc, $1.value, $3.value, 0, 0);
378 | tUNUMBER ':' tUNUMBER ':' unsigned_seconds tMERIDIAN {
379 set_hhmmss (pc, $1.value, $3.value, $5.tv_sec, $5.tv_nsec);
386 tUNUMBER zone_offset {
387 set_hhmmss (pc, $1.value, 0, 0, 0);
388 pc->meridian = MER24;
390 | tUNUMBER ':' tUNUMBER o_zone_offset {
391 set_hhmmss (pc, $1.value, $3.value, 0, 0);
392 pc->meridian = MER24;
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;
406 tSNUMBER o_colon_minutes {
408 if (! time_zone_hhmm (pc, $1, $2)) YYABORT;
413 * Local zone strings only affect DST setting,
414 * and only take affect if the current TZ setting is relevant.
417 * 'EEST' is parsed as tLOCAL_ZONE, as it relates to the effective TZ:
418 * TZ=Europe/Helsinki date -d '2016-12-30 EEST'
421 * 'EEST' is parsed as 'zone' (TZ=+03:00):
422 * TZ=Asia/Tokyo ./src/date --debug -d '2011-06-11 EEST'
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').
432 pc->local_isdst = $1;
433 pc->dsts_seen += (0 < $1);
437 pc->dsts_seen += (0 < $1) + 1;
442 * Note 'T' is a special case, as it is used as the separator in ISO
443 * 8601 date and time of day representation.
450 pc->time_zone = HOUR(7);
452 | tZONE relunit_snumber {
454 apply_relative_time (pc, $2, 1);
456 | 'T' relunit_snumber {
457 pc->time_zone = HOUR(7);
458 apply_relative_time (pc, $2, 1);
460 | tZONE tSNUMBER o_colon_minutes {
461 if (! time_zone_hhmm (pc, $2, $3)) YYABORT;
465 pc->time_zone = $1 + 60;
468 pc->time_zone = $1 + 60;
482 pc->day_ordinal = $1;
486 pc->day_ordinal = $1.value;
492 tUNUMBER '/' tUNUMBER {
493 pc->month = $1.value;
496 | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
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.
504 if (4 <= $1.digits) {
506 pc->month = $3.value;
509 pc->month = $1.value;
514 | tUNUMBER tMONTH tSNUMBER {
515 /* e.g. 17-JUN-1992. */
518 pc->year.value = -$3.value;
519 pc->year.digits = $3.digits;
521 | tMONTH tSNUMBER tSNUMBER {
522 /* e.g. JUN-17-1992. */
525 pc->year.value = -$3.value;
526 pc->year.digits = $3.digits;
532 | tMONTH tUNUMBER ',' tUNUMBER {
541 | tUNUMBER tMONTH tUNUMBER {
550 tUNUMBER tSNUMBER tSNUMBER {
551 /* ISO 8601 format.YYYY-MM-DD. */
553 pc->month = -$2.value;
560 { apply_relative_time (pc, $1, $2); }
562 { apply_relative_time (pc, $1, 1); }
564 { apply_relative_time (pc, $1, 1); }
569 { $$ = RELATIVE_TIME_0; $$.year = $1; }
570 | tUNUMBER tYEAR_UNIT
571 { $$ = RELATIVE_TIME_0; $$.year = $1.value; }
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; }
579 { $$ = RELATIVE_TIME_0; $$.month = 1; }
581 { $$ = RELATIVE_TIME_0; $$.day = $1 * $2; }
583 { $$ = RELATIVE_TIME_0; $$.day = $1.value * $2; }
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; }
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; }
597 { $$ = RELATIVE_TIME_0; $$.minutes = 1; }
599 { $$ = RELATIVE_TIME_0; $$.seconds = $1; }
601 { $$ = RELATIVE_TIME_0; $$.seconds = $1.value; }
602 | tSDECIMAL_NUMBER tSEC_UNIT {
603 $$ = RELATIVE_TIME_0;
604 $$.seconds = $1.tv_sec;
607 | tUDECIMAL_NUMBER tSEC_UNIT {
608 $$ = RELATIVE_TIME_0;
609 $$.seconds = $1.tv_sec;
613 { $$ = RELATIVE_TIME_0; $$.seconds = 1; }
619 { $$ = RELATIVE_TIME_0; $$.year = $1.value; }
620 | tSNUMBER tMONTH_UNIT
621 { $$ = RELATIVE_TIME_0; $$.month = $1.value; }
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; }
629 { $$ = RELATIVE_TIME_0; $$.seconds = $1.value; }
634 { $$ = RELATIVE_TIME_0; $$.day = $1; }
637 seconds: signed_seconds | unsigned_seconds;
642 { $$.tv_sec = $1.value; $$.tv_nsec = 0; }
648 { $$.tv_sec = $1.value; $$.tv_nsec = 0; }
653 { digits_to_date_time (pc, $1); }
657 tUNUMBER relunit_snumber {
659 * Hybrid all-digit and relative offset, so that we accept e.g.,
660 * "YYYYMMDD +N days" as well as "YYYYMMDD N days".
662 digits_to_date_time (pc, $1);
663 apply_relative_time (pc, $2, 1);
669 { $$.value = $$.digits = 0; }
677 static table const meridian_table[] = {
678 { "AM", tMERIDIAN, MERam },
679 { "A.M.", tMERIDIAN, MERam },
680 { "PM", tMERIDIAN, MERpm },
681 { "P.M.", tMERIDIAN, MERpm },
685 static table const dst_table[] = {
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 },
707 { "WEDNESDAY",tDAY, 3 },
708 { "WEDNES", tDAY, 3 },
709 { "THURSDAY", tDAY, 4 },
711 { "THURS", tDAY, 4 },
712 { "FRIDAY", tDAY, 5 },
713 { "SATURDAY", tDAY, 6 },
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 },
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 },
753 { "HENCE", tAGO, 1 },
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.
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) },
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.
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 */
828 * Military time zone table.
830 * Note 'T' is a special case, as it is used as the separator in ISO
831 * 8601 date and time of day representation.
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) },
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) },
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).
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.
874 static int time_zone_hhmm(parser_control *pc, textint hh, textint mm)
878 if (hh.digits > 2 && hh.digits < 5 && mm.digits == 0) {
881 } else if (hh.digits < 3 && (mm.digits == 0 || mm.digits == 2)) {
883 m = hh.negative ? -mm.value : mm.value;
887 if (abs(h) > 24 || abs(m) > 59)
890 pc->time_zone = h * 60 + m;
894 static int to_hour(intmax_t hours, int meridian)
897 default: /* Pacify GCC. */
899 return 0 <= hours && hours < 24 ? hours : -1;
901 return 0 < hours && hours < 12 ? hours : hours == 12 ? 0 : -1;
903 return 0 < hours && hours < 12 ? hours + 12 : hours == 12 ? 12 : -1;
907 static long int to_year(textint textyear)
909 intmax_t year = textyear.value;
915 * XPG4 suggests that years 00-68 map to 2000-2068, and
916 * years 69-99 map to 1969-1999.
918 else if (textyear.digits == 2)
919 year += year < 69 ? 2000 : 1900;
924 static table const * lookup_zone(parser_control const *pc, char const *name)
928 for (tp = universal_time_zone_table; tp->name; tp++)
929 if (strcmp (name, tp->name) == 0)
933 * Try local zone abbreviations before those in time_zone_table, as
934 * the local ones are more likely to be right.
936 for (tp = pc->local_time_zone_table; tp->name; tp++)
937 if (strcmp (name, tp->name) == 0)
940 for (tp = time_zone_table; tp->name; tp++)
941 if (strcmp (name, tp->name) == 0)
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.
954 static int tm_diff(struct tm const *a, struct tm const *b)
957 * Compute intervening leap days correctly even if year is negative.
958 * Take care to avoid int overflow in leap day calculations.
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));
974 #endif /* ! HAVE_TM_GMTOFF */
976 static table const * lookup_word(parser_control const *pc, char *word)
985 /* Make it uppercase. */
986 for (p = word; *p; p++)
987 *p = c_toupper (to_uchar (*p));
989 for (tp = meridian_table; tp->name; tp++)
990 if (strcmp (word, tp->name) == 0)
993 /* See if we have an abbreviation for a month. */
994 wordlen = strlen (word);
995 abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.');
997 for (tp = month_and_day_table; tp->name; tp++)
998 if ((abbrev ? strncmp (word, tp->name, 3) :
999 strcmp (word, tp->name)) == 0)
1002 if ((tp = lookup_zone (pc, word)))
1005 if (strcmp (word, dst_table[0].name) == 0)
1008 for (tp = time_units_table; tp->name; tp++)
1009 if (strcmp (word, tp->name) == 0)
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)
1018 word[wordlen - 1] = 'S'; /* For "this" in relative_time_table. */
1021 for (tp = relative_time_table; tp->name; tp++)
1022 if (strcmp (word, tp->name) == 0)
1025 /* Military time zones. */
1027 for (tp = military_table; tp->name; tp++)
1028 if (word[0] == tp->name[0])
1031 /* Drop out any periods and try the time zone table again. */
1032 for (period_found = 0, p = q = word; (*p = *q); q++)
1037 if (period_found && (tp = lookup_zone (pc, word)))
1043 static int yylex (union YYSTYPE *lvalp, parser_control *pc)
1049 while (c = *pc->input, c_isspace (c))
1052 if (c_isdigit (c) || c == '-' || c == '+') {
1056 if (c == '-' || c == '+') {
1057 sign = c == '-' ? -1 : 1;
1058 while (c = *++pc->input, c_isspace (c))
1060 if (! c_isdigit (c))
1061 /* skip the '-' sign */
1066 for (value = 0; ; value *= 10) {
1067 uintmax_t value1 = value + (c - '0');
1072 if (! c_isdigit (c))
1074 if (UINTMAX_MAX / 10 < value)
1077 if ((c == '.' || c == ',') && c_isdigit (p[1])) {
1083 /* Check for overflow when converting value to
1097 if (value != value1)
1100 /* Accumulate fraction, to ns precision. */
1104 digits <= LOG10_BILLION; digits++) {
1110 /* Skip excess digits, truncating toward
1114 for (; c_isdigit (*p); p++)
1119 while (c_isdigit (*p))
1122 /* Adjust to the timespec convention, which is
1123 * that tv_nsec is always a positive offset even
1124 * if tv_sec is negative.
1126 if (sign < 0 && ns) {
1133 lvalp->timespec.tv_sec = s;
1134 lvalp->timespec.tv_nsec = ns;
1137 sign ? tSDECIMAL_NUMBER : tUDECIMAL_NUMBER;
1139 lvalp->textintval.negative = sign < 0;
1141 lvalp->textintval.value = - value;
1142 if (0 < lvalp->textintval.value)
1145 lvalp->textintval.value = value;
1146 if (lvalp->textintval.value < 0)
1149 lvalp->textintval.digits = p - pc->input;
1151 return sign ? tSNUMBER : tUNUMBER;
1155 if (c_isalpha (c)) {
1161 if (p < buff + sizeof buff - 1)
1165 while (c_isalpha (c) || c == '.');
1168 tp = lookup_word (pc, buff);
1172 lvalp->intval = tp->value;
1177 return to_uchar (*pc->input++);
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__)))
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.
1206 static int mktime_ok(struct tm const *tm0, struct tm const *tm1, time_t t)
1208 if (t == (time_t) -1) {
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.
1214 tm1 = localtime (&t);
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));
1228 * A reasonable upper bound for the size of ordinary TZ strings.
1229 * Use heap allocation if TZ's length exceeds this.
1231 enum { TZBUFSIZE = 100 };
1234 * Return a copy of TZ, stored in TZBUF if it fits, and heap-allocated
1237 static char * get_tz(char tzbuf[TZBUFSIZE])
1239 char *tz = getenv ("TZ");
1241 size_t tzsize = strlen (tz) + 1;
1242 tz = (tzsize <= TZBUFSIZE
1243 ? memcpy (tzbuf, tz, tzsize)
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.
1255 int parse_date(struct timespec *result, char const *p,
1256 struct timespec const *now)
1260 struct tm const *tmp;
1264 struct timespec gettime_buffer;
1266 int tz_was_altered = 0;
1268 char tz0buf[TZBUFSIZE];
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;
1279 Start = now->tv_sec;
1280 Start_ns = now->tv_nsec;
1282 tmp = localtime (&now->tv_sec);
1286 while (c = *p, c_isspace (c))
1289 if (strncmp (p, "TZ=\"", 4) == 0) {
1290 char const *tzbase = p + 4;
1294 for (s = tzbase; *s; s++, tzsize++)
1297 if (! (*s == '\\' || *s == '"'))
1299 } else if (*s == '"') {
1302 char tz1buf[TZBUFSIZE];
1303 int large_tz = TZBUFSIZE < tzsize;
1306 tz0 = get_tz (tz0buf);
1311 z = tz1 = malloc (tzsize);
1317 for (s = tzbase; *s != '"'; s++)
1318 *z++ = *(s += *s == '\\');
1320 setenv_ok = setenv ("TZ", tz1, 1) == 0;
1328 while (c = *p, c_isspace (c))
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.
1344 pc.year.value = tmp->tm_year;
1345 pc.year.value += TM_YEAR_BASE;
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;
1355 pc.meridian = MER24;
1356 pc.rel = RELATIVE_TIME_0;
1357 pc.timespec_seen = 0;
1362 pc.local_zones_seen = 0;
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;
1373 * Probe the names used in the next three calendar quarters, looking
1374 * for a tm_isdst different from the one we already have.
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) {
1385 pc.local_time_zone_table[1].name
1386 = probe_tm->tm_zone;
1387 pc.local_time_zone_table[1].type
1389 pc.local_time_zone_table[1].value
1390 = probe_tm->tm_isdst;
1391 pc.local_time_zone_table[2].name
1401 # if !HAVE_DECL_TZNAME
1402 extern char *tzname[];
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;
1410 pc.local_time_zone_table[i].name = NULL;
1413 pc.local_time_zone_table[0].name = NULL;
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)) {
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.
1425 pc.local_time_zone_table[0].value = -1;
1426 pc.local_time_zone_table[1].name = NULL;
1429 if (yyparse (&pc) != 0) {
1433 if (pc.timespec_seen)
1434 *result = pc.seconds;
1436 if (1 < (pc.times_seen | pc.dates_seen | pc.days_seen
1438 | (pc.local_zones_seen + pc.zones_seen))) {
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) {
1451 tm.tm_min = pc.minutes;
1452 tm.tm_sec = pc.seconds.tv_sec;
1454 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
1455 pc.seconds.tv_nsec = 0;
1459 * Let mktime deduce tm_isdst if we have an absolute timestamp.
1461 if (pc.dates_seen | pc.days_seen | pc.times_seen)
1465 * But if the input explicitly specifies local time with or
1466 * without DST, give mktime that information.
1468 if (pc.local_zones_seen)
1469 tm.tm_isdst = pc.local_isdst;
1473 Start = mktime (&tm);
1475 if (! mktime_ok (&tm0, &tm, Start)) {
1476 if (! pc.zones_seen) {
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.
1493 intmax_t time_zone = pc.time_zone;
1495 intmax_t abs_time_zone = time_zone < 0
1496 ? - time_zone : time_zone;
1498 intmax_t abs_time_zone_hour
1499 = abs_time_zone / 60;
1501 int abs_time_zone_min = abs_time_zone % 60;
1503 char tz1buf[sizeof "XXX+0:00"
1504 + sizeof pc.time_zone
1507 if (!tz_was_altered)
1508 tz0 = get_tz (tz0buf);
1509 sprintf (tz1buf, "XXX%s%jd:%02d",
1510 &"-"[time_zone < 0],
1513 if (setenv ("TZ", tz1buf, 1) != 0) {
1518 Start = mktime (&tm);
1519 if (! mktime_ok (&tm0, &tm, Start)) {
1525 if (pc.days_seen && ! pc.dates_seen) {
1526 tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7 + 7
1528 - (0 < pc.day_ordinal
1529 && tm.tm_wday != pc.day_number)));
1531 Start = mktime (&tm);
1532 if (Start == (time_t) -1) {
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))) {
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) {
1560 * The only "output" of this if-block is an updated Start value,
1561 * so this block must follow others that clobber Start.
1563 if (pc.zones_seen) {
1564 intmax_t delta = pc.time_zone * 60;
1566 #ifdef HAVE_TM_GMTOFF
1567 delta -= tm.tm_gmtoff;
1570 struct tm const *gmt = gmtime (&t);
1574 delta -= tm_diff (&tm, gmt);
1577 if ((Start < t1) != (delta < 0)) {
1578 goto fail; /* time_t overflow */
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.
1592 intmax_t sum_ns = pc.seconds.tv_nsec + pc.rel.ns;
1593 intmax_t normalized_ns = (sum_ns % BILLION + BILLION) % BILLION;
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;
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))
1614 result->tv_sec = t5;
1615 result->tv_nsec = normalized_ns;
1624 ok &= (tz0 ? setenv ("TZ", tz0, 1)
1625 : unsetenv ("TZ")) == 0;