]> git.cworth.org Git - tar/blob - gnu/parse-datetime.c
Imported Upstream version 1.24
[tar] / gnu / parse-datetime.c
1 /* A Bison parser, made by GNU Bison 2.3.  */
2
3 /* Skeleton implementation for Bison's Yacc-like parsers in C
4
5    Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
6    Free Software Foundation, Inc.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor,
21    Boston, MA 02110-1301, USA.  */
22
23 /* As a special exception, you may create a larger work that contains
24    part or all of the Bison parser skeleton and distribute that work
25    under terms of your choice, so long as that work isn't itself a
26    parser generator using the skeleton or a modified version thereof
27    as a parser skeleton.  Alternatively, if you modify or redistribute
28    the parser skeleton itself, you may (at your option) remove this
29    special exception, which will cause the skeleton and the resulting
30    Bison output files to be licensed under the GNU General Public
31    License without this special exception.
32
33    This special exception was added by the Free Software Foundation in
34    version 2.2 of Bison.  */
35
36 /* C LALR(1) parser skeleton written by Richard Stallman, by
37    simplifying the original so-called "semantic" parser.  */
38
39 /* All symbols defined below should begin with yy or YY, to avoid
40    infringing on user name space.  This should be done even for local
41    variables, as they might otherwise be expanded by user macros.
42    There are some unavoidable exceptions within include files to
43    define necessary library symbols; they are noted "INFRINGES ON
44    USER NAME SPACE" below.  */
45
46 /* Identify Bison output.  */
47 #define YYBISON 1
48
49 /* Bison version.  */
50 #define YYBISON_VERSION "2.3"
51
52 /* Skeleton name.  */
53 #define YYSKELETON_NAME "yacc.c"
54
55 /* Pure parsers.  */
56 #define YYPURE 1
57
58 /* Using locations.  */
59 #define YYLSP_NEEDED 0
60
61
62
63 /* Tokens.  */
64 #ifndef YYTOKENTYPE
65 # define YYTOKENTYPE
66    /* Put the tokens into the symbol table, so that GDB and other debuggers
67       know about them.  */
68    enum yytokentype {
69      tAGO = 258,
70      tDST = 259,
71      tYEAR_UNIT = 260,
72      tMONTH_UNIT = 261,
73      tHOUR_UNIT = 262,
74      tMINUTE_UNIT = 263,
75      tSEC_UNIT = 264,
76      tDAY_UNIT = 265,
77      tDAY_SHIFT = 266,
78      tDAY = 267,
79      tDAYZONE = 268,
80      tLOCAL_ZONE = 269,
81      tMERIDIAN = 270,
82      tMONTH = 271,
83      tORDINAL = 272,
84      tZONE = 273,
85      tSNUMBER = 274,
86      tUNUMBER = 275,
87      tSDECIMAL_NUMBER = 276,
88      tUDECIMAL_NUMBER = 277
89    };
90 #endif
91 /* Tokens.  */
92 #define tAGO 258
93 #define tDST 259
94 #define tYEAR_UNIT 260
95 #define tMONTH_UNIT 261
96 #define tHOUR_UNIT 262
97 #define tMINUTE_UNIT 263
98 #define tSEC_UNIT 264
99 #define tDAY_UNIT 265
100 #define tDAY_SHIFT 266
101 #define tDAY 267
102 #define tDAYZONE 268
103 #define tLOCAL_ZONE 269
104 #define tMERIDIAN 270
105 #define tMONTH 271
106 #define tORDINAL 272
107 #define tZONE 273
108 #define tSNUMBER 274
109 #define tUNUMBER 275
110 #define tSDECIMAL_NUMBER 276
111 #define tUDECIMAL_NUMBER 277
112
113
114
115
116 /* Copy the first part of user declarations.  */
117 #line 1 "parse-datetime.y"
118
119 /* Parse a string into an internal time stamp.
120
121    Copyright (C) 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
122    2010 Free Software Foundation, Inc.
123
124    This program is free software: you can redistribute it and/or modify
125    it under the terms of the GNU General Public License as published by
126    the Free Software Foundation; either version 3 of the License, or
127    (at your option) any later version.
128
129    This program is distributed in the hope that it will be useful,
130    but WITHOUT ANY WARRANTY; without even the implied warranty of
131    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132    GNU General Public License for more details.
133
134    You should have received a copy of the GNU General Public License
135    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
136
137 /* Originally written by Steven M. Bellovin <smb@research.att.com> while
138    at the University of North Carolina at Chapel Hill.  Later tweaked by
139    a couple of people on Usenet.  Completely overhauled by Rich $alz
140    <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990.
141
142    Modified by Paul Eggert <eggert@twinsun.com> in August 1999 to do
143    the right thing about local DST.  Also modified by Paul Eggert
144    <eggert@cs.ucla.edu> in February 2004 to support
145    nanosecond-resolution time stamps, and in October 2004 to support
146    TZ strings in dates.  */
147
148 /* FIXME: Check for arithmetic overflow in all cases, not just
149    some of them.  */
150
151 #include <config.h>
152
153 #include "parse-datetime.h"
154
155 #include "intprops.h"
156 #include "timespec.h"
157 #include "verify.h"
158
159 /* There's no need to extend the stack, so there's no need to involve
160    alloca.  */
161 #define YYSTACK_USE_ALLOCA 0
162
163 /* Tell Bison how much stack space is needed.  20 should be plenty for
164    this grammar, which is not right recursive.  Beware setting it too
165    high, since that might cause problems on machines whose
166    implementations have lame stack-overflow checking.  */
167 #define YYMAXDEPTH 20
168 #define YYINITDEPTH YYMAXDEPTH
169
170 /* Since the code of parse-datetime.y is not included in the Emacs executable
171    itself, there is no need to #define static in this file.  Even if
172    the code were included in the Emacs executable, it probably
173    wouldn't do any harm to #undef it here; this will only cause
174    problems if we try to write to a static variable, which I don't
175    think this code needs to do.  */
176 #ifdef emacs
177 # undef static
178 #endif
179
180 #include <c-ctype.h>
181 #include <limits.h>
182 #include <stdio.h>
183 #include <stdlib.h>
184 #include <string.h>
185
186 #include "xalloc.h"
187
188 /* Bison's skeleton tests _STDLIB_H, while some stdlib.h headers
189    use _STDLIB_H_ as witness.  Map the latter to the one bison uses.  */
190 /* FIXME: this is temporary.  Remove when we have a mechanism to ensure
191    that the version we're using is fixed, too.  */
192 #ifdef _STDLIB_H_
193 # undef _STDLIB_H
194 # define _STDLIB_H 1
195 #endif
196
197 /* ISDIGIT differs from isdigit, as follows:
198    - Its arg may be any int or unsigned int; it need not be an unsigned char
199      or EOF.
200    - It's typically faster.
201    POSIX says that only '0' through '9' are digits.  Prefer ISDIGIT to
202    isdigit unless it's important to use the locale's definition
203    of `digit' even when the host does not conform to POSIX.  */
204 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
205
206 /* Shift A right by B bits portably, by dividing A by 2**B and
207    truncating towards minus infinity.  A and B should be free of side
208    effects, and B should be in the range 0 <= B <= INT_BITS - 2, where
209    INT_BITS is the number of useful bits in an int.  GNU code can
210    assume that INT_BITS is at least 32.
211
212    ISO C99 says that A >> B is implementation-defined if A < 0.  Some
213    implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift
214    right in the usual way when A < 0, so SHR falls back on division if
215    ordinary A >> B doesn't seem to be the usual signed shift.  */
216 #define SHR(a, b)       \
217   (-1 >> 1 == -1        \
218    ? (a) >> (b)         \
219    : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0))
220
221 #define EPOCH_YEAR 1970
222 #define TM_YEAR_BASE 1900
223
224 #define HOUR(x) ((x) * 60)
225
226 /* long_time_t is a signed integer type that contains all time_t values.  */
227 verify (TYPE_IS_INTEGER (time_t));
228 #if TIME_T_FITS_IN_LONG_INT
229 typedef long int long_time_t;
230 #else
231 typedef time_t long_time_t;
232 #endif
233
234 /* Lots of this code assumes time_t and time_t-like values fit into
235    long_time_t.  */
236 verify (TYPE_MINIMUM (long_time_t) <= TYPE_MINIMUM (time_t)
237         && TYPE_MAXIMUM (time_t) <= TYPE_MAXIMUM (long_time_t));
238
239 /* FIXME: It also assumes that signed integer overflow silently wraps around,
240    but this is not true any more with recent versions of GCC 4.  */
241
242 /* An integer value, and the number of digits in its textual
243    representation.  */
244 typedef struct
245 {
246   bool negative;
247   long int value;
248   size_t digits;
249 } textint;
250
251 /* An entry in the lexical lookup table.  */
252 typedef struct
253 {
254   char const *name;
255   int type;
256   int value;
257 } table;
258
259 /* Meridian: am, pm, or 24-hour style.  */
260 enum { MERam, MERpm, MER24 };
261
262 enum { BILLION = 1000000000, LOG10_BILLION = 9 };
263
264 /* Relative times.  */
265 typedef struct
266 {
267   /* Relative year, month, day, hour, minutes, seconds, and nanoseconds.  */
268   long int year;
269   long int month;
270   long int day;
271   long int hour;
272   long int minutes;
273   long_time_t seconds;
274   long int ns;
275 } relative_time;
276
277 #if HAVE_COMPOUND_LITERALS
278 # define RELATIVE_TIME_0 ((relative_time) { 0, 0, 0, 0, 0, 0, 0 })
279 #else
280 static relative_time const RELATIVE_TIME_0;
281 #endif
282
283 /* Information passed to and from the parser.  */
284 typedef struct
285 {
286   /* The input string remaining to be parsed. */
287   const char *input;
288
289   /* N, if this is the Nth Tuesday.  */
290   long int day_ordinal;
291
292   /* Day of week; Sunday is 0.  */
293   int day_number;
294
295   /* tm_isdst flag for the local zone.  */
296   int local_isdst;
297
298   /* Time zone, in minutes east of UTC.  */
299   long int time_zone;
300
301   /* Style used for time.  */
302   int meridian;
303
304   /* Gregorian year, month, day, hour, minutes, seconds, and nanoseconds.  */
305   textint year;
306   long int month;
307   long int day;
308   long int hour;
309   long int minutes;
310   struct timespec seconds; /* includes nanoseconds */
311
312   /* Relative year, month, day, hour, minutes, seconds, and nanoseconds.  */
313   relative_time rel;
314
315   /* Presence or counts of nonterminals of various flavors parsed so far.  */
316   bool timespec_seen;
317   bool rels_seen;
318   size_t dates_seen;
319   size_t days_seen;
320   size_t local_zones_seen;
321   size_t dsts_seen;
322   size_t times_seen;
323   size_t zones_seen;
324
325   /* Table of local time zone abbrevations, terminated by a null entry.  */
326   table local_time_zone_table[3];
327 } parser_control;
328
329 union YYSTYPE;
330 static int yylex (union YYSTYPE *, parser_control *);
331 static int yyerror (parser_control const *, char const *);
332 static long int time_zone_hhmm (parser_control *, textint, long int);
333
334 /* Extract into *PC any date and time info from a string of digits
335    of the form e.g., YYYYMMDD, YYMMDD, HHMM, HH (and sometimes YYY,
336    YYYY, ...).  */
337 static void
338 digits_to_date_time (parser_control *pc, textint text_int)
339 {
340   if (pc->dates_seen && ! pc->year.digits
341       && ! pc->rels_seen && (pc->times_seen || 2 < text_int.digits))
342     pc->year = text_int;
343   else
344     {
345       if (4 < text_int.digits)
346         {
347           pc->dates_seen++;
348           pc->day = text_int.value % 100;
349           pc->month = (text_int.value / 100) % 100;
350           pc->year.value = text_int.value / 10000;
351           pc->year.digits = text_int.digits - 4;
352         }
353       else
354         {
355           pc->times_seen++;
356           if (text_int.digits <= 2)
357             {
358               pc->hour = text_int.value;
359               pc->minutes = 0;
360             }
361           else
362             {
363               pc->hour = text_int.value / 100;
364               pc->minutes = text_int.value % 100;
365             }
366           pc->seconds.tv_sec = 0;
367           pc->seconds.tv_nsec = 0;
368           pc->meridian = MER24;
369         }
370     }
371 }
372
373 /* Increment PC->rel by FACTOR * REL (FACTOR is 1 or -1).  */
374 static void
375 apply_relative_time (parser_control *pc, relative_time rel, int factor)
376 {
377   pc->rel.ns += factor * rel.ns;
378   pc->rel.seconds += factor * rel.seconds;
379   pc->rel.minutes += factor * rel.minutes;
380   pc->rel.hour += factor * rel.hour;
381   pc->rel.day += factor * rel.day;
382   pc->rel.month += factor * rel.month;
383   pc->rel.year += factor * rel.year;
384   pc->rels_seen = true;
385 }
386
387 /* Set PC-> hour, minutes, seconds and nanoseconds members from arguments.  */
388 static void
389 set_hhmmss (parser_control *pc, long int hour, long int minutes,
390             time_t sec, long int nsec)
391 {
392   pc->hour = hour;
393   pc->minutes = minutes;
394   pc->seconds.tv_sec = sec;
395   pc->seconds.tv_nsec = nsec;
396 }
397
398
399
400 /* Enabling traces.  */
401 #ifndef YYDEBUG
402 # define YYDEBUG 0
403 #endif
404
405 /* Enabling verbose error messages.  */
406 #ifdef YYERROR_VERBOSE
407 # undef YYERROR_VERBOSE
408 # define YYERROR_VERBOSE 1
409 #else
410 # define YYERROR_VERBOSE 0
411 #endif
412
413 /* Enabling the token table.  */
414 #ifndef YYTOKEN_TABLE
415 # define YYTOKEN_TABLE 0
416 #endif
417
418 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
419 typedef union YYSTYPE
420 #line 293 "parse-datetime.y"
421 {
422   long int intval;
423   textint textintval;
424   struct timespec timespec;
425   relative_time rel;
426 }
427 /* Line 187 of yacc.c.  */
428 #line 429 "parse-datetime.c"
429         YYSTYPE;
430 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
431 # define YYSTYPE_IS_DECLARED 1
432 # define YYSTYPE_IS_TRIVIAL 1
433 #endif
434
435
436
437 /* Copy the second part of user declarations.  */
438
439
440 /* Line 216 of yacc.c.  */
441 #line 442 "parse-datetime.c"
442
443 #ifdef short
444 # undef short
445 #endif
446
447 #ifdef YYTYPE_UINT8
448 typedef YYTYPE_UINT8 yytype_uint8;
449 #else
450 typedef unsigned char yytype_uint8;
451 #endif
452
453 #ifdef YYTYPE_INT8
454 typedef YYTYPE_INT8 yytype_int8;
455 #elif (defined __STDC__ || defined __C99__FUNC__ \
456      || defined __cplusplus || defined _MSC_VER)
457 typedef signed char yytype_int8;
458 #else
459 typedef short int yytype_int8;
460 #endif
461
462 #ifdef YYTYPE_UINT16
463 typedef YYTYPE_UINT16 yytype_uint16;
464 #else
465 typedef unsigned short int yytype_uint16;
466 #endif
467
468 #ifdef YYTYPE_INT16
469 typedef YYTYPE_INT16 yytype_int16;
470 #else
471 typedef short int yytype_int16;
472 #endif
473
474 #ifndef YYSIZE_T
475 # ifdef __SIZE_TYPE__
476 #  define YYSIZE_T __SIZE_TYPE__
477 # elif defined size_t
478 #  define YYSIZE_T size_t
479 # elif ! defined YYSIZE_T && (defined __STDC__ || defined __C99__FUNC__ \
480      || defined __cplusplus || defined _MSC_VER)
481 #  include <stddef.h> /* INFRINGES ON USER NAME SPACE */
482 #  define YYSIZE_T size_t
483 # else
484 #  define YYSIZE_T unsigned int
485 # endif
486 #endif
487
488 #define YYSIZE_MAXIMUM ((YYSIZE_T) -1)
489
490 #ifndef YY_
491 # if YYENABLE_NLS
492 #  if ENABLE_NLS
493 #   include <libintl.h> /* INFRINGES ON USER NAME SPACE */
494 #   define YY_(msgid) dgettext ("bison-runtime", msgid)
495 #  endif
496 # endif
497 # ifndef YY_
498 #  define YY_(msgid) msgid
499 # endif
500 #endif
501
502 /* Suppress unused-variable warnings by "using" E.  */
503 #if ! defined lint || defined __GNUC__
504 # define YYUSE(e) ((void) (e))
505 #else
506 # define YYUSE(e) /* empty */
507 #endif
508
509 /* Identity function, used to suppress warnings about constant conditions.  */
510 #ifndef lint
511 # define YYID(n) (n)
512 #else
513 #if (defined __STDC__ || defined __C99__FUNC__ \
514      || defined __cplusplus || defined _MSC_VER)
515 static int
516 YYID (int i)
517 #else
518 static int
519 YYID (i)
520     int i;
521 #endif
522 {
523   return i;
524 }
525 #endif
526
527 #if ! defined yyoverflow || YYERROR_VERBOSE
528
529 /* The parser invokes alloca or malloc; define the necessary symbols.  */
530
531 # ifdef YYSTACK_USE_ALLOCA
532 #  if YYSTACK_USE_ALLOCA
533 #   ifdef __GNUC__
534 #    define YYSTACK_ALLOC __builtin_alloca
535 #   elif defined __BUILTIN_VA_ARG_INCR
536 #    include <alloca.h> /* INFRINGES ON USER NAME SPACE */
537 #   elif defined _AIX
538 #    define YYSTACK_ALLOC __alloca
539 #   elif defined _MSC_VER
540 #    include <malloc.h> /* INFRINGES ON USER NAME SPACE */
541 #    define alloca _alloca
542 #   else
543 #    define YYSTACK_ALLOC alloca
544 #    if ! defined _ALLOCA_H && ! defined _STDLIB_H && (defined __STDC__ || defined __C99__FUNC__ \
545      || defined __cplusplus || defined _MSC_VER)
546 #     include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
547 #     ifndef _STDLIB_H
548 #      define _STDLIB_H 1
549 #     endif
550 #    endif
551 #   endif
552 #  endif
553 # endif
554
555 # ifdef YYSTACK_ALLOC
556    /* Pacify GCC's `empty if-body' warning.  */
557 #  define YYSTACK_FREE(Ptr) do { /* empty */; } while (YYID (0))
558 #  ifndef YYSTACK_ALLOC_MAXIMUM
559     /* The OS might guarantee only one guard page at the bottom of the stack,
560        and a page size can be as small as 4096 bytes.  So we cannot safely
561        invoke alloca (N) if N exceeds 4096.  Use a slightly smaller number
562        to allow for a few compiler-allocated temporary stack slots.  */
563 #   define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
564 #  endif
565 # else
566 #  define YYSTACK_ALLOC YYMALLOC
567 #  define YYSTACK_FREE YYFREE
568 #  ifndef YYSTACK_ALLOC_MAXIMUM
569 #   define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
570 #  endif
571 #  if (defined __cplusplus && ! defined _STDLIB_H \
572        && ! ((defined YYMALLOC || defined malloc) \
573              && (defined YYFREE || defined free)))
574 #   include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
575 #   ifndef _STDLIB_H
576 #    define _STDLIB_H 1
577 #   endif
578 #  endif
579 #  ifndef YYMALLOC
580 #   define YYMALLOC malloc
581 #   if ! defined malloc && ! defined _STDLIB_H && (defined __STDC__ || defined __C99__FUNC__ \
582      || defined __cplusplus || defined _MSC_VER)
583 void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
584 #   endif
585 #  endif
586 #  ifndef YYFREE
587 #   define YYFREE free
588 #   if ! defined free && ! defined _STDLIB_H && (defined __STDC__ || defined __C99__FUNC__ \
589      || defined __cplusplus || defined _MSC_VER)
590 void free (void *); /* INFRINGES ON USER NAME SPACE */
591 #   endif
592 #  endif
593 # endif
594 #endif /* ! defined yyoverflow || YYERROR_VERBOSE */
595
596
597 #if (! defined yyoverflow \
598      && (! defined __cplusplus \
599          || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL)))
600
601 /* A type that is properly aligned for any stack member.  */
602 union yyalloc
603 {
604   yytype_int16 yyss;
605   YYSTYPE yyvs;
606   };
607
608 /* The size of the maximum gap between one aligned stack and the next.  */
609 # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)
610
611 /* The size of an array large to enough to hold all stacks, each with
612    N elements.  */
613 # define YYSTACK_BYTES(N) \
614      ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \
615       + YYSTACK_GAP_MAXIMUM)
616
617 /* Copy COUNT objects from FROM to TO.  The source and destination do
618    not overlap.  */
619 # ifndef YYCOPY
620 #  if defined __GNUC__ && 1 < __GNUC__
621 #   define YYCOPY(To, From, Count) \
622       __builtin_memcpy (To, From, (Count) * sizeof (*(From)))
623 #  else
624 #   define YYCOPY(To, From, Count)              \
625       do                                        \
626         {                                       \
627           YYSIZE_T yyi;                         \
628           for (yyi = 0; yyi < (Count); yyi++)   \
629             (To)[yyi] = (From)[yyi];            \
630         }                                       \
631       while (YYID (0))
632 #  endif
633 # endif
634
635 /* Relocate STACK from its old location to the new one.  The
636    local variables YYSIZE and YYSTACKSIZE give the old and new number of
637    elements in the stack, and YYPTR gives the new location of the
638    stack.  Advance YYPTR to a properly aligned location for the next
639    stack.  */
640 # define YYSTACK_RELOCATE(Stack)                                        \
641     do                                                                  \
642       {                                                                 \
643         YYSIZE_T yynewbytes;                                            \
644         YYCOPY (&yyptr->Stack, Stack, yysize);                          \
645         Stack = &yyptr->Stack;                                          \
646         yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \
647         yyptr += yynewbytes / sizeof (*yyptr);                          \
648       }                                                                 \
649     while (YYID (0))
650
651 #endif
652
653 /* YYFINAL -- State number of the termination state.  */
654 #define YYFINAL  12
655 /* YYLAST -- Last index in YYTABLE.  */
656 #define YYLAST   98
657
658 /* YYNTOKENS -- Number of terminals.  */
659 #define YYNTOKENS  27
660 /* YYNNTS -- Number of nonterminals.  */
661 #define YYNNTS  21
662 /* YYNRULES -- Number of rules.  */
663 #define YYNRULES  82
664 /* YYNRULES -- Number of states.  */
665 #define YYNSTATES  100
666
667 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX.  */
668 #define YYUNDEFTOK  2
669 #define YYMAXUTOK   277
670
671 #define YYTRANSLATE(YYX)                                                \
672   ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
673
674 /* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX.  */
675 static const yytype_uint8 yytranslate[] =
676 {
677        0,     2,     2,     2,     2,     2,     2,     2,     2,     2,
678        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
679        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
680        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
681        2,     2,     2,     2,    25,     2,     2,    26,     2,     2,
682        2,     2,     2,     2,     2,     2,     2,     2,    24,     2,
683        2,     2,     2,     2,    23,     2,     2,     2,     2,     2,
684        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
685        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
686        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
687        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
688        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
689        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
690        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
691        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
692        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
693        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
694        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
695        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
696        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
697        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
698        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
699        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
700        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
701        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
702        2,     2,     2,     2,     2,     2,     1,     2,     3,     4,
703        5,     6,     7,     8,     9,    10,    11,    12,    13,    14,
704       15,    16,    17,    18,    19,    20,    21,    22
705 };
706
707 #if YYDEBUG
708 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
709    YYRHS.  */
710 static const yytype_uint8 yyprhs[] =
711 {
712        0,     0,     3,     5,     7,    10,    11,    14,    16,    18,
713       20,    22,    24,    26,    28,    30,    33,    38,    44,    51,
714       59,    61,    64,    66,    69,    73,    75,    78,    80,    83,
715       86,    89,    93,    99,   103,   107,   111,   114,   119,   122,
716      126,   129,   131,   133,   136,   139,   141,   144,   147,   149,
717      152,   155,   157,   160,   163,   165,   168,   171,   173,   176,
718      179,   182,   185,   187,   189,   192,   195,   198,   201,   204,
719      207,   209,   211,   213,   215,   217,   219,   221,   223,   226,
720      227,   230,   231
721 };
722
723 /* YYRHS -- A `-1'-separated list of the rules' RHS.  */
724 static const yytype_int8 yyrhs[] =
725 {
726       28,     0,    -1,    29,    -1,    30,    -1,    23,    41,    -1,
727       -1,    30,    31,    -1,    32,    -1,    33,    -1,    34,    -1,
728       36,    -1,    35,    -1,    37,    -1,    44,    -1,    45,    -1,
729       20,    15,    -1,    20,    24,    20,    47,    -1,    20,    24,
730       20,    19,    46,    -1,    20,    24,    20,    24,    43,    47,
731       -1,    20,    24,    20,    24,    43,    19,    46,    -1,    14,
732       -1,    14,     4,    -1,    18,    -1,    18,    39,    -1,    18,
733       19,    46,    -1,    13,    -1,    18,     4,    -1,    12,    -1,
734       12,    25,    -1,    17,    12,    -1,    20,    12,    -1,    20,
735       26,    20,    -1,    20,    26,    20,    26,    20,    -1,    20,
736       19,    19,    -1,    20,    16,    19,    -1,    16,    19,    19,
737       -1,    16,    20,    -1,    16,    20,    25,    20,    -1,    20,
738       16,    -1,    20,    16,    20,    -1,    38,     3,    -1,    38,
739       -1,    40,    -1,    17,     5,    -1,    20,     5,    -1,     5,
740       -1,    17,     6,    -1,    20,     6,    -1,     6,    -1,    17,
741       10,    -1,    20,    10,    -1,    10,    -1,    17,     7,    -1,
742       20,     7,    -1,     7,    -1,    17,     8,    -1,    20,     8,
743       -1,     8,    -1,    17,     9,    -1,    20,     9,    -1,    21,
744        9,    -1,    22,     9,    -1,     9,    -1,    39,    -1,    19,
745        5,    -1,    19,     6,    -1,    19,    10,    -1,    19,     7,
746       -1,    19,     8,    -1,    19,     9,    -1,    11,    -1,    42,
747       -1,    43,    -1,    21,    -1,    19,    -1,    22,    -1,    20,
748       -1,    20,    -1,    20,    39,    -1,    -1,    24,    20,    -1,
749       -1,    15,    -1
750 };
751
752 /* YYRLINE[YYN] -- source line where rule number YYN was defined.  */
753 static const yytype_uint16 yyrline[] =
754 {
755        0,   319,   319,   320,   324,   331,   333,   337,   339,   341,
756      343,   345,   347,   348,   349,   353,   358,   363,   370,   375,
757      385,   390,   398,   400,   403,   405,   407,   412,   417,   422,
758      427,   435,   440,   460,   467,   475,   483,   488,   494,   499,
759      508,   510,   512,   517,   519,   521,   523,   525,   527,   529,
760      531,   533,   535,   537,   539,   541,   543,   545,   547,   549,
761      551,   553,   555,   557,   561,   563,   565,   567,   569,   571,
762      576,   580,   580,   583,   584,   589,   590,   595,   600,   611,
763      612,   618,   619
764 };
765 #endif
766
767 #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
768 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
769    First, the terminals, then, starting at YYNTOKENS, nonterminals.  */
770 static const char *const yytname[] =
771 {
772   "$end", "error", "$undefined", "tAGO", "tDST", "tYEAR_UNIT",
773   "tMONTH_UNIT", "tHOUR_UNIT", "tMINUTE_UNIT", "tSEC_UNIT", "tDAY_UNIT",
774   "tDAY_SHIFT", "tDAY", "tDAYZONE", "tLOCAL_ZONE", "tMERIDIAN", "tMONTH",
775   "tORDINAL", "tZONE", "tSNUMBER", "tUNUMBER", "tSDECIMAL_NUMBER",
776   "tUDECIMAL_NUMBER", "'@'", "':'", "','", "'/'", "$accept", "spec",
777   "timespec", "items", "item", "time", "local_zone", "zone", "day", "date",
778   "rel", "relunit", "relunit_snumber", "dayshift", "seconds",
779   "signed_seconds", "unsigned_seconds", "number", "hybrid",
780   "o_colon_minutes", "o_merid", 0
781 };
782 #endif
783
784 # ifdef YYPRINT
785 /* YYTOKNUM[YYLEX-NUM] -- Internal token number corresponding to
786    token YYLEX-NUM.  */
787 static const yytype_uint16 yytoknum[] =
788 {
789        0,   256,   257,   258,   259,   260,   261,   262,   263,   264,
790      265,   266,   267,   268,   269,   270,   271,   272,   273,   274,
791      275,   276,   277,    64,    58,    44,    47
792 };
793 # endif
794
795 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
796 static const yytype_uint8 yyr1[] =
797 {
798        0,    27,    28,    28,    29,    30,    30,    31,    31,    31,
799       31,    31,    31,    31,    31,    32,    32,    32,    32,    32,
800       33,    33,    34,    34,    34,    34,    34,    35,    35,    35,
801       35,    36,    36,    36,    36,    36,    36,    36,    36,    36,
802       37,    37,    37,    38,    38,    38,    38,    38,    38,    38,
803       38,    38,    38,    38,    38,    38,    38,    38,    38,    38,
804       38,    38,    38,    38,    39,    39,    39,    39,    39,    39,
805       40,    41,    41,    42,    42,    43,    43,    44,    45,    46,
806       46,    47,    47
807 };
808
809 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN.  */
810 static const yytype_uint8 yyr2[] =
811 {
812        0,     2,     1,     1,     2,     0,     2,     1,     1,     1,
813        1,     1,     1,     1,     1,     2,     4,     5,     6,     7,
814        1,     2,     1,     2,     3,     1,     2,     1,     2,     2,
815        2,     3,     5,     3,     3,     3,     2,     4,     2,     3,
816        2,     1,     1,     2,     2,     1,     2,     2,     1,     2,
817        2,     1,     2,     2,     1,     2,     2,     1,     2,     2,
818        2,     2,     1,     1,     2,     2,     2,     2,     2,     2,
819        1,     1,     1,     1,     1,     1,     1,     1,     2,     0,
820        2,     0,     1
821 };
822
823 /* YYDEFACT[STATE-NAME] -- Default rule to reduce with in state
824    STATE-NUM when YYTABLE doesn't specify something else to do.  Zero
825    means the default is an error.  */
826 static const yytype_uint8 yydefact[] =
827 {
828        5,     0,     0,     2,     3,    74,    76,    73,    75,     4,
829       71,    72,     1,    45,    48,    54,    57,    62,    51,    70,
830       27,    25,    20,     0,     0,    22,     0,    77,     0,     0,
831        6,     7,     8,     9,    11,    10,    12,    41,    63,    42,
832       13,    14,    28,    21,     0,    36,    43,    46,    52,    55,
833       58,    49,    29,    26,    79,    23,    64,    65,    67,    68,
834       69,    66,    44,    47,    53,    56,    59,    50,    30,    15,
835       38,     0,     0,     0,    78,    60,    61,    40,    35,     0,
836        0,    24,    34,    39,    33,    81,    31,    37,    80,    82,
837       79,     0,    16,     0,    17,    81,    32,    79,    18,    19
838 };
839
840 /* YYDEFGOTO[NTERM-NUM].  */
841 static const yytype_int8 yydefgoto[] =
842 {
843       -1,     2,     3,     4,    30,    31,    32,    33,    34,    35,
844       36,    37,    38,    39,     9,    10,    11,    40,    41,    81,
845       92
846 };
847
848 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
849    STATE-NUM.  */
850 #define YYPACT_NINF -82
851 static const yytype_int8 yypact[] =
852 {
853      -17,    56,    15,   -82,    26,   -82,   -82,   -82,   -82,   -82,
854      -82,   -82,   -82,   -82,   -82,   -82,   -82,   -82,   -82,   -82,
855       36,   -82,    68,    10,    50,     9,    59,    -5,    72,    73,
856      -82,   -82,   -82,   -82,   -82,   -82,   -82,    80,   -82,   -82,
857      -82,   -82,   -82,   -82,    65,    61,   -82,   -82,   -82,   -82,
858      -82,   -82,   -82,   -82,    17,   -82,   -82,   -82,   -82,   -82,
859      -82,   -82,   -82,   -82,   -82,   -82,   -82,   -82,   -82,   -82,
860       60,    44,    67,    69,   -82,   -82,   -82,   -82,   -82,    70,
861       71,   -82,   -82,   -82,   -82,    -7,    62,   -82,   -82,   -82,
862       74,    -2,   -82,    75,   -82,    55,   -82,    74,   -82,   -82
863 };
864
865 /* YYPGOTO[NTERM-NUM].  */
866 static const yytype_int8 yypgoto[] =
867 {
868      -82,   -82,   -82,   -82,   -82,   -82,   -82,   -82,   -82,   -82,
869      -82,   -82,    46,   -82,   -82,   -82,    -6,   -82,   -82,   -81,
870       -3
871 };
872
873 /* YYTABLE[YYPACT[STATE-NUM]].  What to do in state STATE-NUM.  If
874    positive, shift that token.  If negative, reduce the rule which
875    number is the opposite.  If zero, do what YYDEFACT says.
876    If YYTABLE_NINF, syntax error.  */
877 #define YYTABLE_NINF -1
878 static const yytype_uint8 yytable[] =
879 {
880       62,    63,    64,    65,    66,    67,     1,    68,    89,    94,
881       69,    70,    90,    53,    71,    12,    99,    91,     6,    72,
882        8,    73,    56,    57,    58,    59,    60,    61,    54,    44,
883       45,    13,    14,    15,    16,    17,    18,    19,    20,    21,
884       22,    80,    23,    24,    25,    26,    27,    28,    29,    56,
885       57,    58,    59,    60,    61,    46,    47,    48,    49,    50,
886       51,    42,    52,    84,    56,    57,    58,    59,    60,    61,
887       89,    55,    43,    74,    97,     5,     6,     7,     8,    82,
888       83,    75,    76,    77,    78,    95,    79,    85,    93,    86,
889       87,    88,    98,     0,     0,    96,     0,     0,    80
890 };
891
892 static const yytype_int8 yycheck[] =
893 {
894        5,     6,     7,     8,     9,    10,    23,    12,    15,    90,
895       15,    16,    19,     4,    19,     0,    97,    24,    20,    24,
896       22,    26,     5,     6,     7,     8,     9,    10,    19,    19,
897       20,     5,     6,     7,     8,     9,    10,    11,    12,    13,
898       14,    24,    16,    17,    18,    19,    20,    21,    22,     5,
899        6,     7,     8,     9,    10,     5,     6,     7,     8,     9,
900       10,    25,    12,    19,     5,     6,     7,     8,     9,    10,
901       15,    25,     4,    27,    19,    19,    20,    21,    22,    19,
902       20,     9,     9,     3,    19,    91,    25,    20,    26,    20,
903       20,    20,    95,    -1,    -1,    20,    -1,    -1,    24
904 };
905
906 /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
907    symbol of state STATE-NUM.  */
908 static const yytype_uint8 yystos[] =
909 {
910        0,    23,    28,    29,    30,    19,    20,    21,    22,    41,
911       42,    43,     0,     5,     6,     7,     8,     9,    10,    11,
912       12,    13,    14,    16,    17,    18,    19,    20,    21,    22,
913       31,    32,    33,    34,    35,    36,    37,    38,    39,    40,
914       44,    45,    25,     4,    19,    20,     5,     6,     7,     8,
915        9,    10,    12,     4,    19,    39,     5,     6,     7,     8,
916        9,    10,     5,     6,     7,     8,     9,    10,    12,    15,
917       16,    19,    24,    26,    39,     9,     9,     3,    19,    25,
918       24,    46,    19,    20,    19,    20,    20,    20,    20,    15,
919       19,    24,    47,    26,    46,    43,    20,    19,    47,    46
920 };
921
922 #define yyerrok         (yyerrstatus = 0)
923 #define yyclearin       (yychar = YYEMPTY)
924 #define YYEMPTY         (-2)
925 #define YYEOF           0
926
927 #define YYACCEPT        goto yyacceptlab
928 #define YYABORT         goto yyabortlab
929 #define YYERROR         goto yyerrorlab
930
931
932 /* Like YYERROR except do call yyerror.  This remains here temporarily
933    to ease the transition to the new meaning of YYERROR, for GCC.
934    Once GCC version 2 has supplanted version 1, this can go.  */
935
936 #define YYFAIL          goto yyerrlab
937
938 #define YYRECOVERING()  (!!yyerrstatus)
939
940 #define YYBACKUP(Token, Value)                                  \
941 do                                                              \
942   if (yychar == YYEMPTY && yylen == 1)                          \
943     {                                                           \
944       yychar = (Token);                                         \
945       yylval = (Value);                                         \
946       yytoken = YYTRANSLATE (yychar);                           \
947       YYPOPSTACK (1);                                           \
948       goto yybackup;                                            \
949     }                                                           \
950   else                                                          \
951     {                                                           \
952       yyerror (pc, YY_("syntax error: cannot back up")); \
953       YYERROR;                                                  \
954     }                                                           \
955 while (YYID (0))
956
957
958 #define YYTERROR        1
959 #define YYERRCODE       256
960
961
962 /* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
963    If N is 0, then set CURRENT to the empty location which ends
964    the previous symbol: RHS[0] (always defined).  */
965
966 #define YYRHSLOC(Rhs, K) ((Rhs)[K])
967 #ifndef YYLLOC_DEFAULT
968 # define YYLLOC_DEFAULT(Current, Rhs, N)                                \
969     do                                                                  \
970       if (YYID (N))                                                    \
971         {                                                               \
972           (Current).first_line   = YYRHSLOC (Rhs, 1).first_line;        \
973           (Current).first_column = YYRHSLOC (Rhs, 1).first_column;      \
974           (Current).last_line    = YYRHSLOC (Rhs, N).last_line;         \
975           (Current).last_column  = YYRHSLOC (Rhs, N).last_column;       \
976         }                                                               \
977       else                                                              \
978         {                                                               \
979           (Current).first_line   = (Current).last_line   =              \
980             YYRHSLOC (Rhs, 0).last_line;                                \
981           (Current).first_column = (Current).last_column =              \
982             YYRHSLOC (Rhs, 0).last_column;                              \
983         }                                                               \
984     while (YYID (0))
985 #endif
986
987
988 /* YY_LOCATION_PRINT -- Print the location on the stream.
989    This macro was not mandated originally: define only if we know
990    we won't break user code: when these are the locations we know.  */
991
992 #ifndef YY_LOCATION_PRINT
993 # if YYLTYPE_IS_TRIVIAL
994 #  define YY_LOCATION_PRINT(File, Loc)                  \
995      fprintf (File, "%d.%d-%d.%d",                      \
996               (Loc).first_line, (Loc).first_column,     \
997               (Loc).last_line,  (Loc).last_column)
998 # else
999 #  define YY_LOCATION_PRINT(File, Loc) ((void) 0)
1000 # endif
1001 #endif
1002
1003
1004 /* YYLEX -- calling `yylex' with the right arguments.  */
1005
1006 #ifdef YYLEX_PARAM
1007 # define YYLEX yylex (&yylval, YYLEX_PARAM)
1008 #else
1009 # define YYLEX yylex (&yylval, pc)
1010 #endif
1011
1012 /* Enable debugging if requested.  */
1013 #if YYDEBUG
1014
1015 # ifndef YYFPRINTF
1016 #  include <stdio.h> /* INFRINGES ON USER NAME SPACE */
1017 #  define YYFPRINTF fprintf
1018 # endif
1019
1020 # define YYDPRINTF(Args)                        \
1021 do {                                            \
1022   if (yydebug)                                  \
1023     YYFPRINTF Args;                             \
1024 } while (YYID (0))
1025
1026 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)                    \
1027 do {                                                                      \
1028   if (yydebug)                                                            \
1029     {                                                                     \
1030       YYFPRINTF (stderr, "%s ", Title);                                   \
1031       yy_symbol_print (stderr,                                            \
1032                   Type, Value, pc); \
1033       YYFPRINTF (stderr, "\n");                                           \
1034     }                                                                     \
1035 } while (YYID (0))
1036
1037
1038 /*--------------------------------.
1039 | Print this symbol on YYOUTPUT.  |
1040 `--------------------------------*/
1041
1042 /*ARGSUSED*/
1043 #if (defined __STDC__ || defined __C99__FUNC__ \
1044      || defined __cplusplus || defined _MSC_VER)
1045 static void
1046 yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep, parser_control *pc)
1047 #else
1048 static void
1049 yy_symbol_value_print (yyoutput, yytype, yyvaluep, pc)
1050     FILE *yyoutput;
1051     int yytype;
1052     YYSTYPE const * const yyvaluep;
1053     parser_control *pc;
1054 #endif
1055 {
1056   if (!yyvaluep)
1057     return;
1058   YYUSE (pc);
1059 # ifdef YYPRINT
1060   if (yytype < YYNTOKENS)
1061     YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
1062 # else
1063   YYUSE (yyoutput);
1064 # endif
1065   switch (yytype)
1066     {
1067       default:
1068         break;
1069     }
1070 }
1071
1072
1073 /*--------------------------------.
1074 | Print this symbol on YYOUTPUT.  |
1075 `--------------------------------*/
1076
1077 #if (defined __STDC__ || defined __C99__FUNC__ \
1078      || defined __cplusplus || defined _MSC_VER)
1079 static void
1080 yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep, parser_control *pc)
1081 #else
1082 static void
1083 yy_symbol_print (yyoutput, yytype, yyvaluep, pc)
1084     FILE *yyoutput;
1085     int yytype;
1086     YYSTYPE const * const yyvaluep;
1087     parser_control *pc;
1088 #endif
1089 {
1090   if (yytype < YYNTOKENS)
1091     YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
1092   else
1093     YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
1094
1095   yy_symbol_value_print (yyoutput, yytype, yyvaluep, pc);
1096   YYFPRINTF (yyoutput, ")");
1097 }
1098
1099 /*------------------------------------------------------------------.
1100 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
1101 | TOP (included).                                                   |
1102 `------------------------------------------------------------------*/
1103
1104 #if (defined __STDC__ || defined __C99__FUNC__ \
1105      || defined __cplusplus || defined _MSC_VER)
1106 static void
1107 yy_stack_print (yytype_int16 *bottom, yytype_int16 *top)
1108 #else
1109 static void
1110 yy_stack_print (bottom, top)
1111     yytype_int16 *bottom;
1112     yytype_int16 *top;
1113 #endif
1114 {
1115   YYFPRINTF (stderr, "Stack now");
1116   for (; bottom <= top; ++bottom)
1117     YYFPRINTF (stderr, " %d", *bottom);
1118   YYFPRINTF (stderr, "\n");
1119 }
1120
1121 # define YY_STACK_PRINT(Bottom, Top)                            \
1122 do {                                                            \
1123   if (yydebug)                                                  \
1124     yy_stack_print ((Bottom), (Top));                           \
1125 } while (YYID (0))
1126
1127
1128 /*------------------------------------------------.
1129 | Report that the YYRULE is going to be reduced.  |
1130 `------------------------------------------------*/
1131
1132 #if (defined __STDC__ || defined __C99__FUNC__ \
1133      || defined __cplusplus || defined _MSC_VER)
1134 static void
1135 yy_reduce_print (YYSTYPE *yyvsp, int yyrule, parser_control *pc)
1136 #else
1137 static void
1138 yy_reduce_print (yyvsp, yyrule, pc)
1139     YYSTYPE *yyvsp;
1140     int yyrule;
1141     parser_control *pc;
1142 #endif
1143 {
1144   int yynrhs = yyr2[yyrule];
1145   int yyi;
1146   unsigned long int yylno = yyrline[yyrule];
1147   YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n",
1148              yyrule - 1, yylno);
1149   /* The symbols being reduced.  */
1150   for (yyi = 0; yyi < yynrhs; yyi++)
1151     {
1152       fprintf (stderr, "   $%d = ", yyi + 1);
1153       yy_symbol_print (stderr, yyrhs[yyprhs[yyrule] + yyi],
1154                        &(yyvsp[(yyi + 1) - (yynrhs)])
1155                                        , pc);
1156       fprintf (stderr, "\n");
1157     }
1158 }
1159
1160 # define YY_REDUCE_PRINT(Rule)          \
1161 do {                                    \
1162   if (yydebug)                          \
1163     yy_reduce_print (yyvsp, Rule, pc); \
1164 } while (YYID (0))
1165
1166 /* Nonzero means print parse trace.  It is left uninitialized so that
1167    multiple parsers can coexist.  */
1168 int yydebug;
1169 #else /* !YYDEBUG */
1170 # define YYDPRINTF(Args)
1171 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
1172 # define YY_STACK_PRINT(Bottom, Top)
1173 # define YY_REDUCE_PRINT(Rule)
1174 #endif /* !YYDEBUG */
1175
1176
1177 /* YYINITDEPTH -- initial size of the parser's stacks.  */
1178 #ifndef YYINITDEPTH
1179 # define YYINITDEPTH 200
1180 #endif
1181
1182 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
1183    if the built-in stack extension method is used).
1184
1185    Do not make this value too large; the results are undefined if
1186    YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH)
1187    evaluated with infinite-precision integer arithmetic.  */
1188
1189 #ifndef YYMAXDEPTH
1190 # define YYMAXDEPTH 10000
1191 #endif
1192
1193 \f
1194
1195 #if YYERROR_VERBOSE
1196
1197 # ifndef yystrlen
1198 #  if defined __GLIBC__ && defined _STRING_H
1199 #   define yystrlen strlen
1200 #  else
1201 /* Return the length of YYSTR.  */
1202 #if (defined __STDC__ || defined __C99__FUNC__ \
1203      || defined __cplusplus || defined _MSC_VER)
1204 static YYSIZE_T
1205 yystrlen (const char *yystr)
1206 #else
1207 static YYSIZE_T
1208 yystrlen (yystr)
1209     const char *yystr;
1210 #endif
1211 {
1212   YYSIZE_T yylen;
1213   for (yylen = 0; yystr[yylen]; yylen++)
1214     continue;
1215   return yylen;
1216 }
1217 #  endif
1218 # endif
1219
1220 # ifndef yystpcpy
1221 #  if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE
1222 #   define yystpcpy stpcpy
1223 #  else
1224 /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
1225    YYDEST.  */
1226 #if (defined __STDC__ || defined __C99__FUNC__ \
1227      || defined __cplusplus || defined _MSC_VER)
1228 static char *
1229 yystpcpy (char *yydest, const char *yysrc)
1230 #else
1231 static char *
1232 yystpcpy (yydest, yysrc)
1233     char *yydest;
1234     const char *yysrc;
1235 #endif
1236 {
1237   char *yyd = yydest;
1238   const char *yys = yysrc;
1239
1240   while ((*yyd++ = *yys++) != '\0')
1241     continue;
1242
1243   return yyd - 1;
1244 }
1245 #  endif
1246 # endif
1247
1248 # ifndef yytnamerr
1249 /* Copy to YYRES the contents of YYSTR after stripping away unnecessary
1250    quotes and backslashes, so that it's suitable for yyerror.  The
1251    heuristic is that double-quoting is unnecessary unless the string
1252    contains an apostrophe, a comma, or backslash (other than
1253    backslash-backslash).  YYSTR is taken from yytname.  If YYRES is
1254    null, do not copy; instead, return the length of what the result
1255    would have been.  */
1256 static YYSIZE_T
1257 yytnamerr (char *yyres, const char *yystr)
1258 {
1259   if (*yystr == '"')
1260     {
1261       YYSIZE_T yyn = 0;
1262       char const *yyp = yystr;
1263
1264       for (;;)
1265         switch (*++yyp)
1266           {
1267           case '\'':
1268           case ',':
1269             goto do_not_strip_quotes;
1270
1271           case '\\':
1272             if (*++yyp != '\\')
1273               goto do_not_strip_quotes;
1274             /* Fall through.  */
1275           default:
1276             if (yyres)
1277               yyres[yyn] = *yyp;
1278             yyn++;
1279             break;
1280
1281           case '"':
1282             if (yyres)
1283               yyres[yyn] = '\0';
1284             return yyn;
1285           }
1286     do_not_strip_quotes: ;
1287     }
1288
1289   if (! yyres)
1290     return yystrlen (yystr);
1291
1292   return yystpcpy (yyres, yystr) - yyres;
1293 }
1294 # endif
1295
1296 /* Copy into YYRESULT an error message about the unexpected token
1297    YYCHAR while in state YYSTATE.  Return the number of bytes copied,
1298    including the terminating null byte.  If YYRESULT is null, do not
1299    copy anything; just return the number of bytes that would be
1300    copied.  As a special case, return 0 if an ordinary "syntax error"
1301    message will do.  Return YYSIZE_MAXIMUM if overflow occurs during
1302    size calculation.  */
1303 static YYSIZE_T
1304 yysyntax_error (char *yyresult, int yystate, int yychar)
1305 {
1306   int yyn = yypact[yystate];
1307
1308   if (! (YYPACT_NINF < yyn && yyn <= YYLAST))
1309     return 0;
1310   else
1311     {
1312       int yytype = YYTRANSLATE (yychar);
1313       YYSIZE_T yysize0 = yytnamerr (0, yytname[yytype]);
1314       YYSIZE_T yysize = yysize0;
1315       YYSIZE_T yysize1;
1316       int yysize_overflow = 0;
1317       enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
1318       char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
1319       int yyx;
1320
1321 # if 0
1322       /* This is so xgettext sees the translatable formats that are
1323          constructed on the fly.  */
1324       YY_("syntax error, unexpected %s");
1325       YY_("syntax error, unexpected %s, expecting %s");
1326       YY_("syntax error, unexpected %s, expecting %s or %s");
1327       YY_("syntax error, unexpected %s, expecting %s or %s or %s");
1328       YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s");
1329 # endif
1330       char *yyfmt;
1331       char const *yyf;
1332       static char const yyunexpected[] = "syntax error, unexpected %s";
1333       static char const yyexpecting[] = ", expecting %s";
1334       static char const yyor[] = " or %s";
1335       char yyformat[sizeof yyunexpected
1336                     + sizeof yyexpecting - 1
1337                     + ((YYERROR_VERBOSE_ARGS_MAXIMUM - 2)
1338                        * (sizeof yyor - 1))];
1339       char const *yyprefix = yyexpecting;
1340
1341       /* Start YYX at -YYN if negative to avoid negative indexes in
1342          YYCHECK.  */
1343       int yyxbegin = yyn < 0 ? -yyn : 0;
1344
1345       /* Stay within bounds of both yycheck and yytname.  */
1346       int yychecklim = YYLAST - yyn + 1;
1347       int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
1348       int yycount = 1;
1349
1350       yyarg[0] = yytname[yytype];
1351       yyfmt = yystpcpy (yyformat, yyunexpected);
1352
1353       for (yyx = yyxbegin; yyx < yyxend; ++yyx)
1354         if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
1355           {
1356             if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
1357               {
1358                 yycount = 1;
1359                 yysize = yysize0;
1360                 yyformat[sizeof yyunexpected - 1] = '\0';
1361                 break;
1362               }
1363             yyarg[yycount++] = yytname[yyx];
1364             yysize1 = yysize + yytnamerr (0, yytname[yyx]);
1365             yysize_overflow |= (yysize1 < yysize);
1366             yysize = yysize1;
1367             yyfmt = yystpcpy (yyfmt, yyprefix);
1368             yyprefix = yyor;
1369           }
1370
1371       yyf = YY_(yyformat);
1372       yysize1 = yysize + yystrlen (yyf);
1373       yysize_overflow |= (yysize1 < yysize);
1374       yysize = yysize1;
1375
1376       if (yysize_overflow)
1377         return YYSIZE_MAXIMUM;
1378
1379       if (yyresult)
1380         {
1381           /* Avoid sprintf, as that infringes on the user's name space.
1382              Don't have undefined behavior even if the translation
1383              produced a string with the wrong number of "%s"s.  */
1384           char *yyp = yyresult;
1385           int yyi = 0;
1386           while ((*yyp = *yyf) != '\0')
1387             {
1388               if (*yyp == '%' && yyf[1] == 's' && yyi < yycount)
1389                 {
1390                   yyp += yytnamerr (yyp, yyarg[yyi++]);
1391                   yyf += 2;
1392                 }
1393               else
1394                 {
1395                   yyp++;
1396                   yyf++;
1397                 }
1398             }
1399         }
1400       return yysize;
1401     }
1402 }
1403 #endif /* YYERROR_VERBOSE */
1404 \f
1405
1406 /*-----------------------------------------------.
1407 | Release the memory associated to this symbol.  |
1408 `-----------------------------------------------*/
1409
1410 /*ARGSUSED*/
1411 #if (defined __STDC__ || defined __C99__FUNC__ \
1412      || defined __cplusplus || defined _MSC_VER)
1413 static void
1414 yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep, parser_control *pc)
1415 #else
1416 static void
1417 yydestruct (yymsg, yytype, yyvaluep, pc)
1418     const char *yymsg;
1419     int yytype;
1420     YYSTYPE *yyvaluep;
1421     parser_control *pc;
1422 #endif
1423 {
1424   YYUSE (yyvaluep);
1425   YYUSE (pc);
1426
1427   if (!yymsg)
1428     yymsg = "Deleting";
1429   YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
1430
1431   switch (yytype)
1432     {
1433
1434       default:
1435         break;
1436     }
1437 }
1438 \f
1439
1440 /* Prevent warnings from -Wmissing-prototypes.  */
1441
1442 #ifdef YYPARSE_PARAM
1443 #if defined __STDC__ || defined __cplusplus
1444 int yyparse (void *YYPARSE_PARAM);
1445 #else
1446 int yyparse ();
1447 #endif
1448 #else /* ! YYPARSE_PARAM */
1449 #if defined __STDC__ || defined __cplusplus
1450 int yyparse (parser_control *pc);
1451 #else
1452 int yyparse ();
1453 #endif
1454 #endif /* ! YYPARSE_PARAM */
1455
1456
1457
1458
1459
1460
1461 /*----------.
1462 | yyparse.  |
1463 `----------*/
1464
1465 #ifdef YYPARSE_PARAM
1466 #if (defined __STDC__ || defined __C99__FUNC__ \
1467      || defined __cplusplus || defined _MSC_VER)
1468 int
1469 yyparse (void *YYPARSE_PARAM)
1470 #else
1471 int
1472 yyparse (YYPARSE_PARAM)
1473     void *YYPARSE_PARAM;
1474 #endif
1475 #else /* ! YYPARSE_PARAM */
1476 #if (defined __STDC__ || defined __C99__FUNC__ \
1477      || defined __cplusplus || defined _MSC_VER)
1478 int
1479 yyparse (parser_control *pc)
1480 #else
1481 int
1482 yyparse (pc)
1483     parser_control *pc;
1484 #endif
1485 #endif
1486 {
1487   /* The look-ahead symbol.  */
1488 int yychar;
1489
1490 /* The semantic value of the look-ahead symbol.  */
1491 YYSTYPE yylval;
1492
1493 /* Number of syntax errors so far.  */
1494 int yynerrs;
1495
1496   int yystate;
1497   int yyn;
1498   int yyresult;
1499   /* Number of tokens to shift before error messages enabled.  */
1500   int yyerrstatus;
1501   /* Look-ahead token as an internal (translated) token number.  */
1502   int yytoken = 0;
1503 #if YYERROR_VERBOSE
1504   /* Buffer for error messages, and its allocated size.  */
1505   char yymsgbuf[128];
1506   char *yymsg = yymsgbuf;
1507   YYSIZE_T yymsg_alloc = sizeof yymsgbuf;
1508 #endif
1509
1510   /* Three stacks and their tools:
1511      `yyss': related to states,
1512      `yyvs': related to semantic values,
1513      `yyls': related to locations.
1514
1515      Refer to the stacks thru separate pointers, to allow yyoverflow
1516      to reallocate them elsewhere.  */
1517
1518   /* The state stack.  */
1519   yytype_int16 yyssa[YYINITDEPTH];
1520   yytype_int16 *yyss = yyssa;
1521   yytype_int16 *yyssp;
1522
1523   /* The semantic value stack.  */
1524   YYSTYPE yyvsa[YYINITDEPTH];
1525   YYSTYPE *yyvs = yyvsa;
1526   YYSTYPE *yyvsp;
1527
1528
1529
1530 #define YYPOPSTACK(N)   (yyvsp -= (N), yyssp -= (N))
1531
1532   YYSIZE_T yystacksize = YYINITDEPTH;
1533
1534   /* The variables used to return semantic value and location from the
1535      action routines.  */
1536   YYSTYPE yyval;
1537
1538
1539   /* The number of symbols on the RHS of the reduced rule.
1540      Keep to zero when no symbol should be popped.  */
1541   int yylen = 0;
1542
1543   YYDPRINTF ((stderr, "Starting parse\n"));
1544
1545   yystate = 0;
1546   yyerrstatus = 0;
1547   yynerrs = 0;
1548   yychar = YYEMPTY;             /* Cause a token to be read.  */
1549
1550   /* Initialize stack pointers.
1551      Waste one element of value and location stack
1552      so that they stay on the same level as the state stack.
1553      The wasted elements are never initialized.  */
1554
1555   yyssp = yyss;
1556   yyvsp = yyvs;
1557
1558   goto yysetstate;
1559
1560 /*------------------------------------------------------------.
1561 | yynewstate -- Push a new state, which is found in yystate.  |
1562 `------------------------------------------------------------*/
1563  yynewstate:
1564   /* In all cases, when you get here, the value and location stacks
1565      have just been pushed.  So pushing a state here evens the stacks.  */
1566   yyssp++;
1567
1568  yysetstate:
1569   *yyssp = yystate;
1570
1571   if (yyss + yystacksize - 1 <= yyssp)
1572     {
1573       /* Get the current used size of the three stacks, in elements.  */
1574       YYSIZE_T yysize = yyssp - yyss + 1;
1575
1576 #ifdef yyoverflow
1577       {
1578         /* Give user a chance to reallocate the stack.  Use copies of
1579            these so that the &'s don't force the real ones into
1580            memory.  */
1581         YYSTYPE *yyvs1 = yyvs;
1582         yytype_int16 *yyss1 = yyss;
1583
1584
1585         /* Each stack pointer address is followed by the size of the
1586            data in use in that stack, in bytes.  This used to be a
1587            conditional around just the two extra args, but that might
1588            be undefined if yyoverflow is a macro.  */
1589         yyoverflow (YY_("memory exhausted"),
1590                     &yyss1, yysize * sizeof (*yyssp),
1591                     &yyvs1, yysize * sizeof (*yyvsp),
1592
1593                     &yystacksize);
1594
1595         yyss = yyss1;
1596         yyvs = yyvs1;
1597       }
1598 #else /* no yyoverflow */
1599 # ifndef YYSTACK_RELOCATE
1600       goto yyexhaustedlab;
1601 # else
1602       /* Extend the stack our own way.  */
1603       if (YYMAXDEPTH <= yystacksize)
1604         goto yyexhaustedlab;
1605       yystacksize *= 2;
1606       if (YYMAXDEPTH < yystacksize)
1607         yystacksize = YYMAXDEPTH;
1608
1609       {
1610         yytype_int16 *yyss1 = yyss;
1611         union yyalloc *yyptr =
1612           (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
1613         if (! yyptr)
1614           goto yyexhaustedlab;
1615         YYSTACK_RELOCATE (yyss);
1616         YYSTACK_RELOCATE (yyvs);
1617
1618 #  undef YYSTACK_RELOCATE
1619         if (yyss1 != yyssa)
1620           YYSTACK_FREE (yyss1);
1621       }
1622 # endif
1623 #endif /* no yyoverflow */
1624
1625       yyssp = yyss + yysize - 1;
1626       yyvsp = yyvs + yysize - 1;
1627
1628
1629       YYDPRINTF ((stderr, "Stack size increased to %lu\n",
1630                   (unsigned long int) yystacksize));
1631
1632       if (yyss + yystacksize - 1 <= yyssp)
1633         YYABORT;
1634     }
1635
1636   YYDPRINTF ((stderr, "Entering state %d\n", yystate));
1637
1638   goto yybackup;
1639
1640 /*-----------.
1641 | yybackup.  |
1642 `-----------*/
1643 yybackup:
1644
1645   /* Do appropriate processing given the current state.  Read a
1646      look-ahead token if we need one and don't already have one.  */
1647
1648   /* First try to decide what to do without reference to look-ahead token.  */
1649   yyn = yypact[yystate];
1650   if (yyn == YYPACT_NINF)
1651     goto yydefault;
1652
1653   /* Not known => get a look-ahead token if don't already have one.  */
1654
1655   /* YYCHAR is either YYEMPTY or YYEOF or a valid look-ahead symbol.  */
1656   if (yychar == YYEMPTY)
1657     {
1658       YYDPRINTF ((stderr, "Reading a token: "));
1659       yychar = YYLEX;
1660     }
1661
1662   if (yychar <= YYEOF)
1663     {
1664       yychar = yytoken = YYEOF;
1665       YYDPRINTF ((stderr, "Now at end of input.\n"));
1666     }
1667   else
1668     {
1669       yytoken = YYTRANSLATE (yychar);
1670       YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
1671     }
1672
1673   /* If the proper action on seeing token YYTOKEN is to reduce or to
1674      detect an error, take that action.  */
1675   yyn += yytoken;
1676   if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
1677     goto yydefault;
1678   yyn = yytable[yyn];
1679   if (yyn <= 0)
1680     {
1681       if (yyn == 0 || yyn == YYTABLE_NINF)
1682         goto yyerrlab;
1683       yyn = -yyn;
1684       goto yyreduce;
1685     }
1686
1687   if (yyn == YYFINAL)
1688     YYACCEPT;
1689
1690   /* Count tokens shifted since error; after three, turn off error
1691      status.  */
1692   if (yyerrstatus)
1693     yyerrstatus--;
1694
1695   /* Shift the look-ahead token.  */
1696   YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
1697
1698   /* Discard the shifted token unless it is eof.  */
1699   if (yychar != YYEOF)
1700     yychar = YYEMPTY;
1701
1702   yystate = yyn;
1703   *++yyvsp = yylval;
1704
1705   goto yynewstate;
1706
1707
1708 /*-----------------------------------------------------------.
1709 | yydefault -- do the default action for the current state.  |
1710 `-----------------------------------------------------------*/
1711 yydefault:
1712   yyn = yydefact[yystate];
1713   if (yyn == 0)
1714     goto yyerrlab;
1715   goto yyreduce;
1716
1717
1718 /*-----------------------------.
1719 | yyreduce -- Do a reduction.  |
1720 `-----------------------------*/
1721 yyreduce:
1722   /* yyn is the number of a rule to reduce with.  */
1723   yylen = yyr2[yyn];
1724
1725   /* If YYLEN is nonzero, implement the default value of the action:
1726      `$$ = $1'.
1727
1728      Otherwise, the following line sets YYVAL to garbage.
1729      This behavior is undocumented and Bison
1730      users should not rely upon it.  Assigning to YYVAL
1731      unconditionally makes the parser a bit smaller, and it avoids a
1732      GCC warning that YYVAL may be used uninitialized.  */
1733   yyval = yyvsp[1-yylen];
1734
1735
1736   YY_REDUCE_PRINT (yyn);
1737   switch (yyn)
1738     {
1739         case 4:
1740 #line 325 "parse-datetime.y"
1741     {
1742         pc->seconds = (yyvsp[(2) - (2)].timespec);
1743         pc->timespec_seen = true;
1744       }
1745     break;
1746
1747   case 7:
1748 #line 338 "parse-datetime.y"
1749     { pc->times_seen++; }
1750     break;
1751
1752   case 8:
1753 #line 340 "parse-datetime.y"
1754     { pc->local_zones_seen++; }
1755     break;
1756
1757   case 9:
1758 #line 342 "parse-datetime.y"
1759     { pc->zones_seen++; }
1760     break;
1761
1762   case 10:
1763 #line 344 "parse-datetime.y"
1764     { pc->dates_seen++; }
1765     break;
1766
1767   case 11:
1768 #line 346 "parse-datetime.y"
1769     { pc->days_seen++; }
1770     break;
1771
1772   case 15:
1773 #line 354 "parse-datetime.y"
1774     {
1775         set_hhmmss (pc, (yyvsp[(1) - (2)].textintval).value, 0, 0, 0);
1776         pc->meridian = (yyvsp[(2) - (2)].intval);
1777       }
1778     break;
1779
1780   case 16:
1781 #line 359 "parse-datetime.y"
1782     {
1783         set_hhmmss (pc, (yyvsp[(1) - (4)].textintval).value, (yyvsp[(3) - (4)].textintval).value, 0, 0);
1784         pc->meridian = (yyvsp[(4) - (4)].intval);
1785       }
1786     break;
1787
1788   case 17:
1789 #line 364 "parse-datetime.y"
1790     {
1791         set_hhmmss (pc, (yyvsp[(1) - (5)].textintval).value, (yyvsp[(3) - (5)].textintval).value, 0, 0);
1792         pc->meridian = MER24;
1793         pc->zones_seen++;
1794         pc->time_zone = time_zone_hhmm (pc, (yyvsp[(4) - (5)].textintval), (yyvsp[(5) - (5)].intval));
1795       }
1796     break;
1797
1798   case 18:
1799 #line 371 "parse-datetime.y"
1800     {
1801         set_hhmmss (pc, (yyvsp[(1) - (6)].textintval).value, (yyvsp[(3) - (6)].textintval).value, (yyvsp[(5) - (6)].timespec).tv_sec, (yyvsp[(5) - (6)].timespec).tv_nsec);
1802         pc->meridian = (yyvsp[(6) - (6)].intval);
1803       }
1804     break;
1805
1806   case 19:
1807 #line 376 "parse-datetime.y"
1808     {
1809         set_hhmmss (pc, (yyvsp[(1) - (7)].textintval).value, (yyvsp[(3) - (7)].textintval).value, (yyvsp[(5) - (7)].timespec).tv_sec, (yyvsp[(5) - (7)].timespec).tv_nsec);
1810         pc->meridian = MER24;
1811         pc->zones_seen++;
1812         pc->time_zone = time_zone_hhmm (pc, (yyvsp[(6) - (7)].textintval), (yyvsp[(7) - (7)].intval));
1813       }
1814     break;
1815
1816   case 20:
1817 #line 386 "parse-datetime.y"
1818     {
1819         pc->local_isdst = (yyvsp[(1) - (1)].intval);
1820         pc->dsts_seen += (0 < (yyvsp[(1) - (1)].intval));
1821       }
1822     break;
1823
1824   case 21:
1825 #line 391 "parse-datetime.y"
1826     {
1827         pc->local_isdst = 1;
1828         pc->dsts_seen += (0 < (yyvsp[(1) - (2)].intval)) + 1;
1829       }
1830     break;
1831
1832   case 22:
1833 #line 399 "parse-datetime.y"
1834     { pc->time_zone = (yyvsp[(1) - (1)].intval); }
1835     break;
1836
1837   case 23:
1838 #line 401 "parse-datetime.y"
1839     { pc->time_zone = (yyvsp[(1) - (2)].intval);
1840         apply_relative_time (pc, (yyvsp[(2) - (2)].rel), 1); }
1841     break;
1842
1843   case 24:
1844 #line 404 "parse-datetime.y"
1845     { pc->time_zone = (yyvsp[(1) - (3)].intval) + time_zone_hhmm (pc, (yyvsp[(2) - (3)].textintval), (yyvsp[(3) - (3)].intval)); }
1846     break;
1847
1848   case 25:
1849 #line 406 "parse-datetime.y"
1850     { pc->time_zone = (yyvsp[(1) - (1)].intval) + 60; }
1851     break;
1852
1853   case 26:
1854 #line 408 "parse-datetime.y"
1855     { pc->time_zone = (yyvsp[(1) - (2)].intval) + 60; }
1856     break;
1857
1858   case 27:
1859 #line 413 "parse-datetime.y"
1860     {
1861         pc->day_ordinal = 0;
1862         pc->day_number = (yyvsp[(1) - (1)].intval);
1863       }
1864     break;
1865
1866   case 28:
1867 #line 418 "parse-datetime.y"
1868     {
1869         pc->day_ordinal = 0;
1870         pc->day_number = (yyvsp[(1) - (2)].intval);
1871       }
1872     break;
1873
1874   case 29:
1875 #line 423 "parse-datetime.y"
1876     {
1877         pc->day_ordinal = (yyvsp[(1) - (2)].intval);
1878         pc->day_number = (yyvsp[(2) - (2)].intval);
1879       }
1880     break;
1881
1882   case 30:
1883 #line 428 "parse-datetime.y"
1884     {
1885         pc->day_ordinal = (yyvsp[(1) - (2)].textintval).value;
1886         pc->day_number = (yyvsp[(2) - (2)].intval);
1887       }
1888     break;
1889
1890   case 31:
1891 #line 436 "parse-datetime.y"
1892     {
1893         pc->month = (yyvsp[(1) - (3)].textintval).value;
1894         pc->day = (yyvsp[(3) - (3)].textintval).value;
1895       }
1896     break;
1897
1898   case 32:
1899 #line 441 "parse-datetime.y"
1900     {
1901         /* Interpret as YYYY/MM/DD if the first value has 4 or more digits,
1902            otherwise as MM/DD/YY.
1903            The goal in recognizing YYYY/MM/DD is solely to support legacy
1904            machine-generated dates like those in an RCS log listing.  If
1905            you want portability, use the ISO 8601 format.  */
1906         if (4 <= (yyvsp[(1) - (5)].textintval).digits)
1907           {
1908             pc->year = (yyvsp[(1) - (5)].textintval);
1909             pc->month = (yyvsp[(3) - (5)].textintval).value;
1910             pc->day = (yyvsp[(5) - (5)].textintval).value;
1911           }
1912         else
1913           {
1914             pc->month = (yyvsp[(1) - (5)].textintval).value;
1915             pc->day = (yyvsp[(3) - (5)].textintval).value;
1916             pc->year = (yyvsp[(5) - (5)].textintval);
1917           }
1918       }
1919     break;
1920
1921   case 33:
1922 #line 461 "parse-datetime.y"
1923     {
1924         /* ISO 8601 format.  YYYY-MM-DD.  */
1925         pc->year = (yyvsp[(1) - (3)].textintval);
1926         pc->month = -(yyvsp[(2) - (3)].textintval).value;
1927         pc->day = -(yyvsp[(3) - (3)].textintval).value;
1928       }
1929     break;
1930
1931   case 34:
1932 #line 468 "parse-datetime.y"
1933     {
1934         /* e.g. 17-JUN-1992.  */
1935         pc->day = (yyvsp[(1) - (3)].textintval).value;
1936         pc->month = (yyvsp[(2) - (3)].intval);
1937         pc->year.value = -(yyvsp[(3) - (3)].textintval).value;
1938         pc->year.digits = (yyvsp[(3) - (3)].textintval).digits;
1939       }
1940     break;
1941
1942   case 35:
1943 #line 476 "parse-datetime.y"
1944     {
1945         /* e.g. JUN-17-1992.  */
1946         pc->month = (yyvsp[(1) - (3)].intval);
1947         pc->day = -(yyvsp[(2) - (3)].textintval).value;
1948         pc->year.value = -(yyvsp[(3) - (3)].textintval).value;
1949         pc->year.digits = (yyvsp[(3) - (3)].textintval).digits;
1950       }
1951     break;
1952
1953   case 36:
1954 #line 484 "parse-datetime.y"
1955     {
1956         pc->month = (yyvsp[(1) - (2)].intval);
1957         pc->day = (yyvsp[(2) - (2)].textintval).value;
1958       }
1959     break;
1960
1961   case 37:
1962 #line 489 "parse-datetime.y"
1963     {
1964         pc->month = (yyvsp[(1) - (4)].intval);
1965         pc->day = (yyvsp[(2) - (4)].textintval).value;
1966         pc->year = (yyvsp[(4) - (4)].textintval);
1967       }
1968     break;
1969
1970   case 38:
1971 #line 495 "parse-datetime.y"
1972     {
1973         pc->day = (yyvsp[(1) - (2)].textintval).value;
1974         pc->month = (yyvsp[(2) - (2)].intval);
1975       }
1976     break;
1977
1978   case 39:
1979 #line 500 "parse-datetime.y"
1980     {
1981         pc->day = (yyvsp[(1) - (3)].textintval).value;
1982         pc->month = (yyvsp[(2) - (3)].intval);
1983         pc->year = (yyvsp[(3) - (3)].textintval);
1984       }
1985     break;
1986
1987   case 40:
1988 #line 509 "parse-datetime.y"
1989     { apply_relative_time (pc, (yyvsp[(1) - (2)].rel), -1); }
1990     break;
1991
1992   case 41:
1993 #line 511 "parse-datetime.y"
1994     { apply_relative_time (pc, (yyvsp[(1) - (1)].rel), 1); }
1995     break;
1996
1997   case 42:
1998 #line 513 "parse-datetime.y"
1999     { apply_relative_time (pc, (yyvsp[(1) - (1)].rel), 1); }
2000     break;
2001
2002   case 43:
2003 #line 518 "parse-datetime.y"
2004     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).year = (yyvsp[(1) - (2)].intval); }
2005     break;
2006
2007   case 44:
2008 #line 520 "parse-datetime.y"
2009     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).year = (yyvsp[(1) - (2)].textintval).value; }
2010     break;
2011
2012   case 45:
2013 #line 522 "parse-datetime.y"
2014     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).year = 1; }
2015     break;
2016
2017   case 46:
2018 #line 524 "parse-datetime.y"
2019     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).month = (yyvsp[(1) - (2)].intval); }
2020     break;
2021
2022   case 47:
2023 #line 526 "parse-datetime.y"
2024     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).month = (yyvsp[(1) - (2)].textintval).value; }
2025     break;
2026
2027   case 48:
2028 #line 528 "parse-datetime.y"
2029     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).month = 1; }
2030     break;
2031
2032   case 49:
2033 #line 530 "parse-datetime.y"
2034     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).day = (yyvsp[(1) - (2)].intval) * (yyvsp[(2) - (2)].intval); }
2035     break;
2036
2037   case 50:
2038 #line 532 "parse-datetime.y"
2039     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).day = (yyvsp[(1) - (2)].textintval).value * (yyvsp[(2) - (2)].intval); }
2040     break;
2041
2042   case 51:
2043 #line 534 "parse-datetime.y"
2044     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).day = (yyvsp[(1) - (1)].intval); }
2045     break;
2046
2047   case 52:
2048 #line 536 "parse-datetime.y"
2049     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).hour = (yyvsp[(1) - (2)].intval); }
2050     break;
2051
2052   case 53:
2053 #line 538 "parse-datetime.y"
2054     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).hour = (yyvsp[(1) - (2)].textintval).value; }
2055     break;
2056
2057   case 54:
2058 #line 540 "parse-datetime.y"
2059     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).hour = 1; }
2060     break;
2061
2062   case 55:
2063 #line 542 "parse-datetime.y"
2064     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).minutes = (yyvsp[(1) - (2)].intval); }
2065     break;
2066
2067   case 56:
2068 #line 544 "parse-datetime.y"
2069     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).minutes = (yyvsp[(1) - (2)].textintval).value; }
2070     break;
2071
2072   case 57:
2073 #line 546 "parse-datetime.y"
2074     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).minutes = 1; }
2075     break;
2076
2077   case 58:
2078 #line 548 "parse-datetime.y"
2079     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).seconds = (yyvsp[(1) - (2)].intval); }
2080     break;
2081
2082   case 59:
2083 #line 550 "parse-datetime.y"
2084     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).seconds = (yyvsp[(1) - (2)].textintval).value; }
2085     break;
2086
2087   case 60:
2088 #line 552 "parse-datetime.y"
2089     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).seconds = (yyvsp[(1) - (2)].timespec).tv_sec; (yyval.rel).ns = (yyvsp[(1) - (2)].timespec).tv_nsec; }
2090     break;
2091
2092   case 61:
2093 #line 554 "parse-datetime.y"
2094     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).seconds = (yyvsp[(1) - (2)].timespec).tv_sec; (yyval.rel).ns = (yyvsp[(1) - (2)].timespec).tv_nsec; }
2095     break;
2096
2097   case 62:
2098 #line 556 "parse-datetime.y"
2099     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).seconds = 1; }
2100     break;
2101
2102   case 64:
2103 #line 562 "parse-datetime.y"
2104     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).year = (yyvsp[(1) - (2)].textintval).value; }
2105     break;
2106
2107   case 65:
2108 #line 564 "parse-datetime.y"
2109     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).month = (yyvsp[(1) - (2)].textintval).value; }
2110     break;
2111
2112   case 66:
2113 #line 566 "parse-datetime.y"
2114     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).day = (yyvsp[(1) - (2)].textintval).value * (yyvsp[(2) - (2)].intval); }
2115     break;
2116
2117   case 67:
2118 #line 568 "parse-datetime.y"
2119     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).hour = (yyvsp[(1) - (2)].textintval).value; }
2120     break;
2121
2122   case 68:
2123 #line 570 "parse-datetime.y"
2124     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).minutes = (yyvsp[(1) - (2)].textintval).value; }
2125     break;
2126
2127   case 69:
2128 #line 572 "parse-datetime.y"
2129     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).seconds = (yyvsp[(1) - (2)].textintval).value; }
2130     break;
2131
2132   case 70:
2133 #line 577 "parse-datetime.y"
2134     { (yyval.rel) = RELATIVE_TIME_0; (yyval.rel).day = (yyvsp[(1) - (1)].intval); }
2135     break;
2136
2137   case 74:
2138 #line 585 "parse-datetime.y"
2139     { (yyval.timespec).tv_sec = (yyvsp[(1) - (1)].textintval).value; (yyval.timespec).tv_nsec = 0; }
2140     break;
2141
2142   case 76:
2143 #line 591 "parse-datetime.y"
2144     { (yyval.timespec).tv_sec = (yyvsp[(1) - (1)].textintval).value; (yyval.timespec).tv_nsec = 0; }
2145     break;
2146
2147   case 77:
2148 #line 596 "parse-datetime.y"
2149     { digits_to_date_time (pc, (yyvsp[(1) - (1)].textintval)); }
2150     break;
2151
2152   case 78:
2153 #line 601 "parse-datetime.y"
2154     {
2155         /* Hybrid all-digit and relative offset, so that we accept e.g.,
2156            "YYYYMMDD +N days" as well as "YYYYMMDD N days".  */
2157         digits_to_date_time (pc, (yyvsp[(1) - (2)].textintval));
2158         apply_relative_time (pc, (yyvsp[(2) - (2)].rel), 1);
2159       }
2160     break;
2161
2162   case 79:
2163 #line 611 "parse-datetime.y"
2164     { (yyval.intval) = -1; }
2165     break;
2166
2167   case 80:
2168 #line 613 "parse-datetime.y"
2169     { (yyval.intval) = (yyvsp[(2) - (2)].textintval).value; }
2170     break;
2171
2172   case 81:
2173 #line 618 "parse-datetime.y"
2174     { (yyval.intval) = MER24; }
2175     break;
2176
2177   case 82:
2178 #line 620 "parse-datetime.y"
2179     { (yyval.intval) = (yyvsp[(1) - (1)].intval); }
2180     break;
2181
2182
2183 /* Line 1267 of yacc.c.  */
2184 #line 2185 "parse-datetime.c"
2185       default: break;
2186     }
2187   YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
2188
2189   YYPOPSTACK (yylen);
2190   yylen = 0;
2191   YY_STACK_PRINT (yyss, yyssp);
2192
2193   *++yyvsp = yyval;
2194
2195
2196   /* Now `shift' the result of the reduction.  Determine what state
2197      that goes to, based on the state we popped back to and the rule
2198      number reduced by.  */
2199
2200   yyn = yyr1[yyn];
2201
2202   yystate = yypgoto[yyn - YYNTOKENS] + *yyssp;
2203   if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp)
2204     yystate = yytable[yystate];
2205   else
2206     yystate = yydefgoto[yyn - YYNTOKENS];
2207
2208   goto yynewstate;
2209
2210
2211 /*------------------------------------.
2212 | yyerrlab -- here on detecting error |
2213 `------------------------------------*/
2214 yyerrlab:
2215   /* If not already recovering from an error, report this error.  */
2216   if (!yyerrstatus)
2217     {
2218       ++yynerrs;
2219 #if ! YYERROR_VERBOSE
2220       yyerror (pc, YY_("syntax error"));
2221 #else
2222       {
2223         YYSIZE_T yysize = yysyntax_error (0, yystate, yychar);
2224         if (yymsg_alloc < yysize && yymsg_alloc < YYSTACK_ALLOC_MAXIMUM)
2225           {
2226             YYSIZE_T yyalloc = 2 * yysize;
2227             if (! (yysize <= yyalloc && yyalloc <= YYSTACK_ALLOC_MAXIMUM))
2228               yyalloc = YYSTACK_ALLOC_MAXIMUM;
2229             if (yymsg != yymsgbuf)
2230               YYSTACK_FREE (yymsg);
2231             yymsg = (char *) YYSTACK_ALLOC (yyalloc);
2232             if (yymsg)
2233               yymsg_alloc = yyalloc;
2234             else
2235               {
2236                 yymsg = yymsgbuf;
2237                 yymsg_alloc = sizeof yymsgbuf;
2238               }
2239           }
2240
2241         if (0 < yysize && yysize <= yymsg_alloc)
2242           {
2243             (void) yysyntax_error (yymsg, yystate, yychar);
2244             yyerror (pc, yymsg);
2245           }
2246         else
2247           {
2248             yyerror (pc, YY_("syntax error"));
2249             if (yysize != 0)
2250               goto yyexhaustedlab;
2251           }
2252       }
2253 #endif
2254     }
2255
2256
2257
2258   if (yyerrstatus == 3)
2259     {
2260       /* If just tried and failed to reuse look-ahead token after an
2261          error, discard it.  */
2262
2263       if (yychar <= YYEOF)
2264         {
2265           /* Return failure if at end of input.  */
2266           if (yychar == YYEOF)
2267             YYABORT;
2268         }
2269       else
2270         {
2271           yydestruct ("Error: discarding",
2272                       yytoken, &yylval, pc);
2273           yychar = YYEMPTY;
2274         }
2275     }
2276
2277   /* Else will try to reuse look-ahead token after shifting the error
2278      token.  */
2279   goto yyerrlab1;
2280
2281
2282 /*---------------------------------------------------.
2283 | yyerrorlab -- error raised explicitly by YYERROR.  |
2284 `---------------------------------------------------*/
2285 yyerrorlab:
2286
2287   /* Pacify compilers like GCC when the user code never invokes
2288      YYERROR and the label yyerrorlab therefore never appears in user
2289      code.  */
2290   if (/*CONSTCOND*/ 0)
2291      goto yyerrorlab;
2292
2293   /* Do not reclaim the symbols of the rule which action triggered
2294      this YYERROR.  */
2295   YYPOPSTACK (yylen);
2296   yylen = 0;
2297   YY_STACK_PRINT (yyss, yyssp);
2298   yystate = *yyssp;
2299   goto yyerrlab1;
2300
2301
2302 /*-------------------------------------------------------------.
2303 | yyerrlab1 -- common code for both syntax error and YYERROR.  |
2304 `-------------------------------------------------------------*/
2305 yyerrlab1:
2306   yyerrstatus = 3;      /* Each real token shifted decrements this.  */
2307
2308   for (;;)
2309     {
2310       yyn = yypact[yystate];
2311       if (yyn != YYPACT_NINF)
2312         {
2313           yyn += YYTERROR;
2314           if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
2315             {
2316               yyn = yytable[yyn];
2317               if (0 < yyn)
2318                 break;
2319             }
2320         }
2321
2322       /* Pop the current state because it cannot handle the error token.  */
2323       if (yyssp == yyss)
2324         YYABORT;
2325
2326
2327       yydestruct ("Error: popping",
2328                   yystos[yystate], yyvsp, pc);
2329       YYPOPSTACK (1);
2330       yystate = *yyssp;
2331       YY_STACK_PRINT (yyss, yyssp);
2332     }
2333
2334   if (yyn == YYFINAL)
2335     YYACCEPT;
2336
2337   *++yyvsp = yylval;
2338
2339
2340   /* Shift the error token.  */
2341   YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
2342
2343   yystate = yyn;
2344   goto yynewstate;
2345
2346
2347 /*-------------------------------------.
2348 | yyacceptlab -- YYACCEPT comes here.  |
2349 `-------------------------------------*/
2350 yyacceptlab:
2351   yyresult = 0;
2352   goto yyreturn;
2353
2354 /*-----------------------------------.
2355 | yyabortlab -- YYABORT comes here.  |
2356 `-----------------------------------*/
2357 yyabortlab:
2358   yyresult = 1;
2359   goto yyreturn;
2360
2361 #ifndef yyoverflow
2362 /*-------------------------------------------------.
2363 | yyexhaustedlab -- memory exhaustion comes here.  |
2364 `-------------------------------------------------*/
2365 yyexhaustedlab:
2366   yyerror (pc, YY_("memory exhausted"));
2367   yyresult = 2;
2368   /* Fall through.  */
2369 #endif
2370
2371 yyreturn:
2372   if (yychar != YYEOF && yychar != YYEMPTY)
2373      yydestruct ("Cleanup: discarding lookahead",
2374                  yytoken, &yylval, pc);
2375   /* Do not reclaim the symbols of the rule which action triggered
2376      this YYABORT or YYACCEPT.  */
2377   YYPOPSTACK (yylen);
2378   YY_STACK_PRINT (yyss, yyssp);
2379   while (yyssp != yyss)
2380     {
2381       yydestruct ("Cleanup: popping",
2382                   yystos[*yyssp], yyvsp, pc);
2383       YYPOPSTACK (1);
2384     }
2385 #ifndef yyoverflow
2386   if (yyss != yyssa)
2387     YYSTACK_FREE (yyss);
2388 #endif
2389 #if YYERROR_VERBOSE
2390   if (yymsg != yymsgbuf)
2391     YYSTACK_FREE (yymsg);
2392 #endif
2393   /* Make sure YYID is used.  */
2394   return YYID (yyresult);
2395 }
2396
2397
2398 #line 623 "parse-datetime.y"
2399
2400
2401 static table const meridian_table[] =
2402 {
2403   { "AM",   tMERIDIAN, MERam },
2404   { "A.M.", tMERIDIAN, MERam },
2405   { "PM",   tMERIDIAN, MERpm },
2406   { "P.M.", tMERIDIAN, MERpm },
2407   { NULL, 0, 0 }
2408 };
2409
2410 static table const dst_table[] =
2411 {
2412   { "DST", tDST, 0 }
2413 };
2414
2415 static table const month_and_day_table[] =
2416 {
2417   { "JANUARY",  tMONTH,  1 },
2418   { "FEBRUARY", tMONTH,  2 },
2419   { "MARCH",    tMONTH,  3 },
2420   { "APRIL",    tMONTH,  4 },
2421   { "MAY",      tMONTH,  5 },
2422   { "JUNE",     tMONTH,  6 },
2423   { "JULY",     tMONTH,  7 },
2424   { "AUGUST",   tMONTH,  8 },
2425   { "SEPTEMBER",tMONTH,  9 },
2426   { "SEPT",     tMONTH,  9 },
2427   { "OCTOBER",  tMONTH, 10 },
2428   { "NOVEMBER", tMONTH, 11 },
2429   { "DECEMBER", tMONTH, 12 },
2430   { "SUNDAY",   tDAY,    0 },
2431   { "MONDAY",   tDAY,    1 },
2432   { "TUESDAY",  tDAY,    2 },
2433   { "TUES",     tDAY,    2 },
2434   { "WEDNESDAY",tDAY,    3 },
2435   { "WEDNES",   tDAY,    3 },
2436   { "THURSDAY", tDAY,    4 },
2437   { "THUR",     tDAY,    4 },
2438   { "THURS",    tDAY,    4 },
2439   { "FRIDAY",   tDAY,    5 },
2440   { "SATURDAY", tDAY,    6 },
2441   { NULL, 0, 0 }
2442 };
2443
2444 static table const time_units_table[] =
2445 {
2446   { "YEAR",     tYEAR_UNIT,      1 },
2447   { "MONTH",    tMONTH_UNIT,     1 },
2448   { "FORTNIGHT",tDAY_UNIT,      14 },
2449   { "WEEK",     tDAY_UNIT,       7 },
2450   { "DAY",      tDAY_UNIT,       1 },
2451   { "HOUR",     tHOUR_UNIT,      1 },
2452   { "MINUTE",   tMINUTE_UNIT,    1 },
2453   { "MIN",      tMINUTE_UNIT,    1 },
2454   { "SECOND",   tSEC_UNIT,       1 },
2455   { "SEC",      tSEC_UNIT,       1 },
2456   { NULL, 0, 0 }
2457 };
2458
2459 /* Assorted relative-time words. */
2460 static table const relative_time_table[] =
2461 {
2462   { "TOMORROW", tDAY_SHIFT,      1 },
2463   { "YESTERDAY",tDAY_SHIFT,     -1 },
2464   { "TODAY",    tDAY_SHIFT,      0 },
2465   { "NOW",      tDAY_SHIFT,      0 },
2466   { "LAST",     tORDINAL,       -1 },
2467   { "THIS",     tORDINAL,        0 },
2468   { "NEXT",     tORDINAL,        1 },
2469   { "FIRST",    tORDINAL,        1 },
2470 /*{ "SECOND",   tORDINAL,        2 }, */
2471   { "THIRD",    tORDINAL,        3 },
2472   { "FOURTH",   tORDINAL,        4 },
2473   { "FIFTH",    tORDINAL,        5 },
2474   { "SIXTH",    tORDINAL,        6 },
2475   { "SEVENTH",  tORDINAL,        7 },
2476   { "EIGHTH",   tORDINAL,        8 },
2477   { "NINTH",    tORDINAL,        9 },
2478   { "TENTH",    tORDINAL,       10 },
2479   { "ELEVENTH", tORDINAL,       11 },
2480   { "TWELFTH",  tORDINAL,       12 },
2481   { "AGO",      tAGO,            1 },
2482   { NULL, 0, 0 }
2483 };
2484
2485 /* The universal time zone table.  These labels can be used even for
2486    time stamps that would not otherwise be valid, e.g., GMT time
2487    stamps in London during summer.  */
2488 static table const universal_time_zone_table[] =
2489 {
2490   { "GMT",      tZONE,     HOUR ( 0) }, /* Greenwich Mean */
2491   { "UT",       tZONE,     HOUR ( 0) }, /* Universal (Coordinated) */
2492   { "UTC",      tZONE,     HOUR ( 0) },
2493   { NULL, 0, 0 }
2494 };
2495
2496 /* The time zone table.  This table is necessarily incomplete, as time
2497    zone abbreviations are ambiguous; e.g. Australians interpret "EST"
2498    as Eastern time in Australia, not as US Eastern Standard Time.
2499    You cannot rely on parse_datetime to handle arbitrary time zone
2500    abbreviations; use numeric abbreviations like `-0500' instead.  */
2501 static table const time_zone_table[] =
2502 {
2503   { "WET",      tZONE,     HOUR ( 0) }, /* Western European */
2504   { "WEST",     tDAYZONE,  HOUR ( 0) }, /* Western European Summer */
2505   { "BST",      tDAYZONE,  HOUR ( 0) }, /* British Summer */
2506   { "ART",      tZONE,    -HOUR ( 3) }, /* Argentina */
2507   { "BRT",      tZONE,    -HOUR ( 3) }, /* Brazil */
2508   { "BRST",     tDAYZONE, -HOUR ( 3) }, /* Brazil Summer */
2509   { "NST",      tZONE,   -(HOUR ( 3) + 30) },   /* Newfoundland Standard */
2510   { "NDT",      tDAYZONE,-(HOUR ( 3) + 30) },   /* Newfoundland Daylight */
2511   { "AST",      tZONE,    -HOUR ( 4) }, /* Atlantic Standard */
2512   { "ADT",      tDAYZONE, -HOUR ( 4) }, /* Atlantic Daylight */
2513   { "CLT",      tZONE,    -HOUR ( 4) }, /* Chile */
2514   { "CLST",     tDAYZONE, -HOUR ( 4) }, /* Chile Summer */
2515   { "EST",      tZONE,    -HOUR ( 5) }, /* Eastern Standard */
2516   { "EDT",      tDAYZONE, -HOUR ( 5) }, /* Eastern Daylight */
2517   { "CST",      tZONE,    -HOUR ( 6) }, /* Central Standard */
2518   { "CDT",      tDAYZONE, -HOUR ( 6) }, /* Central Daylight */
2519   { "MST",      tZONE,    -HOUR ( 7) }, /* Mountain Standard */
2520   { "MDT",      tDAYZONE, -HOUR ( 7) }, /* Mountain Daylight */
2521   { "PST",      tZONE,    -HOUR ( 8) }, /* Pacific Standard */
2522   { "PDT",      tDAYZONE, -HOUR ( 8) }, /* Pacific Daylight */
2523   { "AKST",     tZONE,    -HOUR ( 9) }, /* Alaska Standard */
2524   { "AKDT",     tDAYZONE, -HOUR ( 9) }, /* Alaska Daylight */
2525   { "HST",      tZONE,    -HOUR (10) }, /* Hawaii Standard */
2526   { "HAST",     tZONE,    -HOUR (10) }, /* Hawaii-Aleutian Standard */
2527   { "HADT",     tDAYZONE, -HOUR (10) }, /* Hawaii-Aleutian Daylight */
2528   { "SST",      tZONE,    -HOUR (12) }, /* Samoa Standard */
2529   { "WAT",      tZONE,     HOUR ( 1) }, /* West Africa */
2530   { "CET",      tZONE,     HOUR ( 1) }, /* Central European */
2531   { "CEST",     tDAYZONE,  HOUR ( 1) }, /* Central European Summer */
2532   { "MET",      tZONE,     HOUR ( 1) }, /* Middle European */
2533   { "MEZ",      tZONE,     HOUR ( 1) }, /* Middle European */
2534   { "MEST",     tDAYZONE,  HOUR ( 1) }, /* Middle European Summer */
2535   { "MESZ",     tDAYZONE,  HOUR ( 1) }, /* Middle European Summer */
2536   { "EET",      tZONE,     HOUR ( 2) }, /* Eastern European */
2537   { "EEST",     tDAYZONE,  HOUR ( 2) }, /* Eastern European Summer */
2538   { "CAT",      tZONE,     HOUR ( 2) }, /* Central Africa */
2539   { "SAST",     tZONE,     HOUR ( 2) }, /* South Africa Standard */
2540   { "EAT",      tZONE,     HOUR ( 3) }, /* East Africa */
2541   { "MSK",      tZONE,     HOUR ( 3) }, /* Moscow */
2542   { "MSD",      tDAYZONE,  HOUR ( 3) }, /* Moscow Daylight */
2543   { "IST",      tZONE,    (HOUR ( 5) + 30) },   /* India Standard */
2544   { "SGT",      tZONE,     HOUR ( 8) }, /* Singapore */
2545   { "KST",      tZONE,     HOUR ( 9) }, /* Korea Standard */
2546   { "JST",      tZONE,     HOUR ( 9) }, /* Japan Standard */
2547   { "GST",      tZONE,     HOUR (10) }, /* Guam Standard */
2548   { "NZST",     tZONE,     HOUR (12) }, /* New Zealand Standard */
2549   { "NZDT",     tDAYZONE,  HOUR (12) }, /* New Zealand Daylight */
2550   { NULL, 0, 0 }
2551 };
2552
2553 /* Military time zone table. */
2554 static table const military_table[] =
2555 {
2556   { "A", tZONE, -HOUR ( 1) },
2557   { "B", tZONE, -HOUR ( 2) },
2558   { "C", tZONE, -HOUR ( 3) },
2559   { "D", tZONE, -HOUR ( 4) },
2560   { "E", tZONE, -HOUR ( 5) },
2561   { "F", tZONE, -HOUR ( 6) },
2562   { "G", tZONE, -HOUR ( 7) },
2563   { "H", tZONE, -HOUR ( 8) },
2564   { "I", tZONE, -HOUR ( 9) },
2565   { "K", tZONE, -HOUR (10) },
2566   { "L", tZONE, -HOUR (11) },
2567   { "M", tZONE, -HOUR (12) },
2568   { "N", tZONE,  HOUR ( 1) },
2569   { "O", tZONE,  HOUR ( 2) },
2570   { "P", tZONE,  HOUR ( 3) },
2571   { "Q", tZONE,  HOUR ( 4) },
2572   { "R", tZONE,  HOUR ( 5) },
2573   { "S", tZONE,  HOUR ( 6) },
2574   { "T", tZONE,  HOUR ( 7) },
2575   { "U", tZONE,  HOUR ( 8) },
2576   { "V", tZONE,  HOUR ( 9) },
2577   { "W", tZONE,  HOUR (10) },
2578   { "X", tZONE,  HOUR (11) },
2579   { "Y", tZONE,  HOUR (12) },
2580   { "Z", tZONE,  HOUR ( 0) },
2581   { NULL, 0, 0 }
2582 };
2583
2584 \f
2585
2586 /* Convert a time zone expressed as HH:MM into an integer count of
2587    minutes.  If MM is negative, then S is of the form HHMM and needs
2588    to be picked apart; otherwise, S is of the form HH.  As specified in
2589    http://www.opengroup.org/susv3xbd/xbd_chap08.html#tag_08_03, allow
2590    only valid TZ range, and consider first two digits as hours, if no
2591    minutes specified.  */
2592
2593 static long int
2594 time_zone_hhmm (parser_control *pc, textint s, long int mm)
2595 {
2596   long int n_minutes;
2597
2598   /* If the length of S is 1 or 2 and no minutes are specified,
2599      interpret it as a number of hours.  */
2600   if (s.digits <= 2 && mm < 0)
2601     s.value *= 100;
2602
2603   if (mm < 0)
2604     n_minutes = (s.value / 100) * 60 + s.value % 100;
2605   else
2606     n_minutes = s.value * 60 + (s.negative ? -mm : mm);
2607
2608   /* If the absolute number of minutes is larger than 24 hours,
2609      arrange to reject it by incrementing pc->zones_seen.  Thus,
2610      we allow only values in the range UTC-24:00 to UTC+24:00.  */
2611   if (24 * 60 < abs (n_minutes))
2612     pc->zones_seen++;
2613
2614   return n_minutes;
2615 }
2616
2617 static int
2618 to_hour (long int hours, int meridian)
2619 {
2620   switch (meridian)
2621     {
2622     default: /* Pacify GCC.  */
2623     case MER24:
2624       return 0 <= hours && hours < 24 ? hours : -1;
2625     case MERam:
2626       return 0 < hours && hours < 12 ? hours : hours == 12 ? 0 : -1;
2627     case MERpm:
2628       return 0 < hours && hours < 12 ? hours + 12 : hours == 12 ? 12 : -1;
2629     }
2630 }
2631
2632 static long int
2633 to_year (textint textyear)
2634 {
2635   long int year = textyear.value;
2636
2637   if (year < 0)
2638     year = -year;
2639
2640   /* XPG4 suggests that years 00-68 map to 2000-2068, and
2641      years 69-99 map to 1969-1999.  */
2642   else if (textyear.digits == 2)
2643     year += year < 69 ? 2000 : 1900;
2644
2645   return year;
2646 }
2647
2648 static table const *
2649 lookup_zone (parser_control const *pc, char const *name)
2650 {
2651   table const *tp;
2652
2653   for (tp = universal_time_zone_table; tp->name; tp++)
2654     if (strcmp (name, tp->name) == 0)
2655       return tp;
2656
2657   /* Try local zone abbreviations before those in time_zone_table, as
2658      the local ones are more likely to be right.  */
2659   for (tp = pc->local_time_zone_table; tp->name; tp++)
2660     if (strcmp (name, tp->name) == 0)
2661       return tp;
2662
2663   for (tp = time_zone_table; tp->name; tp++)
2664     if (strcmp (name, tp->name) == 0)
2665       return tp;
2666
2667   return NULL;
2668 }
2669
2670 #if ! HAVE_TM_GMTOFF
2671 /* Yield the difference between *A and *B,
2672    measured in seconds, ignoring leap seconds.
2673    The body of this function is taken directly from the GNU C Library;
2674    see src/strftime.c.  */
2675 static long int
2676 tm_diff (struct tm const *a, struct tm const *b)
2677 {
2678   /* Compute intervening leap days correctly even if year is negative.
2679      Take care to avoid int overflow in leap day calculations.  */
2680   int a4 = SHR (a->tm_year, 2) + SHR (TM_YEAR_BASE, 2) - ! (a->tm_year & 3);
2681   int b4 = SHR (b->tm_year, 2) + SHR (TM_YEAR_BASE, 2) - ! (b->tm_year & 3);
2682   int a100 = a4 / 25 - (a4 % 25 < 0);
2683   int b100 = b4 / 25 - (b4 % 25 < 0);
2684   int a400 = SHR (a100, 2);
2685   int b400 = SHR (b100, 2);
2686   int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
2687   long int ayear = a->tm_year;
2688   long int years = ayear - b->tm_year;
2689   long int days = (365 * years + intervening_leap_days
2690                    + (a->tm_yday - b->tm_yday));
2691   return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
2692                 + (a->tm_min - b->tm_min))
2693           + (a->tm_sec - b->tm_sec));
2694 }
2695 #endif /* ! HAVE_TM_GMTOFF */
2696
2697 static table const *
2698 lookup_word (parser_control const *pc, char *word)
2699 {
2700   char *p;
2701   char *q;
2702   size_t wordlen;
2703   table const *tp;
2704   bool period_found;
2705   bool abbrev;
2706
2707   /* Make it uppercase.  */
2708   for (p = word; *p; p++)
2709     {
2710       unsigned char ch = *p;
2711       *p = c_toupper (ch);
2712     }
2713
2714   for (tp = meridian_table; tp->name; tp++)
2715     if (strcmp (word, tp->name) == 0)
2716       return tp;
2717
2718   /* See if we have an abbreviation for a month. */
2719   wordlen = strlen (word);
2720   abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.');
2721
2722   for (tp = month_and_day_table; tp->name; tp++)
2723     if ((abbrev ? strncmp (word, tp->name, 3) : strcmp (word, tp->name)) == 0)
2724       return tp;
2725
2726   if ((tp = lookup_zone (pc, word)))
2727     return tp;
2728
2729   if (strcmp (word, dst_table[0].name) == 0)
2730     return dst_table;
2731
2732   for (tp = time_units_table; tp->name; tp++)
2733     if (strcmp (word, tp->name) == 0)
2734       return tp;
2735
2736   /* Strip off any plural and try the units table again. */
2737   if (word[wordlen - 1] == 'S')
2738     {
2739       word[wordlen - 1] = '\0';
2740       for (tp = time_units_table; tp->name; tp++)
2741         if (strcmp (word, tp->name) == 0)
2742           return tp;
2743       word[wordlen - 1] = 'S';  /* For "this" in relative_time_table.  */
2744     }
2745
2746   for (tp = relative_time_table; tp->name; tp++)
2747     if (strcmp (word, tp->name) == 0)
2748       return tp;
2749
2750   /* Military time zones. */
2751   if (wordlen == 1)
2752     for (tp = military_table; tp->name; tp++)
2753       if (word[0] == tp->name[0])
2754         return tp;
2755
2756   /* Drop out any periods and try the time zone table again. */
2757   for (period_found = false, p = q = word; (*p = *q); q++)
2758     if (*q == '.')
2759       period_found = true;
2760     else
2761       p++;
2762   if (period_found && (tp = lookup_zone (pc, word)))
2763     return tp;
2764
2765   return NULL;
2766 }
2767
2768 static int
2769 yylex (YYSTYPE *lvalp, parser_control *pc)
2770 {
2771   unsigned char c;
2772   size_t count;
2773
2774   for (;;)
2775     {
2776       while (c = *pc->input, c_isspace (c))
2777         pc->input++;
2778
2779       if (ISDIGIT (c) || c == '-' || c == '+')
2780         {
2781           char const *p;
2782           int sign;
2783           unsigned long int value;
2784           if (c == '-' || c == '+')
2785             {
2786               sign = c == '-' ? -1 : 1;
2787               while (c = *++pc->input, c_isspace (c))
2788                 continue;
2789               if (! ISDIGIT (c))
2790                 /* skip the '-' sign */
2791                 continue;
2792             }
2793           else
2794             sign = 0;
2795           p = pc->input;
2796           for (value = 0; ; value *= 10)
2797             {
2798               unsigned long int value1 = value + (c - '0');
2799               if (value1 < value)
2800                 return '?';
2801               value = value1;
2802               c = *++p;
2803               if (! ISDIGIT (c))
2804                 break;
2805               if (ULONG_MAX / 10 < value)
2806                 return '?';
2807             }
2808           if ((c == '.' || c == ',') && ISDIGIT (p[1]))
2809             {
2810               time_t s;
2811               int ns;
2812               int digits;
2813               unsigned long int value1;
2814
2815               /* Check for overflow when converting value to time_t.  */
2816               if (sign < 0)
2817                 {
2818                   s = - value;
2819                   if (0 < s)
2820                     return '?';
2821                   value1 = -s;
2822                 }
2823               else
2824                 {
2825                   s = value;
2826                   if (s < 0)
2827                     return '?';
2828                   value1 = s;
2829                 }
2830               if (value != value1)
2831                 return '?';
2832
2833               /* Accumulate fraction, to ns precision.  */
2834               p++;
2835               ns = *p++ - '0';
2836               for (digits = 2; digits <= LOG10_BILLION; digits++)
2837                 {
2838                   ns *= 10;
2839                   if (ISDIGIT (*p))
2840                     ns += *p++ - '0';
2841                 }
2842
2843               /* Skip excess digits, truncating toward -Infinity.  */
2844               if (sign < 0)
2845                 for (; ISDIGIT (*p); p++)
2846                   if (*p != '0')
2847                     {
2848                       ns++;
2849                       break;
2850                     }
2851               while (ISDIGIT (*p))
2852                 p++;
2853
2854               /* Adjust to the timespec convention, which is that
2855                  tv_nsec is always a positive offset even if tv_sec is
2856                  negative.  */
2857               if (sign < 0 && ns)
2858                 {
2859                   s--;
2860                   if (! (s < 0))
2861                     return '?';
2862                   ns = BILLION - ns;
2863                 }
2864
2865               lvalp->timespec.tv_sec = s;
2866               lvalp->timespec.tv_nsec = ns;
2867               pc->input = p;
2868               return sign ? tSDECIMAL_NUMBER : tUDECIMAL_NUMBER;
2869             }
2870           else
2871             {
2872               lvalp->textintval.negative = sign < 0;
2873               if (sign < 0)
2874                 {
2875                   lvalp->textintval.value = - value;
2876                   if (0 < lvalp->textintval.value)
2877                     return '?';
2878                 }
2879               else
2880                 {
2881                   lvalp->textintval.value = value;
2882                   if (lvalp->textintval.value < 0)
2883                     return '?';
2884                 }
2885               lvalp->textintval.digits = p - pc->input;
2886               pc->input = p;
2887               return sign ? tSNUMBER : tUNUMBER;
2888             }
2889         }
2890
2891       if (c_isalpha (c))
2892         {
2893           char buff[20];
2894           char *p = buff;
2895           table const *tp;
2896
2897           do
2898             {
2899               if (p < buff + sizeof buff - 1)
2900                 *p++ = c;
2901               c = *++pc->input;
2902             }
2903           while (c_isalpha (c) || c == '.');
2904
2905           *p = '\0';
2906           tp = lookup_word (pc, buff);
2907           if (! tp)
2908             return '?';
2909           lvalp->intval = tp->value;
2910           return tp->type;
2911         }
2912
2913       if (c != '(')
2914         return *pc->input++;
2915       count = 0;
2916       do
2917         {
2918           c = *pc->input++;
2919           if (c == '\0')
2920             return c;
2921           if (c == '(')
2922             count++;
2923           else if (c == ')')
2924             count--;
2925         }
2926       while (count != 0);
2927     }
2928 }
2929
2930 /* Do nothing if the parser reports an error.  */
2931 static int
2932 yyerror (parser_control const *pc _GL_UNUSED,
2933          char const *s _GL_UNUSED)
2934 {
2935   return 0;
2936 }
2937
2938 /* If *TM0 is the old and *TM1 is the new value of a struct tm after
2939    passing it to mktime, return true if it's OK that mktime returned T.
2940    It's not OK if *TM0 has out-of-range members.  */
2941
2942 static bool
2943 mktime_ok (struct tm const *tm0, struct tm const *tm1, time_t t)
2944 {
2945   if (t == (time_t) -1)
2946     {
2947       /* Guard against falsely reporting an error when parsing a time
2948          stamp that happens to equal (time_t) -1, on a host that
2949          supports such a time stamp.  */
2950       tm1 = localtime (&t);
2951       if (!tm1)
2952         return false;
2953     }
2954
2955   return ! ((tm0->tm_sec ^ tm1->tm_sec)
2956             | (tm0->tm_min ^ tm1->tm_min)
2957             | (tm0->tm_hour ^ tm1->tm_hour)
2958             | (tm0->tm_mday ^ tm1->tm_mday)
2959             | (tm0->tm_mon ^ tm1->tm_mon)
2960             | (tm0->tm_year ^ tm1->tm_year));
2961 }
2962
2963 /* A reasonable upper bound for the size of ordinary TZ strings.
2964    Use heap allocation if TZ's length exceeds this.  */
2965 enum { TZBUFSIZE = 100 };
2966
2967 /* Return a copy of TZ, stored in TZBUF if it fits, and heap-allocated
2968    otherwise.  */
2969 static char *
2970 get_tz (char tzbuf[TZBUFSIZE])
2971 {
2972   char *tz = getenv ("TZ");
2973   if (tz)
2974     {
2975       size_t tzsize = strlen (tz) + 1;
2976       tz = (tzsize <= TZBUFSIZE
2977             ? memcpy (tzbuf, tz, tzsize)
2978             : xmemdup (tz, tzsize));
2979     }
2980   return tz;
2981 }
2982
2983 /* Parse a date/time string, storing the resulting time value into *RESULT.
2984    The string itself is pointed to by P.  Return true if successful.
2985    P can be an incomplete or relative time specification; if so, use
2986    *NOW as the basis for the returned time.  */
2987 bool
2988 parse_datetime (struct timespec *result, char const *p,
2989                 struct timespec const *now)
2990 {
2991   time_t Start;
2992   long int Start_ns;
2993   struct tm const *tmp;
2994   struct tm tm;
2995   struct tm tm0;
2996   parser_control pc;
2997   struct timespec gettime_buffer;
2998   unsigned char c;
2999   bool tz_was_altered = false;
3000   char *tz0 = NULL;
3001   char tz0buf[TZBUFSIZE];
3002   bool ok = true;
3003
3004   if (! now)
3005     {
3006       gettime (&gettime_buffer);
3007       now = &gettime_buffer;
3008     }
3009
3010   Start = now->tv_sec;
3011   Start_ns = now->tv_nsec;
3012
3013   tmp = localtime (&now->tv_sec);
3014   if (! tmp)
3015     return false;
3016
3017   while (c = *p, c_isspace (c))
3018     p++;
3019
3020   if (strncmp (p, "TZ=\"", 4) == 0)
3021     {
3022       char const *tzbase = p + 4;
3023       size_t tzsize = 1;
3024       char const *s;
3025
3026       for (s = tzbase; *s; s++, tzsize++)
3027         if (*s == '\\')
3028           {
3029             s++;
3030             if (! (*s == '\\' || *s == '"'))
3031               break;
3032           }
3033         else if (*s == '"')
3034           {
3035             char *z;
3036             char *tz1;
3037             char tz1buf[TZBUFSIZE];
3038             bool large_tz = TZBUFSIZE < tzsize;
3039             bool setenv_ok;
3040             /* Free tz0, in case this is the 2nd or subsequent time through. */
3041             free (tz0);
3042             tz0 = get_tz (tz0buf);
3043             z = tz1 = large_tz ? xmalloc (tzsize) : tz1buf;
3044             for (s = tzbase; *s != '"'; s++)
3045               *z++ = *(s += *s == '\\');
3046             *z = '\0';
3047             setenv_ok = setenv ("TZ", tz1, 1) == 0;
3048             if (large_tz)
3049               free (tz1);
3050             if (!setenv_ok)
3051               goto fail;
3052             tz_was_altered = true;
3053             p = s + 1;
3054           }
3055     }
3056
3057   /* As documented, be careful to treat the empty string just like
3058      a date string of "0".  Without this, an empty string would be
3059      declared invalid when parsed during a DST transition.  */
3060   if (*p == '\0')
3061     p = "0";
3062
3063   pc.input = p;
3064   pc.year.value = tmp->tm_year;
3065   pc.year.value += TM_YEAR_BASE;
3066   pc.year.digits = 0;
3067   pc.month = tmp->tm_mon + 1;
3068   pc.day = tmp->tm_mday;
3069   pc.hour = tmp->tm_hour;
3070   pc.minutes = tmp->tm_min;
3071   pc.seconds.tv_sec = tmp->tm_sec;
3072   pc.seconds.tv_nsec = Start_ns;
3073   tm.tm_isdst = tmp->tm_isdst;
3074
3075   pc.meridian = MER24;
3076   pc.rel = RELATIVE_TIME_0;
3077   pc.timespec_seen = false;
3078   pc.rels_seen = false;
3079   pc.dates_seen = 0;
3080   pc.days_seen = 0;
3081   pc.times_seen = 0;
3082   pc.local_zones_seen = 0;
3083   pc.dsts_seen = 0;
3084   pc.zones_seen = 0;
3085
3086 #if HAVE_STRUCT_TM_TM_ZONE
3087   pc.local_time_zone_table[0].name = tmp->tm_zone;
3088   pc.local_time_zone_table[0].type = tLOCAL_ZONE;
3089   pc.local_time_zone_table[0].value = tmp->tm_isdst;
3090   pc.local_time_zone_table[1].name = NULL;
3091
3092   /* Probe the names used in the next three calendar quarters, looking
3093      for a tm_isdst different from the one we already have.  */
3094   {
3095     int quarter;
3096     for (quarter = 1; quarter <= 3; quarter++)
3097       {
3098         time_t probe = Start + quarter * (90 * 24 * 60 * 60);
3099         struct tm const *probe_tm = localtime (&probe);
3100         if (probe_tm && probe_tm->tm_zone
3101             && probe_tm->tm_isdst != pc.local_time_zone_table[0].value)
3102           {
3103               {
3104                 pc.local_time_zone_table[1].name = probe_tm->tm_zone;
3105                 pc.local_time_zone_table[1].type = tLOCAL_ZONE;
3106                 pc.local_time_zone_table[1].value = probe_tm->tm_isdst;
3107                 pc.local_time_zone_table[2].name = NULL;
3108               }
3109             break;
3110           }
3111       }
3112   }
3113 #else
3114 #if HAVE_TZNAME
3115   {
3116 # if !HAVE_DECL_TZNAME
3117     extern char *tzname[];
3118 # endif
3119     int i;
3120     for (i = 0; i < 2; i++)
3121       {
3122         pc.local_time_zone_table[i].name = tzname[i];
3123         pc.local_time_zone_table[i].type = tLOCAL_ZONE;
3124         pc.local_time_zone_table[i].value = i;
3125       }
3126     pc.local_time_zone_table[i].name = NULL;
3127   }
3128 #else
3129   pc.local_time_zone_table[0].name = NULL;
3130 #endif
3131 #endif
3132
3133   if (pc.local_time_zone_table[0].name && pc.local_time_zone_table[1].name
3134       && ! strcmp (pc.local_time_zone_table[0].name,
3135                    pc.local_time_zone_table[1].name))
3136     {
3137       /* This locale uses the same abbrevation for standard and
3138          daylight times.  So if we see that abbreviation, we don't
3139          know whether it's daylight time.  */
3140       pc.local_time_zone_table[0].value = -1;
3141       pc.local_time_zone_table[1].name = NULL;
3142     }
3143
3144   if (yyparse (&pc) != 0)
3145     goto fail;
3146
3147   if (pc.timespec_seen)
3148     *result = pc.seconds;
3149   else
3150     {
3151       if (1 < (pc.times_seen | pc.dates_seen | pc.days_seen | pc.dsts_seen
3152                | (pc.local_zones_seen + pc.zones_seen)))
3153         goto fail;
3154
3155       tm.tm_year = to_year (pc.year) - TM_YEAR_BASE;
3156       tm.tm_mon = pc.month - 1;
3157       tm.tm_mday = pc.day;
3158       if (pc.times_seen || (pc.rels_seen && ! pc.dates_seen && ! pc.days_seen))
3159         {
3160           tm.tm_hour = to_hour (pc.hour, pc.meridian);
3161           if (tm.tm_hour < 0)
3162             goto fail;
3163           tm.tm_min = pc.minutes;
3164           tm.tm_sec = pc.seconds.tv_sec;
3165         }
3166       else
3167         {
3168           tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
3169           pc.seconds.tv_nsec = 0;
3170         }
3171
3172       /* Let mktime deduce tm_isdst if we have an absolute time stamp.  */
3173       if (pc.dates_seen | pc.days_seen | pc.times_seen)
3174         tm.tm_isdst = -1;
3175
3176       /* But if the input explicitly specifies local time with or without
3177          DST, give mktime that information.  */
3178       if (pc.local_zones_seen)
3179         tm.tm_isdst = pc.local_isdst;
3180
3181       tm0 = tm;
3182
3183       Start = mktime (&tm);
3184
3185       if (! mktime_ok (&tm0, &tm, Start))
3186         {
3187           if (! pc.zones_seen)
3188             goto fail;
3189           else
3190             {
3191               /* Guard against falsely reporting errors near the time_t
3192                  boundaries when parsing times in other time zones.  For
3193                  example, suppose the input string "1969-12-31 23:00:00 -0100",
3194                  the current time zone is 8 hours ahead of UTC, and the min
3195                  time_t value is 1970-01-01 00:00:00 UTC.  Then the min
3196                  localtime value is 1970-01-01 08:00:00, and mktime will
3197                  therefore fail on 1969-12-31 23:00:00.  To work around the
3198                  problem, set the time zone to 1 hour behind UTC temporarily
3199                  by setting TZ="XXX1:00" and try mktime again.  */
3200
3201               long int time_zone = pc.time_zone;
3202               long int abs_time_zone = time_zone < 0 ? - time_zone : time_zone;
3203               long int abs_time_zone_hour = abs_time_zone / 60;
3204               int abs_time_zone_min = abs_time_zone % 60;
3205               char tz1buf[sizeof "XXX+0:00"
3206                           + sizeof pc.time_zone * CHAR_BIT / 3];
3207               if (!tz_was_altered)
3208                 tz0 = get_tz (tz0buf);
3209               sprintf (tz1buf, "XXX%s%ld:%02d", "-" + (time_zone < 0),
3210                        abs_time_zone_hour, abs_time_zone_min);
3211               if (setenv ("TZ", tz1buf, 1) != 0)
3212                 goto fail;
3213               tz_was_altered = true;
3214               tm = tm0;
3215               Start = mktime (&tm);
3216               if (! mktime_ok (&tm0, &tm, Start))
3217                 goto fail;
3218             }
3219         }
3220
3221       if (pc.days_seen && ! pc.dates_seen)
3222         {
3223           tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7
3224                          + 7 * (pc.day_ordinal
3225                                 - (0 < pc.day_ordinal
3226                                    && tm.tm_wday != pc.day_number)));
3227           tm.tm_isdst = -1;
3228           Start = mktime (&tm);
3229           if (Start == (time_t) -1)
3230             goto fail;
3231         }
3232
3233       /* Add relative date.  */
3234       if (pc.rel.year | pc.rel.month | pc.rel.day)
3235         {
3236           int year = tm.tm_year + pc.rel.year;
3237           int month = tm.tm_mon + pc.rel.month;
3238           int day = tm.tm_mday + pc.rel.day;
3239           if (((year < tm.tm_year) ^ (pc.rel.year < 0))
3240               | ((month < tm.tm_mon) ^ (pc.rel.month < 0))
3241               | ((day < tm.tm_mday) ^ (pc.rel.day < 0)))
3242             goto fail;
3243           tm.tm_year = year;
3244           tm.tm_mon = month;
3245           tm.tm_mday = day;
3246           tm.tm_hour = tm0.tm_hour;
3247           tm.tm_min = tm0.tm_min;
3248           tm.tm_sec = tm0.tm_sec;
3249           tm.tm_isdst = tm0.tm_isdst;
3250           Start = mktime (&tm);
3251           if (Start == (time_t) -1)
3252             goto fail;
3253         }
3254
3255       /* The only "output" of this if-block is an updated Start value,
3256          so this block must follow others that clobber Start.  */
3257       if (pc.zones_seen)
3258         {
3259           long int delta = pc.time_zone * 60;
3260           time_t t1;
3261 #ifdef HAVE_TM_GMTOFF
3262           delta -= tm.tm_gmtoff;
3263 #else
3264           time_t t = Start;
3265           struct tm const *gmt = gmtime (&t);
3266           if (! gmt)
3267             goto fail;
3268           delta -= tm_diff (&tm, gmt);
3269 #endif
3270           t1 = Start - delta;
3271           if ((Start < t1) != (delta < 0))
3272             goto fail;  /* time_t overflow */
3273           Start = t1;
3274         }
3275
3276       /* Add relative hours, minutes, and seconds.  On hosts that support
3277          leap seconds, ignore the possibility of leap seconds; e.g.,
3278          "+ 10 minutes" adds 600 seconds, even if one of them is a
3279          leap second.  Typically this is not what the user wants, but it's
3280          too hard to do it the other way, because the time zone indicator
3281          must be applied before relative times, and if mktime is applied
3282          again the time zone will be lost.  */
3283       {
3284         long int sum_ns = pc.seconds.tv_nsec + pc.rel.ns;
3285         long int normalized_ns = (sum_ns % BILLION + BILLION) % BILLION;
3286         time_t t0 = Start;
3287         long int d1 = 60 * 60 * pc.rel.hour;
3288         time_t t1 = t0 + d1;
3289         long int d2 = 60 * pc.rel.minutes;
3290         time_t t2 = t1 + d2;
3291         long_time_t d3 = pc.rel.seconds;
3292         long_time_t t3 = t2 + d3;
3293         long int d4 = (sum_ns - normalized_ns) / BILLION;
3294         long_time_t t4 = t3 + d4;
3295         time_t t5 = t4;
3296
3297         if ((d1 / (60 * 60) ^ pc.rel.hour)
3298             | (d2 / 60 ^ pc.rel.minutes)
3299             | ((t1 < t0) ^ (d1 < 0))
3300             | ((t2 < t1) ^ (d2 < 0))
3301             | ((t3 < t2) ^ (d3 < 0))
3302             | ((t4 < t3) ^ (d4 < 0))
3303             | (t5 != t4))
3304           goto fail;
3305
3306         result->tv_sec = t5;
3307         result->tv_nsec = normalized_ns;
3308       }
3309     }
3310
3311   goto done;
3312
3313  fail:
3314   ok = false;
3315  done:
3316   if (tz_was_altered)
3317     ok &= (tz0 ? setenv ("TZ", tz0, 1) : unsetenv ("TZ")) == 0;
3318   if (tz0 != tz0buf)
3319     free (tz0);
3320   return ok;
3321 }
3322
3323 #if TEST
3324
3325 int
3326 main (int ac, char **av)
3327 {
3328   char buff[BUFSIZ];
3329
3330   printf ("Enter date, or blank line to exit.\n\t> ");
3331   fflush (stdout);
3332
3333   buff[BUFSIZ - 1] = '\0';
3334   while (fgets (buff, BUFSIZ - 1, stdin) && buff[0])
3335     {
3336       struct timespec d;
3337       struct tm const *tm;
3338       if (! parse_datetime (&d, buff, NULL))
3339         printf ("Bad format - couldn't convert.\n");
3340       else if (! (tm = localtime (&d.tv_sec)))
3341         {
3342           long int sec = d.tv_sec;
3343           printf ("localtime (%ld) failed\n", sec);
3344         }
3345       else
3346         {
3347           int ns = d.tv_nsec;
3348           printf ("%04ld-%02d-%02d %02d:%02d:%02d.%09d\n",
3349                   tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
3350                   tm->tm_hour, tm->tm_min, tm->tm_sec, ns);
3351         }
3352       printf ("\t> ");
3353       fflush (stdout);
3354     }
3355   return 0;
3356 }
3357 #endif /* TEST */
3358