parsedate.y revision 1.17 1 1.1 christos %{
2 1.1 christos /*
3 1.1 christos ** Originally written by Steven M. Bellovin <smb (at) research.att.com> while
4 1.1 christos ** at the University of North Carolina at Chapel Hill. Later tweaked by
5 1.1 christos ** a couple of people on Usenet. Completely overhauled by Rich $alz
6 1.1 christos ** <rsalz (at) bbn.com> and Jim Berets <jberets (at) bbn.com> in August, 1990;
7 1.1 christos **
8 1.1 christos ** This grammar has 10 shift/reduce conflicts.
9 1.1 christos **
10 1.1 christos ** This code is in the public domain and has no copyright.
11 1.1 christos */
12 1.1 christos /* SUPPRESS 287 on yaccpar_sccsid *//* Unused static variable */
13 1.1 christos /* SUPPRESS 288 on yyerrlab *//* Label unused */
14 1.1 christos
15 1.15 yamt #include <sys/cdefs.h>
16 1.15 yamt #ifdef __RCSID
17 1.17 apb __RCSID("$NetBSD: parsedate.y,v 1.17 2014/10/07 22:27:14 apb Exp $");
18 1.15 yamt #endif
19 1.15 yamt
20 1.1 christos #include <stdio.h>
21 1.1 christos #include <ctype.h>
22 1.14 apb #include <errno.h>
23 1.1 christos #include <string.h>
24 1.1 christos #include <time.h>
25 1.1 christos #include <util.h>
26 1.3 drochner #include <stdlib.h>
27 1.1 christos
28 1.1 christos /* NOTES on rebuilding parsedate.c (particularly for inclusion in CVS
29 1.1 christos releases):
30 1.1 christos
31 1.1 christos We don't want to mess with all the portability hassles of alloca.
32 1.1 christos In particular, most (all?) versions of bison will use alloca in
33 1.1 christos their parser. If bison works on your system (e.g. it should work
34 1.1 christos with gcc), then go ahead and use it, but the more general solution
35 1.1 christos is to use byacc instead of bison, which should generate a portable
36 1.1 christos parser. I played with adding "#define alloca dont_use_alloca", to
37 1.1 christos give an error if the parser generator uses alloca (and thus detect
38 1.1 christos unportable parsedate.c's), but that seems to cause as many problems
39 1.1 christos as it solves. */
40 1.1 christos
41 1.1 christos #define EPOCH 1970
42 1.1 christos #define HOUR(x) ((time_t)(x) * 60)
43 1.1 christos #define SECSPERDAY (24L * 60L * 60L)
44 1.1 christos
45 1.1 christos
46 1.1 christos /*
47 1.1 christos ** An entry in the lexical lookup table.
48 1.1 christos */
49 1.1 christos typedef struct _TABLE {
50 1.1 christos const char *name;
51 1.1 christos int type;
52 1.1 christos time_t value;
53 1.1 christos } TABLE;
54 1.1 christos
55 1.1 christos
56 1.1 christos /*
57 1.1 christos ** Daylight-savings mode: on, off, or not yet known.
58 1.1 christos */
59 1.1 christos typedef enum _DSTMODE {
60 1.1 christos DSTon, DSToff, DSTmaybe
61 1.1 christos } DSTMODE;
62 1.1 christos
63 1.1 christos /*
64 1.1 christos ** Meridian: am, pm, or 24-hour style.
65 1.1 christos */
66 1.1 christos typedef enum _MERIDIAN {
67 1.1 christos MERam, MERpm, MER24
68 1.1 christos } MERIDIAN;
69 1.1 christos
70 1.1 christos
71 1.9 christos struct dateinfo {
72 1.17 apb DSTMODE yyDSTmode; /* DST on/off/maybe */
73 1.9 christos time_t yyDayOrdinal;
74 1.9 christos time_t yyDayNumber;
75 1.9 christos int yyHaveDate;
76 1.17 apb int yyHaveFullYear; /* if true, year is not abbreviated. */
77 1.17 apb /* if false, need to call AdjustYear(). */
78 1.9 christos int yyHaveDay;
79 1.9 christos int yyHaveRel;
80 1.9 christos int yyHaveTime;
81 1.9 christos int yyHaveZone;
82 1.17 apb time_t yyTimezone; /* Timezone as minutes ahead/east of UTC */
83 1.17 apb time_t yyDay; /* Day of month [1-31] */
84 1.17 apb time_t yyHour; /* Hour of day [0-24] or [1-12] */
85 1.17 apb time_t yyMinutes; /* Minute of hour [0-59] */
86 1.17 apb time_t yyMonth; /* Month of year [1-12] */
87 1.17 apb time_t yySeconds; /* Second of minute [0-60] */
88 1.17 apb time_t yyYear; /* Year, see also yyHaveFullYear */
89 1.17 apb MERIDIAN yyMeridian; /* Interpret yyHour as AM/PM/24 hour clock */
90 1.9 christos time_t yyRelMonth;
91 1.9 christos time_t yyRelSeconds;
92 1.9 christos };
93 1.1 christos %}
94 1.1 christos
95 1.1 christos %union {
96 1.1 christos time_t Number;
97 1.1 christos enum _MERIDIAN Meridian;
98 1.1 christos }
99 1.1 christos
100 1.1 christos %token tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
101 1.5 tron %token tSEC_UNIT tSNUMBER tUNUMBER tZONE tDST AT_SIGN
102 1.1 christos
103 1.1 christos %type <Number> tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
104 1.1 christos %type <Number> tSEC_UNIT tSNUMBER tUNUMBER tZONE
105 1.1 christos %type <Meridian> tMERIDIAN o_merid
106 1.1 christos
107 1.9 christos %parse-param { struct dateinfo *param }
108 1.9 christos %parse-param { const char **yyInput }
109 1.9 christos %lex-param { const char **yyInput }
110 1.9 christos %pure-parser
111 1.9 christos
112 1.1 christos %%
113 1.1 christos
114 1.1 christos spec : /* NULL */
115 1.1 christos | spec item
116 1.1 christos ;
117 1.1 christos
118 1.1 christos item : time {
119 1.9 christos param->yyHaveTime++;
120 1.1 christos }
121 1.1 christos | zone {
122 1.9 christos param->yyHaveZone++;
123 1.1 christos }
124 1.1 christos | date {
125 1.9 christos param->yyHaveDate++;
126 1.1 christos }
127 1.1 christos | day {
128 1.9 christos param->yyHaveDay++;
129 1.1 christos }
130 1.1 christos | rel {
131 1.9 christos param->yyHaveRel++;
132 1.1 christos }
133 1.1 christos | cvsstamp {
134 1.9 christos param->yyHaveTime++;
135 1.9 christos param->yyHaveDate++;
136 1.9 christos param->yyHaveZone++;
137 1.1 christos }
138 1.5 tron | epochdate {
139 1.9 christos param->yyHaveTime++;
140 1.9 christos param->yyHaveDate++;
141 1.9 christos param->yyHaveZone++;
142 1.5 tron }
143 1.1 christos | number
144 1.1 christos ;
145 1.1 christos
146 1.1 christos cvsstamp: tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER {
147 1.9 christos param->yyYear = $1;
148 1.9 christos if (param->yyYear < 100) param->yyYear += 1900;
149 1.17 apb param->yyHaveFullYear = 1;
150 1.9 christos param->yyMonth = $3;
151 1.9 christos param->yyDay = $5;
152 1.9 christos param->yyHour = $7;
153 1.9 christos param->yyMinutes = $9;
154 1.9 christos param->yySeconds = $11;
155 1.9 christos param->yyDSTmode = DSToff;
156 1.9 christos param->yyTimezone = 0;
157 1.1 christos }
158 1.1 christos ;
159 1.1 christos
160 1.14 apb epochdate: AT_SIGN at_number {
161 1.14 apb time_t when = $<Number>2;
162 1.5 tron struct tm tmbuf;
163 1.5 tron if (gmtime_r(&when, &tmbuf) != NULL) {
164 1.9 christos param->yyYear = tmbuf.tm_year + 1900;
165 1.9 christos param->yyMonth = tmbuf.tm_mon + 1;
166 1.9 christos param->yyDay = tmbuf.tm_mday;
167 1.9 christos
168 1.9 christos param->yyHour = tmbuf.tm_hour;
169 1.9 christos param->yyMinutes = tmbuf.tm_min;
170 1.9 christos param->yySeconds = tmbuf.tm_sec;
171 1.5 tron } else {
172 1.9 christos param->yyYear = EPOCH;
173 1.9 christos param->yyMonth = 1;
174 1.9 christos param->yyDay = 1;
175 1.9 christos
176 1.9 christos param->yyHour = 0;
177 1.9 christos param->yyMinutes = 0;
178 1.9 christos param->yySeconds = 0;
179 1.5 tron }
180 1.17 apb param->yyHaveFullYear = 1;
181 1.9 christos param->yyDSTmode = DSToff;
182 1.9 christos param->yyTimezone = 0;
183 1.5 tron }
184 1.5 tron ;
185 1.5 tron
186 1.14 apb at_number : tUNUMBER | tSNUMBER ;
187 1.14 apb
188 1.1 christos time : tUNUMBER tMERIDIAN {
189 1.9 christos param->yyHour = $1;
190 1.9 christos param->yyMinutes = 0;
191 1.9 christos param->yySeconds = 0;
192 1.9 christos param->yyMeridian = $2;
193 1.1 christos }
194 1.1 christos | tUNUMBER ':' tUNUMBER o_merid {
195 1.9 christos param->yyHour = $1;
196 1.9 christos param->yyMinutes = $3;
197 1.9 christos param->yySeconds = 0;
198 1.9 christos param->yyMeridian = $4;
199 1.1 christos }
200 1.1 christos | tUNUMBER ':' tUNUMBER tSNUMBER {
201 1.9 christos param->yyHour = $1;
202 1.9 christos param->yyMinutes = $3;
203 1.9 christos param->yyMeridian = MER24;
204 1.9 christos param->yyDSTmode = DSToff;
205 1.9 christos param->yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
206 1.1 christos }
207 1.1 christos | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
208 1.9 christos param->yyHour = $1;
209 1.9 christos param->yyMinutes = $3;
210 1.9 christos param->yySeconds = $5;
211 1.9 christos param->yyMeridian = $6;
212 1.1 christos }
213 1.1 christos | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
214 1.9 christos param->yyHour = $1;
215 1.9 christos param->yyMinutes = $3;
216 1.9 christos param->yySeconds = $5;
217 1.9 christos param->yyMeridian = MER24;
218 1.9 christos param->yyDSTmode = DSToff;
219 1.9 christos param->yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
220 1.1 christos }
221 1.7 christos | tUNUMBER ':' tUNUMBER ':' tUNUMBER '.' tUNUMBER {
222 1.9 christos param->yyHour = $1;
223 1.9 christos param->yyMinutes = $3;
224 1.9 christos param->yySeconds = $5;
225 1.9 christos param->yyMeridian = MER24;
226 1.9 christos param->yyDSTmode = DSToff;
227 1.7 christos /* XXX: Do nothing with millis */
228 1.9 christos /* param->yyTimezone = ($7 % 100 + ($7 / 100) * 60); */
229 1.7 christos }
230 1.1 christos ;
231 1.1 christos
232 1.1 christos zone : tZONE {
233 1.9 christos param->yyTimezone = $1;
234 1.9 christos param->yyDSTmode = DSToff;
235 1.1 christos }
236 1.1 christos | tDAYZONE {
237 1.9 christos param->yyTimezone = $1;
238 1.9 christos param->yyDSTmode = DSTon;
239 1.1 christos }
240 1.1 christos |
241 1.1 christos tZONE tDST {
242 1.9 christos param->yyTimezone = $1;
243 1.9 christos param->yyDSTmode = DSTon;
244 1.1 christos }
245 1.1 christos ;
246 1.1 christos
247 1.1 christos day : tDAY {
248 1.9 christos param->yyDayOrdinal = 1;
249 1.9 christos param->yyDayNumber = $1;
250 1.1 christos }
251 1.1 christos | tDAY ',' {
252 1.9 christos param->yyDayOrdinal = 1;
253 1.9 christos param->yyDayNumber = $1;
254 1.1 christos }
255 1.1 christos | tUNUMBER tDAY {
256 1.9 christos param->yyDayOrdinal = $1;
257 1.9 christos param->yyDayNumber = $2;
258 1.1 christos }
259 1.1 christos ;
260 1.1 christos
261 1.1 christos date : tUNUMBER '/' tUNUMBER {
262 1.9 christos param->yyMonth = $1;
263 1.9 christos param->yyDay = $3;
264 1.1 christos }
265 1.1 christos | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
266 1.1 christos if ($1 >= 100) {
267 1.9 christos param->yyYear = $1;
268 1.9 christos param->yyMonth = $3;
269 1.9 christos param->yyDay = $5;
270 1.1 christos } else {
271 1.9 christos param->yyMonth = $1;
272 1.9 christos param->yyDay = $3;
273 1.9 christos param->yyYear = $5;
274 1.1 christos }
275 1.1 christos }
276 1.1 christos | tUNUMBER tSNUMBER tSNUMBER {
277 1.1 christos /* ISO 8601 format. yyyy-mm-dd. */
278 1.9 christos param->yyYear = $1;
279 1.17 apb param->yyHaveFullYear = 1;
280 1.9 christos param->yyMonth = -$2;
281 1.9 christos param->yyDay = -$3;
282 1.1 christos }
283 1.1 christos | tUNUMBER tMONTH tSNUMBER {
284 1.1 christos /* e.g. 17-JUN-1992. */
285 1.9 christos param->yyDay = $1;
286 1.9 christos param->yyMonth = $2;
287 1.9 christos param->yyYear = -$3;
288 1.1 christos }
289 1.1 christos | tMONTH tUNUMBER {
290 1.9 christos param->yyMonth = $1;
291 1.9 christos param->yyDay = $2;
292 1.1 christos }
293 1.1 christos | tMONTH tUNUMBER ',' tUNUMBER {
294 1.9 christos param->yyMonth = $1;
295 1.9 christos param->yyDay = $2;
296 1.9 christos param->yyYear = $4;
297 1.1 christos }
298 1.1 christos | tUNUMBER tMONTH {
299 1.9 christos param->yyMonth = $2;
300 1.9 christos param->yyDay = $1;
301 1.1 christos }
302 1.1 christos | tUNUMBER tMONTH tUNUMBER {
303 1.9 christos param->yyMonth = $2;
304 1.9 christos param->yyDay = $1;
305 1.9 christos param->yyYear = $3;
306 1.1 christos }
307 1.1 christos ;
308 1.1 christos
309 1.1 christos rel : relunit tAGO {
310 1.9 christos param->yyRelSeconds = -param->yyRelSeconds;
311 1.9 christos param->yyRelMonth = -param->yyRelMonth;
312 1.1 christos }
313 1.1 christos | relunit
314 1.1 christos ;
315 1.1 christos
316 1.1 christos relunit : tUNUMBER tMINUTE_UNIT {
317 1.9 christos param->yyRelSeconds += $1 * $2 * 60L;
318 1.1 christos }
319 1.1 christos | tSNUMBER tMINUTE_UNIT {
320 1.9 christos param->yyRelSeconds += $1 * $2 * 60L;
321 1.1 christos }
322 1.1 christos | tMINUTE_UNIT {
323 1.9 christos param->yyRelSeconds += $1 * 60L;
324 1.1 christos }
325 1.1 christos | tSNUMBER tSEC_UNIT {
326 1.9 christos param->yyRelSeconds += $1;
327 1.1 christos }
328 1.1 christos | tUNUMBER tSEC_UNIT {
329 1.9 christos param->yyRelSeconds += $1;
330 1.1 christos }
331 1.1 christos | tSEC_UNIT {
332 1.9 christos param->yyRelSeconds++;
333 1.1 christos }
334 1.1 christos | tSNUMBER tMONTH_UNIT {
335 1.9 christos param->yyRelMonth += $1 * $2;
336 1.1 christos }
337 1.1 christos | tUNUMBER tMONTH_UNIT {
338 1.9 christos param->yyRelMonth += $1 * $2;
339 1.1 christos }
340 1.1 christos | tMONTH_UNIT {
341 1.9 christos param->yyRelMonth += $1;
342 1.1 christos }
343 1.1 christos ;
344 1.1 christos
345 1.1 christos number : tUNUMBER {
346 1.9 christos if (param->yyHaveTime && param->yyHaveDate && !param->yyHaveRel)
347 1.9 christos param->yyYear = $1;
348 1.1 christos else {
349 1.1 christos if($1>10000) {
350 1.9 christos param->yyHaveDate++;
351 1.9 christos param->yyDay= ($1)%100;
352 1.9 christos param->yyMonth= ($1/100)%100;
353 1.9 christos param->yyYear = $1/10000;
354 1.1 christos }
355 1.1 christos else {
356 1.9 christos param->yyHaveTime++;
357 1.1 christos if ($1 < 100) {
358 1.9 christos param->yyHour = $1;
359 1.9 christos param->yyMinutes = 0;
360 1.1 christos }
361 1.1 christos else {
362 1.9 christos param->yyHour = $1 / 100;
363 1.9 christos param->yyMinutes = $1 % 100;
364 1.1 christos }
365 1.9 christos param->yySeconds = 0;
366 1.9 christos param->yyMeridian = MER24;
367 1.1 christos }
368 1.1 christos }
369 1.1 christos }
370 1.1 christos ;
371 1.1 christos
372 1.1 christos o_merid : /* NULL */ {
373 1.1 christos $$ = MER24;
374 1.1 christos }
375 1.1 christos | tMERIDIAN {
376 1.1 christos $$ = $1;
377 1.1 christos }
378 1.1 christos ;
379 1.1 christos
380 1.1 christos %%
381 1.1 christos
382 1.1 christos /* Month and day table. */
383 1.12 joerg static const TABLE MonthDayTable[] = {
384 1.1 christos { "january", tMONTH, 1 },
385 1.1 christos { "february", tMONTH, 2 },
386 1.1 christos { "march", tMONTH, 3 },
387 1.1 christos { "april", tMONTH, 4 },
388 1.1 christos { "may", tMONTH, 5 },
389 1.1 christos { "june", tMONTH, 6 },
390 1.1 christos { "july", tMONTH, 7 },
391 1.1 christos { "august", tMONTH, 8 },
392 1.1 christos { "september", tMONTH, 9 },
393 1.1 christos { "sept", tMONTH, 9 },
394 1.1 christos { "october", tMONTH, 10 },
395 1.1 christos { "november", tMONTH, 11 },
396 1.1 christos { "december", tMONTH, 12 },
397 1.1 christos { "sunday", tDAY, 0 },
398 1.1 christos { "monday", tDAY, 1 },
399 1.1 christos { "tuesday", tDAY, 2 },
400 1.1 christos { "tues", tDAY, 2 },
401 1.1 christos { "wednesday", tDAY, 3 },
402 1.1 christos { "wednes", tDAY, 3 },
403 1.1 christos { "thursday", tDAY, 4 },
404 1.1 christos { "thur", tDAY, 4 },
405 1.1 christos { "thurs", tDAY, 4 },
406 1.1 christos { "friday", tDAY, 5 },
407 1.1 christos { "saturday", tDAY, 6 },
408 1.1 christos { NULL, 0, 0 }
409 1.1 christos };
410 1.1 christos
411 1.1 christos /* Time units table. */
412 1.12 joerg static const TABLE UnitsTable[] = {
413 1.1 christos { "year", tMONTH_UNIT, 12 },
414 1.1 christos { "month", tMONTH_UNIT, 1 },
415 1.1 christos { "fortnight", tMINUTE_UNIT, 14 * 24 * 60 },
416 1.1 christos { "week", tMINUTE_UNIT, 7 * 24 * 60 },
417 1.1 christos { "day", tMINUTE_UNIT, 1 * 24 * 60 },
418 1.1 christos { "hour", tMINUTE_UNIT, 60 },
419 1.1 christos { "minute", tMINUTE_UNIT, 1 },
420 1.1 christos { "min", tMINUTE_UNIT, 1 },
421 1.1 christos { "second", tSEC_UNIT, 1 },
422 1.1 christos { "sec", tSEC_UNIT, 1 },
423 1.1 christos { NULL, 0, 0 }
424 1.1 christos };
425 1.1 christos
426 1.1 christos /* Assorted relative-time words. */
427 1.12 joerg static const TABLE OtherTable[] = {
428 1.1 christos { "tomorrow", tMINUTE_UNIT, 1 * 24 * 60 },
429 1.1 christos { "yesterday", tMINUTE_UNIT, -1 * 24 * 60 },
430 1.1 christos { "today", tMINUTE_UNIT, 0 },
431 1.1 christos { "now", tMINUTE_UNIT, 0 },
432 1.1 christos { "last", tUNUMBER, -1 },
433 1.1 christos { "this", tMINUTE_UNIT, 0 },
434 1.1 christos { "next", tUNUMBER, 2 },
435 1.1 christos { "first", tUNUMBER, 1 },
436 1.7 christos { "one", tUNUMBER, 1 },
437 1.1 christos /* { "second", tUNUMBER, 2 }, */
438 1.7 christos { "two", tUNUMBER, 2 },
439 1.1 christos { "third", tUNUMBER, 3 },
440 1.7 christos { "three", tUNUMBER, 3 },
441 1.1 christos { "fourth", tUNUMBER, 4 },
442 1.7 christos { "four", tUNUMBER, 4 },
443 1.1 christos { "fifth", tUNUMBER, 5 },
444 1.7 christos { "five", tUNUMBER, 5 },
445 1.1 christos { "sixth", tUNUMBER, 6 },
446 1.7 christos { "six", tUNUMBER, 6 },
447 1.1 christos { "seventh", tUNUMBER, 7 },
448 1.7 christos { "seven", tUNUMBER, 7 },
449 1.1 christos { "eighth", tUNUMBER, 8 },
450 1.7 christos { "eight", tUNUMBER, 8 },
451 1.1 christos { "ninth", tUNUMBER, 9 },
452 1.7 christos { "nine", tUNUMBER, 9 },
453 1.1 christos { "tenth", tUNUMBER, 10 },
454 1.7 christos { "ten", tUNUMBER, 10 },
455 1.1 christos { "eleventh", tUNUMBER, 11 },
456 1.7 christos { "eleven", tUNUMBER, 11 },
457 1.1 christos { "twelfth", tUNUMBER, 12 },
458 1.7 christos { "twelve", tUNUMBER, 12 },
459 1.1 christos { "ago", tAGO, 1 },
460 1.1 christos { NULL, 0, 0 }
461 1.1 christos };
462 1.1 christos
463 1.1 christos /* The timezone table. */
464 1.1 christos /* Some of these are commented out because a time_t can't store a float. */
465 1.12 joerg static const TABLE TimezoneTable[] = {
466 1.1 christos { "gmt", tZONE, HOUR( 0) }, /* Greenwich Mean */
467 1.1 christos { "ut", tZONE, HOUR( 0) }, /* Universal (Coordinated) */
468 1.1 christos { "utc", tZONE, HOUR( 0) },
469 1.1 christos { "wet", tZONE, HOUR( 0) }, /* Western European */
470 1.1 christos { "bst", tDAYZONE, HOUR( 0) }, /* British Summer */
471 1.1 christos { "wat", tZONE, HOUR( 1) }, /* West Africa */
472 1.1 christos { "at", tZONE, HOUR( 2) }, /* Azores */
473 1.1 christos #if 0
474 1.1 christos /* For completeness. BST is also British Summer, and GST is
475 1.1 christos * also Guam Standard. */
476 1.1 christos { "bst", tZONE, HOUR( 3) }, /* Brazil Standard */
477 1.1 christos { "gst", tZONE, HOUR( 3) }, /* Greenland Standard */
478 1.1 christos #endif
479 1.1 christos #if 0
480 1.1 christos { "nft", tZONE, HOUR(3.5) }, /* Newfoundland */
481 1.1 christos { "nst", tZONE, HOUR(3.5) }, /* Newfoundland Standard */
482 1.1 christos { "ndt", tDAYZONE, HOUR(3.5) }, /* Newfoundland Daylight */
483 1.1 christos #endif
484 1.1 christos { "ast", tZONE, HOUR( 4) }, /* Atlantic Standard */
485 1.1 christos { "adt", tDAYZONE, HOUR( 4) }, /* Atlantic Daylight */
486 1.1 christos { "est", tZONE, HOUR( 5) }, /* Eastern Standard */
487 1.1 christos { "edt", tDAYZONE, HOUR( 5) }, /* Eastern Daylight */
488 1.1 christos { "cst", tZONE, HOUR( 6) }, /* Central Standard */
489 1.1 christos { "cdt", tDAYZONE, HOUR( 6) }, /* Central Daylight */
490 1.1 christos { "mst", tZONE, HOUR( 7) }, /* Mountain Standard */
491 1.1 christos { "mdt", tDAYZONE, HOUR( 7) }, /* Mountain Daylight */
492 1.1 christos { "pst", tZONE, HOUR( 8) }, /* Pacific Standard */
493 1.1 christos { "pdt", tDAYZONE, HOUR( 8) }, /* Pacific Daylight */
494 1.1 christos { "yst", tZONE, HOUR( 9) }, /* Yukon Standard */
495 1.1 christos { "ydt", tDAYZONE, HOUR( 9) }, /* Yukon Daylight */
496 1.1 christos { "hst", tZONE, HOUR(10) }, /* Hawaii Standard */
497 1.1 christos { "hdt", tDAYZONE, HOUR(10) }, /* Hawaii Daylight */
498 1.1 christos { "cat", tZONE, HOUR(10) }, /* Central Alaska */
499 1.1 christos { "ahst", tZONE, HOUR(10) }, /* Alaska-Hawaii Standard */
500 1.1 christos { "nt", tZONE, HOUR(11) }, /* Nome */
501 1.1 christos { "idlw", tZONE, HOUR(12) }, /* International Date Line West */
502 1.1 christos { "cet", tZONE, -HOUR(1) }, /* Central European */
503 1.1 christos { "met", tZONE, -HOUR(1) }, /* Middle European */
504 1.1 christos { "mewt", tZONE, -HOUR(1) }, /* Middle European Winter */
505 1.1 christos { "mest", tDAYZONE, -HOUR(1) }, /* Middle European Summer */
506 1.1 christos { "swt", tZONE, -HOUR(1) }, /* Swedish Winter */
507 1.1 christos { "sst", tDAYZONE, -HOUR(1) }, /* Swedish Summer */
508 1.1 christos { "fwt", tZONE, -HOUR(1) }, /* French Winter */
509 1.1 christos { "fst", tDAYZONE, -HOUR(1) }, /* French Summer */
510 1.1 christos { "eet", tZONE, -HOUR(2) }, /* Eastern Europe, USSR Zone 1 */
511 1.1 christos { "bt", tZONE, -HOUR(3) }, /* Baghdad, USSR Zone 2 */
512 1.1 christos #if 0
513 1.1 christos { "it", tZONE, -HOUR(3.5) },/* Iran */
514 1.1 christos #endif
515 1.1 christos { "zp4", tZONE, -HOUR(4) }, /* USSR Zone 3 */
516 1.1 christos { "zp5", tZONE, -HOUR(5) }, /* USSR Zone 4 */
517 1.1 christos #if 0
518 1.1 christos { "ist", tZONE, -HOUR(5.5) },/* Indian Standard */
519 1.1 christos #endif
520 1.1 christos { "zp6", tZONE, -HOUR(6) }, /* USSR Zone 5 */
521 1.1 christos #if 0
522 1.1 christos /* For completeness. NST is also Newfoundland Stanard, and SST is
523 1.1 christos * also Swedish Summer. */
524 1.1 christos { "nst", tZONE, -HOUR(6.5) },/* North Sumatra */
525 1.1 christos { "sst", tZONE, -HOUR(7) }, /* South Sumatra, USSR Zone 6 */
526 1.1 christos #endif /* 0 */
527 1.1 christos { "wast", tZONE, -HOUR(7) }, /* West Australian Standard */
528 1.1 christos { "wadt", tDAYZONE, -HOUR(7) }, /* West Australian Daylight */
529 1.1 christos #if 0
530 1.1 christos { "jt", tZONE, -HOUR(7.5) },/* Java (3pm in Cronusland!) */
531 1.1 christos #endif
532 1.1 christos { "cct", tZONE, -HOUR(8) }, /* China Coast, USSR Zone 7 */
533 1.1 christos { "jst", tZONE, -HOUR(9) }, /* Japan Standard, USSR Zone 8 */
534 1.1 christos #if 0
535 1.1 christos { "cast", tZONE, -HOUR(9.5) },/* Central Australian Standard */
536 1.1 christos { "cadt", tDAYZONE, -HOUR(9.5) },/* Central Australian Daylight */
537 1.1 christos #endif
538 1.1 christos { "east", tZONE, -HOUR(10) }, /* Eastern Australian Standard */
539 1.1 christos { "eadt", tDAYZONE, -HOUR(10) }, /* Eastern Australian Daylight */
540 1.1 christos { "gst", tZONE, -HOUR(10) }, /* Guam Standard, USSR Zone 9 */
541 1.1 christos { "nzt", tZONE, -HOUR(12) }, /* New Zealand */
542 1.1 christos { "nzst", tZONE, -HOUR(12) }, /* New Zealand Standard */
543 1.1 christos { "nzdt", tDAYZONE, -HOUR(12) }, /* New Zealand Daylight */
544 1.1 christos { "idle", tZONE, -HOUR(12) }, /* International Date Line East */
545 1.1 christos { NULL, 0, 0 }
546 1.1 christos };
547 1.1 christos
548 1.1 christos /* Military timezone table. */
549 1.12 joerg static const TABLE MilitaryTable[] = {
550 1.1 christos { "a", tZONE, HOUR( 1) },
551 1.1 christos { "b", tZONE, HOUR( 2) },
552 1.1 christos { "c", tZONE, HOUR( 3) },
553 1.1 christos { "d", tZONE, HOUR( 4) },
554 1.1 christos { "e", tZONE, HOUR( 5) },
555 1.1 christos { "f", tZONE, HOUR( 6) },
556 1.1 christos { "g", tZONE, HOUR( 7) },
557 1.1 christos { "h", tZONE, HOUR( 8) },
558 1.1 christos { "i", tZONE, HOUR( 9) },
559 1.1 christos { "k", tZONE, HOUR( 10) },
560 1.1 christos { "l", tZONE, HOUR( 11) },
561 1.1 christos { "m", tZONE, HOUR( 12) },
562 1.1 christos { "n", tZONE, HOUR(- 1) },
563 1.1 christos { "o", tZONE, HOUR(- 2) },
564 1.1 christos { "p", tZONE, HOUR(- 3) },
565 1.1 christos { "q", tZONE, HOUR(- 4) },
566 1.1 christos { "r", tZONE, HOUR(- 5) },
567 1.1 christos { "s", tZONE, HOUR(- 6) },
568 1.1 christos { "t", tZONE, HOUR(- 7) },
569 1.1 christos { "u", tZONE, HOUR(- 8) },
570 1.1 christos { "v", tZONE, HOUR(- 9) },
571 1.1 christos { "w", tZONE, HOUR(-10) },
572 1.1 christos { "x", tZONE, HOUR(-11) },
573 1.1 christos { "y", tZONE, HOUR(-12) },
574 1.1 christos { "z", tZONE, HOUR( 0) },
575 1.1 christos { NULL, 0, 0 }
576 1.1 christos };
577 1.1 christos
578 1.1 christos
579 1.1 christos
581 1.1 christos
582 1.1 christos /* ARGSUSED */
583 1.9 christos static int
584 1.1 christos yyerror(struct dateinfo *param, const char **inp, const char *s __unused)
585 1.1 christos {
586 1.1 christos return 0;
587 1.1 christos }
588 1.1 christos
589 1.17 apb
590 1.17 apb /* Adjust year from a value that might be abbreviated, to a full value.
591 1.17 apb * e.g. convert 70 to 1970.
592 1.17 apb * Input Year is either:
593 1.17 apb * - A negative number, which means to use its absolute value (why?)
594 1.17 apb * - A number from 0 to 99, which means a year from 1900 to 1999, or
595 1.17 apb * - The actual year (>=100).
596 1.17 apb * Returns the full year. */
597 1.17 apb static time_t
598 1.17 apb AdjustYear(time_t Year)
599 1.17 apb {
600 1.17 apb /* XXX Y2K */
601 1.17 apb if (Year < 0)
602 1.17 apb Year = -Year;
603 1.17 apb if (Year < 70)
604 1.17 apb Year += 2000;
605 1.17 apb else if (Year < 100)
606 1.17 apb Year += 1900;
607 1.17 apb return Year;
608 1.17 apb }
609 1.1 christos
610 1.1 christos static time_t
611 1.11 apb Convert(
612 1.11 apb time_t Month, /* month of year [1-12] */
613 1.17 apb time_t Day, /* day of month [1-31] */
614 1.11 apb time_t Year, /* year, not abbreviated in any way */
615 1.11 apb time_t Hours, /* Hour of day [0-24] */
616 1.11 apb time_t Minutes, /* Minute of hour [0-59] */
617 1.16 yamt time_t Seconds, /* Second of minute [0-60] */
618 1.11 apb time_t Timezone, /* Timezone as minutes east of UTC */
619 1.11 apb MERIDIAN Meridian, /* Hours are am/pm/24 hour clock */
620 1.1 christos DSTMODE DSTmode /* DST on/off/maybe */
621 1.1 christos )
622 1.13 apb {
623 1.13 apb struct tm tm = {.tm_sec = 0};
624 1.1 christos time_t result;
625 1.11 apb
626 1.11 apb tm.tm_sec = Seconds;
627 1.11 apb tm.tm_min = Minutes;
628 1.11 apb tm.tm_hour = Hours + (Meridian == MERpm ? 12 : 0);
629 1.11 apb tm.tm_mday = Day;
630 1.11 apb tm.tm_mon = Month - 1;
631 1.11 apb tm.tm_year = Year - 1900;
632 1.11 apb switch (DSTmode) {
633 1.11 apb case DSTon: tm.tm_isdst = 1; break;
634 1.11 apb case DSToff: tm.tm_isdst = 0; break;
635 1.6 christos default: tm.tm_isdst = -1; break;
636 1.6 christos }
637 1.13 apb
638 1.13 apb /* We rely on mktime_z(NULL, ...) working in UTC, not in local time. */
639 1.16 yamt result = mktime_z(NULL, &tm);
640 1.13 apb result += Timezone * 60;
641 1.1 christos return result;
642 1.1 christos }
643 1.1 christos
644 1.1 christos
645 1.1 christos static time_t
646 1.1 christos DSTcorrect(
647 1.1 christos time_t Start,
648 1.1 christos time_t Future
649 1.1 christos )
650 1.1 christos {
651 1.1 christos time_t StartDay;
652 1.6 christos time_t FutureDay;
653 1.6 christos struct tm *tm;
654 1.6 christos
655 1.6 christos if ((tm = localtime(&Start)) == NULL)
656 1.6 christos return -1;
657 1.6 christos StartDay = (tm->tm_hour + 1) % 24;
658 1.6 christos
659 1.6 christos if ((tm = localtime(&Future)) == NULL)
660 1.6 christos return -1;
661 1.1 christos FutureDay = (tm->tm_hour + 1) % 24;
662 1.1 christos
663 1.1 christos return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
664 1.1 christos }
665 1.1 christos
666 1.1 christos
667 1.1 christos static time_t
668 1.1 christos RelativeDate(
669 1.1 christos time_t Start,
670 1.1 christos time_t DayOrdinal,
671 1.1 christos time_t DayNumber
672 1.1 christos )
673 1.1 christos {
674 1.1 christos struct tm *tm;
675 1.1 christos time_t now;
676 1.1 christos
677 1.1 christos now = Start;
678 1.1 christos tm = localtime(&now);
679 1.1 christos now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
680 1.1 christos now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
681 1.1 christos return DSTcorrect(Start, now);
682 1.1 christos }
683 1.1 christos
684 1.1 christos
685 1.1 christos static time_t
686 1.1 christos RelativeMonth(
687 1.9 christos time_t Start,
688 1.9 christos time_t RelMonth,
689 1.1 christos time_t Timezone
690 1.1 christos )
691 1.1 christos {
692 1.1 christos struct tm *tm;
693 1.1 christos time_t Month;
694 1.1 christos time_t Year;
695 1.1 christos
696 1.1 christos if (RelMonth == 0)
697 1.1 christos return 0;
698 1.6 christos tm = localtime(&Start);
699 1.6 christos if (tm == NULL)
700 1.1 christos return -1;
701 1.1 christos Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
702 1.1 christos Year = Month / 12;
703 1.1 christos Month = Month % 12 + 1;
704 1.1 christos return DSTcorrect(Start,
705 1.1 christos Convert(Month, (time_t)tm->tm_mday, Year,
706 1.9 christos (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
707 1.1 christos Timezone, MER24, DSTmaybe));
708 1.1 christos }
709 1.1 christos
710 1.1 christos
711 1.9 christos static int
712 1.1 christos LookupWord(YYSTYPE *yylval, char *buff)
713 1.1 christos {
714 1.1 christos register char *p;
715 1.1 christos register char *q;
716 1.1 christos register const TABLE *tp;
717 1.1 christos int i;
718 1.1 christos int abbrev;
719 1.1 christos
720 1.1 christos /* Make it lowercase. */
721 1.1 christos for (p = buff; *p; p++)
722 1.1 christos if (isupper((unsigned char)*p))
723 1.1 christos *p = tolower((unsigned char)*p);
724 1.1 christos
725 1.9 christos if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
726 1.1 christos yylval->Meridian = MERam;
727 1.1 christos return tMERIDIAN;
728 1.1 christos }
729 1.9 christos if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
730 1.1 christos yylval->Meridian = MERpm;
731 1.1 christos return tMERIDIAN;
732 1.1 christos }
733 1.1 christos
734 1.1 christos /* See if we have an abbreviation for a month. */
735 1.1 christos if (strlen(buff) == 3)
736 1.1 christos abbrev = 1;
737 1.1 christos else if (strlen(buff) == 4 && buff[3] == '.') {
738 1.1 christos abbrev = 1;
739 1.1 christos buff[3] = '\0';
740 1.1 christos }
741 1.1 christos else
742 1.1 christos abbrev = 0;
743 1.1 christos
744 1.1 christos for (tp = MonthDayTable; tp->name; tp++) {
745 1.1 christos if (abbrev) {
746 1.9 christos if (strncmp(buff, tp->name, 3) == 0) {
747 1.1 christos yylval->Number = tp->value;
748 1.1 christos return tp->type;
749 1.1 christos }
750 1.1 christos }
751 1.9 christos else if (strcmp(buff, tp->name) == 0) {
752 1.1 christos yylval->Number = tp->value;
753 1.1 christos return tp->type;
754 1.1 christos }
755 1.1 christos }
756 1.1 christos
757 1.1 christos for (tp = TimezoneTable; tp->name; tp++)
758 1.9 christos if (strcmp(buff, tp->name) == 0) {
759 1.1 christos yylval->Number = tp->value;
760 1.1 christos return tp->type;
761 1.1 christos }
762 1.1 christos
763 1.1 christos if (strcmp(buff, "dst") == 0)
764 1.1 christos return tDST;
765 1.1 christos
766 1.1 christos for (tp = UnitsTable; tp->name; tp++)
767 1.9 christos if (strcmp(buff, tp->name) == 0) {
768 1.1 christos yylval->Number = tp->value;
769 1.1 christos return tp->type;
770 1.1 christos }
771 1.1 christos
772 1.1 christos /* Strip off any plural and try the units table again. */
773 1.1 christos i = strlen(buff) - 1;
774 1.1 christos if (buff[i] == 's') {
775 1.1 christos buff[i] = '\0';
776 1.1 christos for (tp = UnitsTable; tp->name; tp++)
777 1.9 christos if (strcmp(buff, tp->name) == 0) {
778 1.1 christos yylval->Number = tp->value;
779 1.1 christos return tp->type;
780 1.1 christos }
781 1.1 christos buff[i] = 's'; /* Put back for "this" in OtherTable. */
782 1.1 christos }
783 1.1 christos
784 1.1 christos for (tp = OtherTable; tp->name; tp++)
785 1.9 christos if (strcmp(buff, tp->name) == 0) {
786 1.1 christos yylval->Number = tp->value;
787 1.1 christos return tp->type;
788 1.1 christos }
789 1.1 christos
790 1.1 christos /* Military timezones. */
791 1.1 christos if (buff[1] == '\0' && isalpha((unsigned char)*buff)) {
792 1.1 christos for (tp = MilitaryTable; tp->name; tp++)
793 1.9 christos if (strcmp(buff, tp->name) == 0) {
794 1.1 christos yylval->Number = tp->value;
795 1.1 christos return tp->type;
796 1.1 christos }
797 1.1 christos }
798 1.1 christos
799 1.1 christos /* Drop out any periods and try the timezone table again. */
800 1.1 christos for (i = 0, p = q = buff; *q; q++)
801 1.1 christos if (*q != '.')
802 1.1 christos *p++ = *q;
803 1.1 christos else
804 1.1 christos i++;
805 1.1 christos *p = '\0';
806 1.1 christos if (i)
807 1.1 christos for (tp = TimezoneTable; tp->name; tp++)
808 1.9 christos if (strcmp(buff, tp->name) == 0) {
809 1.1 christos yylval->Number = tp->value;
810 1.1 christos return tp->type;
811 1.1 christos }
812 1.1 christos
813 1.1 christos return tID;
814 1.1 christos }
815 1.1 christos
816 1.1 christos
817 1.9 christos static int
818 1.1 christos yylex(YYSTYPE *yylval, const char **yyInput)
819 1.1 christos {
820 1.1 christos register char c;
821 1.1 christos register char *p;
822 1.1 christos char buff[20];
823 1.1 christos int Count;
824 1.9 christos int sign;
825 1.1 christos const char *inp = *yyInput;
826 1.1 christos
827 1.9 christos for ( ; ; ) {
828 1.9 christos while (isspace((unsigned char)*inp))
829 1.1 christos inp++;
830 1.9 christos
831 1.1 christos if (isdigit((unsigned char)(c = *inp)) || c == '-' || c == '+') {
832 1.1 christos if (c == '-' || c == '+') {
833 1.9 christos sign = c == '-' ? -1 : 1;
834 1.1 christos if (!isdigit((unsigned char)*++inp))
835 1.1 christos /* skip the '-' sign */
836 1.1 christos continue;
837 1.1 christos }
838 1.1 christos else
839 1.9 christos sign = 0;
840 1.9 christos for (yylval->Number = 0; isdigit((unsigned char)(c = *inp++)); )
841 1.1 christos yylval->Number = 10 * yylval->Number + c - '0';
842 1.9 christos if (sign < 0)
843 1.9 christos yylval->Number = -yylval->Number;
844 1.1 christos *yyInput = --inp;
845 1.1 christos return sign ? tSNUMBER : tUNUMBER;
846 1.1 christos }
847 1.9 christos if (isalpha((unsigned char)c)) {
848 1.1 christos for (p = buff; isalpha((unsigned char)(c = *inp++)) || c == '.'; )
849 1.1 christos if (p < &buff[sizeof buff - 1])
850 1.1 christos *p++ = c;
851 1.9 christos *p = '\0';
852 1.9 christos *yyInput = --inp;
853 1.1 christos return LookupWord(yylval, buff);
854 1.5 tron }
855 1.9 christos if (c == '@') {
856 1.5 tron *yyInput = ++inp;
857 1.5 tron return AT_SIGN;
858 1.9 christos }
859 1.9 christos if (c != '(') {
860 1.9 christos *yyInput = ++inp;
861 1.9 christos return c;
862 1.1 christos }
863 1.1 christos Count = 0;
864 1.9 christos do {
865 1.1 christos c = *inp++;
866 1.1 christos if (c == '\0')
867 1.1 christos return c;
868 1.1 christos if (c == '(')
869 1.1 christos Count++;
870 1.1 christos else if (c == ')')
871 1.1 christos Count--;
872 1.1 christos } while (Count > 0);
873 1.1 christos }
874 1.1 christos }
875 1.1 christos
876 1.1 christos #define TM_YEAR_ORIGIN 1900
877 1.1 christos
878 1.6 christos /* Yield A - B, measured in seconds. */
879 1.1 christos static time_t
880 1.1 christos difftm (struct tm *a, struct tm *b)
881 1.1 christos {
882 1.1 christos int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
883 1.1 christos int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
884 1.1 christos int days = (
885 1.1 christos /* difference in day of year */
886 1.1 christos a->tm_yday - b->tm_yday
887 1.1 christos /* + intervening leap days */
888 1.1 christos + ((ay >> 2) - (by >> 2))
889 1.1 christos - (ay/100 - by/100)
890 1.1 christos + ((ay/100 >> 2) - (by/100 >> 2))
891 1.1 christos /* + difference in years * 365 */
892 1.1 christos + (long)(ay-by) * 365
893 1.6 christos );
894 1.1 christos return ((time_t)60*(60*(24*days + (a->tm_hour - b->tm_hour))
895 1.1 christos + (a->tm_min - b->tm_min))
896 1.1 christos + (a->tm_sec - b->tm_sec));
897 1.1 christos }
898 1.1 christos
899 1.1 christos time_t
900 1.1 christos parsedate(const char *p, const time_t *now, const int *zone)
901 1.1 christos {
902 1.1 christos struct tm gmt, local, *gmt_ptr, *tm;
903 1.1 christos time_t nowt;
904 1.1 christos int zonet;
905 1.6 christos time_t Start;
906 1.9 christos time_t tod, rm;
907 1.14 apb struct dateinfo param;
908 1.14 apb int saved_errno;
909 1.14 apb
910 1.14 apb saved_errno = errno;
911 1.1 christos errno = 0;
912 1.1 christos
913 1.1 christos if (now == NULL || zone == NULL) {
914 1.1 christos now = &nowt;
915 1.1 christos zone = &zonet;
916 1.1 christos (void)time(&nowt);
917 1.1 christos
918 1.1 christos gmt_ptr = gmtime_r(now, &gmt);
919 1.1 christos if ((tm = localtime_r(now, &local)) == NULL)
920 1.1 christos return -1;
921 1.1 christos
922 1.1 christos if (gmt_ptr != NULL)
923 1.1 christos zonet = difftm(&gmt, &local) / 60;
924 1.1 christos else
925 1.1 christos /* We are on a system like VMS, where the system clock is
926 1.1 christos in local time and the system has no concept of timezones.
927 1.1 christos Hopefully we can fake this out (for the case in which the
928 1.1 christos user specifies no timezone) by just saying the timezone
929 1.1 christos is zero. */
930 1.1 christos zonet = 0;
931 1.1 christos
932 1.1 christos if (local.tm_isdst)
933 1.1 christos zonet += 60;
934 1.1 christos } else {
935 1.1 christos if ((tm = localtime_r(now, &local)) == NULL)
936 1.1 christos return -1;
937 1.9 christos }
938 1.9 christos param.yyYear = tm->tm_year + 1900;
939 1.9 christos param.yyMonth = tm->tm_mon + 1;
940 1.9 christos param.yyDay = tm->tm_mday;
941 1.9 christos param.yyTimezone = *zone;
942 1.9 christos param.yyDSTmode = DSTmaybe;
943 1.9 christos param.yyHour = 0;
944 1.9 christos param.yyMinutes = 0;
945 1.9 christos param.yySeconds = 0;
946 1.9 christos param.yyMeridian = MER24;
947 1.9 christos param.yyRelSeconds = 0;
948 1.9 christos param.yyRelMonth = 0;
949 1.17 apb param.yyHaveDate = 0;
950 1.9 christos param.yyHaveFullYear = 0;
951 1.9 christos param.yyHaveDay = 0;
952 1.9 christos param.yyHaveRel = 0;
953 1.9 christos param.yyHaveTime = 0;
954 1.9 christos param.yyHaveZone = 0;
955 1.9 christos
956 1.14 apb if (yyparse(¶m, &p) || param.yyHaveTime > 1 || param.yyHaveZone > 1 ||
957 1.14 apb param.yyHaveDate > 1 || param.yyHaveDay > 1) {
958 1.9 christos errno = EINVAL;
959 1.14 apb return -1;
960 1.9 christos }
961 1.9 christos
962 1.17 apb if (param.yyHaveDate || param.yyHaveTime || param.yyHaveDay) {
963 1.17 apb if (! param.yyHaveFullYear) {
964 1.17 apb param.yyYear = AdjustYear(param.yyYear);
965 1.17 apb param.yyHaveFullYear = 1;
966 1.9 christos }
967 1.9 christos Start = Convert(param.yyMonth, param.yyDay, param.yyYear, param.yyHour,
968 1.9 christos param.yyMinutes, param.yySeconds, param.yyTimezone,
969 1.14 apb param.yyMeridian, param.yyDSTmode);
970 1.1 christos if (Start == -1 && errno != 0)
971 1.1 christos return -1;
972 1.1 christos }
973 1.1 christos else {
974 1.9 christos Start = *now;
975 1.1 christos if (!param.yyHaveRel)
976 1.1 christos Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
977 1.1 christos }
978 1.9 christos
979 1.9 christos Start += param.yyRelSeconds;
980 1.14 apb rm = RelativeMonth(Start, param.yyRelMonth, param.yyTimezone);
981 1.6 christos if (rm == -1 && errno != 0)
982 1.6 christos return -1;
983 1.1 christos Start += rm;
984 1.9 christos
985 1.9 christos if (param.yyHaveDay && !param.yyHaveDate) {
986 1.1 christos tod = RelativeDate(Start, param.yyDayOrdinal, param.yyDayNumber);
987 1.1 christos Start += tod;
988 1.1 christos }
989 1.14 apb
990 1.14 apb if (errno == 0)
991 1.6 christos errno = saved_errno;
992 1.1 christos return Start;
993 1.1 christos }
994 1.1 christos
995 1.1 christos
996 1.1 christos #if defined(TEST)
997 1.1 christos
998 1.1 christos /* ARGSUSED */
999 1.14 apb int
1000 1.1 christos main(int ac, char *av[])
1001 1.1 christos {
1002 1.1 christos char buff[128];
1003 1.1 christos time_t d;
1004 1.1 christos
1005 1.1 christos (void)printf("Enter date, or blank line to exit.\n\t> ");
1006 1.14 apb (void)fflush(stdout);
1007 1.14 apb while (fgets(buff, sizeof(buff), stdin) && buff[0] != '\n') {
1008 1.1 christos errno = 0;
1009 1.14 apb d = parsedate(buff, NULL, NULL);
1010 1.14 apb if (d == -1 && errno != 0)
1011 1.14 apb (void)printf("Bad format - couldn't convert: %s\n",
1012 1.1 christos strerror(errno));
1013 1.14 apb else
1014 1.1 christos (void)printf("%jd\t%s", (intmax_t)d, ctime(&d));
1015 1.1 christos (void)printf("\t> ");
1016 1.1 christos (void)fflush(stdout);
1017 1.1 christos }
1018 1.1 christos exit(0);
1019 1.1 christos /* NOTREACHED */
1020 1.1 christos }
1021 #endif /* defined(TEST) */
1022