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