1 /* $NetBSD: strptime.c,v 1.49 2015/10/09 17:21:45 christos Exp $ */
4 * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
7 * This code was contributed to The NetBSD Foundation by Klaus Klein.
8 * Heavily optimised by David Laight
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: strptime.c,v 1.49 2015/10/09 17:21:45 christos Exp $");
37 #include "namespace.h"
38 #include <sys/localedef.h>
39 #include <sys/types.h>
46 #include "setlocale_local.h"
49 __weak_alias(strptime
,_strptime
)
50 __weak_alias(strptime_l
, _strptime_l
)
53 static const u_char
*conv_num(const unsigned char *, int *, uint
, uint
);
54 static const u_char
*find_string(const u_char
*, int *, const char * const *,
55 const char * const *, int);
57 #define _TIME_LOCALE(loc) \
58 ((_TimeLocale *)((loc)->part_impl[(size_t)LC_TIME]))
61 * We do not implement alternate representations. However, we always
62 * check whether a given modifier is allowed for a certain conversion.
66 #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
68 #define S_YEAR (1 << 0)
69 #define S_MON (1 << 1)
70 #define S_YDAY (1 << 2)
71 #define S_MDAY (1 << 3)
72 #define S_WDAY (1 << 4)
73 #define S_HOUR (1 << 5)
75 #define HAVE_MDAY(s) (s & S_MDAY)
76 #define HAVE_MON(s) (s & S_MON)
77 #define HAVE_WDAY(s) (s & S_WDAY)
78 #define HAVE_YDAY(s) (s & S_YDAY)
79 #define HAVE_YEAR(s) (s & S_YEAR)
80 #define HAVE_HOUR(s) (s & S_HOUR)
82 static char gmt
[] = { "GMT" };
83 static char utc
[] = { "UTC" };
84 /* RFC-822/RFC-2822 */
85 static const char * const nast
[5] = {
86 "EST", "CST", "MST", "PST", "\0\0\0"
88 static const char * const nadt
[5] = {
89 "EDT", "CDT", "MDT", "PDT", "\0\0\0"
93 * Table to determine the ordinal date for the start of a month.
94 * Ref: http://en.wikipedia.org/wiki/ISO_week_date
96 static const int start_of_month
[2][13] = {
98 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
100 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
104 * Calculate the week day of the first day of a year. Valid for
105 * the Gregorian calendar, which began Sept 14, 1752 in the UK
106 * and its colonies. Ref:
107 * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
111 first_wday_of(int yr
)
113 return ((2 * (3 - (yr
/ 100) % 4)) + (yr
% 100) + ((yr
% 100) / 4) +
114 (isleap(yr
) ? 6 : 0) + 1) % 7;
118 strptime(const char *buf
, const char *fmt
, struct tm
*tm
)
120 return strptime_l(buf
, fmt
, tm
, _current_locale());
124 strptime_l(const char *buf
, const char *fmt
, struct tm
*tm
, locale_t loc
)
127 const unsigned char *bp
, *ep
;
128 int alt_format
, i
, split_year
= 0, neg
= 0, state
= 0,
129 day_offset
= -1, week_offset
= 0, offs
;
132 bp
= (const u_char
*)buf
;
134 while (bp
!= NULL
&& (c
= *fmt
++) != '\0') {
135 /* Clear `alternate' modifier prior to new conversion. */
139 /* Eat up white-space. */
150 again
: switch (c
= *fmt
++) {
151 case '%': /* "%%" is converted to "%". */
159 * "Alternative" modifiers. Just set the appropriate flag
160 * and start over again.
162 case 'E': /* "%E?" alternative conversion modifier. */
167 case 'O': /* "%O?" alternative conversion modifier. */
173 * "Complex" conversion rules, implemented through recursion.
175 case 'c': /* Date and time, using the locale's format. */
176 new_fmt
= _TIME_LOCALE(loc
)->d_t_fmt
;
177 state
|= S_WDAY
| S_MON
| S_MDAY
| S_YEAR
;
180 case 'D': /* The date as "%m/%d/%y". */
181 new_fmt
= "%m/%d/%y";
183 state
|= S_MON
| S_MDAY
| S_YEAR
;
186 case 'F': /* The date as "%Y-%m-%d". */
187 new_fmt
= "%Y-%m-%d";
189 state
|= S_MON
| S_MDAY
| S_YEAR
;
192 case 'R': /* The time as "%H:%M". */
197 case 'r': /* The time in 12-hour clock representation. */
198 new_fmt
= _TIME_LOCALE(loc
)->t_fmt_ampm
;
202 case 'T': /* The time as "%H:%M:%S". */
203 new_fmt
= "%H:%M:%S";
207 case 'X': /* The time, using the locale's format. */
208 new_fmt
= _TIME_LOCALE(loc
)->t_fmt
;
211 case 'x': /* The date, using the locale's format. */
212 new_fmt
= _TIME_LOCALE(loc
)->d_fmt
;
213 state
|= S_MON
| S_MDAY
| S_YEAR
;
215 bp
= (const u_char
*)strptime((const char *)bp
,
221 * "Elementary" conversion rules.
223 case 'A': /* The day of week, using the locale's form. */
225 bp
= find_string(bp
, &tm
->tm_wday
,
226 _TIME_LOCALE(loc
)->day
, _TIME_LOCALE(loc
)->abday
, 7);
231 case 'B': /* The month, using the locale's form. */
234 bp
= find_string(bp
, &tm
->tm_mon
,
235 _TIME_LOCALE(loc
)->mon
, _TIME_LOCALE(loc
)->abmon
,
241 case 'C': /* The century number. */
243 bp
= conv_num(bp
, &i
, 0, 99);
245 i
= i
* 100 - TM_YEAR_BASE
;
247 i
+= tm
->tm_year
% 100;
254 case 'd': /* The day of month. */
256 bp
= conv_num(bp
, &tm
->tm_mday
, 1, 31);
261 case 'k': /* The hour (24-hour clock representation). */
265 bp
= conv_num(bp
, &tm
->tm_hour
, 0, 23);
270 case 'l': /* The hour (12-hour clock representation). */
274 bp
= conv_num(bp
, &tm
->tm_hour
, 1, 12);
275 if (tm
->tm_hour
== 12)
281 case 'j': /* The day of year. */
283 bp
= conv_num(bp
, &i
, 1, 366);
289 case 'M': /* The minute. */
290 bp
= conv_num(bp
, &tm
->tm_min
, 0, 59);
294 case 'm': /* The month. */
296 bp
= conv_num(bp
, &i
, 1, 12);
302 case 'p': /* The locale's equivalent of AM/PM. */
303 bp
= find_string(bp
, &i
, _TIME_LOCALE(loc
)->am_pm
,
305 if (HAVE_HOUR(state
) && tm
->tm_hour
> 11)
307 tm
->tm_hour
+= i
* 12;
311 case 'S': /* The seconds. */
312 bp
= conv_num(bp
, &tm
->tm_sec
, 0, 61);
317 #define TIME_MAX INT64_MAX
319 case 's': /* seconds since the epoch */
322 uint64_t rulim
= TIME_MAX
;
324 if (*bp
< '0' || *bp
> '9') {
333 } while ((sse
* 10 <= TIME_MAX
) &&
334 rulim
&& *bp
>= '0' && *bp
<= '9');
336 if (sse
< 0 || (uint64_t)sse
> TIME_MAX
) {
341 if (localtime_r(&sse
, tm
) == NULL
)
344 state
|= S_YDAY
| S_WDAY
|
345 S_MON
| S_MDAY
| S_YEAR
;
349 case 'U': /* The week of year, beginning on sunday. */
350 case 'W': /* The week of year, beginning on monday. */
352 * XXX This is bogus, as we can not assume any valid
353 * information present in the tm structure at this
354 * point to calculate a real value, so just check the
357 bp
= conv_num(bp
, &i
, 0, 53);
360 day_offset
= TM_SUNDAY
;
362 day_offset
= TM_MONDAY
;
366 case 'w': /* The day of week, beginning on sunday. */
367 bp
= conv_num(bp
, &tm
->tm_wday
, 0, 6);
372 case 'u': /* The day of week, monday = 1. */
373 bp
= conv_num(bp
, &i
, 1, 7);
379 case 'g': /* The year corresponding to the ISO week
380 * number but without the century.
382 bp
= conv_num(bp
, &i
, 0, 99);
385 case 'G': /* The year corresponding to the ISO week
386 * number with century.
390 while (isdigit(*bp
));
393 case 'V': /* The ISO 8601:1988 week number as decimal */
394 bp
= conv_num(bp
, &i
, 0, 53);
397 case 'Y': /* The year. */
398 i
= TM_YEAR_BASE
; /* just for data sanity... */
399 bp
= conv_num(bp
, &i
, 0, 9999);
400 tm
->tm_year
= i
- TM_YEAR_BASE
;
405 case 'y': /* The year within 100 years of the epoch. */
406 /* LEGAL_ALT(ALT_E | ALT_O); */
407 bp
= conv_num(bp
, &i
, 0, 99);
410 /* preserve century */
411 i
+= (tm
->tm_year
/ 100) * 100;
415 i
= i
+ 2000 - TM_YEAR_BASE
;
417 i
= i
+ 1900 - TM_YEAR_BASE
;
425 if (strncmp((const char *)bp
, gmt
, 3) == 0 ||
426 strncmp((const char *)bp
, utc
, 3) == 0) {
436 ep
= find_string(bp
, &i
,
437 (const char * const *)tzname
,
442 tm
->TM_GMTOFF
= -(timezone
);
445 tm
->TM_ZONE
= tzname
[i
];
454 * We recognize all ISO 8601 formats:
459 * We recognize all RFC-822/RFC-2822 formats:
461 * North American : UTC offsets
462 * E[DS]T = Eastern : -4 | -5
463 * C[DS]T = Central : -5 | -6
464 * M[DS]T = Mountain: -6 | -7
465 * P[DS]T = Pacific : -7 | -8
467 * [A-IL-M] = -1 ... -9 (J not used)
499 ep
= find_string(bp
, &i
, nast
, NULL
, 4);
502 tm
->TM_GMTOFF
= -5 - i
;
505 tm
->TM_ZONE
= __UNCONST(nast
[i
]);
510 ep
= find_string(bp
, &i
, nadt
, NULL
, 4);
514 tm
->TM_GMTOFF
= -4 - i
;
517 tm
->TM_ZONE
= __UNCONST(nadt
[i
]);
523 if ((*bp
>= 'A' && *bp
<= 'I') ||
524 (*bp
>= 'L' && *bp
<= 'Y')) {
527 if (*bp
>= 'A' && *bp
<= 'I')
529 ('A' - 1) - (int)*bp
;
530 else if (*bp
>= 'L' && *bp
<= 'M')
531 tm
->TM_GMTOFF
= 'A' - (int)*bp
;
532 else if (*bp
>= 'N' && *bp
<= 'Y')
533 tm
->TM_GMTOFF
= (int)*bp
- 'M';
536 tm
->TM_ZONE
= utc
; /* XXX */
544 for (i
= 0; i
< 4; ) {
546 offs
= offs
* 10 + (*bp
++ - '0');
550 if (i
== 2 && *bp
== ':') {
564 /* Convert minutes into decimal */
565 offs
= (offs
/ 100) * 100 + (i
* 50) / 30;
572 tm
->tm_isdst
= 0; /* XXX */
574 tm
->TM_GMTOFF
= offs
;
577 tm
->TM_ZONE
= utc
; /* XXX */
582 * Miscellaneous conversions.
584 case 'n': /* Any kind of white-space. */
592 default: /* Unknown/unsupported conversion. */
597 if (!HAVE_YDAY(state
) && HAVE_YEAR(state
)) {
598 if (HAVE_MON(state
) && HAVE_MDAY(state
)) {
599 /* calculate day of year (ordinal date) */
600 tm
->tm_yday
= start_of_month
[isleap_sum(tm
->tm_year
,
601 TM_YEAR_BASE
)][tm
->tm_mon
] + (tm
->tm_mday
- 1);
603 } else if (day_offset
!= -1) {
605 * Set the date to the first Sunday (or Monday)
606 * of the specified week of the year.
608 if (!HAVE_WDAY(state
)) {
609 tm
->tm_wday
= day_offset
;
613 first_wday_of(tm
->tm_year
+ TM_YEAR_BASE
) +
614 day_offset
) % 7 + (week_offset
- 1) * 7 +
615 tm
->tm_wday
- day_offset
;
620 if (HAVE_YDAY(state
) && HAVE_YEAR(state
)) {
623 if (!HAVE_MON(state
)) {
624 /* calculate month of day of year */
626 isleap
= isleap_sum(tm
->tm_year
, TM_YEAR_BASE
);
627 while (tm
->tm_yday
>= start_of_month
[isleap
][i
])
631 tm
->tm_yday
-= start_of_month
[isleap
][12];
638 if (!HAVE_MDAY(state
)) {
639 /* calculate day of month */
640 isleap
= isleap_sum(tm
->tm_year
, TM_YEAR_BASE
);
641 tm
->tm_mday
= tm
->tm_yday
-
642 start_of_month
[isleap
][tm
->tm_mon
] + 1;
646 if (!HAVE_WDAY(state
)) {
647 /* calculate day of week */
649 week_offset
= first_wday_of(tm
->tm_year
);
650 while (i
++ <= tm
->tm_yday
) {
651 if (week_offset
++ >= 6)
654 tm
->tm_wday
= week_offset
;
659 return __UNCONST(bp
);
663 static const u_char
*
664 conv_num(const unsigned char *buf
, int *dest
, uint llim
, uint ulim
)
669 /* The limit also determines the number of valid digits. */
673 if (ch
< '0' || ch
> '9')
681 } while ((result
* 10 <= ulim
) && rulim
&& ch
>= '0' && ch
<= '9');
683 if (result
< llim
|| result
> ulim
)
690 static const u_char
*
691 find_string(const u_char
*bp
, int *tgt
, const char * const *n1
,
692 const char * const *n2
, int c
)
697 /* check full name - then abbreviated ones */
698 for (; n1
!= NULL
; n1
= n2
, n2
= NULL
) {
699 for (i
= 0; i
< c
; i
++, n1
++) {
701 if (strncasecmp(*n1
, (const char *)bp
, len
) == 0) {
708 /* Nothing matched */