2 * Copyright (c) 1999, 2003, 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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. */
37 #include "strpftime-test.h"
42 RCSID("$Id: strptime.c 21895 2007-08-09 08:45:54Z lha $");
44 static const char *abb_weekdays[] = {
55 static const char *full_weekdays[] = {
66 static const char *abb_month[] = {
82 static const char *full_month[] = {
98 static const char *ampm[] = {
105 * Try to match `*buf' to one of the strings in `strs'. Return the
106 * index of the matching string (or -1 if none). Also advance buf.
110 match_string (const char **buf, const char **strs)
114 for (i = 0; strs[i] != NULL; ++i) {
115 int len = strlen (strs[i]);
117 if (strncasecmp (*buf, strs[i], len) == 0) {
126 * Try to match `*buf' to at the most `n' characters and return the
127 * resulting number in `num'. Returns 0 or an error. Also advance
132 parse_number (const char **buf, int n, int *num)
141 /* skip whitespace */
142 for (; **buf != '\0' && isspace((unsigned char)(**buf)); (*buf)++)
145 /* parse at least n characters */
146 for (i = 0; **buf != '\0' && i < n && isdigit((unsigned char)(**buf)); i++, (*buf)++)
150 *num = strtol (str, &s, 10);
159 * tm_year is relative this year
162 const int tm_year_base = 1900;
165 * Return TRUE iff `year' was a leap year.
169 is_leap_year (int year)
171 return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
175 * Return the weekday [0,6] (0 = Sunday) of the first day of `year'
183 for (; year > 1970; --year)
184 ret = (ret + 365 + is_leap_year (year) ? 1 : 0) % 7;
189 * Set `timeptr' given `wnum' (week number [0, 53])
193 set_week_number_sun (struct tm *timeptr, int wnum)
195 int fday = first_day (timeptr->tm_year + tm_year_base);
197 timeptr->tm_yday = wnum * 7 + timeptr->tm_wday - fday;
198 if (timeptr->tm_yday < 0) {
199 timeptr->tm_wday = fday;
200 timeptr->tm_yday = 0;
205 * Set `timeptr' given `wnum' (week number [0, 53])
209 set_week_number_mon (struct tm *timeptr, int wnum)
211 int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
213 timeptr->tm_yday = wnum * 7 + (timeptr->tm_wday + 6) % 7 - fday;
214 if (timeptr->tm_yday < 0) {
215 timeptr->tm_wday = (fday + 1) % 7;
216 timeptr->tm_yday = 0;
221 * Set `timeptr' given `wnum' (week number [0, 53])
225 set_week_number_mon4 (struct tm *timeptr, int wnum)
227 int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
233 timeptr->tm_yday = offset + (wnum - 1) * 7 + timeptr->tm_wday - fday;
234 if (timeptr->tm_yday < 0) {
235 timeptr->tm_wday = fday;
236 timeptr->tm_yday = 0;
244 char * ROKEN_LIB_FUNCTION
245 strptime (const char *buf, const char *format, struct tm *timeptr)
249 for (; (c = *format) != '\0'; ++format) {
253 if (isspace ((unsigned char)c)) {
254 while (isspace ((unsigned char)*buf))
256 } else if (c == '%' && format[1] != '\0') {
258 if (c == 'E' || c == 'O')
262 ret = match_string (&buf, full_weekdays);
265 timeptr->tm_wday = ret;
268 ret = match_string (&buf, abb_weekdays);
271 timeptr->tm_wday = ret;
274 ret = match_string (&buf, full_month);
277 timeptr->tm_mon = ret;
281 ret = match_string (&buf, abb_month);
284 timeptr->tm_mon = ret;
287 if (parse_number(&buf, 2, &ret))
289 timeptr->tm_year = (ret * 100) - tm_year_base;
293 case 'D' : /* %m/%d/%y */
294 s = strptime (buf, "%m/%d/%y", timeptr);
301 if (parse_number(&buf, 2, &ret))
303 timeptr->tm_mday = ret;
307 if (parse_number(&buf, 2, &ret))
309 timeptr->tm_hour = ret;
313 if (parse_number(&buf, 2, &ret))
316 timeptr->tm_hour = 0;
318 timeptr->tm_hour = ret;
321 if (parse_number(&buf, 3, &ret))
325 timeptr->tm_yday = ret - 1;
328 if (parse_number(&buf, 2, &ret))
332 timeptr->tm_mon = ret - 1;
335 if (parse_number(&buf, 2, &ret))
337 timeptr->tm_min = ret;
340 while (isspace ((unsigned char)*buf))
344 ret = match_string (&buf, ampm);
347 if (timeptr->tm_hour == 0) {
349 timeptr->tm_hour = 12;
351 timeptr->tm_hour += 12;
353 case 'r' : /* %I:%M:%S %p */
354 s = strptime (buf, "%I:%M:%S %p", timeptr);
359 case 'R' : /* %H:%M */
360 s = strptime (buf, "%H:%M", timeptr);
366 if (parse_number(&buf, 2, &ret))
368 timeptr->tm_sec = ret;
371 while (isspace ((unsigned char)*buf))
374 case 'T' : /* %H:%M:%S */
376 s = strptime (buf, "%H:%M:%S", timeptr);
382 if (parse_number(&buf, 1, &ret))
386 timeptr->tm_wday = ret - 1;
389 if (parse_number(&buf, 1, &ret))
391 timeptr->tm_wday = ret;
394 if (parse_number(&buf, 2, &ret))
396 set_week_number_sun (timeptr, ret);
399 if (parse_number(&buf, 2, &ret))
401 set_week_number_mon4 (timeptr, ret);
404 if (parse_number(&buf, 2, &ret))
406 set_week_number_mon (timeptr, ret);
409 s = strptime (buf, "%Y:%m:%d", timeptr);
415 if (parse_number(&buf, 2, &ret))
418 timeptr->tm_year = 100 + ret;
420 timeptr->tm_year = ret;
423 if (parse_number(&buf, 4, &ret))
425 timeptr->tm_year = ret - tm_year_base;
439 if (*buf == '%' || *++buf == c)
452 return rk_UNCONST(buf);