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