]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/compat/strptime.c
10ec315748b25fe13db6859d7d828a30e5a9fa16
1 /** strptime workaround (for oa macos leopard)
2 * This strptime follows the man strptime (2001-11-12)
3 * conforming to SUSv2, POSIX.1-2001
5 * This very simple version of strptime has no:
9 * - Does not process week numbers
10 * - Does not properly processes year day
13 * Copyright (c) 2008, NLnet Labs, Matthijs Mekking
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are met:
18 * * Redistributions of source code must retain the above copyright notice,
19 * this list of conditions and the following disclaimer.
20 * * Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * * Neither the name of NLnetLabs nor the names of its
24 * contributors may be used to endorse or promote products derived from this
25 * software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
46 #ifndef STRPTIME_WORKS
48 #define TM_YEAR_BASE 1900
53 static const char *abb_weekdays
[] = {
54 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
56 static const char *full_weekdays
[] = {
57 "Sunday", "Monday", "Tuesday", "Wednesday",
58 "Thursday", "Friday", "Saturday", NULL
60 static const char *abb_months
[] = {
61 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
62 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
64 static const char *full_months
[] = {
65 "January", "February", "March", "April", "May", "June",
66 "July", "August", "September", "October", "November", "December", NULL
68 static const char *ampm
[] = {
73 match_string(const char **buf
, const char **strs
)
77 for (i
= 0; strs
[i
] != NULL
; i
++) {
78 int len
= strlen(strs
[i
]);
79 if (strncasecmp (*buf
, strs
[i
], len
) == 0) {
88 str2int(const char **buf
, int max
)
92 while (*buf
[0] != '\0' && isdigit((unsigned char)*buf
[0]) && count
<max
) {
93 ret
= ret
*10 + (*buf
[0] - '0');
103 /** Converts the character string s to values which are stored in tm
104 * using the format specified by format
107 unbound_strptime(const char *s
, const char *format
, struct tm
*tm
)
112 while ((c
= *format
) != '\0') {
113 /* whitespace, literal or format */
114 if (isspace((unsigned char)c
)) { /* whitespace */
115 /** whitespace matches zero or more whitespace characters in the
118 while (isspace((unsigned char)*s
))
121 else if (c
== '%') { /* format */
125 case '%': /* %% is converted to % */
131 case 'a': /* weekday name, abbreviated or full */
133 ret
= match_string(&s
, full_weekdays
);
135 ret
= match_string(&s
, abb_weekdays
);
141 case 'b': /* month name, abbreviated or full */
144 ret
= match_string(&s
, full_months
);
146 ret
= match_string(&s
, abb_months
);
152 case 'c': /* date and time representation */
153 if (!(s
= unbound_strptime(s
, "%x %X", tm
))) {
157 case 'C': /* century number */
158 ret
= str2int(&s
, 2);
159 if (ret
< 0 || ret
> 99) { /* must be in [00,99] */
164 tm
->tm_year
= ret
*100 + (tm
->tm_year%100
);
167 tm
->tm_year
= ret
*100 - TM_YEAR_BASE
;
171 case 'd': /* day of month */
173 ret
= str2int(&s
, 2);
174 if (ret
< 1 || ret
> 31) { /* must be in [01,31] */
179 case 'D': /* equivalent to %m/%d/%y */
180 if (!(s
= unbound_strptime(s
, "%m/%d/%y", tm
))) {
185 ret
= str2int(&s
, 2);
186 if (ret
< 0 || ret
> 23) { /* must be in [00,23] */
191 case 'I': /* 12hr clock hour */
192 ret
= str2int(&s
, 2);
193 if (ret
< 1 || ret
> 12) { /* must be in [01,12] */
196 if (ret
== 12) /* actually [0,11] */
200 case 'j': /* day of year */
201 ret
= str2int(&s
, 2);
202 if (ret
< 1 || ret
> 366) { /* must be in [001,366] */
207 case 'm': /* month */
208 ret
= str2int(&s
, 2);
209 if (ret
< 1 || ret
> 12) { /* must be in [01,12] */
212 /* months go from 0-11 */
213 tm
->tm_mon
= (ret
-1);
215 case 'M': /* minute */
216 ret
= str2int(&s
, 2);
217 if (ret
< 0 || ret
> 59) { /* must be in [00,59] */
222 case 'n': /* arbitrary whitespace */
224 while (isspace((unsigned char)*s
))
227 case 'p': /* am pm */
228 ret
= match_string(&s
, ampm
);
232 if (tm
->tm_hour
< 0 || tm
->tm_hour
> 11) { /* %I */
236 if (ret
== 1) /* pm */
239 case 'r': /* equivalent of %I:%M:%S %p */
240 if (!(s
= unbound_strptime(s
, "%I:%M:%S %p", tm
))) {
244 case 'R': /* equivalent of %H:%M */
245 if (!(s
= unbound_strptime(s
, "%H:%M", tm
))) {
249 case 'S': /* seconds */
250 ret
= str2int(&s
, 2);
251 /* 60 may occur for leap seconds */
252 /* earlier 61 was also allowed */
253 if (ret
< 0 || ret
> 60) { /* must be in [00,60] */
258 case 'T': /* equivalent of %H:%M:%S */
259 if (!(s
= unbound_strptime(s
, "%H:%M:%S", tm
))) {
263 case 'U': /* week number, with the first Sun of Jan being w1 */
264 ret
= str2int(&s
, 2);
265 if (ret
< 0 || ret
> 53) { /* must be in [00,53] */
268 /** it is hard (and not necessary for nsd) to determine time
269 * data from week number.
272 case 'w': /* day of week */
273 ret
= str2int(&s
, 1);
274 if (ret
< 0 || ret
> 6) { /* must be in [0,6] */
279 case 'W': /* week number, with the first Mon of Jan being w1 */
280 ret
= str2int(&s
, 2);
281 if (ret
< 0 || ret
> 53) { /* must be in [00,53] */
284 /** it is hard (and not necessary for nsd) to determine time
285 * data from week number.
288 case 'x': /* date format */
289 if (!(s
= unbound_strptime(s
, "%m/%d/%y", tm
))) {
293 case 'X': /* time format */
294 if (!(s
= unbound_strptime(s
, "%H:%M:%S", tm
))) {
298 case 'y': /* last two digits of a year */
299 ret
= str2int(&s
, 2);
300 if (ret
< 0 || ret
> 99) { /* must be in [00,99] */
304 tm
->tm_year
= ((tm
->tm_year
/100) * 100) + ret
;
310 * if in [0,68] we are in 21th century,
311 * if in [69,99] we are in 20th century.
313 if (ret
< 69) /* 2000 */
319 ret
= str2int(&s
, 4);
320 if (ret
< 0 || ret
> 9999) {
323 tm
->tm_year
= ret
- TM_YEAR_BASE
;
326 default: /* unsupported, cannot match format */
332 /* if input cannot match format, return NULL */
341 /* return pointer to remainder of s */
345 #endif /* STRPTIME_WORKS */