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. */
36 #include "strpftime-test.h"
40 static const char *abb_weekdays
[] = {
51 static const char *full_weekdays
[] = {
62 static const char *abb_month
[] = {
78 static const char *full_month
[] = {
94 static const char *ampm
[] = {
101 * Try to match `*buf' to one of the strings in `strs'. Return the
102 * index of the matching string (or -1 if none). Also advance buf.
106 match_string (const char **buf
, const char **strs
)
110 for (i
= 0; strs
[i
] != NULL
; ++i
) {
111 int len
= strlen (strs
[i
]);
113 if (strncasecmp (*buf
, strs
[i
], len
) == 0) {
122 * Try to match `*buf' to at the most `n' characters and return the
123 * resulting number in `num'. Returns 0 or an error. Also advance
128 parse_number (const char **buf
, int n
, int *num
)
137 /* skip whitespace */
138 for (; **buf
!= '\0' && isspace((unsigned char)(**buf
)); (*buf
)++)
141 /* parse at least n characters */
142 for (i
= 0; **buf
!= '\0' && i
< n
&& isdigit((unsigned char)(**buf
)); i
++, (*buf
)++)
146 *num
= strtol (str
, &s
, 10);
155 * tm_year is relative this year
158 const int tm_year_base
= 1900;
161 * Return TRUE iff `year' was a leap year.
165 is_leap_year (int year
)
167 return (year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0);
171 * Return the weekday [0,6] (0 = Sunday) of the first day of `year'
179 for (; year
> 1970; --year
)
180 ret
= (ret
+ (is_leap_year (year
) ? 366 : 365)) % 7;
185 * Set `timeptr' given `wnum' (week number [0, 53])
189 set_week_number_sun (struct tm
*timeptr
, int wnum
)
191 int fday
= first_day (timeptr
->tm_year
+ tm_year_base
);
193 timeptr
->tm_yday
= wnum
* 7 + timeptr
->tm_wday
- fday
;
194 if (timeptr
->tm_yday
< 0) {
195 timeptr
->tm_wday
= fday
;
196 timeptr
->tm_yday
= 0;
201 * Set `timeptr' given `wnum' (week number [0, 53])
205 set_week_number_mon (struct tm
*timeptr
, int wnum
)
207 int fday
= (first_day (timeptr
->tm_year
+ tm_year_base
) + 6) % 7;
209 timeptr
->tm_yday
= wnum
* 7 + (timeptr
->tm_wday
+ 6) % 7 - fday
;
210 if (timeptr
->tm_yday
< 0) {
211 timeptr
->tm_wday
= (fday
+ 1) % 7;
212 timeptr
->tm_yday
= 0;
217 * Set `timeptr' given `wnum' (week number [0, 53])
221 set_week_number_mon4 (struct tm
*timeptr
, int wnum
)
223 int fday
= (first_day (timeptr
->tm_year
+ tm_year_base
) + 6) % 7;
229 timeptr
->tm_yday
= offset
+ (wnum
- 1) * 7 + timeptr
->tm_wday
- fday
;
230 if (timeptr
->tm_yday
< 0) {
231 timeptr
->tm_wday
= fday
;
232 timeptr
->tm_yday
= 0;
240 ROKEN_LIB_FUNCTION
char * ROKEN_LIB_CALL
241 strptime (const char *buf
, const char *format
, struct tm
*timeptr
)
245 for (; (c
= *format
) != '\0'; ++format
) {
249 if (isspace ((unsigned char)c
)) {
250 while (isspace ((unsigned char)*buf
))
252 } else if (c
== '%' && format
[1] != '\0') {
254 if (c
== 'E' || c
== 'O')
258 ret
= match_string (&buf
, full_weekdays
);
261 timeptr
->tm_wday
= ret
;
264 ret
= match_string (&buf
, abb_weekdays
);
267 timeptr
->tm_wday
= ret
;
270 ret
= match_string (&buf
, full_month
);
273 timeptr
->tm_mon
= ret
;
277 ret
= match_string (&buf
, abb_month
);
280 timeptr
->tm_mon
= ret
;
283 if (parse_number(&buf
, 2, &ret
))
285 timeptr
->tm_year
= (ret
* 100) - tm_year_base
;
289 case 'D' : /* %m/%d/%y */
290 s
= strptime (buf
, "%m/%d/%y", timeptr
);
297 if (parse_number(&buf
, 2, &ret
))
299 timeptr
->tm_mday
= ret
;
303 if (parse_number(&buf
, 2, &ret
))
305 timeptr
->tm_hour
= ret
;
309 if (parse_number(&buf
, 2, &ret
))
312 timeptr
->tm_hour
= 0;
314 timeptr
->tm_hour
= ret
;
317 if (parse_number(&buf
, 3, &ret
))
321 timeptr
->tm_yday
= ret
- 1;
324 if (parse_number(&buf
, 2, &ret
))
328 timeptr
->tm_mon
= ret
- 1;
331 if (parse_number(&buf
, 2, &ret
))
333 timeptr
->tm_min
= ret
;
336 while (isspace ((unsigned char)*buf
))
340 ret
= match_string (&buf
, ampm
);
343 if (timeptr
->tm_hour
== 0) {
345 timeptr
->tm_hour
= 12;
347 timeptr
->tm_hour
+= 12;
349 case 'r' : /* %I:%M:%S %p */
350 s
= strptime (buf
, "%I:%M:%S %p", timeptr
);
355 case 'R' : /* %H:%M */
356 s
= strptime (buf
, "%H:%M", timeptr
);
362 if (parse_number(&buf
, 2, &ret
))
364 timeptr
->tm_sec
= ret
;
367 while (isspace ((unsigned char)*buf
))
370 case 'T' : /* %H:%M:%S */
372 s
= strptime (buf
, "%H:%M:%S", timeptr
);
378 if (parse_number(&buf
, 1, &ret
))
382 timeptr
->tm_wday
= ret
- 1;
385 if (parse_number(&buf
, 1, &ret
))
387 timeptr
->tm_wday
= ret
;
390 if (parse_number(&buf
, 2, &ret
))
392 set_week_number_sun (timeptr
, ret
);
395 if (parse_number(&buf
, 2, &ret
))
397 set_week_number_mon4 (timeptr
, ret
);
400 if (parse_number(&buf
, 2, &ret
))
402 set_week_number_mon (timeptr
, ret
);
405 s
= strptime (buf
, "%Y:%m:%d", timeptr
);
411 if (parse_number(&buf
, 2, &ret
))
414 timeptr
->tm_year
= 100 + ret
;
416 timeptr
->tm_year
= ret
;
419 if (parse_number(&buf
, 4, &ret
))
421 timeptr
->tm_year
= ret
- tm_year_base
;
435 if (*buf
== '%' || *++buf
== c
)
448 return rk_UNCONST(buf
);