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 yylex getdate_yylex
28 #define yyerror getdate_yyerror
30 static int yylex(void);
31 static int yyerror(const char *);
34 #define HOUR(x) ((time_t)(x) * 60)
35 #define SECSPERDAY (24L * 60L * 60L)
39 ** An entry in the lexical lookup table.
41 typedef struct _TABLE {
49 ** Daylight-savings mode: on, off, or not yet known.
51 typedef enum _DSTMODE {
52 DSToff, DSTon, DSTmaybe
56 ** Meridian: am, pm, or 24-hour style.
58 typedef enum _MERIDIAN {
64 ** Global variables. We could get rid of most of these by using a good
65 ** union as the yacc stack. (This routine was originally written before
66 ** yacc had the %union construct.) Maybe someday; right now we only use
67 ** the %union very rarely.
70 static DSTMODE yyDSTmode;
71 static time_t yyDayOrdinal;
72 static time_t yyDayNumber;
73 static int yyHaveDate;
76 static int yyHaveTime;
77 static int yyHaveZone;
78 static time_t yyTimezone;
81 static time_t yyMinutes;
82 static time_t yyMonth;
83 static time_t yySeconds;
85 static MERIDIAN yyMeridian;
86 static time_t yyRelMonth;
87 static time_t yyRelSeconds;
93 enum _MERIDIAN Meridian;
96 %token tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
97 %token tSEC_UNIT tSNUMBER tUNUMBER tZONE tDST
99 %type <Number> tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
100 %type <Number> tSEC_UNIT tSNUMBER tUNUMBER tZONE
101 %type <Meridian> tMERIDIAN o_merid
132 cvsstamp: tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER {
134 if (yyYear < 100) yyYear += 1900;
145 time : tUNUMBER tMERIDIAN {
151 | tUNUMBER ':' tUNUMBER o_merid {
157 | tUNUMBER ':' tUNUMBER tSNUMBER {
162 yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
164 | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
170 | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
176 yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
209 date : tUNUMBER '/' tUNUMBER {
213 | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
224 | tUNUMBER tSNUMBER tSNUMBER {
225 /* ISO 8601 format. yyyy-mm-dd. */
230 | tUNUMBER tMONTH tSNUMBER {
231 /* e.g. 17-JUN-1992. */
240 | tMONTH tUNUMBER ',' tUNUMBER {
249 | tUNUMBER tMONTH tUNUMBER {
257 yyRelSeconds = -yyRelSeconds;
258 yyRelMonth = -yyRelMonth;
263 relunit : tUNUMBER tMINUTE_UNIT {
264 yyRelSeconds += $1 * $2 * 60L;
266 | tSNUMBER tMINUTE_UNIT {
267 yyRelSeconds += $1 * $2 * 60L;
270 yyRelSeconds += $1 * 60L;
272 | tSNUMBER tSEC_UNIT {
275 | tUNUMBER tSEC_UNIT {
281 | tSNUMBER tMONTH_UNIT {
282 yyRelMonth += $1 * $2;
284 | tUNUMBER tMONTH_UNIT {
285 yyRelMonth += $1 * $2;
293 if (yyHaveTime && yyHaveDate && !yyHaveRel)
299 yyMonth= ($1/100)%100;
310 yyMinutes = $1 % 100;
319 o_merid : /* NULL */ {
329 /* Month and day table. */
330 static TABLE const MonthDayTable[] = {
331 { "january", tMONTH, 1 },
332 { "february", tMONTH, 2 },
333 { "march", tMONTH, 3 },
334 { "april", tMONTH, 4 },
335 { "may", tMONTH, 5 },
336 { "june", tMONTH, 6 },
337 { "july", tMONTH, 7 },
338 { "august", tMONTH, 8 },
339 { "september", tMONTH, 9 },
340 { "sept", tMONTH, 9 },
341 { "october", tMONTH, 10 },
342 { "november", tMONTH, 11 },
343 { "december", tMONTH, 12 },
344 { "sunday", tDAY, 0 },
345 { "monday", tDAY, 1 },
346 { "tuesday", tDAY, 2 },
348 { "wednesday", tDAY, 3 },
349 { "wednes", tDAY, 3 },
350 { "thursday", tDAY, 4 },
352 { "thurs", tDAY, 4 },
353 { "friday", tDAY, 5 },
354 { "saturday", tDAY, 6 },
358 /* Time units table. */
359 static TABLE const UnitsTable[] = {
360 { "year", tMONTH_UNIT, 12 },
361 { "month", tMONTH_UNIT, 1 },
362 { "fortnight", tMINUTE_UNIT, 14 * 24 * 60 },
363 { "week", tMINUTE_UNIT, 7 * 24 * 60 },
364 { "day", tMINUTE_UNIT, 1 * 24 * 60 },
365 { "hour", tMINUTE_UNIT, 60 },
366 { "minute", tMINUTE_UNIT, 1 },
367 { "min", tMINUTE_UNIT, 1 },
368 { "second", tSEC_UNIT, 1 },
369 { "sec", tSEC_UNIT, 1 },
373 /* Assorted relative-time words. */
374 static TABLE const OtherTable[] = {
375 { "tomorrow", tMINUTE_UNIT, 1 * 24 * 60 },
376 { "yesterday", tMINUTE_UNIT, -1 * 24 * 60 },
377 { "today", tMINUTE_UNIT, 0 },
378 { "now", tMINUTE_UNIT, 0 },
379 { "last", tUNUMBER, -1 },
380 { "this", tMINUTE_UNIT, 0 },
381 { "next", tUNUMBER, 2 },
382 { "first", tUNUMBER, 1 },
383 /* { "second", tUNUMBER, 2 }, */
384 { "third", tUNUMBER, 3 },
385 { "fourth", tUNUMBER, 4 },
386 { "fifth", tUNUMBER, 5 },
387 { "sixth", tUNUMBER, 6 },
388 { "seventh", tUNUMBER, 7 },
389 { "eighth", tUNUMBER, 8 },
390 { "ninth", tUNUMBER, 9 },
391 { "tenth", tUNUMBER, 10 },
392 { "eleventh", tUNUMBER, 11 },
393 { "twelfth", tUNUMBER, 12 },
398 /* The timezone table. */
399 /* Some of these are commented out because a time_t can't store a float. */
400 static TABLE const TimezoneTable[] = {
401 { "gmt", tZONE, HOUR( 0) }, /* Greenwich Mean */
402 { "ut", tZONE, HOUR( 0) }, /* Universal (Coordinated) */
403 { "utc", tZONE, HOUR( 0) },
404 { "wet", tZONE, HOUR( 0) }, /* Western European */
405 { "bst", tDAYZONE, HOUR( 0) }, /* British Summer */
406 { "wat", tZONE, HOUR( 1) }, /* West Africa */
407 { "at", tZONE, HOUR( 2) }, /* Azores */
409 /* For completeness. BST is also British Summer, and GST is
410 * also Guam Standard. */
411 { "bst", tZONE, HOUR( 3) }, /* Brazil Standard */
412 { "gst", tZONE, HOUR( 3) }, /* Greenland Standard */
415 { "nft", tZONE, HOUR(3.5) }, /* Newfoundland */
416 { "nst", tZONE, HOUR(3.5) }, /* Newfoundland Standard */
417 { "ndt", tDAYZONE, HOUR(3.5) }, /* Newfoundland Daylight */
419 { "ast", tZONE, HOUR( 4) }, /* Atlantic Standard */
420 { "adt", tDAYZONE, HOUR( 4) }, /* Atlantic Daylight */
421 { "est", tZONE, HOUR( 5) }, /* Eastern Standard */
422 { "edt", tDAYZONE, HOUR( 5) }, /* Eastern Daylight */
423 { "cst", tZONE, HOUR( 6) }, /* Central Standard */
424 { "cdt", tDAYZONE, HOUR( 6) }, /* Central Daylight */
425 { "mst", tZONE, HOUR( 7) }, /* Mountain Standard */
426 { "mdt", tDAYZONE, HOUR( 7) }, /* Mountain Daylight */
427 { "pst", tZONE, HOUR( 8) }, /* Pacific Standard */
428 { "pdt", tDAYZONE, HOUR( 8) }, /* Pacific Daylight */
429 { "yst", tZONE, HOUR( 9) }, /* Yukon Standard */
430 { "ydt", tDAYZONE, HOUR( 9) }, /* Yukon Daylight */
431 { "hst", tZONE, HOUR(10) }, /* Hawaii Standard */
432 { "hdt", tDAYZONE, HOUR(10) }, /* Hawaii Daylight */
433 { "cat", tZONE, HOUR(10) }, /* Central Alaska */
434 { "ahst", tZONE, HOUR(10) }, /* Alaska-Hawaii Standard */
435 { "nt", tZONE, HOUR(11) }, /* Nome */
436 { "idlw", tZONE, HOUR(12) }, /* International Date Line West */
437 { "cet", tZONE, -HOUR(1) }, /* Central European */
438 { "met", tZONE, -HOUR(1) }, /* Middle European */
439 { "mewt", tZONE, -HOUR(1) }, /* Middle European Winter */
440 { "mest", tDAYZONE, -HOUR(1) }, /* Middle European Summer */
441 { "swt", tZONE, -HOUR(1) }, /* Swedish Winter */
442 { "sst", tDAYZONE, -HOUR(1) }, /* Swedish Summer */
443 { "fwt", tZONE, -HOUR(1) }, /* French Winter */
444 { "fst", tDAYZONE, -HOUR(1) }, /* French Summer */
445 { "eet", tZONE, -HOUR(2) }, /* Eastern Europe, USSR Zone 1 */
446 { "bt", tZONE, -HOUR(3) }, /* Baghdad, USSR Zone 2 */
448 { "it", tZONE, -HOUR(3.5) },/* Iran */
450 { "zp4", tZONE, -HOUR(4) }, /* USSR Zone 3 */
451 { "zp5", tZONE, -HOUR(5) }, /* USSR Zone 4 */
453 { "ist", tZONE, -HOUR(5.5) },/* Indian Standard */
455 { "zp6", tZONE, -HOUR(6) }, /* USSR Zone 5 */
457 /* For completeness. NST is also Newfoundland Stanard, and SST is
458 * also Swedish Summer. */
459 { "nst", tZONE, -HOUR(6.5) },/* North Sumatra */
460 { "sst", tZONE, -HOUR(7) }, /* South Sumatra, USSR Zone 6 */
462 { "wast", tZONE, -HOUR(7) }, /* West Australian Standard */
463 { "wadt", tDAYZONE, -HOUR(7) }, /* West Australian Daylight */
465 { "jt", tZONE, -HOUR(7.5) },/* Java (3pm in Cronusland!) */
467 { "cct", tZONE, -HOUR(8) }, /* China Coast, USSR Zone 7 */
468 { "jst", tZONE, -HOUR(9) }, /* Japan Standard, USSR Zone 8 */
470 { "cast", tZONE, -HOUR(9.5) },/* Central Australian Standard */
471 { "cadt", tDAYZONE, -HOUR(9.5) },/* Central Australian Daylight */
473 { "east", tZONE, -HOUR(10) }, /* Eastern Australian Standard */
474 { "eadt", tDAYZONE, -HOUR(10) }, /* Eastern Australian Daylight */
475 { "gst", tZONE, -HOUR(10) }, /* Guam Standard, USSR Zone 9 */
476 { "nzt", tZONE, -HOUR(12) }, /* New Zealand */
477 { "nzst", tZONE, -HOUR(12) }, /* New Zealand Standard */
478 { "nzdt", tDAYZONE, -HOUR(12) }, /* New Zealand Daylight */
479 { "idle", tZONE, -HOUR(12) }, /* International Date Line East */
483 /* Military timezone table. */
484 static TABLE const MilitaryTable[] = {
485 { "a", tZONE, HOUR( 1) },
486 { "b", tZONE, HOUR( 2) },
487 { "c", tZONE, HOUR( 3) },
488 { "d", tZONE, HOUR( 4) },
489 { "e", tZONE, HOUR( 5) },
490 { "f", tZONE, HOUR( 6) },
491 { "g", tZONE, HOUR( 7) },
492 { "h", tZONE, HOUR( 8) },
493 { "i", tZONE, HOUR( 9) },
494 { "k", tZONE, HOUR( 10) },
495 { "l", tZONE, HOUR( 11) },
496 { "m", tZONE, HOUR( 12) },
497 { "n", tZONE, HOUR(- 1) },
498 { "o", tZONE, HOUR(- 2) },
499 { "p", tZONE, HOUR(- 3) },
500 { "q", tZONE, HOUR(- 4) },
501 { "r", tZONE, HOUR(- 5) },
502 { "s", tZONE, HOUR(- 6) },
503 { "t", tZONE, HOUR(- 7) },
504 { "u", tZONE, HOUR(- 8) },
505 { "v", tZONE, HOUR(- 9) },
506 { "w", tZONE, HOUR(-10) },
507 { "x", tZONE, HOUR(-11) },
508 { "y", tZONE, HOUR(-12) },
509 { "z", tZONE, HOUR( 0) },
518 yyerror(const char *s __unused)
525 ToSeconds(time_t Hours, time_t Minutes, time_t Seconds, MERIDIAN Meridian)
527 if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
531 if (Hours < 0 || Hours > 23)
533 return (Hours * 60L + Minutes) * 60L + Seconds;
535 if (Hours < 1 || Hours > 12)
539 return (Hours * 60L + Minutes) * 60L + Seconds;
541 if (Hours < 1 || Hours > 12)
545 return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
554 * A negative number, which means to use its absolute value (why?)
555 * A number from 0 to 99, which means a year from 1900 to 1999, or
556 * The actual year (>=100). */
558 Convert(time_t Month, time_t Day, time_t Year,
559 time_t Hours, time_t Minutes, time_t Seconds,
560 MERIDIAN Meridian, DSTMODE DSTmode)
562 static int DaysInMonth[12] = {
563 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
576 DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
578 /* Checking for 2038 bogusly assumes that time_t is 32 bits. But
579 I'm too lazy to try to check for time_t overflow in another way. */
580 if (Year < EPOCH || Year > 2038
581 || Month < 1 || Month > 12
582 /* Lint fluff: "conversion from long may lose accuracy" */
583 || Day < 1 || Day > DaysInMonth[(int)--Month])
585 * It would be nice to set a global error string here.
586 * "February 30 is not a valid date" is much more informative than
587 * "Can't parse date/time: 100 months" when the user input was
588 * "100 months" and addition resolved that to February 30, for
589 * example. See rcs2-7 in src/sanity.sh for more. */
592 for (Julian = Day - 1, i = 0; i < Month; i++)
593 Julian += DaysInMonth[i];
594 for (i = EPOCH; i < Year; i++)
595 Julian += 365 + (i % 4 == 0);
596 Julian *= SECSPERDAY;
597 Julian += yyTimezone * 60L;
598 if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
601 ltm = localtime(&Julian);
602 fprintf(stderr, "DST %d TZ %s %d\n", DSTmode, ltm->tm_zone, ltm->tm_isdst);
604 || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
611 DSTcorrect(time_t Start, time_t Future)
616 StartDay = (localtime(&Start)->tm_hour + 1) % 24;
617 FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
618 return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
623 RelativeDate(time_t Start, time_t DayOrdinal, time_t DayNumber)
629 tm = localtime(&now);
630 now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
631 now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
632 return DSTcorrect(Start, now);
637 RelativeMonth(time_t Start, time_t RelMonth)
645 tm = localtime(&Start);
646 Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
648 Month = Month % 12 + 1;
649 return DSTcorrect(Start,
650 Convert(Month, (time_t)tm->tm_mday, Year,
651 (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
657 LookupWord(char *buff)
665 /* Make it lowercase. */
666 for (p = buff; *p; p++)
670 if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
671 yylval.Meridian = MERam;
674 if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
675 yylval.Meridian = MERpm;
679 /* See if we have an abbreviation for a month. */
680 if (strlen(buff) == 3)
682 else if (strlen(buff) == 4 && buff[3] == '.') {
689 for (tp = MonthDayTable; tp->name; tp++) {
691 if (strncmp(buff, tp->name, 3) == 0) {
692 yylval.Number = tp->value;
696 else if (strcmp(buff, tp->name) == 0) {
697 yylval.Number = tp->value;
702 for (tp = TimezoneTable; tp->name; tp++)
703 if (strcmp(buff, tp->name) == 0) {
704 yylval.Number = tp->value;
708 if (strcmp(buff, "dst") == 0)
711 for (tp = UnitsTable; tp->name; tp++)
712 if (strcmp(buff, tp->name) == 0) {
713 yylval.Number = tp->value;
717 /* Strip off any plural and try the units table again. */
718 i = strlen(buff) - 1;
719 if (buff[i] == 's') {
721 for (tp = UnitsTable; tp->name; tp++)
722 if (strcmp(buff, tp->name) == 0) {
723 yylval.Number = tp->value;
726 buff[i] = 's'; /* Put back for "this" in OtherTable. */
729 for (tp = OtherTable; tp->name; tp++)
730 if (strcmp(buff, tp->name) == 0) {
731 yylval.Number = tp->value;
735 /* Military timezones. */
736 if (buff[1] == '\0' && isalpha(*buff)) {
737 for (tp = MilitaryTable; tp->name; tp++)
738 if (strcmp(buff, tp->name) == 0) {
739 yylval.Number = tp->value;
744 /* Drop out any periods and try the timezone table again. */
745 for (i = 0, p = q = buff; *q; q++)
752 for (tp = TimezoneTable; tp->name; tp++)
753 if (strcmp(buff, tp->name) == 0) {
754 yylval.Number = tp->value;
772 while (isspace(*yyInput))
775 if (isdigit(c = *yyInput) || c == '-' || c == '+') {
776 if (c == '-' || c == '+') {
777 sign = c == '-' ? -1 : 1;
778 if (!isdigit(*++yyInput))
779 /* skip the '-' sign */
784 for (yylval.Number = 0; isdigit(c = *yyInput++); )
785 yylval.Number = 10 * yylval.Number + c - '0';
788 yylval.Number = -yylval.Number;
789 return sign ? tSNUMBER : tUNUMBER;
792 for (p = buff; isalpha(c = *yyInput++) || c == '.'; )
793 if (p < &buff[sizeof buff - 1])
797 return LookupWord(buff);
814 #define TM_YEAR_ORIGIN 1900
827 (void)time (&nowtime);
829 gmt_ptr = gmtime (&nowtime);
832 /* Make a copy, in case localtime modifies *tm (I think
833 that comment now applies to *gmt_ptr, but I am too
834 lazy to dig into how gmtime and locatime allocate the
835 structures they return pointers to). */
839 if (! (tm = localtime (&nowtime)))
842 tm = localtime(&nowtime);
843 yyYear = tm->tm_year + 1900;
844 yyMonth = tm->tm_mon + 1;
846 yyTimezone = tm->tm_gmtoff;
847 yyDSTmode = DSTmaybe;
861 || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
864 if (yyHaveDate || yyHaveTime || yyHaveDay) {
865 Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
866 yyMeridian, yyDSTmode);
873 Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
876 Start += yyRelSeconds;
877 Start += RelativeMonth(Start, yyRelMonth);
879 if (yyHaveDay && !yyHaveDate) {
880 tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
884 /* Have to do *something* with a legitimate -1 so it's distinguishable
885 * from the error return value. (Alternately could set errno on error.) */
886 return Start == -1 ? 0 : Start;