2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2014 Gary Mills
5 * Copyright 2011, Nexenta Systems, Inc. All rights reserved.
6 * Copyright (c) 1994 Powerdog Industries. All rights reserved.
8 * Copyright (c) 2011 The FreeBSD Foundation
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer
20 * in the documentation and/or other materials provided with the
23 * THIS SOFTWARE IS PROVIDED BY POWERDOG INDUSTRIES ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE POWERDOG INDUSTRIES BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * The views and conclusions contained in the software and documentation
36 * are those of the authors and should not be interpreted as representing
37 * official policies, either expressed or implied, of Powerdog Industries.
40 #include <sys/cdefs.h>
43 static char copyright[] __unused =
44 "@(#) Copyright (c) 1994 Powerdog Industries. All rights reserved.";
45 static char sccsid[] __unused = "@(#)strptime.c 0.1 (Powerdog) 94/03/27";
46 #endif /* !defined NOID */
48 __FBSDID("$FreeBSD$");
50 #include "namespace.h"
58 #include "un-namespace.h"
59 #include "libc_private.h"
60 #include "timelocal.h"
63 static char * _strptime(const char *, const char *, struct tm *, int *, locale_t);
65 #define asizeof(a) (sizeof(a) / sizeof((a)[0]))
67 #define FLAG_NONE (1 << 0)
68 #define FLAG_YEAR (1 << 1)
69 #define FLAG_MONTH (1 << 2)
70 #define FLAG_YDAY (1 << 3)
71 #define FLAG_MDAY (1 << 4)
72 #define FLAG_WDAY (1 << 5)
75 * Calculate the week day of the first day of a year. Valid for
76 * the Gregorian calendar, which began Sept 14, 1752 in the UK
77 * and its colonies. Ref:
78 * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
82 first_wday_of(int year)
84 return (((2 * (3 - (year / 100) % 4)) + (year % 100) +
85 ((year % 100) / 4) + (isleap(year) ? 6 : 0) + 1) % 7);
89 _strptime(const char *buf, const char *fmt, struct tm *tm, int *GMTp,
94 int day_offset = -1, wday_offset;
98 int Ealternative, Oalternative;
100 const struct lc_time_T *tptr = __get_current_time_locale(locale);
101 static int start_of_month[2][13] = {
102 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
103 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
115 if (isspace_l((unsigned char)c, locale))
117 isspace_l((unsigned char)*buf, locale))
119 else if (c != *buf++)
135 buf = _strptime(buf, tptr->date_fmt, tm, GMTp, locale);
138 flags |= FLAG_WDAY | FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
142 if (!isdigit_l((unsigned char)*buf, locale))
145 /* XXX This will break for 3-digit centuries. */
147 for (i = 0; len && *buf != 0 &&
148 isdigit_l((unsigned char)*buf, locale); buf++) {
160 buf = _strptime(buf, tptr->c_fmt, tm, GMTp, locale);
163 flags |= FLAG_WDAY | FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
167 buf = _strptime(buf, "%m/%d/%y", tm, GMTp, locale);
170 flags |= FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
174 if (Ealternative || Oalternative)
180 if (Ealternative || Oalternative)
186 buf = _strptime(buf, "%Y-%m-%d", tm, GMTp, locale);
189 flags |= FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
193 buf = _strptime(buf, "%H:%M", tm, GMTp, locale);
199 buf = _strptime(buf, tptr->ampm_fmt, tm, GMTp, locale);
205 buf = _strptime(buf, "%H:%M:%S", tm, GMTp, locale);
211 buf = _strptime(buf, tptr->X_fmt, tm, GMTp, locale);
217 buf = _strptime(buf, tptr->x_fmt, tm, GMTp, locale);
220 flags |= FLAG_MONTH | FLAG_MDAY | FLAG_YEAR;
224 if (!isdigit_l((unsigned char)*buf, locale))
228 for (i = 0; len && *buf != 0 &&
229 isdigit_l((unsigned char)*buf, locale); buf++){
234 if (i < 1 || i > 366)
245 isspace_l((unsigned char)*buf, locale))
248 if (!isdigit_l((unsigned char)*buf, locale))
252 for (i = 0; len && *buf != 0 &&
253 isdigit_l((unsigned char)*buf, locale); buf++){
276 * %k and %l specifiers are documented as being
277 * blank-padded. However, there is no harm in
278 * allowing zero-padding.
280 * XXX %k and %l specifiers may gobble one too many
281 * digits if used incorrectly.
285 if ((c == 'k' || c == 'l') &&
286 isblank_l((unsigned char)*buf, locale)) {
291 if (!isdigit_l((unsigned char)*buf, locale))
294 for (i = 0; len && *buf != 0 &&
295 isdigit_l((unsigned char)*buf, locale); buf++) {
300 if (c == 'H' || c == 'k') {
303 } else if (i == 0 || i > 12)
312 * XXX This is bogus if parsed before hour-related
315 if (tm->tm_hour > 12)
318 len = strlen(tptr->am);
319 if (strncasecmp_l(buf, tptr->am, len, locale) == 0) {
320 if (tm->tm_hour == 12)
326 len = strlen(tptr->pm);
327 if (strncasecmp_l(buf, tptr->pm, len, locale) == 0) {
328 if (tm->tm_hour != 12)
338 for (i = 0; i < asizeof(tptr->weekday); i++) {
339 len = strlen(tptr->weekday[i]);
340 if (strncasecmp_l(buf, tptr->weekday[i],
343 len = strlen(tptr->wday[i]);
344 if (strncasecmp_l(buf, tptr->wday[i],
348 if (i == asizeof(tptr->weekday))
359 * XXX This is bogus, as we can not assume any valid
360 * information present in the tm structure at this
361 * point to calculate a real value, so just check the
364 if (!isdigit_l((unsigned char)*buf, locale))
368 for (i = 0; len && *buf != 0 &&
369 isdigit_l((unsigned char)*buf, locale); buf++) {
378 day_offset = TM_SUNDAY;
380 day_offset = TM_MONDAY;
389 if (!isdigit_l((unsigned char)*buf, locale))
393 if (i < 0 || i > 7 || (c == 'u' && i < 1) ||
404 * With %e format, our strftime(3) adds a blank space
405 * before single digits.
408 isspace_l((unsigned char)*buf, locale))
413 * The %e specifier was once explicitly documented as
414 * not being zero-padded but was later changed to
415 * equivalent to %d. There is no harm in allowing
418 * XXX The %e specifier may gobble one too many
419 * digits if used incorrectly.
421 if (!isdigit_l((unsigned char)*buf, locale))
425 for (i = 0; len && *buf != 0 &&
426 isdigit_l((unsigned char)*buf, locale); buf++) {
431 if (i == 0 || i > 31)
442 for (i = 0; i < asizeof(tptr->month); i++) {
445 len = strlen(tptr->alt_month[i]);
446 if (strncasecmp_l(buf,
452 len = strlen(tptr->month[i]);
453 if (strncasecmp_l(buf, tptr->month[i],
459 * Try the abbreviated month name if the full name
460 * wasn't found and Oalternative was not requested.
462 if (i == asizeof(tptr->month) && !Oalternative) {
463 for (i = 0; i < asizeof(tptr->month); i++) {
464 len = strlen(tptr->mon[i]);
465 if (strncasecmp_l(buf, tptr->mon[i],
470 if (i == asizeof(tptr->month))
480 if (!isdigit_l((unsigned char)*buf, locale))
484 for (i = 0; len && *buf != 0 &&
485 isdigit_l((unsigned char)*buf, locale); buf++) {
507 n = strtol_l(buf, &cp, 10, locale);
508 if (errno == ERANGE || (long)(t = n) != n) {
514 if (gmtime_r(&t, tm) == NULL)
517 flags |= FLAG_YDAY | FLAG_WDAY | FLAG_MONTH |
518 FLAG_MDAY | FLAG_YEAR;
525 isspace_l((unsigned char)*buf, locale))
528 if (!isdigit_l((unsigned char)*buf, locale))
531 len = (c == 'Y') ? 4 : 2;
532 for (i = 0; len && *buf != 0 &&
533 isdigit_l((unsigned char)*buf, locale); buf++) {
551 for (cp = buf; *cp &&
552 isupper_l((unsigned char)*cp, locale); ++cp) {
555 zonestr = alloca(cp - buf + 1);
556 strncpy(zonestr, buf, cp - buf);
557 zonestr[cp - buf] = '\0';
559 if (0 == strcmp(zonestr, "GMT") ||
560 0 == strcmp(zonestr, "UTC")) {
562 } else if (0 == strcmp(zonestr, tzname[0])) {
564 } else if (0 == strcmp(zonestr, tzname[1])) {
587 for (len = 4; len > 0; len--) {
588 if (isdigit_l((unsigned char)*buf, locale)) {
592 } else if (len == 2) {
599 if (i > 1400 || (sign == -1 && i > 1200) ||
602 tm->tm_hour -= sign * (i / 100);
603 tm->tm_min -= sign * (i % 100);
610 while (isspace_l((unsigned char)*buf, locale))
619 if (century != -1 || year != -1) {
626 year += century * 100 - TM_YEAR_BASE;
630 if (!(flags & FLAG_YDAY) && (flags & FLAG_YEAR)) {
631 if ((flags & (FLAG_MONTH | FLAG_MDAY)) ==
632 (FLAG_MONTH | FLAG_MDAY)) {
633 tm->tm_yday = start_of_month[isleap(tm->tm_year +
634 TM_YEAR_BASE)][tm->tm_mon] + (tm->tm_mday - 1);
636 } else if (day_offset != -1) {
637 int tmpwday, tmpyday, fwo;
639 fwo = first_wday_of(tm->tm_year + TM_YEAR_BASE);
640 /* No incomplete week (week 0). */
641 if (week_offset == 0 && fwo == day_offset)
644 /* Set the date to the first Sunday (or Monday)
645 * of the specified week of the year.
647 tmpwday = (flags & FLAG_WDAY) ? tm->tm_wday :
649 tmpyday = (7 - fwo + day_offset) % 7 +
650 (week_offset - 1) * 7 +
651 (tmpwday - day_offset + 7) % 7;
652 /* Impossible yday for incomplete week (week 0). */
654 if (flags & FLAG_WDAY)
658 tm->tm_yday = tmpyday;
663 if ((flags & (FLAG_YEAR | FLAG_YDAY)) == (FLAG_YEAR | FLAG_YDAY)) {
664 if (!(flags & FLAG_MONTH)) {
666 while (tm->tm_yday >=
667 start_of_month[isleap(tm->tm_year +
673 start_of_month[isleap(tm->tm_year +
680 if (!(flags & FLAG_MDAY)) {
681 tm->tm_mday = tm->tm_yday -
682 start_of_month[isleap(tm->tm_year + TM_YEAR_BASE)]
686 if (!(flags & FLAG_WDAY)) {
688 wday_offset = first_wday_of(tm->tm_year);
689 while (i++ <= tm->tm_yday) {
690 if (wday_offset++ >= 6)
693 tm->tm_wday = wday_offset;
698 return ((char *)buf);
702 strptime_l(const char * __restrict buf, const char * __restrict fmt,
703 struct tm * __restrict tm, locale_t loc)
710 ret = _strptime(buf, fmt, tm, &gmt, loc);
712 time_t t = timegm(tm);
721 strptime(const char * __restrict buf, const char * __restrict fmt,
722 struct tm * __restrict tm)
724 return strptime_l(buf, fmt, tm, __get_locale());