]> Dogcows Code - chaz/tar/blob - lib/getdate.y
Fix typo in previous change.
[chaz/tar] / lib / getdate.y
1 %{
2 /* Parse a string into an internal time stamp.
3 Copyright 1999 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19 /* Originally written by Steven M. Bellovin <smb@research.att.com> while
20 at the University of North Carolina at Chapel Hill. Later tweaked by
21 a couple of people on Usenet. Completely overhauled by Rich $alz
22 <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990.
23
24 Modified by Paul Eggert <eggert@twinsun.com> in August 1999 to do
25 the right thing about local DST. Unlike previous versions, this
26 version is reentrant. */
27
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 # ifdef HAVE_ALLOCA_H
31 # include <alloca.h>
32 # endif
33 #endif
34
35 /* Since the code of getdate.y is not included in the Emacs executable
36 itself, there is no need to #define static in this file. Even if
37 the code were included in the Emacs executable, it probably
38 wouldn't do any harm to #undef it here; this will only cause
39 problems if we try to write to a static variable, which I don't
40 think this code needs to do. */
41 #ifdef emacs
42 # undef static
43 #endif
44
45 #include <ctype.h>
46
47 #if HAVE_STDLIB_H
48 # include <stdlib.h> /* for `free'; used by Bison 1.27 */
49 #endif
50
51 #if STDC_HEADERS || (! defined isascii && ! HAVE_ISASCII)
52 # define IN_CTYPE_DOMAIN(c) 1
53 #else
54 # define IN_CTYPE_DOMAIN(c) isascii (c)
55 #endif
56
57 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
58 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
59 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
60 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
61
62 /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
63 - Its arg may be any int or unsigned int; it need not be an unsigned char.
64 - It's guaranteed to evaluate its argument exactly once.
65 - It's typically faster.
66 Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
67 only '0' through '9' are digits. Prefer ISDIGIT to ISDIGIT_LOCALE unless
68 it's important to use the locale's definition of `digit' even when the
69 host does not conform to Posix. */
70 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
71
72 #if STDC_HEADERS || HAVE_STRING_H
73 # include <string.h>
74 #endif
75
76 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
77 # define __attribute__(x)
78 #endif
79
80 #ifndef ATTRIBUTE_UNUSED
81 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
82 #endif
83
84 #define EPOCH_YEAR 1970
85 #define TM_YEAR_ORIGIN 1900
86
87 #define HOUR(x) ((x) * 60)
88
89 /* An entry in the lexical lookup table. */
90 typedef struct
91 {
92 char const *name;
93 int type;
94 int value;
95 } table;
96
97 /* Meridian: am, pm, or 24-hour style. */
98 enum { MERam, MERpm, MER24 };
99
100 /* Information passed to and from the parser. */
101 struct parser_control
102 {
103 /* The input string remaining to be parsed. */
104 const char *input;
105
106 /* N, if this is the Nth Tuesday. */
107 int day_ordinal;
108
109 /* Day of week; Sunday is 0. */
110 int day_number;
111
112 /* tm_isdst flag for the local zone. */
113 int local_isdst;
114
115 /* Time zone, in minutes east of UTC. */
116 int time_zone;
117
118 /* Style used for time. */
119 int meridian;
120
121 /* Gregorian year, month, day, hour, minutes, and seconds. */
122 int year;
123 int month;
124 int day;
125 int hour;
126 int minutes;
127 int seconds;
128
129 /* Relative year, month, day, hour, minutes, and seconds. */
130 int rel_year;
131 int rel_month;
132 int rel_day;
133 int rel_hour;
134 int rel_minutes;
135 int rel_seconds;
136
137 /* Counts of nonterminals of various flavors parsed so far. */
138 int dates_seen;
139 int days_seen;
140 int local_zones_seen;
141 int rels_seen;
142 int times_seen;
143 int zones_seen;
144
145 /* Table of local time zone abbrevations, terminated by a null entry. */
146 table local_time_zone_table[3];
147 };
148
149 #define YYLEX_PARAM parm
150 #define YYPARSE_PARAM parm
151 #define YYSTYPE int
152
153 static int yyerror ();
154 static int yylex ();
155
156 %}
157
158 /* We want a reentrant parser. */
159 %pure_parser
160
161 /* This grammar has 13 shift/reduce conflicts. */
162 %expect 13
163
164 %token tAGO tDAY tDAY_UNIT tDAYZONE tDST tHOUR_UNIT tID
165 %token tLOCAL_ZONE tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
166 %token tSEC_UNIT tSNUMBER tUNUMBER tYEAR_UNIT tZONE
167
168 %%
169
170 spec:
171 /* empty */
172 | spec item
173 ;
174
175 item:
176 time
177 { ((struct parser_control *) parm)->times_seen++; }
178 | local_zone
179 { ((struct parser_control *) parm)->local_zones_seen++; }
180 | zone
181 { ((struct parser_control *) parm)->zones_seen++; }
182 | date
183 { ((struct parser_control *) parm)->dates_seen++; }
184 | day
185 { ((struct parser_control *) parm)->days_seen++; }
186 | rel
187 { ((struct parser_control *) parm)->rels_seen++; }
188 | number
189 ;
190
191 time:
192 tUNUMBER tMERIDIAN
193 {
194 ((struct parser_control *) parm)->hour = $1;
195 ((struct parser_control *) parm)->minutes = 0;
196 ((struct parser_control *) parm)->seconds = 0;
197 ((struct parser_control *) parm)->meridian = $2;
198 }
199 | tUNUMBER ':' tUNUMBER o_merid
200 {
201 ((struct parser_control *) parm)->hour = $1;
202 ((struct parser_control *) parm)->minutes = $3;
203 ((struct parser_control *) parm)->seconds = 0;
204 ((struct parser_control *) parm)->meridian = $4;
205 }
206 | tUNUMBER ':' tUNUMBER tSNUMBER
207 {
208 ((struct parser_control *) parm)->hour = $1;
209 ((struct parser_control *) parm)->minutes = $3;
210 ((struct parser_control *) parm)->meridian = MER24;
211 ((struct parser_control *) parm)->zones_seen++;
212 ((struct parser_control *) parm)->time_zone =
213 $4 % 100 + ($4 / 100) * 60;
214 }
215 | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid
216 {
217 ((struct parser_control *) parm)->hour = $1;
218 ((struct parser_control *) parm)->minutes = $3;
219 ((struct parser_control *) parm)->seconds = $5;
220 ((struct parser_control *) parm)->meridian = $6;
221 }
222 | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER
223 {
224 ((struct parser_control *) parm)->hour = $1;
225 ((struct parser_control *) parm)->minutes = $3;
226 ((struct parser_control *) parm)->seconds = $5;
227 ((struct parser_control *) parm)->meridian = MER24;
228 ((struct parser_control *) parm)->zones_seen++;
229 ((struct parser_control *) parm)->time_zone =
230 $6 % 100 + ($6 / 100) * 60;
231 }
232 ;
233
234 local_zone:
235 tLOCAL_ZONE
236 { ((struct parser_control *) parm)->local_isdst = $1; }
237 | tLOCAL_ZONE tDST
238 { ((struct parser_control *) parm)->local_isdst = $1 < 0 ? 1 : $1 + 1; }
239 ;
240
241 zone:
242 tZONE
243 { ((struct parser_control *) parm)->time_zone = $1; }
244 | tDAYZONE
245 { ((struct parser_control *) parm)->time_zone = $1 + 60; }
246 | tZONE tDST
247 { ((struct parser_control *) parm)->time_zone = $1 + 60; }
248 ;
249
250 day:
251 tDAY
252 {
253 ((struct parser_control *) parm)->day_ordinal = 1;
254 ((struct parser_control *) parm)->day_number = $1;
255 }
256 | tDAY ','
257 {
258 ((struct parser_control *) parm)->day_ordinal = 1;
259 ((struct parser_control *) parm)->day_number = $1;
260 }
261 | tUNUMBER tDAY
262 {
263 ((struct parser_control *) parm)->day_ordinal = $1;
264 ((struct parser_control *) parm)->day_number = $2;
265 }
266 ;
267
268 date:
269 tUNUMBER '/' tUNUMBER
270 {
271 ((struct parser_control *) parm)->month = $1;
272 ((struct parser_control *) parm)->day = $3;
273 }
274 | tUNUMBER '/' tUNUMBER '/' tUNUMBER
275 {
276 /* Interpret as YYYY/MM/DD if 1000 <= $1, otherwise as MM/DD/YY.
277 The goal in recognizing YYYY/MM/DD is solely to support legacy
278 machine-generated dates like those in an RCS log listing. If
279 you want portability, use the ISO 8601 format. */
280 if (1000 <= $1)
281 {
282 ((struct parser_control *) parm)->year = $1;
283 ((struct parser_control *) parm)->month = $3;
284 ((struct parser_control *) parm)->day = $5;
285 }
286 else
287 {
288 ((struct parser_control *) parm)->month = $1;
289 ((struct parser_control *) parm)->day = $3;
290 ((struct parser_control *) parm)->year = $5;
291 }
292 }
293 | tUNUMBER tSNUMBER tSNUMBER
294 {
295 /* ISO 8601 format. YYYY-MM-DD. */
296 ((struct parser_control *) parm)->year = $1;
297 ((struct parser_control *) parm)->month = -$2;
298 ((struct parser_control *) parm)->day = -$3;
299 }
300 | tUNUMBER tMONTH tSNUMBER
301 {
302 /* e.g. 17-JUN-1992. */
303 ((struct parser_control *) parm)->day = $1;
304 ((struct parser_control *) parm)->month = $2;
305 ((struct parser_control *) parm)->year = -$3;
306 }
307 | tMONTH tUNUMBER
308 {
309 ((struct parser_control *) parm)->month = $1;
310 ((struct parser_control *) parm)->day = $2;
311 }
312 | tMONTH tUNUMBER ',' tUNUMBER
313 {
314 ((struct parser_control *) parm)->month = $1;
315 ((struct parser_control *) parm)->day = $2;
316 ((struct parser_control *) parm)->year = $4;
317 }
318 | tUNUMBER tMONTH
319 {
320 ((struct parser_control *) parm)->month = $2;
321 ((struct parser_control *) parm)->day = $1;
322 }
323 | tUNUMBER tMONTH tUNUMBER
324 {
325 ((struct parser_control *) parm)->month = $2;
326 ((struct parser_control *) parm)->day = $1;
327 ((struct parser_control *) parm)->year = $3;
328 }
329 ;
330
331 rel:
332 relunit tAGO
333 {
334 ((struct parser_control *) parm)->rel_seconds = -((struct parser_control *) parm)->rel_seconds;
335 ((struct parser_control *) parm)->rel_minutes = -((struct parser_control *) parm)->rel_minutes;
336 ((struct parser_control *) parm)->rel_hour = -((struct parser_control *) parm)->rel_hour;
337 ((struct parser_control *) parm)->rel_day = -((struct parser_control *) parm)->rel_day;
338 ((struct parser_control *) parm)->rel_month = -((struct parser_control *) parm)->rel_month;
339 ((struct parser_control *) parm)->rel_year = -((struct parser_control *) parm)->rel_year;
340 }
341 | relunit
342 ;
343
344 relunit:
345 tUNUMBER tYEAR_UNIT
346 { ((struct parser_control *) parm)->rel_year += $1 * $2; }
347 | tSNUMBER tYEAR_UNIT
348 { ((struct parser_control *) parm)->rel_year += $1 * $2; }
349 | tYEAR_UNIT
350 { ((struct parser_control *) parm)->rel_year += $1; }
351 | tUNUMBER tMONTH_UNIT
352 { ((struct parser_control *) parm)->rel_month += $1 * $2; }
353 | tSNUMBER tMONTH_UNIT
354 { ((struct parser_control *) parm)->rel_month += $1 * $2; }
355 | tMONTH_UNIT
356 { ((struct parser_control *) parm)->rel_month += $1; }
357 | tUNUMBER tDAY_UNIT
358 { ((struct parser_control *) parm)->rel_day += $1 * $2; }
359 | tSNUMBER tDAY_UNIT
360 { ((struct parser_control *) parm)->rel_day += $1 * $2; }
361 | tDAY_UNIT
362 { ((struct parser_control *) parm)->rel_day += $1; }
363 | tUNUMBER tHOUR_UNIT
364 { ((struct parser_control *) parm)->rel_hour += $1 * $2; }
365 | tSNUMBER tHOUR_UNIT
366 { ((struct parser_control *) parm)->rel_hour += $1 * $2; }
367 | tHOUR_UNIT
368 { ((struct parser_control *) parm)->rel_hour += $1; }
369 | tUNUMBER tMINUTE_UNIT
370 { ((struct parser_control *) parm)->rel_minutes += $1 * $2; }
371 | tSNUMBER tMINUTE_UNIT
372 { ((struct parser_control *) parm)->rel_minutes += $1 * $2; }
373 | tMINUTE_UNIT
374 { ((struct parser_control *) parm)->rel_minutes += $1; }
375 | tUNUMBER tSEC_UNIT
376 { ((struct parser_control *) parm)->rel_seconds += $1 * $2; }
377 | tSNUMBER tSEC_UNIT
378 { ((struct parser_control *) parm)->rel_seconds += $1 * $2; }
379 | tSEC_UNIT
380 { ((struct parser_control *) parm)->rel_seconds += $1; }
381 ;
382
383 number:
384 tUNUMBER
385 {
386 if (((struct parser_control *) parm)->times_seen
387 && ((struct parser_control *) parm)->dates_seen
388 && ! ((struct parser_control *) parm)->rels_seen)
389 ((struct parser_control *) parm)->year = $1;
390 else
391 {
392 if (10000 < $1)
393 {
394 ((struct parser_control *) parm)->dates_seen++;
395 ((struct parser_control *) parm)->day = $1 % 100;
396 ((struct parser_control *) parm)->month = ($1 / 100) % 100;
397 ((struct parser_control *) parm)->year = $1 / 10000;
398 }
399 else
400 {
401 ((struct parser_control *) parm)->times_seen++;
402 if ($1 < 100)
403 {
404 ((struct parser_control *) parm)->hour = $1;
405 ((struct parser_control *) parm)->minutes = 0;
406 }
407 else
408 {
409 ((struct parser_control *) parm)->hour = $1 / 100;
410 ((struct parser_control *) parm)->minutes = $1 % 100;
411 }
412 ((struct parser_control *) parm)->seconds = 0;
413 ((struct parser_control *) parm)->meridian = MER24;
414 }
415 }
416 }
417 ;
418
419 o_merid:
420 /* empty */
421 { $$ = MER24; }
422 | tMERIDIAN
423 { $$ = $1; }
424 ;
425
426 %%
427
428 /* Include this file down here because bison inserts code above which
429 may define-away `const'. We want the prototype for get_date to have
430 the same signature as the function definition. */
431 #include "getdate.h"
432
433 #ifndef gmtime
434 struct tm *gmtime ();
435 #endif
436 #ifndef localtime
437 struct tm *localtime ();
438 #endif
439 #ifndef mktime
440 time_t mktime ();
441 #endif
442
443 static table const meridian_table[] =
444 {
445 { "AM", tMERIDIAN, MERam },
446 { "A.M.", tMERIDIAN, MERam },
447 { "PM", tMERIDIAN, MERpm },
448 { "P.M.", tMERIDIAN, MERpm },
449 { 0, 0, 0 }
450 };
451
452 static table const dst_table[] =
453 {
454 { "DST", tDST, 0 }
455 };
456
457 static table const month_and_day_table[] =
458 {
459 { "JANUARY", tMONTH, 1 },
460 { "FEBRUARY", tMONTH, 2 },
461 { "MARCH", tMONTH, 3 },
462 { "APRIL", tMONTH, 4 },
463 { "MAY", tMONTH, 5 },
464 { "JUNE", tMONTH, 6 },
465 { "JULY", tMONTH, 7 },
466 { "AUGUST", tMONTH, 8 },
467 { "SEPTEMBER",tMONTH, 9 },
468 { "SEPT", tMONTH, 9 },
469 { "OCTOBER", tMONTH, 10 },
470 { "NOVEMBER", tMONTH, 11 },
471 { "DECEMBER", tMONTH, 12 },
472 { "SUNDAY", tDAY, 0 },
473 { "MONDAY", tDAY, 1 },
474 { "TUESDAY", tDAY, 2 },
475 { "TUES", tDAY, 2 },
476 { "WEDNESDAY",tDAY, 3 },
477 { "WEDNES", tDAY, 3 },
478 { "THURSDAY", tDAY, 4 },
479 { "THUR", tDAY, 4 },
480 { "THURS", tDAY, 4 },
481 { "FRIDAY", tDAY, 5 },
482 { "SATURDAY", tDAY, 6 },
483 { 0, 0, 0 }
484 };
485
486 static table const time_units_table[] =
487 {
488 { "YEAR", tYEAR_UNIT, 1 },
489 { "MONTH", tMONTH_UNIT, 1 },
490 { "FORTNIGHT",tDAY_UNIT, 14 },
491 { "WEEK", tDAY_UNIT, 7 },
492 { "DAY", tDAY_UNIT, 1 },
493 { "HOUR", tHOUR_UNIT, 1 },
494 { "MINUTE", tMINUTE_UNIT, 1 },
495 { "MIN", tMINUTE_UNIT, 1 },
496 { "SECOND", tSEC_UNIT, 1 },
497 { "SEC", tSEC_UNIT, 1 },
498 { 0, 0, 0 }
499 };
500
501 /* Assorted relative-time words. */
502 static table const relative_time_table[] =
503 {
504 { "TOMORROW", tMINUTE_UNIT, 24 * 60 },
505 { "YESTERDAY",tMINUTE_UNIT, - (24 * 60) },
506 { "TODAY", tMINUTE_UNIT, 0 },
507 { "NOW", tMINUTE_UNIT, 0 },
508 { "LAST", tUNUMBER, -1 },
509 { "THIS", tMINUTE_UNIT, 0 },
510 { "NEXT", tUNUMBER, 1 },
511 { "FIRST", tUNUMBER, 1 },
512 /*{ "SECOND", tUNUMBER, 2 }, */
513 { "THIRD", tUNUMBER, 3 },
514 { "FOURTH", tUNUMBER, 4 },
515 { "FIFTH", tUNUMBER, 5 },
516 { "SIXTH", tUNUMBER, 6 },
517 { "SEVENTH", tUNUMBER, 7 },
518 { "EIGHTH", tUNUMBER, 8 },
519 { "NINTH", tUNUMBER, 9 },
520 { "TENTH", tUNUMBER, 10 },
521 { "ELEVENTH", tUNUMBER, 11 },
522 { "TWELFTH", tUNUMBER, 12 },
523 { "AGO", tAGO, 1 },
524 { 0, 0, 0 }
525 };
526
527 /* The time zone table. This table is necessarily incomplete, as time
528 zone abbreviations are ambiguous; e.g. Australians interpret "EST"
529 as Eastern time in Australia, not as US Eastern Standard Time.
530 You cannot rely on getdate to handle arbitrary time zone
531 abbreviations; use numeric abbreviations like `-0500' instead. */
532 static table const time_zone_table[] =
533 {
534 { "GMT", tZONE, HOUR ( 0) }, /* Greenwich Mean */
535 { "UT", tZONE, HOUR ( 0) }, /* Universal (Coordinated) */
536 { "UTC", tZONE, HOUR ( 0) },
537 { "WET", tZONE, HOUR ( 0) }, /* Western European */
538 { "WEST", tDAYZONE, HOUR ( 0) }, /* Western European Summer */
539 { "BST", tDAYZONE, HOUR ( 0) }, /* British Summer */
540 { "ART", tZONE, -HOUR ( 3) }, /* Argentina */
541 { "BRT", tZONE, -HOUR ( 3) }, /* Brazil */
542 { "BRST", tDAYZONE, -HOUR ( 3) }, /* Brazil Summer */
543 { "NST", tZONE, -(HOUR ( 3) + 30) }, /* Newfoundland Standard */
544 { "NDT", tDAYZONE,-(HOUR ( 3) + 30) }, /* Newfoundland Daylight */
545 { "AST", tZONE, -HOUR ( 4) }, /* Atlantic Standard */
546 { "ADT", tDAYZONE, -HOUR ( 4) }, /* Atlantic Daylight */
547 { "CLT", tZONE, -HOUR ( 4) }, /* Chile */
548 { "CLST", tDAYZONE, -HOUR ( 4) }, /* Chile Summer */
549 { "EST", tZONE, -HOUR ( 5) }, /* Eastern Standard */
550 { "EDT", tDAYZONE, -HOUR ( 5) }, /* Eastern Daylight */
551 { "CST", tZONE, -HOUR ( 6) }, /* Central Standard */
552 { "CDT", tDAYZONE, -HOUR ( 6) }, /* Central Daylight */
553 { "MST", tZONE, -HOUR ( 7) }, /* Mountain Standard */
554 { "MDT", tDAYZONE, -HOUR ( 7) }, /* Mountain Daylight */
555 { "PST", tZONE, -HOUR ( 8) }, /* Pacific Standard */
556 { "PDT", tDAYZONE, -HOUR ( 8) }, /* Pacific Daylight */
557 { "AKST", tZONE, -HOUR ( 9) }, /* Alaska Standard */
558 { "AKDT", tDAYZONE, -HOUR ( 9) }, /* Alaska Daylight */
559 { "HST", tZONE, -HOUR (10) }, /* Hawaii Standard */
560 { "HAST", tZONE, -HOUR (10) }, /* Hawaii-Aleutian Standard */
561 { "HADT", tDAYZONE, -HOUR (10) }, /* Hawaii-Aleutian Daylight */
562 { "SST", tZONE, -HOUR (12) }, /* Samoa Standard */
563 { "WAT", tZONE, HOUR ( 1) }, /* West Africa */
564 { "CET", tZONE, HOUR ( 1) }, /* Central European */
565 { "CEST", tDAYZONE, HOUR ( 1) }, /* Central European Summer */
566 { "MET", tZONE, HOUR ( 1) }, /* Middle European */
567 { "MEZ", tZONE, HOUR ( 1) }, /* Middle European */
568 { "MEST", tDAYZONE, HOUR ( 1) }, /* Middle European Summer */
569 { "MESZ", tDAYZONE, HOUR ( 1) }, /* Middle European Summer */
570 { "EET", tZONE, HOUR ( 2) }, /* Eastern European */
571 { "EEST", tDAYZONE, HOUR ( 2) }, /* Eastern European Summer */
572 { "CAT", tZONE, HOUR ( 2) }, /* Central Africa */
573 { "SAST", tZONE, HOUR ( 2) }, /* South Africa Standard */
574 { "EAT", tZONE, HOUR ( 3) }, /* East Africa */
575 { "MSK", tZONE, HOUR ( 3) }, /* Moscow */
576 { "MSD", tDAYZONE, HOUR ( 3) }, /* Moscow Daylight */
577 { "IST", tZONE, (HOUR ( 5) + 30) }, /* India Standard */
578 { "SGT", tZONE, HOUR ( 8) }, /* Singapore */
579 { "KST", tZONE, HOUR ( 9) }, /* Korea Standard */
580 { "JST", tZONE, HOUR ( 9) }, /* Japan Standard */
581 { "GST", tZONE, HOUR (10) }, /* Guam Standard */
582 { "NZST", tZONE, HOUR (12) }, /* New Zealand Standard */
583 { "NZDT", tDAYZONE, HOUR (12) }, /* New Zealand Daylight */
584 { 0, 0, 0 }
585 };
586
587 /* Military time zone table. */
588 static table const military_table[] =
589 {
590 { "A", tZONE, -HOUR ( 1) },
591 { "B", tZONE, -HOUR ( 2) },
592 { "C", tZONE, -HOUR ( 3) },
593 { "D", tZONE, -HOUR ( 4) },
594 { "E", tZONE, -HOUR ( 5) },
595 { "F", tZONE, -HOUR ( 6) },
596 { "G", tZONE, -HOUR ( 7) },
597 { "H", tZONE, -HOUR ( 8) },
598 { "I", tZONE, -HOUR ( 9) },
599 { "K", tZONE, -HOUR (10) },
600 { "L", tZONE, -HOUR (11) },
601 { "M", tZONE, -HOUR (12) },
602 { "N", tZONE, HOUR ( 1) },
603 { "O", tZONE, HOUR ( 2) },
604 { "P", tZONE, HOUR ( 3) },
605 { "Q", tZONE, HOUR ( 4) },
606 { "R", tZONE, HOUR ( 5) },
607 { "S", tZONE, HOUR ( 6) },
608 { "T", tZONE, HOUR ( 7) },
609 { "U", tZONE, HOUR ( 8) },
610 { "V", tZONE, HOUR ( 9) },
611 { "W", tZONE, HOUR (10) },
612 { "X", tZONE, HOUR (11) },
613 { "Y", tZONE, HOUR (12) },
614 { "Z", tZONE, HOUR ( 0) },
615 { 0, 0, 0 }
616 };
617
618 \f
619
620 static int
621 to_hour (int hours, int meridian)
622 {
623 switch (meridian)
624 {
625 case MER24:
626 return 0 <= hours && hours < 24 ? hours : -1;
627 case MERam:
628 return 0 < hours && hours < 12 ? hours : hours == 12 ? 0 : -1;
629 case MERpm:
630 return 0 < hours && hours < 12 ? hours + 12 : hours == 12 ? 12 : -1;
631 default:
632 abort ();
633 }
634 /* NOTREACHED */
635 }
636
637 static int
638 to_year (int year)
639 {
640 if (year < 0)
641 year = -year;
642
643 /* XPG4 suggests that years 00-68 map to 2000-2068, and
644 years 69-99 map to 1969-1999. */
645 if (year < 69)
646 year += 2000;
647 else if (year < 100)
648 year += 1900;
649
650 return year;
651 }
652
653 static table const *
654 lookup_zone (struct parser_control const *pc, char const *name)
655 {
656 table const *tp;
657
658 /* Try local zone abbreviations first; they're more likely to be right. */
659 for (tp = pc->local_time_zone_table; tp->name; tp++)
660 if (strcmp (name, tp->name) == 0)
661 return tp;
662
663 for (tp = time_zone_table; tp->name; tp++)
664 if (strcmp (name, tp->name) == 0)
665 return tp;
666
667 return 0;
668 }
669
670 /* Yield A - B, measured in seconds. */
671 static int
672 difftm (struct tm *a, struct tm *b)
673 {
674 int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
675 int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
676 int days = (
677 /* difference in day of year */
678 a->tm_yday - b->tm_yday
679 /* + intervening leap days */
680 + ((ay >> 2) - (by >> 2))
681 - (ay / 100 - by / 100)
682 + ((ay / 100 >> 2) - (by / 100 >> 2))
683 /* + difference in years * 365 */
684 + (int) (ay - by) * 365
685 );
686 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
687 + (a->tm_min - b->tm_min))
688 + (a->tm_sec - b->tm_sec));
689 }
690
691 static table const *
692 lookup_word (struct parser_control const *pc, char *word)
693 {
694 char *p;
695 char *q;
696 size_t wordlen;
697 table const *tp;
698 int i;
699 int abbrev;
700
701 /* Make it uppercase. */
702 for (p = word; *p; p++)
703 if (ISLOWER ((unsigned char) *p))
704 *p = toupper ((unsigned char) *p);
705
706 for (tp = meridian_table; tp->name; tp++)
707 if (strcmp (word, tp->name) == 0)
708 return tp;
709
710 /* See if we have an abbreviation for a month. */
711 wordlen = strlen (word);
712 abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.');
713
714 for (tp = month_and_day_table; tp->name; tp++)
715 if ((abbrev ? strncmp (word, tp->name, 3) : strcmp (word, tp->name)) == 0)
716 return tp;
717
718 if ((tp = lookup_zone (pc, word)))
719 return tp;
720
721 if (strcmp (word, dst_table[0].name) == 0)
722 return dst_table;
723
724 for (tp = time_units_table; tp->name; tp++)
725 if (strcmp (word, tp->name) == 0)
726 return tp;
727
728 /* Strip off any plural and try the units table again. */
729 if (word[wordlen - 1] == 'S')
730 {
731 word[wordlen - 1] = '\0';
732 for (tp = time_units_table; tp->name; tp++)
733 if (strcmp (word, tp->name) == 0)
734 return tp;
735 word[wordlen - 1] = 'S'; /* For "this" in relative_time_table. */
736 }
737
738 for (tp = relative_time_table; tp->name; tp++)
739 if (strcmp (word, tp->name) == 0)
740 return tp;
741
742 /* Military time zones. */
743 if (wordlen == 1)
744 for (tp = military_table; tp->name; tp++)
745 if (word[0] == tp->name[0])
746 return tp;
747
748 /* Drop out any periods and try the time zone table again. */
749 for (i = 0, p = q = word; (*p = *q); q++)
750 if (*q == '.')
751 i = 1;
752 else
753 p++;
754 if (i && (tp = lookup_zone (pc, word)))
755 return tp;
756
757 return 0;
758 }
759
760 static int
761 yylex (YYSTYPE *lvalp, struct parser_control *pc)
762 {
763 unsigned char c;
764 int count;
765
766 for (;;)
767 {
768 while (c = *pc->input, ISSPACE (c))
769 pc->input++;
770
771 if (ISDIGIT (c) || c == '-' || c == '+')
772 {
773 int sign;
774 if (c == '-' || c == '+')
775 {
776 sign = c == '-' ? -1 : 1;
777 if (! ISDIGIT (*++pc->input))
778 /* skip the '-' sign */
779 continue;
780 }
781 else
782 sign = 0;
783 for (*lvalp = 0; ISDIGIT (c = *pc->input++);)
784 *lvalp = 10 * *lvalp + (c - '0');
785 pc->input--;
786 if (sign < 0)
787 *lvalp = - *lvalp;
788 return sign ? tSNUMBER : tUNUMBER;
789 }
790
791 if (ISALPHA (c))
792 {
793 char buff[20];
794 char *p = buff;
795 table const *tp;
796
797 do
798 {
799 if (p < buff + sizeof buff - 1)
800 *p++ = c;
801 c = *++pc->input;
802 }
803 while (ISALPHA (c) || c == '.');
804
805 *p = '\0';
806 tp = lookup_word (pc, buff);
807 if (! tp)
808 return tID;
809 *lvalp = tp->value;
810 return tp->type;
811 }
812
813 if (c != '(')
814 return *pc->input++;
815 count = 0;
816 do
817 {
818 c = *pc->input++;
819 if (c == '\0')
820 return c;
821 if (c == '(')
822 count++;
823 else if (c == ')')
824 count--;
825 }
826 while (count > 0);
827 }
828 }
829
830 /* Do nothing if the parser reports an error. */
831 static int
832 yyerror (char *s ATTRIBUTE_UNUSED)
833 {
834 return 0;
835 }
836
837 /* ?? */
838 time_t
839 get_date (const char *p, const time_t *now)
840 {
841 time_t Start = now ? *now : time (0);
842 struct tm *tmp = localtime (&Start);
843 struct tm tm;
844 struct tm tm0;
845 struct parser_control pc;
846
847 if (! tmp)
848 return -1;
849
850 pc.input = p;
851 pc.year = tmp->tm_year + TM_YEAR_ORIGIN;
852 pc.month = tmp->tm_mon + 1;
853 pc.day = tmp->tm_mday;
854 pc.hour = tmp->tm_hour;
855 pc.minutes = tmp->tm_min;
856 pc.seconds = tmp->tm_sec;
857 tm.tm_isdst = tmp->tm_isdst;
858
859 pc.meridian = MER24;
860 pc.rel_seconds = 0;
861 pc.rel_minutes = 0;
862 pc.rel_hour = 0;
863 pc.rel_day = 0;
864 pc.rel_month = 0;
865 pc.rel_year = 0;
866 pc.dates_seen = 0;
867 pc.days_seen = 0;
868 pc.rels_seen = 0;
869 pc.times_seen = 0;
870 pc.local_zones_seen = 0;
871 pc.zones_seen = 0;
872
873 #if HAVE_TM_ZONE
874 pc.local_time_zone_table[0].name = tmp->tm_zone;
875 pc.local_time_zone_table[0].type = tLOCAL_ZONE;
876 pc.local_time_zone_table[0].value = tmp->tm_isdst;
877 pc.local_time_zone_table[1].name = 0;
878
879 /* Probe the names used in the next three calendar quarters, looking
880 for a tm_isdst different from the one we already have. */
881 {
882 int probe;
883 for (probe = 1; probe <= 3; probe++)
884 {
885 time_t probe = Start + probe * (90 * 24 * 60 * 60);
886 struct tm *tm = localtime (&probe);
887 if (tm && tm->tm_zone
888 && tm->tm_isdst != pc.local_time_zone_table[0].value)
889 {
890 {
891 pc.local_time_zone_table[1].name = tm->tm_zone;
892 pc.local_time_zone_table[1].type = tLOCAL_ZONE;
893 pc.local_time_zone_table[1].value = tm->tm_isdst;
894 pc.local_time_zone_table[2].name = 0;
895 }
896 break;
897 }
898 }
899 }
900 #else
901 #if HAVE_TZNAME
902 {
903 # ifndef tzname
904 extern char *tzname[];
905 # endif
906 int i;
907 for (i = 0; i < 2; i++)
908 {
909 pc.local_time_zone_table[i].name = tzname[i];
910 pc.local_time_zone_table[i].type = tLOCAL_ZONE;
911 pc.local_time_zone_table[i].value = i;
912 }
913 pc.local_time_zone_table[i].name = 0;
914 }
915 #else
916 pc.local_time_zone_table[0].name = 0;
917 #endif
918 #endif
919
920 if (pc.local_time_zone_table[0].name && pc.local_time_zone_table[1].name
921 && ! strcmp (pc.local_time_zone_table[0].name,
922 pc.local_time_zone_table[1].name))
923 {
924 /* This locale uses the same abbrevation for standard and
925 daylight times. So if we see that abbreviation, we don't
926 know whether it's daylight time. */
927 pc.local_time_zone_table[0].value = -1;
928 pc.local_time_zone_table[1].name = 0;
929 }
930
931 if (yyparse (&pc) != 0
932 || 1 < pc.times_seen || 1 < pc.dates_seen || 1 < pc.days_seen
933 || 1 < (pc.local_zones_seen + pc.zones_seen)
934 || (pc.local_zones_seen && 1 < pc.local_isdst))
935 return -1;
936
937 tm.tm_year = to_year (pc.year) - TM_YEAR_ORIGIN + pc.rel_year;
938 tm.tm_mon = pc.month - 1 + pc.rel_month;
939 tm.tm_mday = pc.day + pc.rel_day;
940 if (pc.times_seen || (pc.rels_seen && ! pc.dates_seen && ! pc.days_seen))
941 {
942 tm.tm_hour = to_hour (pc.hour, pc.meridian);
943 if (tm.tm_hour < 0)
944 return -1;
945 tm.tm_min = pc.minutes;
946 tm.tm_sec = pc.seconds;
947 }
948 else
949 {
950 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
951 }
952 tm.tm_hour += pc.rel_hour;
953 tm.tm_min += pc.rel_minutes;
954 tm.tm_sec += pc.rel_seconds;
955
956 /* Let mktime deduce tm_isdst if we have an absolute time stamp,
957 or if the relative time stamp mentions days, months, or years. */
958 if (pc.dates_seen | pc.days_seen | pc.times_seen | pc.rel_day | pc.rel_month | pc.rel_year)
959 tm.tm_isdst = -1;
960
961 /* But if the input explicitly specifies local time with or without
962 DST, give mktime that information. */
963 if (pc.local_zones_seen)
964 tm.tm_isdst = pc.local_isdst;
965
966 tm0 = tm;
967
968 Start = mktime (&tm);
969
970 if (Start == (time_t) -1)
971 {
972
973 /* Guard against falsely reporting errors near the time_t boundaries
974 when parsing times in other time zones. For example, if the min
975 time_t value is 1970-01-01 00:00:00 UTC and we are 8 hours ahead
976 of UTC, then the min localtime value is 1970-01-01 08:00:00; if
977 we apply mktime to 1970-01-01 00:00:00 we will get an error, so
978 we apply mktime to 1970-01-02 08:00:00 instead and adjust the time
979 zone by 24 hours to compensate. This algorithm assumes that
980 there is no DST transition within a day of the time_t boundaries. */
981 if (pc.zones_seen)
982 {
983 tm = tm0;
984 if (tm.tm_year <= EPOCH_YEAR - TM_YEAR_ORIGIN)
985 {
986 tm.tm_mday++;
987 pc.time_zone += 24 * 60;
988 }
989 else
990 {
991 tm.tm_mday--;
992 pc.time_zone -= 24 * 60;
993 }
994 Start = mktime (&tm);
995 }
996
997 if (Start == (time_t) -1)
998 return Start;
999 }
1000
1001 if (pc.days_seen && ! pc.dates_seen)
1002 {
1003 tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7
1004 + 7 * (pc.day_ordinal - (0 < pc.day_ordinal)));
1005 Start = mktime (&tm);
1006 if (Start == (time_t) -1)
1007 return Start;
1008 }
1009
1010 if (pc.zones_seen)
1011 {
1012 int delta;
1013 struct tm *gmt = gmtime (&Start);
1014 if (! gmt)
1015 return -1;
1016 delta = pc.time_zone * 60 + difftm (gmt, &tm);
1017 if ((Start - delta < Start) != (delta < 0))
1018 return -1; /* time_t overflow */
1019 Start -= delta;
1020 }
1021
1022 return Start;
1023 }
1024
1025 #if TEST
1026
1027 #include <stdio.h>
1028
1029 int
1030 main (int ac, char **av)
1031 {
1032 char buff[BUFSIZ];
1033 time_t d;
1034
1035 printf ("Enter date, or blank line to exit.\n\t> ");
1036 fflush (stdout);
1037
1038 buff[BUFSIZ - 1] = 0;
1039 while (fgets (buff, BUFSIZ - 1, stdin) && buff[0])
1040 {
1041 d = get_date (buff, 0);
1042 if (d == (time_t) -1)
1043 printf ("Bad format - couldn't convert.\n");
1044 else
1045 printf ("%s", ctime (&d));
1046 printf ("\t> ");
1047 fflush (stdout);
1048 }
1049 return 0;
1050 }
1051 #endif /* defined TEST */
This page took 0.074793 seconds and 5 git commands to generate.