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