3 ** Originally written by Steven M. Bellovin <smb@research.att.com> while
4 ** at the University of North Carolina at Chapel Hill. Later tweaked by
5 ** a couple of people on Usenet. Completely overhauled by Rich $alz
6 ** <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
8 ** This grammar has 10 shift/reduce conflicts.
10 ** This code is in the public domain and has no copyright.
12 ** Picked up from CVS and slightly cleaned up by to WARNS=5 level by
13 ** Poul-Henning Kamp <phk@FreeBSD.org>
22 #include <sys/types.h>
25 #include "libfifolog.h"
27 #define yyparse getdate_yyparse
28 #define yylex getdate_yylex
29 #define yyerror getdate_yyerror
31 static int yyparse(void);
32 static int yylex(void);
33 static int yyerror(const char *);
36 #define HOUR(x) ((time_t)(x) * 60)
37 #define SECSPERDAY (24L * 60L * 60L)
41 ** An entry in the lexical lookup table.
43 typedef struct _TABLE {
51 ** Daylight-savings mode: on, off, or not yet known.
53 typedef enum _DSTMODE {
54 DSToff, DSTon, DSTmaybe
58 ** Meridian: am, pm, or 24-hour style.
60 typedef enum _MERIDIAN {
66 ** Global variables. We could get rid of most of these by using a good
67 ** union as the yacc stack. (This routine was originally written before
68 ** yacc had the %union construct.) Maybe someday; right now we only use
69 ** the %union very rarely.
72 static DSTMODE yyDSTmode;
73 static time_t yyDayOrdinal;
74 static time_t yyDayNumber;
75 static int yyHaveDate;
78 static int yyHaveTime;
79 static int yyHaveZone;
80 static time_t yyTimezone;
83 static time_t yyMinutes;
84 static time_t yyMonth;
85 static time_t yySeconds;
87 static MERIDIAN yyMeridian;
88 static time_t yyRelMonth;
89 static time_t yyRelSeconds;
95 enum _MERIDIAN Meridian;
98 %token tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
99 %token tSEC_UNIT tSNUMBER tUNUMBER tZONE tDST
101 %type <Number> tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
102 %type <Number> tSEC_UNIT tSNUMBER tUNUMBER tZONE
103 %type <Meridian> tMERIDIAN o_merid
134 cvsstamp: tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER {
136 if (yyYear < 100) yyYear += 1900;
147 time : tUNUMBER tMERIDIAN {
153 | tUNUMBER ':' tUNUMBER o_merid {
159 | tUNUMBER ':' tUNUMBER tSNUMBER {
164 yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
166 | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
172 | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
178 yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
211 date : tUNUMBER '/' tUNUMBER {
215 | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
226 | tUNUMBER tSNUMBER tSNUMBER {
227 /* ISO 8601 format. yyyy-mm-dd. */
232 | tUNUMBER tMONTH tSNUMBER {
233 /* e.g. 17-JUN-1992. */
242 | tMONTH tUNUMBER ',' tUNUMBER {
251 | tUNUMBER tMONTH tUNUMBER {
259 yyRelSeconds = -yyRelSeconds;
260 yyRelMonth = -yyRelMonth;
265 relunit : tUNUMBER tMINUTE_UNIT {
266 yyRelSeconds += $1 * $2 * 60L;
268 | tSNUMBER tMINUTE_UNIT {
269 yyRelSeconds += $1 * $2 * 60L;
272 yyRelSeconds += $1 * 60L;
274 | tSNUMBER tSEC_UNIT {
277 | tUNUMBER tSEC_UNIT {
283 | tSNUMBER tMONTH_UNIT {
284 yyRelMonth += $1 * $2;
286 | tUNUMBER tMONTH_UNIT {
287 yyRelMonth += $1 * $2;
295 if (yyHaveTime && yyHaveDate && !yyHaveRel)
301 yyMonth= ($1/100)%100;
312 yyMinutes = $1 % 100;
321 o_merid : /* NULL */ {
331 /* Month and day table. */
332 static TABLE const MonthDayTable[] = {
333 { "january", tMONTH, 1 },
334 { "february", tMONTH, 2 },
335 { "march", tMONTH, 3 },
336 { "april", tMONTH, 4 },
337 { "may", tMONTH, 5 },
338 { "june", tMONTH, 6 },
339 { "july", tMONTH, 7 },
340 { "august", tMONTH, 8 },
341 { "september", tMONTH, 9 },
342 { "sept", tMONTH, 9 },
343 { "october", tMONTH, 10 },
344 { "november", tMONTH, 11 },
345 { "december", tMONTH, 12 },
346 { "sunday", tDAY, 0 },
347 { "monday", tDAY, 1 },
348 { "tuesday", tDAY, 2 },
350 { "wednesday", tDAY, 3 },
351 { "wednes", tDAY, 3 },
352 { "thursday", tDAY, 4 },
354 { "thurs", tDAY, 4 },
355 { "friday", tDAY, 5 },
356 { "saturday", tDAY, 6 },
360 /* Time units table. */
361 static TABLE const UnitsTable[] = {
362 { "year", tMONTH_UNIT, 12 },
363 { "month", tMONTH_UNIT, 1 },
364 { "fortnight", tMINUTE_UNIT, 14 * 24 * 60 },
365 { "week", tMINUTE_UNIT, 7 * 24 * 60 },
366 { "day", tMINUTE_UNIT, 1 * 24 * 60 },
367 { "hour", tMINUTE_UNIT, 60 },
368 { "minute", tMINUTE_UNIT, 1 },
369 { "min", tMINUTE_UNIT, 1 },
370 { "second", tSEC_UNIT, 1 },
371 { "sec", tSEC_UNIT, 1 },
375 /* Assorted relative-time words. */
376 static TABLE const OtherTable[] = {
377 { "tomorrow", tMINUTE_UNIT, 1 * 24 * 60 },
378 { "yesterday", tMINUTE_UNIT, -1 * 24 * 60 },
379 { "today", tMINUTE_UNIT, 0 },
380 { "now", tMINUTE_UNIT, 0 },
381 { "last", tUNUMBER, -1 },
382 { "this", tMINUTE_UNIT, 0 },
383 { "next", tUNUMBER, 2 },
384 { "first", tUNUMBER, 1 },
385 /* { "second", tUNUMBER, 2 }, */
386 { "third", tUNUMBER, 3 },
387 { "fourth", tUNUMBER, 4 },
388 { "fifth", tUNUMBER, 5 },
389 { "sixth", tUNUMBER, 6 },
390 { "seventh", tUNUMBER, 7 },
391 { "eighth", tUNUMBER, 8 },
392 { "ninth", tUNUMBER, 9 },
393 { "tenth", tUNUMBER, 10 },
394 { "eleventh", tUNUMBER, 11 },
395 { "twelfth", tUNUMBER, 12 },
400 /* The timezone table. */
401 /* Some of these are commented out because a time_t can't store a float. */
402 static TABLE const TimezoneTable[] = {
403 { "gmt", tZONE, HOUR( 0) }, /* Greenwich Mean */
404 { "ut", tZONE, HOUR( 0) }, /* Universal (Coordinated) */
405 { "utc", tZONE, HOUR( 0) },
406 { "wet", tZONE, HOUR( 0) }, /* Western European */
407 { "bst", tDAYZONE, HOUR( 0) }, /* British Summer */
408 { "wat", tZONE, HOUR( 1) }, /* West Africa */
409 { "at", tZONE, HOUR( 2) }, /* Azores */
411 /* For completeness. BST is also British Summer, and GST is
412 * also Guam Standard. */
413 { "bst", tZONE, HOUR( 3) }, /* Brazil Standard */
414 { "gst", tZONE, HOUR( 3) }, /* Greenland Standard */
417 { "nft", tZONE, HOUR(3.5) }, /* Newfoundland */
418 { "nst", tZONE, HOUR(3.5) }, /* Newfoundland Standard */
419 { "ndt", tDAYZONE, HOUR(3.5) }, /* Newfoundland Daylight */
421 { "ast", tZONE, HOUR( 4) }, /* Atlantic Standard */
422 { "adt", tDAYZONE, HOUR( 4) }, /* Atlantic Daylight */
423 { "est", tZONE, HOUR( 5) }, /* Eastern Standard */
424 { "edt", tDAYZONE, HOUR( 5) }, /* Eastern Daylight */
425 { "cst", tZONE, HOUR( 6) }, /* Central Standard */
426 { "cdt", tDAYZONE, HOUR( 6) }, /* Central Daylight */
427 { "mst", tZONE, HOUR( 7) }, /* Mountain Standard */
428 { "mdt", tDAYZONE, HOUR( 7) }, /* Mountain Daylight */
429 { "pst", tZONE, HOUR( 8) }, /* Pacific Standard */
430 { "pdt", tDAYZONE, HOUR( 8) }, /* Pacific Daylight */
431 { "yst", tZONE, HOUR( 9) }, /* Yukon Standard */
432 { "ydt", tDAYZONE, HOUR( 9) }, /* Yukon Daylight */
433 { "hst", tZONE, HOUR(10) }, /* Hawaii Standard */
434 { "hdt", tDAYZONE, HOUR(10) }, /* Hawaii Daylight */
435 { "cat", tZONE, HOUR(10) }, /* Central Alaska */
436 { "ahst", tZONE, HOUR(10) }, /* Alaska-Hawaii Standard */
437 { "nt", tZONE, HOUR(11) }, /* Nome */
438 { "idlw", tZONE, HOUR(12) }, /* International Date Line West */
439 { "cet", tZONE, -HOUR(1) }, /* Central European */
440 { "met", tZONE, -HOUR(1) }, /* Middle European */
441 { "mewt", tZONE, -HOUR(1) }, /* Middle European Winter */
442 { "mest", tDAYZONE, -HOUR(1) }, /* Middle European Summer */
443 { "swt", tZONE, -HOUR(1) }, /* Swedish Winter */
444 { "sst", tDAYZONE, -HOUR(1) }, /* Swedish Summer */
445 { "fwt", tZONE, -HOUR(1) }, /* French Winter */
446 { "fst", tDAYZONE, -HOUR(1) }, /* French Summer */
447 { "eet", tZONE, -HOUR(2) }, /* Eastern Europe, USSR Zone 1 */
448 { "bt", tZONE, -HOUR(3) }, /* Baghdad, USSR Zone 2 */
450 { "it", tZONE, -HOUR(3.5) },/* Iran */
452 { "zp4", tZONE, -HOUR(4) }, /* USSR Zone 3 */
453 { "zp5", tZONE, -HOUR(5) }, /* USSR Zone 4 */
455 { "ist", tZONE, -HOUR(5.5) },/* Indian Standard */
457 { "zp6", tZONE, -HOUR(6) }, /* USSR Zone 5 */
459 /* For completeness. NST is also Newfoundland Stanard, and SST is
460 * also Swedish Summer. */
461 { "nst", tZONE, -HOUR(6.5) },/* North Sumatra */
462 { "sst", tZONE, -HOUR(7) }, /* South Sumatra, USSR Zone 6 */
464 { "wast", tZONE, -HOUR(7) }, /* West Australian Standard */
465 { "wadt", tDAYZONE, -HOUR(7) }, /* West Australian Daylight */
467 { "jt", tZONE, -HOUR(7.5) },/* Java (3pm in Cronusland!) */
469 { "cct", tZONE, -HOUR(8) }, /* China Coast, USSR Zone 7 */
470 { "jst", tZONE, -HOUR(9) }, /* Japan Standard, USSR Zone 8 */
472 { "cast", tZONE, -HOUR(9.5) },/* Central Australian Standard */
473 { "cadt", tDAYZONE, -HOUR(9.5) },/* Central Australian Daylight */
475 { "east", tZONE, -HOUR(10) }, /* Eastern Australian Standard */
476 { "eadt", tDAYZONE, -HOUR(10) }, /* Eastern Australian Daylight */
477 { "gst", tZONE, -HOUR(10) }, /* Guam Standard, USSR Zone 9 */
478 { "nzt", tZONE, -HOUR(12) }, /* New Zealand */
479 { "nzst", tZONE, -HOUR(12) }, /* New Zealand Standard */
480 { "nzdt", tDAYZONE, -HOUR(12) }, /* New Zealand Daylight */
481 { "idle", tZONE, -HOUR(12) }, /* International Date Line East */
485 /* Military timezone table. */
486 static TABLE const MilitaryTable[] = {
487 { "a", tZONE, HOUR( 1) },
488 { "b", tZONE, HOUR( 2) },
489 { "c", tZONE, HOUR( 3) },
490 { "d", tZONE, HOUR( 4) },
491 { "e", tZONE, HOUR( 5) },
492 { "f", tZONE, HOUR( 6) },
493 { "g", tZONE, HOUR( 7) },
494 { "h", tZONE, HOUR( 8) },
495 { "i", tZONE, HOUR( 9) },
496 { "k", tZONE, HOUR( 10) },
497 { "l", tZONE, HOUR( 11) },
498 { "m", tZONE, HOUR( 12) },
499 { "n", tZONE, HOUR(- 1) },
500 { "o", tZONE, HOUR(- 2) },
501 { "p", tZONE, HOUR(- 3) },
502 { "q", tZONE, HOUR(- 4) },
503 { "r", tZONE, HOUR(- 5) },
504 { "s", tZONE, HOUR(- 6) },
505 { "t", tZONE, HOUR(- 7) },
506 { "u", tZONE, HOUR(- 8) },
507 { "v", tZONE, HOUR(- 9) },
508 { "w", tZONE, HOUR(-10) },
509 { "x", tZONE, HOUR(-11) },
510 { "y", tZONE, HOUR(-12) },
511 { "z", tZONE, HOUR( 0) },
520 yyerror(const char *s __unused)
527 ToSeconds(time_t Hours, time_t Minutes, time_t Seconds, MERIDIAN Meridian)
529 if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
533 if (Hours < 0 || Hours > 23)
535 return (Hours * 60L + Minutes) * 60L + Seconds;
537 if (Hours < 1 || Hours > 12)
541 return (Hours * 60L + Minutes) * 60L + Seconds;
543 if (Hours < 1 || Hours > 12)
547 return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
556 * A negative number, which means to use its absolute value (why?)
557 * A number from 0 to 99, which means a year from 1900 to 1999, or
558 * The actual year (>=100). */
560 Convert(time_t Month, time_t Day, time_t Year,
561 time_t Hours, time_t Minutes, time_t Seconds,
562 MERIDIAN Meridian, DSTMODE DSTmode)
564 static int DaysInMonth[12] = {
565 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
578 DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
580 /* Checking for 2038 bogusly assumes that time_t is 32 bits. But
581 I'm too lazy to try to check for time_t overflow in another way. */
582 if (Year < EPOCH || Year > 2038
583 || Month < 1 || Month > 12
584 /* Lint fluff: "conversion from long may lose accuracy" */
585 || Day < 1 || Day > DaysInMonth[(int)--Month])
587 * It would be nice to set a global error string here.
588 * "February 30 is not a valid date" is much more informative than
589 * "Can't parse date/time: 100 months" when the user input was
590 * "100 months" and addition resolved that to February 30, for
591 * example. See rcs2-7 in src/sanity.sh for more. */
594 for (Julian = Day - 1, i = 0; i < Month; i++)
595 Julian += DaysInMonth[i];
596 for (i = EPOCH; i < Year; i++)
597 Julian += 365 + (i % 4 == 0);
598 Julian *= SECSPERDAY;
599 Julian += yyTimezone * 60L;
600 if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
603 ltm = localtime(&Julian);
604 fprintf(stderr, "DST %d TZ %s %d\n", DSTmode, ltm->tm_zone, ltm->tm_isdst);
606 || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
613 DSTcorrect(time_t Start, time_t Future)
618 StartDay = (localtime(&Start)->tm_hour + 1) % 24;
619 FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
620 return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
625 RelativeDate(time_t Start, time_t DayOrdinal, time_t DayNumber)
631 tm = localtime(&now);
632 now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
633 now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
634 return DSTcorrect(Start, now);
639 RelativeMonth(time_t Start, time_t RelMonth)
647 tm = localtime(&Start);
648 Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
650 Month = Month % 12 + 1;
651 return DSTcorrect(Start,
652 Convert(Month, (time_t)tm->tm_mday, Year,
653 (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
659 LookupWord(char *buff)
667 /* Make it lowercase. */
668 for (p = buff; *p; p++)
672 if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
673 yylval.Meridian = MERam;
676 if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
677 yylval.Meridian = MERpm;
681 /* See if we have an abbreviation for a month. */
682 if (strlen(buff) == 3)
684 else if (strlen(buff) == 4 && buff[3] == '.') {
691 for (tp = MonthDayTable; tp->name; tp++) {
693 if (strncmp(buff, tp->name, 3) == 0) {
694 yylval.Number = tp->value;
698 else if (strcmp(buff, tp->name) == 0) {
699 yylval.Number = tp->value;
704 for (tp = TimezoneTable; tp->name; tp++)
705 if (strcmp(buff, tp->name) == 0) {
706 yylval.Number = tp->value;
710 if (strcmp(buff, "dst") == 0)
713 for (tp = UnitsTable; tp->name; tp++)
714 if (strcmp(buff, tp->name) == 0) {
715 yylval.Number = tp->value;
719 /* Strip off any plural and try the units table again. */
720 i = strlen(buff) - 1;
721 if (buff[i] == 's') {
723 for (tp = UnitsTable; tp->name; tp++)
724 if (strcmp(buff, tp->name) == 0) {
725 yylval.Number = tp->value;
728 buff[i] = 's'; /* Put back for "this" in OtherTable. */
731 for (tp = OtherTable; tp->name; tp++)
732 if (strcmp(buff, tp->name) == 0) {
733 yylval.Number = tp->value;
737 /* Military timezones. */
738 if (buff[1] == '\0' && isalpha(*buff)) {
739 for (tp = MilitaryTable; tp->name; tp++)
740 if (strcmp(buff, tp->name) == 0) {
741 yylval.Number = tp->value;
746 /* Drop out any periods and try the timezone table again. */
747 for (i = 0, p = q = buff; *q; q++)
754 for (tp = TimezoneTable; tp->name; tp++)
755 if (strcmp(buff, tp->name) == 0) {
756 yylval.Number = tp->value;
774 while (isspace(*yyInput))
777 if (isdigit(c = *yyInput) || c == '-' || c == '+') {
778 if (c == '-' || c == '+') {
779 sign = c == '-' ? -1 : 1;
780 if (!isdigit(*++yyInput))
781 /* skip the '-' sign */
786 for (yylval.Number = 0; isdigit(c = *yyInput++); )
787 yylval.Number = 10 * yylval.Number + c - '0';
790 yylval.Number = -yylval.Number;
791 return sign ? tSNUMBER : tUNUMBER;
794 for (p = buff; isalpha(c = *yyInput++) || c == '.'; )
795 if (p < &buff[sizeof buff - 1])
799 return LookupWord(buff);
816 #define TM_YEAR_ORIGIN 1900
829 (void)time (&nowtime);
831 gmt_ptr = gmtime (&nowtime);
834 /* Make a copy, in case localtime modifies *tm (I think
835 that comment now applies to *gmt_ptr, but I am too
836 lazy to dig into how gmtime and locatime allocate the
837 structures they return pointers to). */
841 if (! (tm = localtime (&nowtime)))
844 tm = localtime(&nowtime);
845 yyYear = tm->tm_year + 1900;
846 yyMonth = tm->tm_mon + 1;
848 yyTimezone = tm->tm_gmtoff;
849 yyDSTmode = DSTmaybe;
863 || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
866 if (yyHaveDate || yyHaveTime || yyHaveDay) {
867 Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
868 yyMeridian, yyDSTmode);
875 Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
878 Start += yyRelSeconds;
879 Start += RelativeMonth(Start, yyRelMonth);
881 if (yyHaveDay && !yyHaveDate) {
882 tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
886 /* Have to do *something* with a legitimate -1 so it's distinguishable
887 * from the error return value. (Alternately could set errno on error.) */
888 return Start == -1 ? 0 : Start;