1 /* $NetBSD: strptime.c,v 1.1.1.1 2011/04/13 18:15:43 elric Exp $ */
4 * Copyright (c) 1999, 2003, 2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * 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 * 3. Neither the name of KTH nor the names of its contributors may be
20 * used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``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 KTH OR ITS CONTRIBUTORS 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 OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 #include <krb5/roken.h>
38 #include "strpftime-test.h"
42 static const char *abb_weekdays
[] = {
53 static const char *full_weekdays
[] = {
64 static const char *abb_month
[] = {
80 static const char *full_month
[] = {
96 static const char *ampm
[] = {
103 * Try to match `*buf' to one of the strings in `strs'. Return the
104 * index of the matching string (or -1 if none). Also advance buf.
108 match_string (const char **buf
, const char **strs
)
112 for (i
= 0; strs
[i
] != NULL
; ++i
) {
113 int len
= strlen (strs
[i
]);
115 if (strncasecmp (*buf
, strs
[i
], len
) == 0) {
124 * Try to match `*buf' to at the most `n' characters and return the
125 * resulting number in `num'. Returns 0 or an error. Also advance
130 parse_number (const char **buf
, int n
, int *num
)
139 /* skip whitespace */
140 for (; **buf
!= '\0' && isspace((unsigned char)(**buf
)); (*buf
)++)
143 /* parse at least n characters */
144 for (i
= 0; **buf
!= '\0' && i
< n
&& isdigit((unsigned char)(**buf
)); i
++, (*buf
)++)
148 *num
= strtol (str
, &s
, 10);
157 * tm_year is relative this year
160 const int tm_year_base
= 1900;
163 * Return TRUE iff `year' was a leap year.
167 is_leap_year (int year
)
169 return (year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0);
173 * Return the weekday [0,6] (0 = Sunday) of the first day of `year'
181 for (; year
> 1970; --year
)
182 ret
= (ret
+ (is_leap_year (year
) ? 366 : 365)) % 7;
187 * Set `timeptr' given `wnum' (week number [0, 53])
191 set_week_number_sun (struct tm
*timeptr
, int wnum
)
193 int fday
= first_day (timeptr
->tm_year
+ tm_year_base
);
195 timeptr
->tm_yday
= wnum
* 7 + timeptr
->tm_wday
- fday
;
196 if (timeptr
->tm_yday
< 0) {
197 timeptr
->tm_wday
= fday
;
198 timeptr
->tm_yday
= 0;
203 * Set `timeptr' given `wnum' (week number [0, 53])
207 set_week_number_mon (struct tm
*timeptr
, int wnum
)
209 int fday
= (first_day (timeptr
->tm_year
+ tm_year_base
) + 6) % 7;
211 timeptr
->tm_yday
= wnum
* 7 + (timeptr
->tm_wday
+ 6) % 7 - fday
;
212 if (timeptr
->tm_yday
< 0) {
213 timeptr
->tm_wday
= (fday
+ 1) % 7;
214 timeptr
->tm_yday
= 0;
219 * Set `timeptr' given `wnum' (week number [0, 53])
223 set_week_number_mon4 (struct tm
*timeptr
, int wnum
)
225 int fday
= (first_day (timeptr
->tm_year
+ tm_year_base
) + 6) % 7;
231 timeptr
->tm_yday
= offset
+ (wnum
- 1) * 7 + timeptr
->tm_wday
- fday
;
232 if (timeptr
->tm_yday
< 0) {
233 timeptr
->tm_wday
= fday
;
234 timeptr
->tm_yday
= 0;
242 ROKEN_LIB_FUNCTION
char * ROKEN_LIB_CALL
243 strptime (const char *buf
, const char *format
, struct tm
*timeptr
)
247 for (; (c
= *format
) != '\0'; ++format
) {
251 if (isspace ((unsigned char)c
)) {
252 while (isspace ((unsigned char)*buf
))
254 } else if (c
== '%' && format
[1] != '\0') {
256 if (c
== 'E' || c
== 'O')
260 ret
= match_string (&buf
, full_weekdays
);
263 timeptr
->tm_wday
= ret
;
266 ret
= match_string (&buf
, abb_weekdays
);
269 timeptr
->tm_wday
= ret
;
272 ret
= match_string (&buf
, full_month
);
275 timeptr
->tm_mon
= ret
;
279 ret
= match_string (&buf
, abb_month
);
282 timeptr
->tm_mon
= ret
;
285 if (parse_number(&buf
, 2, &ret
))
287 timeptr
->tm_year
= (ret
* 100) - tm_year_base
;
291 case 'D' : /* %m/%d/%y */
292 s
= strptime (buf
, "%m/%d/%y", timeptr
);
299 if (parse_number(&buf
, 2, &ret
))
301 timeptr
->tm_mday
= ret
;
305 if (parse_number(&buf
, 2, &ret
))
307 timeptr
->tm_hour
= ret
;
311 if (parse_number(&buf
, 2, &ret
))
314 timeptr
->tm_hour
= 0;
316 timeptr
->tm_hour
= ret
;
319 if (parse_number(&buf
, 3, &ret
))
323 timeptr
->tm_yday
= ret
- 1;
326 if (parse_number(&buf
, 2, &ret
))
330 timeptr
->tm_mon
= ret
- 1;
333 if (parse_number(&buf
, 2, &ret
))
335 timeptr
->tm_min
= ret
;
338 while (isspace ((unsigned char)*buf
))
342 ret
= match_string (&buf
, ampm
);
345 if (timeptr
->tm_hour
== 0) {
347 timeptr
->tm_hour
= 12;
349 timeptr
->tm_hour
+= 12;
351 case 'r' : /* %I:%M:%S %p */
352 s
= strptime (buf
, "%I:%M:%S %p", timeptr
);
357 case 'R' : /* %H:%M */
358 s
= strptime (buf
, "%H:%M", timeptr
);
364 if (parse_number(&buf
, 2, &ret
))
366 timeptr
->tm_sec
= ret
;
369 while (isspace ((unsigned char)*buf
))
372 case 'T' : /* %H:%M:%S */
374 s
= strptime (buf
, "%H:%M:%S", timeptr
);
380 if (parse_number(&buf
, 1, &ret
))
384 timeptr
->tm_wday
= ret
- 1;
387 if (parse_number(&buf
, 1, &ret
))
389 timeptr
->tm_wday
= ret
;
392 if (parse_number(&buf
, 2, &ret
))
394 set_week_number_sun (timeptr
, ret
);
397 if (parse_number(&buf
, 2, &ret
))
399 set_week_number_mon4 (timeptr
, ret
);
402 if (parse_number(&buf
, 2, &ret
))
404 set_week_number_mon (timeptr
, ret
);
407 s
= strptime (buf
, "%Y:%m:%d", timeptr
);
413 if (parse_number(&buf
, 2, &ret
))
416 timeptr
->tm_year
= 100 + ret
;
418 timeptr
->tm_year
= ret
;
421 if (parse_number(&buf
, 4, &ret
))
423 timeptr
->tm_year
= ret
- tm_year_base
;
437 if (*buf
== '%' || *++buf
== c
)
450 return rk_UNCONST(buf
);