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