wrong character in the GSM 03.38 table (ç for Ç)
[gammu.git] / helper / strptime.c
blobf6bb64122a2eb6fd5efbd4068210597220a96b39
1 /* $OpenBSD: strptime.c,v 1.11 2005/08/08 08:05:38 espie Exp $ */
2 /* $NetBSD: strptime.c,v 1.12 1998/01/20 21:39:40 mycroft Exp $ */
4 /*-
5 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
8 * This code was contributed to The NetBSD Foundation by Klaus Klein.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE 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 BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include <ctype.h>
40 #include <locale.h>
41 #include <string.h>
42 #include <time.h>
43 #include "tzfile.h"
44 #include "strptime.h"
45 #include "string.h"
47 static const struct {
48 const char *abday[7];
49 const char *day[7];
50 const char *abmon[12];
51 const char *mon[12];
52 const char *am_pm[2];
53 const char *d_t_fmt;
54 const char *d_fmt;
55 const char *t_fmt;
56 const char *t_fmt_ampm;
57 } _DefaultTimeLocale = {
59 "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
62 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
63 "Friday", "Saturday"
66 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
67 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
70 "January", "February", "March", "April", "May", "June", "July",
71 "August", "September", "October", "November", "December"
74 "AM", "PM"
76 "%a %b %d %H:%M:%S %Y",
77 "%m/%d/%y",
78 "%H:%M:%S",
79 "%I:%M:%S %p"
82 #define _ctloc(x) (_DefaultTimeLocale.x)
85 * We do not implement alternate representations. However, we always
86 * check whether a given modifier is allowed for a certain conversion.
88 #define _ALT_E 0x01
89 #define _ALT_O 0x02
90 #define _LEGAL_ALT(x) { if (alt_format & ~(x)) return (0); }
93 static int _conv_num(unsigned char **, int *, int, int);
94 static char *_strptime(const char *, const char *, struct tm *, int);
97 char *
98 strptime(const char *buf, const char *fmt, struct tm *tm)
100 return(_strptime(buf, fmt, tm, 1));
103 static char *
104 _strptime(const char *buf, const char *fmt, struct tm *tm, int initialize)
106 unsigned char c;
107 unsigned char *bp;
108 size_t len = 0;
109 int alt_format, i;
110 static int century, relyear;
112 if (initialize) {
113 century = TM_YEAR_BASE;
114 relyear = -1;
117 bp = (unsigned char *)buf;
118 while ((c = *fmt) != '\0') {
119 /* Clear `alternate' modifier prior to new conversion. */
120 alt_format = 0;
122 /* Eat up white-space. */
123 if (isspace(c)) {
124 while (isspace(*bp))
125 bp++;
127 fmt++;
128 continue;
131 if ((c = *fmt++) != '%')
132 goto literal;
135 again: switch (c = *fmt++) {
136 case '%': /* "%%" is converted to "%". */
137 literal:
138 if (c != *bp++)
139 return (NULL);
141 break;
144 * "Alternative" modifiers. Just set the appropriate flag
145 * and start over again.
147 case 'E': /* "%E?" alternative conversion modifier. */
148 _LEGAL_ALT(0);
149 alt_format |= _ALT_E;
150 goto again;
152 case 'O': /* "%O?" alternative conversion modifier. */
153 _LEGAL_ALT(0);
154 alt_format |= _ALT_O;
155 goto again;
158 * "Complex" conversion rules, implemented through recursion.
160 case 'c': /* Date and time, using the locale's format. */
161 _LEGAL_ALT(_ALT_E);
162 if ((bp = _strptime(bp, _ctloc(d_t_fmt), tm, 0)) == 0)
163 return (NULL);
164 break;
166 case 'D': /* The date as "%m/%d/%y". */
167 _LEGAL_ALT(0);
168 if ((bp = _strptime(bp, "%m/%d/%y", tm, 0)) == 0)
169 return (NULL);
170 break;
172 case 'R': /* The time as "%H:%M". */
173 _LEGAL_ALT(0);
174 if ((bp = _strptime(bp, "%H:%M", tm, 0)) == 0)
175 return (NULL);
176 break;
178 case 'r': /* The time as "%I:%M:%S %p". */
179 _LEGAL_ALT(0);
180 if ((bp = _strptime(bp, "%I:%M:%S %p", tm, 0)) == 0)
181 return (NULL);
182 break;
184 case 'T': /* The time as "%H:%M:%S". */
185 _LEGAL_ALT(0);
186 if ((bp = _strptime(bp, "%H:%M:%S", tm, 0)) == 0)
187 return (NULL);
188 break;
190 case 'X': /* The time, using the locale's format. */
191 _LEGAL_ALT(_ALT_E);
192 if ((bp = _strptime(bp, _ctloc(t_fmt), tm, 0)) == 0)
193 return (NULL);
194 break;
196 case 'x': /* The date, using the locale's format. */
197 _LEGAL_ALT(_ALT_E);
198 if ((bp = _strptime(bp, _ctloc(d_fmt), tm, 0)) == 0)
199 return (NULL);
200 break;
203 * "Elementary" conversion rules.
205 case 'A': /* The day of week, using the locale's form. */
206 case 'a':
207 _LEGAL_ALT(0);
208 for (i = 0; i < 7; i++) {
209 /* Full name. */
210 len = strlen(_ctloc(day[i]));
211 if (strncasecmp(_ctloc(day[i]), bp, len) == 0)
212 break;
214 /* Abbreviated name. */
215 len = strlen(_ctloc(abday[i]));
216 if (strncasecmp(_ctloc(abday[i]), bp, len) == 0)
217 break;
220 /* Nothing matched. */
221 if (i == 7)
222 return (NULL);
224 tm->tm_wday = i;
225 bp += len;
226 break;
228 case 'B': /* The month, using the locale's form. */
229 case 'b':
230 case 'h':
231 _LEGAL_ALT(0);
232 for (i = 0; i < 12; i++) {
233 /* Full name. */
234 len = strlen(_ctloc(mon[i]));
235 if (strncasecmp(_ctloc(mon[i]), bp, len) == 0)
236 break;
238 /* Abbreviated name. */
239 len = strlen(_ctloc(abmon[i]));
240 if (strncasecmp(_ctloc(abmon[i]), bp, len) == 0)
241 break;
244 /* Nothing matched. */
245 if (i == 12)
246 return (NULL);
248 tm->tm_mon = i;
249 bp += len;
250 break;
252 case 'C': /* The century number. */
253 _LEGAL_ALT(_ALT_E);
254 if (!(_conv_num(&bp, &i, 0, 99)))
255 return (NULL);
257 century = i * 100;
258 break;
260 case 'd': /* The day of month. */
261 case 'e':
262 _LEGAL_ALT(_ALT_O);
263 if (!(_conv_num(&bp, &tm->tm_mday, 1, 31)))
264 return (NULL);
265 break;
267 case 'k': /* The hour (24-hour clock representation). */
268 _LEGAL_ALT(0);
269 /* FALLTHROUGH */
270 case 'H':
271 _LEGAL_ALT(_ALT_O);
272 if (!(_conv_num(&bp, &tm->tm_hour, 0, 23)))
273 return (NULL);
274 break;
276 case 'l': /* The hour (12-hour clock representation). */
277 _LEGAL_ALT(0);
278 /* FALLTHROUGH */
279 case 'I':
280 _LEGAL_ALT(_ALT_O);
281 if (!(_conv_num(&bp, &tm->tm_hour, 1, 12)))
282 return (NULL);
283 break;
285 case 'j': /* The day of year. */
286 _LEGAL_ALT(0);
287 if (!(_conv_num(&bp, &tm->tm_yday, 1, 366)))
288 return (NULL);
289 tm->tm_yday--;
290 break;
292 case 'M': /* The minute. */
293 _LEGAL_ALT(_ALT_O);
294 if (!(_conv_num(&bp, &tm->tm_min, 0, 59)))
295 return (NULL);
296 break;
298 case 'm': /* The month. */
299 _LEGAL_ALT(_ALT_O);
300 if (!(_conv_num(&bp, &tm->tm_mon, 1, 12)))
301 return (NULL);
302 tm->tm_mon--;
303 break;
305 case 'p': /* The locale's equivalent of AM/PM. */
306 _LEGAL_ALT(0);
307 /* AM? */
308 len = strlen(_ctloc(am_pm[0]));
309 if (strncasecmp(_ctloc(am_pm[0]), bp, len) == 0) {
310 if (tm->tm_hour > 12) /* i.e., 13:00 AM ?! */
311 return (NULL);
312 else if (tm->tm_hour == 12)
313 tm->tm_hour = 0;
315 bp += len;
316 break;
318 /* PM? */
319 len = strlen(_ctloc(am_pm[1]));
320 if (strncasecmp(_ctloc(am_pm[1]), bp, len) == 0) {
321 if (tm->tm_hour > 12) /* i.e., 13:00 PM ?! */
322 return (NULL);
323 else if (tm->tm_hour < 12)
324 tm->tm_hour += 12;
326 bp += len;
327 break;
330 /* Nothing matched. */
331 return (NULL);
333 case 'S': /* The seconds. */
334 _LEGAL_ALT(_ALT_O);
335 if (!(_conv_num(&bp, &tm->tm_sec, 0, 61)))
336 return (NULL);
337 break;
339 case 'U': /* The week of year, beginning on sunday. */
340 case 'W': /* The week of year, beginning on monday. */
341 _LEGAL_ALT(_ALT_O);
343 * XXX This is bogus, as we can not assume any valid
344 * information present in the tm structure at this
345 * point to calculate a real value, so just check the
346 * range for now.
348 if (!(_conv_num(&bp, &i, 0, 53)))
349 return (NULL);
350 break;
352 case 'w': /* The day of week, beginning on sunday. */
353 _LEGAL_ALT(_ALT_O);
354 if (!(_conv_num(&bp, &tm->tm_wday, 0, 6)))
355 return (NULL);
356 break;
358 case 'Y': /* The year. */
359 _LEGAL_ALT(_ALT_E);
360 if (!(_conv_num(&bp, &i, 0, 9999)))
361 return (NULL);
363 relyear = -1;
364 tm->tm_year = i - TM_YEAR_BASE;
365 break;
367 case 'y': /* The year within the century (2 digits). */
368 _LEGAL_ALT(_ALT_E | _ALT_O);
369 if (!(_conv_num(&bp, &relyear, 0, 99)))
370 return (NULL);
371 break;
374 * Miscellaneous conversions.
376 case 'n': /* Any kind of white-space. */
377 case 't':
378 _LEGAL_ALT(0);
379 while (isspace(*bp))
380 bp++;
381 break;
384 default: /* Unknown/unsupported conversion. */
385 return (NULL);
392 * We need to evaluate the two digit year spec (%y)
393 * last as we can get a century spec (%C) at any time.
395 if (relyear != -1) {
396 if (century == TM_YEAR_BASE) {
397 if (relyear <= 68)
398 tm->tm_year = relyear + 2000 - TM_YEAR_BASE;
399 else
400 tm->tm_year = relyear + 1900 - TM_YEAR_BASE;
401 } else {
402 tm->tm_year = relyear + century - TM_YEAR_BASE;
406 return ((char *)bp);
410 static int
411 _conv_num(unsigned char **buf, int *dest, int llim, int ulim)
413 int result = 0;
414 int rulim = ulim;
416 if (**buf < '0' || **buf > '9')
417 return (0);
419 /* we use rulim to break out of the loop when we run out of digits */
420 do {
421 result *= 10;
422 result += *(*buf)++ - '0';
423 rulim /= 10;
424 } while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9');
426 if (result < llim || result > ulim)
427 return (0);
429 *dest = result;
430 return (1);