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