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