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