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. */
33
#if defined(_WIN32) && !defined(__CYGWIN__)
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
* tm_year is relative this year
107
const int tm_year_base = 1900;
110
* Return TRUE iff `year' was a leap year.
111
* Needed for strptime.
114
is_leap_year (int year)
116
return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
119
/* Needed for strptime. */
121
match_string (const char **buf, const char **strs)
125
for (i = 0; strs[i] != NULL; ++i) {
126
int len = strlen (strs[i]);
128
if (strncasecmp (*buf, strs[i], len) == 0) {
136
/* Needed for strptime. */
142
for (; year > 1970; --year)
143
ret = (ret + 365 + is_leap_year (year) ? 1 : 0) % 7;
148
* Set `timeptr' given `wnum' (week number [0, 53])
149
* Needed for strptime
153
set_week_number_sun (struct tm *timeptr, int wnum)
155
int fday = first_day (timeptr->tm_year + tm_year_base);
157
timeptr->tm_yday = wnum * 7 + timeptr->tm_wday - fday;
158
if (timeptr->tm_yday < 0) {
159
timeptr->tm_wday = fday;
160
timeptr->tm_yday = 0;
165
* Set `timeptr' given `wnum' (week number [0, 53])
166
* Needed for strptime
170
set_week_number_mon (struct tm *timeptr, int wnum)
172
int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
174
timeptr->tm_yday = wnum * 7 + (timeptr->tm_wday + 6) % 7 - fday;
175
if (timeptr->tm_yday < 0) {
176
timeptr->tm_wday = (fday + 1) % 7;
177
timeptr->tm_yday = 0;
182
* Set `timeptr' given `wnum' (week number [0, 53])
183
* Needed for strptime
186
set_week_number_mon4 (struct tm *timeptr, int wnum)
188
int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
194
timeptr->tm_yday = offset + (wnum - 1) * 7 + timeptr->tm_wday - fday;
195
if (timeptr->tm_yday < 0) {
196
timeptr->tm_wday = fday;
197
timeptr->tm_yday = 0;
201
/* strptime: roken */
204
/* strptime (const char *buf, const char *format, struct tm *timeptr) */
205
strptime (const char *buf, const char *format, struct tm *timeptr)
209
for (; (c = *format) != '\0'; ++format) {
214
while (isspace (*buf))
216
} else if (c == '%' && format[1] != '\0') {
218
if (c == 'E' || c == 'O')
222
ret = match_string (&buf, full_weekdays);
225
timeptr->tm_wday = ret;
228
ret = match_string (&buf, abb_weekdays);
231
timeptr->tm_wday = ret;
234
ret = match_string (&buf, full_month);
237
timeptr->tm_mon = ret;
241
ret = match_string (&buf, abb_month);
244
timeptr->tm_mon = ret;
247
ret = strtol (buf, &s, 10);
250
timeptr->tm_year = (ret * 100) - tm_year_base;
255
case 'D' : /* %m/%d/%y */
256
s = strptime (buf, "%m/%d/%y", timeptr);
263
ret = strtol (buf, &s, 10);
266
timeptr->tm_mday = ret;
271
ret = strtol (buf, &s, 10);
274
timeptr->tm_hour = ret;
279
ret = strtol (buf, &s, 10);
283
timeptr->tm_hour = 0;
285
timeptr->tm_hour = ret;
289
ret = strtol (buf, &s, 10);
292
timeptr->tm_yday = ret - 1;
296
ret = strtol (buf, &s, 10);
299
timeptr->tm_mon = ret - 1;
303
ret = strtol (buf, &s, 10);
306
timeptr->tm_min = ret;
316
ret = match_string (&buf, ampm);
319
if (timeptr->tm_hour == 0) {
321
timeptr->tm_hour = 12;
323
timeptr->tm_hour += 12;
325
case 'r' : /* %I:%M:%S %p */
326
s = strptime (buf, "%I:%M:%S %p", timeptr);
331
case 'R' : /* %H:%M */
332
s = strptime (buf, "%H:%M", timeptr);
338
ret = strtol (buf, &s, 10);
341
timeptr->tm_sec = ret;
350
case 'T' : /* %H:%M:%S */
352
s = strptime (buf, "%H:%M:%S", timeptr);
358
ret = strtol (buf, &s, 10);
361
timeptr->tm_wday = ret - 1;
365
ret = strtol (buf, &s, 10);
368
timeptr->tm_wday = ret;
372
ret = strtol (buf, &s, 10);
375
set_week_number_sun (timeptr, ret);
379
ret = strtol (buf, &s, 10);
382
set_week_number_mon4 (timeptr, ret);
386
ret = strtol (buf, &s, 10);
389
set_week_number_mon (timeptr, ret);
393
s = strptime (buf, "%Y:%m:%d", timeptr);
399
ret = strtol (buf, &s, 10);
403
timeptr->tm_year = 100 + ret;
405
timeptr->tm_year = ret;
409
ret = strtol (buf, &s, 10);
412
timeptr->tm_year = ret - tm_year_base;
427
if (*buf == '%' || *++buf == c)