1 /* dtimep.l -- parse dates and times.
3 * This exceeds the default table capacities for some old versions
4 * of lex (and the minimum defaults as specified by POSIX). The following
5 * choices meet or exceed the lex defaults for older SunOS4.x, Solaris,
17 /* Since we're looking at a string at a time, don't worry about
18 * wrapping to the next buffer.
20 #if YY_FLEX_MAJOR_VERSION == 2 && \
21 YY_FLEX_MINOR_VERSION == 6 && \
22 YY_FLEX_SUBMINOR_VERSION == 3
23 /* https://github.com/westes/flex/issues/162 */
27 #define YY_SKIP_YYWRAP
31 /* This is the tricky thing that makes this function cool. We
32 * replace the traditional int yylex(void) declaration with our
33 * dparsetime() declaration, essentially piggy-backing off the
34 * utility of the yylex() function and adding what we need to make
35 * the parsing function useful to us.
37 #define YY_DECL struct tws *dparsetime(char *lexstr)
39 /* yyterminate() is called after the input string is matched to
40 * completion (actually, when the lexer reaches an EOF). The only
41 * thing that really needs to be in this macro function is the
42 * return call, which must be substituted inline into dparsetime.
45 #define yyterminate() (void)yy_delete_buffer(lexhandle); \
46 if(!(tw.tw_flags & TW_SUCC)) { \
47 return (struct tws *)NULL; \
49 if(tw.tw_year < 1970) \
51 if(tw.tw_year < 1970) \
56 * Table to convert month names to numeric month. We use the
57 * fact that the low order 5 bits of the sum of the 2nd & 3rd
58 * characters of the name is a hash with no collisions for the 12
59 * valid month names. (The mask to 5 bits maps any combination of
60 * upper and lower case into the same hash value).
62 static int month_map[] = {
95 * Lookup table for day-of-week using the same hash trick as for above name-of-
96 * month table, but using the first and second character, not second and third.
98 * Compute index into table using: (day_name[0] & 7) + (day_name[1] & 4)
100 static int day_map[] = {
115 /* The SET* macros will parse for the appropriate field, and leave the
116 * cp pointer at the first character after the desired field. Be
117 * careful with variable-length fields or alpha-num mixes.
119 * The SKIP* macros skip over characters of a particular class and
120 * leave cp at the position of the first character that doesn't match
121 * that class. Correspondingly, SKIPTO* skips until it reaches a
122 * character of a particular class.
125 #define INIT() { cp = yytext;}
126 #define SETWDAY() { tw.tw_wday= day_map[(cp[0] & 7) + (cp[1] & 4)]; \
127 tw.tw_flags &= ~TW_SDAY; tw.tw_flags |= TW_SEXP; \
129 #define SETMON() { cp++; \
130 tw.tw_mon = month_map[(((unsigned char) cp[0]) + ((unsigned char) cp[1])) & 0x1f]; \
132 #define SETMON_NUM() { tw.tw_mon = atoi(cp)-1; \
134 #define SETYEAR() { tw.tw_year = atoi(cp); \
136 #define SETDAY() { tw.tw_mday = atoi(cp); \
137 tw.tw_flags |= TW_SUCC; \
139 #define SETTIME() { tw.tw_hour = atoi(cp); \
142 tw.tw_min = atoi(cp); \
145 tw.tw_sec = atoi(++cp); SKIPD(); } }
146 #define SETZONE(x) { tw.tw_zone = ((x)/100)*60+(x)%100; \
147 tw.tw_flags |= TW_SZEXP; \
149 #define SETDST() { tw.tw_flags |= TW_DST; }
150 #define SKIPD() { while ( isdigit((unsigned char) *cp++) ) ; \
152 #define SKIPTOD() { while ( !isdigit((unsigned char) *cp++) ) ; \
154 #define SKIPA() { while ( isalpha((unsigned char) *cp++) ) ; \
156 #define SKIPTOA() { while ( !isalpha((unsigned char) *cp++) ) ; \
158 #define SKIPSP() { while ( isspace((unsigned char) *cp++) ) ; \
160 #define SKIPTOSP() { while ( !isspace((unsigned char) *cp++) ) ; \
163 # ifdef HAVE_SYS_TIME_H
164 # include <sys/time.h>
169 zonehack (struct tws *tw)
173 if (dmktime (tw) == (time_t) -1)
176 tm = localtime (&tw->tw_clock);
178 tw->tw_flags |= TW_DST;
192 DAY {sun}|{mon}|{tue}|{wed}|{thu}|{fri}|{sat}
207 MONTH {jan}|{feb}|{mar}|{apr}|{may}|{jun}|{jul}|{aug}|{sep}|{oct}|{nov}|{dec}
209 TIME {D}:{d}{d}(:{d}{d})?
211 /* The year can either be 2 digits, or 4. However, after
212 Y2K, we found that some MUA were reporting the year 100, hence
213 the middle term here. yyterminate() resolves the actual
214 issues with 2-digit years.
217 YEAR ({d}{d})|(1{d}{d})|({d}{4})
227 /* This section begins the definition of dparsetime().
228 Put here any local variable definitions and initializations */
230 YY_BUFFER_STATE lexhandle;
233 static struct tws tw;
237 lexhandle = yy_scan_string(lexstr);
240 {DAY}","?{W}{MONTH}{W}{D}{W}{TIME}{W}{YEAR} {
253 {DAY}","?{W}{D}{W}{MONTH}{W}{YEAR}{W}{TIME} {
265 {D}{W}{MONTH}{W}{YEAR}{W}{TIME} {
275 {DAY}","?{W}{MONTH}{W}{D}","?{W}{YEAR}","?{W}{TIME} {
287 {DAY}","?{W}{MONTH}{W}{D}","?{W}{YEAR} {
297 {MONTH}{W}{D}","?{W}{YEAR}","?{W}{DAY} {
307 {MONTH}{W}{D}","?{W}{YEAR} {
315 {D}("-"|"/"){D}("-"|"/"){YEAR}{W}{TIME} {
326 {D}("-"|"/"){D}("-"|"/"){YEAR} {
337 "[Pp][Mm]" tw.tw_hour += 12;
354 {nl}("ut"|"UT") INIT(); SETZONE(0); yyterminate();
355 {nl}("gmt"|"GMT") INIT(); SETZONE(0); yyterminate();
356 {nl}("est"|"EST") INIT(); SETZONE(-500); yyterminate();
357 {nl}("edt"|"EDT") { INIT(); SETDST(); SETZONE(-500);
359 {nl}("cst"|"CST") INIT(); SETZONE(-600); yyterminate();
360 {nl}("cdt"|"CDT") { INIT(); SETDST(); SETZONE(-600);
362 {nl}("mst"|"MST") INIT(); SETZONE(-700); yyterminate();
363 {nl}("mdt"|"MDT") { INIT(); SETDST(); SETZONE(-700);
365 {nl}("pst"|"PST") INIT(); SETZONE(-800); yyterminate();
366 {nl}("pdt"|"PDT") { INIT(); SETDST(); SETZONE(-800);
368 {nl}("nst"|"NST") INIT(); SETZONE(-330); yyterminate();
369 {nl}("ast"|"AST") INIT(); SETZONE(-400); yyterminate();
370 {nl}("adt"|"ADT") { INIT(); SETDST(); SETZONE(-400);
372 {nl}("hst"|"HST") INIT(); SETZONE(-1000); yyterminate();
373 {nl}("hdt"|"HDT") { INIT(); SETDST(); SETZONE(-1000);
378 /* This is a portable way to squash a warning about the yyunput()
379 * function being static but never used. It costs us a tiny amount
380 * of extra code in the binary but the other options are:
381 * "%option nounput" which is flex-specific
382 * makefile hackery just to compile dtimep.c with different flags
384 void dtimep_yyunput(int c)