strptime.c revision 72168
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[] =
5650476Speter  "$FreeBSD: head/lib/libc/stdtime/strptime.c 72168 2001-02-08 17:08:13Z phantom $";
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
6771579Sdeischen#include "namespace.h"
6828019Sjoerg#include <time.h>
6928019Sjoerg#include <ctype.h>
7028019Sjoerg#include <string.h>
7148614Sobrien#include <pthread.h>
7271579Sdeischen#include "un-namespace.h"
7371579Sdeischen#include "libc_private.h"
7428021Sjoerg#include "timelocal.h"
7528019Sjoerg
7648614Sobrienstatic char * _strptime(const char *, const char *, struct tm *);
7748614Sobrien
7871579Sdeischenstatic pthread_mutex_t	gotgmt_mutex = PTHREAD_MUTEX_INITIALIZER;
7948614Sobrienstatic int got_GMT;
8048614Sobrien
8128021Sjoerg#define asizeof(a)	(sizeof (a) / sizeof ((a)[0]))
8228019Sjoerg
8348614Sobrienstatic char *
8448614Sobrien_strptime(const char *buf, const char *fmt, struct tm *tm)
8528019Sjoerg{
8628021Sjoerg	char	c;
8728021Sjoerg	const char *ptr;
8828021Sjoerg	int	i,
8928021Sjoerg		len;
9053941Sache	int Ealternative, Oalternative;
9172168Sphantom	struct lc_time_T *tptr = __get_current_time_locale();
9228019Sjoerg
9328021Sjoerg	ptr = fmt;
9428021Sjoerg	while (*ptr != 0) {
9528021Sjoerg		if (*buf == 0)
9628021Sjoerg			break;
9728019Sjoerg
9828021Sjoerg		c = *ptr++;
9928019Sjoerg
10028021Sjoerg		if (c != '%') {
10128164Sache			if (isspace((unsigned char)c))
10228164Sache				while (*buf != 0 && isspace((unsigned char)*buf))
10328021Sjoerg					buf++;
10428021Sjoerg			else if (c != *buf++)
10528021Sjoerg				return 0;
10628021Sjoerg			continue;
10728021Sjoerg		}
10828019Sjoerg
10953941Sache		Ealternative = 0;
11053941Sache		Oalternative = 0;
11153941Sachelabel:
11228021Sjoerg		c = *ptr++;
11328021Sjoerg		switch (c) {
11428021Sjoerg		case 0:
11528021Sjoerg		case '%':
11628021Sjoerg			if (*buf++ != '%')
11728021Sjoerg				return 0;
11828021Sjoerg			break;
11928019Sjoerg
12053941Sache		case '+':
12172168Sphantom			buf = _strptime(buf, tptr->date_fmt, tm);
12228021Sjoerg			if (buf == 0)
12328021Sjoerg				return 0;
12428021Sjoerg			break;
12528019Sjoerg
12653941Sache		case 'C':
12753941Sache			if (!isdigit((unsigned char)*buf))
12853941Sache				return 0;
12953941Sache
13054316Ssheldonh			/* XXX This will break for 3-digit centuries. */
13154316Ssheldonh			len = 2;
13254316Ssheldonh			for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
13353941Sache				i *= 10;
13453941Sache				i += *buf - '0';
13554316Ssheldonh				len--;
13653941Sache			}
13753941Sache			if (i < 19)
13853941Sache				return 0;
13953941Sache
14053941Sache			tm->tm_year = i * 100 - 1900;
14153941Sache			break;
14253941Sache
14328021Sjoerg		case 'c':
14467632Sache			/* NOTE: c_fmt is intentionally ignored */
14567632Sache			buf = _strptime(buf, "%a %Ef %T %Y", tm);
14628021Sjoerg			if (buf == 0)
14728021Sjoerg				return 0;
14828021Sjoerg			break;
14928019Sjoerg
15028021Sjoerg		case 'D':
15148614Sobrien			buf = _strptime(buf, "%m/%d/%y", tm);
15228021Sjoerg			if (buf == 0)
15328021Sjoerg				return 0;
15428021Sjoerg			break;
15528019Sjoerg
15653941Sache		case 'E':
15753960Sache			if (Ealternative || Oalternative)
15853960Sache				break;
15953941Sache			Ealternative++;
16053941Sache			goto label;
16153941Sache
16253941Sache		case 'O':
16353960Sache			if (Ealternative || Oalternative)
16453960Sache				break;
16553941Sache			Oalternative++;
16653941Sache			goto label;
16753941Sache
16853960Sache		case 'F':
16953960Sache		case 'f':
17053960Sache			if (!Ealternative)
17153960Sache				break;
17272168Sphantom			buf = _strptime(buf, (c == 'f') ? tptr->Ef_fmt : tptr->EF_fmt, tm);
17353960Sache			if (buf == 0)
17453960Sache				return 0;
17553960Sache			break;
17653960Sache
17728021Sjoerg		case 'R':
17848614Sobrien			buf = _strptime(buf, "%H:%M", tm);
17928021Sjoerg			if (buf == 0)
18028021Sjoerg				return 0;
18128021Sjoerg			break;
18228019Sjoerg
18328021Sjoerg		case 'r':
18448614Sobrien			buf = _strptime(buf, "%I:%M:%S %p", tm);
18528021Sjoerg			if (buf == 0)
18628021Sjoerg				return 0;
18728021Sjoerg			break;
18828019Sjoerg
18928021Sjoerg		case 'T':
19048614Sobrien			buf = _strptime(buf, "%H:%M:%S", tm);
19128021Sjoerg			if (buf == 0)
19228021Sjoerg				return 0;
19328021Sjoerg			break;
19428019Sjoerg
19528021Sjoerg		case 'X':
19672168Sphantom			buf = _strptime(buf, tptr->X_fmt, tm);
19728021Sjoerg			if (buf == 0)
19828021Sjoerg				return 0;
19928021Sjoerg			break;
20028019Sjoerg
20128021Sjoerg		case 'x':
20272168Sphantom			buf = _strptime(buf, tptr->x_fmt, tm);
20328021Sjoerg			if (buf == 0)
20428021Sjoerg				return 0;
20528021Sjoerg			break;
20628019Sjoerg
20728021Sjoerg		case 'j':
20828164Sache			if (!isdigit((unsigned char)*buf))
20928021Sjoerg				return 0;
21028019Sjoerg
21154316Ssheldonh			len = 3;
21254316Ssheldonh			for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
21328021Sjoerg				i *= 10;
21428021Sjoerg				i += *buf - '0';
21554316Ssheldonh				len--;
21628021Sjoerg			}
21753083Ssheldonh			if (i < 1 || i > 366)
21828021Sjoerg				return 0;
21928019Sjoerg
22053083Ssheldonh			tm->tm_yday = i - 1;
22128021Sjoerg			break;
22228019Sjoerg
22328021Sjoerg		case 'M':
22428021Sjoerg		case 'S':
22528164Sache			if (*buf == 0 || isspace((unsigned char)*buf))
22628021Sjoerg				break;
22728019Sjoerg
22828164Sache			if (!isdigit((unsigned char)*buf))
22928021Sjoerg				return 0;
23028019Sjoerg
23154316Ssheldonh			len = 2;
23254316Ssheldonh			for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
23328021Sjoerg				i *= 10;
23428021Sjoerg				i += *buf - '0';
23554316Ssheldonh				len--;
23628021Sjoerg			}
23728019Sjoerg
23853083Ssheldonh			if (c == 'M') {
23953083Ssheldonh				if (i > 59)
24053083Ssheldonh					return 0;
24128021Sjoerg				tm->tm_min = i;
24253083Ssheldonh			} else {
24353083Ssheldonh				if (i > 60)
24453083Ssheldonh					return 0;
24528021Sjoerg				tm->tm_sec = i;
24653083Ssheldonh			}
24728019Sjoerg
24828164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
24928164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
25028021Sjoerg					ptr++;
25128021Sjoerg			break;
25228019Sjoerg
25328021Sjoerg		case 'H':
25428021Sjoerg		case 'I':
25528021Sjoerg		case 'k':
25628021Sjoerg		case 'l':
25754316Ssheldonh			/*
25854316Ssheldonh			 * Of these, %l is the only specifier explicitly
25954316Ssheldonh			 * documented as not being zero-padded.  However,
26054316Ssheldonh			 * there is no harm in allowing zero-padding.
26154316Ssheldonh			 *
26254316Ssheldonh			 * XXX The %l specifier may gobble one too many
26354316Ssheldonh			 * digits if used incorrectly.
26454316Ssheldonh			 */
26528164Sache			if (!isdigit((unsigned char)*buf))
26628021Sjoerg				return 0;
26728019Sjoerg
26854316Ssheldonh			len = 2;
26954316Ssheldonh			for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
27028021Sjoerg				i *= 10;
27128021Sjoerg				i += *buf - '0';
27254316Ssheldonh				len--;
27328021Sjoerg			}
27428021Sjoerg			if (c == 'H' || c == 'k') {
27528021Sjoerg				if (i > 23)
27628021Sjoerg					return 0;
27754301Ssheldonh			} else if (i > 12)
27828021Sjoerg				return 0;
27928019Sjoerg
28028021Sjoerg			tm->tm_hour = i;
28128019Sjoerg
28228164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
28328164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
28428021Sjoerg					ptr++;
28528021Sjoerg			break;
28628019Sjoerg
28728021Sjoerg		case 'p':
28854316Ssheldonh			/*
28954316Ssheldonh			 * XXX This is bogus if parsed before hour-related
29054316Ssheldonh			 * specifiers.
29154316Ssheldonh			 */
29272168Sphantom			len = strlen(tptr->am);
29372168Sphantom			if (strncasecmp(buf, tptr->am, len) == 0) {
29428021Sjoerg				if (tm->tm_hour > 12)
29528021Sjoerg					return 0;
29628021Sjoerg				if (tm->tm_hour == 12)
29728021Sjoerg					tm->tm_hour = 0;
29828021Sjoerg				buf += len;
29928021Sjoerg				break;
30028021Sjoerg			}
30128019Sjoerg
30272168Sphantom			len = strlen(tptr->pm);
30372168Sphantom			if (strncasecmp(buf, tptr->pm, len) == 0) {
30428021Sjoerg				if (tm->tm_hour > 12)
30528021Sjoerg					return 0;
30628021Sjoerg				if (tm->tm_hour != 12)
30728021Sjoerg					tm->tm_hour += 12;
30828021Sjoerg				buf += len;
30928021Sjoerg				break;
31028021Sjoerg			}
31128019Sjoerg
31228021Sjoerg			return 0;
31328019Sjoerg
31428021Sjoerg		case 'A':
31528021Sjoerg		case 'a':
31672168Sphantom			for (i = 0; i < asizeof(tptr->weekday); i++) {
31753942Sache				if (c == 'A') {
31872168Sphantom					len = strlen(tptr->weekday[i]);
31953942Sache					if (strncasecmp(buf,
32072168Sphantom							tptr->weekday[i],
32153942Sache							len) == 0)
32253942Sache						break;
32353942Sache				} else {
32472168Sphantom					len = strlen(tptr->wday[i]);
32553942Sache					if (strncasecmp(buf,
32672168Sphantom							tptr->wday[i],
32753942Sache							len) == 0)
32853942Sache						break;
32953942Sache				}
33028021Sjoerg			}
33172168Sphantom			if (i == asizeof(tptr->weekday))
33228021Sjoerg				return 0;
33328019Sjoerg
33428021Sjoerg			tm->tm_wday = i;
33528021Sjoerg			buf += len;
33628021Sjoerg			break;
33728019Sjoerg
33853083Ssheldonh		case 'U':
33953083Ssheldonh		case 'W':
34053083Ssheldonh			/*
34153083Ssheldonh			 * XXX This is bogus, as we can not assume any valid
34253083Ssheldonh			 * information present in the tm structure at this
34353083Ssheldonh			 * point to calculate a real value, so just check the
34453083Ssheldonh			 * range for now.
34553083Ssheldonh			 */
34653083Ssheldonh			if (!isdigit((unsigned char)*buf))
34753083Ssheldonh				return 0;
34853083Ssheldonh
34954316Ssheldonh			len = 2;
35054316Ssheldonh			for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
35153083Ssheldonh				i *= 10;
35253083Ssheldonh				i += *buf - '0';
35354316Ssheldonh				len--;
35453083Ssheldonh			}
35553083Ssheldonh			if (i > 53)
35653083Ssheldonh				return 0;
35753083Ssheldonh
35853083Ssheldonh			if (*buf != 0 && isspace((unsigned char)*buf))
35953083Ssheldonh				while (*ptr != 0 && !isspace((unsigned char)*ptr))
36053083Ssheldonh					ptr++;
36153083Ssheldonh			break;
36253083Ssheldonh
36353083Ssheldonh		case 'w':
36453083Ssheldonh			if (!isdigit((unsigned char)*buf))
36553083Ssheldonh				return 0;
36653083Ssheldonh
36754316Ssheldonh			i = *buf - '0';
36853083Ssheldonh			if (i > 6)
36953083Ssheldonh				return 0;
37053083Ssheldonh
37153083Ssheldonh			tm->tm_wday = i;
37253083Ssheldonh
37353083Ssheldonh			if (*buf != 0 && isspace((unsigned char)*buf))
37453083Ssheldonh				while (*ptr != 0 && !isspace((unsigned char)*ptr))
37553083Ssheldonh					ptr++;
37653083Ssheldonh			break;
37753083Ssheldonh
37828021Sjoerg		case 'd':
37928021Sjoerg		case 'e':
38054316Ssheldonh			/*
38154316Ssheldonh			 * The %e specifier is explicitly documented as not
38254316Ssheldonh			 * being zero-padded but there is no harm in allowing
38354316Ssheldonh			 * such padding.
38454316Ssheldonh			 *
38554316Ssheldonh			 * XXX The %e specifier may gobble one too many
38654316Ssheldonh			 * digits if used incorrectly.
38754316Ssheldonh			 */
38828164Sache			if (!isdigit((unsigned char)*buf))
38928021Sjoerg				return 0;
39028019Sjoerg
39154316Ssheldonh			len = 2;
39254316Ssheldonh			for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
39328021Sjoerg				i *= 10;
39428021Sjoerg				i += *buf - '0';
39554316Ssheldonh				len--;
39628021Sjoerg			}
39728021Sjoerg			if (i > 31)
39828021Sjoerg				return 0;
39928019Sjoerg
40028021Sjoerg			tm->tm_mday = i;
40128019Sjoerg
40228164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
40328164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
40428021Sjoerg					ptr++;
40528021Sjoerg			break;
40628019Sjoerg
40728021Sjoerg		case 'B':
40828021Sjoerg		case 'b':
40928021Sjoerg		case 'h':
41072168Sphantom			for (i = 0; i < asizeof(tptr->month); i++) {
41153941Sache				if (Oalternative) {
41253941Sache					if (c == 'B') {
41372168Sphantom						len = strlen(tptr->alt_month[i]);
41453941Sache						if (strncasecmp(buf,
41572168Sphantom								tptr->alt_month[i],
41653941Sache								len) == 0)
41753941Sache							break;
41853941Sache					}
41953941Sache				} else {
42053941Sache					if (c == 'B') {
42172168Sphantom						len = strlen(tptr->month[i]);
42253941Sache						if (strncasecmp(buf,
42372168Sphantom								tptr->month[i],
42453941Sache								len) == 0)
42553941Sache							break;
42653941Sache					} else {
42772168Sphantom						len = strlen(tptr->mon[i]);
42853941Sache						if (strncasecmp(buf,
42972168Sphantom								tptr->mon[i],
43053941Sache								len) == 0)
43153941Sache							break;
43253941Sache					}
43353941Sache				}
43428021Sjoerg			}
43572168Sphantom			if (i == asizeof(tptr->month))
43628021Sjoerg				return 0;
43728019Sjoerg
43828021Sjoerg			tm->tm_mon = i;
43928021Sjoerg			buf += len;
44028021Sjoerg			break;
44128019Sjoerg
44228021Sjoerg		case 'm':
44328164Sache			if (!isdigit((unsigned char)*buf))
44428021Sjoerg				return 0;
44528019Sjoerg
44654316Ssheldonh			len = 2;
44754316Ssheldonh			for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
44828021Sjoerg				i *= 10;
44928021Sjoerg				i += *buf - '0';
45054316Ssheldonh				len--;
45128021Sjoerg			}
45228021Sjoerg			if (i < 1 || i > 12)
45328021Sjoerg				return 0;
45428019Sjoerg
45528021Sjoerg			tm->tm_mon = i - 1;
45628019Sjoerg
45728164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
45828164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
45928021Sjoerg					ptr++;
46028021Sjoerg			break;
46128019Sjoerg
46228021Sjoerg		case 'Y':
46328021Sjoerg		case 'y':
46428164Sache			if (*buf == 0 || isspace((unsigned char)*buf))
46528021Sjoerg				break;
46628019Sjoerg
46728164Sache			if (!isdigit((unsigned char)*buf))
46828021Sjoerg				return 0;
46928019Sjoerg
47054316Ssheldonh			len = (c == 'Y') ? 4 : 2;
47154316Ssheldonh			for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) {
47228021Sjoerg				i *= 10;
47328021Sjoerg				i += *buf - '0';
47454316Ssheldonh				len--;
47528021Sjoerg			}
47628021Sjoerg			if (c == 'Y')
47728021Sjoerg				i -= 1900;
47846051Swes			if (c == 'y' && i < 69)
47946042Swes				i += 100;
48028021Sjoerg			if (i < 0)
48128021Sjoerg				return 0;
48228019Sjoerg
48328021Sjoerg			tm->tm_year = i;
48428019Sjoerg
48528164Sache			if (*buf != 0 && isspace((unsigned char)*buf))
48628164Sache				while (*ptr != 0 && !isspace((unsigned char)*ptr))
48728021Sjoerg					ptr++;
48828021Sjoerg			break;
48948550Sobrien
49048550Sobrien		case 'Z':
49148550Sobrien			{
49248550Sobrien			const char *cp;
49348550Sobrien			char *zonestr;
49448550Sobrien
49552860Sache			for (cp = buf; *cp && isupper((unsigned char)*cp); ++cp) {/*empty*/}
49648550Sobrien			if (cp - buf) {
49748550Sobrien				zonestr = alloca(cp - buf + 1);
49848550Sobrien				strncpy(zonestr, buf, cp - buf);
49948550Sobrien				zonestr[cp - buf] = '\0';
50048550Sobrien				tzset();
50148550Sobrien				if (0 == strcmp(zonestr, "GMT")) {
50248614Sobrien				    got_GMT = 1;
50348550Sobrien				} else if (0 == strcmp(zonestr, tzname[0])) {
50448614Sobrien				    tm->tm_isdst = 0;
50548550Sobrien				} else if (0 == strcmp(zonestr, tzname[1])) {
50648614Sobrien				    tm->tm_isdst = 1;
50748550Sobrien				} else {
50848614Sobrien				    return 0;
50948550Sobrien				}
51048550Sobrien				buf += cp - buf;
51148550Sobrien			}
51248550Sobrien			}
51348550Sobrien			break;
51428021Sjoerg		}
51528021Sjoerg	}
51648614Sobrien	return (char *)buf;
51748614Sobrien}
51828019Sjoerg
51948614Sobrien
52048614Sobrienchar *
52148614Sobrienstrptime(const char *buf, const char *fmt, struct tm *tm)
52248614Sobrien{
52348614Sobrien	char *ret;
52448614Sobrien
52571579Sdeischen	if (__isthreaded)
52671579Sdeischen		_pthread_mutex_lock(&gotgmt_mutex);
52748614Sobrien
52848614Sobrien	got_GMT = 0;
52948614Sobrien	ret = _strptime(buf, fmt, tm);
53048614Sobrien	if (ret && got_GMT) {
53148550Sobrien		time_t t = timegm(tm);
53271579Sdeischen		localtime_r(&t, tm);
53348614Sobrien		got_GMT = 0;
53448550Sobrien	}
53548614Sobrien
53671579Sdeischen	if (__isthreaded)
53771579Sdeischen		_pthread_mutex_unlock(&gotgmt_mutex);
53848614Sobrien
53948614Sobrien	return ret;
54028019Sjoerg}
541