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 timeptr->tm_yday = 1; // Initialize to a well known, valid date
190 timeptr->tm_isdst = 0; // Tuesday March 18 14:05:00 2003 UTC
193 timeptr->tm_hour = 14;
194 timeptr->tm_mday = 18;
196 timeptr->tm_year = 103;
197 timeptr->tm_wday = 2;
198 timeptr->tm_yday = 77;
200 for (; (c = *format) != '\0'; ++format) {
205 while (isspace (*buf))
207 } else if (c == '%' && format[1] != '\0') {
209 if (c == 'E' || c == 'O')
213 ret = match_string (&buf, weekdays, 0);
216 timeptr->tm_wday = ret;
219 ret = match_string (&buf, weekdays, 3);
222 timeptr->tm_wday = ret;
225 ret = match_string (&buf, month, 0);
228 timeptr->tm_mon = ret;
232 ret = match_string (&buf, month, 3);
235 timeptr->tm_mon = ret;
238 ret = strtol (buf, &s, 10);
241 timeptr->tm_year = (ret * 100) - tm_year_base;
245 // Date and Time in the current locale - unsupported
247 case 'D' : /* %m/%d/%y */
248 s = strptime (buf, "%m/%d/%y", timeptr);
255 ret = strtol (buf, &s, 10);
258 timeptr->tm_mday = ret;
263 ret = strtol (buf, &s, 10);
266 timeptr->tm_hour = ret;
271 ret = strtol (buf, &s, 10);
275 timeptr->tm_hour = 0;
277 timeptr->tm_hour = ret;
281 ret = strtol (buf, &s, 10);
284 timeptr->tm_yday = ret - 1;
288 ret = strtol (buf, &s, 10);
291 timeptr->tm_mon = ret - 1;
295 ret = strtol (buf, &s, 10);
298 timeptr->tm_min = ret;
308 ret = match_string (&buf, ampm, 0);
311 if (timeptr->tm_hour == 0) {
313 timeptr->tm_hour = 12;
315 timeptr->tm_hour += 12;
317 case 'r' : /* %I:%M:%S %p */
318 s = strptime (buf, "%I:%M:%S %p", timeptr);
323 case 'R' : /* %H:%M */
324 s = strptime (buf, "%H:%M", timeptr);
330 ret = strtol (buf, &s, 10);
333 timeptr->tm_sec = ret;
342 case 'T' : /* %H:%M:%S */
344 s = strptime (buf, "%H:%M:%S", timeptr);
350 ret = strtol (buf, &s, 10);
353 timeptr->tm_wday = ret - 1;
357 ret = strtol (buf, &s, 10);
360 timeptr->tm_wday = ret;
364 ret = strtol (buf, &s, 10);
367 set_week_number_sun (timeptr, ret);
371 ret = strtol (buf, &s, 10);
374 set_week_number_mon4 (timeptr, ret);
378 ret = strtol (buf, &s, 10);
381 set_week_number_mon (timeptr, ret);
385 s = strptime (buf, "%Y:%m:%d", timeptr);
391 ret = strtol (buf, &s, 10);
395 timeptr->tm_year = 100 + ret;
397 timeptr->tm_year = ret;
401 ret = strtol (buf, &s, 10);
404 timeptr->tm_year = ret - tm_year_base;
408 // Timezone spec not handled
420 if (*buf == '%' || *++buf == c)