OpenBSD fixes for dtcm. There are no global "timezone" and "tzname" symbols on BSD...
[oweals/cde.git] / cde / programs / dtcm / libDtCmP / getdate.y
1 /*******************************************************************************
2 **
3 **  getdate.y
4 **
5 **  $XConsortium: getdate.y /main/3 1995/11/03 10:38:01 rswiston $
6 **
7 **  RESTRICTED CONFIDENTIAL INFORMATION:
8 **
9 **  The information in this document is subject to special
10 **  restrictions in a confidential disclosure agreement between
11 **  HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
12 **  document outside HP, IBM, Sun, USL, SCO, or Univel without
13 **  Sun's specific written approval.  This document and all copies
14 **  and derivative works thereof must be returned or destroyed at
15 **  Sun's request.
16 **
17 **  Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
18 **
19 *******************************************************************************/
20
21 /*                                                                      *
22  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
23  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
24  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
25  * (c) Copyright 1993, 1994 Novell, Inc.                                *
26  */
27
28
29 %{
30 #ifndef lint
31 static  char sccsid[] = "@(#)getdate.y 1.10 94/11/07 Copyr 1993 Sun Microsystems, Inc.";
32 #endif
33 %}
34
35 %token ID MONTH DAY MERIDIAN NUMBER UNIT MUNIT SUNIT ZONE DAYZONE AGO
36 %{
37         /*      Steven M. Bellovin (unc!smb)                    */
38         /*      Dept. of Computer Science                       */
39         /*      University of North Carolina at Chapel Hill     */
40         /*      @(#)getdate.y   2.6     4/20/84 */
41
42 #include <EUSCompat.h>
43 #include <ctype.h>
44 #include <time.h>
45 #include "getdate.h"
46 #ifdef SVR4
47 #include <sys/time.h>
48 #endif /* SVR4 */
49
50 #ifndef NULL
51 #define NULL    0
52 #endif
53
54 #define daysec (24L*60L*60L)
55         static int timeflag, zoneflag, dateflag, dayflag, relflag;
56         static time_t relsec, relmonth;
57         static int hh, mm, ss, merid, daylightsavings;
58         static int dayord, dayreq;
59         static int month, day, year;
60         static int noyear;
61         static int ourzone;
62 #define AM 1
63 #define PM 2
64 #define DAYLIGHT 1
65 #define STANDARD 2
66 #define MAYBE    3
67
68 #ifdef SVR4
69 extern long timezone;
70 #endif
71
72 %}
73
74 %%
75 timedate:               /* empty */
76         | timedate item;
77
78 item:   tspec
79                 {timeflag++;}
80         | zone
81                 {zoneflag++;}
82         | dtspec
83                 {dateflag++;}
84         | dyspec
85                 {dayflag++;}
86         | rspec
87                 {relflag++;}
88         | nspec;
89
90 nspec:  NUMBER
91                 {if (timeflag && dateflag && !relflag) year = $1;
92                 else {timeflag++;hh = $1/100;mm = $1%100;ss = 0;merid = 24;}};
93
94 tspec:  NUMBER MERIDIAN
95                 {hh = $1; mm = 0; ss = 0; merid = $2;}
96         | NUMBER ':' NUMBER
97                 {hh = $1; mm = $3; merid = 24;}
98         | NUMBER ':' NUMBER MERIDIAN
99                 {hh = $1; mm = $3; merid = $4;}
100         | NUMBER ':' NUMBER ':' NUMBER
101                 {hh = $1; mm = $3; ss = $5; merid = 24;}
102         | NUMBER ':' NUMBER ':' NUMBER MERIDIAN
103                 {hh = $1; mm = $3; ss = $5; merid = $6;};
104
105 zone:   ZONE
106                 {ourzone = $1; daylightsavings = STANDARD;}
107         | DAYZONE
108                 {ourzone = $1; daylightsavings = DAYLIGHT;};
109
110 dyspec: DAY
111                 {dayord = 1; dayreq = $1;}
112         | DAY ','
113                 {dayord = 1; dayreq = $1;}
114         | NUMBER DAY
115                 {dayord = $1; dayreq = $2;};
116
117 dtspec: NUMBER '/' NUMBER
118                 {month = $1; day = $3; noyear = 1;}
119         | NUMBER '/' NUMBER '/' NUMBER
120                 {month = $1; day = $3; year = $5;}
121         | NUMBER '-' NUMBER
122                 {month = $1; day = $3; noyear = 1;}
123         | NUMBER '-' NUMBER '-' NUMBER
124                 {month = $1; day = $3; year = $5;}
125         | MONTH NUMBER
126                 {month = $1; day = $2; noyear = 1;}
127         | MONTH NUMBER ',' NUMBER
128                 {month = $1; day = $2; year = $4;}
129         | NUMBER MONTH
130                 {month = $2; day = $1; noyear = 1;}
131         | NUMBER MONTH NUMBER
132                 {month = $2; day = $1; year = $3;};
133
134
135 rspec:  NUMBER UNIT
136                 {relsec +=  60L * $1 * $2;}
137         | NUMBER MUNIT
138                 {relmonth += $1 * $2;}
139         | NUMBER SUNIT
140                 {relsec += $1;}
141         | UNIT
142                 {relsec +=  60L * $1;}
143         | MUNIT
144                 {relmonth += $1;}
145         | SUNIT
146                 {relsec++;}
147         | rspec AGO
148                 {relsec = -relsec; relmonth = -relmonth;};
149 %%
150
151 static int mdays[12] =
152         {31, 0, 31,  30, 31, 30,  31, 31, 30,  31, 30, 31};
153 #define epoch BOT_YEAR
154
155 extern struct tm *localtime();
156 time_t dateconv(int mm, 
157     int dd, 
158     int yy, 
159     int h, 
160     int m, 
161     int s, 
162     int mer, 
163     int zone, 
164     int dayflag)
165 {
166         time_t tod, jdate;
167         register int i;
168
169         if (yy < 0) yy = -yy;
170
171         if (yy < 70)
172                 yy += 2000;
173         else if (yy < 100)
174                 yy += 1900;
175
176         mdays[1] = 28 + (yy%4 == 0);
177
178         if (yy < epoch)
179                 return(DATE_BBOT);
180
181         if (yy > EOT_YEAR)
182                 return(DATE_AEOT);
183
184         if (mm < 1 || mm > 12)
185                 return(DATE_BMONTH);
186
187         if (dd < 1 || dd > mdays[--mm])
188                 return(DATE_BDAY);
189
190         jdate = dd-1;
191         for (i=0; i<mm; i++) jdate += mdays[i];
192         for (i = epoch; i < yy; i++) jdate += 365 + (i%4 == 0);
193         jdate *= daysec;
194         jdate += zone * 60L;
195         if ((tod = timeconv(h, m, s, mer)) < 0) return (tod);
196         jdate += tod;
197         if (dayflag==DAYLIGHT || (dayflag==MAYBE&&localtime(&jdate)->tm_isdst))
198                 jdate += -1*60*60;
199         return (jdate);
200 }
201
202 time_t dayconv(int ord, int day, time_t now)
203 {
204         register struct tm *loctime;
205         time_t tod;
206
207         tod = now;
208         loctime = localtime(&tod);
209         tod += daysec * ((day - loctime->tm_wday + 7) % 7);
210         tod += 7*daysec*(ord<=0?ord:ord-1);
211         return daylcorr(tod, now);
212 }
213
214 time_t timeconv(register int hh, int mm, int ss, int mer)
215 {
216         if (mm < 0 || mm > 59 || ss < 0 || ss > 59) return (DATE_BMIN);
217         switch (mer) {
218                 case AM: if (hh < 1 || hh > 12) return(DATE_BHOUR);
219                          return (60L * ((hh%12)*60L + mm)+ss);
220                 case PM: if (hh < 1 || hh > 12) return(DATE_BHOUR);
221                          return (60L * ((hh%12 +12)*60L + mm)+ss);
222                 case 24: if (hh < 0 || hh > 23) return (DATE_BHOUR);
223                          return (60L * (hh*60L + mm)+ss);
224                 default: return (DATE_CONV);
225         }
226 }
227 time_t monthadd(time_t sdate, time_t relmonth)
228 {
229         struct tm *ltime;
230         int mm, yy;
231
232         if (relmonth == 0) return 0;
233         ltime = localtime(&sdate);
234         mm = 12*ltime->tm_year + ltime->tm_mon + relmonth;
235         yy = mm/12;
236         mm = mm%12 + 1;
237         return daylcorr(dateconv(mm, ltime->tm_mday, yy, ltime->tm_hour,
238                 ltime->tm_min, ltime->tm_sec, 24, ourzone, MAYBE), sdate);
239 }
240
241 time_t daylcorr(time_t future, time_t now)
242 {
243         int fdayl, nowdayl;
244
245         nowdayl = (localtime(&now)->tm_hour+1) % 24;
246         fdayl = (localtime(&future)->tm_hour+1) % 24;
247         return (future-now) + 60L*60L*(nowdayl-fdayl);
248 }
249
250 struct table {
251         char *name;
252         int type, value;
253 };
254
255 struct table mdtab[] = {
256         {"January", MONTH, 1},
257         {"February", MONTH, 2},
258         {"March", MONTH, 3},
259         {"April", MONTH, 4},
260         {"May", MONTH, 5},
261         {"June", MONTH, 6},
262         {"July", MONTH, 7},
263         {"August", MONTH, 8},
264         {"September", MONTH, 9},
265         {"Sept", MONTH, 9},
266         {"October", MONTH, 10},
267         {"November", MONTH, 11},
268         {"December", MONTH, 12},
269
270         {"Sunday", DAY, 0},
271         {"Monday", DAY, 1},
272         {"Tuesday", DAY, 2},
273         {"Tues", DAY, 2},
274         {"Wednesday", DAY, 3},
275         {"Wednes", DAY, 3},
276         {"Thursday", DAY, 4},
277         {"Thur", DAY, 4},
278         {"Thurs", DAY, 4},
279         {"Friday", DAY, 5},
280         {"Saturday", DAY, 6},
281         {0, 0, 0}};
282
283 #define HRS *60
284 #define HALFHR 30
285 struct table mztab[] = {
286         {"a.m.", MERIDIAN, AM},
287         {"am", MERIDIAN, AM},
288         {"p.m.", MERIDIAN, PM},
289         {"pm", MERIDIAN, PM},
290         {"nst", ZONE, 3 HRS + HALFHR},          /* Newfoundland */
291         {"n.s.t.", ZONE, 3 HRS + HALFHR},
292         {"ast", ZONE, 4 HRS},           /* Atlantic */
293         {"a.s.t.", ZONE, 4 HRS},
294         {"adt", DAYZONE, 4 HRS},
295         {"a.d.t.", DAYZONE, 4 HRS},
296         {"est", ZONE, 5 HRS},           /* Eastern */
297         {"e.s.t.", ZONE, 5 HRS},
298         {"edt", DAYZONE, 5 HRS},
299         {"e.d.t.", DAYZONE, 5 HRS},
300         {"cst", ZONE, 6 HRS},           /* Central */
301         {"c.s.t.", ZONE, 6 HRS},
302         {"cdt", DAYZONE, 6 HRS},
303         {"c.d.t.", DAYZONE, 6 HRS},
304         {"mst", ZONE, 7 HRS},           /* Mountain */
305         {"m.s.t.", ZONE, 7 HRS},
306         {"mdt", DAYZONE, 7 HRS},
307         {"m.d.t.", DAYZONE, 7 HRS},
308         {"pst", ZONE, 8 HRS},           /* Pacific */
309         {"p.s.t.", ZONE, 8 HRS},
310         {"pdt", DAYZONE, 8 HRS},
311         {"p.d.t.", DAYZONE, 8 HRS},
312         {"yst", ZONE, 9 HRS},           /* Yukon */
313         {"y.s.t.", ZONE, 9 HRS},
314         {"ydt", DAYZONE, 9 HRS},
315         {"y.d.t.", DAYZONE, 9 HRS},
316         {"hst", ZONE, 10 HRS},          /* Hawaii */
317         {"h.s.t.", ZONE, 10 HRS},
318         {"hdt", DAYZONE, 10 HRS},
319         {"h.d.t.", DAYZONE, 10 HRS},
320         {"bst", ZONE, 11 HRS},          /* Bering */
321         {"b.s.t.", ZONE, 11 HRS},
322         {"bdt", DAYZONE, 11 HRS},
323         {"b.d.t.", DAYZONE, 11 HRS},
324
325         {"gmt", ZONE, 0 HRS},
326         {"g.m.t.", ZONE, 0 HRS},
327
328         {"aest", ZONE, -10 HRS},        /* Australian Eastern Time */
329         {"a.e.s.t.", ZONE, -10 HRS},
330         {"aesst", DAYZONE, -10 HRS},    /* Australian Eastern Summer Time */
331         {"a.e.s.s.t.", DAYZONE, -10 HRS},
332         {"acst", ZONE, -(9 HRS + HALFHR)},      /* Australian Central Time */
333         {"a.c.s.t.", ZONE, -(9 HRS + HALFHR)},
334         {"acsst", DAYZONE, -(9 HRS + HALFHR)},  /* Australian Central Summer */
335         {"a.c.s.s.t.", DAYZONE, -(9 HRS + HALFHR)},
336         {"awst", ZONE, -8 HRS},         /* Australian Western Time */
337         {"a.w.s.t.", ZONE, -8 HRS},     /* (no daylightsavings time there, I'm told */
338         {0, 0, 0}};
339
340 struct table unittb[] = {
341         {"year", MUNIT, 12},
342         {"month", MUNIT, 1},
343         {"fortnight", UNIT, 14*24*60},
344         {"week", UNIT, 7*24*60},
345         {"day", UNIT, 1*24*60},
346         {"hour", UNIT, 60},
347         {"minute", UNIT, 1},
348         {"min", UNIT, 1},
349         {"second", SUNIT, 1},
350         {"sec", SUNIT, 1},
351         {0, 0, 0}};
352
353 struct table othertb[] = {
354         {"tomorrow", UNIT, 1*24*60},
355         {"yesterday", UNIT, -1*24*60},
356         {"today", UNIT, 0},
357         {"now", UNIT, 0},
358         {"last", NUMBER, -1},
359         {"this", UNIT, 0},
360         {"next", NUMBER, 2},
361         {"first", NUMBER, 1},
362         /* {"second", NUMBER, 2}, */
363         {"third", NUMBER, 3},
364         {"fourth", NUMBER, 4},
365         {"fifth", NUMBER, 5},
366         {"sixth", NUMBER, 6},
367         {"seventh", NUMBER, 7},
368         {"eighth", NUMBER, 8},
369         {"ninth", NUMBER, 9},
370         {"tenth", NUMBER, 10},
371         {"eleventh", NUMBER, 11},
372         {"twelfth", NUMBER, 12},
373         {"ago", AGO, 1},
374         {0, 0, 0}};
375
376 struct table milzone[] = {
377         {"a", ZONE, 1 HRS},
378         {"b", ZONE, 2 HRS},
379         {"c", ZONE, 3 HRS},
380         {"d", ZONE, 4 HRS},
381         {"e", ZONE, 5 HRS},
382         {"f", ZONE, 6 HRS},
383         {"g", ZONE, 7 HRS},
384         {"h", ZONE, 8 HRS},
385         {"i", ZONE, 9 HRS},
386         {"k", ZONE, 10 HRS},
387         {"l", ZONE, 11 HRS},
388         {"m", ZONE, 12 HRS},
389         {"n", ZONE, -1 HRS},
390         {"o", ZONE, -2 HRS},
391         {"p", ZONE, -3 HRS},
392         {"q", ZONE, -4 HRS},
393         {"r", ZONE, -5 HRS},
394         {"s", ZONE, -6 HRS},
395         {"t", ZONE, -7 HRS},
396         {"u", ZONE, -8 HRS},
397         {"v", ZONE, -9 HRS},
398         {"w", ZONE, -10 HRS},
399         {"x", ZONE, -11 HRS},
400         {"y", ZONE, -12 HRS},
401         {"z", ZONE, 0 HRS},
402         {0, 0, 0}};
403
404 static int
405 lookup(char *id)
406 {
407 #define gotit (yylval=i->value,  i->type)
408 #define getid for(j=idvar, k=id; *j++ = *k++; )
409
410         char idvar[20];
411         register char *j, *k;
412         register struct table *i;
413         int abbrev;
414
415         getid;
416         if (strlen(idvar) == 3) abbrev = 1;
417         else if (strlen(idvar) == 4 && idvar[3] == '.') {
418                 abbrev = 1;
419                 idvar[3] = '\0';
420         }
421         else abbrev = 0;
422
423         if (islower(*idvar)) *idvar = toupper(*idvar);
424
425         for (i = mdtab; i->name; i++) {
426                 k = idvar;
427                 for (j = i->name; *j++ == *k++;) {
428                         if (abbrev && j==i->name+3) return gotit;
429                         if (j[-1] == 0) return gotit;
430                 }
431         }
432
433         getid;
434         for (i = mztab; i->name; i++)
435                 if (strcmp(i->name, idvar) == 0) return gotit;
436
437         for (j = idvar; *j; j++) if (isupper(*j)) *j = tolower(*j);
438         for (i=mztab; i->name; i++)
439                 if (strcmp(i->name, idvar) == 0) return gotit;
440
441         getid;
442         for (i=unittb; i->name; i++)
443                 if (strcmp(i->name, idvar) == 0) return gotit;
444
445         if (idvar[strlen(idvar)-1] == 's') idvar[strlen(idvar)-1] = '\0';
446         for (i=unittb; i->name; i++)
447                 if (strcmp(i->name, idvar) == 0) return gotit;
448
449         getid;
450         for (i = othertb; i->name; i++)
451                 if (strcmp(i->name, idvar) == 0) return gotit;
452
453         getid;
454         if (strlen(idvar) == 1 && isalpha(*idvar)) {
455                 if (isupper(*idvar)) *idvar = tolower(*idvar);
456                 for (i = milzone; i->name; i++)
457                         if (strcmp(i->name, idvar) == 0) return gotit;
458         }
459
460         return(ID);
461 }
462
463 static char *lptr;
464
465 yylex()
466 {
467 #if defined(__osf__)
468         extern long yylval;
469 #else /* __osf__ */
470         extern int yylval;
471 #endif /* __osf__ */
472         int sign;
473         register char c;
474         register char *p;
475         char idbuf[20];
476         int pcnt;
477
478         for (;;) {
479                 while (isspace(*lptr)) lptr++;
480
481                 if (isdigit(c = *lptr) || c == '-' || c == '+') {
482                         if (c== '-' || c == '+') {
483                                 if (c=='-') sign = -1;
484                                 else sign = 1;
485                                 if (!isdigit(*++lptr)) {
486                                         /* yylval = sign; return (NUMBER); */
487                                         return yylex(); /* skip the '-' sign */
488                                 }
489                         } else sign = 1;
490                         yylval = 0;
491                         while (isdigit(c = *lptr++)) yylval = 10*yylval + c - '0';
492                         yylval *= sign;
493                         lptr--;
494                         return (NUMBER);
495
496                 } else if (isalpha(c)) {
497                         p = idbuf;
498                         while (isalpha(c = *lptr++) || c=='.')
499                                 *p++ = c;
500                         *p = '\0';
501                         lptr--;
502                         return (lookup(idbuf));
503                 }
504
505                 else if (c == '(') {
506                         pcnt = 0;
507                         do {
508                                 c = *lptr++;
509                                 if (c == '\0') return(c);
510                                 else if (c == '(') pcnt++;
511                                 else if (c == ')') pcnt--;
512                         } while (pcnt > 0);
513                 }
514
515                 else return (*lptr++);
516         }
517 }
518
519
520 time_t cm_getdate(char *p, struct timeb *now)
521 {
522 #define mcheck(f)       if (f>1) err++
523         int err;
524         struct tm *lt=NULL;
525         time_t sdate, tod;
526         struct timeb ftz;
527
528         lptr = p;
529         if (now == ((struct timeb *) NULL)) {
530                 now = &ftz;
531 #ifdef SVR4
532                 tod = time(0);
533                 lt = localtime(&tod);
534                 now->time = lt->tm_sec;
535                 now->timezone = timezone/60;
536 #else
537                 ftime(&ftz);
538 #endif /* SVR4 */
539         }
540         if (lt == NULL)
541                 lt = localtime(&now->time);
542         year = lt->tm_year;
543         month = lt->tm_mon+1;
544         day = lt->tm_mday;
545         relsec = 0; relmonth = 0;
546         timeflag=zoneflag=dateflag=dayflag=relflag=0;
547         ourzone = now->timezone;
548         daylightsavings = MAYBE;
549         hh = mm = ss = 0;
550         merid = 24;
551
552         if (err = yyparse()) return (-1);
553
554         mcheck(timeflag);
555         mcheck(zoneflag);
556         mcheck(dateflag);
557         mcheck(dayflag);
558
559         if (err) return (-1);
560
561         /* Relflag also needs to set the sdate variable */
562         /* This fixes QAR 28447 */
563         if (dateflag || timeflag || dayflag || relflag) {
564                 sdate = dateconv(month, day, year, hh, mm, ss, merid, ourzone,
565                     daylightsavings);
566                 if (sdate < 0) return(sdate);
567                 /*
568                   if more than 6 months ago, then probably means
569                   next year
570                  
571                 if (noyear && sdate < now->time - daysec*(365/2)) {
572                                 year++;
573                                 sdate = dateconv(month, day, year, hh, mm, ss,
574                                     merid, ourzone, daylightsavings);
575                 }  */
576         }
577         else {
578                 sdate = now->time;
579                 if (relflag == 0)
580                         sdate -= (lt->tm_sec + lt->tm_min*60 +
581                                 lt->tm_hour*(60L*60L));
582         }
583
584         sdate += relsec;
585         sdate += monthadd(sdate, relmonth);
586
587         if (dayflag && !dateflag) {
588                 tod = dayconv(dayord, dayreq, sdate);
589                 sdate += tod;
590         }
591
592         return sdate;
593 }
594
595 yyerror(s) char *s;
596 {}