Annotation of src/usr.bin/rcs/date.y, Revision 1.2
1.1 joris 1: %{
1.2 ! ray 2: /* $OpenBSD: date.y,v 1.1 2006/04/26 02:55:13 joris Exp $ */
1.1 joris 3:
4: /*
5: ** Originally written by Steven M. Bellovin <smb@research.att.com> while
6: ** at the University of North Carolina at Chapel Hill. Later tweaked by
7: ** a couple of people on Usenet. Completely overhauled by Rich $alz
8: ** <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
9: **
10: ** This grammar has 10 shift/reduce conflicts.
11: **
12: ** This code is in the public domain and has no copyright.
13: */
14: /* SUPPRESS 287 on yaccpar_sccsid *//* Unused static variable */
15: /* SUPPRESS 288 on yyerrlab *//* Label unused */
16:
17: #include "includes.h"
18:
19: #include "rcsprog.h"
20:
21: #define YEAR_EPOCH 1970
22: #define YEAR_TMORIGIN 1900
23: #define HOUR(x) ((time_t)(x) * 60)
24: #define SECSPERDAY (24L * 60L * 60L)
25:
26:
27: /* An entry in the lexical lookup table */
28: typedef struct _TABLE {
29: char *name;
30: int type;
31: time_t value;
32: } TABLE;
33:
34:
35: /* Daylight-savings mode: on, off, or not yet known. */
36: typedef enum _DSTMODE {
37: DSTon, DSToff, DSTmaybe
38: } DSTMODE;
39:
40: /* Meridian: am, pm, or 24-hour style. */
41: typedef enum _MERIDIAN {
42: MERam, MERpm, MER24
43: } MERIDIAN;
44:
45:
46: /*
47: * Global variables. We could get rid of most of these by using a good
48: * union as the yacc stack. (This routine was originally written before
49: * yacc had the %union construct.) Maybe someday; right now we only use
50: * the %union very rarely.
51: */
52: static const char *yyInput;
53: static DSTMODE yyDSTmode;
54: static time_t yyDayOrdinal;
55: static time_t yyDayNumber;
56: static int yyHaveDate;
57: static int yyHaveDay;
58: static int yyHaveRel;
59: static int yyHaveTime;
60: static int yyHaveZone;
61: static time_t yyTimezone;
62: static time_t yyDay;
63: static time_t yyHour;
64: static time_t yyMinutes;
65: static time_t yyMonth;
66: static time_t yySeconds;
67: static time_t yyYear;
68: static MERIDIAN yyMeridian;
69: static time_t yyRelMonth;
70: static time_t yyRelSeconds;
71:
72:
73: static int yyerror (const char *);
74: static int yylex (void);
75: static int yyparse (void);
76: static int lookup (char *);
77:
78: %}
79:
80: %union {
81: time_t Number;
82: enum _MERIDIAN Meridian;
83: }
84:
85: %token tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
86: %token tSEC_UNIT tSNUMBER tUNUMBER tZONE tDST
87:
88: %type <Number> tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
89: %type <Number> tSEC_UNIT tSNUMBER tUNUMBER tZONE
90: %type <Meridian> tMERIDIAN o_merid
91:
92: %%
93:
94: spec : /* NULL */
95: | spec item
96: ;
97:
98: item : time {
99: yyHaveTime++;
100: }
101: | zone {
102: yyHaveZone++;
103: }
104: | date {
105: yyHaveDate++;
106: }
107: | day {
108: yyHaveDay++;
109: }
110: | rel {
111: yyHaveRel++;
112: }
113: | number
114: ;
115:
116: time : tUNUMBER tMERIDIAN {
117: yyHour = $1;
118: yyMinutes = 0;
119: yySeconds = 0;
120: yyMeridian = $2;
121: }
122: | tUNUMBER ':' tUNUMBER o_merid {
123: yyHour = $1;
124: yyMinutes = $3;
125: yySeconds = 0;
126: yyMeridian = $4;
127: }
128: | tUNUMBER ':' tUNUMBER tSNUMBER {
129: yyHour = $1;
130: yyMinutes = $3;
131: yyMeridian = MER24;
132: yyDSTmode = DSToff;
133: yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
134: }
135: | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
136: yyHour = $1;
137: yyMinutes = $3;
138: yySeconds = $5;
139: yyMeridian = $6;
140: }
141: | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
142: yyHour = $1;
143: yyMinutes = $3;
144: yySeconds = $5;
145: yyMeridian = MER24;
146: yyDSTmode = DSToff;
147: yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
148: }
149: ;
150:
151: zone : tZONE {
152: yyTimezone = $1;
153: yyDSTmode = DSToff;
154: }
155: | tDAYZONE {
156: yyTimezone = $1;
157: yyDSTmode = DSTon;
158: }
159: | tZONE tDST {
160: yyTimezone = $1;
161: yyDSTmode = DSTon;
162: }
163: ;
164:
165: day : tDAY {
166: yyDayOrdinal = 1;
167: yyDayNumber = $1;
168: }
169: | tDAY ',' {
170: yyDayOrdinal = 1;
171: yyDayNumber = $1;
172: }
173: | tUNUMBER tDAY {
174: yyDayOrdinal = $1;
175: yyDayNumber = $2;
176: }
177: ;
178:
179: date : tUNUMBER '/' tUNUMBER {
180: yyMonth = $1;
181: yyDay = $3;
182: }
183: | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
184: if ($1 >= 100) {
185: yyYear = $1;
186: yyMonth = $3;
187: yyDay = $5;
188: } else {
189: yyMonth = $1;
190: yyDay = $3;
191: yyYear = $5;
192: }
193: }
194: | tUNUMBER tSNUMBER tSNUMBER {
195: /* ISO 8601 format. yyyy-mm-dd. */
196: yyYear = $1;
197: yyMonth = -$2;
198: yyDay = -$3;
199: }
200: | tUNUMBER tMONTH tSNUMBER {
201: /* e.g. 17-JUN-1992. */
202: yyDay = $1;
203: yyMonth = $2;
204: yyYear = -$3;
205: }
206: | tMONTH tUNUMBER {
207: yyMonth = $1;
208: yyDay = $2;
209: }
210: | tMONTH tUNUMBER ',' tUNUMBER {
211: yyMonth = $1;
212: yyDay = $2;
213: yyYear = $4;
214: }
215: | tUNUMBER tMONTH {
216: yyMonth = $2;
217: yyDay = $1;
218: }
219: | tUNUMBER tMONTH tUNUMBER {
220: yyMonth = $2;
221: yyDay = $1;
222: yyYear = $3;
223: }
224: ;
225:
226: rel : relunit tAGO {
227: yyRelSeconds = -yyRelSeconds;
228: yyRelMonth = -yyRelMonth;
229: }
230: | relunit
231: ;
232:
233: relunit : tUNUMBER tMINUTE_UNIT {
234: yyRelSeconds += $1 * $2 * 60L;
235: }
236: | tSNUMBER tMINUTE_UNIT {
237: yyRelSeconds += $1 * $2 * 60L;
238: }
239: | tMINUTE_UNIT {
240: yyRelSeconds += $1 * 60L;
241: }
242: | tSNUMBER tSEC_UNIT {
243: yyRelSeconds += $1;
244: }
245: | tUNUMBER tSEC_UNIT {
246: yyRelSeconds += $1;
247: }
248: | tSEC_UNIT {
249: yyRelSeconds++;
250: }
251: | tSNUMBER tMONTH_UNIT {
252: yyRelMonth += $1 * $2;
253: }
254: | tUNUMBER tMONTH_UNIT {
255: yyRelMonth += $1 * $2;
256: }
257: | tMONTH_UNIT {
258: yyRelMonth += $1;
259: }
260: ;
261:
262: number : tUNUMBER {
263: if (yyHaveTime && yyHaveDate && !yyHaveRel)
264: yyYear = $1;
265: else {
266: if ($1 > 10000) {
267: yyHaveDate++;
268: yyDay= ($1)%100;
269: yyMonth= ($1/100)%100;
270: yyYear = $1/10000;
271: } else {
272: yyHaveTime++;
273: if ($1 < 100) {
274: yyHour = $1;
275: yyMinutes = 0;
276: } else {
277: yyHour = $1 / 100;
278: yyMinutes = $1 % 100;
279: }
280: yySeconds = 0;
281: yyMeridian = MER24;
282: }
283: }
284: }
285: ;
286:
287: o_merid : /* NULL */ {
288: $$ = MER24;
289: }
290: | tMERIDIAN {
291: $$ = $1;
292: }
293: ;
294:
295: %%
296:
297: /* Month and day table. */
298: static TABLE const MonthDayTable[] = {
299: { "january", tMONTH, 1 },
300: { "february", tMONTH, 2 },
301: { "march", tMONTH, 3 },
302: { "april", tMONTH, 4 },
303: { "may", tMONTH, 5 },
304: { "june", tMONTH, 6 },
305: { "july", tMONTH, 7 },
306: { "august", tMONTH, 8 },
307: { "september", tMONTH, 9 },
308: { "sept", tMONTH, 9 },
309: { "october", tMONTH, 10 },
310: { "november", tMONTH, 11 },
311: { "december", tMONTH, 12 },
312: { "sunday", tDAY, 0 },
313: { "monday", tDAY, 1 },
314: { "tuesday", tDAY, 2 },
315: { "tues", tDAY, 2 },
316: { "wednesday", tDAY, 3 },
317: { "wednes", tDAY, 3 },
318: { "thursday", tDAY, 4 },
319: { "thur", tDAY, 4 },
320: { "thurs", tDAY, 4 },
321: { "friday", tDAY, 5 },
322: { "saturday", tDAY, 6 },
323: { NULL }
324: };
325:
326: /* Time units table. */
327: static TABLE const UnitsTable[] = {
328: { "year", tMONTH_UNIT, 12 },
329: { "month", tMONTH_UNIT, 1 },
330: { "fortnight", tMINUTE_UNIT, 14 * 24 * 60 },
331: { "week", tMINUTE_UNIT, 7 * 24 * 60 },
332: { "day", tMINUTE_UNIT, 1 * 24 * 60 },
333: { "hour", tMINUTE_UNIT, 60 },
334: { "minute", tMINUTE_UNIT, 1 },
335: { "min", tMINUTE_UNIT, 1 },
336: { "second", tSEC_UNIT, 1 },
337: { "sec", tSEC_UNIT, 1 },
338: { NULL }
339: };
340:
341: /* Assorted relative-time words. */
342: static TABLE const OtherTable[] = {
343: { "tomorrow", tMINUTE_UNIT, 1 * 24 * 60 },
344: { "yesterday", tMINUTE_UNIT, -1 * 24 * 60 },
345: { "today", tMINUTE_UNIT, 0 },
346: { "now", tMINUTE_UNIT, 0 },
347: { "last", tUNUMBER, -1 },
348: { "this", tMINUTE_UNIT, 0 },
349: { "next", tUNUMBER, 2 },
350: { "first", tUNUMBER, 1 },
351: /* { "second", tUNUMBER, 2 }, */
352: { "third", tUNUMBER, 3 },
353: { "fourth", tUNUMBER, 4 },
354: { "fifth", tUNUMBER, 5 },
355: { "sixth", tUNUMBER, 6 },
356: { "seventh", tUNUMBER, 7 },
357: { "eighth", tUNUMBER, 8 },
358: { "ninth", tUNUMBER, 9 },
359: { "tenth", tUNUMBER, 10 },
360: { "eleventh", tUNUMBER, 11 },
361: { "twelfth", tUNUMBER, 12 },
362: { "ago", tAGO, 1 },
363: { NULL }
364: };
365:
366: /* The timezone table. */
367: /* Some of these are commented out because a time_t can't store a float. */
368: static TABLE const TimezoneTable[] = {
369: { "gmt", tZONE, HOUR( 0) }, /* Greenwich Mean */
370: { "ut", tZONE, HOUR( 0) }, /* Universal (Coordinated) */
371: { "utc", tZONE, HOUR( 0) },
372: { "wet", tZONE, HOUR( 0) }, /* Western European */
373: { "bst", tDAYZONE, HOUR( 0) }, /* British Summer */
374: { "wat", tZONE, HOUR( 1) }, /* West Africa */
375: { "at", tZONE, HOUR( 2) }, /* Azores */
376: #if 0
377: /* For completeness. BST is also British Summer, and GST is
378: * also Guam Standard. */
379: { "bst", tZONE, HOUR( 3) }, /* Brazil Standard */
380: { "gst", tZONE, HOUR( 3) }, /* Greenland Standard */
381: #endif
382: #if 0
383: { "nft", tZONE, HOUR(3.5) }, /* Newfoundland */
384: { "nst", tZONE, HOUR(3.5) }, /* Newfoundland Standard */
385: { "ndt", tDAYZONE, HOUR(3.5) }, /* Newfoundland Daylight */
386: #endif
387: { "ast", tZONE, HOUR( 4) }, /* Atlantic Standard */
388: { "adt", tDAYZONE, HOUR( 4) }, /* Atlantic Daylight */
389: { "est", tZONE, HOUR( 5) }, /* Eastern Standard */
390: { "edt", tDAYZONE, HOUR( 5) }, /* Eastern Daylight */
391: { "cst", tZONE, HOUR( 6) }, /* Central Standard */
392: { "cdt", tDAYZONE, HOUR( 6) }, /* Central Daylight */
393: { "mst", tZONE, HOUR( 7) }, /* Mountain Standard */
394: { "mdt", tDAYZONE, HOUR( 7) }, /* Mountain Daylight */
395: { "pst", tZONE, HOUR( 8) }, /* Pacific Standard */
396: { "pdt", tDAYZONE, HOUR( 8) }, /* Pacific Daylight */
397: { "yst", tZONE, HOUR( 9) }, /* Yukon Standard */
398: { "ydt", tDAYZONE, HOUR( 9) }, /* Yukon Daylight */
399: { "hst", tZONE, HOUR(10) }, /* Hawaii Standard */
400: { "hdt", tDAYZONE, HOUR(10) }, /* Hawaii Daylight */
401: { "cat", tZONE, HOUR(10) }, /* Central Alaska */
402: { "ahst", tZONE, HOUR(10) }, /* Alaska-Hawaii Standard */
403: { "nt", tZONE, HOUR(11) }, /* Nome */
404: { "idlw", tZONE, HOUR(12) }, /* International Date Line West */
405: { "cet", tZONE, -HOUR(1) }, /* Central European */
406: { "met", tZONE, -HOUR(1) }, /* Middle European */
407: { "mewt", tZONE, -HOUR(1) }, /* Middle European Winter */
408: { "mest", tDAYZONE, -HOUR(1) }, /* Middle European Summer */
409: { "swt", tZONE, -HOUR(1) }, /* Swedish Winter */
410: { "sst", tDAYZONE, -HOUR(1) }, /* Swedish Summer */
411: { "fwt", tZONE, -HOUR(1) }, /* French Winter */
412: { "fst", tDAYZONE, -HOUR(1) }, /* French Summer */
413: { "eet", tZONE, -HOUR(2) }, /* Eastern Europe, USSR Zone 1 */
414: { "bt", tZONE, -HOUR(3) }, /* Baghdad, USSR Zone 2 */
415: #if 0
416: { "it", tZONE, -HOUR(3.5) },/* Iran */
417: #endif
418: { "zp4", tZONE, -HOUR(4) }, /* USSR Zone 3 */
419: { "zp5", tZONE, -HOUR(5) }, /* USSR Zone 4 */
420: #if 0
421: { "ist", tZONE, -HOUR(5.5) },/* Indian Standard */
422: #endif
423: { "zp6", tZONE, -HOUR(6) }, /* USSR Zone 5 */
424: #if 0
425: /* For completeness. NST is also Newfoundland Stanard, and SST is
426: * also Swedish Summer. */
427: { "nst", tZONE, -HOUR(6.5) },/* North Sumatra */
428: { "sst", tZONE, -HOUR(7) }, /* South Sumatra, USSR Zone 6 */
429: #endif /* 0 */
430: { "wast", tZONE, -HOUR(7) }, /* West Australian Standard */
431: { "wadt", tDAYZONE, -HOUR(7) }, /* West Australian Daylight */
432: #if 0
433: { "jt", tZONE, -HOUR(7.5) },/* Java (3pm in Cronusland!) */
434: #endif
435: { "cct", tZONE, -HOUR(8) }, /* China Coast, USSR Zone 7 */
436: { "jst", tZONE, -HOUR(9) }, /* Japan Standard, USSR Zone 8 */
437: #if 0
438: { "cast", tZONE, -HOUR(9.5) },/* Central Australian Standard */
439: { "cadt", tDAYZONE, -HOUR(9.5) },/* Central Australian Daylight */
440: #endif
441: { "east", tZONE, -HOUR(10) }, /* Eastern Australian Standard */
442: { "eadt", tDAYZONE, -HOUR(10) }, /* Eastern Australian Daylight */
443: { "gst", tZONE, -HOUR(10) }, /* Guam Standard, USSR Zone 9 */
444: { "nzt", tZONE, -HOUR(12) }, /* New Zealand */
445: { "nzst", tZONE, -HOUR(12) }, /* New Zealand Standard */
446: { "nzdt", tDAYZONE, -HOUR(12) }, /* New Zealand Daylight */
447: { "idle", tZONE, -HOUR(12) }, /* International Date Line East */
448: { NULL }
449: };
450:
451: /* Military timezone table. */
452: static TABLE const MilitaryTable[] = {
453: { "a", tZONE, HOUR( 1) },
454: { "b", tZONE, HOUR( 2) },
455: { "c", tZONE, HOUR( 3) },
456: { "d", tZONE, HOUR( 4) },
457: { "e", tZONE, HOUR( 5) },
458: { "f", tZONE, HOUR( 6) },
459: { "g", tZONE, HOUR( 7) },
460: { "h", tZONE, HOUR( 8) },
461: { "i", tZONE, HOUR( 9) },
462: { "k", tZONE, HOUR( 10) },
463: { "l", tZONE, HOUR( 11) },
464: { "m", tZONE, HOUR( 12) },
465: { "n", tZONE, HOUR(- 1) },
466: { "o", tZONE, HOUR(- 2) },
467: { "p", tZONE, HOUR(- 3) },
468: { "q", tZONE, HOUR(- 4) },
469: { "r", tZONE, HOUR(- 5) },
470: { "s", tZONE, HOUR(- 6) },
471: { "t", tZONE, HOUR(- 7) },
472: { "u", tZONE, HOUR(- 8) },
473: { "v", tZONE, HOUR(- 9) },
474: { "w", tZONE, HOUR(-10) },
475: { "x", tZONE, HOUR(-11) },
476: { "y", tZONE, HOUR(-12) },
477: { "z", tZONE, HOUR( 0) },
478: { NULL }
479: };
480:
481:
482: static int
483: yyerror(const char *s)
484: {
485: char *str;
486: int n;
487:
488: if (isspace(yyInput[0]) || !isprint(yyInput[0]))
489: n = asprintf(&str, "%s: unexpected char 0x%02x in date string",
490: s, yyInput[0]);
491: else
492: n = asprintf(&str, "%s: unexpected %s in date string",
493: s, yyInput);
494: if (n == -1)
495: return (0);
496:
497: #if defined(TEST)
498: printf("%s", str);
499: #else
500: warnx("%s", str);
501: #endif
502: free(str);
503: return (0);
504: }
505:
506:
507: static time_t
508: ToSeconds(time_t Hours, time_t Minutes, time_t Seconds, MERIDIAN Meridian)
509: {
510: if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
511: return (-1);
512:
513: switch (Meridian) {
514: case MER24:
515: if (Hours < 0 || Hours > 23)
516: return (-1);
517: return (Hours * 60L + Minutes) * 60L + Seconds;
518: case MERam:
519: if (Hours < 1 || Hours > 12)
520: return (-1);
521: if (Hours == 12)
522: Hours = 0;
523: return (Hours * 60L + Minutes) * 60L + Seconds;
524: case MERpm:
525: if (Hours < 1 || Hours > 12)
526: return (-1);
527: if (Hours == 12)
528: Hours = 0;
529: return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
530: default:
531: abort();
532: }
533: /* NOTREACHED */
534: }
535:
536:
537: /* Year is either
538: * A negative number, which means to use its absolute value (why?)
539: * A number from 0 to 99, which means a year from 1900 to 1999, or
540: * The actual year (>=100).
541: */
542: static time_t
543: Convert(time_t Month, time_t Day, time_t Year, time_t Hours, time_t Minutes,
544: time_t Seconds, MERIDIAN Meridian, DSTMODE DSTmode)
545: {
546: static int DaysInMonth[12] = {
547: 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
548: };
549: time_t tod;
550: time_t julian;
551: int i;
552:
553: if (Year < 0)
554: Year = -Year;
555: if (Year < 69)
556: Year += 2000;
557: else if (Year < 100) {
558: Year += 1900;
559: if (Year < YEAR_EPOCH)
560: Year += 100;
561: }
562: DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
563: ? 29 : 28;
564: /* Checking for 2038 bogusly assumes that time_t is 32 bits. But
565: I'm too lazy to try to check for time_t overflow in another way. */
566: if (Year < YEAR_EPOCH || Year > 2038 || Month < 1 || Month > 12 ||
567: /* Lint fluff: "conversion from long may lose accuracy" */
568: Day < 1 || Day > DaysInMonth[(int)--Month])
569: return (-1);
570:
571: for (julian = Day - 1, i = 0; i < Month; i++)
572: julian += DaysInMonth[i];
573:
574: for (i = YEAR_EPOCH; i < Year; i++)
575: julian += 365 + (i % 4 == 0);
576: julian *= SECSPERDAY;
577: julian += yyTimezone * 60L;
578:
579: if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
580: return (-1);
581: julian += tod;
582: if ((DSTmode == DSTon) ||
583: (DSTmode == DSTmaybe && localtime(&julian)->tm_isdst))
584: julian -= 60 * 60;
585: return (julian);
586: }
587:
588:
589: static time_t
590: DSTcorrect(time_t Start, time_t Future)
591: {
592: time_t StartDay;
593: time_t FutureDay;
594:
595: StartDay = (localtime(&Start)->tm_hour + 1) % 24;
596: FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
597: return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
598: }
599:
600:
601: static time_t
602: RelativeDate(time_t Start, time_t DayOrdinal, time_t DayNumber)
603: {
604: struct tm *tm;
605: time_t now;
606:
607: now = Start;
608: tm = localtime(&now);
609: now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
610: now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
611: return DSTcorrect(Start, now);
612: }
613:
614:
615: static time_t
616: RelativeMonth(time_t Start, time_t RelMonth)
617: {
618: struct tm *tm;
619: time_t Month;
620: time_t Year;
621:
622: if (RelMonth == 0)
623: return (0);
624: tm = localtime(&Start);
625: Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
626: Year = Month / 12;
627: Month = Month % 12 + 1;
628: return DSTcorrect(Start,
629: Convert(Month, (time_t)tm->tm_mday, Year,
630: (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
631: MER24, DSTmaybe));
632: }
633:
634:
635: static int
636: lookup(char *buff)
637: {
638: char *p, *q;
639: int i, abbrev;
640: const TABLE *tp;
641:
642: /* Make it lowercase. */
643: for (p = buff; *p; p++)
644: if (isupper(*p))
645: *p = tolower(*p);
646:
647: if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
648: yylval.Meridian = MERam;
649: return (tMERIDIAN);
650: }
651: if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
652: yylval.Meridian = MERpm;
653: return (tMERIDIAN);
654: }
655:
656: /* See if we have an abbreviation for a month. */
657: if (strlen(buff) == 3)
658: abbrev = 1;
659: else if (strlen(buff) == 4 && buff[3] == '.') {
660: abbrev = 1;
661: buff[3] = '\0';
662: } else
663: abbrev = 0;
664:
665: for (tp = MonthDayTable; tp->name; tp++) {
666: if (abbrev) {
667: if (strncmp(buff, tp->name, 3) == 0) {
668: yylval.Number = tp->value;
669: return (tp->type);
670: }
671: } else if (strcmp(buff, tp->name) == 0) {
672: yylval.Number = tp->value;
673: return (tp->type);
674: }
675: }
676:
677: for (tp = TimezoneTable; tp->name; tp++)
678: if (strcmp(buff, tp->name) == 0) {
679: yylval.Number = tp->value;
680: return (tp->type);
681: }
682:
683: if (strcmp(buff, "dst") == 0)
684: return (tDST);
685:
686: for (tp = UnitsTable; tp->name; tp++)
687: if (strcmp(buff, tp->name) == 0) {
688: yylval.Number = tp->value;
689: return (tp->type);
690: }
691:
692: /* Strip off any plural and try the units table again. */
693: i = strlen(buff) - 1;
694: if (buff[i] == 's') {
695: buff[i] = '\0';
696: for (tp = UnitsTable; tp->name; tp++)
697: if (strcmp(buff, tp->name) == 0) {
698: yylval.Number = tp->value;
699: return (tp->type);
700: }
701: buff[i] = 's'; /* Put back for "this" in OtherTable. */
702: }
703:
704: for (tp = OtherTable; tp->name; tp++)
705: if (strcmp(buff, tp->name) == 0) {
706: yylval.Number = tp->value;
707: return (tp->type);
708: }
709:
710: /* Military timezones. */
711: if (buff[1] == '\0' && isalpha(*buff)) {
712: for (tp = MilitaryTable; tp->name; tp++)
713: if (strcmp(buff, tp->name) == 0) {
714: yylval.Number = tp->value;
715: return (tp->type);
716: }
717: }
718:
719: /* Drop out any periods and try the timezone table again. */
720: for (i = 0, p = q = buff; *q; q++)
721: if (*q != '.')
722: *p++ = *q;
723: else
724: i++;
725: *p = '\0';
726: if (i)
727: for (tp = TimezoneTable; tp->name; tp++)
728: if (strcmp(buff, tp->name) == 0) {
729: yylval.Number = tp->value;
730: return (tp->type);
731: }
732:
733: return (tID);
734: }
735:
736:
737: static int
738: yylex(void)
739: {
740: char c, *p, buff[20];
741: int count, sign;
742:
743: for (;;) {
744: while (isspace(*yyInput))
745: yyInput++;
746:
747: if (isdigit(c = *yyInput) || c == '-' || c == '+') {
748: if (c == '-' || c == '+') {
749: sign = c == '-' ? -1 : 1;
750: if (!isdigit(*++yyInput))
751: /* skip the '-' sign */
752: continue;
753: }
754: else
755: sign = 0;
756:
757: for (yylval.Number = 0; isdigit(c = *yyInput++); )
758: yylval.Number = 10 * yylval.Number + c - '0';
759: yyInput--;
760: if (sign < 0)
761: yylval.Number = -yylval.Number;
762: return sign ? tSNUMBER : tUNUMBER;
763: }
764:
765: if (isalpha(c)) {
766: for (p = buff; isalpha(c = *yyInput++) || c == '.'; )
767: if (p < &buff[sizeof buff - 1])
768: *p++ = c;
769: *p = '\0';
770: yyInput--;
771: return lookup(buff);
772: }
773: if (c != '(')
774: return *yyInput++;
775:
776: count = 0;
777: do {
778: c = *yyInput++;
779: if (c == '\0')
780: return (c);
781: if (c == '(')
782: count++;
783: else if (c == ')')
784: count--;
785: } while (count > 0);
786: }
787: }
788:
789: /* Yield A - B, measured in seconds. */
790: static long
791: difftm(struct tm *a, struct tm *b)
792: {
793: int ay = a->tm_year + (YEAR_TMORIGIN - 1);
794: int by = b->tm_year + (YEAR_TMORIGIN - 1);
795: int days = (
796: /* difference in day of year */
797: a->tm_yday - b->tm_yday
798: /* + intervening leap days */
799: + ((ay >> 2) - (by >> 2))
800: - (ay/100 - by/100)
801: + ((ay/100 >> 2) - (by/100 >> 2))
802: /* + difference in years * 365 */
803: + (long)(ay-by) * 365);
804: return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
805: + (a->tm_min - b->tm_min)) + (a->tm_sec - b->tm_sec));
806: }
807:
808: /*
809: * rcs_date_parse()
810: *
811: * Returns the number of seconds since the Epoch corresponding to the date.
812: */
813: time_t
814: rcs_date_parse(const char *p)
815: {
1.2 ! ray 816: struct tm gmt, *gmt_ptr, *tm;
1.1 joris 817: struct timeb ftz, *now;
818: time_t Start, tod, nowtime;
819:
820: yyInput = p;
821:
1.2 ! ray 822: now = &ftz;
! 823: (void)time(&nowtime);
1.1 joris 824:
1.2 ! ray 825: gmt_ptr = gmtime(&nowtime);
! 826: if (gmt_ptr != NULL) {
! 827: /* Make a copy, in case localtime modifies *tm (I think
! 828: * that comment now applies to *gmt_ptr, but I am too
! 829: * lazy to dig into how gmtime and locatime allocate the
! 830: * structures they return pointers to).
! 831: */
! 832: gmt = *gmt_ptr;
! 833: }
1.1 joris 834:
1.2 ! ray 835: if (!(tm = localtime(&nowtime)))
! 836: return (-1);
1.1 joris 837:
1.2 ! ray 838: if (gmt_ptr != NULL)
! 839: ftz.timezone = difftm(&gmt, tm) / 60;
1.1 joris 840:
1.2 ! ray 841: if (tm->tm_isdst)
! 842: ftz.timezone += 60;
1.1 joris 843:
844: tm = localtime(&nowtime);
845: yyYear = tm->tm_year + 1900;
846: yyMonth = tm->tm_mon + 1;
847: yyDay = tm->tm_mday;
848: yyTimezone = now->timezone;
849: yyDSTmode = DSTmaybe;
850: yyHour = 0;
851: yyMinutes = 0;
852: yySeconds = 0;
853: yyMeridian = MER24;
854: yyRelSeconds = 0;
855: yyRelMonth = 0;
856: yyHaveDate = 0;
857: yyHaveDay = 0;
858: yyHaveRel = 0;
859: yyHaveTime = 0;
860: yyHaveZone = 0;
861:
862: if (yyparse() || yyHaveTime > 1 || yyHaveZone > 1 ||
863: yyHaveDate > 1 || yyHaveDay > 1)
864: return (-1);
865:
866: if (yyHaveDate || yyHaveTime || yyHaveDay) {
867: Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes,
868: yySeconds, yyMeridian, yyDSTmode);
869: if (Start < 0)
870: return (-1);
871: } else {
872: Start = nowtime;
873: if (!yyHaveRel)
874: Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) +
875: tm->tm_sec;
876: }
877:
878: Start += yyRelSeconds;
879: Start += RelativeMonth(Start, yyRelMonth);
880:
881: if (yyHaveDay && !yyHaveDate) {
882: tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
883: Start += tod;
884: }
885:
886: /* Have to do *something* with a legitimate -1 so it's distinguishable
887: * from the error return value. (Alternately could set errno on error.)
888: */
889: return (Start == -1) ? (0) : (Start);
890: }
891:
892: #if defined(TEST)
893: /* ARGSUSED */
894: int
895: main(int argc, char **argv)
896: {
897: char buff[128];
898: time_t d;
899:
900: (void)printf("Enter date, or blank line to exit.\n\t> ");
901: (void)fflush(stdout);
902: while (fgets(buff, sizeof(buff), stdin) && buff[0]) {
903: d = rcs_date_parse(buff);
904: if (d == -1)
905: (void)printf("Bad format - couldn't convert.\n");
906: else
907: (void)printf("%s", ctime(&d));
908: (void)printf("\t> ");
909: (void)fflush(stdout);
910: }
911:
912: return (0);
913: }
914: #endif /* defined(TEST) */