3 * March 2005: Further modified and simplified by Tim Kientzle:
4 * Eliminate minutes-based calculations (just do everything in
5 * seconds), have lexer only recognize unsigned integers (handle '+'
6 * and '-' characters in grammar), combine tables into one table with
7 * explicit abbreviation notes, do am/pm adjustments in the grammar
8 * (eliminate some state variables and post-processing). Among other
9 * things, these changes eliminated two shift/reduce conflicts. (Went
14 ** Originally written by Steven M. Bellovin <smb@research.att.com> while
15 ** at the University of North Carolina at Chapel Hill. Later tweaked by
16 ** a couple of people on Usenet. Completely overhauled by Rich $alz
17 ** <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
19 ** This grammar has 10 shift/reduce conflicts.
21 ** This code is in the public domain and has no copyright.
23 /* SUPPRESS 287 on yaccpar_sccsid *//* Unused static variable */
24 /* SUPPRESS 288 on yyerrlab *//* Label unused */
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/tar/getdate.y,v 1.4 2005/04/23 18:38:32 kientzle Exp $");
35 #define yyparse getdate_yyparse
36 #define yylex getdate_yylex
37 #define yyerror getdate_yyerror
39 static int yyparse(void);
40 static int yylex(void);
41 static int yyerror(const char *);
43 time_t get_date(char *);
46 #define HOUR(x) ((time_t)(x) * 60)
47 #define SECSPERDAY (24L * 60L * 60L)
50 ** Daylight-savings mode: on, off, or not yet known.
52 typedef enum _DSTMODE {
53 DSTon, DSToff, DSTmaybe
57 ** Meridian: am or pm.
62 ** Global variables. We could get rid of most of these by using a good
63 ** union as the yacc stack. (This routine was originally written before
64 ** yacc had the %union construct.) Maybe someday; right now we only use
65 ** the %union very rarely.
69 static DSTMODE yyDSTmode;
70 static time_t yyDayOrdinal;
71 static time_t yyDayNumber;
72 static int yyHaveDate;
75 static int yyHaveTime;
76 static int yyHaveZone;
77 static time_t yyTimezone;
80 static time_t yyMinutes;
81 static time_t yyMonth;
82 static time_t yySeconds;
84 static time_t yyRelMonth;
85 static time_t yyRelSeconds;
93 %token tAGO tDAY tDAYZONE tAMPM tMONTH tMONTH_UNIT tSEC_UNIT tUNUMBER
96 %type <Number> tDAY tDAYZONE tMONTH tMONTH_UNIT
97 %type <Number> tSEC_UNIT tUNUMBER tZONE tAMPM
105 item : time { yyHaveTime++; }
106 | zone { yyHaveZone++; }
107 | date { yyHaveDate++; }
108 | day { yyHaveDay++; }
109 | rel { yyHaveRel++; }
113 time : tUNUMBER tAMPM {
124 /* "7:12:18" "19:17" */
127 /* "7:12pm", "12:20:13am" */
133 | bare_time '+' tUNUMBER {
136 yyTimezone = - ($3 % 100 + ($3 / 100) * 60);
138 | bare_time '-' tUNUMBER {
139 /* "19:14:12-0530" */
141 yyTimezone = + ($3 % 100 + ($3 / 100) * 60);
145 bare_time : tUNUMBER ':' tUNUMBER {
150 | tUNUMBER ':' tUNUMBER ':' tUNUMBER {
176 /* "tue," "wednesday," */
181 /* "second tues" "3 wed" */
187 date : tUNUMBER '/' tUNUMBER {
192 | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
194 /* First number is big: 2004/01/29, 99/02/17 */
198 } else if (($5 >= 13) || ($3 >= 13)) {
199 /* Last number is big: 01/07/98 */
200 /* Middle number is big: 01/29/04 */
205 /* No significant clues: 02/03/04 */
211 | tUNUMBER '-' tUNUMBER '-' tUNUMBER {
212 /* ISO 8601 format. yyyy-mm-dd. */
217 | tUNUMBER '-' tMONTH '-' tUNUMBER {
219 /* e.g. 1992-Jun-17 */
224 /* e.g. 17-JUN-1992. */
235 | tMONTH tUNUMBER ',' tUNUMBER {
236 /* "June 17, 2001" */
246 | tUNUMBER tMONTH tUNUMBER {
255 yyRelSeconds = -yyRelSeconds;
256 yyRelMonth = -yyRelMonth;
261 relunit : '-' tUNUMBER tSEC_UNIT {
263 yyRelSeconds -= $2 * $3;
265 | '+' tUNUMBER tSEC_UNIT {
267 yyRelSeconds += $2 * $3;
269 | tUNUMBER tSEC_UNIT {
277 | '-' tUNUMBER tMONTH_UNIT {
279 yyRelMonth -= $2 * $3;
281 | '+' tUNUMBER tMONTH_UNIT {
283 yyRelMonth += $2 * $3;
285 | tUNUMBER tMONTH_UNIT {
287 yyRelMonth += $1 * $2;
296 if (yyHaveTime && yyHaveDate && !yyHaveRel)
303 yyMonth= ($1/100)%100;
307 /* "513" is same as "5:13" */
315 yyMinutes = $1 % 100;
326 static struct TABLE {
331 } const TimeWords[] = {
333 { 0, "am", tAMPM, tAM },
334 { 0, "pm", tAMPM, tPM },
337 { 3, "january", tMONTH, 1 },
338 { 3, "february", tMONTH, 2 },
339 { 3, "march", tMONTH, 3 },
340 { 3, "april", tMONTH, 4 },
341 { 3, "may", tMONTH, 5 },
342 { 3, "june", tMONTH, 6 },
343 { 3, "july", tMONTH, 7 },
344 { 3, "august", tMONTH, 8 },
345 { 3, "september", tMONTH, 9 },
346 { 3, "october", tMONTH, 10 },
347 { 3, "november", tMONTH, 11 },
348 { 3, "december", tMONTH, 12 },
350 /* Days of the week. */
351 { 2, "sunday", tDAY, 0 },
352 { 3, "monday", tDAY, 1 },
353 { 2, "tuesday", tDAY, 2 },
354 { 3, "wednesday", tDAY, 3 },
355 { 2, "thursday", tDAY, 4 },
356 { 2, "friday", tDAY, 5 },
357 { 2, "saturday", tDAY, 6 },
359 /* Timezones: Offsets are in minutes. */
360 { 0, "gmt", tZONE, HOUR( 0) }, /* Greenwich Mean */
361 { 0, "ut", tZONE, HOUR( 0) }, /* Universal (Coordinated) */
362 { 0, "utc", tZONE, HOUR( 0) },
363 { 0, "wet", tZONE, HOUR( 0) }, /* Western European */
364 { 0, "bst", tDAYZONE, HOUR( 0) }, /* British Summer */
365 { 0, "wat", tZONE, HOUR( 1) }, /* West Africa */
366 { 0, "at", tZONE, HOUR( 2) }, /* Azores */
367 /* { 0, "bst", tZONE, HOUR( 3) }, */ /* Brazil Standard: Conflict */
368 /* { 0, "gst", tZONE, HOUR( 3) }, */ /* Greenland Standard: Conflict*/
369 { 0, "nft", tZONE, HOUR(3)+30 }, /* Newfoundland */
370 { 0, "nst", tZONE, HOUR(3)+30 }, /* Newfoundland Standard */
371 { 0, "ndt", tDAYZONE, HOUR(3)+30 }, /* Newfoundland Daylight */
372 { 0, "ast", tZONE, HOUR( 4) }, /* Atlantic Standard */
373 { 0, "adt", tDAYZONE, HOUR( 4) }, /* Atlantic Daylight */
374 { 0, "est", tZONE, HOUR( 5) }, /* Eastern Standard */
375 { 0, "edt", tDAYZONE, HOUR( 5) }, /* Eastern Daylight */
376 { 0, "cst", tZONE, HOUR( 6) }, /* Central Standard */
377 { 0, "cdt", tDAYZONE, HOUR( 6) }, /* Central Daylight */
378 { 0, "mst", tZONE, HOUR( 7) }, /* Mountain Standard */
379 { 0, "mdt", tDAYZONE, HOUR( 7) }, /* Mountain Daylight */
380 { 0, "pst", tZONE, HOUR( 8) }, /* Pacific Standard */
381 { 0, "pdt", tDAYZONE, HOUR( 8) }, /* Pacific Daylight */
382 { 0, "yst", tZONE, HOUR( 9) }, /* Yukon Standard */
383 { 0, "ydt", tDAYZONE, HOUR( 9) }, /* Yukon Daylight */
384 { 0, "hst", tZONE, HOUR(10) }, /* Hawaii Standard */
385 { 0, "hdt", tDAYZONE, HOUR(10) }, /* Hawaii Daylight */
386 { 0, "cat", tZONE, HOUR(10) }, /* Central Alaska */
387 { 0, "ahst", tZONE, HOUR(10) }, /* Alaska-Hawaii Standard */
388 { 0, "nt", tZONE, HOUR(11) }, /* Nome */
389 { 0, "idlw", tZONE, HOUR(12) }, /* Intl Date Line West */
390 { 0, "cet", tZONE, -HOUR(1) }, /* Central European */
391 { 0, "met", tZONE, -HOUR(1) }, /* Middle European */
392 { 0, "mewt", tZONE, -HOUR(1) }, /* Middle European Winter */
393 { 0, "mest", tDAYZONE, -HOUR(1) }, /* Middle European Summer */
394 { 0, "swt", tZONE, -HOUR(1) }, /* Swedish Winter */
395 { 0, "sst", tDAYZONE, -HOUR(1) }, /* Swedish Summer */
396 { 0, "fwt", tZONE, -HOUR(1) }, /* French Winter */
397 { 0, "fst", tDAYZONE, -HOUR(1) }, /* French Summer */
398 { 0, "eet", tZONE, -HOUR(2) }, /* Eastern Eur, USSR Zone 1 */
399 { 0, "bt", tZONE, -HOUR(3) }, /* Baghdad, USSR Zone 2 */
400 { 0, "it", tZONE, -HOUR(3)-30 },/* Iran */
401 { 0, "zp4", tZONE, -HOUR(4) }, /* USSR Zone 3 */
402 { 0, "zp5", tZONE, -HOUR(5) }, /* USSR Zone 4 */
403 { 0, "ist", tZONE, -HOUR(5)-30 },/* Indian Standard */
404 { 0, "zp6", tZONE, -HOUR(6) }, /* USSR Zone 5 */
405 /* { 0, "nst", tZONE, -HOUR(6.5) }, */ /* North Sumatra: Conflict */
406 /* { 0, "sst", tZONE, -HOUR(7) }, */ /* So Sumatra, USSR 6: Conflict */
407 { 0, "wast", tZONE, -HOUR(7) }, /* West Australian Standard */
408 { 0, "wadt", tDAYZONE, -HOUR(7) }, /* West Australian Daylight */
409 { 0, "jt", tZONE, -HOUR(7)-30 },/* Java (3pm in Cronusland!)*/
410 { 0, "cct", tZONE, -HOUR(8) }, /* China Coast, USSR Zone 7 */
411 { 0, "jst", tZONE, -HOUR(9) }, /* Japan Std, USSR Zone 8 */
412 { 0, "cast", tZONE, -HOUR(9)-30 },/* Central Australian Std */
413 { 0, "cadt", tDAYZONE, -HOUR(9)-30 },/* Central Australian Daylt */
414 { 0, "east", tZONE, -HOUR(10) }, /* Eastern Australian Std */
415 { 0, "eadt", tDAYZONE, -HOUR(10) }, /* Eastern Australian Daylt */
416 { 0, "gst", tZONE, -HOUR(10) }, /* Guam Std, USSR Zone 9 */
417 { 0, "nzt", tZONE, -HOUR(12) }, /* New Zealand */
418 { 0, "nzst", tZONE, -HOUR(12) }, /* New Zealand Standard */
419 { 0, "nzdt", tDAYZONE, -HOUR(12) }, /* New Zealand Daylight */
420 { 0, "idle", tZONE, -HOUR(12) }, /* Intl Date Line East */
422 { 0, "dst", tDST, 0 },
425 { 4, "years", tMONTH_UNIT, 12 },
426 { 5, "months", tMONTH_UNIT, 1 },
427 { 9, "fortnights", tSEC_UNIT, 14 * 24 * 60 * 60 },
428 { 4, "weeks", tSEC_UNIT, 7 * 24 * 60 * 60 },
429 { 3, "days", tSEC_UNIT, 1 * 24 * 60 * 60 },
430 { 4, "hours", tSEC_UNIT, 60 * 60 },
431 { 3, "minutes", tSEC_UNIT, 60 },
432 { 3, "seconds", tSEC_UNIT, 1 },
434 /* Relative-time words. */
435 { 0, "tomorrow", tSEC_UNIT, 1 * 24 * 60 * 60 },
436 { 0, "yesterday", tSEC_UNIT, -1 * 24 * 60 * 60 },
437 { 0, "today", tSEC_UNIT, 0 },
438 { 0, "now", tSEC_UNIT, 0 },
439 { 0, "last", tUNUMBER, -1 },
440 { 0, "this", tSEC_UNIT, 0 },
441 { 0, "next", tUNUMBER, 2 },
442 { 0, "first", tUNUMBER, 1 },
443 { 0, "1st", tUNUMBER, 1 },
444 /* { 0, "second", tUNUMBER, 2 }, */
445 { 0, "2nd", tUNUMBER, 2 },
446 { 0, "third", tUNUMBER, 3 },
447 { 0, "3rd", tUNUMBER, 3 },
448 { 0, "fourth", tUNUMBER, 4 },
449 { 0, "4th", tUNUMBER, 4 },
450 { 0, "fifth", tUNUMBER, 5 },
451 { 0, "5th", tUNUMBER, 5 },
452 { 0, "sixth", tUNUMBER, 6 },
453 { 0, "seventh", tUNUMBER, 7 },
454 { 0, "eighth", tUNUMBER, 8 },
455 { 0, "ninth", tUNUMBER, 9 },
456 { 0, "tenth", tUNUMBER, 10 },
457 { 0, "eleventh", tUNUMBER, 11 },
458 { 0, "twelfth", tUNUMBER, 12 },
459 { 0, "ago", tAGO, 1 },
461 /* Military timezones. */
462 { 0, "a", tZONE, HOUR( 1) },
463 { 0, "b", tZONE, HOUR( 2) },
464 { 0, "c", tZONE, HOUR( 3) },
465 { 0, "d", tZONE, HOUR( 4) },
466 { 0, "e", tZONE, HOUR( 5) },
467 { 0, "f", tZONE, HOUR( 6) },
468 { 0, "g", tZONE, HOUR( 7) },
469 { 0, "h", tZONE, HOUR( 8) },
470 { 0, "i", tZONE, HOUR( 9) },
471 { 0, "k", tZONE, HOUR( 10) },
472 { 0, "l", tZONE, HOUR( 11) },
473 { 0, "m", tZONE, HOUR( 12) },
474 { 0, "n", tZONE, HOUR(- 1) },
475 { 0, "o", tZONE, HOUR(- 2) },
476 { 0, "p", tZONE, HOUR(- 3) },
477 { 0, "q", tZONE, HOUR(- 4) },
478 { 0, "r", tZONE, HOUR(- 5) },
479 { 0, "s", tZONE, HOUR(- 6) },
480 { 0, "t", tZONE, HOUR(- 7) },
481 { 0, "u", tZONE, HOUR(- 8) },
482 { 0, "v", tZONE, HOUR(- 9) },
483 { 0, "w", tZONE, HOUR(-10) },
484 { 0, "x", tZONE, HOUR(-11) },
485 { 0, "y", tZONE, HOUR(-12) },
486 { 0, "z", tZONE, HOUR( 0) },
497 yyerror(const char *s)
504 ToSeconds(time_t Hours, time_t Minutes, time_t Seconds)
506 if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
508 if (Hours < 0 || Hours > 23)
510 return (Hours * 60L + Minutes) * 60L + Seconds;
515 * A number from 0 to 99, which means a year from 1970 to 2069, or
516 * The actual year (>=100). */
518 Convert(time_t Month, time_t Day, time_t Year,
519 time_t Hours, time_t Minutes, time_t Seconds, DSTMODE DSTmode)
521 static int DaysInMonth[12] = {
522 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
532 DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
534 /* Checking for 2038 bogusly assumes that time_t is 32 bits. But
535 I'm too lazy to try to check for time_t overflow in another way. */
536 if (Year < EPOCH || Year > 2038
537 || Month < 1 || Month > 12
538 /* Lint fluff: "conversion from long may lose accuracy" */
539 || Day < 1 || Day > DaysInMonth[(int)--Month])
543 for (i = 0; i < Month; i++)
544 Julian += DaysInMonth[i];
545 for (i = EPOCH; i < Year; i++)
546 Julian += 365 + (i % 4 == 0);
547 Julian *= SECSPERDAY;
548 Julian += yyTimezone * 60L;
549 if ((tod = ToSeconds(Hours, Minutes, Seconds)) < 0)
553 || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
560 DSTcorrect(time_t Start, time_t Future)
565 StartDay = (localtime(&Start)->tm_hour + 1) % 24;
566 FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
567 return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
572 RelativeDate(time_t Start, time_t DayOrdinal, time_t DayNumber)
578 tm = localtime(&now);
579 now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
580 now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
581 return DSTcorrect(Start, now);
586 RelativeMonth(time_t Start, time_t RelMonth)
594 tm = localtime(&Start);
595 Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
597 Month = Month % 12 + 1;
598 return DSTcorrect(Start,
599 Convert(Month, (time_t)tm->tm_mday, Year,
600 (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
611 while (isspace(*yyInput))
614 /* Skip parenthesized comments. */
615 if (*yyInput == '(') {
629 /* Try the next token in the word table first. */
630 /* This allows us to match "2nd", for example. */
633 const struct TABLE *tp;
636 /* Force to lowercase and strip '.' characters. */
638 && (isalnum(*src) || *src == '.')
639 && i < sizeof(buff)-1) {
642 buff[i++] = tolower(*src);
651 * Find the first match. If the word can be
652 * abbreviated, make sure we match at least
653 * the minimum abbreviation.
655 for (tp = TimeWords; tp->name; tp++) {
656 size_t abbrev = tp->abbrev;
658 abbrev = strlen(tp->name);
659 if (strlen(buff) >= abbrev
660 && strncmp(tp->name, buff, strlen(buff))
662 /* Skip over token. */
664 /* Return the match. */
665 yylval.Number = tp->value;
672 * Not in the word table, maybe it's a number. Note:
673 * Because '-' and '+' have other special meanings, I
674 * don't deal with signed numbers here.
676 if (isdigit(c = *yyInput)) {
677 for (yylval.Number = 0; isdigit(c = *yyInput++); )
678 yylval.Number = 10 * yylval.Number + c - '0';
687 #define TM_YEAR_ORIGIN 1900
689 /* Yield A - B, measured in seconds. */
691 difftm (struct tm *a, struct tm *b)
693 int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
694 int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
696 /* difference in day of year */
697 a->tm_yday - b->tm_yday
698 /* + intervening leap days */
699 + ((ay >> 2) - (by >> 2))
701 + ((ay/100 >> 2) - (by/100 >> 2))
702 /* + difference in years * 365 */
703 + (long)(ay-by) * 365
705 return (60*(60*(24*days + (a->tm_hour - b->tm_hour))
706 + (a->tm_min - b->tm_min))
707 + (a->tm_sec - b->tm_sec));
714 struct tm gmt, *gmt_ptr;
722 (void)time (&nowtime);
724 gmt_ptr = gmtime (&nowtime);
725 if (gmt_ptr != NULL) {
726 /* Copy, in case localtime and gmtime use the same buffer. */
730 if (! (tm = localtime (&nowtime)))
734 tzone = difftm (&gmt, tm) / 60;
736 /* This system doesn't understand timezones; fake it. */
741 yyYear = tm->tm_year + 1900;
742 yyMonth = tm->tm_mon + 1;
745 yyDSTmode = DSTmaybe;
758 || yyHaveTime > 1 || yyHaveZone > 1
759 || yyHaveDate > 1 || yyHaveDay > 1)
762 if (yyHaveDate || yyHaveTime || yyHaveDay) {
763 Start = Convert(yyMonth, yyDay, yyYear,
764 yyHour, yyMinutes, yySeconds, yyDSTmode);
770 Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
773 Start += yyRelSeconds;
774 Start += RelativeMonth(Start, yyRelMonth);
776 if (yyHaveDay && !yyHaveDate) {
777 tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
781 /* Have to do *something* with a legitimate -1 so it's
782 * distinguishable from the error return value. (Alternately
783 * could set errno on error.) */
784 return Start == -1 ? 0 : Start;
792 main(int argc, char **argv)
796 while (*++argv != NULL) {
797 (void)printf("Input: %s\n", *argv);
800 (void)printf("Bad format - couldn't convert.\n");
802 (void)printf("Output: %s\n", ctime(&d));
807 #endif /* defined(TEST) */