2 * Copyright (c) 1999 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. */
39 RCSID("$Id: strptime.c,v 1.2 1999/11/12 15:29:55 assar Exp $");
41 static const char *abb_weekdays
[] = {
52 static const char *full_weekdays
[] = {
63 static const char *abb_month
[] = {
79 static const char *full_month
[] = {
95 static const char *ampm
[] = {
102 * Try to match `*buf' to one of the strings in `strs'. Return the
103 * index of the matching string (or -1 if none). Also advance buf.
107 match_string (const char **buf
, const char **strs
)
111 for (i
= 0; strs
[i
] != NULL
; ++i
) {
112 int len
= strlen (strs
[i
]);
114 if (strncasecmp (*buf
, strs
[i
], len
) == 0) {
123 * tm_year is relative this year */
125 const int tm_year_base
= 1900;
128 * Return TRUE iff `year' was a leap year.
132 is_leap_year (int year
)
134 return (year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0);
138 * Return the weekday [0,6] (0 = Sunday) of the first day of `year'
146 for (; year
> 1970; --year
)
147 ret
= (ret
+ 365 + is_leap_year (year
) ? 1 : 0) % 7;
152 * Set `timeptr' given `wnum' (week number [0, 53])
156 set_week_number_sun (struct tm
*timeptr
, int wnum
)
158 int fday
= first_day (timeptr
->tm_year
+ tm_year_base
);
160 timeptr
->tm_yday
= wnum
* 7 + timeptr
->tm_wday
- fday
;
161 if (timeptr
->tm_yday
< 0) {
162 timeptr
->tm_wday
= fday
;
163 timeptr
->tm_yday
= 0;
168 * Set `timeptr' given `wnum' (week number [0, 53])
172 set_week_number_mon (struct tm
*timeptr
, int wnum
)
174 int fday
= (first_day (timeptr
->tm_year
+ tm_year_base
) + 6) % 7;
176 timeptr
->tm_yday
= wnum
* 7 + (timeptr
->tm_wday
+ 6) % 7 - fday
;
177 if (timeptr
->tm_yday
< 0) {
178 timeptr
->tm_wday
= (fday
+ 1) % 7;
179 timeptr
->tm_yday
= 0;
184 * Set `timeptr' given `wnum' (week number [0, 53])
188 set_week_number_mon4 (struct tm
*timeptr
, int wnum
)
190 int fday
= (first_day (timeptr
->tm_year
+ tm_year_base
) + 6) % 7;
196 timeptr
->tm_yday
= offset
+ (wnum
- 1) * 7 + timeptr
->tm_wday
- fday
;
197 if (timeptr
->tm_yday
< 0) {
198 timeptr
->tm_wday
= fday
;
199 timeptr
->tm_yday
= 0;
208 strptime (const char *buf
, const char *format
, struct tm
*timeptr
)
212 for (; (c
= *format
) != '\0'; ++format
) {
217 while (isspace (*buf
))
219 } else if (c
== '%' && format
[1] != '\0') {
221 if (c
== 'E' || c
== 'O')
225 ret
= match_string (&buf
, full_weekdays
);
228 timeptr
->tm_wday
= ret
;
231 ret
= match_string (&buf
, abb_weekdays
);
234 timeptr
->tm_wday
= ret
;
237 ret
= match_string (&buf
, full_month
);
240 timeptr
->tm_mon
= ret
;
244 ret
= match_string (&buf
, abb_month
);
247 timeptr
->tm_mon
= ret
;
250 ret
= strtol (buf
, &s
, 10);
253 timeptr
->tm_year
= (ret
* 100) - tm_year_base
;
258 case 'D' : /* %m/%d/%y */
259 s
= strptime (buf
, "%m/%d/%y", timeptr
);
266 ret
= strtol (buf
, &s
, 10);
269 timeptr
->tm_mday
= ret
;
274 ret
= strtol (buf
, &s
, 10);
277 timeptr
->tm_hour
= ret
;
282 ret
= strtol (buf
, &s
, 10);
286 timeptr
->tm_hour
= 0;
288 timeptr
->tm_hour
= ret
;
292 ret
= strtol (buf
, &s
, 10);
295 timeptr
->tm_yday
= ret
- 1;
299 ret
= strtol (buf
, &s
, 10);
302 timeptr
->tm_mon
= ret
- 1;
306 ret
= strtol (buf
, &s
, 10);
309 timeptr
->tm_min
= ret
;
319 ret
= match_string (&buf
, ampm
);
322 if (timeptr
->tm_hour
== 0) {
324 timeptr
->tm_hour
= 12;
326 timeptr
->tm_hour
+= 12;
328 case 'r' : /* %I:%M:%S %p */
329 s
= strptime (buf
, "%I:%M:%S %p", timeptr
);
334 case 'R' : /* %H:%M */
335 s
= strptime (buf
, "%H:%M", timeptr
);
341 ret
= strtol (buf
, &s
, 10);
344 timeptr
->tm_sec
= ret
;
353 case 'T' : /* %H:%M:%S */
355 s
= strptime (buf
, "%H:%M:%S", timeptr
);
361 ret
= strtol (buf
, &s
, 10);
364 timeptr
->tm_wday
= ret
- 1;
368 ret
= strtol (buf
, &s
, 10);
371 timeptr
->tm_wday
= ret
;
375 ret
= strtol (buf
, &s
, 10);
378 set_week_number_sun (timeptr
, ret
);
382 ret
= strtol (buf
, &s
, 10);
385 set_week_number_mon4 (timeptr
, ret
);
389 ret
= strtol (buf
, &s
, 10);
392 set_week_number_mon (timeptr
, ret
);
396 s
= strptime (buf
, "%Y:%m:%d", timeptr
);
402 ret
= strtol (buf
, &s
, 10);
406 timeptr
->tm_year
= 100 + ret
;
408 timeptr
->tm_year
= ret
;
412 ret
= strtol (buf
, &s
, 10);
415 timeptr
->tm_year
= ret
- tm_year_base
;
430 if (*buf
== '%' || *++buf
== c
)