]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Time/strptime.c
2 strptime implementation
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
14 * All rights reserved.
16 * This code was contributed to The NetBSD Foundation by Klaus Klein.
17 * Heavily optimised by David Laight
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
40 $NetBSD: strptime.c,v 1.28 2008/04/28 20:23:01 martin Exp $
44 #if defined(_MSC_VER) /* Handle Microsoft VC++ compiler specifics. */
45 #pragma warning ( disable : 4244 )
46 #pragma warning ( disable : 4018 )
49 #include <LibConfig.h>
51 #include <sys/cdefs.h>
53 #include "namespace.h"
59 #include <sys/localedef.h>
66 __weak_alias(strptime
,_strptime
)
69 #define _ctloc(x) (_CurrentTimeLocale->x)
72 * We do not implement alternate representations. However, we always
73 * check whether a given modifier is allowed for a certain conversion.
77 #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
79 static const unsigned char *conv_num(const unsigned char *, int *, unsigned int, unsigned int);
80 static const unsigned char *find_string(const unsigned char *, int *, const char * const *,
81 const char * const *, int);
85 strptime(const char *buf
, const char *fmt
, struct tm
*tm
)
88 const unsigned char *bp
;
89 int alt_format
, i
, split_year
= 0;
92 bp
= (const unsigned char *)buf
;
94 while (bp
!= NULL
&& (c
= *fmt
++) != '\0') {
95 /* Clear `alternate' modifier prior to new conversion. */
99 /* Eat up white-space. */
110 again
: switch (c
= *fmt
++) {
111 case '%': /* "%%" is converted to "%". */
119 * "Alternative" modifiers. Just set the appropriate flag
120 * and start over again.
122 case 'E': /* "%E?" alternative conversion modifier. */
127 case 'O': /* "%O?" alternative conversion modifier. */
133 * "Complex" conversion rules, implemented through recursion.
135 case 'c': /* Date and time, using the locale's format. */
136 new_fmt
= _ctloc(d_t_fmt
);
139 case 'D': /* The date as "%m/%d/%y". */
140 new_fmt
= "%m/%d/%y";
144 case 'F': /* The date as "%Y-%m-%d". */
145 new_fmt
= "%Y-%m-%d";
149 case 'R': /* The time as "%H:%M". */
154 case 'r': /* The time in 12-hour clock representation. */
155 new_fmt
=_ctloc(t_fmt_ampm
);
159 case 'T': /* The time as "%H:%M:%S". */
160 new_fmt
= "%H:%M:%S";
164 case 'X': /* The time, using the locale's format. */
165 new_fmt
=_ctloc(t_fmt
);
168 case 'x': /* The date, using the locale's format. */
169 new_fmt
=_ctloc(d_fmt
);
171 bp
= (const unsigned char *)strptime((const char *)bp
,
177 * "Elementary" conversion rules.
179 case 'A': /* The day of week, using the locale's form. */
181 bp
= find_string(bp
, &tm
->tm_wday
, _ctloc(day
),
186 case 'B': /* The month, using the locale's form. */
189 bp
= find_string(bp
, &tm
->tm_mon
, _ctloc(mon
),
194 case 'C': /* The century number. */
196 bp
= conv_num(bp
, &i
, 0, 99);
198 i
= i
* 100 - TM_YEAR_BASE
;
200 i
+= tm
->tm_year
% 100;
206 case 'd': /* The day of month. */
208 bp
= conv_num(bp
, &tm
->tm_mday
, 1, 31);
212 case 'k': /* The hour (24-hour clock representation). */
216 bp
= conv_num(bp
, &tm
->tm_hour
, 0, 23);
220 case 'l': /* The hour (12-hour clock representation). */
224 bp
= conv_num(bp
, &tm
->tm_hour
, 1, 12);
225 if (tm
->tm_hour
== 12)
230 case 'j': /* The day of year. */
232 bp
= conv_num(bp
, &i
, 1, 366);
237 case 'M': /* The minute. */
238 bp
= conv_num(bp
, &tm
->tm_min
, 0, 59);
242 case 'm': /* The month. */
244 bp
= conv_num(bp
, &i
, 1, 12);
249 case 'p': /* The locale's equivalent of AM/PM. */
250 bp
= find_string(bp
, &i
, _ctloc(am_pm
), NULL
, 2);
251 if (tm
->tm_hour
> 11)
253 tm
->tm_hour
+= i
* 12;
257 case 'S': /* The seconds. */
258 bp
= conv_num(bp
, &tm
->tm_sec
, 0, 61);
262 case 'U': /* The week of year, beginning on sunday. */
263 case 'W': /* The week of year, beginning on monday. */
265 * XXX This is bogus, as we can not assume any valid
266 * information present in the tm structure at this
267 * point to calculate a real value, so just check the
270 bp
= conv_num(bp
, &i
, 0, 53);
274 case 'w': /* The day of week, beginning on sunday. */
275 bp
= conv_num(bp
, &tm
->tm_wday
, 0, 6);
279 case 'Y': /* The year. */
280 i
= TM_YEAR_BASE
; /* just for data sanity... */
281 bp
= conv_num(bp
, &i
, 0, 9999);
282 tm
->tm_year
= i
- TM_YEAR_BASE
;
286 case 'y': /* The year within 100 years of the epoch. */
287 /* LEGAL_ALT(ALT_E | ALT_O); */
288 bp
= conv_num(bp
, &i
, 0, 99);
291 /* preserve century */
292 i
+= (tm
->tm_year
/ 100) * 100;
296 i
= i
+ 2000 - TM_YEAR_BASE
;
298 i
= i
+ 1900 - TM_YEAR_BASE
;
305 if (strncmp((const char *)bp
, gmt
, 3) == 0) {
315 const unsigned char *ep
;
317 ep
= find_string(bp
, &i
,
318 (const char * const *)tzname
,
323 tm
->TM_GMTOFF
= -(timezone
);
326 tm
->TM_ZONE
= tzname
[i
];
334 * Miscellaneous conversions.
336 case 'n': /* Any kind of white-space. */
344 default: /* Unknown/unsupported conversion. */
349 return __UNCONST(bp
);
353 static const unsigned char *
354 conv_num(const unsigned char *buf
, int *dest
, unsigned int llim
, unsigned int ulim
)
356 unsigned int result
= 0;
359 /* The limit also determines the number of valid digits. */
360 unsigned int rulim
= ulim
;
363 if (ch
< '0' || ch
> '9')
371 } while ((result
* 10 <= ulim
) && rulim
&& ch
>= '0' && ch
<= '9');
373 if (result
< llim
|| result
> ulim
)
380 static const unsigned char *
381 find_string(const unsigned char *bp
, int *tgt
, const char * const *n1
,
382 const char * const *n2
, int c
)
387 /* check full name - then abbreviated ones */
388 for (; n1
!= NULL
; n1
= n2
, n2
= NULL
) {
389 for (i
= 0; i
< c
; i
++, n1
++) {
391 if (strncasecmp(*n1
, (const char *)bp
, len
) == 0) {
398 /* Nothing matched */