strptime.c revision 48614
128019Sjoerg/*
228021Sjoerg * Powerdog Industries kindly requests feedback from anyone modifying
328021Sjoerg * this function:
428021Sjoerg *
528021Sjoerg * Date: Thu, 05 Jun 1997 23:17:17 -0400
628021Sjoerg * From: Kevin Ruddy <kevin.ruddy@powerdog.com>
728021Sjoerg * To: James FitzGibbon <james@nexis.net>
828021Sjoerg * Subject: Re: Use of your strptime(3) code (fwd)
928021Sjoerg *
1028021Sjoerg * The reason for the "no mod" clause was so that modifications would
1128021Sjoerg * come back and we could integrate them and reissue so that a wider
1228021Sjoerg * audience could use it (thereby spreading the wealth).  This has
1328021Sjoerg * made it possible to get strptime to work on many operating systems.
1428021Sjoerg * I'm not sure why that's "plain unacceptable" to the FreeBSD team.
1528021Sjoerg *
1628021Sjoerg * Anyway, you can change it to "with or without modification" as
1728021Sjoerg * you see fit.  Enjoy.
1828021Sjoerg *
1928021Sjoerg * Kevin Ruddy
2028021Sjoerg * Powerdog Industries, Inc.
2128021Sjoerg */
2228021Sjoerg/*
2328019Sjoerg * Copyright (c) 1994 Powerdog Industries.  All rights reserved.
2428019Sjoerg *
2528021Sjoerg * Redistribution and use in source and binary forms, with or without
2628019Sjoerg * modification, are permitted provided that the following conditions
2728019Sjoerg * are met:
2828019Sjoerg * 1. Redistributions of source code must retain the above copyright
2928019Sjoerg *    notice, this list of conditions and the following disclaimer.
3028019Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
3128019Sjoerg *    notice, this list of conditions and the following disclaimer
3228019Sjoerg *    in the documentation and/or other materials provided with the
3328019Sjoerg *    distribution.
3428019Sjoerg * 3. All advertising materials mentioning features or use of this
3528019Sjoerg *    software must display the following acknowledgement:
3628019Sjoerg *      This product includes software developed by Powerdog Industries.
3728019Sjoerg * 4. The name of Powerdog Industries may not be used to endorse or
3828019Sjoerg *    promote products derived from this software without specific prior
3928019Sjoerg *    written permission.
4028019Sjoerg *
4128019Sjoerg * THIS SOFTWARE IS PROVIDED BY POWERDOG INDUSTRIES ``AS IS'' AND ANY
4228019Sjoerg * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4328019Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4428019Sjoerg * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE POWERDOG INDUSTRIES BE
4528019Sjoerg * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4628019Sjoerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4728019Sjoerg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
4828019Sjoerg * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4928019Sjoerg * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
5028019Sjoerg * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
5128019Sjoerg * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5228019Sjoerg */
5328019Sjoerg
5428021Sjoerg#ifdef LIBC_RCS
5528021Sjoergstatic const char rcsid[] =
5648550Sobrien	"$Id: strptime.c,v 1.6 1999/04/25 07:28:39 wes Exp $";
5728021Sjoerg#endif
5828021Sjoerg
5928019Sjoerg#ifndef lint
6028021Sjoerg#ifndef NOID
6128019Sjoergstatic char copyright[] =
6228019Sjoerg"@(#) Copyright (c) 1994 Powerdog Industries.  All rights reserved.";
6328021Sjoergstatic char sccsid[] = "@(#)strptime.c	0.1 (Powerdog) 94/03/27";
6428021Sjoerg#endif /* !defined NOID */
6528019Sjoerg#endif /* not lint */
6628019Sjoerg
6728019Sjoerg#include <time.h>
6828019Sjoerg#include <ctype.h>
6928019Sjoerg#include <string.h>
7048614Sobrien#ifdef	_THREAD_SAFE
7148614Sobrien#include <pthread.h>
7248614Sobrien#include "pthread_private.h"
7348614Sobrien#endif
7428021Sjoerg#include "timelocal.h"
7528019Sjoerg
7648614Sobrienstatic char * _strptime(const char *, const char *, struct tm *);
7748614Sobrien
7848614Sobrien#ifdef	_THREAD_SAFE
7948614Sobrienstatic struct pthread_mutex	_gotgmt_mutexd = PTHREAD_MUTEX_STATIC_INITIALIZER;
8048614Sobrienstatic pthread_mutex_t		gotgmt_mutex   = &_gotgmt_mutexd;
8148614Sobrien#endif
8248614Sobrienstatic int got_GMT;
8348614Sobrien
8428021Sjoerg#define asizeof(a)	(sizeof (a) / sizeof ((a)[0]))
8528019Sjoerg
8648614Sobrienstatic char *
8748614Sobrien_strptime(const char *buf, const char *fmt, struct tm *tm)
8828019Sjoerg{
8928021Sjoerg	char	c;
9028021Sjoerg	const char *ptr;
9128021Sjoerg	int	i,
9228021Sjoerg		len;
9328019Sjoerg
9428021Sjoerg	ptr = fmt;
9528021Sjoerg	while (*ptr != 0) {
9628021Sjoerg		if (*buf == 0)
9728021Sjoerg			break;
9828019Sjoerg
9928021Sjoerg		c = *ptr++;
10028019Sjoerg
10128021Sjoerg		if (c != '%') {
10228164Sache			if (isspace((unsigned char)c))
10328164Sache				while (*buf != 0 && isspace((unsigned char)*buf))
10428021Sjoerg					buf++;
10528021Sjoerg			else if (c != *buf++)
10628021Sjoerg				return 0;
10728021Sjoerg			continue;
10828021Sjoerg		}
10928019Sjoerg
11028021Sjoerg		c = *ptr++;
11128021Sjoerg		switch (c) {
11228021Sjoerg		case 0:
11328021Sjoerg		case '%':
11428021Sjoerg			if (*buf++ != '%')
11528021Sjoerg				return 0;
11628021Sjoerg			break;
11728019Sjoerg
11828021Sjoerg		case 'C':
11948614Sobrien			buf = _strptime(buf, Locale->date_fmt, tm);
12028021Sjoerg			if (buf == 0)
12128021Sjoerg				return 0;
12228021Sjoerg			break;
12328019Sjoerg
12428021Sjoerg		case 'c':
12548614Sobrien			buf = _strptime(buf, "%x %X", tm);
12628021Sjoerg			if (buf == 0)
12728021Sjoerg				return 0;
12828021Sjoerg			break;
12928019Sjoerg
13028021Sjoerg		case 'D':
13148614Sobrien			buf = _strptime(buf, "%m/%d/%y", tm);
13228021Sjoerg			if (buf == 0)
13328021Sjoerg				return 0;
13428021Sjoerg			break;
13528019Sjoerg
13628021Sjoerg		case 'R':
13748614Sobrien			buf = _strptime(buf, "%H:%M", tm);
13828021Sjoerg			if (buf == 0)
13928021Sjoerg				return 0;
14028021Sjoerg			break;
14128019Sjoerg
14228021Sjoerg		case 'r':
14348614Sobrien			buf = _strptime(buf, "%I:%M:%S %p", tm);
14428021Sjoerg			if (buf == 0)
14528021Sjoerg				return 0;
14628021Sjoerg			break;
14728019Sjoerg
14828021Sjoerg		case 'T':
14948614Sobrien			buf = _strptime(buf, "%H:%M:%S", tm);
15028021Sjoerg			if (buf == 0)
15128021Sjoerg				return 0;
15228021Sjoerg			break;
15328019Sjoerg
15428021Sjoerg		case 'X':
15548614Sobrien			buf = _strptime(buf, Locale->X_fmt, tm);
15628021Sjoerg			if (buf == 0)
15728021Sjoerg				return 0;
15828021Sjoerg			break;
15928019Sjoerg
16028021Sjoerg		case 'x':
16148614Sobrien			buf = _strptime(buf, Locale->x_fmt, tm);
16228021Sjoerg			if (buf == 0)
16328021Sjoerg				return 0;
16428021Sjoerg			break;
16528019Sjoerg
16628021Sjoerg		case 'j':
16728164Sache			if (!isdigit((unsigned char)*buf))
16828021Sjoerg				return 0;
16928019Sjoerg
17028164Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
17128021Sjoerg				i *= 10;
17228021Sjoerg				i += *buf - '0';
17328021Sjoerg			}
17428021Sjoerg			if (i > 365)
17528021Sjoerg				return 0;
17628019Sjoerg
17728021Sjoerg			tm->tm_yday = i;
17828021Sjoerg			break;
17928019Sjoerg
18028021Sjoerg		case 'M':
18128021Sjoerg		case 'S':
18228164Sache			if (*buf == 0 || isspace((unsigned char)*buf))
18328021Sjoerg				break;
18428019Sjoerg
18528164Sache			if (!isdigit((unsigned char)*buf))
18628021Sjoerg				return 0;
18728019Sjoerg
18828164Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
18928021Sjoerg				i *= 10;
19028021Sjoerg				i += *buf - '0';
19128021Sjoerg			}
19228021Sjoerg			if (i > 59)
19328021Sjoerg				return 0;
19428019Sjoerg
19528021Sjoerg			if (c == 'M')
19628021Sjoerg				tm->tm_min = i;
19728021Sjoerg			else
19828021Sjoerg				tm->tm_sec = i;
19928019Sjoerg
20028164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
20128164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
20228021Sjoerg					ptr++;
20328021Sjoerg			break;
20428019Sjoerg
20528021Sjoerg		case 'H':
20628021Sjoerg		case 'I':
20728021Sjoerg		case 'k':
20828021Sjoerg		case 'l':
20928164Sache			if (!isdigit((unsigned char)*buf))
21028021Sjoerg				return 0;
21128019Sjoerg
21228164Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
21328021Sjoerg				i *= 10;
21428021Sjoerg				i += *buf - '0';
21528021Sjoerg			}
21628021Sjoerg			if (c == 'H' || c == 'k') {
21728021Sjoerg				if (i > 23)
21828021Sjoerg					return 0;
21928021Sjoerg			} else if (i > 11)
22028021Sjoerg				return 0;
22128019Sjoerg
22228021Sjoerg			tm->tm_hour = i;
22328019Sjoerg
22428164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
22528164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
22628021Sjoerg					ptr++;
22728021Sjoerg			break;
22828019Sjoerg
22928021Sjoerg		case 'p':
23028021Sjoerg			len = strlen(Locale->am);
23128021Sjoerg			if (strncasecmp(buf, Locale->am, len) == 0) {
23228021Sjoerg				if (tm->tm_hour > 12)
23328021Sjoerg					return 0;
23428021Sjoerg				if (tm->tm_hour == 12)
23528021Sjoerg					tm->tm_hour = 0;
23628021Sjoerg				buf += len;
23728021Sjoerg				break;
23828021Sjoerg			}
23928019Sjoerg
24028021Sjoerg			len = strlen(Locale->pm);
24128021Sjoerg			if (strncasecmp(buf, Locale->pm, len) == 0) {
24228021Sjoerg				if (tm->tm_hour > 12)
24328021Sjoerg					return 0;
24428021Sjoerg				if (tm->tm_hour != 12)
24528021Sjoerg					tm->tm_hour += 12;
24628021Sjoerg				buf += len;
24728021Sjoerg				break;
24828021Sjoerg			}
24928019Sjoerg
25028021Sjoerg			return 0;
25128019Sjoerg
25228021Sjoerg		case 'A':
25328021Sjoerg		case 'a':
25428021Sjoerg			for (i = 0; i < asizeof(Locale->weekday); i++) {
25528021Sjoerg				len = strlen(Locale->weekday[i]);
25628021Sjoerg				if (strncasecmp(buf,
25728021Sjoerg						Locale->weekday[i],
25828021Sjoerg						len) == 0)
25928021Sjoerg					break;
26028019Sjoerg
26128021Sjoerg				len = strlen(Locale->wday[i]);
26228021Sjoerg				if (strncasecmp(buf,
26328021Sjoerg						Locale->wday[i],
26428021Sjoerg						len) == 0)
26528021Sjoerg					break;
26628021Sjoerg			}
26728021Sjoerg			if (i == asizeof(Locale->weekday))
26828021Sjoerg				return 0;
26928019Sjoerg
27028021Sjoerg			tm->tm_wday = i;
27128021Sjoerg			buf += len;
27228021Sjoerg			break;
27328019Sjoerg
27428021Sjoerg		case 'd':
27528021Sjoerg		case 'e':
27628164Sache			if (!isdigit((unsigned char)*buf))
27728021Sjoerg				return 0;
27828019Sjoerg
27928164Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
28028021Sjoerg				i *= 10;
28128021Sjoerg				i += *buf - '0';
28228021Sjoerg			}
28328021Sjoerg			if (i > 31)
28428021Sjoerg				return 0;
28528019Sjoerg
28628021Sjoerg			tm->tm_mday = i;
28728019Sjoerg
28828164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
28928164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
29028021Sjoerg					ptr++;
29128021Sjoerg			break;
29228019Sjoerg
29328021Sjoerg		case 'B':
29428021Sjoerg		case 'b':
29528021Sjoerg		case 'h':
29628021Sjoerg			for (i = 0; i < asizeof(Locale->month); i++) {
29728021Sjoerg				len = strlen(Locale->month[i]);
29828021Sjoerg				if (strncasecmp(buf,
29928021Sjoerg						Locale->month[i],
30028021Sjoerg						len) == 0)
30128021Sjoerg					break;
30228019Sjoerg
30328021Sjoerg				len = strlen(Locale->mon[i]);
30428021Sjoerg				if (strncasecmp(buf,
30528021Sjoerg						Locale->mon[i],
30628021Sjoerg						len) == 0)
30728021Sjoerg					break;
30828021Sjoerg			}
30928021Sjoerg			if (i == asizeof(Locale->month))
31028021Sjoerg				return 0;
31128019Sjoerg
31228021Sjoerg			tm->tm_mon = i;
31328021Sjoerg			buf += len;
31428021Sjoerg			break;
31528019Sjoerg
31628021Sjoerg		case 'm':
31728164Sache			if (!isdigit((unsigned char)*buf))
31828021Sjoerg				return 0;
31928019Sjoerg
32028164Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
32128021Sjoerg				i *= 10;
32228021Sjoerg				i += *buf - '0';
32328021Sjoerg			}
32428021Sjoerg			if (i < 1 || i > 12)
32528021Sjoerg				return 0;
32628019Sjoerg
32728021Sjoerg			tm->tm_mon = i - 1;
32828019Sjoerg
32928164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
33028164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
33128021Sjoerg					ptr++;
33228021Sjoerg			break;
33328019Sjoerg
33428021Sjoerg		case 'Y':
33528021Sjoerg		case 'y':
33628164Sache			if (*buf == 0 || isspace((unsigned char)*buf))
33728021Sjoerg				break;
33828019Sjoerg
33928164Sache			if (!isdigit((unsigned char)*buf))
34028021Sjoerg				return 0;
34128019Sjoerg
34228164Sache			for (i = 0; *buf != 0 && isdigit((unsigned char)*buf); buf++) {
34328021Sjoerg				i *= 10;
34428021Sjoerg				i += *buf - '0';
34528021Sjoerg			}
34628021Sjoerg			if (c == 'Y')
34728021Sjoerg				i -= 1900;
34846051Swes			if (c == 'y' && i < 69)
34946042Swes				i += 100;
35028021Sjoerg			if (i < 0)
35128021Sjoerg				return 0;
35228019Sjoerg
35328021Sjoerg			tm->tm_year = i;
35428019Sjoerg
35528164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
35628164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
35728021Sjoerg					ptr++;
35828021Sjoerg			break;
35948550Sobrien
36048550Sobrien		case 'Z':
36148550Sobrien			{
36248550Sobrien			const char *cp;
36348550Sobrien			char *zonestr;
36448550Sobrien
36548550Sobrien			for (cp = buf; *cp && isupper(*cp); ++cp) {/*empty*/}
36648550Sobrien			if (cp - buf) {
36748550Sobrien				zonestr = alloca(cp - buf + 1);
36848550Sobrien				strncpy(zonestr, buf, cp - buf);
36948550Sobrien				zonestr[cp - buf] = '\0';
37048550Sobrien				tzset();
37148550Sobrien				if (0 == strcmp(zonestr, "GMT")) {
37248614Sobrien				    got_GMT = 1;
37348550Sobrien				} else if (0 == strcmp(zonestr, tzname[0])) {
37448614Sobrien				    tm->tm_isdst = 0;
37548550Sobrien				} else if (0 == strcmp(zonestr, tzname[1])) {
37648614Sobrien				    tm->tm_isdst = 1;
37748550Sobrien				} else {
37848614Sobrien				    return 0;
37948550Sobrien				}
38048550Sobrien				buf += cp - buf;
38148550Sobrien			}
38248550Sobrien			}
38348550Sobrien			break;
38428021Sjoerg		}
38528021Sjoerg	}
38648614Sobrien	return (char *)buf;
38748614Sobrien}
38828019Sjoerg
38948614Sobrien
39048614Sobrienchar *
39148614Sobrienstrptime(const char *buf, const char *fmt, struct tm *tm)
39248614Sobrien{
39348614Sobrien	char *ret;
39448614Sobrien
39548614Sobrien#ifdef	_THREAD_SAFE
39648614Sobrien	pthread_mutex_lock(&gotgmt_mutex);
39748614Sobrien#endif
39848614Sobrien
39948614Sobrien	got_GMT = 0;
40048614Sobrien	ret = _strptime(buf, fmt, tm);
40148614Sobrien	if (ret && got_GMT) {
40248550Sobrien		time_t t = timegm(tm);
40348614Sobrien	    localtime_r(&t, tm);
40448614Sobrien		got_GMT = 0;
40548550Sobrien	}
40648614Sobrien
40748614Sobrien#ifdef	_THREAD_SAFE
40848614Sobrien	pthread_mutex_unlock(&gotgmt_mutex);
40948614Sobrien#endif
41048614Sobrien
41148614Sobrien	return ret;
41228019Sjoerg}
413