1 /* strptime.c - partial strptime() reimplementation
3 * (c) 2008 Richard Kettlewell.
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.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 /* strptime() is here reimplemented because the FreeBSD (and older MacOS) one
31 * is broken and does not report errors properly. See TODO remarks below for
32 * some missing bits. */
40 /** @brief Lookup table entry for locale-specific strings */
41 struct locale_item_match {
42 /** @brief Locale key to try */
45 /** @brief Value to return if value of @ref key matches subject string */
49 static const struct locale_item_match days[] = {
67 static const struct locale_item_match months[] = {
95 /** @brief Match a string
96 * @param buf Start of subject
97 * @param limit End of subject
98 * @param match String to match subject against
99 * @return True if match == [buf,limit) otherwise false
101 * The match is case-independent at least in ASCII.
103 static int try_match(const char *buf,
106 /* TODO this won't work well outside single-byte encodings. A good bet is
107 * probably to convert to Unicode and then use utf32_casefold_compat() (or
108 * utf8_casefold_compat(); using compatibility matching will ensure missing
109 * accents and so on aren't a problem.
111 * en_GB and en_US will probably be in any reasonable encoding for them.
113 while(buf < limit && *match) {
114 if(tolower((unsigned char)*buf) != tolower((unsigned char)*match))
119 if(buf != limit || *match)
124 /** @brief Match from table of locale-specific strings
125 * @param buf Start of subject
126 * @param limit End of subject
127 * @param lim Table of locale lookups
128 * @return Looked up value or -1
130 * The match is case-independent.
132 static int try_locale_match(const char *buf,
134 const struct locale_item_match *lim) {
135 /* This is not very efficient! A (correct) built-in implementation will
136 * presumably have more direct access to locale information. */
137 while(lim->value != -1) {
138 if(try_match(buf, limit, nl_langinfo(lim->key)))
145 static int try_numeric_match(const char *buf,
152 int ch = (unsigned char)*buf++;
153 if(ch >= '0' && ch <= '9') {
155 || (n == INT_MAX / 10 && ch >= INT_MAX % 10 + '0'))
156 return -1; /* overflow */
157 n = 10 * n + ch - '0';
161 if(n < low || n > high)
166 static const char *my_strptime_guts(const char *buf,
169 int fc, mod, spec, next, value;
171 /* nl_langinfo() is allowed to trash its last return value so we copy.
172 * (We're relying on it being usable at all in multithreaded environments
174 #define USE_SUBFORMAT(ITEM, EITEM, DEF) do { \
176 char subformat[128]; \
179 s = nl_langinfo(EITEM); \
181 s = nl_langinfo(ITEM); \
183 s = nl_langinfo(ITEM); \
186 if(strlen(s) >= sizeof subformat) \
188 strcpy(subformat, s); \
189 if(!(buf = my_strptime_guts(buf, subformat, tm))) \
194 fc = (unsigned char)*format++;
196 /* Get the character defining the converstion specification */
197 spec = (unsigned char)*format++;
198 if(spec == 'E' || spec == 'O') {
199 /* Oops, there's a modifier first */
201 spec = (unsigned char)*format++;
205 return NULL; /* format string broken! */
206 /* See what the next directive is. The specification is written in terms
207 * of stopping the match at a character that matches the next directive.
208 * This implementation mirrors this aspect of the specification
210 next = (unsigned char)*format;
214 /* Next directive is whitespace, so bound the input string (at least)
216 while(*limit && !isspace((unsigned char)*limit))
218 } else if(next == '%') {
219 /* Prohibited: "The application shall ensure that there is
220 * white-space or other non-alphanumeric characters between any two
221 * conversion specifications". In fact we let alphanumerics
224 * Forbidding even %% seems a bit harsh but is consistent with the
225 * specification as written.
229 /* Next directive is a specific character, so bound the input string
230 * (at least) by that. This will work badly in the face of multibyte
231 * characters, but then the spec is vague about what kind of string
232 * we're dealing with anyway so you probably couldn't safely use them
233 * in the format string at least in any case. */
234 while(*limit && *limit != next)
238 limit = buf + strlen(buf);
240 case 'A': case 'a': /* day name (abbrev or full) */
241 if((value = try_locale_match(buf, limit, days)) == -1)
245 case 'B': case 'b': case 'h': /* month name (abbrev or full) */
246 if((value = try_locale_match(buf, limit, months)) == -1)
248 tm->tm_mon = value - 1;
250 case 'c': /* locale date+time */
251 USE_SUBFORMAT(D_T_FMT, ERA_D_T_FMT, "%a %b %e %H:%M:%S %Y");
253 case 'C': /* century number 0-99 */
256 case 'd': case 'e': /* day of month 1-31 */
257 if((value = try_numeric_match(buf, limit, 1, 31)) == -1)
261 case 'D': /* == "%m / %d / %y" */
262 if(!(buf = my_strptime_guts(buf, "%m / %d / %y", tm)))
265 case 'H': /* hour 0-23 */
266 if((value = try_numeric_match(buf, limit, 0, 23)) == -1)
270 case 'I': /* hour 1-12 */
273 case 'j': /* day 1-366 */
274 if((value = try_numeric_match(buf, limit, 1, 366)) == -1)
276 tm->tm_yday = value - 1;
278 case 'm': /* month 1-12 */
279 if((value = try_numeric_match(buf, limit, 1, 12)) == -1)
281 tm->tm_mon = value - 1;
283 case 'M': /* minute 0-59 */
284 if((value = try_numeric_match(buf, limit, 0, 59)) == -1)
288 case 'n': case 't': /* any whitespace */
289 goto matchwhitespace;
290 case 'p': /* locale am/pm */
293 case 'r': /* == "%I : %M : %S %p" */
294 /* TODO actually this is locale-dependent; and we don't implement %I
295 * anyway, so it's not going to work even as it stands. */
296 if(!(buf = my_strptime_guts(buf, "%I : %M : %S %p", tm)))
299 case 'R': /* == "%H : %M" */
300 if(!(buf = my_strptime_guts(buf, "%H : %M", tm)))
303 case 'S': /* seconds 0-60 */
304 if((value = try_numeric_match(buf, limit, 0, 60)) == -1)
308 case 'U': /* week number from Sunday 0-53 */
311 case 'w': /* day number 0-6 from Sunday */
312 if((value = try_numeric_match(buf, limit, 0, 6)) == -1)
316 case 'W': /* week number from Monday 0-53 */
319 case 'x': /* locale date format */
320 USE_SUBFORMAT(D_FMT, ERA_D_FMT, "%m/%d/%y");
322 case 'X': /* locale time format */
323 USE_SUBFORMAT(T_FMT, ERA_T_FMT, "%H:%M:%S");
325 case 'y': /* year mod 100 */
326 if((value = try_numeric_match(buf, limit, 0, INT_MAX)) == -1)
328 if(value >= 0 && value <= 68)
329 value = 2000 + value;
330 else if(value >= 69 && value <= 99)
331 value = 1900 + value;
332 tm->tm_year = value - 1900;
335 if((value = try_numeric_match(buf, limit, 1, INT_MAX)) == -1)
337 tm->tm_year = value - 1900;
342 /* The spec is a bit vague about what to do with invalid format
343 * strings. We return NULL immediately and hope someone will
348 } else if(isspace(fc)) {
350 /* Any format whitespace matches any number of input whitespace
351 * characters. The directive can formally contain more than one
352 * whitespace character; for the second and subsequent ones we'll match 0
353 * characters from the input. */
354 while(isspace((unsigned char)*buf))
358 /* Non-% non-whitespace characters must match themselves exactly */
359 if(fc != (unsigned char)*buf++)
363 /* When we run out of format string we return a pointer to the rest of the
368 /** @brief Reimplementation of strptime()
369 * @param buf Input buffer
370 * @param format Format string
371 * @param tm Where to put result
372 * @return Pointer to first unparsed input character, or NULL on error
375 * href="http://www.opengroup.org/onlinepubs/009695399/functions/strptime.html">http://www.opengroup.org/onlinepubs/009695399/functions/strptime.html</a>.
377 char *my_strptime(const char *buf,
380 /* Whether to overwrite or update is unspecified (rather bizarrely). This
381 * implementation does not overwrites, as xgetdate() depends on this
384 if(!(buf = my_strptime_guts(buf, format, tm)))
386 /* TODO various things we could/should do:
387 * - infer day/month from %j+year
388 * - infer day/month from %U/%W+%w/%a+year
389 * - infer hour from %p+%I
390 * - fill wday/yday from other fields