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 */
27
#include <sys/cdefs.h>
28
__FBSDID("$FreeBSD: src/usr.bin/tar/getdate.y,v 1.6 2006/03/21 17:03:51 kientzle Exp $");
37
#define yyparse getdate_yyparse
38
#define yylex getdate_yylex
39
#define yyerror getdate_yyerror
41
static int yyparse(void);
42
static int yylex(void);
43
static int yyerror(const char *);
45
time_t get_date(char *);
48
#define HOUR(x) ((time_t)(x) * 60)
49
#define SECSPERDAY (24L * 60L * 60L)
52
** Daylight-savings mode: on, off, or not yet known.
54
typedef enum _DSTMODE {
55
DSTon, DSToff, DSTmaybe
59
** Meridian: am or pm.
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.
71
static DSTMODE yyDSTmode;
72
static time_t yyDayOrdinal;
73
static time_t yyDayNumber;
74
static int yyHaveDate;
77
static int yyHaveTime;
78
static int yyHaveZone;
79
static time_t yyTimezone;
82
static time_t yyMinutes;
83
static time_t yyMonth;
84
static time_t yySeconds;
86
static time_t yyRelMonth;
87
static time_t yyRelSeconds;
95
%token tAGO tDAY tDAYZONE tAMPM tMONTH tMONTH_UNIT tSEC_UNIT tUNUMBER
98
%type <Number> tDAY tDAYZONE tMONTH tMONTH_UNIT
99
%type <Number> tSEC_UNIT tUNUMBER tZONE tAMPM
107
item : time { yyHaveTime++; }
108
| zone { yyHaveZone++; }
109
| date { yyHaveDate++; }
110
| day { yyHaveDay++; }
111
| rel { yyHaveRel++; }
115
time : tUNUMBER tAMPM {
126
/* "7:12:18" "19:17" */
129
/* "7:12pm", "12:20:13am" */
135
| bare_time '+' tUNUMBER {
138
yyTimezone = - ($3 % 100 + ($3 / 100) * 60);
140
| bare_time '-' tUNUMBER {
141
/* "19:14:12-0530" */
143
yyTimezone = + ($3 % 100 + ($3 / 100) * 60);
147
bare_time : tUNUMBER ':' tUNUMBER {
152
| tUNUMBER ':' tUNUMBER ':' tUNUMBER {
178
/* "tue," "wednesday," */
183
/* "second tues" "3 wed" */
189
date : tUNUMBER '/' tUNUMBER {
194
| tUNUMBER '/' tUNUMBER '/' tUNUMBER {
196
/* First number is big: 2004/01/29, 99/02/17 */
200
} else if (($5 >= 13) || ($3 >= 13)) {
201
/* Last number is big: 01/07/98 */
202
/* Middle number is big: 01/29/04 */
207
/* No significant clues: 02/03/04 */
213
| tUNUMBER '-' tUNUMBER '-' tUNUMBER {
214
/* ISO 8601 format. yyyy-mm-dd. */
219
| tUNUMBER '-' tMONTH '-' tUNUMBER {
221
/* e.g. 1992-Jun-17 */
226
/* e.g. 17-JUN-1992. */
237
| tMONTH tUNUMBER ',' tUNUMBER {
238
/* "June 17, 2001" */
248
| tUNUMBER tMONTH tUNUMBER {
257
yyRelSeconds = -yyRelSeconds;
258
yyRelMonth = -yyRelMonth;
263
relunit : '-' tUNUMBER tSEC_UNIT {
265
yyRelSeconds -= $2 * $3;
267
| '+' tUNUMBER tSEC_UNIT {
269
yyRelSeconds += $2 * $3;
271
| tUNUMBER tSEC_UNIT {
279
| '-' tUNUMBER tMONTH_UNIT {
281
yyRelMonth -= $2 * $3;
283
| '+' tUNUMBER tMONTH_UNIT {
285
yyRelMonth += $2 * $3;
287
| tUNUMBER tMONTH_UNIT {
289
yyRelMonth += $1 * $2;
298
if (yyHaveTime && yyHaveDate && !yyHaveRel)
305
yyMonth= ($1/100)%100;
309
/* "513" is same as "5:13" */
317
yyMinutes = $1 % 100;
328
static struct TABLE {
333
} const TimeWords[] = {
335
{ 0, "am", tAMPM, tAM },
336
{ 0, "pm", tAMPM, tPM },
339
{ 3, "january", tMONTH, 1 },
340
{ 3, "february", tMONTH, 2 },
341
{ 3, "march", tMONTH, 3 },
342
{ 3, "april", tMONTH, 4 },
343
{ 3, "may", tMONTH, 5 },
344
{ 3, "june", tMONTH, 6 },
345
{ 3, "july", tMONTH, 7 },
346
{ 3, "august", tMONTH, 8 },
347
{ 3, "september", tMONTH, 9 },
348
{ 3, "october", tMONTH, 10 },
349
{ 3, "november", tMONTH, 11 },
350
{ 3, "december", tMONTH, 12 },
352
/* Days of the week. */
353
{ 2, "sunday", tDAY, 0 },
354
{ 3, "monday", tDAY, 1 },
355
{ 2, "tuesday", tDAY, 2 },
356
{ 3, "wednesday", tDAY, 3 },
357
{ 2, "thursday", tDAY, 4 },
358
{ 2, "friday", tDAY, 5 },
359
{ 2, "saturday", tDAY, 6 },
361
/* Timezones: Offsets are in minutes. */
362
{ 0, "gmt", tZONE, HOUR( 0) }, /* Greenwich Mean */
363
{ 0, "ut", tZONE, HOUR( 0) }, /* Universal (Coordinated) */
364
{ 0, "utc", tZONE, HOUR( 0) },
365
{ 0, "wet", tZONE, HOUR( 0) }, /* Western European */
366
{ 0, "bst", tDAYZONE, HOUR( 0) }, /* British Summer */
367
{ 0, "wat", tZONE, HOUR( 1) }, /* West Africa */
368
{ 0, "at", tZONE, HOUR( 2) }, /* Azores */
369
/* { 0, "bst", tZONE, HOUR( 3) }, */ /* Brazil Standard: Conflict */
370
/* { 0, "gst", tZONE, HOUR( 3) }, */ /* Greenland Standard: Conflict*/
371
{ 0, "nft", tZONE, HOUR(3)+30 }, /* Newfoundland */
372
{ 0, "nst", tZONE, HOUR(3)+30 }, /* Newfoundland Standard */
373
{ 0, "ndt", tDAYZONE, HOUR(3)+30 }, /* Newfoundland Daylight */
374
{ 0, "ast", tZONE, HOUR( 4) }, /* Atlantic Standard */
375
{ 0, "adt", tDAYZONE, HOUR( 4) }, /* Atlantic Daylight */
376
{ 0, "est", tZONE, HOUR( 5) }, /* Eastern Standard */
377
{ 0, "edt", tDAYZONE, HOUR( 5) }, /* Eastern Daylight */
378
{ 0, "cst", tZONE, HOUR( 6) }, /* Central Standard */
379
{ 0, "cdt", tDAYZONE, HOUR( 6) }, /* Central Daylight */
380
{ 0, "mst", tZONE, HOUR( 7) }, /* Mountain Standard */
381
{ 0, "mdt", tDAYZONE, HOUR( 7) }, /* Mountain Daylight */
382
{ 0, "pst", tZONE, HOUR( 8) }, /* Pacific Standard */
383
{ 0, "pdt", tDAYZONE, HOUR( 8) }, /* Pacific Daylight */
384
{ 0, "yst", tZONE, HOUR( 9) }, /* Yukon Standard */
385
{ 0, "ydt", tDAYZONE, HOUR( 9) }, /* Yukon Daylight */
386
{ 0, "hst", tZONE, HOUR(10) }, /* Hawaii Standard */
387
{ 0, "hdt", tDAYZONE, HOUR(10) }, /* Hawaii Daylight */
388
{ 0, "cat", tZONE, HOUR(10) }, /* Central Alaska */
389
{ 0, "ahst", tZONE, HOUR(10) }, /* Alaska-Hawaii Standard */
390
{ 0, "nt", tZONE, HOUR(11) }, /* Nome */
391
{ 0, "idlw", tZONE, HOUR(12) }, /* Intl Date Line West */
392
{ 0, "cet", tZONE, -HOUR(1) }, /* Central European */
393
{ 0, "met", tZONE, -HOUR(1) }, /* Middle European */
394
{ 0, "mewt", tZONE, -HOUR(1) }, /* Middle European Winter */
395
{ 0, "mest", tDAYZONE, -HOUR(1) }, /* Middle European Summer */
396
{ 0, "swt", tZONE, -HOUR(1) }, /* Swedish Winter */
397
{ 0, "sst", tDAYZONE, -HOUR(1) }, /* Swedish Summer */
398
{ 0, "fwt", tZONE, -HOUR(1) }, /* French Winter */
399
{ 0, "fst", tDAYZONE, -HOUR(1) }, /* French Summer */
400
{ 0, "eet", tZONE, -HOUR(2) }, /* Eastern Eur, USSR Zone 1 */
401
{ 0, "bt", tZONE, -HOUR(3) }, /* Baghdad, USSR Zone 2 */
402
{ 0, "it", tZONE, -HOUR(3)-30 },/* Iran */
403
{ 0, "zp4", tZONE, -HOUR(4) }, /* USSR Zone 3 */
404
{ 0, "zp5", tZONE, -HOUR(5) }, /* USSR Zone 4 */
405
{ 0, "ist", tZONE, -HOUR(5)-30 },/* Indian Standard */
406
{ 0, "zp6", tZONE, -HOUR(6) }, /* USSR Zone 5 */
407
/* { 0, "nst", tZONE, -HOUR(6.5) }, */ /* North Sumatra: Conflict */
408
/* { 0, "sst", tZONE, -HOUR(7) }, */ /* So Sumatra, USSR 6: Conflict */
409
{ 0, "wast", tZONE, -HOUR(7) }, /* West Australian Standard */
410
{ 0, "wadt", tDAYZONE, -HOUR(7) }, /* West Australian Daylight */
411
{ 0, "jt", tZONE, -HOUR(7)-30 },/* Java (3pm in Cronusland!)*/
412
{ 0, "cct", tZONE, -HOUR(8) }, /* China Coast, USSR Zone 7 */
413
{ 0, "jst", tZONE, -HOUR(9) }, /* Japan Std, USSR Zone 8 */
414
{ 0, "cast", tZONE, -HOUR(9)-30 },/* Central Australian Std */
415
{ 0, "cadt", tDAYZONE, -HOUR(9)-30 },/* Central Australian Daylt */
416
{ 0, "east", tZONE, -HOUR(10) }, /* Eastern Australian Std */
417
{ 0, "eadt", tDAYZONE, -HOUR(10) }, /* Eastern Australian Daylt */
418
{ 0, "gst", tZONE, -HOUR(10) }, /* Guam Std, USSR Zone 9 */
419
{ 0, "nzt", tZONE, -HOUR(12) }, /* New Zealand */
420
{ 0, "nzst", tZONE, -HOUR(12) }, /* New Zealand Standard */
421
{ 0, "nzdt", tDAYZONE, -HOUR(12) }, /* New Zealand Daylight */
422
{ 0, "idle", tZONE, -HOUR(12) }, /* Intl Date Line East */
424
{ 0, "dst", tDST, 0 },
427
{ 4, "years", tMONTH_UNIT, 12 },
428
{ 5, "months", tMONTH_UNIT, 1 },
429
{ 9, "fortnights", tSEC_UNIT, 14 * 24 * 60 * 60 },
430
{ 4, "weeks", tSEC_UNIT, 7 * 24 * 60 * 60 },
431
{ 3, "days", tSEC_UNIT, 1 * 24 * 60 * 60 },
432
{ 4, "hours", tSEC_UNIT, 60 * 60 },
433
{ 3, "minutes", tSEC_UNIT, 60 },
434
{ 3, "seconds", tSEC_UNIT, 1 },
436
/* Relative-time words. */
437
{ 0, "tomorrow", tSEC_UNIT, 1 * 24 * 60 * 60 },
438
{ 0, "yesterday", tSEC_UNIT, -1 * 24 * 60 * 60 },
439
{ 0, "today", tSEC_UNIT, 0 },
440
{ 0, "now", tSEC_UNIT, 0 },
441
{ 0, "last", tUNUMBER, -1 },
442
{ 0, "this", tSEC_UNIT, 0 },
443
{ 0, "next", tUNUMBER, 2 },
444
{ 0, "first", tUNUMBER, 1 },
445
{ 0, "1st", tUNUMBER, 1 },
446
/* { 0, "second", tUNUMBER, 2 }, */
447
{ 0, "2nd", tUNUMBER, 2 },
448
{ 0, "third", tUNUMBER, 3 },
449
{ 0, "3rd", tUNUMBER, 3 },
450
{ 0, "fourth", tUNUMBER, 4 },
451
{ 0, "4th", tUNUMBER, 4 },
452
{ 0, "fifth", tUNUMBER, 5 },
453
{ 0, "5th", tUNUMBER, 5 },
454
{ 0, "sixth", tUNUMBER, 6 },
455
{ 0, "seventh", tUNUMBER, 7 },
456
{ 0, "eighth", tUNUMBER, 8 },
457
{ 0, "ninth", tUNUMBER, 9 },
458
{ 0, "tenth", tUNUMBER, 10 },
459
{ 0, "eleventh", tUNUMBER, 11 },
460
{ 0, "twelfth", tUNUMBER, 12 },
461
{ 0, "ago", tAGO, 1 },
463
/* Military timezones. */
464
{ 0, "a", tZONE, HOUR( 1) },
465
{ 0, "b", tZONE, HOUR( 2) },
466
{ 0, "c", tZONE, HOUR( 3) },
467
{ 0, "d", tZONE, HOUR( 4) },
468
{ 0, "e", tZONE, HOUR( 5) },
469
{ 0, "f", tZONE, HOUR( 6) },
470
{ 0, "g", tZONE, HOUR( 7) },
471
{ 0, "h", tZONE, HOUR( 8) },
472
{ 0, "i", tZONE, HOUR( 9) },
473
{ 0, "k", tZONE, HOUR( 10) },
474
{ 0, "l", tZONE, HOUR( 11) },
475
{ 0, "m", tZONE, HOUR( 12) },
476
{ 0, "n", tZONE, HOUR(- 1) },
477
{ 0, "o", tZONE, HOUR(- 2) },
478
{ 0, "p", tZONE, HOUR(- 3) },
479
{ 0, "q", tZONE, HOUR(- 4) },
480
{ 0, "r", tZONE, HOUR(- 5) },
481
{ 0, "s", tZONE, HOUR(- 6) },
482
{ 0, "t", tZONE, HOUR(- 7) },
483
{ 0, "u", tZONE, HOUR(- 8) },
484
{ 0, "v", tZONE, HOUR(- 9) },
485
{ 0, "w", tZONE, HOUR(-10) },
486
{ 0, "x", tZONE, HOUR(-11) },
487
{ 0, "y", tZONE, HOUR(-12) },
488
{ 0, "z", tZONE, HOUR( 0) },
499
yyerror(const char *s)
506
ToSeconds(time_t Hours, time_t Minutes, time_t Seconds)
508
if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
510
if (Hours < 0 || Hours > 23)
512
return (Hours * 60L + Minutes) * 60L + Seconds;
517
* A number from 0 to 99, which means a year from 1970 to 2069, or
518
* The actual year (>=100). */
520
Convert(time_t Month, time_t Day, time_t Year,
521
time_t Hours, time_t Minutes, time_t Seconds, DSTMODE DSTmode)
523
static int DaysInMonth[12] = {
524
31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
534
DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
536
/* Checking for 2038 bogusly assumes that time_t is 32 bits. But
537
I'm too lazy to try to check for time_t overflow in another way. */
538
if (Year < EPOCH || Year > 2038
539
|| Month < 1 || Month > 12
540
/* Lint fluff: "conversion from long may lose accuracy" */
541
|| Day < 1 || Day > DaysInMonth[(int)--Month])
545
for (i = 0; i < Month; i++)
546
Julian += DaysInMonth[i];
547
for (i = EPOCH; i < Year; i++)
548
Julian += 365 + (i % 4 == 0);
549
Julian *= SECSPERDAY;
550
Julian += yyTimezone * 60L;
551
if ((tod = ToSeconds(Hours, Minutes, Seconds)) < 0)
555
|| (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
562
DSTcorrect(time_t Start, time_t Future)
567
StartDay = (localtime(&Start)->tm_hour + 1) % 24;
568
FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
569
return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
574
RelativeDate(time_t Start, time_t DayOrdinal, time_t DayNumber)
580
tm = localtime(&now);
581
now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
582
now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
583
return DSTcorrect(Start, now);
588
RelativeMonth(time_t Start, time_t RelMonth)
596
tm = localtime(&Start);
597
Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
599
Month = Month % 12 + 1;
600
return DSTcorrect(Start,
601
Convert(Month, (time_t)tm->tm_mday, Year,
602
(time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
613
while (isspace(*yyInput))
616
/* Skip parenthesized comments. */
617
if (*yyInput == '(') {
631
/* Try the next token in the word table first. */
632
/* This allows us to match "2nd", for example. */
635
const struct TABLE *tp;
638
/* Force to lowercase and strip '.' characters. */
640
&& (isalnum(*src) || *src == '.')
641
&& i < sizeof(buff)-1) {
644
buff[i++] = tolower(*src);
653
* Find the first match. If the word can be
654
* abbreviated, make sure we match at least
655
* the minimum abbreviation.
657
for (tp = TimeWords; tp->name; tp++) {
658
size_t abbrev = tp->abbrev;
660
abbrev = strlen(tp->name);
661
if (strlen(buff) >= abbrev
662
&& strncmp(tp->name, buff, strlen(buff))
664
/* Skip over token. */
666
/* Return the match. */
667
yylval.Number = tp->value;
674
* Not in the word table, maybe it's a number. Note:
675
* Because '-' and '+' have other special meanings, I
676
* don't deal with signed numbers here.
678
if (isdigit(c = *yyInput)) {
679
for (yylval.Number = 0; isdigit(c = *yyInput++); )
680
yylval.Number = 10 * yylval.Number + c - '0';
689
#define TM_YEAR_ORIGIN 1900
691
/* Yield A - B, measured in seconds. */
693
difftm (struct tm *a, struct tm *b)
695
int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
696
int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
698
/* difference in day of year */
699
a->tm_yday - b->tm_yday
700
/* + intervening leap days */
701
+ ((ay >> 2) - (by >> 2))
703
+ ((ay/100 >> 2) - (by/100 >> 2))
704
/* + difference in years * 365 */
705
+ (long)(ay-by) * 365
707
return (60*(60*(24*days + (a->tm_hour - b->tm_hour))
708
+ (a->tm_min - b->tm_min))
709
+ (a->tm_sec - b->tm_sec));
716
struct tm gmt, *gmt_ptr;
722
memset(&gmt, 0, sizeof(gmt));
725
(void)time (&nowtime);
727
gmt_ptr = gmtime (&nowtime);
728
if (gmt_ptr != NULL) {
729
/* Copy, in case localtime and gmtime use the same buffer. */
733
if (! (tm = localtime (&nowtime)))
737
tzone = difftm (&gmt, tm) / 60;
739
/* This system doesn't understand timezones; fake it. */
744
yyYear = tm->tm_year + 1900;
745
yyMonth = tm->tm_mon + 1;
748
yyDSTmode = DSTmaybe;
761
|| yyHaveTime > 1 || yyHaveZone > 1
762
|| yyHaveDate > 1 || yyHaveDay > 1)
765
if (yyHaveDate || yyHaveTime || yyHaveDay) {
766
Start = Convert(yyMonth, yyDay, yyYear,
767
yyHour, yyMinutes, yySeconds, yyDSTmode);
773
Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
776
Start += yyRelSeconds;
777
Start += RelativeMonth(Start, yyRelMonth);
779
if (yyHaveDay && !yyHaveDate) {
780
tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
784
/* Have to do *something* with a legitimate -1 so it's
785
* distinguishable from the error return value. (Alternately
786
* could set errno on error.) */
787
return Start == -1 ? 0 : Start;
795
main(int argc, char **argv)
799
while (*++argv != NULL) {
800
(void)printf("Input: %s\n", *argv);
803
(void)printf("Bad format - couldn't convert.\n");
805
(void)printf("Output: %s\n", ctime(&d));
810
#endif /* defined(TEST) */