1 /* $OpenBSD: strptime.c,v 1.12 2008/06/26 05:42:05 ray Exp $ */
2 /* $NetBSD: strptime.c,v 1.12 1998/01/20 21:39:40 mycroft Exp $ */
5 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
8 * This code was contributed to The NetBSD Foundation by Klaus Klein.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 /* OPENBSD ORIGINAL: lib/libc/time/strptime.c */
38 #define TM_YEAR_BASE 1900 /* from tzfile.h */
45 /* #define _ctloc(x) (_CurrentTimeLocale->x) */
48 * We do not implement alternate representations. However, we always
49 * check whether a given modifier is allowed for a certain conversion.
53 #define _LEGAL_ALT(x) { if (alt_format & ~(x)) return (0); }
56 static int _conv_num(const unsigned char **, int *, int, int);
57 static char *_strptime(const char *, const char *, struct tm *, int);
61 strptime(const char *buf, const char *fmt, struct tm *tm)
63 return(_strptime(buf, fmt, tm, 1));
67 _strptime(const char *buf, const char *fmt, struct tm *tm, int initialize)
70 const unsigned char *bp;
73 static int century, relyear;
76 century = TM_YEAR_BASE;
80 bp = (unsigned char *)buf;
81 while ((c = *fmt) != '\0') {
82 /* Clear `alternate' modifier prior to new conversion. */
85 /* Eat up white-space. */
94 if ((c = *fmt++) != '%')
98 again: switch (c = *fmt++) {
99 case '%': /* "%%" is converted to "%". */
107 * "Alternative" modifiers. Just set the appropriate flag
108 * and start over again.
110 case 'E': /* "%E?" alternative conversion modifier. */
112 alt_format |= _ALT_E;
115 case 'O': /* "%O?" alternative conversion modifier. */
117 alt_format |= _ALT_O;
121 * "Complex" conversion rules, implemented through recursion.
124 case 'c': /* Date and time, using the locale's format. */
126 if (!(bp = _strptime(bp, _ctloc(d_t_fmt), tm, 0)))
130 case 'D': /* The date as "%m/%d/%y". */
132 if (!(bp = _strptime(bp, "%m/%d/%y", tm, 0)))
136 case 'R': /* The time as "%H:%M". */
138 if (!(bp = _strptime(bp, "%H:%M", tm, 0)))
142 case 'r': /* The time as "%I:%M:%S %p". */
144 if (!(bp = _strptime(bp, "%I:%M:%S %p", tm, 0)))
148 case 'T': /* The time as "%H:%M:%S". */
150 if (!(bp = _strptime(bp, "%H:%M:%S", tm, 0)))
154 case 'X': /* The time, using the locale's format. */
156 if (!(bp = _strptime(bp, _ctloc(t_fmt), tm, 0)))
160 case 'x': /* The date, using the locale's format. */
162 if (!(bp = _strptime(bp, _ctloc(d_fmt), tm, 0)))
167 * "Elementary" conversion rules.
170 case 'A': /* The day of week, using the locale's form. */
173 for (i = 0; i < 7; i++) {
175 len = strlen(_ctloc(day[i]));
176 if (strncasecmp(_ctloc(day[i]), bp, len) == 0)
179 /* Abbreviated name. */
180 len = strlen(_ctloc(abday[i]));
181 if (strncasecmp(_ctloc(abday[i]), bp, len) == 0)
185 /* Nothing matched. */
193 case 'B': /* The month, using the locale's form. */
197 for (i = 0; i < 12; i++) {
199 len = strlen(_ctloc(mon[i]));
200 if (strncasecmp(_ctloc(mon[i]), bp, len) == 0)
203 /* Abbreviated name. */
204 len = strlen(_ctloc(abmon[i]));
205 if (strncasecmp(_ctloc(abmon[i]), bp, len) == 0)
209 /* Nothing matched. */
218 case 'C': /* The century number. */
220 if (!(_conv_num(&bp, &i, 0, 99)))
226 case 'd': /* The day of month. */
229 if (!(_conv_num(&bp, &tm->tm_mday, 1, 31)))
233 case 'k': /* The hour (24-hour clock representation). */
238 if (!(_conv_num(&bp, &tm->tm_hour, 0, 23)))
242 case 'l': /* The hour (12-hour clock representation). */
247 if (!(_conv_num(&bp, &tm->tm_hour, 1, 12)))
251 case 'j': /* The day of year. */
253 if (!(_conv_num(&bp, &tm->tm_yday, 1, 366)))
258 case 'M': /* The minute. */
260 if (!(_conv_num(&bp, &tm->tm_min, 0, 59)))
264 case 'm': /* The month. */
266 if (!(_conv_num(&bp, &tm->tm_mon, 1, 12)))
272 case 'p': /* The locale's equivalent of AM/PM. */
275 len = strlen(_ctloc(am_pm[0]));
276 if (strncasecmp(_ctloc(am_pm[0]), bp, len) == 0) {
277 if (tm->tm_hour > 12) /* i.e., 13:00 AM ?! */
279 else if (tm->tm_hour == 12)
286 len = strlen(_ctloc(am_pm[1]));
287 if (strncasecmp(_ctloc(am_pm[1]), bp, len) == 0) {
288 if (tm->tm_hour > 12) /* i.e., 13:00 PM ?! */
290 else if (tm->tm_hour < 12)
297 /* Nothing matched. */
300 case 'S': /* The seconds. */
302 if (!(_conv_num(&bp, &tm->tm_sec, 0, 61)))
306 case 'U': /* The week of year, beginning on sunday. */
307 case 'W': /* The week of year, beginning on monday. */
310 * XXX This is bogus, as we can not assume any valid
311 * information present in the tm structure at this
312 * point to calculate a real value, so just check the
315 if (!(_conv_num(&bp, &i, 0, 53)))
319 case 'w': /* The day of week, beginning on sunday. */
321 if (!(_conv_num(&bp, &tm->tm_wday, 0, 6)))
325 case 'Y': /* The year. */
327 if (!(_conv_num(&bp, &i, 0, 9999)))
331 tm->tm_year = i - TM_YEAR_BASE;
334 case 'y': /* The year within the century (2 digits). */
335 _LEGAL_ALT(_ALT_E | _ALT_O);
336 if (!(_conv_num(&bp, &relyear, 0, 99)))
341 * Miscellaneous conversions.
343 case 'n': /* Any kind of white-space. */
351 default: /* Unknown/unsupported conversion. */
359 * We need to evaluate the two digit year spec (%y)
360 * last as we can get a century spec (%C) at any time.
363 if (century == TM_YEAR_BASE) {
365 tm->tm_year = relyear + 2000 - TM_YEAR_BASE;
367 tm->tm_year = relyear + 1900 - TM_YEAR_BASE;
369 tm->tm_year = relyear + century - TM_YEAR_BASE;
378 _conv_num(const unsigned char **buf, int *dest, int llim, int ulim)
383 if (**buf < '0' || **buf > '9')
386 /* we use rulim to break out of the loop when we run out of digits */
389 result += *(*buf)++ - '0';
391 } while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9');
393 if (result < llim || result > ulim)
400 #endif /* HAVE_STRPTIME */