2 * Powerdog Industries kindly requests feedback from anyone modifying
5 * Date: Thu, 05 Jun 1997 23:17:17 -0400
6 * From: Kevin Ruddy <kevin.ruddy@powerdog.com>
7 * To: James FitzGibbon <james@nexis.net>
8 * Subject: Re: Use of your strptime(3) code (fwd)
10 * The reason for the "no mod" clause was so that modifications would
11 * come back and we could integrate them and reissue so that a wider
12 * audience could use it (thereby spreading the wealth). This has
13 * made it possible to get strptime to work on many operating systems.
14 * I'm not sure why that's "plain unacceptable" to the FreeBSD team.
16 * Anyway, you can change it to "with or without modification" as
20 * Powerdog Industries, Inc.
22 * @(#) Copyright (c) 1994 Powerdog Industries. All rights reserved.
23 * @(#)strptime.c 0.1 (Powerdog) 94/03/27
24 * $FreeBSD: src/lib/libc/stdtime/strptime.c,v 1.17.2.3 2002/03/12 17:24:54 phantom Exp $
25 * $DragonFly: src/lib/libc/stdtime/strptime.c,v 1.3 2005/01/31 22:29:44 dillon Exp $
28 * Copyright (c) 1994 Powerdog Industries. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer
37 * in the documentation and/or other materials provided with the
39 * 3. All advertising materials mentioning features or use of this
40 * software must display the following acknowledgement:
41 * This product includes software developed by Powerdog Industries.
42 * 4. The name of Powerdog Industries may not be used to endorse or
43 * promote products derived from this software without specific prior
46 * THIS SOFTWARE IS PROVIDED BY POWERDOG INDUSTRIES ``AS IS'' AND ANY
47 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE POWERDOG INDUSTRIES BE
50 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
53 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
54 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
55 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
56 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 #include "namespace.h"
66 #include "un-namespace.h"
67 #include "libc_private.h"
68 #include "timelocal.h"
70 static char * _strptime(const char *, const char *, struct tm *);
72 static pthread_mutex_t gotgmt_mutex = PTHREAD_MUTEX_INITIALIZER;
75 #define asizeof(a) (sizeof (a) / sizeof ((a)[0]))
78 _strptime(const char *buf, const char *fmt, struct tm *tm)
84 int Ealternative, Oalternative;
85 struct lc_time_T *tptr = __get_current_time_locale();
95 if (isspace((unsigned char)c))
96 while (*buf != 0 && isspace((unsigned char)*buf))
115 buf = _strptime(buf, tptr->date_fmt, tm);
121 if (!isdigit((unsigned char)*buf))
124 /* XXX This will break for 3-digit centuries. */
126 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
134 tm->tm_year = i * 100 - 1900;
138 buf = _strptime(buf, tptr->c_fmt, tm);
144 buf = _strptime(buf, "%m/%d/%y", tm);
150 if (Ealternative || Oalternative)
156 if (Ealternative || Oalternative)
162 buf = _strptime(buf, "%Y-%m-%d", tm);
168 buf = _strptime(buf, "%H:%M", tm);
174 buf = _strptime(buf, tptr->ampm_fmt, tm);
180 buf = _strptime(buf, "%H:%M:%S", tm);
186 buf = _strptime(buf, tptr->X_fmt, tm);
192 buf = _strptime(buf, tptr->x_fmt, tm);
198 if (!isdigit((unsigned char)*buf))
202 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
207 if (i < 1 || i > 366)
215 if (*buf == 0 || isspace((unsigned char)*buf))
218 if (!isdigit((unsigned char)*buf))
222 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
238 if (*buf != 0 && isspace((unsigned char)*buf))
239 while (*ptr != 0 && !isspace((unsigned char)*ptr))
248 * Of these, %l is the only specifier explicitly
249 * documented as not being zero-padded. However,
250 * there is no harm in allowing zero-padding.
252 * XXX The %l specifier may gobble one too many
253 * digits if used incorrectly.
255 if (!isdigit((unsigned char)*buf))
259 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
264 if (c == 'H' || c == 'k') {
272 if (*buf != 0 && isspace((unsigned char)*buf))
273 while (*ptr != 0 && !isspace((unsigned char)*ptr))
279 * XXX This is bogus if parsed before hour-related
282 len = strlen(tptr->am);
283 if (strncasecmp(buf, tptr->am, len) == 0) {
284 if (tm->tm_hour > 12)
286 if (tm->tm_hour == 12)
292 len = strlen(tptr->pm);
293 if (strncasecmp(buf, tptr->pm, len) == 0) {
294 if (tm->tm_hour > 12)
296 if (tm->tm_hour != 12)
306 for (i = 0; i < asizeof(tptr->weekday); i++) {
307 len = strlen(tptr->weekday[i]);
308 if (strncasecmp(buf, tptr->weekday[i],
311 len = strlen(tptr->wday[i]);
312 if (strncasecmp(buf, tptr->wday[i],
316 if (i == asizeof(tptr->weekday))
326 * XXX This is bogus, as we can not assume any valid
327 * information present in the tm structure at this
328 * point to calculate a real value, so just check the
331 if (!isdigit((unsigned char)*buf))
335 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
343 if (*buf != 0 && isspace((unsigned char)*buf))
344 while (*ptr != 0 && !isspace((unsigned char)*ptr))
349 if (!isdigit((unsigned char)*buf))
358 if (*buf != 0 && isspace((unsigned char)*buf))
359 while (*ptr != 0 && !isspace((unsigned char)*ptr))
366 * The %e specifier is explicitly documented as not
367 * being zero-padded but there is no harm in allowing
370 * XXX The %e specifier may gobble one too many
371 * digits if used incorrectly.
373 if (!isdigit((unsigned char)*buf))
377 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
387 if (*buf != 0 && isspace((unsigned char)*buf))
388 while (*ptr != 0 && !isspace((unsigned char)*ptr))
395 for (i = 0; i < asizeof(tptr->month); i++) {
398 len = strlen(tptr->alt_month[i]);
405 len = strlen(tptr->month[i]);
406 if (strncasecmp(buf, tptr->month[i],
409 len = strlen(tptr->mon[i]);
410 if (strncasecmp(buf, tptr->mon[i],
415 if (i == asizeof(tptr->month))
423 if (!isdigit((unsigned char)*buf))
427 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
437 if (*buf != 0 && isspace((unsigned char)*buf))
438 while (*ptr != 0 && !isspace((unsigned char)*ptr))
447 t = strtol(buf, &cp, 10);
458 if (*buf == 0 || isspace((unsigned char)*buf))
461 if (!isdigit((unsigned char)*buf))
464 len = (c == 'Y') ? 4 : 2;
465 for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
472 if (c == 'y' && i < 69)
479 if (*buf != 0 && isspace((unsigned char)*buf))
480 while (*ptr != 0 && !isspace((unsigned char)*ptr))
489 for (cp = buf; *cp && isupper((unsigned char)*cp); ++cp) {/*empty*/}
491 zonestr = alloca(cp - buf + 1);
492 strncpy(zonestr, buf, cp - buf);
493 zonestr[cp - buf] = '\0';
495 if (0 == strcmp(zonestr, "GMT")) {
497 } else if (0 == strcmp(zonestr, tzname[0])) {
499 } else if (0 == strcmp(zonestr, tzname[1])) {
515 strptime(const char *buf, const char *fmt, struct tm *tm)
521 _pthread_mutex_lock(&gotgmt_mutex);
523 ret = _strptime(buf, fmt, tm);
524 if (ret && got_GMT) {
525 time_t t = timegm(tm);
531 _pthread_mutex_unlock(&gotgmt_mutex);