2 // Adapted for use in eCos by Gary Thomas
3 // Copyright (C) 2003 Gary Thomas
7 * Copyright (c) 1999 Kungliga Tekniska Högskolan
8 * (Royal Institute of Technology, Stockholm, Sweden).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of KTH nor the names of its contributors may be
23 * used to endorse or promote products derived from this software without
24 * specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
27 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
44 static const char *weekdays[] = {
55 static const char *month[] = {
71 static const char *ampm[] = {
78 * Try to match `*buf' to one of the strings in `strs'. Return the
79 * index of the matching string (or -1 if none). Also advance buf.
83 match_string (const char **buf, const char **strs, int ablen)
87 for (i = 0; strs[i] != NULL; ++i) {
88 int len = ablen > 0 ? ablen : strlen (strs[i]);
90 if (strncasecmp (*buf, strs[i], len) == 0) {
99 * tm_year is relative this year
102 const int tm_year_base = 1900;
105 * Return TRUE iff `year' was a leap year.
109 is_leap_year (int year)
111 return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
115 * Return the weekday [0,6] (0 = Sunday) of the first day of `year'
121 int ret = 4; // Jan 1, 1970 was on Thursday
123 for (; year > 1970; --year)
124 ret = (ret + 365 + is_leap_year (year) ? 1 : 0) % 7;
129 * Set `timeptr' given `wnum' (week number [0, 53])
133 set_week_number_sun (struct tm *timeptr, int wnum)
135 int fday = first_day (timeptr->tm_year + tm_year_base);
137 timeptr->tm_yday = wnum * 7 + timeptr->tm_wday - fday;
138 if (timeptr->tm_yday < 0) {
139 timeptr->tm_wday = fday;
140 timeptr->tm_yday = 0;
145 * Set `timeptr' given `wnum' (week number [0, 53])
149 set_week_number_mon (struct tm *timeptr, int wnum)
151 int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
153 timeptr->tm_yday = wnum * 7 + (timeptr->tm_wday + 6) % 7 - fday;
154 if (timeptr->tm_yday < 0) {
155 timeptr->tm_wday = (fday + 1) % 7;
156 timeptr->tm_yday = 0;
161 * Set `timeptr' given `wnum' (week number [0, 53])
165 set_week_number_mon4 (struct tm *timeptr, int wnum)
167 int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
173 timeptr->tm_yday = offset + (wnum - 1) * 7 + timeptr->tm_wday - fday;
174 if (timeptr->tm_yday < 0) {
175 timeptr->tm_wday = fday;
176 timeptr->tm_yday = 0;
185 strptime (const char *buf, const char *format, struct tm *timeptr)
189 for (; (c = *format) != '\0'; ++format) {
194 while (isspace (*buf))
196 } else if (c == '%' && format[1] != '\0') {
198 if (c == 'E' || c == 'O')
202 ret = match_string (&buf, weekdays, 0);
205 timeptr->tm_wday = ret;
208 ret = match_string (&buf, weekdays, 3);
211 timeptr->tm_wday = ret;
214 ret = match_string (&buf, month, 0);
217 timeptr->tm_mon = ret;
221 ret = match_string (&buf, month, 3);
224 timeptr->tm_mon = ret;
227 ret = strtol (buf, &s, 10);
230 timeptr->tm_year = (ret * 100) - tm_year_base;
234 // Date and Time in the current locale - unsupported
236 case 'D' : /* %m/%d/%y */
237 s = strptime (buf, "%m/%d/%y", timeptr);
244 ret = strtol (buf, &s, 10);
247 timeptr->tm_mday = ret;
252 ret = strtol (buf, &s, 10);
255 timeptr->tm_hour = ret;
260 ret = strtol (buf, &s, 10);
264 timeptr->tm_hour = 0;
266 timeptr->tm_hour = ret;
270 ret = strtol (buf, &s, 10);
273 timeptr->tm_yday = ret - 1;
277 ret = strtol (buf, &s, 10);
280 timeptr->tm_mon = ret - 1;
284 ret = strtol (buf, &s, 10);
287 timeptr->tm_min = ret;
297 ret = match_string (&buf, ampm, 0);
300 if (timeptr->tm_hour == 0) {
302 timeptr->tm_hour = 12;
304 timeptr->tm_hour += 12;
306 case 'r' : /* %I:%M:%S %p */
307 s = strptime (buf, "%I:%M:%S %p", timeptr);
312 case 'R' : /* %H:%M */
313 s = strptime (buf, "%H:%M", timeptr);
319 ret = strtol (buf, &s, 10);
322 timeptr->tm_sec = ret;
331 case 'T' : /* %H:%M:%S */
333 s = strptime (buf, "%H:%M:%S", timeptr);
339 ret = strtol (buf, &s, 10);
342 timeptr->tm_wday = ret - 1;
346 ret = strtol (buf, &s, 10);
349 timeptr->tm_wday = ret;
353 ret = strtol (buf, &s, 10);
356 set_week_number_sun (timeptr, ret);
360 ret = strtol (buf, &s, 10);
363 set_week_number_mon4 (timeptr, ret);
367 ret = strtol (buf, &s, 10);
370 set_week_number_mon (timeptr, ret);
374 s = strptime (buf, "%Y:%m:%d", timeptr);
380 ret = strtol (buf, &s, 10);
384 timeptr->tm_year = 100 + ret;
386 timeptr->tm_year = ret;
390 ret = strtol (buf, &s, 10);
393 timeptr->tm_year = ret - tm_year_base;
397 // Timezone spec not handled
409 if (*buf == '%' || *++buf == c)