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. */
37 static const char *abb_weekdays
[] = {
48 static const char *full_weekdays
[] = {
59 static const char *abb_month
[] = {
75 static const char *full_month
[] = {
91 static const char *ampm
[] = {
98 * tm_year is relative this year
100 const int tm_year_base
= 1900;
103 * Return TRUE iff `year' was a leap year.
104 * Needed for strptime.
107 is_leap_year (int year
)
109 return (year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0);
112 /* Needed for strptime. */
114 match_string (const char **buf
, const char **strs
)
118 for (i
= 0; strs
[i
] != NULL
; ++i
) {
119 int len
= strlen (strs
[i
]);
121 if (strncasecmp (*buf
, strs
[i
], len
) == 0) {
129 /* Needed for strptime. */
135 for (; year
> 1970; --year
)
136 ret
= (ret
+ 365 + is_leap_year (year
) ? 1 : 0) % 7;
141 * Set `timeptr' given `wnum' (week number [0, 53])
142 * Needed for strptime
146 set_week_number_sun (struct tm
*timeptr
, int wnum
)
148 int fday
= first_day (timeptr
->tm_year
+ tm_year_base
);
150 timeptr
->tm_yday
= wnum
* 7 + timeptr
->tm_wday
- fday
;
151 if (timeptr
->tm_yday
< 0) {
152 timeptr
->tm_wday
= fday
;
153 timeptr
->tm_yday
= 0;
158 * Set `timeptr' given `wnum' (week number [0, 53])
159 * Needed for strptime
163 set_week_number_mon (struct tm
*timeptr
, int wnum
)
165 int fday
= (first_day (timeptr
->tm_year
+ tm_year_base
) + 6) % 7;
167 timeptr
->tm_yday
= wnum
* 7 + (timeptr
->tm_wday
+ 6) % 7 - fday
;
168 if (timeptr
->tm_yday
< 0) {
169 timeptr
->tm_wday
= (fday
+ 1) % 7;
170 timeptr
->tm_yday
= 0;
175 * Set `timeptr' given `wnum' (week number [0, 53])
176 * Needed for strptime
179 set_week_number_mon4 (struct tm
*timeptr
, int wnum
)
181 int fday
= (first_day (timeptr
->tm_year
+ tm_year_base
) + 6) % 7;
187 timeptr
->tm_yday
= offset
+ (wnum
- 1) * 7 + timeptr
->tm_wday
- fday
;
188 if (timeptr
->tm_yday
< 0) {
189 timeptr
->tm_wday
= fday
;
190 timeptr
->tm_yday
= 0;
194 /* strptime: roken */
197 //strptime (const char *buf, const char *format, struct tm *timeptr)
198 _DEFUN (strptime
, (buf
, format
, timeptr
),
199 _CONST
char *buf _AND
200 _CONST
char *format _AND
205 for (; (c
= *format
) != '\0'; ++format
) {
210 while (isspace (*buf
))
212 } else if (c
== '%' && format
[1] != '\0') {
214 if (c
== 'E' || c
== 'O')
218 ret
= match_string (&buf
, full_weekdays
);
221 timeptr
->tm_wday
= ret
;
224 ret
= match_string (&buf
, abb_weekdays
);
227 timeptr
->tm_wday
= ret
;
230 ret
= match_string (&buf
, full_month
);
233 timeptr
->tm_mon
= ret
;
237 ret
= match_string (&buf
, abb_month
);
240 timeptr
->tm_mon
= ret
;
243 ret
= strtol (buf
, &s
, 10);
246 timeptr
->tm_year
= (ret
* 100) - tm_year_base
;
251 case 'D' : /* %m/%d/%y */
252 s
= strptime (buf
, "%m/%d/%y", timeptr
);
259 ret
= strtol (buf
, &s
, 10);
262 timeptr
->tm_mday
= ret
;
267 ret
= strtol (buf
, &s
, 10);
270 timeptr
->tm_hour
= ret
;
275 ret
= strtol (buf
, &s
, 10);
279 timeptr
->tm_hour
= 0;
281 timeptr
->tm_hour
= ret
;
285 ret
= strtol (buf
, &s
, 10);
288 timeptr
->tm_yday
= ret
- 1;
292 ret
= strtol (buf
, &s
, 10);
295 timeptr
->tm_mon
= ret
- 1;
299 ret
= strtol (buf
, &s
, 10);
302 timeptr
->tm_min
= ret
;
312 ret
= match_string (&buf
, ampm
);
315 if (timeptr
->tm_hour
== 0) {
317 timeptr
->tm_hour
= 12;
319 timeptr
->tm_hour
+= 12;
321 case 'r' : /* %I:%M:%S %p */
322 s
= strptime (buf
, "%I:%M:%S %p", timeptr
);
327 case 'R' : /* %H:%M */
328 s
= strptime (buf
, "%H:%M", timeptr
);
334 ret
= strtol (buf
, &s
, 10);
337 timeptr
->tm_sec
= ret
;
346 case 'T' : /* %H:%M:%S */
348 s
= strptime (buf
, "%H:%M:%S", timeptr
);
354 ret
= strtol (buf
, &s
, 10);
357 timeptr
->tm_wday
= ret
- 1;
361 ret
= strtol (buf
, &s
, 10);
364 timeptr
->tm_wday
= ret
;
368 ret
= strtol (buf
, &s
, 10);
371 set_week_number_sun (timeptr
, ret
);
375 ret
= strtol (buf
, &s
, 10);
378 set_week_number_mon4 (timeptr
, ret
);
382 ret
= strtol (buf
, &s
, 10);
385 set_week_number_mon (timeptr
, ret
);
389 s
= strptime (buf
, "%Y:%m:%d", timeptr
);
395 ret
= strtol (buf
, &s
, 10);
399 timeptr
->tm_year
= 100 + ret
;
401 timeptr
->tm_year
= ret
;
405 ret
= strtol (buf
, &s
, 10);
408 timeptr
->tm_year
= ret
- tm_year_base
;
423 if (*buf
== '%' || *++buf
== c
)