3 ** @(#)getdate.y 1.8 92/03/03
5 ** Originally written by Steven M. Bellovin <smb@research.att.com> while
6 ** at the University of North Carolina at Chapel Hill. Later tweaked by
7 ** a couple of people on Usenet. Completely overhauled by Rich $alz
8 ** <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
9 ** send any email to Rich.
11 ** This grammar has eight shift/reduce conflicts.
13 ** This code is in the public domain and has no copyright.
15 /* SUPPRESS 287 on yaccpar_sccsid *//* Unused static variable */
16 /* SUPPRESS 288 on yyerrlab *//* Label unused */
21 #if (!defined (__STDC__) && defined (sparc)) || defined (__sparc__)
23 #undef alloca /* might get redefined below */
27 extern struct tm *localtime();
29 #define yyparse getdate_yyparse
30 #define yylex getdate_yylex
31 #define yyerror getdate_yyerror
33 #if !defined(lint) && !defined(SABER)
34 static char RCS[] = "@(#)getdate.y 1.8 92/03/03";
35 #endif /* !defined(lint) && !defined(SABER) */
39 #define HOUR(x) ((time_t)(x) * 60)
40 #define SECSPERDAY (24L * 60L * 60L)
44 ** An entry in the lexical lookup table.
46 typedef struct _TABLE {
54 ** Daylight-savings mode: on, off, or not yet known.
56 typedef enum _DSTMODE {
57 DSTon, DSToff, DSTmaybe
61 ** Meridian: am, pm, or 24-hour style.
63 typedef enum _MERIDIAN {
69 ** Global variables. We could get rid of most of these by using a good
70 ** union as the yacc stack. (This routine was originally written before
71 ** yacc had the %union construct.) Maybe someday; right now we only use
72 ** the %union very rarely.
75 static DSTMODE yyDSTmode;
76 static time_t yyDayOrdinal;
77 static time_t yyDayNumber;
78 static int yyHaveDate;
81 static int yyHaveTime;
82 static int yyHaveZone;
83 static time_t yyTimezone;
86 static time_t yyMinutes;
87 static time_t yyMonth;
88 static time_t yySeconds;
90 static MERIDIAN yyMeridian;
91 static time_t yyRelMonth;
92 static time_t yyRelSeconds;
98 enum _MERIDIAN Meridian;
101 %token tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
102 %token tSEC_UNIT tSNUMBER tUNUMBER tZONE tDST
104 %type <Number> tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
105 %type <Number> tSEC_UNIT tSNUMBER tUNUMBER tZONE
106 %type <Meridian> tMERIDIAN o_merid
132 time : tUNUMBER tMERIDIAN {
138 | tUNUMBER ':' tUNUMBER o_merid {
144 | tUNUMBER ':' tUNUMBER tSNUMBER {
149 yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
151 | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
157 | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
163 yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
196 date : tUNUMBER '/' tUNUMBER {
200 | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
209 | tMONTH tUNUMBER ',' tUNUMBER {
218 | tUNUMBER tMONTH tUNUMBER {
226 yyRelSeconds = -yyRelSeconds;
227 yyRelMonth = -yyRelMonth;
232 relunit : tUNUMBER tMINUTE_UNIT {
233 yyRelSeconds += $1 * $2 * 60L;
235 | tSNUMBER tMINUTE_UNIT {
236 yyRelSeconds += $1 * $2 * 60L;
239 yyRelSeconds += $1 * 60L;
241 | tSNUMBER tSEC_UNIT {
244 | tUNUMBER tSEC_UNIT {
250 | tSNUMBER tMONTH_UNIT {
251 yyRelMonth += $1 * $2;
253 | tUNUMBER tMONTH_UNIT {
254 yyRelMonth += $1 * $2;
262 if (yyHaveTime && yyHaveDate && !yyHaveRel)
270 yyDay= (date_part)%100;
271 yyMonth= (date_part/100)%100;
272 yyYear = date_part/10000;
281 yyMinutes = $1 % 100;
289 o_merid : /* NULL */ {
299 /* Month and day table. */
300 static TABLE MonthDayTable[] = {
301 { "january", tMONTH, 1 },
302 { "february", tMONTH, 2 },
303 { "march", tMONTH, 3 },
304 { "april", tMONTH, 4 },
305 { "may", tMONTH, 5 },
306 { "june", tMONTH, 6 },
307 { "july", tMONTH, 7 },
308 { "august", tMONTH, 8 },
309 { "september", tMONTH, 9 },
310 { "sept", tMONTH, 9 },
311 { "october", tMONTH, 10 },
312 { "november", tMONTH, 11 },
313 { "december", tMONTH, 12 },
314 { "sunday", tDAY, 0 },
315 { "monday", tDAY, 1 },
316 { "tuesday", tDAY, 2 },
318 { "wednesday", tDAY, 3 },
319 { "wednes", tDAY, 3 },
320 { "thursday", tDAY, 4 },
322 { "thurs", tDAY, 4 },
323 { "friday", tDAY, 5 },
324 { "saturday", tDAY, 6 },
328 /* Time units table. */
329 static TABLE UnitsTable[] = {
330 { "year", tMONTH_UNIT, 12 },
331 { "month", tMONTH_UNIT, 1 },
332 { "fortnight", tMINUTE_UNIT, 14 * 24 * 60 },
333 { "week", tMINUTE_UNIT, 7 * 24 * 60 },
334 { "day", tMINUTE_UNIT, 1 * 24 * 60 },
335 { "hour", tMINUTE_UNIT, 60 },
336 { "minute", tMINUTE_UNIT, 1 },
337 { "min", tMINUTE_UNIT, 1 },
338 { "second", tSEC_UNIT, 1 },
339 { "sec", tSEC_UNIT, 1 },
343 /* Assorted relative-time words. */
344 static TABLE OtherTable[] = {
345 { "tomorrow", tMINUTE_UNIT, 1 * 24 * 60 },
346 { "yesterday", tMINUTE_UNIT, -1 * 24 * 60 },
347 { "today", tMINUTE_UNIT, 0 },
348 { "now", tMINUTE_UNIT, 0 },
349 { "last", tUNUMBER, -1 },
350 { "this", tMINUTE_UNIT, 0 },
351 { "next", tUNUMBER, 2 },
352 { "first", tUNUMBER, 1 },
353 /* { "second", tUNUMBER, 2 }, */
354 { "third", tUNUMBER, 3 },
355 { "fourth", tUNUMBER, 4 },
356 { "fifth", tUNUMBER, 5 },
357 { "sixth", tUNUMBER, 6 },
358 { "seventh", tUNUMBER, 7 },
359 { "eighth", tUNUMBER, 8 },
360 { "ninth", tUNUMBER, 9 },
361 { "tenth", tUNUMBER, 10 },
362 { "eleventh", tUNUMBER, 11 },
363 { "twelfth", tUNUMBER, 12 },
368 /* The timezone table. */
369 /* Some of these are commented out because a time_t can't store a float. */
370 static TABLE TimezoneTable[] = {
371 { "gmt", tZONE, HOUR( 0) }, /* Greenwich Mean */
372 { "ut", tZONE, HOUR( 0) }, /* Universal (Coordinated) */
373 { "utc", tZONE, HOUR( 0) },
374 { "wet", tZONE, HOUR( 0) }, /* Western European */
375 { "bst", tDAYZONE, HOUR( 0) }, /* British Summer */
376 { "wat", tZONE, HOUR( 1) }, /* West Africa */
377 { "at", tZONE, HOUR( 2) }, /* Azores */
379 /* For completeness. BST is also British Summer, and GST is
380 * also Guam Standard. */
381 { "bst", tZONE, HOUR( 3) }, /* Brazil Standard */
382 { "gst", tZONE, HOUR( 3) }, /* Greenland Standard */
385 { "nft", tZONE, HOUR(3.5) }, /* Newfoundland */
386 { "nst", tZONE, HOUR(3.5) }, /* Newfoundland Standard */
387 { "ndt", tDAYZONE, HOUR(3.5) }, /* Newfoundland Daylight */
389 { "ast", tZONE, HOUR( 4) }, /* Atlantic Standard */
390 { "adt", tDAYZONE, HOUR( 4) }, /* Atlantic Daylight */
391 { "est", tZONE, HOUR( 5) }, /* Eastern Standard */
392 { "edt", tDAYZONE, HOUR( 5) }, /* Eastern Daylight */
393 { "cst", tZONE, HOUR( 6) }, /* Central Standard */
394 { "cdt", tDAYZONE, HOUR( 6) }, /* Central Daylight */
395 { "mst", tZONE, HOUR( 7) }, /* Mountain Standard */
396 { "mdt", tDAYZONE, HOUR( 7) }, /* Mountain Daylight */
397 { "pst", tZONE, HOUR( 8) }, /* Pacific Standard */
398 { "pdt", tDAYZONE, HOUR( 8) }, /* Pacific Daylight */
399 { "yst", tZONE, HOUR( 9) }, /* Yukon Standard */
400 { "ydt", tDAYZONE, HOUR( 9) }, /* Yukon Daylight */
401 { "hst", tZONE, HOUR(10) }, /* Hawaii Standard */
402 { "hdt", tDAYZONE, HOUR(10) }, /* Hawaii Daylight */
403 { "cat", tZONE, HOUR(10) }, /* Central Alaska */
404 { "ahst", tZONE, HOUR(10) }, /* Alaska-Hawaii Standard */
405 { "nt", tZONE, HOUR(11) }, /* Nome */
406 { "idlw", tZONE, HOUR(12) }, /* International Date Line West */
407 { "cet", tZONE, -HOUR(1) }, /* Central European */
408 { "met", tZONE, -HOUR(1) }, /* Middle European */
409 { "mewt", tZONE, -HOUR(1) }, /* Middle European Winter */
410 { "mest", tDAYZONE, -HOUR(1) }, /* Middle European Summer */
411 { "swt", tZONE, -HOUR(1) }, /* Swedish Winter */
412 { "sst", tDAYZONE, -HOUR(1) }, /* Swedish Summer */
413 { "fwt", tZONE, -HOUR(1) }, /* French Winter */
414 { "fst", tDAYZONE, -HOUR(1) }, /* French Summer */
415 { "eet", tZONE, -HOUR(2) }, /* Eastern Europe, USSR Zone 1 */
416 { "bt", tZONE, -HOUR(3) }, /* Baghdad, USSR Zone 2 */
418 { "it", tZONE, -HOUR(3.5) },/* Iran */
420 { "zp4", tZONE, -HOUR(4) }, /* USSR Zone 3 */
421 { "zp5", tZONE, -HOUR(5) }, /* USSR Zone 4 */
423 { "ist", tZONE, -HOUR(5.5) },/* Indian Standard */
425 { "zp6", tZONE, -HOUR(6) }, /* USSR Zone 5 */
427 /* For completeness. NST is also Newfoundland Stanard, and SST is
428 * also Swedish Summer. */
429 { "nst", tZONE, -HOUR(6.5) },/* North Sumatra */
430 { "sst", tZONE, -HOUR(7) }, /* South Sumatra, USSR Zone 6 */
432 { "wast", tZONE, -HOUR(7) }, /* West Australian Standard */
433 { "wadt", tDAYZONE, -HOUR(7) }, /* West Australian Daylight */
435 { "jt", tZONE, -HOUR(7.5) },/* Java (3pm in Cronusland!) */
437 { "cct", tZONE, -HOUR(8) }, /* China Coast, USSR Zone 7 */
438 { "jst", tZONE, -HOUR(9) }, /* Japan Standard, USSR Zone 8 */
440 { "cast", tZONE, -HOUR(9.5) },/* Central Australian Standard */
441 { "cadt", tDAYZONE, -HOUR(9.5) },/* Central Australian Daylight */
443 { "east", tZONE, -HOUR(10) }, /* Eastern Australian Standard */
444 { "eadt", tDAYZONE, -HOUR(10) }, /* Eastern Australian Daylight */
445 { "gst", tZONE, -HOUR(10) }, /* Guam Standard, USSR Zone 9 */
446 { "nzt", tZONE, -HOUR(12) }, /* New Zealand */
447 { "nzst", tZONE, -HOUR(12) }, /* New Zealand Standard */
448 { "nzdt", tDAYZONE, -HOUR(12) }, /* New Zealand Daylight */
449 { "idle", tZONE, -HOUR(12) }, /* International Date Line East */
453 /* Military timezone table. */
454 static TABLE MilitaryTable[] = {
455 { "a", tZONE, HOUR( 1) },
456 { "b", tZONE, HOUR( 2) },
457 { "c", tZONE, HOUR( 3) },
458 { "d", tZONE, HOUR( 4) },
459 { "e", tZONE, HOUR( 5) },
460 { "f", tZONE, HOUR( 6) },
461 { "g", tZONE, HOUR( 7) },
462 { "h", tZONE, HOUR( 8) },
463 { "i", tZONE, HOUR( 9) },
464 { "k", tZONE, HOUR( 10) },
465 { "l", tZONE, HOUR( 11) },
466 { "m", tZONE, HOUR( 12) },
467 { "n", tZONE, HOUR(- 1) },
468 { "o", tZONE, HOUR(- 2) },
469 { "p", tZONE, HOUR(- 3) },
470 { "q", tZONE, HOUR(- 4) },
471 { "r", tZONE, HOUR(- 5) },
472 { "s", tZONE, HOUR(- 6) },
473 { "t", tZONE, HOUR(- 7) },
474 { "u", tZONE, HOUR(- 8) },
475 { "v", tZONE, HOUR(- 9) },
476 { "w", tZONE, HOUR(-10) },
477 { "x", tZONE, HOUR(-11) },
478 { "y", tZONE, HOUR(-12) },
479 { "z", tZONE, HOUR( 0) },
496 ToSeconds(Hours, Minutes, Seconds, Meridian)
502 if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
506 if (Hours < 0 || Hours > 23)
508 return (Hours * 60L + Minutes) * 60L + Seconds;
510 if (Hours < 1 || Hours > 12)
512 return (Hours * 60L + Minutes) * 60L + Seconds;
514 if (Hours < 1 || Hours > 12)
516 return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
523 Convert(Month, Day, Year, Hours, Minutes, Seconds, Meridian, DSTmode)
533 static int DaysInMonth[12] = {
534 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
544 DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
546 if (Year < EPOCH || Year > 1999
547 || Month < 1 || Month > 12
548 /* Lint fluff: "conversion from long may lose accuracy" */
549 || Day < 1 || Day > DaysInMonth[(int)--Month])
552 for (Julian = Day - 1, i = 0; i < Month; i++)
553 Julian += DaysInMonth[i];
554 for (i = EPOCH; i < Year; i++)
555 Julian += 365 + (i % 4 == 0);
556 Julian *= SECSPERDAY;
557 Julian += yyTimezone * 60L;
558 if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
562 || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
569 DSTcorrect(Start, Future)
576 StartDay = (localtime(&Start)->tm_hour + 1) % 24;
577 FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
578 return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
583 RelativeDate(Start, DayOrdinal, DayNumber)
592 tm = localtime(&now);
593 now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
594 now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
595 return DSTcorrect(Start, now);
600 RelativeMonth(Start, RelMonth)
610 tm = localtime(&Start);
611 Month = 12 * tm->tm_year + tm->tm_mon + RelMonth;
613 Month = Month % 12 + 1;
614 return DSTcorrect(Start,
615 Convert(Month, (time_t)tm->tm_mday, Year,
616 (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
631 /* Make it lowercase. */
632 for (p = buff; *p; p++)
636 if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
637 yylval.Meridian = MERam;
640 if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
641 yylval.Meridian = MERpm;
645 /* See if we have an abbreviation for a month. */
646 if (strlen(buff) == 3)
648 else if (strlen(buff) == 4 && buff[3] == '.') {
655 for (tp = MonthDayTable; tp->name; tp++) {
657 if (strncmp(buff, tp->name, 3) == 0) {
658 yylval.Number = tp->value;
662 else if (strcmp(buff, tp->name) == 0) {
663 yylval.Number = tp->value;
668 for (tp = TimezoneTable; tp->name; tp++)
669 if (strcmp(buff, tp->name) == 0) {
670 yylval.Number = tp->value;
674 if (strcmp(buff, "dst") == 0)
677 for (tp = UnitsTable; tp->name; tp++)
678 if (strcmp(buff, tp->name) == 0) {
679 yylval.Number = tp->value;
683 /* Strip off any plural and try the units table again. */
684 i = strlen(buff) - 1;
685 if (buff[i] == 's') {
687 for (tp = UnitsTable; tp->name; tp++)
688 if (strcmp(buff, tp->name) == 0) {
689 yylval.Number = tp->value;
692 buff[i] = 's'; /* Put back for "this" in OtherTable. */
695 for (tp = OtherTable; tp->name; tp++)
696 if (strcmp(buff, tp->name) == 0) {
697 yylval.Number = tp->value;
701 /* Military timezones. */
702 if (buff[1] == '\0' && isalpha(*buff)) {
703 for (tp = MilitaryTable; tp->name; tp++)
704 if (strcmp(buff, tp->name) == 0) {
705 yylval.Number = tp->value;
710 /* Drop out any periods and try the timezone table again. */
711 for (i = 0, p = q = buff; *q; q++)
718 for (tp = TimezoneTable; tp->name; tp++)
719 if (strcmp(buff, tp->name) == 0) {
720 yylval.Number = tp->value;
738 while (isspace(*yyInput))
741 if (isdigit(c = *yyInput) || c == '-' || c == '+') {
742 if (c == '-' || c == '+') {
743 sign = c == '-' ? -1 : 1;
744 if (!isdigit(*++yyInput))
745 /* skip the '-' sign */
750 for (yylval.Number = 0; isdigit(c = *yyInput++); )
751 yylval.Number = 10 * yylval.Number + c - '0';
754 yylval.Number = -yylval.Number;
755 return sign ? tSNUMBER : tUNUMBER;
758 for (p = buff; isalpha(c = *yyInput++) || c == '.'; )
759 if (p < &buff[sizeof buff - 1])
763 return LookupWord(buff);
794 #if defined(FTIME_MISSING)
795 (void)time(&ftz.time);
796 /* Set the timezone global. */
798 #if defined(HAVE_TIMEZONE)
799 tm = localtime(&ftz.time);
800 ftz.timezone = tm->tm_gmtoff / 60;
802 #if defined(timezone)
803 ftz.tzone = (int) timezone / 60;
805 ftz.timezone = (int) timezone / 60;
806 #endif /* defined(timezone) */
807 #endif /* defined(HAVE_TIMEZONE) */
810 #endif /* defined(FTIME_MISSING) */
813 tm = localtime(&now->time);
814 yyYear = tm->tm_year;
815 yyMonth = tm->tm_mon + 1;
817 #if defined(timezone)
818 yyTimezone = now->tzone;
820 yyTimezone = now->timezone;
821 #endif /* defined(timezone) */
822 yyDSTmode = DSTmaybe;
836 || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
839 if (yyHaveDate || yyHaveTime || yyHaveDay) {
840 Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
841 yyMeridian, yyDSTmode);
848 Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
851 Start += yyRelSeconds;
852 Start += RelativeMonth(Start, yyRelMonth);
854 if (yyHaveDay && !yyHaveDate) {
855 tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
859 /* Have to do *something* with a legitimate -1 so it's distinguishable
860 * from the error return value. (Alternately could set errno on error.) */
861 return Start == -1 ? 0 : Start;
875 (void)printf("Enter date, or blank line to exit.\n\t> ");
876 (void)fflush(stdout);
877 while (gets(buff) && buff[0]) {
878 d = get_date(buff, (struct timeb *)NULL);
880 (void)printf("Bad format - couldn't convert.\n");
882 (void)printf("%s", ctime(&d));
883 (void)printf("\t> ");
884 (void)fflush(stdout);
889 #endif /* defined(TEST) */