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