xref: /freebsd/contrib/unbound/compat/strptime.c (revision 98e0ffaefb0f241cda3a72395d3be04192ae0d47)
1b7579f77SDag-Erling Smørgrav /** strptime workaround (for oa macos leopard)
2b7579f77SDag-Erling Smørgrav   * This strptime follows the man strptime (2001-11-12)
3b7579f77SDag-Erling Smørgrav   *		conforming to SUSv2, POSIX.1-2001
4b7579f77SDag-Erling Smørgrav   *
5b7579f77SDag-Erling Smørgrav   * This very simple version of strptime has no:
6b7579f77SDag-Erling Smørgrav   * - E alternatives
7b7579f77SDag-Erling Smørgrav   * - O alternatives
8b7579f77SDag-Erling Smørgrav   * - Glibc additions
9b7579f77SDag-Erling Smørgrav   * - Does not process week numbers
10b7579f77SDag-Erling Smørgrav   * - Does not properly processes year day
11b7579f77SDag-Erling Smørgrav   *
12b7579f77SDag-Erling Smørgrav   * LICENSE
13b7579f77SDag-Erling Smørgrav   * Copyright (c) 2008, NLnet Labs, Matthijs Mekking
14b7579f77SDag-Erling Smørgrav   * All rights reserved.
15b7579f77SDag-Erling Smørgrav   *
16b7579f77SDag-Erling Smørgrav   * Redistribution and use in source and binary forms, with or without
17b7579f77SDag-Erling Smørgrav   * modification, are permitted provided that the following conditions are met:
18b7579f77SDag-Erling Smørgrav   * * Redistributions of source code must retain the above copyright notice,
19b7579f77SDag-Erling Smørgrav   *     this list of conditions and the following disclaimer.
20b7579f77SDag-Erling Smørgrav   * * Redistributions in binary form must reproduce the above copyright
21b7579f77SDag-Erling Smørgrav   *   notice, this list of conditions and the following disclaimer in the
22b7579f77SDag-Erling Smørgrav   *   documentation and/or other materials provided with the distribution.
23b7579f77SDag-Erling Smørgrav   * * Neither the name of NLnetLabs nor the names of its
24b7579f77SDag-Erling Smørgrav   *   contributors may be used to endorse or promote products derived from this
25b7579f77SDag-Erling Smørgrav   *   software without specific prior written permission.
26b7579f77SDag-Erling Smørgrav   *
27b7579f77SDag-Erling Smørgrav   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28b7579f77SDag-Erling Smørgrav   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29b7579f77SDag-Erling Smørgrav   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30b7579f77SDag-Erling Smørgrav   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31b7579f77SDag-Erling Smørgrav   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32b7579f77SDag-Erling Smørgrav   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33b7579f77SDag-Erling Smørgrav   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34b7579f77SDag-Erling Smørgrav   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35b7579f77SDag-Erling Smørgrav   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36b7579f77SDag-Erling Smørgrav   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37b7579f77SDag-Erling Smørgrav   * POSSIBILITY OF SUCH DAMAGE.
38b7579f77SDag-Erling Smørgrav  **/
39b7579f77SDag-Erling Smørgrav 
40b7579f77SDag-Erling Smørgrav #include "config.h"
41b7579f77SDag-Erling Smørgrav 
42b7579f77SDag-Erling Smørgrav #ifndef HAVE_CONFIG_H
43b7579f77SDag-Erling Smørgrav #include <time.h>
44b7579f77SDag-Erling Smørgrav #endif
45b7579f77SDag-Erling Smørgrav 
46b7579f77SDag-Erling Smørgrav #ifndef STRPTIME_WORKS
47b7579f77SDag-Erling Smørgrav 
48b7579f77SDag-Erling Smørgrav #define TM_YEAR_BASE 1900
49b7579f77SDag-Erling Smørgrav 
50b7579f77SDag-Erling Smørgrav #include <ctype.h>
51b7579f77SDag-Erling Smørgrav #include <string.h>
52b7579f77SDag-Erling Smørgrav 
53b7579f77SDag-Erling Smørgrav static const char *abb_weekdays[] = {
54b7579f77SDag-Erling Smørgrav 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
55b7579f77SDag-Erling Smørgrav };
56b7579f77SDag-Erling Smørgrav static const char *full_weekdays[] = {
57b7579f77SDag-Erling Smørgrav 	"Sunday", "Monday", "Tuesday", "Wednesday",
58b7579f77SDag-Erling Smørgrav 	"Thursday", "Friday", "Saturday", NULL
59b7579f77SDag-Erling Smørgrav };
60b7579f77SDag-Erling Smørgrav static const char *abb_months[] = {
61b7579f77SDag-Erling Smørgrav 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
62b7579f77SDag-Erling Smørgrav 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
63b7579f77SDag-Erling Smørgrav };
64b7579f77SDag-Erling Smørgrav static const char *full_months[] = {
65b7579f77SDag-Erling Smørgrav 	"January", "February", "March", "April", "May", "June",
66b7579f77SDag-Erling Smørgrav 	"July", "August", "September", "October", "November", "December", NULL
67b7579f77SDag-Erling Smørgrav };
68b7579f77SDag-Erling Smørgrav static const char *ampm[] = {
69b7579f77SDag-Erling Smørgrav     "am", "pm", NULL
70b7579f77SDag-Erling Smørgrav };
71b7579f77SDag-Erling Smørgrav 
72b7579f77SDag-Erling Smørgrav static int
match_string(const char ** buf,const char ** strs)73b7579f77SDag-Erling Smørgrav match_string(const char **buf, const char **strs)
74b7579f77SDag-Erling Smørgrav {
75b7579f77SDag-Erling Smørgrav 	int i = 0;
76b7579f77SDag-Erling Smørgrav 
77b7579f77SDag-Erling Smørgrav 	for (i = 0; strs[i] != NULL; i++) {
78b7579f77SDag-Erling Smørgrav 		int len = strlen(strs[i]);
79b7579f77SDag-Erling Smørgrav 		if (strncasecmp (*buf, strs[i], len) == 0) {
80b7579f77SDag-Erling Smørgrav 			*buf += len;
81b7579f77SDag-Erling Smørgrav 			return i;
82b7579f77SDag-Erling Smørgrav 		}
83b7579f77SDag-Erling Smørgrav 	}
84b7579f77SDag-Erling Smørgrav 	return -1;
85b7579f77SDag-Erling Smørgrav }
86b7579f77SDag-Erling Smørgrav 
87b7579f77SDag-Erling Smørgrav static int
str2int(const char ** buf,int max)88b7579f77SDag-Erling Smørgrav str2int(const char **buf, int max)
89b7579f77SDag-Erling Smørgrav {
90b7579f77SDag-Erling Smørgrav 	int ret=0, count=0;
91b7579f77SDag-Erling Smørgrav 
92*ff825849SDag-Erling Smørgrav 	while (*buf[0] != '\0' && isdigit((unsigned char)*buf[0]) && count<max) {
93b7579f77SDag-Erling Smørgrav 		ret = ret*10 + (*buf[0] - '0');
94b7579f77SDag-Erling Smørgrav 		(*buf)++;
95b7579f77SDag-Erling Smørgrav 		count++;
96b7579f77SDag-Erling Smørgrav 	}
97b7579f77SDag-Erling Smørgrav 
98b7579f77SDag-Erling Smørgrav 	if (!count)
99b7579f77SDag-Erling Smørgrav 		return -1;
100b7579f77SDag-Erling Smørgrav 	return ret;
101b7579f77SDag-Erling Smørgrav }
102b7579f77SDag-Erling Smørgrav 
103b7579f77SDag-Erling Smørgrav /** Converts the character string s to values which are stored in tm
104b7579f77SDag-Erling Smørgrav   * using the format specified by format
105b7579f77SDag-Erling Smørgrav  **/
106b7579f77SDag-Erling Smørgrav char *
unbound_strptime(const char * s,const char * format,struct tm * tm)107b7579f77SDag-Erling Smørgrav unbound_strptime(const char *s, const char *format, struct tm *tm)
108b7579f77SDag-Erling Smørgrav {
109b7579f77SDag-Erling Smørgrav 	int c, ret;
110b7579f77SDag-Erling Smørgrav 	int split_year = 0;
111b7579f77SDag-Erling Smørgrav 
112b7579f77SDag-Erling Smørgrav 	while ((c = *format) != '\0') {
113b7579f77SDag-Erling Smørgrav 		/* whitespace, literal or format */
114*ff825849SDag-Erling Smørgrav 		if (isspace((unsigned char)c)) { /* whitespace */
115b7579f77SDag-Erling Smørgrav 			/** whitespace matches zero or more whitespace characters in the
116b7579f77SDag-Erling Smørgrav 			  * input string.
117b7579f77SDag-Erling Smørgrav 			 **/
118*ff825849SDag-Erling Smørgrav 			while (isspace((unsigned char)*s))
119b7579f77SDag-Erling Smørgrav 				s++;
120b7579f77SDag-Erling Smørgrav 		}
121b7579f77SDag-Erling Smørgrav 		else if (c == '%') { /* format */
122b7579f77SDag-Erling Smørgrav 			format++;
123b7579f77SDag-Erling Smørgrav 			c = *format;
124b7579f77SDag-Erling Smørgrav 			switch (c) {
125b7579f77SDag-Erling Smørgrav 				case '%': /* %% is converted to % */
126b7579f77SDag-Erling Smørgrav 					if (*s != c) {
127b7579f77SDag-Erling Smørgrav 						return NULL;
128b7579f77SDag-Erling Smørgrav 					}
129b7579f77SDag-Erling Smørgrav 					s++;
130b7579f77SDag-Erling Smørgrav 					break;
131b7579f77SDag-Erling Smørgrav 				case 'a': /* weekday name, abbreviated or full */
132b7579f77SDag-Erling Smørgrav 				case 'A':
133b7579f77SDag-Erling Smørgrav 					ret = match_string(&s, full_weekdays);
134b7579f77SDag-Erling Smørgrav 					if (ret < 0)
135b7579f77SDag-Erling Smørgrav 						ret = match_string(&s, abb_weekdays);
136b7579f77SDag-Erling Smørgrav 					if (ret < 0) {
137b7579f77SDag-Erling Smørgrav 						return NULL;
138b7579f77SDag-Erling Smørgrav 					}
139b7579f77SDag-Erling Smørgrav 					tm->tm_wday = ret;
140b7579f77SDag-Erling Smørgrav 					break;
141b7579f77SDag-Erling Smørgrav 				case 'b': /* month name, abbreviated or full */
142b7579f77SDag-Erling Smørgrav 				case 'B':
143b7579f77SDag-Erling Smørgrav 				case 'h':
144b7579f77SDag-Erling Smørgrav 					ret = match_string(&s, full_months);
145b7579f77SDag-Erling Smørgrav 					if (ret < 0)
146b7579f77SDag-Erling Smørgrav 						ret = match_string(&s, abb_months);
147b7579f77SDag-Erling Smørgrav 					if (ret < 0) {
148b7579f77SDag-Erling Smørgrav 						return NULL;
149b7579f77SDag-Erling Smørgrav 					}
150b7579f77SDag-Erling Smørgrav 					tm->tm_mon = ret;
151b7579f77SDag-Erling Smørgrav 					break;
152b7579f77SDag-Erling Smørgrav 				case 'c': /* date and time representation */
153b7579f77SDag-Erling Smørgrav 					if (!(s = unbound_strptime(s, "%x %X", tm))) {
154b7579f77SDag-Erling Smørgrav 						return NULL;
155b7579f77SDag-Erling Smørgrav 					}
156b7579f77SDag-Erling Smørgrav 					break;
157b7579f77SDag-Erling Smørgrav 				case 'C': /* century number */
158b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 2);
159b7579f77SDag-Erling Smørgrav 					if (ret < 0 || ret > 99) { /* must be in [00,99] */
160b7579f77SDag-Erling Smørgrav 						return NULL;
161b7579f77SDag-Erling Smørgrav 					}
162b7579f77SDag-Erling Smørgrav 
163b7579f77SDag-Erling Smørgrav 					if (split_year)	{
164b7579f77SDag-Erling Smørgrav 						tm->tm_year = ret*100 + (tm->tm_year%100);
165b7579f77SDag-Erling Smørgrav 					}
166b7579f77SDag-Erling Smørgrav 					else {
167b7579f77SDag-Erling Smørgrav 						tm->tm_year = ret*100 - TM_YEAR_BASE;
168b7579f77SDag-Erling Smørgrav 						split_year = 1;
169b7579f77SDag-Erling Smørgrav 					}
170b7579f77SDag-Erling Smørgrav 					break;
171b7579f77SDag-Erling Smørgrav 				case 'd': /* day of month */
172b7579f77SDag-Erling Smørgrav 				case 'e':
173b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 2);
174b7579f77SDag-Erling Smørgrav 					if (ret < 1 || ret > 31) { /* must be in [01,31] */
175b7579f77SDag-Erling Smørgrav 						return NULL;
176b7579f77SDag-Erling Smørgrav 					}
177b7579f77SDag-Erling Smørgrav 					tm->tm_mday = ret;
178b7579f77SDag-Erling Smørgrav 					break;
179b7579f77SDag-Erling Smørgrav 				case 'D': /* equivalent to %m/%d/%y */
180b7579f77SDag-Erling Smørgrav 					if (!(s = unbound_strptime(s, "%m/%d/%y", tm))) {
181b7579f77SDag-Erling Smørgrav 						return NULL;
182b7579f77SDag-Erling Smørgrav 					}
183b7579f77SDag-Erling Smørgrav 					break;
184b7579f77SDag-Erling Smørgrav 				case 'H': /* hour */
185b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 2);
186b7579f77SDag-Erling Smørgrav 					if (ret < 0 || ret > 23) { /* must be in [00,23] */
187b7579f77SDag-Erling Smørgrav 						return NULL;
188b7579f77SDag-Erling Smørgrav 					}
189b7579f77SDag-Erling Smørgrav 					tm->tm_hour = ret;
190b7579f77SDag-Erling Smørgrav 					break;
191b7579f77SDag-Erling Smørgrav 				case 'I': /* 12hr clock hour */
192b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 2);
193b7579f77SDag-Erling Smørgrav 					if (ret < 1 || ret > 12) { /* must be in [01,12] */
194b7579f77SDag-Erling Smørgrav 						return NULL;
195b7579f77SDag-Erling Smørgrav 					}
196b7579f77SDag-Erling Smørgrav 					if (ret == 12) /* actually [0,11] */
197b7579f77SDag-Erling Smørgrav 						ret = 0;
198b7579f77SDag-Erling Smørgrav 					tm->tm_hour = ret;
199b7579f77SDag-Erling Smørgrav 					break;
200b7579f77SDag-Erling Smørgrav 				case 'j': /* day of year */
201b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 2);
202b7579f77SDag-Erling Smørgrav 					if (ret < 1 || ret > 366) { /* must be in [001,366] */
203b7579f77SDag-Erling Smørgrav 						return NULL;
204b7579f77SDag-Erling Smørgrav 					}
205b7579f77SDag-Erling Smørgrav 					tm->tm_yday = ret;
206b7579f77SDag-Erling Smørgrav 					break;
207b7579f77SDag-Erling Smørgrav 				case 'm': /* month */
208b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 2);
209b7579f77SDag-Erling Smørgrav 					if (ret < 1 || ret > 12) { /* must be in [01,12] */
210b7579f77SDag-Erling Smørgrav 						return NULL;
211b7579f77SDag-Erling Smørgrav 					}
212b7579f77SDag-Erling Smørgrav 					/* months go from 0-11 */
213b7579f77SDag-Erling Smørgrav 					tm->tm_mon = (ret-1);
214b7579f77SDag-Erling Smørgrav 					break;
215b7579f77SDag-Erling Smørgrav 				case 'M': /* minute */
216b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 2);
217b7579f77SDag-Erling Smørgrav 					if (ret < 0 || ret > 59) { /* must be in [00,59] */
218b7579f77SDag-Erling Smørgrav 						return NULL;
219b7579f77SDag-Erling Smørgrav 					}
220b7579f77SDag-Erling Smørgrav 					tm->tm_min = ret;
221b7579f77SDag-Erling Smørgrav 					break;
222b7579f77SDag-Erling Smørgrav 				case 'n': /* arbitrary whitespace */
223b7579f77SDag-Erling Smørgrav 				case 't':
224*ff825849SDag-Erling Smørgrav 					while (isspace((unsigned char)*s))
225b7579f77SDag-Erling Smørgrav 						s++;
226b7579f77SDag-Erling Smørgrav 					break;
227b7579f77SDag-Erling Smørgrav 				case 'p': /* am pm */
228b7579f77SDag-Erling Smørgrav 					ret = match_string(&s, ampm);
229b7579f77SDag-Erling Smørgrav 					if (ret < 0) {
230b7579f77SDag-Erling Smørgrav 						return NULL;
231b7579f77SDag-Erling Smørgrav 					}
232b7579f77SDag-Erling Smørgrav 					if (tm->tm_hour < 0 || tm->tm_hour > 11) { /* %I */
233b7579f77SDag-Erling Smørgrav 						return NULL;
234b7579f77SDag-Erling Smørgrav 					}
235b7579f77SDag-Erling Smørgrav 
236b7579f77SDag-Erling Smørgrav 					if (ret == 1) /* pm */
237b7579f77SDag-Erling Smørgrav 						tm->tm_hour += 12;
238b7579f77SDag-Erling Smørgrav 					break;
239b7579f77SDag-Erling Smørgrav 				case 'r': /* equivalent of %I:%M:%S %p */
240b7579f77SDag-Erling Smørgrav 					if (!(s = unbound_strptime(s, "%I:%M:%S %p", tm))) {
241b7579f77SDag-Erling Smørgrav 						return NULL;
242b7579f77SDag-Erling Smørgrav 					}
243b7579f77SDag-Erling Smørgrav 					break;
244b7579f77SDag-Erling Smørgrav 				case 'R': /* equivalent of %H:%M */
245b7579f77SDag-Erling Smørgrav 					if (!(s = unbound_strptime(s, "%H:%M", tm))) {
246b7579f77SDag-Erling Smørgrav 						return NULL;
247b7579f77SDag-Erling Smørgrav 					}
248b7579f77SDag-Erling Smørgrav 					break;
249b7579f77SDag-Erling Smørgrav 				case 'S': /* seconds */
250b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 2);
251b7579f77SDag-Erling Smørgrav 					/* 60 may occur for leap seconds */
252b7579f77SDag-Erling Smørgrav 					/* earlier 61 was also allowed */
253b7579f77SDag-Erling Smørgrav 					if (ret < 0 || ret > 60) { /* must be in [00,60] */
254b7579f77SDag-Erling Smørgrav 						return NULL;
255b7579f77SDag-Erling Smørgrav 					}
256b7579f77SDag-Erling Smørgrav 					tm->tm_sec = ret;
257b7579f77SDag-Erling Smørgrav 					break;
258b7579f77SDag-Erling Smørgrav 				case 'T': /* equivalent of %H:%M:%S */
259b7579f77SDag-Erling Smørgrav 					if (!(s = unbound_strptime(s, "%H:%M:%S", tm))) {
260b7579f77SDag-Erling Smørgrav 						return NULL;
261b7579f77SDag-Erling Smørgrav 					}
262b7579f77SDag-Erling Smørgrav 					break;
263b7579f77SDag-Erling Smørgrav 				case 'U': /* week number, with the first Sun of Jan being w1 */
264b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 2);
265b7579f77SDag-Erling Smørgrav 					if (ret < 0 || ret > 53) { /* must be in [00,53] */
266b7579f77SDag-Erling Smørgrav 						return NULL;
267b7579f77SDag-Erling Smørgrav 					}
268b7579f77SDag-Erling Smørgrav 					/** it is hard (and not necessary for nsd) to determine time
269b7579f77SDag-Erling Smørgrav 					  * data from week number.
270b7579f77SDag-Erling Smørgrav 					 **/
271b7579f77SDag-Erling Smørgrav 					break;
272b7579f77SDag-Erling Smørgrav 				case 'w': /* day of week */
273b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 1);
274b7579f77SDag-Erling Smørgrav 					if (ret < 0 || ret > 6) { /* must be in [0,6] */
275b7579f77SDag-Erling Smørgrav 						return NULL;
276b7579f77SDag-Erling Smørgrav 					}
277b7579f77SDag-Erling Smørgrav 					tm->tm_wday = ret;
278b7579f77SDag-Erling Smørgrav 					break;
279b7579f77SDag-Erling Smørgrav 				case 'W': /* week number, with the first Mon of Jan being w1 */
280b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 2);
281b7579f77SDag-Erling Smørgrav 					if (ret < 0 || ret > 53) { /* must be in [00,53] */
282b7579f77SDag-Erling Smørgrav 						return NULL;
283b7579f77SDag-Erling Smørgrav 					}
284b7579f77SDag-Erling Smørgrav 					/** it is hard (and not necessary for nsd) to determine time
285b7579f77SDag-Erling Smørgrav 					  * data from week number.
286b7579f77SDag-Erling Smørgrav 					 **/
287b7579f77SDag-Erling Smørgrav 					break;
288b7579f77SDag-Erling Smørgrav 				case 'x': /* date format */
289b7579f77SDag-Erling Smørgrav 					if (!(s = unbound_strptime(s, "%m/%d/%y", tm))) {
290b7579f77SDag-Erling Smørgrav 						return NULL;
291b7579f77SDag-Erling Smørgrav 					}
292b7579f77SDag-Erling Smørgrav 					break;
293b7579f77SDag-Erling Smørgrav 				case 'X': /* time format */
294b7579f77SDag-Erling Smørgrav 					if (!(s = unbound_strptime(s, "%H:%M:%S", tm))) {
295b7579f77SDag-Erling Smørgrav 						return NULL;
296b7579f77SDag-Erling Smørgrav 					}
297b7579f77SDag-Erling Smørgrav 					break;
298b7579f77SDag-Erling Smørgrav 				case 'y': /* last two digits of a year */
299b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 2);
300b7579f77SDag-Erling Smørgrav 					if (ret < 0 || ret > 99) { /* must be in [00,99] */
301b7579f77SDag-Erling Smørgrav 						return NULL;
302b7579f77SDag-Erling Smørgrav 					}
303b7579f77SDag-Erling Smørgrav 					if (split_year) {
304b7579f77SDag-Erling Smørgrav 						tm->tm_year = ((tm->tm_year/100) * 100) + ret;
305b7579f77SDag-Erling Smørgrav 					}
306b7579f77SDag-Erling Smørgrav 					else {
307b7579f77SDag-Erling Smørgrav 						split_year = 1;
308b7579f77SDag-Erling Smørgrav 
309b7579f77SDag-Erling Smørgrav 						/** currently:
310b7579f77SDag-Erling Smørgrav 						  * if in [0,68] we are in 21th century,
311b7579f77SDag-Erling Smørgrav 						  * if in [69,99] we are in 20th century.
312b7579f77SDag-Erling Smørgrav 						 **/
313b7579f77SDag-Erling Smørgrav 						if (ret < 69) /* 2000 */
314b7579f77SDag-Erling Smørgrav 							ret += 100;
315b7579f77SDag-Erling Smørgrav 						tm->tm_year = ret;
316b7579f77SDag-Erling Smørgrav 					}
317b7579f77SDag-Erling Smørgrav 					break;
318b7579f77SDag-Erling Smørgrav 				case 'Y': /* year */
319b7579f77SDag-Erling Smørgrav 					ret = str2int(&s, 4);
320b7579f77SDag-Erling Smørgrav 					if (ret < 0 || ret > 9999) {
321b7579f77SDag-Erling Smørgrav 						return NULL;
322b7579f77SDag-Erling Smørgrav 					}
323b7579f77SDag-Erling Smørgrav 					tm->tm_year = ret - TM_YEAR_BASE;
324b7579f77SDag-Erling Smørgrav 					break;
325b7579f77SDag-Erling Smørgrav 				case '\0':
326b7579f77SDag-Erling Smørgrav 				default: /* unsupported, cannot match format */
327b7579f77SDag-Erling Smørgrav 					return NULL;
328b7579f77SDag-Erling Smørgrav 					break;
329b7579f77SDag-Erling Smørgrav 			}
330b7579f77SDag-Erling Smørgrav 		}
331b7579f77SDag-Erling Smørgrav 		else { /* literal */
332b7579f77SDag-Erling Smørgrav 			/* if input cannot match format, return NULL */
333b7579f77SDag-Erling Smørgrav 			if (*s != c)
334b7579f77SDag-Erling Smørgrav 				return NULL;
335b7579f77SDag-Erling Smørgrav 			s++;
336b7579f77SDag-Erling Smørgrav 		}
337b7579f77SDag-Erling Smørgrav 
338b7579f77SDag-Erling Smørgrav 		format++;
339b7579f77SDag-Erling Smørgrav 	}
340b7579f77SDag-Erling Smørgrav 
341b7579f77SDag-Erling Smørgrav 	/* return pointer to remainder of s */
342b7579f77SDag-Erling Smørgrav 	return (char*) s;
343b7579f77SDag-Erling Smørgrav }
344b7579f77SDag-Erling Smørgrav 
345b7579f77SDag-Erling Smørgrav #endif /* STRPTIME_WORKS */
346