2 /* Parse a string into an internal time stamp.
3 Copyright 1999 Free Software Foundation, Inc.
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)
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.
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. */
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.
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. */
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. */
48 # include <stdlib.h> /* for `free'; used by Bison 1.27 */
51 #if STDC_HEADERS || (! defined isascii && ! HAVE_ISASCII)
52 # define IN_CTYPE_DOMAIN(c) 1
54 # define IN_CTYPE_DOMAIN(c) isascii (c)
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))
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)
72 #if STDC_HEADERS || HAVE_STRING_H
76 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
77 # define __attribute__(x)
80 #ifndef ATTRIBUTE_UNUSED
81 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
84 #define EPOCH_YEAR 1970
85 #define TM_YEAR_ORIGIN 1900
87 #define HOUR(x) ((x) * 60)
89 /* An entry in the lexical lookup table. */
97 /* Meridian: am, pm, or 24-hour style. */
98 enum { MERam, MERpm, MER24 };
100 /* Information passed to and from the parser. */
101 struct parser_control
103 /* The input string remaining to be parsed. */
106 /* N, if this is the Nth Tuesday. */
109 /* Day of week; Sunday is 0. */
112 /* tm_isdst flag for the local zone. */
115 /* Time zone, in minutes east of UTC. */
118 /* Style used for time. */
121 /* Gregorian year, month, day, hour, minutes, and seconds. */
129 /* Relative year, month, day, hour, minutes, and seconds. */
137 /* Counts of nonterminals of various flavors parsed so far. */
140 int local_zones_seen;
145 /* Table of local time zone abbrevations, terminated by a null entry. */
146 table local_time_zone_table[3];
149 #define YYLEX_PARAM parm
150 #define YYPARSE_PARAM parm
153 static int yyerror ();
158 /* We want a reentrant parser. */
161 /* This grammar has 13 shift/reduce conflicts. */
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
177 { ((struct parser_control *) parm)->times_seen++; }
179 { ((struct parser_control *) parm)->local_zones_seen++; }
181 { ((struct parser_control *) parm)->zones_seen++; }
183 { ((struct parser_control *) parm)->dates_seen++; }
185 { ((struct parser_control *) parm)->days_seen++; }
187 { ((struct parser_control *) parm)->rels_seen++; }
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;
199 | tUNUMBER ':' tUNUMBER o_merid
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;
206 | tUNUMBER ':' tUNUMBER tSNUMBER
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;
215 | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid
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;
222 | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER
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;
236 { ((struct parser_control *) parm)->local_isdst = $1; }
238 { ((struct parser_control *) parm)->local_isdst = $1 < 0 ? 1 : $1 + 1; }
243 { ((struct parser_control *) parm)->time_zone = $1; }
245 { ((struct parser_control *) parm)->time_zone = $1 + 60; }
247 { ((struct parser_control *) parm)->time_zone = $1 + 60; }
253 ((struct parser_control *) parm)->day_ordinal = 1;
254 ((struct parser_control *) parm)->day_number = $1;
258 ((struct parser_control *) parm)->day_ordinal = 1;
259 ((struct parser_control *) parm)->day_number = $1;
263 ((struct parser_control *) parm)->day_ordinal = $1;
264 ((struct parser_control *) parm)->day_number = $2;
269 tUNUMBER '/' tUNUMBER
271 ((struct parser_control *) parm)->month = $1;
272 ((struct parser_control *) parm)->day = $3;
274 | tUNUMBER '/' tUNUMBER '/' tUNUMBER
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. */
282 ((struct parser_control *) parm)->year = $1;
283 ((struct parser_control *) parm)->month = $3;
284 ((struct parser_control *) parm)->day = $5;
288 ((struct parser_control *) parm)->month = $1;
289 ((struct parser_control *) parm)->day = $3;
290 ((struct parser_control *) parm)->year = $5;
293 | tUNUMBER tSNUMBER tSNUMBER
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;
300 | tUNUMBER tMONTH tSNUMBER
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;
309 ((struct parser_control *) parm)->month = $1;
310 ((struct parser_control *) parm)->day = $2;
312 | tMONTH tUNUMBER ',' tUNUMBER
314 ((struct parser_control *) parm)->month = $1;
315 ((struct parser_control *) parm)->day = $2;
316 ((struct parser_control *) parm)->year = $4;
320 ((struct parser_control *) parm)->month = $2;
321 ((struct parser_control *) parm)->day = $1;
323 | tUNUMBER tMONTH tUNUMBER
325 ((struct parser_control *) parm)->month = $2;
326 ((struct parser_control *) parm)->day = $1;
327 ((struct parser_control *) parm)->year = $3;
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;
346 { ((struct parser_control *) parm)->rel_year += $1 * $2; }
347 | tSNUMBER tYEAR_UNIT
348 { ((struct parser_control *) parm)->rel_year += $1 * $2; }
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; }
356 { ((struct parser_control *) parm)->rel_month += $1; }
358 { ((struct parser_control *) parm)->rel_day += $1 * $2; }
360 { ((struct parser_control *) parm)->rel_day += $1 * $2; }
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; }
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; }
374 { ((struct parser_control *) parm)->rel_minutes += $1; }
376 { ((struct parser_control *) parm)->rel_seconds += $1 * $2; }
378 { ((struct parser_control *) parm)->rel_seconds += $1 * $2; }
380 { ((struct parser_control *) parm)->rel_seconds += $1; }
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;
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;
401 ((struct parser_control *) parm)->times_seen++;
404 ((struct parser_control *) parm)->hour = $1;
405 ((struct parser_control *) parm)->minutes = 0;
409 ((struct parser_control *) parm)->hour = $1 / 100;
410 ((struct parser_control *) parm)->minutes = $1 % 100;
412 ((struct parser_control *) parm)->seconds = 0;
413 ((struct parser_control *) parm)->meridian = MER24;
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. */
434 struct tm *gmtime ();
437 struct tm *localtime ();
443 static table const meridian_table[] =
445 { "AM", tMERIDIAN, MERam },
446 { "A.M.", tMERIDIAN, MERam },
447 { "PM", tMERIDIAN, MERpm },
448 { "P.M.", tMERIDIAN, MERpm },
452 static table const dst_table[] =
457 static table const month_and_day_table[] =
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 },
476 { "WEDNESDAY",tDAY, 3 },
477 { "WEDNES", tDAY, 3 },
478 { "THURSDAY", tDAY, 4 },
480 { "THURS", tDAY, 4 },
481 { "FRIDAY", tDAY, 5 },
482 { "SATURDAY", tDAY, 6 },
486 static table const time_units_table[] =
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 },
501 /* Assorted relative-time words. */
502 static table const relative_time_table[] =
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 },
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[] =
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 */
587 /* Military time zone table. */
588 static table const military_table[] =
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) },
621 to_hour (int hours, int meridian)
626 return 0 <= hours && hours < 24 ? hours : -1;
628 return 0 < hours && hours < 12 ? hours : hours == 12 ? 0 : -1;
630 return 0 < hours && hours < 12 ? hours + 12 : hours == 12 ? 12 : -1;
643 /* XPG4 suggests that years 00-68 map to 2000-2068, and
644 years 69-99 map to 1969-1999. */
654 lookup_zone (struct parser_control const *pc, char const *name)
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)
663 for (tp = time_zone_table; tp->name; tp++)
664 if (strcmp (name, tp->name) == 0)
670 /* Yield A - B, measured in seconds. */
672 difftm (struct tm *a, struct tm *b)
674 int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
675 int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
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
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));
692 lookup_word (struct parser_control const *pc, char *word)
701 /* Make it uppercase. */
702 for (p = word; *p; p++)
703 if (ISLOWER ((unsigned char) *p))
704 *p = toupper ((unsigned char) *p);
706 for (tp = meridian_table; tp->name; tp++)
707 if (strcmp (word, tp->name) == 0)
710 /* See if we have an abbreviation for a month. */
711 wordlen = strlen (word);
712 abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.');
714 for (tp = month_and_day_table; tp->name; tp++)
715 if ((abbrev ? strncmp (word, tp->name, 3) : strcmp (word, tp->name)) == 0)
718 if ((tp = lookup_zone (pc, word)))
721 if (strcmp (word, dst_table[0].name) == 0)
724 for (tp = time_units_table; tp->name; tp++)
725 if (strcmp (word, tp->name) == 0)
728 /* Strip off any plural and try the units table again. */
729 if (word[wordlen - 1] == 'S')
731 word[wordlen - 1] = '\0';
732 for (tp = time_units_table; tp->name; tp++)
733 if (strcmp (word, tp->name) == 0)
735 word[wordlen - 1] = 'S'; /* For "this" in relative_time_table. */
738 for (tp = relative_time_table; tp->name; tp++)
739 if (strcmp (word, tp->name) == 0)
742 /* Military time zones. */
744 for (tp = military_table; tp->name; tp++)
745 if (word[0] == tp->name[0])
748 /* Drop out any periods and try the time zone table again. */
749 for (i = 0, p = q = word; (*p = *q); q++)
754 if (i && (tp = lookup_zone (pc, word)))
761 yylex (YYSTYPE *lvalp, struct parser_control *pc)
768 while (c = *pc->input, ISSPACE (c))
771 if (ISDIGIT (c) || c == '-' || c == '+')
774 if (c == '-' || c == '+')
776 sign = c == '-' ? -1 : 1;
777 if (! ISDIGIT (*++pc->input))
778 /* skip the '-' sign */
783 for (*lvalp = 0; ISDIGIT (c = *pc->input++);)
784 *lvalp = 10 * *lvalp + (c - '0');
788 return sign ? tSNUMBER : tUNUMBER;
799 if (p < buff + sizeof buff - 1)
803 while (ISALPHA (c) || c == '.');
806 tp = lookup_word (pc, buff);
830 /* Do nothing if the parser reports an error. */
832 yyerror (char *s ATTRIBUTE_UNUSED)
839 get_date (const char *p, const time_t *now)
841 time_t Start = now ? *now : time (0);
842 struct tm *tmp = localtime (&Start);
845 struct parser_control pc;
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;
870 pc.local_zones_seen = 0;
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;
879 /* Probe the names used in the next three calendar quarters, looking
880 for a tm_isdst different from the one we already have. */
883 for (probe = 1; probe <= 3; probe++)
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)
891 pc.local_time_zone_table[1].name = tm->tm_zone;
892 pc.local_time_zone_table[1].type = tLOCAL_TIME_ZONE_TABLE;
893 pc.local_time_zone_table[1].value = tm->tm_isdst;
894 pc.local_time_zone_table[2].name = 0;
904 extern char *tzname[];
907 for (i = 0; i < 2; i++)
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;
913 pc.local_time_zone_table[i].name = 0;
916 pc.local_time_zone_table[0].name = 0;
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))
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;
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))
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))
942 tm.tm_hour = to_hour (pc.hour, pc.meridian);
945 tm.tm_min = pc.minutes;
946 tm.tm_sec = pc.seconds;
950 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
952 tm.tm_hour += pc.rel_hour;
953 tm.tm_min += pc.rel_minutes;
954 tm.tm_sec += pc.rel_seconds;
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)
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;
968 Start = mktime (&tm);
970 if (Start == (time_t) -1)
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. */
984 if (tm.tm_year <= EPOCH_YEAR - TM_YEAR_ORIGIN)
987 pc.time_zone += 24 * 60;
992 pc.time_zone -= 24 * 60;
994 Start = mktime (&tm);
997 if (Start == (time_t) -1)
1001 if (pc.days_seen && ! pc.dates_seen)
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)
1013 struct tm *gmt = gmtime (&Start);
1016 delta = pc.time_zone * 60 + difftm (gmt, &tm);
1017 if ((Start - delta < Start) != (delta < 0))
1018 return -1; /* time_t overflow */
1030 main (int ac, char **av)
1035 printf ("Enter date, or blank line to exit.\n\t> ");
1038 buff[BUFSIZ - 1] = 0;
1039 while (fgets (buff, BUFSIZ - 1, stdin) && buff[0])
1041 d = get_date (buff, 0);
1042 if (d == (time_t) -1)
1043 printf ("Bad format - couldn't convert.\n");
1045 printf ("%s", ctime (&d));
1051 #endif /* defined TEST */