Merge from vendor branch CVS:
[dragonfly.git] / contrib / bsdtar / getdate.y
1 %{
2 /*
3  * March 2005: Further modified and simplified by Tim Kientzle:
4  * Eliminate minutes-based calculations (just do everything in
5  * seconds), have lexer only recognize unsigned integers (handle '+'
6  * and '-' characters in grammar), combine tables into one table with
7  * explicit abbreviation notes, do am/pm adjustments in the grammar
8  * (eliminate some state variables and post-processing).  Among other
9  * things, these changes eliminated two shift/reduce conflicts.  (Went
10  * from 10 to 8.)
11  */
12
13 /*
14 **  Originally written by Steven M. Bellovin <smb@research.att.com> while
15 **  at the University of North Carolina at Chapel Hill.  Later tweaked by
16 **  a couple of people on Usenet.  Completely overhauled by Rich $alz
17 **  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
18 **
19 **  This grammar has 10 shift/reduce conflicts.
20 **
21 **  This code is in the public domain and has no copyright.
22 */
23 /* SUPPRESS 287 on yaccpar_sccsid *//* Unused static variable */
24 /* SUPPRESS 288 on yyerrlab *//* Label unused */
25
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/tar/getdate.y,v 1.4 2005/04/23 18:38:32 kientzle Exp $");
28
29 #include <ctype.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34
35 #define yyparse getdate_yyparse
36 #define yylex getdate_yylex
37 #define yyerror getdate_yyerror
38
39 static int yyparse(void);
40 static int yylex(void);
41 static int yyerror(const char *);
42
43 time_t get_date(char *);
44
45 #define EPOCH           1970
46 #define HOUR(x)         ((time_t)(x) * 60)
47 #define SECSPERDAY      (24L * 60L * 60L)
48
49 /*
50 **  Daylight-savings mode:  on, off, or not yet known.
51 */
52 typedef enum _DSTMODE {
53     DSTon, DSToff, DSTmaybe
54 } DSTMODE;
55
56 /*
57 **  Meridian:  am or pm.
58 */
59 enum { tAM, tPM };
60
61 /*
62 **  Global variables.  We could get rid of most of these by using a good
63 **  union as the yacc stack.  (This routine was originally written before
64 **  yacc had the %union construct.)  Maybe someday; right now we only use
65 **  the %union very rarely.
66 */
67 static char     *yyInput;
68
69 static DSTMODE  yyDSTmode;
70 static time_t   yyDayOrdinal;
71 static time_t   yyDayNumber;
72 static int      yyHaveDate;
73 static int      yyHaveDay;
74 static int      yyHaveRel;
75 static int      yyHaveTime;
76 static int      yyHaveZone;
77 static time_t   yyTimezone;
78 static time_t   yyDay;
79 static time_t   yyHour;
80 static time_t   yyMinutes;
81 static time_t   yyMonth;
82 static time_t   yySeconds;
83 static time_t   yyYear;
84 static time_t   yyRelMonth;
85 static time_t   yyRelSeconds;
86
87 %}
88
89 %union {
90     time_t              Number;
91 }
92
93 %token  tAGO tDAY tDAYZONE tAMPM tMONTH tMONTH_UNIT tSEC_UNIT tUNUMBER
94 %token  tZONE tDST
95
96 %type   <Number>        tDAY tDAYZONE tMONTH tMONTH_UNIT
97 %type   <Number>        tSEC_UNIT tUNUMBER tZONE tAMPM
98
99 %%
100
101 spec    : /* NULL */
102         | spec item
103         ;
104
105 item    : time { yyHaveTime++; }
106         | zone { yyHaveZone++; }
107         | date { yyHaveDate++; }
108         | day { yyHaveDay++; }
109         | rel { yyHaveRel++; }
110         | number
111         ;
112
113 time    : tUNUMBER tAMPM {
114                 /* "7am" */
115                 yyHour = $1;
116                 if (yyHour == 12)
117                         yyHour = 0;
118                 yyMinutes = 0;
119                 yySeconds = 0;
120                 if ($2 == tPM)
121                         yyHour += 12;
122         }
123         | bare_time {
124                 /* "7:12:18" "19:17" */
125         }
126         | bare_time tAMPM {
127                 /* "7:12pm", "12:20:13am" */
128                 if (yyHour == 12)
129                         yyHour = 0;
130                 if ($2 == tPM)
131                         yyHour += 12;
132         }
133         | bare_time  '+' tUNUMBER {
134                 /* "7:14+0700" */
135                 yyDSTmode = DSToff;
136                 yyTimezone = - ($3 % 100 + ($3 / 100) * 60);
137         }
138         | bare_time '-' tUNUMBER {
139                 /* "19:14:12-0530" */
140                 yyDSTmode = DSToff;
141                 yyTimezone = + ($3 % 100 + ($3 / 100) * 60);
142         }
143         ;
144
145 bare_time : tUNUMBER ':' tUNUMBER {
146                 yyHour = $1;
147                 yyMinutes = $3;
148                 yySeconds = 0;
149         }
150         | tUNUMBER ':' tUNUMBER ':' tUNUMBER {
151                 yyHour = $1;
152                 yyMinutes = $3;
153                 yySeconds = $5;
154         }
155         ;
156
157 zone    : tZONE {
158                 yyTimezone = $1;
159                 yyDSTmode = DSToff;
160         }
161         | tDAYZONE {
162                 yyTimezone = $1;
163                 yyDSTmode = DSTon;
164         }
165         | tZONE tDST {
166                 yyTimezone = $1;
167                 yyDSTmode = DSTon;
168         }
169         ;
170
171 day     : tDAY {
172                 yyDayOrdinal = 1;
173                 yyDayNumber = $1;
174         }
175         | tDAY ',' {
176                 /* "tue," "wednesday," */
177                 yyDayOrdinal = 1;
178                 yyDayNumber = $1;
179         }
180         | tUNUMBER tDAY {
181                 /* "second tues" "3 wed" */
182                 yyDayOrdinal = $1;
183                 yyDayNumber = $2;
184         }
185         ;
186
187 date    : tUNUMBER '/' tUNUMBER {
188                 /* "1/15" */
189                 yyMonth = $1;
190                 yyDay = $3;
191         }
192         | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
193                 if ($1 >= 13) {
194                         /* First number is big:  2004/01/29, 99/02/17 */
195                         yyYear = $1;
196                         yyMonth = $3;
197                         yyDay = $5;
198                 } else if (($5 >= 13) || ($3 >= 13)) {
199                         /* Last number is big:  01/07/98 */
200                         /* Middle number is big:  01/29/04 */
201                         yyMonth = $1;
202                         yyDay = $3;
203                         yyYear = $5;
204                 } else {
205                         /* No significant clues: 02/03/04 */
206                         yyMonth = $1;
207                         yyDay = $3;
208                         yyYear = $5;
209                 }
210         }
211         | tUNUMBER '-' tUNUMBER '-' tUNUMBER {
212                 /* ISO 8601 format.  yyyy-mm-dd.  */
213                 yyYear = $1;
214                 yyMonth = $3;
215                 yyDay = $5;
216         }
217         | tUNUMBER '-' tMONTH '-' tUNUMBER {
218                 if ($1 > 31) {
219                         /* e.g. 1992-Jun-17 */
220                         yyYear = $1;
221                         yyMonth = $3;
222                         yyDay = $5;
223                 } else {
224                         /* e.g. 17-JUN-1992.  */
225                         yyDay = $1;
226                         yyMonth = $3;
227                         yyYear = $5;
228                 }
229         }
230         | tMONTH tUNUMBER {
231                 /* "May 3" */
232                 yyMonth = $1;
233                 yyDay = $2;
234         }
235         | tMONTH tUNUMBER ',' tUNUMBER {
236                 /* "June 17, 2001" */
237                 yyMonth = $1;
238                 yyDay = $2;
239                 yyYear = $4;
240         }
241         | tUNUMBER tMONTH {
242                 /* "12 Sept" */
243                 yyDay = $1;
244                 yyMonth = $2;
245         }
246         | tUNUMBER tMONTH tUNUMBER {
247                 /* "12 Sept 1997" */
248                 yyDay = $1;
249                 yyMonth = $2;
250                 yyYear = $3;
251         }
252         ;
253
254 rel     : relunit tAGO {
255                 yyRelSeconds = -yyRelSeconds;
256                 yyRelMonth = -yyRelMonth;
257         }
258         | relunit
259         ;
260
261 relunit : '-' tUNUMBER tSEC_UNIT {
262                 /* "-3 hours" */
263                 yyRelSeconds -= $2 * $3;
264         }
265         | '+' tUNUMBER tSEC_UNIT {
266                 /* "+1 minute" */
267                 yyRelSeconds += $2 * $3;
268         }
269         | tUNUMBER tSEC_UNIT {
270                 /* "1 day" */
271                 yyRelSeconds += $1;
272         }
273         | tSEC_UNIT {
274                 /* "hour" */
275                 yyRelSeconds++;
276         }
277         | '-' tUNUMBER tMONTH_UNIT {
278                 /* "-3 months" */
279                 yyRelMonth -= $2 * $3;
280         }
281         | '+' tUNUMBER tMONTH_UNIT {
282                 /* "+5 years" */
283                 yyRelMonth += $2 * $3;
284         }
285         | tUNUMBER tMONTH_UNIT {
286                 /* "2 years" */
287                 yyRelMonth += $1 * $2;
288         }
289         | tMONTH_UNIT {
290                 /* "6 months" */
291                 yyRelMonth += $1;
292         }
293         ;
294
295 number  : tUNUMBER {
296                 if (yyHaveTime && yyHaveDate && !yyHaveRel)
297                         yyYear = $1;
298                 else {
299                         if($1>10000) {
300                                 /* "20040301" */
301                                 yyHaveDate++;
302                                 yyDay= ($1)%100;
303                                 yyMonth= ($1/100)%100;
304                                 yyYear = $1/10000;
305                         }
306                         else {
307                                 /* "513" is same as "5:13" */
308                                 yyHaveTime++;
309                                 if ($1 < 100) {
310                                         yyHour = $1;
311                                         yyMinutes = 0;
312                                 }
313                                 else {
314                                         yyHour = $1 / 100;
315                                         yyMinutes = $1 % 100;
316                                 }
317                                 yySeconds = 0;
318                         }
319                 }
320         }
321         ;
322
323
324 %%
325
326 static struct TABLE {
327         size_t          abbrev;
328         const char      *name;
329         int             type;
330         time_t          value;
331 } const TimeWords[] = {
332         /* am/pm */
333         { 0, "am",              tAMPM,  tAM },
334         { 0, "pm",              tAMPM,  tPM },
335
336         /* Month names. */
337         { 3, "january",         tMONTH,  1 },
338         { 3, "february",        tMONTH,  2 },
339         { 3, "march",           tMONTH,  3 },
340         { 3, "april",           tMONTH,  4 },
341         { 3, "may",             tMONTH,  5 },
342         { 3, "june",            tMONTH,  6 },
343         { 3, "july",            tMONTH,  7 },
344         { 3, "august",          tMONTH,  8 },
345         { 3, "september",       tMONTH,  9 },
346         { 3, "october",         tMONTH, 10 },
347         { 3, "november",        tMONTH, 11 },
348         { 3, "december",        tMONTH, 12 },
349
350         /* Days of the week. */
351         { 2, "sunday",          tDAY, 0 },
352         { 3, "monday",          tDAY, 1 },
353         { 2, "tuesday",         tDAY, 2 },
354         { 3, "wednesday",       tDAY, 3 },
355         { 2, "thursday",        tDAY, 4 },
356         { 2, "friday",          tDAY, 5 },
357         { 2, "saturday",        tDAY, 6 },
358
359         /* Timezones: Offsets are in minutes. */
360         { 0, "gmt",  tZONE,     HOUR( 0) }, /* Greenwich Mean */
361         { 0, "ut",   tZONE,     HOUR( 0) }, /* Universal (Coordinated) */
362         { 0, "utc",  tZONE,     HOUR( 0) },
363         { 0, "wet",  tZONE,     HOUR( 0) }, /* Western European */
364         { 0, "bst",  tDAYZONE,  HOUR( 0) }, /* British Summer */
365         { 0, "wat",  tZONE,     HOUR( 1) }, /* West Africa */
366         { 0, "at",   tZONE,     HOUR( 2) }, /* Azores */
367         /* { 0, "bst", tZONE, HOUR( 3) }, */ /* Brazil Standard: Conflict */
368         /* { 0, "gst", tZONE, HOUR( 3) }, */ /* Greenland Standard: Conflict*/
369         { 0, "nft",  tZONE,     HOUR(3)+30 }, /* Newfoundland */
370         { 0, "nst",  tZONE,     HOUR(3)+30 }, /* Newfoundland Standard */
371         { 0, "ndt",  tDAYZONE,  HOUR(3)+30 }, /* Newfoundland Daylight */
372         { 0, "ast",  tZONE,     HOUR( 4) }, /* Atlantic Standard */
373         { 0, "adt",  tDAYZONE,  HOUR( 4) }, /* Atlantic Daylight */
374         { 0, "est",  tZONE,     HOUR( 5) }, /* Eastern Standard */
375         { 0, "edt",  tDAYZONE,  HOUR( 5) }, /* Eastern Daylight */
376         { 0, "cst",  tZONE,     HOUR( 6) }, /* Central Standard */
377         { 0, "cdt",  tDAYZONE,  HOUR( 6) }, /* Central Daylight */
378         { 0, "mst",  tZONE,     HOUR( 7) }, /* Mountain Standard */
379         { 0, "mdt",  tDAYZONE,  HOUR( 7) }, /* Mountain Daylight */
380         { 0, "pst",  tZONE,     HOUR( 8) }, /* Pacific Standard */
381         { 0, "pdt",  tDAYZONE,  HOUR( 8) }, /* Pacific Daylight */
382         { 0, "yst",  tZONE,     HOUR( 9) }, /* Yukon Standard */
383         { 0, "ydt",  tDAYZONE,  HOUR( 9) }, /* Yukon Daylight */
384         { 0, "hst",  tZONE,     HOUR(10) }, /* Hawaii Standard */
385         { 0, "hdt",  tDAYZONE,  HOUR(10) }, /* Hawaii Daylight */
386         { 0, "cat",  tZONE,     HOUR(10) }, /* Central Alaska */
387         { 0, "ahst", tZONE,     HOUR(10) }, /* Alaska-Hawaii Standard */
388         { 0, "nt",   tZONE,     HOUR(11) }, /* Nome */
389         { 0, "idlw", tZONE,     HOUR(12) }, /* Intl Date Line West */
390         { 0, "cet",  tZONE,     -HOUR(1) }, /* Central European */
391         { 0, "met",  tZONE,     -HOUR(1) }, /* Middle European */
392         { 0, "mewt", tZONE,     -HOUR(1) }, /* Middle European Winter */
393         { 0, "mest", tDAYZONE,  -HOUR(1) }, /* Middle European Summer */
394         { 0, "swt",  tZONE,     -HOUR(1) }, /* Swedish Winter */
395         { 0, "sst",  tDAYZONE,  -HOUR(1) }, /* Swedish Summer */
396         { 0, "fwt",  tZONE,     -HOUR(1) }, /* French Winter */
397         { 0, "fst",  tDAYZONE,  -HOUR(1) }, /* French Summer */
398         { 0, "eet",  tZONE,     -HOUR(2) }, /* Eastern Eur, USSR Zone 1 */
399         { 0, "bt",   tZONE,     -HOUR(3) }, /* Baghdad, USSR Zone 2 */
400         { 0, "it",   tZONE,     -HOUR(3)-30 },/* Iran */
401         { 0, "zp4",  tZONE,     -HOUR(4) }, /* USSR Zone 3 */
402         { 0, "zp5",  tZONE,     -HOUR(5) }, /* USSR Zone 4 */
403         { 0, "ist",  tZONE,     -HOUR(5)-30 },/* Indian Standard */
404         { 0, "zp6",  tZONE,     -HOUR(6) }, /* USSR Zone 5 */
405         /* { 0, "nst",  tZONE, -HOUR(6.5) }, */ /* North Sumatra: Conflict */
406         /* { 0, "sst", tZONE, -HOUR(7) }, */ /* So Sumatra, USSR 6: Conflict */
407         { 0, "wast", tZONE,     -HOUR(7) }, /* West Australian Standard */
408         { 0, "wadt", tDAYZONE,  -HOUR(7) }, /* West Australian Daylight */
409         { 0, "jt",   tZONE,     -HOUR(7)-30 },/* Java (3pm in Cronusland!)*/
410         { 0, "cct",  tZONE,     -HOUR(8) }, /* China Coast, USSR Zone 7 */
411         { 0, "jst",  tZONE,     -HOUR(9) }, /* Japan Std, USSR Zone 8 */
412         { 0, "cast", tZONE,     -HOUR(9)-30 },/* Central Australian Std */
413         { 0, "cadt", tDAYZONE,  -HOUR(9)-30 },/* Central Australian Daylt */
414         { 0, "east", tZONE,     -HOUR(10) }, /* Eastern Australian Std */
415         { 0, "eadt", tDAYZONE,  -HOUR(10) }, /* Eastern Australian Daylt */
416         { 0, "gst",  tZONE,     -HOUR(10) }, /* Guam Std, USSR Zone 9 */
417         { 0, "nzt",  tZONE,     -HOUR(12) }, /* New Zealand */
418         { 0, "nzst", tZONE,     -HOUR(12) }, /* New Zealand Standard */
419         { 0, "nzdt", tDAYZONE,  -HOUR(12) }, /* New Zealand Daylight */
420         { 0, "idle", tZONE,     -HOUR(12) }, /* Intl Date Line East */
421
422         { 0, "dst",  tDST,              0 },
423
424         /* Time units. */
425         { 4, "years",           tMONTH_UNIT,    12 },
426         { 5, "months",          tMONTH_UNIT,    1 },
427         { 9, "fortnights",      tSEC_UNIT,      14 * 24 * 60 * 60 },
428         { 4, "weeks",           tSEC_UNIT,      7 * 24 * 60 * 60 },
429         { 3, "days",            tSEC_UNIT,      1 * 24 * 60 * 60 },
430         { 4, "hours",           tSEC_UNIT,      60 * 60 },
431         { 3, "minutes",         tSEC_UNIT,      60 },
432         { 3, "seconds",         tSEC_UNIT,      1 },
433
434         /* Relative-time words. */
435         { 0, "tomorrow",        tSEC_UNIT,      1 * 24 * 60 * 60 },
436         { 0, "yesterday",       tSEC_UNIT,      -1 * 24 * 60 * 60 },
437         { 0, "today",           tSEC_UNIT,      0 },
438         { 0, "now",             tSEC_UNIT,      0 },
439         { 0, "last",            tUNUMBER,       -1 },
440         { 0, "this",            tSEC_UNIT,      0 },
441         { 0, "next",            tUNUMBER,       2 },
442         { 0, "first",           tUNUMBER,       1 },
443         { 0, "1st",             tUNUMBER,       1 },
444 /*      { 0, "second",          tUNUMBER,       2 }, */
445         { 0, "2nd",             tUNUMBER,       2 },
446         { 0, "third",           tUNUMBER,       3 },
447         { 0, "3rd",             tUNUMBER,       3 },
448         { 0, "fourth",          tUNUMBER,       4 },
449         { 0, "4th",             tUNUMBER,       4 },
450         { 0, "fifth",           tUNUMBER,       5 },
451         { 0, "5th",             tUNUMBER,       5 },
452         { 0, "sixth",           tUNUMBER,       6 },
453         { 0, "seventh",         tUNUMBER,       7 },
454         { 0, "eighth",          tUNUMBER,       8 },
455         { 0, "ninth",           tUNUMBER,       9 },
456         { 0, "tenth",           tUNUMBER,       10 },
457         { 0, "eleventh",        tUNUMBER,       11 },
458         { 0, "twelfth",         tUNUMBER,       12 },
459         { 0, "ago",             tAGO,           1 },
460
461         /* Military timezones. */
462         { 0, "a",       tZONE,  HOUR(  1) },
463         { 0, "b",       tZONE,  HOUR(  2) },
464         { 0, "c",       tZONE,  HOUR(  3) },
465         { 0, "d",       tZONE,  HOUR(  4) },
466         { 0, "e",       tZONE,  HOUR(  5) },
467         { 0, "f",       tZONE,  HOUR(  6) },
468         { 0, "g",       tZONE,  HOUR(  7) },
469         { 0, "h",       tZONE,  HOUR(  8) },
470         { 0, "i",       tZONE,  HOUR(  9) },
471         { 0, "k",       tZONE,  HOUR( 10) },
472         { 0, "l",       tZONE,  HOUR( 11) },
473         { 0, "m",       tZONE,  HOUR( 12) },
474         { 0, "n",       tZONE,  HOUR(- 1) },
475         { 0, "o",       tZONE,  HOUR(- 2) },
476         { 0, "p",       tZONE,  HOUR(- 3) },
477         { 0, "q",       tZONE,  HOUR(- 4) },
478         { 0, "r",       tZONE,  HOUR(- 5) },
479         { 0, "s",       tZONE,  HOUR(- 6) },
480         { 0, "t",       tZONE,  HOUR(- 7) },
481         { 0, "u",       tZONE,  HOUR(- 8) },
482         { 0, "v",       tZONE,  HOUR(- 9) },
483         { 0, "w",       tZONE,  HOUR(-10) },
484         { 0, "x",       tZONE,  HOUR(-11) },
485         { 0, "y",       tZONE,  HOUR(-12) },
486         { 0, "z",       tZONE,  HOUR(  0) },
487
488         /* End of table. */
489         { 0, NULL,      0,      0 }
490 };
491
492 \f
493
494
495 /* ARGSUSED */
496 static int
497 yyerror(const char *s)
498 {
499         (void)s;
500         return 0;
501 }
502
503 static time_t
504 ToSeconds(time_t Hours, time_t Minutes, time_t Seconds)
505 {
506         if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
507                 return -1;
508         if (Hours < 0 || Hours > 23)
509                 return -1;
510         return (Hours * 60L + Minutes) * 60L + Seconds;
511 }
512
513
514 /* Year is either
515  * A number from 0 to 99, which means a year from 1970 to 2069, or
516  * The actual year (>=100).  */
517 static time_t
518 Convert(time_t Month, time_t Day, time_t Year,
519         time_t Hours, time_t Minutes, time_t Seconds, DSTMODE DSTmode)
520 {
521         static int DaysInMonth[12] = {
522                 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
523         };
524         time_t  tod;
525         time_t  Julian;
526         int     i;
527
528         if (Year < 69)
529                 Year += 2000;
530         else if (Year < 100)
531                 Year += 1900;
532         DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
533             ? 29 : 28;
534         /* Checking for 2038 bogusly assumes that time_t is 32 bits.  But
535            I'm too lazy to try to check for time_t overflow in another way.  */
536         if (Year < EPOCH || Year > 2038
537             || Month < 1 || Month > 12
538             /* Lint fluff:  "conversion from long may lose accuracy" */
539             || Day < 1 || Day > DaysInMonth[(int)--Month])
540                 return -1;
541
542         Julian = Day - 1;
543         for (i = 0; i < Month; i++)
544                 Julian += DaysInMonth[i];
545         for (i = EPOCH; i < Year; i++)
546                 Julian += 365 + (i % 4 == 0);
547         Julian *= SECSPERDAY;
548         Julian += yyTimezone * 60L;
549         if ((tod = ToSeconds(Hours, Minutes, Seconds)) < 0)
550                 return -1;
551         Julian += tod;
552         if (DSTmode == DSTon
553             || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
554                 Julian -= 60 * 60;
555         return Julian;
556 }
557
558
559 static time_t
560 DSTcorrect(time_t Start, time_t Future)
561 {
562         time_t  StartDay;
563         time_t  FutureDay;
564
565         StartDay = (localtime(&Start)->tm_hour + 1) % 24;
566         FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
567         return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
568 }
569
570
571 static time_t
572 RelativeDate(time_t Start, time_t DayOrdinal, time_t DayNumber)
573 {
574         struct tm       *tm;
575         time_t  now;
576
577         now = Start;
578         tm = localtime(&now);
579         now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
580         now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
581         return DSTcorrect(Start, now);
582 }
583
584
585 static time_t
586 RelativeMonth(time_t Start, time_t RelMonth)
587 {
588         struct tm       *tm;
589         time_t  Month;
590         time_t  Year;
591
592         if (RelMonth == 0)
593                 return 0;
594         tm = localtime(&Start);
595         Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
596         Year = Month / 12;
597         Month = Month % 12 + 1;
598         return DSTcorrect(Start,
599             Convert(Month, (time_t)tm->tm_mday, Year,
600                 (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
601                 DSTmaybe));
602 }
603
604 static int
605 yylex(void)
606 {
607         char    c;
608         char    buff[64];
609
610         for ( ; ; ) {
611                 while (isspace(*yyInput))
612                         yyInput++;
613
614                 /* Skip parenthesized comments. */
615                 if (*yyInput == '(') {
616                         int Count = 0;
617                         do {
618                                 c = *yyInput++;
619                                 if (c == '\0')
620                                         return c;
621                                 if (c == '(')
622                                         Count++;
623                                 else if (c == ')')
624                                         Count--;
625                         } while (Count > 0);
626                         continue;
627                 }
628
629                 /* Try the next token in the word table first. */
630                 /* This allows us to match "2nd", for example. */
631                 {
632                         char *src = yyInput;
633                         const struct TABLE *tp;
634                         unsigned i = 0;
635
636                         /* Force to lowercase and strip '.' characters. */
637                         while (*src != '\0'
638                             && (isalnum(*src) || *src == '.')
639                             && i < sizeof(buff)-1) {
640                                 if (*src != '.') {
641                                         if (isupper(*src))
642                                                 buff[i++] = tolower(*src);
643                                         else
644                                                 buff[i++] = *src;
645                                 }
646                                 src++;
647                         }
648                         buff[i++] = '\0';
649
650                         /*
651                          * Find the first match.  If the word can be
652                          * abbreviated, make sure we match at least
653                          * the minimum abbreviation.
654                          */
655                         for (tp = TimeWords; tp->name; tp++) {
656                                 size_t abbrev = tp->abbrev;
657                                 if (abbrev == 0)
658                                         abbrev = strlen(tp->name);
659                                 if (strlen(buff) >= abbrev
660                                     && strncmp(tp->name, buff, strlen(buff))
661                                         == 0) {
662                                         /* Skip over token. */
663                                         yyInput = src;
664                                         /* Return the match. */
665                                         yylval.Number = tp->value;
666                                         return tp->type;
667                                 }
668                         }
669                 }
670
671                 /*
672                  * Not in the word table, maybe it's a number.  Note:
673                  * Because '-' and '+' have other special meanings, I
674                  * don't deal with signed numbers here.
675                  */
676                 if (isdigit(c = *yyInput)) {
677                         for (yylval.Number = 0; isdigit(c = *yyInput++); )
678                                 yylval.Number = 10 * yylval.Number + c - '0';
679                         yyInput--;
680                         return (tUNUMBER);
681                 }
682
683                 return (*yyInput++);
684         }
685 }
686
687 #define TM_YEAR_ORIGIN 1900
688
689 /* Yield A - B, measured in seconds.  */
690 static long
691 difftm (struct tm *a, struct tm *b)
692 {
693         int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
694         int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
695         int days = (
696                 /* difference in day of year */
697                 a->tm_yday - b->tm_yday
698                 /* + intervening leap days */
699                 +  ((ay >> 2) - (by >> 2))
700                 -  (ay/100 - by/100)
701                 +  ((ay/100 >> 2) - (by/100 >> 2))
702                 /* + difference in years * 365 */
703                 +  (long)(ay-by) * 365
704                 );
705         return (60*(60*(24*days + (a->tm_hour - b->tm_hour))
706             + (a->tm_min - b->tm_min))
707             + (a->tm_sec - b->tm_sec));
708 }
709
710 time_t
711 get_date(char *p)
712 {
713         struct tm       *tm;
714         struct tm       gmt, *gmt_ptr;
715         time_t          Start;
716         time_t          tod;
717         time_t          nowtime;
718         long            tzone;
719
720         yyInput = p;
721
722         (void)time (&nowtime);
723
724         gmt_ptr = gmtime (&nowtime);
725         if (gmt_ptr != NULL) {
726                 /* Copy, in case localtime and gmtime use the same buffer. */
727                 gmt = *gmt_ptr;
728         }
729
730         if (! (tm = localtime (&nowtime)))
731                 return -1;
732
733         if (gmt_ptr != NULL)
734                 tzone = difftm (&gmt, tm) / 60;
735         else
736                 /* This system doesn't understand timezones; fake it. */
737                 tzone = 0;
738         if(tm->tm_isdst)
739                 tzone += 60;
740
741         yyYear = tm->tm_year + 1900;
742         yyMonth = tm->tm_mon + 1;
743         yyDay = tm->tm_mday;
744         yyTimezone = tzone;
745         yyDSTmode = DSTmaybe;
746         yyHour = 0;
747         yyMinutes = 0;
748         yySeconds = 0;
749         yyRelSeconds = 0;
750         yyRelMonth = 0;
751         yyHaveDate = 0;
752         yyHaveDay = 0;
753         yyHaveRel = 0;
754         yyHaveTime = 0;
755         yyHaveZone = 0;
756
757         if (yyparse()
758             || yyHaveTime > 1 || yyHaveZone > 1
759             || yyHaveDate > 1 || yyHaveDay > 1)
760                 return -1;
761
762         if (yyHaveDate || yyHaveTime || yyHaveDay) {
763                 Start = Convert(yyMonth, yyDay, yyYear,
764                     yyHour, yyMinutes, yySeconds, yyDSTmode);
765                 if (Start < 0)
766                         return -1;
767         } else {
768                 Start = nowtime;
769                 if (!yyHaveRel)
770                         Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
771         }
772
773         Start += yyRelSeconds;
774         Start += RelativeMonth(Start, yyRelMonth);
775
776         if (yyHaveDay && !yyHaveDate) {
777                 tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
778                 Start += tod;
779         }
780
781         /* Have to do *something* with a legitimate -1 so it's
782          * distinguishable from the error return value.  (Alternately
783          * could set errno on error.) */
784         return Start == -1 ? 0 : Start;
785 }
786
787
788 #if     defined(TEST)
789
790 /* ARGSUSED */
791 int
792 main(int argc, char **argv)
793 {
794     time_t      d;
795
796     while (*++argv != NULL) {
797             (void)printf("Input: %s\n", *argv);
798             d = get_date(*argv);
799             if (d == -1)
800                     (void)printf("Bad format - couldn't convert.\n");
801             else
802                     (void)printf("Output: %s\n", ctime(&d));
803     }
804     exit(0);
805     /* NOTREACHED */
806 }
807 #endif  /* defined(TEST) */