xref: /freebsd/usr.sbin/fifolog/lib/getdate.y (revision 42b388439bd3795e09258c57a74ce9eec3651c7b)
1662cb04cSPoul-Henning Kamp %{
2662cb04cSPoul-Henning Kamp /*
3662cb04cSPoul-Henning Kamp **  Originally written by Steven M. Bellovin <smb@research.att.com> while
4662cb04cSPoul-Henning Kamp **  at the University of North Carolina at Chapel Hill.  Later tweaked by
5662cb04cSPoul-Henning Kamp **  a couple of people on Usenet.  Completely overhauled by Rich $alz
6662cb04cSPoul-Henning Kamp **  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
7662cb04cSPoul-Henning Kamp **
8662cb04cSPoul-Henning Kamp **  This grammar has 10 shift/reduce conflicts.
9662cb04cSPoul-Henning Kamp **
10662cb04cSPoul-Henning Kamp **  This code is in the public domain and has no copyright.
11662cb04cSPoul-Henning Kamp **
12662cb04cSPoul-Henning Kamp **  Picked up from CVS and slightly cleaned up by to WARNS=5 level by
13662cb04cSPoul-Henning Kamp **  Poul-Henning Kamp <phk@FreeBSD.org>
14662cb04cSPoul-Henning Kamp **
15662cb04cSPoul-Henning Kamp */
16662cb04cSPoul-Henning Kamp 
17662cb04cSPoul-Henning Kamp #include <stdio.h>
18662cb04cSPoul-Henning Kamp #include <stdlib.h>
19662cb04cSPoul-Henning Kamp #include <ctype.h>
20662cb04cSPoul-Henning Kamp #include <string.h>
21662cb04cSPoul-Henning Kamp #include <sys/types.h>
22662cb04cSPoul-Henning Kamp #include <sys/time.h>
23662cb04cSPoul-Henning Kamp 
24662cb04cSPoul-Henning Kamp #include "libfifolog.h"
25662cb04cSPoul-Henning Kamp 
26aaa28628SPoul-Henning Kamp #define yyparse getdate_yyparse
27662cb04cSPoul-Henning Kamp #define yylex getdate_yylex
28662cb04cSPoul-Henning Kamp #define yyerror getdate_yyerror
29662cb04cSPoul-Henning Kamp 
30aaa28628SPoul-Henning Kamp static int yyparse(void);
31662cb04cSPoul-Henning Kamp static int yylex(void);
32662cb04cSPoul-Henning Kamp static int yyerror(const char *);
33662cb04cSPoul-Henning Kamp 
34662cb04cSPoul-Henning Kamp #define EPOCH		1970
35662cb04cSPoul-Henning Kamp #define HOUR(x)		((time_t)(x) * 60)
36662cb04cSPoul-Henning Kamp #define SECSPERDAY	(24L * 60L * 60L)
37662cb04cSPoul-Henning Kamp 
38662cb04cSPoul-Henning Kamp 
39662cb04cSPoul-Henning Kamp /*
40662cb04cSPoul-Henning Kamp **  An entry in the lexical lookup table.
41662cb04cSPoul-Henning Kamp */
42662cb04cSPoul-Henning Kamp typedef struct _TABLE {
43662cb04cSPoul-Henning Kamp     const char	*name;
44662cb04cSPoul-Henning Kamp     int		type;
45662cb04cSPoul-Henning Kamp     time_t	value;
46662cb04cSPoul-Henning Kamp } TABLE;
47662cb04cSPoul-Henning Kamp 
48662cb04cSPoul-Henning Kamp 
49662cb04cSPoul-Henning Kamp /*
50662cb04cSPoul-Henning Kamp **  Daylight-savings mode:  on, off, or not yet known.
51662cb04cSPoul-Henning Kamp */
52662cb04cSPoul-Henning Kamp typedef enum _DSTMODE {
53662cb04cSPoul-Henning Kamp     DSToff, DSTon, DSTmaybe
54662cb04cSPoul-Henning Kamp } DSTMODE;
55662cb04cSPoul-Henning Kamp 
56662cb04cSPoul-Henning Kamp /*
57662cb04cSPoul-Henning Kamp **  Meridian:  am, pm, or 24-hour style.
58662cb04cSPoul-Henning Kamp */
59662cb04cSPoul-Henning Kamp typedef enum _MERIDIAN {
60662cb04cSPoul-Henning Kamp     MERam, MERpm, MER24
61662cb04cSPoul-Henning Kamp } MERIDIAN;
62662cb04cSPoul-Henning Kamp 
63662cb04cSPoul-Henning Kamp 
64662cb04cSPoul-Henning Kamp /*
65662cb04cSPoul-Henning Kamp **  Global variables.  We could get rid of most of these by using a good
66662cb04cSPoul-Henning Kamp **  union as the yacc stack.  (This routine was originally written before
67662cb04cSPoul-Henning Kamp **  yacc had the %union construct.)  Maybe someday; right now we only use
68662cb04cSPoul-Henning Kamp **  the %union very rarely.
69662cb04cSPoul-Henning Kamp */
70662cb04cSPoul-Henning Kamp static char	*yyInput;
71662cb04cSPoul-Henning Kamp static DSTMODE	yyDSTmode;
72662cb04cSPoul-Henning Kamp static time_t	yyDayOrdinal;
73662cb04cSPoul-Henning Kamp static time_t	yyDayNumber;
74662cb04cSPoul-Henning Kamp static int	yyHaveDate;
75662cb04cSPoul-Henning Kamp static int	yyHaveDay;
76662cb04cSPoul-Henning Kamp static int	yyHaveRel;
77662cb04cSPoul-Henning Kamp static int	yyHaveTime;
78662cb04cSPoul-Henning Kamp static int	yyHaveZone;
79662cb04cSPoul-Henning Kamp static time_t	yyTimezone;
80662cb04cSPoul-Henning Kamp static time_t	yyDay;
81662cb04cSPoul-Henning Kamp static time_t	yyHour;
82662cb04cSPoul-Henning Kamp static time_t	yyMinutes;
83662cb04cSPoul-Henning Kamp static time_t	yyMonth;
84662cb04cSPoul-Henning Kamp static time_t	yySeconds;
85662cb04cSPoul-Henning Kamp static time_t	yyYear;
86662cb04cSPoul-Henning Kamp static MERIDIAN	yyMeridian;
87662cb04cSPoul-Henning Kamp static time_t	yyRelMonth;
88662cb04cSPoul-Henning Kamp static time_t	yyRelSeconds;
89662cb04cSPoul-Henning Kamp 
90662cb04cSPoul-Henning Kamp %}
91662cb04cSPoul-Henning Kamp 
92662cb04cSPoul-Henning Kamp %union {
93662cb04cSPoul-Henning Kamp     time_t		Number;
94662cb04cSPoul-Henning Kamp     enum _MERIDIAN	Meridian;
95662cb04cSPoul-Henning Kamp }
96662cb04cSPoul-Henning Kamp 
97662cb04cSPoul-Henning Kamp %token	tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
98662cb04cSPoul-Henning Kamp %token	tSEC_UNIT tSNUMBER tUNUMBER tZONE tDST
99662cb04cSPoul-Henning Kamp 
100662cb04cSPoul-Henning Kamp %type	<Number>	tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
101662cb04cSPoul-Henning Kamp %type	<Number>	tSEC_UNIT tSNUMBER tUNUMBER tZONE
102662cb04cSPoul-Henning Kamp %type	<Meridian>	tMERIDIAN o_merid
103662cb04cSPoul-Henning Kamp 
104662cb04cSPoul-Henning Kamp %%
105662cb04cSPoul-Henning Kamp 
106662cb04cSPoul-Henning Kamp spec	: /* NULL */
107662cb04cSPoul-Henning Kamp 	| spec item
108662cb04cSPoul-Henning Kamp 	;
109662cb04cSPoul-Henning Kamp 
110662cb04cSPoul-Henning Kamp item	: time {
111662cb04cSPoul-Henning Kamp 	    yyHaveTime++;
112662cb04cSPoul-Henning Kamp 	}
113662cb04cSPoul-Henning Kamp 	| zone {
114662cb04cSPoul-Henning Kamp 	    yyHaveZone++;
115662cb04cSPoul-Henning Kamp 	}
116662cb04cSPoul-Henning Kamp 	| date {
117662cb04cSPoul-Henning Kamp 	    yyHaveDate++;
118662cb04cSPoul-Henning Kamp 	}
119662cb04cSPoul-Henning Kamp 	| day {
120662cb04cSPoul-Henning Kamp 	    yyHaveDay++;
121662cb04cSPoul-Henning Kamp 	}
122662cb04cSPoul-Henning Kamp 	| rel {
123662cb04cSPoul-Henning Kamp 	    yyHaveRel++;
124662cb04cSPoul-Henning Kamp 	}
125662cb04cSPoul-Henning Kamp 	| cvsstamp {
126662cb04cSPoul-Henning Kamp 	    yyHaveTime++;
127662cb04cSPoul-Henning Kamp 	    yyHaveDate++;
128662cb04cSPoul-Henning Kamp 	    yyHaveZone++;
129662cb04cSPoul-Henning Kamp 	}
130662cb04cSPoul-Henning Kamp 	| number
131662cb04cSPoul-Henning Kamp 	;
132662cb04cSPoul-Henning Kamp 
133662cb04cSPoul-Henning Kamp cvsstamp: tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER {
134662cb04cSPoul-Henning Kamp 	    yyYear = $1;
135662cb04cSPoul-Henning Kamp 	    if (yyYear < 100) yyYear += 1900;
136662cb04cSPoul-Henning Kamp 	    yyMonth = $3;
137662cb04cSPoul-Henning Kamp 	    yyDay = $5;
138662cb04cSPoul-Henning Kamp 	    yyHour = $7;
139662cb04cSPoul-Henning Kamp 	    yyMinutes = $9;
140662cb04cSPoul-Henning Kamp 	    yySeconds = $11;
141662cb04cSPoul-Henning Kamp 	    yyDSTmode = DSToff;
142662cb04cSPoul-Henning Kamp 	    yyTimezone = 0;
143662cb04cSPoul-Henning Kamp 	}
144662cb04cSPoul-Henning Kamp 	;
145662cb04cSPoul-Henning Kamp 
146662cb04cSPoul-Henning Kamp time	: tUNUMBER tMERIDIAN {
147662cb04cSPoul-Henning Kamp 	    yyHour = $1;
148662cb04cSPoul-Henning Kamp 	    yyMinutes = 0;
149662cb04cSPoul-Henning Kamp 	    yySeconds = 0;
150662cb04cSPoul-Henning Kamp 	    yyMeridian = $2;
151662cb04cSPoul-Henning Kamp 	}
152662cb04cSPoul-Henning Kamp 	| tUNUMBER ':' tUNUMBER o_merid {
153662cb04cSPoul-Henning Kamp 	    yyHour = $1;
154662cb04cSPoul-Henning Kamp 	    yyMinutes = $3;
155662cb04cSPoul-Henning Kamp 	    yySeconds = 0;
156662cb04cSPoul-Henning Kamp 	    yyMeridian = $4;
157662cb04cSPoul-Henning Kamp 	}
158662cb04cSPoul-Henning Kamp 	| tUNUMBER ':' tUNUMBER tSNUMBER {
159662cb04cSPoul-Henning Kamp 	    yyHour = $1;
160662cb04cSPoul-Henning Kamp 	    yyMinutes = $3;
161662cb04cSPoul-Henning Kamp 	    yyMeridian = MER24;
162662cb04cSPoul-Henning Kamp 	    yyDSTmode = DSToff;
163662cb04cSPoul-Henning Kamp 	    yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
164662cb04cSPoul-Henning Kamp 	}
165662cb04cSPoul-Henning Kamp 	| tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
166662cb04cSPoul-Henning Kamp 	    yyHour = $1;
167662cb04cSPoul-Henning Kamp 	    yyMinutes = $3;
168662cb04cSPoul-Henning Kamp 	    yySeconds = $5;
169662cb04cSPoul-Henning Kamp 	    yyMeridian = $6;
170662cb04cSPoul-Henning Kamp 	}
171662cb04cSPoul-Henning Kamp 	| tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
172662cb04cSPoul-Henning Kamp 	    yyHour = $1;
173662cb04cSPoul-Henning Kamp 	    yyMinutes = $3;
174662cb04cSPoul-Henning Kamp 	    yySeconds = $5;
175662cb04cSPoul-Henning Kamp 	    yyMeridian = MER24;
176662cb04cSPoul-Henning Kamp 	    yyDSTmode = DSToff;
177662cb04cSPoul-Henning Kamp 	    yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
178662cb04cSPoul-Henning Kamp 	}
179662cb04cSPoul-Henning Kamp 	;
180662cb04cSPoul-Henning Kamp 
181662cb04cSPoul-Henning Kamp zone	: tZONE {
182662cb04cSPoul-Henning Kamp 	    yyTimezone = $1;
183662cb04cSPoul-Henning Kamp 	    yyDSTmode = DSToff;
184662cb04cSPoul-Henning Kamp 	}
185662cb04cSPoul-Henning Kamp 	| tDAYZONE {
186662cb04cSPoul-Henning Kamp 	    yyTimezone = $1;
187662cb04cSPoul-Henning Kamp 	    yyDSTmode = DSTon;
188662cb04cSPoul-Henning Kamp 	}
189662cb04cSPoul-Henning Kamp 	|
190662cb04cSPoul-Henning Kamp 	  tZONE tDST {
191662cb04cSPoul-Henning Kamp 	    yyTimezone = $1;
192662cb04cSPoul-Henning Kamp 	    yyDSTmode = DSTon;
193662cb04cSPoul-Henning Kamp 	}
194662cb04cSPoul-Henning Kamp 	;
195662cb04cSPoul-Henning Kamp 
196662cb04cSPoul-Henning Kamp day	: tDAY {
197662cb04cSPoul-Henning Kamp 	    yyDayOrdinal = 1;
198662cb04cSPoul-Henning Kamp 	    yyDayNumber = $1;
199662cb04cSPoul-Henning Kamp 	}
200662cb04cSPoul-Henning Kamp 	| tDAY ',' {
201662cb04cSPoul-Henning Kamp 	    yyDayOrdinal = 1;
202662cb04cSPoul-Henning Kamp 	    yyDayNumber = $1;
203662cb04cSPoul-Henning Kamp 	}
204662cb04cSPoul-Henning Kamp 	| tUNUMBER tDAY {
205662cb04cSPoul-Henning Kamp 	    yyDayOrdinal = $1;
206662cb04cSPoul-Henning Kamp 	    yyDayNumber = $2;
207662cb04cSPoul-Henning Kamp 	}
208662cb04cSPoul-Henning Kamp 	;
209662cb04cSPoul-Henning Kamp 
210662cb04cSPoul-Henning Kamp date	: tUNUMBER '/' tUNUMBER {
211662cb04cSPoul-Henning Kamp 	    yyMonth = $1;
212662cb04cSPoul-Henning Kamp 	    yyDay = $3;
213662cb04cSPoul-Henning Kamp 	}
214662cb04cSPoul-Henning Kamp 	| tUNUMBER '/' tUNUMBER '/' tUNUMBER {
215662cb04cSPoul-Henning Kamp 	    if ($1 >= 100) {
216662cb04cSPoul-Henning Kamp 		yyYear = $1;
217662cb04cSPoul-Henning Kamp 		yyMonth = $3;
218662cb04cSPoul-Henning Kamp 		yyDay = $5;
219662cb04cSPoul-Henning Kamp 	    } else {
220662cb04cSPoul-Henning Kamp 		yyMonth = $1;
221662cb04cSPoul-Henning Kamp 		yyDay = $3;
222662cb04cSPoul-Henning Kamp 		yyYear = $5;
223662cb04cSPoul-Henning Kamp 	    }
224662cb04cSPoul-Henning Kamp 	}
225662cb04cSPoul-Henning Kamp 	| tUNUMBER tSNUMBER tSNUMBER {
226662cb04cSPoul-Henning Kamp 	    /* ISO 8601 format.  yyyy-mm-dd.  */
227662cb04cSPoul-Henning Kamp 	    yyYear = $1;
228662cb04cSPoul-Henning Kamp 	    yyMonth = -$2;
229662cb04cSPoul-Henning Kamp 	    yyDay = -$3;
230662cb04cSPoul-Henning Kamp 	}
231662cb04cSPoul-Henning Kamp 	| tUNUMBER tMONTH tSNUMBER {
232662cb04cSPoul-Henning Kamp 	    /* e.g. 17-JUN-1992.  */
233662cb04cSPoul-Henning Kamp 	    yyDay = $1;
234662cb04cSPoul-Henning Kamp 	    yyMonth = $2;
235662cb04cSPoul-Henning Kamp 	    yyYear = -$3;
236662cb04cSPoul-Henning Kamp 	}
237662cb04cSPoul-Henning Kamp 	| tMONTH tUNUMBER {
238662cb04cSPoul-Henning Kamp 	    yyMonth = $1;
239662cb04cSPoul-Henning Kamp 	    yyDay = $2;
240662cb04cSPoul-Henning Kamp 	}
241662cb04cSPoul-Henning Kamp 	| tMONTH tUNUMBER ',' tUNUMBER {
242662cb04cSPoul-Henning Kamp 	    yyMonth = $1;
243662cb04cSPoul-Henning Kamp 	    yyDay = $2;
244662cb04cSPoul-Henning Kamp 	    yyYear = $4;
245662cb04cSPoul-Henning Kamp 	}
246662cb04cSPoul-Henning Kamp 	| tUNUMBER tMONTH {
247662cb04cSPoul-Henning Kamp 	    yyMonth = $2;
248662cb04cSPoul-Henning Kamp 	    yyDay = $1;
249662cb04cSPoul-Henning Kamp 	}
250662cb04cSPoul-Henning Kamp 	| tUNUMBER tMONTH tUNUMBER {
251662cb04cSPoul-Henning Kamp 	    yyMonth = $2;
252662cb04cSPoul-Henning Kamp 	    yyDay = $1;
253662cb04cSPoul-Henning Kamp 	    yyYear = $3;
254662cb04cSPoul-Henning Kamp 	}
255662cb04cSPoul-Henning Kamp 	;
256662cb04cSPoul-Henning Kamp 
257662cb04cSPoul-Henning Kamp rel	: relunit tAGO {
258662cb04cSPoul-Henning Kamp 	    yyRelSeconds = -yyRelSeconds;
259662cb04cSPoul-Henning Kamp 	    yyRelMonth = -yyRelMonth;
260662cb04cSPoul-Henning Kamp 	}
261662cb04cSPoul-Henning Kamp 	| relunit
262662cb04cSPoul-Henning Kamp 	;
263662cb04cSPoul-Henning Kamp 
264662cb04cSPoul-Henning Kamp relunit	: tUNUMBER tMINUTE_UNIT {
265662cb04cSPoul-Henning Kamp 	    yyRelSeconds += $1 * $2 * 60L;
266662cb04cSPoul-Henning Kamp 	}
267662cb04cSPoul-Henning Kamp 	| tSNUMBER tMINUTE_UNIT {
268662cb04cSPoul-Henning Kamp 	    yyRelSeconds += $1 * $2 * 60L;
269662cb04cSPoul-Henning Kamp 	}
270662cb04cSPoul-Henning Kamp 	| tMINUTE_UNIT {
271662cb04cSPoul-Henning Kamp 	    yyRelSeconds += $1 * 60L;
272662cb04cSPoul-Henning Kamp 	}
273662cb04cSPoul-Henning Kamp 	| tSNUMBER tSEC_UNIT {
274662cb04cSPoul-Henning Kamp 	    yyRelSeconds += $1;
275662cb04cSPoul-Henning Kamp 	}
276662cb04cSPoul-Henning Kamp 	| tUNUMBER tSEC_UNIT {
277662cb04cSPoul-Henning Kamp 	    yyRelSeconds += $1;
278662cb04cSPoul-Henning Kamp 	}
279662cb04cSPoul-Henning Kamp 	| tSEC_UNIT {
280662cb04cSPoul-Henning Kamp 	    yyRelSeconds++;
281662cb04cSPoul-Henning Kamp 	}
282662cb04cSPoul-Henning Kamp 	| tSNUMBER tMONTH_UNIT {
283662cb04cSPoul-Henning Kamp 	    yyRelMonth += $1 * $2;
284662cb04cSPoul-Henning Kamp 	}
285662cb04cSPoul-Henning Kamp 	| tUNUMBER tMONTH_UNIT {
286662cb04cSPoul-Henning Kamp 	    yyRelMonth += $1 * $2;
287662cb04cSPoul-Henning Kamp 	}
288662cb04cSPoul-Henning Kamp 	| tMONTH_UNIT {
289662cb04cSPoul-Henning Kamp 	    yyRelMonth += $1;
290662cb04cSPoul-Henning Kamp 	}
291662cb04cSPoul-Henning Kamp 	;
292662cb04cSPoul-Henning Kamp 
293662cb04cSPoul-Henning Kamp number	: tUNUMBER {
294662cb04cSPoul-Henning Kamp 	    if (yyHaveTime && yyHaveDate && !yyHaveRel)
295662cb04cSPoul-Henning Kamp 		yyYear = $1;
296662cb04cSPoul-Henning Kamp 	    else {
297662cb04cSPoul-Henning Kamp 		if($1>10000) {
298662cb04cSPoul-Henning Kamp 		    yyHaveDate++;
299662cb04cSPoul-Henning Kamp 		    yyDay= ($1)%100;
300662cb04cSPoul-Henning Kamp 		    yyMonth= ($1/100)%100;
301662cb04cSPoul-Henning Kamp 		    yyYear = $1/10000;
302662cb04cSPoul-Henning Kamp 		}
303662cb04cSPoul-Henning Kamp 		else {
304662cb04cSPoul-Henning Kamp 		    yyHaveTime++;
305662cb04cSPoul-Henning Kamp 		    if ($1 < 100) {
306662cb04cSPoul-Henning Kamp 			yyHour = $1;
307662cb04cSPoul-Henning Kamp 			yyMinutes = 0;
308662cb04cSPoul-Henning Kamp 		    }
309662cb04cSPoul-Henning Kamp 		    else {
310662cb04cSPoul-Henning Kamp 		    	yyHour = $1 / 100;
311662cb04cSPoul-Henning Kamp 		    	yyMinutes = $1 % 100;
312662cb04cSPoul-Henning Kamp 		    }
313662cb04cSPoul-Henning Kamp 		    yySeconds = 0;
314662cb04cSPoul-Henning Kamp 		    yyMeridian = MER24;
315662cb04cSPoul-Henning Kamp 	        }
316662cb04cSPoul-Henning Kamp 	    }
317662cb04cSPoul-Henning Kamp 	}
318662cb04cSPoul-Henning Kamp 	;
319662cb04cSPoul-Henning Kamp 
320662cb04cSPoul-Henning Kamp o_merid	: /* NULL */ {
321662cb04cSPoul-Henning Kamp 	    $$ = MER24;
322662cb04cSPoul-Henning Kamp 	}
323662cb04cSPoul-Henning Kamp 	| tMERIDIAN {
324662cb04cSPoul-Henning Kamp 	    $$ = $1;
325662cb04cSPoul-Henning Kamp 	}
326662cb04cSPoul-Henning Kamp 	;
327662cb04cSPoul-Henning Kamp 
328662cb04cSPoul-Henning Kamp %%
329662cb04cSPoul-Henning Kamp 
330662cb04cSPoul-Henning Kamp /* Month and day table. */
331662cb04cSPoul-Henning Kamp static TABLE const MonthDayTable[] = {
332662cb04cSPoul-Henning Kamp     { "january",	tMONTH,  1 },
333662cb04cSPoul-Henning Kamp     { "february",	tMONTH,  2 },
334662cb04cSPoul-Henning Kamp     { "march",		tMONTH,  3 },
335662cb04cSPoul-Henning Kamp     { "april",		tMONTH,  4 },
336662cb04cSPoul-Henning Kamp     { "may",		tMONTH,  5 },
337662cb04cSPoul-Henning Kamp     { "june",		tMONTH,  6 },
338662cb04cSPoul-Henning Kamp     { "july",		tMONTH,  7 },
339662cb04cSPoul-Henning Kamp     { "august",		tMONTH,  8 },
340662cb04cSPoul-Henning Kamp     { "september",	tMONTH,  9 },
341662cb04cSPoul-Henning Kamp     { "sept",		tMONTH,  9 },
342662cb04cSPoul-Henning Kamp     { "october",	tMONTH, 10 },
343662cb04cSPoul-Henning Kamp     { "november",	tMONTH, 11 },
344662cb04cSPoul-Henning Kamp     { "december",	tMONTH, 12 },
345662cb04cSPoul-Henning Kamp     { "sunday",		tDAY, 0 },
346662cb04cSPoul-Henning Kamp     { "monday",		tDAY, 1 },
347662cb04cSPoul-Henning Kamp     { "tuesday",	tDAY, 2 },
348662cb04cSPoul-Henning Kamp     { "tues",		tDAY, 2 },
349662cb04cSPoul-Henning Kamp     { "wednesday",	tDAY, 3 },
350662cb04cSPoul-Henning Kamp     { "wednes",		tDAY, 3 },
351662cb04cSPoul-Henning Kamp     { "thursday",	tDAY, 4 },
352662cb04cSPoul-Henning Kamp     { "thur",		tDAY, 4 },
353662cb04cSPoul-Henning Kamp     { "thurs",		tDAY, 4 },
354662cb04cSPoul-Henning Kamp     { "friday",		tDAY, 5 },
355662cb04cSPoul-Henning Kamp     { "saturday",	tDAY, 6 },
356662cb04cSPoul-Henning Kamp     { NULL,		0,    0 }
357662cb04cSPoul-Henning Kamp };
358662cb04cSPoul-Henning Kamp 
359662cb04cSPoul-Henning Kamp /* Time units table. */
360662cb04cSPoul-Henning Kamp static TABLE const UnitsTable[] = {
361662cb04cSPoul-Henning Kamp     { "year",		tMONTH_UNIT,	12 },
362662cb04cSPoul-Henning Kamp     { "month",		tMONTH_UNIT,	1 },
363662cb04cSPoul-Henning Kamp     { "fortnight",	tMINUTE_UNIT,	14 * 24 * 60 },
364662cb04cSPoul-Henning Kamp     { "week",		tMINUTE_UNIT,	7 * 24 * 60 },
365662cb04cSPoul-Henning Kamp     { "day",		tMINUTE_UNIT,	1 * 24 * 60 },
366662cb04cSPoul-Henning Kamp     { "hour",		tMINUTE_UNIT,	60 },
367662cb04cSPoul-Henning Kamp     { "minute",		tMINUTE_UNIT,	1 },
368662cb04cSPoul-Henning Kamp     { "min",		tMINUTE_UNIT,	1 },
369662cb04cSPoul-Henning Kamp     { "second",		tSEC_UNIT,	1 },
370662cb04cSPoul-Henning Kamp     { "sec",		tSEC_UNIT,	1 },
371662cb04cSPoul-Henning Kamp     { NULL,		0,		0 }
372662cb04cSPoul-Henning Kamp };
373662cb04cSPoul-Henning Kamp 
374662cb04cSPoul-Henning Kamp /* Assorted relative-time words. */
375662cb04cSPoul-Henning Kamp static TABLE const OtherTable[] = {
376662cb04cSPoul-Henning Kamp     { "tomorrow",	tMINUTE_UNIT,	1 * 24 * 60 },
377662cb04cSPoul-Henning Kamp     { "yesterday",	tMINUTE_UNIT,	-1 * 24 * 60 },
378662cb04cSPoul-Henning Kamp     { "today",		tMINUTE_UNIT,	0 },
379662cb04cSPoul-Henning Kamp     { "now",		tMINUTE_UNIT,	0 },
380662cb04cSPoul-Henning Kamp     { "last",		tUNUMBER,	-1 },
381662cb04cSPoul-Henning Kamp     { "this",		tMINUTE_UNIT,	0 },
382662cb04cSPoul-Henning Kamp     { "next",		tUNUMBER,	2 },
383662cb04cSPoul-Henning Kamp     { "first",		tUNUMBER,	1 },
384662cb04cSPoul-Henning Kamp /*  { "second",		tUNUMBER,	2 }, */
385662cb04cSPoul-Henning Kamp     { "third",		tUNUMBER,	3 },
386662cb04cSPoul-Henning Kamp     { "fourth",		tUNUMBER,	4 },
387662cb04cSPoul-Henning Kamp     { "fifth",		tUNUMBER,	5 },
388662cb04cSPoul-Henning Kamp     { "sixth",		tUNUMBER,	6 },
389662cb04cSPoul-Henning Kamp     { "seventh",	tUNUMBER,	7 },
390662cb04cSPoul-Henning Kamp     { "eighth",		tUNUMBER,	8 },
391662cb04cSPoul-Henning Kamp     { "ninth",		tUNUMBER,	9 },
392662cb04cSPoul-Henning Kamp     { "tenth",		tUNUMBER,	10 },
393662cb04cSPoul-Henning Kamp     { "eleventh",	tUNUMBER,	11 },
394662cb04cSPoul-Henning Kamp     { "twelfth",	tUNUMBER,	12 },
395662cb04cSPoul-Henning Kamp     { "ago",		tAGO,		1 },
396662cb04cSPoul-Henning Kamp     { NULL,		0,		0 }
397662cb04cSPoul-Henning Kamp };
398662cb04cSPoul-Henning Kamp 
399662cb04cSPoul-Henning Kamp /* The timezone table. */
400662cb04cSPoul-Henning Kamp /* Some of these are commented out because a time_t can't store a float. */
401662cb04cSPoul-Henning Kamp static TABLE const TimezoneTable[] = {
402662cb04cSPoul-Henning Kamp     { "gmt",	tZONE,     HOUR( 0) },	/* Greenwich Mean */
403662cb04cSPoul-Henning Kamp     { "ut",	tZONE,     HOUR( 0) },	/* Universal (Coordinated) */
404662cb04cSPoul-Henning Kamp     { "utc",	tZONE,     HOUR( 0) },
405662cb04cSPoul-Henning Kamp     { "wet",	tZONE,     HOUR( 0) },	/* Western European */
406662cb04cSPoul-Henning Kamp     { "bst",	tDAYZONE,  HOUR( 0) },	/* British Summer */
407662cb04cSPoul-Henning Kamp     { "wat",	tZONE,     HOUR( 1) },	/* West Africa */
408662cb04cSPoul-Henning Kamp     { "at",	tZONE,     HOUR( 2) },	/* Azores */
409662cb04cSPoul-Henning Kamp #if	0
410662cb04cSPoul-Henning Kamp     /* For completeness.  BST is also British Summer, and GST is
411662cb04cSPoul-Henning Kamp      * also Guam Standard. */
412662cb04cSPoul-Henning Kamp     { "bst",	tZONE,     HOUR( 3) },	/* Brazil Standard */
413662cb04cSPoul-Henning Kamp     { "gst",	tZONE,     HOUR( 3) },	/* Greenland Standard */
414662cb04cSPoul-Henning Kamp #endif
415662cb04cSPoul-Henning Kamp #if 0
416662cb04cSPoul-Henning Kamp     { "nft",	tZONE,     HOUR(3.5) },	/* Newfoundland */
417662cb04cSPoul-Henning Kamp     { "nst",	tZONE,     HOUR(3.5) },	/* Newfoundland Standard */
418662cb04cSPoul-Henning Kamp     { "ndt",	tDAYZONE,  HOUR(3.5) },	/* Newfoundland Daylight */
419662cb04cSPoul-Henning Kamp #endif
420662cb04cSPoul-Henning Kamp     { "ast",	tZONE,     HOUR( 4) },	/* Atlantic Standard */
421662cb04cSPoul-Henning Kamp     { "adt",	tDAYZONE,  HOUR( 4) },	/* Atlantic Daylight */
422662cb04cSPoul-Henning Kamp     { "est",	tZONE,     HOUR( 5) },	/* Eastern Standard */
423662cb04cSPoul-Henning Kamp     { "edt",	tDAYZONE,  HOUR( 5) },	/* Eastern Daylight */
424662cb04cSPoul-Henning Kamp     { "cst",	tZONE,     HOUR( 6) },	/* Central Standard */
425662cb04cSPoul-Henning Kamp     { "cdt",	tDAYZONE,  HOUR( 6) },	/* Central Daylight */
426662cb04cSPoul-Henning Kamp     { "mst",	tZONE,     HOUR( 7) },	/* Mountain Standard */
427662cb04cSPoul-Henning Kamp     { "mdt",	tDAYZONE,  HOUR( 7) },	/* Mountain Daylight */
428662cb04cSPoul-Henning Kamp     { "pst",	tZONE,     HOUR( 8) },	/* Pacific Standard */
429662cb04cSPoul-Henning Kamp     { "pdt",	tDAYZONE,  HOUR( 8) },	/* Pacific Daylight */
430662cb04cSPoul-Henning Kamp     { "yst",	tZONE,     HOUR( 9) },	/* Yukon Standard */
431662cb04cSPoul-Henning Kamp     { "ydt",	tDAYZONE,  HOUR( 9) },	/* Yukon Daylight */
432662cb04cSPoul-Henning Kamp     { "hst",	tZONE,     HOUR(10) },	/* Hawaii Standard */
433662cb04cSPoul-Henning Kamp     { "hdt",	tDAYZONE,  HOUR(10) },	/* Hawaii Daylight */
434662cb04cSPoul-Henning Kamp     { "cat",	tZONE,     HOUR(10) },	/* Central Alaska */
435662cb04cSPoul-Henning Kamp     { "ahst",	tZONE,     HOUR(10) },	/* Alaska-Hawaii Standard */
436662cb04cSPoul-Henning Kamp     { "nt",	tZONE,     HOUR(11) },	/* Nome */
437662cb04cSPoul-Henning Kamp     { "idlw",	tZONE,     HOUR(12) },	/* International Date Line West */
438662cb04cSPoul-Henning Kamp     { "cet",	tZONE,     -HOUR(1) },	/* Central European */
439662cb04cSPoul-Henning Kamp     { "met",	tZONE,     -HOUR(1) },	/* Middle European */
440662cb04cSPoul-Henning Kamp     { "mewt",	tZONE,     -HOUR(1) },	/* Middle European Winter */
441662cb04cSPoul-Henning Kamp     { "mest",	tDAYZONE,  -HOUR(1) },	/* Middle European Summer */
442662cb04cSPoul-Henning Kamp     { "swt",	tZONE,     -HOUR(1) },	/* Swedish Winter */
443662cb04cSPoul-Henning Kamp     { "sst",	tDAYZONE,  -HOUR(1) },	/* Swedish Summer */
444662cb04cSPoul-Henning Kamp     { "fwt",	tZONE,     -HOUR(1) },	/* French Winter */
445662cb04cSPoul-Henning Kamp     { "fst",	tDAYZONE,  -HOUR(1) },	/* French Summer */
446662cb04cSPoul-Henning Kamp     { "eet",	tZONE,     -HOUR(2) },	/* Eastern Europe, USSR Zone 1 */
447662cb04cSPoul-Henning Kamp     { "bt",	tZONE,     -HOUR(3) },	/* Baghdad, USSR Zone 2 */
448662cb04cSPoul-Henning Kamp #if 0
449662cb04cSPoul-Henning Kamp     { "it",	tZONE,     -HOUR(3.5) },/* Iran */
450662cb04cSPoul-Henning Kamp #endif
451662cb04cSPoul-Henning Kamp     { "zp4",	tZONE,     -HOUR(4) },	/* USSR Zone 3 */
452662cb04cSPoul-Henning Kamp     { "zp5",	tZONE,     -HOUR(5) },	/* USSR Zone 4 */
453662cb04cSPoul-Henning Kamp #if 0
454662cb04cSPoul-Henning Kamp     { "ist",	tZONE,     -HOUR(5.5) },/* Indian Standard */
455662cb04cSPoul-Henning Kamp #endif
456662cb04cSPoul-Henning Kamp     { "zp6",	tZONE,     -HOUR(6) },	/* USSR Zone 5 */
457662cb04cSPoul-Henning Kamp #if	0
458662cb04cSPoul-Henning Kamp     /* For completeness.  NST is also Newfoundland Stanard, and SST is
459662cb04cSPoul-Henning Kamp      * also Swedish Summer. */
460662cb04cSPoul-Henning Kamp     { "nst",	tZONE,     -HOUR(6.5) },/* North Sumatra */
461662cb04cSPoul-Henning Kamp     { "sst",	tZONE,     -HOUR(7) },	/* South Sumatra, USSR Zone 6 */
462662cb04cSPoul-Henning Kamp #endif	/* 0 */
463662cb04cSPoul-Henning Kamp     { "wast",	tZONE,     -HOUR(7) },	/* West Australian Standard */
464662cb04cSPoul-Henning Kamp     { "wadt",	tDAYZONE,  -HOUR(7) },	/* West Australian Daylight */
465662cb04cSPoul-Henning Kamp #if 0
466662cb04cSPoul-Henning Kamp     { "jt",	tZONE,     -HOUR(7.5) },/* Java (3pm in Cronusland!) */
467662cb04cSPoul-Henning Kamp #endif
468662cb04cSPoul-Henning Kamp     { "cct",	tZONE,     -HOUR(8) },	/* China Coast, USSR Zone 7 */
469662cb04cSPoul-Henning Kamp     { "jst",	tZONE,     -HOUR(9) },	/* Japan Standard, USSR Zone 8 */
470662cb04cSPoul-Henning Kamp #if 0
471662cb04cSPoul-Henning Kamp     { "cast",	tZONE,     -HOUR(9.5) },/* Central Australian Standard */
472662cb04cSPoul-Henning Kamp     { "cadt",	tDAYZONE,  -HOUR(9.5) },/* Central Australian Daylight */
473662cb04cSPoul-Henning Kamp #endif
474662cb04cSPoul-Henning Kamp     { "east",	tZONE,     -HOUR(10) },	/* Eastern Australian Standard */
475662cb04cSPoul-Henning Kamp     { "eadt",	tDAYZONE,  -HOUR(10) },	/* Eastern Australian Daylight */
476662cb04cSPoul-Henning Kamp     { "gst",	tZONE,     -HOUR(10) },	/* Guam Standard, USSR Zone 9 */
477662cb04cSPoul-Henning Kamp     { "nzt",	tZONE,     -HOUR(12) },	/* New Zealand */
478662cb04cSPoul-Henning Kamp     { "nzst",	tZONE,     -HOUR(12) },	/* New Zealand Standard */
479662cb04cSPoul-Henning Kamp     { "nzdt",	tDAYZONE,  -HOUR(12) },	/* New Zealand Daylight */
480662cb04cSPoul-Henning Kamp     { "idle",	tZONE,     -HOUR(12) },	/* International Date Line East */
481662cb04cSPoul-Henning Kamp     {  NULL,	0,	   0 }
482662cb04cSPoul-Henning Kamp };
483662cb04cSPoul-Henning Kamp 
484662cb04cSPoul-Henning Kamp /* Military timezone table. */
485662cb04cSPoul-Henning Kamp static TABLE const MilitaryTable[] = {
486662cb04cSPoul-Henning Kamp     { "a",	tZONE,	HOUR(  1) },
487662cb04cSPoul-Henning Kamp     { "b",	tZONE,	HOUR(  2) },
488662cb04cSPoul-Henning Kamp     { "c",	tZONE,	HOUR(  3) },
489662cb04cSPoul-Henning Kamp     { "d",	tZONE,	HOUR(  4) },
490662cb04cSPoul-Henning Kamp     { "e",	tZONE,	HOUR(  5) },
491662cb04cSPoul-Henning Kamp     { "f",	tZONE,	HOUR(  6) },
492662cb04cSPoul-Henning Kamp     { "g",	tZONE,	HOUR(  7) },
493662cb04cSPoul-Henning Kamp     { "h",	tZONE,	HOUR(  8) },
494662cb04cSPoul-Henning Kamp     { "i",	tZONE,	HOUR(  9) },
495662cb04cSPoul-Henning Kamp     { "k",	tZONE,	HOUR( 10) },
496662cb04cSPoul-Henning Kamp     { "l",	tZONE,	HOUR( 11) },
497662cb04cSPoul-Henning Kamp     { "m",	tZONE,	HOUR( 12) },
498662cb04cSPoul-Henning Kamp     { "n",	tZONE,	HOUR(- 1) },
499662cb04cSPoul-Henning Kamp     { "o",	tZONE,	HOUR(- 2) },
500662cb04cSPoul-Henning Kamp     { "p",	tZONE,	HOUR(- 3) },
501662cb04cSPoul-Henning Kamp     { "q",	tZONE,	HOUR(- 4) },
502662cb04cSPoul-Henning Kamp     { "r",	tZONE,	HOUR(- 5) },
503662cb04cSPoul-Henning Kamp     { "s",	tZONE,	HOUR(- 6) },
504662cb04cSPoul-Henning Kamp     { "t",	tZONE,	HOUR(- 7) },
505662cb04cSPoul-Henning Kamp     { "u",	tZONE,	HOUR(- 8) },
506662cb04cSPoul-Henning Kamp     { "v",	tZONE,	HOUR(- 9) },
507662cb04cSPoul-Henning Kamp     { "w",	tZONE,	HOUR(-10) },
508662cb04cSPoul-Henning Kamp     { "x",	tZONE,	HOUR(-11) },
509662cb04cSPoul-Henning Kamp     { "y",	tZONE,	HOUR(-12) },
510662cb04cSPoul-Henning Kamp     { "z",	tZONE,	HOUR(  0) },
511662cb04cSPoul-Henning Kamp     { NULL,	0,  0 }
512662cb04cSPoul-Henning Kamp };
513662cb04cSPoul-Henning Kamp 
514662cb04cSPoul-Henning Kamp 
515662cb04cSPoul-Henning Kamp 
516662cb04cSPoul-Henning Kamp 
517662cb04cSPoul-Henning Kamp /* ARGSUSED */
518662cb04cSPoul-Henning Kamp static int
yyerror(const char * s __unused)519662cb04cSPoul-Henning Kamp yyerror(const char *s __unused)
520662cb04cSPoul-Henning Kamp {
521662cb04cSPoul-Henning Kamp   return 0;
522662cb04cSPoul-Henning Kamp }
523662cb04cSPoul-Henning Kamp 
524662cb04cSPoul-Henning Kamp 
525662cb04cSPoul-Henning Kamp static time_t
ToSeconds(time_t Hours,time_t Minutes,time_t Seconds,MERIDIAN Meridian)526662cb04cSPoul-Henning Kamp ToSeconds(time_t Hours, time_t Minutes, time_t Seconds, MERIDIAN Meridian)
527662cb04cSPoul-Henning Kamp {
528662cb04cSPoul-Henning Kamp     if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
529662cb04cSPoul-Henning Kamp 	return -1;
530662cb04cSPoul-Henning Kamp     switch (Meridian) {
531662cb04cSPoul-Henning Kamp     case MER24:
532662cb04cSPoul-Henning Kamp 	if (Hours < 0 || Hours > 23)
533662cb04cSPoul-Henning Kamp 	    return -1;
534662cb04cSPoul-Henning Kamp 	return (Hours * 60L + Minutes) * 60L + Seconds;
535662cb04cSPoul-Henning Kamp     case MERam:
536662cb04cSPoul-Henning Kamp 	if (Hours < 1 || Hours > 12)
537662cb04cSPoul-Henning Kamp 	    return -1;
538662cb04cSPoul-Henning Kamp 	if (Hours == 12)
539662cb04cSPoul-Henning Kamp 	    Hours = 0;
540662cb04cSPoul-Henning Kamp 	return (Hours * 60L + Minutes) * 60L + Seconds;
541662cb04cSPoul-Henning Kamp     case MERpm:
542662cb04cSPoul-Henning Kamp 	if (Hours < 1 || Hours > 12)
543662cb04cSPoul-Henning Kamp 	    return -1;
544662cb04cSPoul-Henning Kamp 	if (Hours == 12)
545662cb04cSPoul-Henning Kamp 	    Hours = 0;
546662cb04cSPoul-Henning Kamp 	return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
547662cb04cSPoul-Henning Kamp     default:
548662cb04cSPoul-Henning Kamp 	abort ();
549662cb04cSPoul-Henning Kamp     }
550662cb04cSPoul-Henning Kamp     /* NOTREACHED */
551662cb04cSPoul-Henning Kamp }
552662cb04cSPoul-Henning Kamp 
553662cb04cSPoul-Henning Kamp 
554662cb04cSPoul-Henning Kamp /* Year is either
555662cb04cSPoul-Henning Kamp    * A negative number, which means to use its absolute value (why?)
556662cb04cSPoul-Henning Kamp    * A number from 0 to 99, which means a year from 1900 to 1999, or
557662cb04cSPoul-Henning Kamp    * The actual year (>=100).  */
558662cb04cSPoul-Henning Kamp static time_t
Convert(time_t Month,time_t Day,time_t Year,time_t Hours,time_t Minutes,time_t Seconds,MERIDIAN Meridian,DSTMODE DSTmode)559662cb04cSPoul-Henning Kamp Convert(time_t Month, time_t Day, time_t Year,
560662cb04cSPoul-Henning Kamp     time_t Hours, time_t Minutes, time_t Seconds,
561662cb04cSPoul-Henning Kamp     MERIDIAN Meridian, DSTMODE DSTmode)
562662cb04cSPoul-Henning Kamp {
563662cb04cSPoul-Henning Kamp     static int DaysInMonth[12] = {
564662cb04cSPoul-Henning Kamp 	31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
565662cb04cSPoul-Henning Kamp     };
566662cb04cSPoul-Henning Kamp     time_t	tod;
567662cb04cSPoul-Henning Kamp     time_t	Julian;
568662cb04cSPoul-Henning Kamp     int		i;
569662cb04cSPoul-Henning Kamp     struct tm   *ltm;
570662cb04cSPoul-Henning Kamp 
571662cb04cSPoul-Henning Kamp     if (Year < 0)
572662cb04cSPoul-Henning Kamp 	Year = -Year;
573662cb04cSPoul-Henning Kamp     if (Year < 69)
574662cb04cSPoul-Henning Kamp 	Year += 2000;
575662cb04cSPoul-Henning Kamp     else if (Year < 100)
576662cb04cSPoul-Henning Kamp 	Year += 1900;
577662cb04cSPoul-Henning Kamp     DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
578662cb04cSPoul-Henning Kamp 		    ? 29 : 28;
579662cb04cSPoul-Henning Kamp     /* Checking for 2038 bogusly assumes that time_t is 32 bits.  But
580662cb04cSPoul-Henning Kamp        I'm too lazy to try to check for time_t overflow in another way.  */
581662cb04cSPoul-Henning Kamp     if (Year < EPOCH || Year > 2038
582662cb04cSPoul-Henning Kamp      || Month < 1 || Month > 12
583662cb04cSPoul-Henning Kamp      /* Lint fluff:  "conversion from long may lose accuracy" */
584662cb04cSPoul-Henning Kamp      || Day < 1 || Day > DaysInMonth[(int)--Month])
585662cb04cSPoul-Henning Kamp 	/* FIXME:
586662cb04cSPoul-Henning Kamp 	 * It would be nice to set a global error string here.
587662cb04cSPoul-Henning Kamp 	 * "February 30 is not a valid date" is much more informative than
588662cb04cSPoul-Henning Kamp 	 * "Can't parse date/time: 100 months" when the user input was
589662cb04cSPoul-Henning Kamp 	 * "100 months" and addition resolved that to February 30, for
590662cb04cSPoul-Henning Kamp 	 * example.  See rcs2-7 in src/sanity.sh for more. */
591662cb04cSPoul-Henning Kamp 	return -1;
592662cb04cSPoul-Henning Kamp 
593662cb04cSPoul-Henning Kamp     for (Julian = Day - 1, i = 0; i < Month; i++)
594662cb04cSPoul-Henning Kamp 	Julian += DaysInMonth[i];
595662cb04cSPoul-Henning Kamp     for (i = EPOCH; i < Year; i++)
596662cb04cSPoul-Henning Kamp 	Julian += 365 + (i % 4 == 0);
597662cb04cSPoul-Henning Kamp     Julian *= SECSPERDAY;
598662cb04cSPoul-Henning Kamp     Julian += yyTimezone * 60L;
599662cb04cSPoul-Henning Kamp     if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
600662cb04cSPoul-Henning Kamp 	return -1;
601662cb04cSPoul-Henning Kamp     Julian += tod;
602662cb04cSPoul-Henning Kamp     ltm = localtime(&Julian);
603662cb04cSPoul-Henning Kamp fprintf(stderr, "DST %d TZ %s %d\n", DSTmode, ltm->tm_zone, ltm->tm_isdst);
604662cb04cSPoul-Henning Kamp     if (DSTmode == DSTon
605662cb04cSPoul-Henning Kamp      || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
606662cb04cSPoul-Henning Kamp 	Julian -= 60 * 60;
607662cb04cSPoul-Henning Kamp     return Julian;
608662cb04cSPoul-Henning Kamp }
609662cb04cSPoul-Henning Kamp 
610662cb04cSPoul-Henning Kamp 
611662cb04cSPoul-Henning Kamp static time_t
DSTcorrect(time_t Start,time_t Future)612662cb04cSPoul-Henning Kamp DSTcorrect(time_t Start, time_t Future)
613662cb04cSPoul-Henning Kamp {
614662cb04cSPoul-Henning Kamp     time_t	StartDay;
615662cb04cSPoul-Henning Kamp     time_t	FutureDay;
616662cb04cSPoul-Henning Kamp 
617662cb04cSPoul-Henning Kamp     StartDay = (localtime(&Start)->tm_hour + 1) % 24;
618662cb04cSPoul-Henning Kamp     FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
619662cb04cSPoul-Henning Kamp     return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
620662cb04cSPoul-Henning Kamp }
621662cb04cSPoul-Henning Kamp 
622662cb04cSPoul-Henning Kamp 
623662cb04cSPoul-Henning Kamp static time_t
RelativeDate(time_t Start,time_t DayOrdinal,time_t DayNumber)624662cb04cSPoul-Henning Kamp RelativeDate(time_t Start, time_t DayOrdinal, time_t DayNumber)
625662cb04cSPoul-Henning Kamp {
626662cb04cSPoul-Henning Kamp     struct tm	*tm;
627662cb04cSPoul-Henning Kamp     time_t	now;
628662cb04cSPoul-Henning Kamp 
629662cb04cSPoul-Henning Kamp     now = Start;
630662cb04cSPoul-Henning Kamp     tm = localtime(&now);
631662cb04cSPoul-Henning Kamp     now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
632662cb04cSPoul-Henning Kamp     now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
633662cb04cSPoul-Henning Kamp     return DSTcorrect(Start, now);
634662cb04cSPoul-Henning Kamp }
635662cb04cSPoul-Henning Kamp 
636662cb04cSPoul-Henning Kamp 
637662cb04cSPoul-Henning Kamp static time_t
RelativeMonth(time_t Start,time_t RelMonth)638662cb04cSPoul-Henning Kamp RelativeMonth(time_t Start, time_t RelMonth)
639662cb04cSPoul-Henning Kamp {
640662cb04cSPoul-Henning Kamp     struct tm	*tm;
641662cb04cSPoul-Henning Kamp     time_t	Month;
642662cb04cSPoul-Henning Kamp     time_t	Year;
643662cb04cSPoul-Henning Kamp 
644662cb04cSPoul-Henning Kamp     if (RelMonth == 0)
645662cb04cSPoul-Henning Kamp 	return 0;
646662cb04cSPoul-Henning Kamp     tm = localtime(&Start);
647662cb04cSPoul-Henning Kamp     Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
648662cb04cSPoul-Henning Kamp     Year = Month / 12;
649662cb04cSPoul-Henning Kamp     Month = Month % 12 + 1;
650662cb04cSPoul-Henning Kamp     return DSTcorrect(Start,
651662cb04cSPoul-Henning Kamp 	    Convert(Month, (time_t)tm->tm_mday, Year,
652662cb04cSPoul-Henning Kamp 		(time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
653662cb04cSPoul-Henning Kamp 		MER24, DSTmaybe));
654662cb04cSPoul-Henning Kamp }
655662cb04cSPoul-Henning Kamp 
656662cb04cSPoul-Henning Kamp 
657662cb04cSPoul-Henning Kamp static int
LookupWord(char * buff)658662cb04cSPoul-Henning Kamp LookupWord(char *buff)
659662cb04cSPoul-Henning Kamp {
660662cb04cSPoul-Henning Kamp     char	*p;
661662cb04cSPoul-Henning Kamp     char	*q;
662662cb04cSPoul-Henning Kamp     const TABLE	*tp;
663662cb04cSPoul-Henning Kamp     int			i;
664662cb04cSPoul-Henning Kamp     int			abbrev;
665662cb04cSPoul-Henning Kamp 
666662cb04cSPoul-Henning Kamp     /* Make it lowercase. */
667662cb04cSPoul-Henning Kamp     for (p = buff; *p; p++)
668662cb04cSPoul-Henning Kamp 	if (isupper(*p))
669662cb04cSPoul-Henning Kamp 	    *p = tolower(*p);
670662cb04cSPoul-Henning Kamp 
671662cb04cSPoul-Henning Kamp     if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
672662cb04cSPoul-Henning Kamp 	yylval.Meridian = MERam;
673662cb04cSPoul-Henning Kamp 	return tMERIDIAN;
674662cb04cSPoul-Henning Kamp     }
675662cb04cSPoul-Henning Kamp     if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
676662cb04cSPoul-Henning Kamp 	yylval.Meridian = MERpm;
677662cb04cSPoul-Henning Kamp 	return tMERIDIAN;
678662cb04cSPoul-Henning Kamp     }
679662cb04cSPoul-Henning Kamp 
680662cb04cSPoul-Henning Kamp     /* See if we have an abbreviation for a month. */
681662cb04cSPoul-Henning Kamp     if (strlen(buff) == 3)
682662cb04cSPoul-Henning Kamp 	abbrev = 1;
683662cb04cSPoul-Henning Kamp     else if (strlen(buff) == 4 && buff[3] == '.') {
684662cb04cSPoul-Henning Kamp 	abbrev = 1;
685662cb04cSPoul-Henning Kamp 	buff[3] = '\0';
686662cb04cSPoul-Henning Kamp     }
687662cb04cSPoul-Henning Kamp     else
688662cb04cSPoul-Henning Kamp 	abbrev = 0;
689662cb04cSPoul-Henning Kamp 
690662cb04cSPoul-Henning Kamp     for (tp = MonthDayTable; tp->name; tp++) {
691662cb04cSPoul-Henning Kamp 	if (abbrev) {
692662cb04cSPoul-Henning Kamp 	    if (strncmp(buff, tp->name, 3) == 0) {
693662cb04cSPoul-Henning Kamp 		yylval.Number = tp->value;
694662cb04cSPoul-Henning Kamp 		return tp->type;
695662cb04cSPoul-Henning Kamp 	    }
696662cb04cSPoul-Henning Kamp 	}
697662cb04cSPoul-Henning Kamp 	else if (strcmp(buff, tp->name) == 0) {
698662cb04cSPoul-Henning Kamp 	    yylval.Number = tp->value;
699662cb04cSPoul-Henning Kamp 	    return tp->type;
700662cb04cSPoul-Henning Kamp 	}
701662cb04cSPoul-Henning Kamp     }
702662cb04cSPoul-Henning Kamp 
703662cb04cSPoul-Henning Kamp     for (tp = TimezoneTable; tp->name; tp++)
704662cb04cSPoul-Henning Kamp 	if (strcmp(buff, tp->name) == 0) {
705662cb04cSPoul-Henning Kamp 	    yylval.Number = tp->value;
706662cb04cSPoul-Henning Kamp 	    return tp->type;
707662cb04cSPoul-Henning Kamp 	}
708662cb04cSPoul-Henning Kamp 
709662cb04cSPoul-Henning Kamp     if (strcmp(buff, "dst") == 0)
710662cb04cSPoul-Henning Kamp 	return tDST;
711662cb04cSPoul-Henning Kamp 
712662cb04cSPoul-Henning Kamp     for (tp = UnitsTable; tp->name; tp++)
713662cb04cSPoul-Henning Kamp 	if (strcmp(buff, tp->name) == 0) {
714662cb04cSPoul-Henning Kamp 	    yylval.Number = tp->value;
715662cb04cSPoul-Henning Kamp 	    return tp->type;
716662cb04cSPoul-Henning Kamp 	}
717662cb04cSPoul-Henning Kamp 
718662cb04cSPoul-Henning Kamp     /* Strip off any plural and try the units table again. */
719662cb04cSPoul-Henning Kamp     i = strlen(buff) - 1;
720662cb04cSPoul-Henning Kamp     if (buff[i] == 's') {
721662cb04cSPoul-Henning Kamp 	buff[i] = '\0';
722662cb04cSPoul-Henning Kamp 	for (tp = UnitsTable; tp->name; tp++)
723662cb04cSPoul-Henning Kamp 	    if (strcmp(buff, tp->name) == 0) {
724662cb04cSPoul-Henning Kamp 		yylval.Number = tp->value;
725662cb04cSPoul-Henning Kamp 		return tp->type;
726662cb04cSPoul-Henning Kamp 	    }
727662cb04cSPoul-Henning Kamp 	buff[i] = 's';		/* Put back for "this" in OtherTable. */
728662cb04cSPoul-Henning Kamp     }
729662cb04cSPoul-Henning Kamp 
730662cb04cSPoul-Henning Kamp     for (tp = OtherTable; tp->name; tp++)
731662cb04cSPoul-Henning Kamp 	if (strcmp(buff, tp->name) == 0) {
732662cb04cSPoul-Henning Kamp 	    yylval.Number = tp->value;
733662cb04cSPoul-Henning Kamp 	    return tp->type;
734662cb04cSPoul-Henning Kamp 	}
735662cb04cSPoul-Henning Kamp 
736662cb04cSPoul-Henning Kamp     /* Military timezones. */
737662cb04cSPoul-Henning Kamp     if (buff[1] == '\0' && isalpha(*buff)) {
738662cb04cSPoul-Henning Kamp 	for (tp = MilitaryTable; tp->name; tp++)
739662cb04cSPoul-Henning Kamp 	    if (strcmp(buff, tp->name) == 0) {
740662cb04cSPoul-Henning Kamp 		yylval.Number = tp->value;
741662cb04cSPoul-Henning Kamp 		return tp->type;
742662cb04cSPoul-Henning Kamp 	    }
743662cb04cSPoul-Henning Kamp     }
744662cb04cSPoul-Henning Kamp 
745662cb04cSPoul-Henning Kamp     /* Drop out any periods and try the timezone table again. */
746662cb04cSPoul-Henning Kamp     for (i = 0, p = q = buff; *q; q++)
747662cb04cSPoul-Henning Kamp 	if (*q != '.')
748662cb04cSPoul-Henning Kamp 	    *p++ = *q;
749662cb04cSPoul-Henning Kamp 	else
750662cb04cSPoul-Henning Kamp 	    i++;
751662cb04cSPoul-Henning Kamp     *p = '\0';
752662cb04cSPoul-Henning Kamp     if (i)
753662cb04cSPoul-Henning Kamp 	for (tp = TimezoneTable; tp->name; tp++)
754662cb04cSPoul-Henning Kamp 	    if (strcmp(buff, tp->name) == 0) {
755662cb04cSPoul-Henning Kamp 		yylval.Number = tp->value;
756662cb04cSPoul-Henning Kamp 		return tp->type;
757662cb04cSPoul-Henning Kamp 	    }
758662cb04cSPoul-Henning Kamp 
759662cb04cSPoul-Henning Kamp     return tID;
760662cb04cSPoul-Henning Kamp }
761662cb04cSPoul-Henning Kamp 
762662cb04cSPoul-Henning Kamp 
763662cb04cSPoul-Henning Kamp static int
yylex(void)76410bc3a7fSEd Schouten yylex(void)
765662cb04cSPoul-Henning Kamp {
766662cb04cSPoul-Henning Kamp     char	c;
767662cb04cSPoul-Henning Kamp     char	*p;
768662cb04cSPoul-Henning Kamp     char		buff[20];
769662cb04cSPoul-Henning Kamp     int			Count;
770662cb04cSPoul-Henning Kamp     int			sign;
771662cb04cSPoul-Henning Kamp 
772662cb04cSPoul-Henning Kamp     for ( ; ; ) {
773662cb04cSPoul-Henning Kamp 	while (isspace(*yyInput))
774662cb04cSPoul-Henning Kamp 	    yyInput++;
775662cb04cSPoul-Henning Kamp 
776662cb04cSPoul-Henning Kamp 	if (isdigit(c = *yyInput) || c == '-' || c == '+') {
777662cb04cSPoul-Henning Kamp 	    if (c == '-' || c == '+') {
778662cb04cSPoul-Henning Kamp 		sign = c == '-' ? -1 : 1;
779662cb04cSPoul-Henning Kamp 		if (!isdigit(*++yyInput))
780662cb04cSPoul-Henning Kamp 		    /* skip the '-' sign */
781662cb04cSPoul-Henning Kamp 		    continue;
782662cb04cSPoul-Henning Kamp 	    }
783662cb04cSPoul-Henning Kamp 	    else
784662cb04cSPoul-Henning Kamp 		sign = 0;
785662cb04cSPoul-Henning Kamp 	    for (yylval.Number = 0; isdigit(c = *yyInput++); )
786662cb04cSPoul-Henning Kamp 		yylval.Number = 10 * yylval.Number + c - '0';
787662cb04cSPoul-Henning Kamp 	    yyInput--;
788662cb04cSPoul-Henning Kamp 	    if (sign < 0)
789662cb04cSPoul-Henning Kamp 		yylval.Number = -yylval.Number;
790662cb04cSPoul-Henning Kamp 	    return sign ? tSNUMBER : tUNUMBER;
791662cb04cSPoul-Henning Kamp 	}
792662cb04cSPoul-Henning Kamp 	if (isalpha(c)) {
793662cb04cSPoul-Henning Kamp 	    for (p = buff; isalpha(c = *yyInput++) || c == '.'; )
794662cb04cSPoul-Henning Kamp 		if (p < &buff[sizeof buff - 1])
795662cb04cSPoul-Henning Kamp 		    *p++ = c;
796662cb04cSPoul-Henning Kamp 	    *p = '\0';
797662cb04cSPoul-Henning Kamp 	    yyInput--;
798662cb04cSPoul-Henning Kamp 	    return LookupWord(buff);
799662cb04cSPoul-Henning Kamp 	}
800662cb04cSPoul-Henning Kamp 	if (c != '(')
801662cb04cSPoul-Henning Kamp 	    return *yyInput++;
802662cb04cSPoul-Henning Kamp 	Count = 0;
803662cb04cSPoul-Henning Kamp 	do {
804662cb04cSPoul-Henning Kamp 	    c = *yyInput++;
805662cb04cSPoul-Henning Kamp 	    if (c == '\0')
806662cb04cSPoul-Henning Kamp 		return c;
807662cb04cSPoul-Henning Kamp 	    if (c == '(')
808662cb04cSPoul-Henning Kamp 		Count++;
809662cb04cSPoul-Henning Kamp 	    else if (c == ')')
810662cb04cSPoul-Henning Kamp 		Count--;
811662cb04cSPoul-Henning Kamp 	} while (Count > 0);
812662cb04cSPoul-Henning Kamp     }
813662cb04cSPoul-Henning Kamp }
814662cb04cSPoul-Henning Kamp 
815662cb04cSPoul-Henning Kamp #define TM_YEAR_ORIGIN 1900
816662cb04cSPoul-Henning Kamp 
817662cb04cSPoul-Henning Kamp time_t
get_date(char * p)818662cb04cSPoul-Henning Kamp get_date(char *p)
819662cb04cSPoul-Henning Kamp {
820*4a0b57b0SJohn Baldwin     struct tm		*tm;
821662cb04cSPoul-Henning Kamp     time_t		Start;
822662cb04cSPoul-Henning Kamp     time_t		tod;
823662cb04cSPoul-Henning Kamp     time_t nowtime;
824662cb04cSPoul-Henning Kamp 
825662cb04cSPoul-Henning Kamp     yyInput = p;
826662cb04cSPoul-Henning Kamp 
827662cb04cSPoul-Henning Kamp     (void)time (&nowtime);
828662cb04cSPoul-Henning Kamp 
829662cb04cSPoul-Henning Kamp     if (! (tm = localtime (&nowtime)))
830662cb04cSPoul-Henning Kamp 	return -1;
831662cb04cSPoul-Henning Kamp 
832662cb04cSPoul-Henning Kamp     yyYear = tm->tm_year + 1900;
833662cb04cSPoul-Henning Kamp     yyMonth = tm->tm_mon + 1;
834662cb04cSPoul-Henning Kamp     yyDay = tm->tm_mday;
835662cb04cSPoul-Henning Kamp     yyTimezone = tm->tm_gmtoff;
836662cb04cSPoul-Henning Kamp     yyDSTmode = DSTmaybe;
837662cb04cSPoul-Henning Kamp     yyHour = 0;
838662cb04cSPoul-Henning Kamp     yyMinutes = 0;
839662cb04cSPoul-Henning Kamp     yySeconds = 0;
840662cb04cSPoul-Henning Kamp     yyMeridian = MER24;
841662cb04cSPoul-Henning Kamp     yyRelSeconds = 0;
842662cb04cSPoul-Henning Kamp     yyRelMonth = 0;
843662cb04cSPoul-Henning Kamp     yyHaveDate = 0;
844662cb04cSPoul-Henning Kamp     yyHaveDay = 0;
845662cb04cSPoul-Henning Kamp     yyHaveRel = 0;
846662cb04cSPoul-Henning Kamp     yyHaveTime = 0;
847662cb04cSPoul-Henning Kamp     yyHaveZone = 0;
848662cb04cSPoul-Henning Kamp 
849662cb04cSPoul-Henning Kamp     if (yyparse()
850662cb04cSPoul-Henning Kamp      || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
851662cb04cSPoul-Henning Kamp 	return -1;
852662cb04cSPoul-Henning Kamp 
853662cb04cSPoul-Henning Kamp     if (yyHaveDate || yyHaveTime || yyHaveDay) {
854662cb04cSPoul-Henning Kamp 	Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
855662cb04cSPoul-Henning Kamp 		    yyMeridian, yyDSTmode);
856662cb04cSPoul-Henning Kamp 	if (Start < 0)
857662cb04cSPoul-Henning Kamp 	    return -1;
858662cb04cSPoul-Henning Kamp     }
859662cb04cSPoul-Henning Kamp     else {
860662cb04cSPoul-Henning Kamp 	Start = nowtime;
861662cb04cSPoul-Henning Kamp 	if (!yyHaveRel)
862662cb04cSPoul-Henning Kamp 	    Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
863662cb04cSPoul-Henning Kamp     }
864662cb04cSPoul-Henning Kamp 
865662cb04cSPoul-Henning Kamp     Start += yyRelSeconds;
866662cb04cSPoul-Henning Kamp     Start += RelativeMonth(Start, yyRelMonth);
867662cb04cSPoul-Henning Kamp 
868662cb04cSPoul-Henning Kamp     if (yyHaveDay && !yyHaveDate) {
869662cb04cSPoul-Henning Kamp 	tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
870662cb04cSPoul-Henning Kamp 	Start += tod;
871662cb04cSPoul-Henning Kamp     }
872662cb04cSPoul-Henning Kamp 
873662cb04cSPoul-Henning Kamp     /* Have to do *something* with a legitimate -1 so it's distinguishable
874662cb04cSPoul-Henning Kamp      * from the error return value.  (Alternately could set errno on error.) */
875662cb04cSPoul-Henning Kamp     return Start == -1 ? 0 : Start;
876662cb04cSPoul-Henning Kamp }
877