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