1 /* tdate_parse - parse string dates into internal form, stripped-down version
3 ** Copyright © 1995 by Jef Poskanzer <jef@mail.acme.com>.
4 ** All rights reserved.
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions
9 ** 1. Redistributions of source code must retain the above copyright
10 ** notice, this list of conditions and the following disclaimer.
11 ** 2. Redistributions in binary form must reproduce the above copyright
12 ** notice, this list of conditions and the following disclaimer in the
13 ** documentation and/or other materials provided with the distribution.
15 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 /* This is a stripped-down version of date_parse.c, available at
29 ** http://www.acme.com/software/date_parse/
32 #include <sys/types.h>
43 #include "tdate_parse.h"
53 pound_case( char* str )
55 for ( ; *str != '\0'; ++str )
57 if ( isupper( (int) *str ) )
58 *str = tolower( (int) *str );
63 strlong_compare( v1, v2 )
67 return strcmp( ((struct strlong*) v1)->s, ((struct strlong*) v2)->s );
72 strlong_search( char* str, struct strlong* tab, int n, long* lP )
81 r = strcmp( str, tab[i].s );
98 scan_wday( char* str_wday, long* tm_wdayP )
100 static struct strlong wday_tab[] = {
101 { "sun", 0 }, { "sunday", 0 },
102 { "mon", 1 }, { "monday", 1 },
103 { "tue", 2 }, { "tuesday", 2 },
104 { "wed", 3 }, { "wednesday", 3 },
105 { "thu", 4 }, { "thursday", 4 },
106 { "fri", 5 }, { "friday", 5 },
107 { "sat", 6 }, { "saturday", 6 },
109 static int sorted = 0;
114 wday_tab, sizeof(wday_tab)/sizeof(struct strlong),
115 sizeof(struct strlong), strlong_compare );
118 pound_case( str_wday );
119 return strlong_search(
120 str_wday, wday_tab, sizeof(wday_tab)/sizeof(struct strlong), tm_wdayP );
125 scan_mon( char* str_mon, long* tm_monP )
127 static struct strlong mon_tab[] = {
128 { "jan", 0 }, { "january", 0 },
129 { "feb", 1 }, { "february", 1 },
130 { "mar", 2 }, { "march", 2 },
131 { "apr", 3 }, { "april", 3 },
133 { "jun", 5 }, { "june", 5 },
134 { "jul", 6 }, { "july", 6 },
135 { "aug", 7 }, { "august", 7 },
136 { "sep", 8 }, { "september", 8 },
137 { "oct", 9 }, { "october", 9 },
138 { "nov", 10 }, { "november", 10 },
139 { "dec", 11 }, { "december", 11 },
141 static int sorted = 0;
146 mon_tab, sizeof(mon_tab)/sizeof(struct strlong),
147 sizeof(struct strlong), strlong_compare );
150 pound_case( str_mon );
151 return strlong_search(
152 str_mon, mon_tab, sizeof(mon_tab)/sizeof(struct strlong), tm_monP );
159 return year % 400? ( year % 100 ? ( year % 4 ? 0 : 1 ) : 0 ) : 1;
163 /* Basically the same as mktime(). */
165 tm_to_time( struct tm* tmP )
168 static int monthtab[12] = {
169 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
171 /* Years since epoch, converted to days. */
172 t = ( tmP->tm_year - 70 ) * 365;
173 /* Leap days for previous years. */
174 t += ( tmP->tm_year - 69 ) / 4;
175 /* Days for the beginning of this month. */
176 t += monthtab[tmP->tm_mon];
177 /* Leap day for this year. */
178 if ( tmP->tm_mon >= 2 && is_leap( tmP->tm_year + 1900 ) )
180 /* Days since the beginning of this month. */
181 t += tmP->tm_mday - 1; /* 1-based field */
182 /* Hours, minutes, and seconds. */
183 t = t * 24 + tmP->tm_hour;
184 t = t * 60 + tmP->tm_min;
185 t = t * 60 + tmP->tm_sec;
192 tdate_parse( char* str )
196 char str_mon[500], str_wday[500];
197 int tm_sec, tm_min, tm_hour, tm_mday, tm_year;
198 long tm_mon, tm_wday;
202 (void) memset( (char*) &tm, 0, sizeof(struct tm) );
204 /* Skip initial whitespace ourselves - sscanf is clumsy at this. */
205 for ( cp = str; *cp == ' ' || *cp == '\t'; ++cp )
208 /* And do the sscanfs. WARNING: you can add more formats here,
209 ** but be careful! You can easily screw up the parsing of existing
210 ** formats when you add new ones. The order is important.
213 /* DD-mth-YY HH:MM:SS GMT */
214 if ( sscanf( cp, "%d-%400[a-zA-Z]-%d %d:%d:%d GMT",
215 &tm_mday, str_mon, &tm_year, &tm_hour, &tm_min,
217 scan_mon( str_mon, &tm_mon ) )
219 tm.tm_mday = tm_mday;
221 tm.tm_year = tm_year;
222 tm.tm_hour = tm_hour;
227 /* DD mth YY HH:MM:SS GMT */
228 else if ( sscanf( cp, "%d %400[a-zA-Z] %d %d:%d:%d GMT",
229 &tm_mday, str_mon, &tm_year, &tm_hour, &tm_min,
231 scan_mon( str_mon, &tm_mon ) )
233 tm.tm_mday = tm_mday;
235 tm.tm_year = tm_year;
236 tm.tm_hour = tm_hour;
241 /* HH:MM:SS GMT DD-mth-YY */
242 else if ( sscanf( cp, "%d:%d:%d GMT %d-%400[a-zA-Z]-%d",
243 &tm_hour, &tm_min, &tm_sec, &tm_mday, str_mon,
245 scan_mon( str_mon, &tm_mon ) )
247 tm.tm_hour = tm_hour;
250 tm.tm_mday = tm_mday;
252 tm.tm_year = tm_year;
255 /* HH:MM:SS GMT DD mth YY */
256 else if ( sscanf( cp, "%d:%d:%d GMT %d %400[a-zA-Z] %d",
257 &tm_hour, &tm_min, &tm_sec, &tm_mday, str_mon,
259 scan_mon( str_mon, &tm_mon ) )
261 tm.tm_hour = tm_hour;
264 tm.tm_mday = tm_mday;
266 tm.tm_year = tm_year;
269 /* wdy, DD-mth-YY HH:MM:SS GMT */
270 else if ( sscanf( cp, "%400[a-zA-Z], %d-%400[a-zA-Z]-%d %d:%d:%d GMT",
271 str_wday, &tm_mday, str_mon, &tm_year, &tm_hour, &tm_min,
273 scan_wday( str_wday, &tm_wday ) &&
274 scan_mon( str_mon, &tm_mon ) )
276 tm.tm_wday = tm_wday;
277 tm.tm_mday = tm_mday;
279 tm.tm_year = tm_year;
280 tm.tm_hour = tm_hour;
285 /* wdy, DD mth YY HH:MM:SS GMT */
286 else if ( sscanf( cp, "%400[a-zA-Z], %d %400[a-zA-Z] %d %d:%d:%d GMT",
287 str_wday, &tm_mday, str_mon, &tm_year, &tm_hour, &tm_min,
289 scan_wday( str_wday, &tm_wday ) &&
290 scan_mon( str_mon, &tm_mon ) )
292 tm.tm_wday = tm_wday;
293 tm.tm_mday = tm_mday;
295 tm.tm_year = tm_year;
296 tm.tm_hour = tm_hour;
301 /* wdy mth DD HH:MM:SS GMT YY */
302 else if ( sscanf( cp, "%400[a-zA-Z] %400[a-zA-Z] %d %d:%d:%d GMT %d",
303 str_wday, str_mon, &tm_mday, &tm_hour, &tm_min, &tm_sec,
305 scan_wday( str_wday, &tm_wday ) &&
306 scan_mon( str_mon, &tm_mon ) )
308 tm.tm_wday = tm_wday;
310 tm.tm_mday = tm_mday;
311 tm.tm_hour = tm_hour;
314 tm.tm_year = tm_year;
319 if ( tm.tm_year > 1900 )
321 else if ( tm.tm_year < 70 )
324 t = tm_to_time( &tm );