4 * Contains yacc grammer for parsing date and time strings..
5 *---------------------------------------------------------------------------
6 * Copyright 1992 Karl Lehenbauer and Mark Diekhans.
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation for any purpose and without fee is hereby granted, provided
10 * that the above copyright notice appear in all copies. Karl Lehenbauer and
11 * Mark Diekhans make no representations about the suitability of this
12 * software for any purpose. It is provided "as is" without express or
14 *---------------------------------------------------------------------------
15 * This code is a slightly modified version of getdate.y by:
16 * Steven M. Bellovin (unc!smb)
17 * Dept. of Computer Science
18 * University of North Carolina at Chapel Hill
19 * getdate.y 2.13 9/16/86
20 *-----------------------------------------------------------------------------
21 * $Id: tclXgetdate.y,v 2.0 1992/10/16 04:51:34 markd Rel $
22 *-----------------------------------------------------------------------------
25 %token ID MONTH DAY MERIDIAN NUMBER UNIT MUNIT SUNIT ZONE DAYZONE AGO
29 #include <sys/types.h>
37 #define daysec (24L*60L*60L)
39 static int timeflag, zoneflag, dateflag, dayflag, relflag;
40 static time_t relsec, relmonth;
41 static int hh, mm, ss, merid, dayLight;
42 static int dayord, dayreq;
43 static int month, day, year;
47 static time_t timeconv(int hh, int mm, int ss, int mer);
48 static time_t daylcorr(time_t future, time_t now);
49 static lookup(char *id);
51 static time_t timeconv();
52 static time_t daylcorr();
80 {if (timeflag && dateflag && !relflag) year = $1;
81 else {timeflag++;hh = $1/100;mm = $1%100;ss = 0;merid = 24;}};
83 tspec: NUMBER MERIDIAN
84 {hh = $1; mm = 0; ss = 0; merid = $2;}
86 {hh = $1; mm = $3; merid = 24;}
87 | NUMBER ':' NUMBER MERIDIAN
88 {hh = $1; mm = $3; merid = $4;}
89 | NUMBER ':' NUMBER NUMBER
90 {hh = $1; mm = $3; merid = 24;
91 dayLight = STANDARD; ourzone = -($4%100 + 60*$4/100);}
92 | NUMBER ':' NUMBER ':' NUMBER
93 {hh = $1; mm = $3; ss = $5; merid = 24;}
94 | NUMBER ':' NUMBER ':' NUMBER MERIDIAN
95 {hh = $1; mm = $3; ss = $5; merid = $6;}
96 | NUMBER ':' NUMBER ':' NUMBER NUMBER
97 {hh = $1; mm = $3; ss = $5; merid = 24;
98 dayLight = STANDARD; ourzone = -($6%100 + 60*$6/100);};
101 {ourzone = $1; dayLight = STANDARD;}
103 {ourzone = $1; dayLight = DAYLIGHT;};
106 {dayord = 1; dayreq = $1;}
108 {dayord = 1; dayreq = $1;}
110 {dayord = $1; dayreq = $2;};
112 dtspec: NUMBER '/' NUMBER
113 {month = $1; day = $3;}
114 | NUMBER '/' NUMBER '/' NUMBER
115 {month = $1; day = $3; year = $5;}
117 {month = $1; day = $2;}
118 | MONTH NUMBER ',' NUMBER
119 {month = $1; day = $2; year = $4;}
121 {month = $2; day = $1;}
122 | NUMBER MONTH NUMBER
123 {month = $2; day = $1; year = $3;};
127 {relsec += 60L * $1 * $2;}
129 {relmonth += $1 * $2;}
133 {relsec += 60L * $1;}
139 {relsec = -relsec; relmonth = -relmonth;};
142 static int mdays[12] =
143 {31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
146 extern struct tm *localtime();
149 time_t dateconv(mm, dd, yy, h, m, s, mer, zone, dayflag)
150 int mm, dd, yy, h, m, s, mer, zone, dayflag;
156 if (yy < 0) yy = -yy;
157 if (yy < 100) yy += 1900;
158 mdays[1] = 28 + (yy%4 == 0 && (yy%100 != 0 || yy%400 == 0));
159 if (yy < epoch || yy > 1999 || mm < 1 || mm > 12 ||
160 dd < 1 || dd > mdays[--mm]) return (-1);
162 for (i=0; i<mm; i++) jdate += mdays[i];
163 for (i = epoch; i < yy; i++) jdate += 365 + (i%4 == 0);
166 if ((tod = timeconv(h, m, s, mer)) < 0) return (-1);
168 if (dayflag==DAYLIGHT || (dayflag==MAYBE&&localtime(&jdate)->tm_isdst))
174 time_t dayconv(ord, day, now) int ord, day; time_t now;
176 register struct tm *loctime;
181 loctime = localtime(&tod);
182 tod += daysec * ((day - loctime->tm_wday + 7) % 7);
183 tod += 7*daysec*(ord<=0?ord:ord-1);
184 return daylcorr(tod, now);
188 time_t timeconv(hh, mm, ss, mer) register int hh, mm, ss, mer;
190 if (mm < 0 || mm > 59 || ss < 0 || ss > 59) return (-1);
192 case AM: if (hh < 1 || hh > 12) return(-1);
193 return (60L * ((hh%12)*60L + mm)+ss);
194 case PM: if (hh < 1 || hh > 12) return(-1);
195 return (60L * ((hh%12 +12)*60L + mm)+ss);
196 case 24: if (hh < 0 || hh > 23) return (-1);
197 return (60L * (hh*60L + mm)+ss);
198 default: return (-1);
203 time_t monthadd(sdate, relmonth) time_t sdate, relmonth;
210 if (relmonth == 0) return 0;
211 ltime = localtime(&sdate);
212 mm = 12*ltime->tm_year + ltime->tm_mon + relmonth;
215 return daylcorr(dateconv(mm, ltime->tm_mday, yy, ltime->tm_hour,
216 ltime->tm_min, ltime->tm_sec, 24, ourzone, MAYBE), sdate);
220 time_t daylcorr(future, now) time_t future, now;
224 nowdayl = (localtime(&now)->tm_hour+1) % 24;
225 fdayl = (localtime(&future)->tm_hour+1) % 24;
226 return (future-now) + 60L*60L*(nowdayl-fdayl);
235 //#define YYSTYPE extern int
246 while (isspace(*lptr)) lptr++;
248 if (isdigit(c = *lptr) || c == '-' || c == '+') {
249 if (c== '-' || c == '+') {
250 if (c=='-') sign = -1;
252 if (!isdigit(*++lptr)) {
253 /* yylval = sign; return (NUMBER); */
254 return yylex(); /* skip the '-' sign */
258 while (isdigit(c = *lptr++)) yylval = 10*yylval + c - '0';
263 } else if (isalpha(c)) {
265 while (isalpha(c = *lptr++) || c=='.')
266 if (p < &idbuf[sizeof(idbuf)-1])
270 return (lookup(idbuf));
277 if (c == '\0') return(c);
278 else if (c == '(') pcnt++;
279 else if (c == ')') pcnt--;
283 else return (*lptr++);
294 struct table mdtab[] = {
295 {"January", MONTH, 1},
296 {"February", MONTH, 2},
302 {"August", MONTH, 8},
303 {"September", MONTH, 9},
305 {"October", MONTH, 10},
306 {"November", MONTH, 11},
307 {"December", MONTH, 12},
313 {"Wednesday", DAY, 3},
315 {"Thursday", DAY, 4},
319 {"Saturday", DAY, 6},
325 struct table mztab[] = {
326 {"a.m.", MERIDIAN, AM},
327 {"am", MERIDIAN, AM},
328 {"p.m.", MERIDIAN, PM},
329 {"pm", MERIDIAN, PM},
330 {"nst", ZONE, 3 HRS + HALFHR}, /* Newfoundland */
331 {"n.s.t.", ZONE, 3 HRS + HALFHR},
332 {"ast", ZONE, 4 HRS}, /* Atlantic */
333 {"a.s.t.", ZONE, 4 HRS},
334 {"adt", DAYZONE, 4 HRS},
335 {"a.d.t.", DAYZONE, 4 HRS},
336 {"est", ZONE, 5 HRS}, /* Eastern */
337 {"e.s.t.", ZONE, 5 HRS},
338 {"edt", DAYZONE, 5 HRS},
339 {"e.d.t.", DAYZONE, 5 HRS},
340 {"cst", ZONE, 6 HRS}, /* Central */
341 {"c.s.t.", ZONE, 6 HRS},
342 {"cdt", DAYZONE, 6 HRS},
343 {"c.d.t.", DAYZONE, 6 HRS},
344 {"mst", ZONE, 7 HRS}, /* Mountain */
345 {"m.s.t.", ZONE, 7 HRS},
346 {"mdt", DAYZONE, 7 HRS},
347 {"m.d.t.", DAYZONE, 7 HRS},
348 {"pst", ZONE, 8 HRS}, /* Pacific */
349 {"p.s.t.", ZONE, 8 HRS},
350 {"pdt", DAYZONE, 8 HRS},
351 {"p.d.t.", DAYZONE, 8 HRS},
352 {"yst", ZONE, 9 HRS}, /* Yukon */
353 {"y.s.t.", ZONE, 9 HRS},
354 {"ydt", DAYZONE, 9 HRS},
355 {"y.d.t.", DAYZONE, 9 HRS},
356 {"hst", ZONE, 10 HRS}, /* Hawaii */
357 {"h.s.t.", ZONE, 10 HRS},
358 {"hdt", DAYZONE, 10 HRS},
359 {"h.d.t.", DAYZONE, 10 HRS},
361 {"gmt", ZONE, 0 HRS},
362 {"g.m.t.", ZONE, 0 HRS},
363 {"bst", DAYZONE, 0 HRS}, /* British Summer Time */
364 {"b.s.t.", DAYZONE, 0 HRS},
365 {"eet", ZONE, 0 HRS}, /* European Eastern Time */
366 {"e.e.t.", ZONE, 0 HRS},
367 {"eest", DAYZONE, 0 HRS}, /* European Eastern Summer Time */
368 {"e.e.s.t.", DAYZONE, 0 HRS},
369 {"met", ZONE, -1 HRS}, /* Middle European Time */
370 {"m.e.t.", ZONE, -1 HRS},
371 {"mest", DAYZONE, -1 HRS}, /* Middle European Summer Time */
372 {"m.e.s.t.", DAYZONE, -1 HRS},
373 {"wet", ZONE, -2 HRS }, /* Western European Time */
374 {"w.e.t.", ZONE, -2 HRS },
375 {"west", DAYZONE, -2 HRS}, /* Western European Summer Time */
376 {"w.e.s.t.", DAYZONE, -2 HRS},
378 {"jst", ZONE, -9 HRS}, /* Japan Standard Time */
379 {"j.s.t.", ZONE, -9 HRS}, /* Japan Standard Time */
380 /* No daylight savings time */
382 {"aest", ZONE, -10 HRS}, /* Australian Eastern Time */
383 {"a.e.s.t.", ZONE, -10 HRS},
384 {"aesst", DAYZONE, -10 HRS}, /* Australian Eastern Summer Time */
385 {"a.e.s.s.t.", DAYZONE, -10 HRS},
386 {"acst", ZONE, -(9 HRS + HALFHR)}, /* Australian Central Time */
387 {"a.c.s.t.", ZONE, -(9 HRS + HALFHR)},
388 {"acsst", DAYZONE, -(9 HRS + HALFHR)}, /* Australian Central Summer */
389 {"a.c.s.s.t.", DAYZONE, -(9 HRS + HALFHR)},
390 {"awst", ZONE, -8 HRS}, /* Australian Western Time */
391 {"a.w.s.t.", ZONE, -8 HRS}, /* (no daylight time there, I'm told */
395 struct table unittb[] = {
398 {"fortnight", UNIT, 14*24*60},
399 {"week", UNIT, 7*24*60},
400 {"day", UNIT, 1*24*60},
404 {"second", SUNIT, 1},
409 struct table othertb[] = {
410 {"tomorrow", UNIT, 1*24*60},
411 {"yesterday", UNIT, -1*24*60},
414 {"last", NUMBER, -1},
417 {"first", NUMBER, 1},
418 /* {"second", NUMBER, 2}, */
419 {"third", NUMBER, 3},
420 {"fourth", NUMBER, 4},
421 {"fifth", NUMBER, 5},
422 {"sixth", NUMBER, 6},
423 {"seventh", NUMBER, 7},
424 {"eigth", NUMBER, 8},
425 {"ninth", NUMBER, 9},
426 {"tenth", NUMBER, 10},
427 {"eleventh", NUMBER, 11},
428 {"twelfth", NUMBER, 12},
433 struct table milzone[] = {
455 {"w", ZONE, -10 HRS},
456 {"x", ZONE, -11 HRS},
457 {"y", ZONE, -12 HRS},
464 #define gotit (yylval=i->value, i->type)
465 #define getid for(j=idvar, k=id; *j++ = *k++; )
468 register char *j, *k;
469 register struct table *i;
473 if (strlen(idvar) == 3) abbrev = 1;
474 else if (strlen(idvar) == 4 && idvar[3] == '.') {
480 if (islower(*idvar)) *idvar = toupper(*idvar);
482 for (i = mdtab; i->name; i++) {
484 for (j = i->name; *j++ == *k++;) {
485 if (abbrev && j==i->name+3) return gotit;
486 if (j[-1] == 0) return gotit;
491 for (i = mztab; i->name; i++)
492 if (strcmp(i->name, idvar) == 0) return gotit;
494 for (j = idvar; *j; j++)
495 if (isupper(*j)) *j = tolower(*j);
496 for (i=mztab; i->name; i++)
497 if (strcmp(i->name, idvar) == 0) return gotit;
500 for (i=unittb; i->name; i++)
501 if (strcmp(i->name, idvar) == 0) return gotit;
503 if (idvar[strlen(idvar)-1] == 's')
504 idvar[strlen(idvar)-1] = '\0';
505 for (i=unittb; i->name; i++)
506 if (strcmp(i->name, idvar) == 0) return gotit;
509 for (i = othertb; i->name; i++)
510 if (strcmp(i->name, idvar) == 0) return gotit;
513 if (strlen(idvar) == 1 && isalpha(*idvar)) {
514 if (isupper(*idvar)) *idvar = tolower(*idvar);
515 for (i = milzone; i->name; i++)
516 if (strcmp(i->name, idvar) == 0) return gotit;
523 Tcl_GetDate (p, now, zone)
528 #define mcheck(f) if (f>1) err++
537 lt = localtime(&now);
539 month = lt->tm_mon+1;
541 relsec = 0; relmonth = 0;
542 timeflag=zoneflag=dateflag=dayflag=relflag=0;
548 if (err = yyparse()) return (-1);
555 if (err) return (-1);
556 if (dateflag || timeflag || dayflag) {
557 sdate = dateconv(month,day,year,hh,mm,ss,merid,ourzone,
559 if (sdate < 0) return -1;
564 sdate -= (lt->tm_sec + lt->tm_min*60 +
565 lt->tm_hour*(60L*60L));
569 sdate += monthadd(sdate, relmonth);
571 if (dayflag && !dateflag) {
572 tod = dayconv(dayord, dayreq, sdate);
580 * Error message are not used, so discard with dummy function.