1 /*
2  * Copyright (c) 1999 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of KTH nor the names of its contributors may be
18  *    used to endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32 
33 #define _GNU_SOURCE
34 #include <stddef.h>
35 #include <stdio.h>
36 #include <time.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <ctype.h>
40 #include <stdlib.h>
41 #include <errno.h>
42 #include <inttypes.h>
43 #include <limits.h>
44 #include "../locale/setlocale.h"
45 
46 #define _ctloc(x) (_CurrentTimeLocale->x)
47 
48 static const int _DAYS_BEFORE_MONTH[12] =
49 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
50 
51 #define SET_MDAY 1
52 #define SET_MON  2
53 #define SET_YEAR 4
54 #define SET_WDAY 8
55 #define SET_YDAY 16
56 #define SET_YMD  (SET_YEAR | SET_MON | SET_MDAY)
57 
58 /*
59  * tm_year is relative this year
60  */
61 const int tm_year_base = 1900;
62 
63 /*
64  * Return TRUE iff `year' was a leap year.
65  * Needed for strptime.
66  */
67 static int
is_leap_year(int year)68 is_leap_year (int year)
69 {
70     return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
71 }
72 
73 /* Needed for strptime. */
74 static int
match_string(const char * __restrict * buf,const char * const * strs,locale_t locale)75 match_string (const char *__restrict *buf, const char * const*strs,
76 	      locale_t locale)
77 {
78     int i = 0;
79 
80     for (i = 0; strs[i] != NULL; ++i) {
81 	int len = strlen (strs[i]);
82 
83 	if (strncasecmp_l (*buf, strs[i], len, locale) == 0) {
84 	    *buf += len;
85 	    return i;
86 	}
87     }
88     return -1;
89 }
90 
91 /* Needed for strptime. */
92 static int
first_day(int year)93 first_day (int year)
94 {
95     int ret = 4;
96 
97     while (--year >= 1970)
98 	ret = (ret + 365 + is_leap_year (year)) % 7;
99     return ret;
100 }
101 
102 /*
103  * Set `timeptr' given `wnum' (week number [0, 53])
104  * Needed for strptime
105  */
106 
107 static void
set_week_number_sun(struct tm * timeptr,int wnum)108 set_week_number_sun (struct tm *timeptr, int wnum)
109 {
110     int fday = first_day (timeptr->tm_year + tm_year_base);
111 
112     timeptr->tm_yday = wnum * 7 + timeptr->tm_wday - fday;
113     if (timeptr->tm_yday < 0) {
114 	timeptr->tm_wday = fday;
115 	timeptr->tm_yday = 0;
116     }
117 }
118 
119 /*
120  * Set `timeptr' given `wnum' (week number [0, 53])
121  * Needed for strptime
122  */
123 
124 static void
set_week_number_mon(struct tm * timeptr,int wnum)125 set_week_number_mon (struct tm *timeptr, int wnum)
126 {
127     int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
128 
129     timeptr->tm_yday = wnum * 7 + (timeptr->tm_wday + 6) % 7 - fday;
130     if (timeptr->tm_yday < 0) {
131 	timeptr->tm_wday = (fday + 1) % 7;
132 	timeptr->tm_yday = 0;
133     }
134 }
135 
136 /*
137  * Set `timeptr' given `wnum' (week number [0, 53])
138  * Needed for strptime
139  */
140 static void
set_week_number_mon4(struct tm * timeptr,int wnum)141 set_week_number_mon4 (struct tm *timeptr, int wnum)
142 {
143     int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
144     int offset = 0;
145 
146     if (fday < 4)
147 	offset += 7;
148 
149     timeptr->tm_yday = offset + (wnum - 1) * 7 + timeptr->tm_wday - fday;
150     if (timeptr->tm_yday < 0) {
151 	timeptr->tm_wday = fday;
152 	timeptr->tm_yday = 0;
153     }
154 }
155 
156 char *
strptime_l(const char * buf,const char * format,struct tm * timeptr,locale_t locale)157 strptime_l (const char *buf, const char *format, struct tm *timeptr,
158 	    locale_t locale)
159 {
160     char c;
161     int ymd = 0;
162 
163     const struct lc_time_T *_CurrentTimeLocale = __get_time_locale (locale);
164     for (; (c = *format) != '\0'; ++format) {
165 	char *s;
166 	int ret;
167 
168 	if (isspace_l ((unsigned char) c, locale)) {
169 	    while (isspace_l ((unsigned char) *buf, locale))
170 		++buf;
171 	} else if (c == '%' && format[1] != '\0') {
172 	    c = *++format;
173 	    if (c == 'E' || c == 'O')
174 		c = *++format;
175 	    switch (c) {
176 	    case 'A' :
177 		ret = match_string (&buf, _ctloc (weekday), locale);
178 		if (ret < 0)
179 		    return NULL;
180 		timeptr->tm_wday = ret;
181 		ymd |= SET_WDAY;
182 		break;
183 	    case 'a' :
184 		ret = match_string (&buf, _ctloc (wday), locale);
185 		if (ret < 0)
186 		    return NULL;
187 		timeptr->tm_wday = ret;
188 		ymd |= SET_WDAY;
189 		break;
190 	    case 'B' :
191 		ret = match_string (&buf, _ctloc (month), locale);
192 		if (ret < 0)
193 		    return NULL;
194 		timeptr->tm_mon = ret;
195 		ymd |= SET_MON;
196 		break;
197 	    case 'b' :
198 	    case 'h' :
199 		ret = match_string (&buf, _ctloc (mon), locale);
200 		if (ret < 0)
201 		    return NULL;
202 		timeptr->tm_mon = ret;
203 		ymd |= SET_MON;
204 		break;
205 	    case 'C' :
206 		ret = strtol_l (buf, &s, 10, locale);
207 		if (s == buf)
208 		    return NULL;
209 		timeptr->tm_year = (ret * 100) - tm_year_base;
210 		buf = s;
211 		ymd |= SET_YEAR;
212 		break;
213 	    case 'c' :		/* %a %b %e %H:%M:%S %Y */
214 		s = strptime_l (buf, _ctloc (c_fmt), timeptr, locale);
215 		if (s == NULL)
216 		    return NULL;
217 		buf = s;
218 		ymd |= SET_WDAY | SET_YMD;
219 		break;
220 	    case 'D' :		/* %m/%d/%y */
221 		s = strptime_l (buf, "%m/%d/%y", timeptr, locale);
222 		if (s == NULL)
223 		    return NULL;
224 		buf = s;
225 		ymd |= SET_YMD;
226 		break;
227 	    case 'd' :
228 	    case 'e' :
229 		ret = strtol_l (buf, &s, 10, locale);
230 		if (s == buf)
231 		    return NULL;
232 		timeptr->tm_mday = ret;
233 		buf = s;
234 		ymd |= SET_MDAY;
235 		break;
236 	    case 'F' :		/* %Y-%m-%d - GNU extension */
237 		s = strptime_l (buf, "%Y-%m-%d", timeptr, locale);
238 		if (s == NULL || s == buf)
239 		    return NULL;
240 		buf = s;
241 		ymd |= SET_YMD;
242 		break;
243 	    case 'H' :
244 	    case 'k' :		/* hour with leading space - GNU extension */
245 		ret = strtol_l (buf, &s, 10, locale);
246 		if (s == buf)
247 		    return NULL;
248 		timeptr->tm_hour = ret;
249 		buf = s;
250 		break;
251 	    case 'I' :
252 	    case 'l' :		/* hour with leading space - GNU extension */
253 		ret = strtol_l (buf, &s, 10, locale);
254 		if (s == buf)
255 		    return NULL;
256 		if (ret == 12)
257 		    timeptr->tm_hour = 0;
258 		else
259 		    timeptr->tm_hour = ret;
260 		buf = s;
261 		break;
262 	    case 'j' :
263 		ret = strtol_l (buf, &s, 10, locale);
264 		if (s == buf)
265 		    return NULL;
266 		timeptr->tm_yday = ret - 1;
267 		buf = s;
268 		ymd |= SET_YDAY;
269 		break;
270 	    case 'm' :
271 		ret = strtol_l (buf, &s, 10, locale);
272 		if (s == buf)
273 		    return NULL;
274 		timeptr->tm_mon = ret - 1;
275 		buf = s;
276 		ymd |= SET_MON;
277 		break;
278 	    case 'M' :
279 		ret = strtol_l (buf, &s, 10, locale);
280 		if (s == buf)
281 		    return NULL;
282 		timeptr->tm_min = ret;
283 		buf = s;
284 		break;
285 	    case 'n' :
286 		if (*buf == '\n')
287 		    ++buf;
288 		else
289 		    return NULL;
290 		break;
291 	    case 'p' :
292 		ret = match_string (&buf, _ctloc (am_pm), locale);
293 		if (ret < 0)
294 		    return NULL;
295 		if (timeptr->tm_hour > 12)
296 		    return NULL;
297 		else if (timeptr->tm_hour == 12)
298 		    timeptr->tm_hour = ret * 12;
299 		else
300 		    timeptr->tm_hour += ret * 12;
301 		break;
302 	    case 'q' :		/* quarter year - GNU extension */
303 		ret = strtol_l (buf, &s, 10, locale);
304 		if (s == buf)
305 		    return NULL;
306 		timeptr->tm_mon = (ret - 1)*3;
307 		buf = s;
308 		ymd |= SET_MON;
309 		break;
310 	    case 'r' :		/* %I:%M:%S %p */
311 		s = strptime_l (buf, _ctloc (ampm_fmt), timeptr, locale);
312 		if (s == NULL)
313 		    return NULL;
314 		buf = s;
315 		break;
316 	    case 'R' :		/* %H:%M */
317 		s = strptime_l (buf, "%H:%M", timeptr, locale);
318 		if (s == NULL)
319 		    return NULL;
320 		buf = s;
321 		break;
322 	    case 's' :		/* seconds since Unix epoch - GNU extension */
323 		{
324 		    long long sec;
325 		    time_t t;
326 		    int save_errno;
327 
328 		    save_errno = errno;
329 		    errno = 0;
330 		    sec = strtoll_l (buf, &s, 10, locale);
331 		    t = sec;
332 		    if (s == buf
333 			|| errno != 0
334 			|| t != sec
335 			|| localtime_r (&t, timeptr) != timeptr)
336 			return NULL;
337 		    errno = save_errno;
338 		    buf = s;
339 		    ymd |= SET_YDAY | SET_WDAY | SET_YMD;
340 		    break;
341 		}
342 	    case 'S' :
343 		ret = strtol_l (buf, &s, 10, locale);
344 		if (s == buf)
345 		    return NULL;
346 		timeptr->tm_sec = ret;
347 		buf = s;
348 		break;
349 	    case 't' :
350 		if (*buf == '\t')
351 		    ++buf;
352 		else
353 		    return NULL;
354 		break;
355 	    case 'T' :		/* %H:%M:%S */
356 		s = strptime_l (buf, "%H:%M:%S", timeptr, locale);
357 		if (s == NULL)
358 		    return NULL;
359 		buf = s;
360 		break;
361 	    case 'u' :
362 		ret = strtol_l (buf, &s, 10, locale);
363 		if (s == buf)
364 		    return NULL;
365 		timeptr->tm_wday = ret - 1;
366 		buf = s;
367 		ymd |= SET_WDAY;
368 		break;
369 	    case 'w' :
370 		ret = strtol_l (buf, &s, 10, locale);
371 		if (s == buf)
372 		    return NULL;
373 		timeptr->tm_wday = ret;
374 		buf = s;
375 		ymd |= SET_WDAY;
376 		break;
377 	    case 'U' :
378 		ret = strtol_l (buf, &s, 10, locale);
379 		if (s == buf)
380 		    return NULL;
381 		set_week_number_sun (timeptr, ret);
382 		buf = s;
383 		ymd |= SET_YDAY;
384 		break;
385 	    case 'V' :
386 		ret = strtol_l (buf, &s, 10, locale);
387 		if (s == buf)
388 		    return NULL;
389 		set_week_number_mon4 (timeptr, ret);
390 		buf = s;
391 		ymd |= SET_YDAY;
392 		break;
393 	    case 'W' :
394 		ret = strtol_l (buf, &s, 10, locale);
395 		if (s == buf)
396 		    return NULL;
397 		set_week_number_mon (timeptr, ret);
398 		buf = s;
399 		ymd |= SET_YDAY;
400 		break;
401 	    case 'x' :
402 		s = strptime_l (buf, _ctloc (x_fmt), timeptr, locale);
403 		if (s == NULL)
404 		    return NULL;
405 		buf = s;
406 		ymd |= SET_YMD;
407 		break;
408 	    case 'X' :
409 		s = strptime_l (buf, _ctloc (X_fmt), timeptr, locale);
410 		if (s == NULL)
411 		    return NULL;
412 		buf = s;
413 	    	break;
414 	    case 'y' :
415 		ret = strtol_l (buf, &s, 10, locale);
416 		if (s == buf)
417 		    return NULL;
418 		if (ret < 70)
419 		    timeptr->tm_year = 100 + ret;
420 		else
421 		    timeptr->tm_year = ret;
422 		buf = s;
423 		ymd |= SET_YEAR;
424 		break;
425 	    case 'Y' :
426 		ret = strtol_l (buf, &s, 10, locale);
427 		if (s == buf)
428 		    return NULL;
429 		timeptr->tm_year = ret - tm_year_base;
430 		buf = s;
431 		ymd |= SET_YEAR;
432 		break;
433 	    case 'Z' :
434 		/* Unsupported. Just ignore.  */
435 		break;
436 	    case '\0' :
437 		--format;
438 		__PICOLIBC_FALLTHROUGH;
439 	    case '%' :
440 		if (*buf == '%')
441 		    ++buf;
442 		else
443 		    return NULL;
444 		break;
445 	    default :
446 		if (*buf == '%' || *++buf == c)
447 		    ++buf;
448 		else
449 		    return NULL;
450 		break;
451 	    }
452 	} else {
453 	    if (*buf == c)
454 		++buf;
455 	    else
456 		return NULL;
457 	}
458     }
459 
460     if ((ymd & SET_YMD) == SET_YMD) {
461 	/* all of tm_year, tm_mon and tm_mday, but... */
462 
463 	if (!(ymd & SET_YDAY)) {
464 	    /* ...not tm_yday, so fill it in */
465 	    timeptr->tm_yday = _DAYS_BEFORE_MONTH[timeptr->tm_mon]
466 		+ timeptr->tm_mday;
467 	    if (!is_leap_year (timeptr->tm_year + tm_year_base)
468 		|| timeptr->tm_mon < 2)
469 	    {
470 		timeptr->tm_yday--;
471 	    }
472 	    ymd |= SET_YDAY;
473 	}
474     }
475     else if ((ymd & (SET_YEAR | SET_YDAY)) == (SET_YEAR | SET_YDAY)) {
476 	/* both of tm_year and tm_yday, but... */
477 
478 	if (!(ymd & SET_MON)) {
479 	    /* ...not tm_mon, so fill it in, and/or... */
480 	    if (timeptr->tm_yday < _DAYS_BEFORE_MONTH[1])
481 		timeptr->tm_mon = 0;
482 	    else {
483 		int leap = is_leap_year (timeptr->tm_year + tm_year_base);
484 		int i;
485 		for (i = 2; i < 12; ++i) {
486 		    if (timeptr->tm_yday < _DAYS_BEFORE_MONTH[i] + leap)
487 			break;
488 		}
489 		timeptr->tm_mon = i - 1;
490 	    }
491 	}
492 
493 	if (!(ymd & SET_MDAY)) {
494 	    /* ...not tm_mday, so fill it in */
495 	    timeptr->tm_mday = timeptr->tm_yday
496 		- _DAYS_BEFORE_MONTH[timeptr->tm_mon];
497 	    if (!is_leap_year (timeptr->tm_year + tm_year_base)
498 		|| timeptr->tm_mon < 2)
499 	    {
500 		timeptr->tm_mday++;
501 	    }
502 	}
503     }
504 
505     if ((ymd & (SET_YEAR | SET_YDAY | SET_WDAY)) == (SET_YEAR | SET_YDAY)) {
506 	/* fill in tm_wday */
507 	int fday = first_day (timeptr->tm_year + tm_year_base);
508 	timeptr->tm_wday = (fday + timeptr->tm_yday) % 7;
509     }
510 
511     return (char *)buf;
512 }
513 
514 char *
strptime(const char * buf,const char * format,struct tm * timeptr)515 strptime (const char *buf, const char *format, struct tm *timeptr)
516 {
517   return strptime_l (buf, format, timeptr, __get_current_locale ());
518 }
519