Mercurial > libavformat.hg
annotate strptime.c @ 229:b0771ae979e3 libavformat
removed ctype.h header
author | bellard |
---|---|
date | Mon, 08 Sep 2003 20:04:44 +0000 |
parents | 89a78842f6c2 |
children |
rev | line source |
---|---|
0 | 1 /* Convert a string representation of time to a time value. |
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc. | |
3 This file is part of the GNU C Library. | |
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. | |
5 | |
6 The GNU C Library is free software; you can redistribute it and/or | |
7 modify it under the terms of the GNU Library General Public License as | |
8 published by the Free Software Foundation; either version 2 of the | |
9 License, or (at your option) any later version. | |
10 | |
11 The GNU C Library is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 Library General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU Library General Public | |
17 License along with the GNU C Library; see the file COPYING.LIB. If not, | |
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 Boston, MA 02111-1307, USA. */ | |
20 | |
21 /* XXX This version of the implementation is not really complete. | |
22 Some of the fields cannot add information alone. But if seeing | |
23 some of them in the same format (such as year, week and weekday) | |
24 this is enough information for determining the date. */ | |
25 | |
26 #ifdef HAVE_CONFIG_H | |
27 # include "config.h" | |
28 #endif | |
29 | |
30 #include <ctype.h> | |
31 #include <limits.h> | |
32 #include <string.h> | |
33 #include <time.h> | |
34 | |
35 #ifdef _LIBC | |
36 # include "../locale/localeinfo.h" | |
37 #endif | |
38 | |
39 #include "strptime.h" | |
213
89a78842f6c2
localtime_r patch by (Glenn Maynard <g_sf at zewt dot org>)
michaelni
parents:
0
diff
changeset
|
40 #include "localtime_r.h" |
0 | 41 |
42 #ifndef __P | |
43 # if defined (__GNUC__) || (defined (__STDC__) && __STDC__) | |
44 # define __P(args) args | |
45 # else | |
46 # define __P(args) () | |
47 # endif /* GCC. */ | |
48 #endif /* Not __P. */ | |
49 | |
50 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL | |
51 #if defined __GNUC__ && __GNUC__ >= 2 | |
52 # define match_string(cs1, s2) \ | |
53 ({ size_t len = strlen (cs1); \ | |
54 int result = strncasecmp ((cs1), (s2), len) == 0; \ | |
55 if (result) (s2) += len; \ | |
56 result; }) | |
57 #else | |
58 /* Oh come on. Get a reasonable compiler. */ | |
59 # define match_string(cs1, s2) \ | |
60 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1)) | |
61 #endif | |
62 /* We intentionally do not use isdigit() for testing because this will | |
63 lead to problems with the wide character version. */ | |
64 #define get_number(from, to, n) \ | |
65 do { \ | |
66 int __n = n; \ | |
67 val = 0; \ | |
68 while (*rp == ' ') \ | |
69 ++rp; \ | |
70 if (*rp < '0' || *rp > '9') \ | |
71 return NULL; \ | |
72 do { \ | |
73 val *= 10; \ | |
74 val += *rp++ - '0'; \ | |
75 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \ | |
76 if (val < from || val > to) \ | |
77 return NULL; \ | |
78 } while (0) | |
79 #ifdef _NL_CURRENT | |
80 # define get_alt_number(from, to, n) \ | |
81 ({ \ | |
82 __label__ do_normal; \ | |
83 if (*decided != raw) \ | |
84 { \ | |
85 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \ | |
86 int __n = n; \ | |
87 int any = 0; \ | |
88 while (*rp == ' ') \ | |
89 ++rp; \ | |
90 val = 0; \ | |
91 do { \ | |
92 val *= 10; \ | |
93 while (*alts != '\0') \ | |
94 { \ | |
95 size_t len = strlen (alts); \ | |
96 if (strncasecmp (alts, rp, len) == 0) \ | |
97 break; \ | |
98 alts += len + 1; \ | |
99 ++val; \ | |
100 } \ | |
101 if (*alts == '\0') \ | |
102 { \ | |
103 if (*decided == not && ! any) \ | |
104 goto do_normal; \ | |
105 /* If we haven't read anything it's an error. */ \ | |
106 if (! any) \ | |
107 return NULL; \ | |
108 /* Correct the premature multiplication. */ \ | |
109 val /= 10; \ | |
110 break; \ | |
111 } \ | |
112 else \ | |
113 *decided = loc; \ | |
114 } while (--__n > 0 && val * 10 <= to); \ | |
115 if (val < from || val > to) \ | |
116 return NULL; \ | |
117 } \ | |
118 else \ | |
119 { \ | |
120 do_normal: \ | |
121 get_number (from, to, n); \ | |
122 } \ | |
123 0; \ | |
124 }) | |
125 #else | |
126 # define get_alt_number(from, to, n) \ | |
127 /* We don't have the alternate representation. */ \ | |
128 get_number(from, to, n) | |
129 #endif | |
130 #define recursive(new_fmt) \ | |
131 (*(new_fmt) != '\0' \ | |
132 && (rp = strptime_internal (rp, (new_fmt), tm, decided, era_cnt)) != NULL) | |
133 | |
134 | |
135 #ifdef _LIBC | |
136 /* This is defined in locale/C-time.c in the GNU libc. */ | |
137 extern const struct locale_data _nl_C_LC_TIME; | |
138 extern const unsigned short int __mon_yday[2][13]; | |
139 | |
140 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string) | |
141 # define ab_weekday_name \ | |
142 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string) | |
143 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string) | |
144 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string) | |
145 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string) | |
146 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string) | |
147 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string) | |
148 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string) | |
149 # define HERE_T_FMT_AMPM \ | |
150 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string) | |
151 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string) | |
152 | |
153 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n) | |
154 #else | |
155 static char const weekday_name[][10] = | |
156 { | |
157 "Sunday", "Monday", "Tuesday", "Wednesday", | |
158 "Thursday", "Friday", "Saturday" | |
159 }; | |
160 static char const ab_weekday_name[][4] = | |
161 { | |
162 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" | |
163 }; | |
164 static char const month_name[][10] = | |
165 { | |
166 "January", "February", "March", "April", "May", "June", | |
167 "July", "August", "September", "October", "November", "December" | |
168 }; | |
169 static char const ab_month_name[][4] = | |
170 { | |
171 "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
172 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
173 }; | |
174 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y" | |
175 # define HERE_D_FMT "%m/%d/%y" | |
176 # define HERE_AM_STR "AM" | |
177 # define HERE_PM_STR "PM" | |
178 # define HERE_T_FMT_AMPM "%I:%M:%S %p" | |
179 # define HERE_T_FMT "%H:%M:%S" | |
180 | |
181 const unsigned short int __mon_yday[2][13] = | |
182 { | |
183 /* Normal years. */ | |
184 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, | |
185 /* Leap years. */ | |
186 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } | |
187 }; | |
188 #endif | |
189 | |
190 /* Status of lookup: do we use the locale data or the raw data? */ | |
191 enum locale_status { not, loc, raw }; | |
192 | |
193 | |
194 #ifndef __isleap | |
195 /* Nonzero if YEAR is a leap year (every 4 years, | |
196 except every 100th isn't, and every 400th is). */ | |
197 # define __isleap(year) \ | |
198 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) | |
199 #endif | |
200 | |
201 /* Compute the day of the week. */ | |
202 static void | |
203 day_of_the_week (struct tm *tm) | |
204 { | |
205 /* We know that January 1st 1970 was a Thursday (= 4). Compute the | |
206 the difference between this data in the one on TM and so determine | |
207 the weekday. */ | |
208 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2); | |
209 int wday = (-473 | |
210 + (365 * (tm->tm_year - 70)) | |
211 + (corr_year / 4) | |
212 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0) | |
213 + (((corr_year / 4) / 25) / 4) | |
214 + __mon_yday[0][tm->tm_mon] | |
215 + tm->tm_mday - 1); | |
216 tm->tm_wday = ((wday % 7) + 7) % 7; | |
217 } | |
218 | |
219 /* Compute the day of the year. */ | |
220 static void | |
221 day_of_the_year (struct tm *tm) | |
222 { | |
223 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon] | |
224 + (tm->tm_mday - 1)); | |
225 } | |
226 | |
227 static char * | |
228 #ifdef _LIBC | |
229 internal_function | |
230 #endif | |
231 strptime_internal __P ((const char *rp, const char *fmt, struct tm *tm, | |
232 enum locale_status *decided, int era_cnt)); | |
233 | |
234 static char * | |
235 #ifdef _LIBC | |
236 internal_function | |
237 #endif | |
238 strptime_internal (rp, fmt, tm, decided, era_cnt) | |
239 const char *rp; | |
240 const char *fmt; | |
241 struct tm *tm; | |
242 enum locale_status *decided; | |
243 int era_cnt; | |
244 { | |
245 const char *rp_backup; | |
246 int cnt; | |
247 size_t val; | |
248 int have_I, is_pm; | |
249 int century, want_century; | |
250 int want_era; | |
251 int have_wday, want_xday; | |
252 int have_yday; | |
253 int have_mon, have_mday; | |
254 #ifdef _NL_CURRENT | |
255 size_t num_eras; | |
256 #endif | |
257 struct era_entry *era; | |
258 | |
259 have_I = is_pm = 0; | |
260 century = -1; | |
261 want_century = 0; | |
262 want_era = 0; | |
263 era = NULL; | |
264 | |
265 have_wday = want_xday = have_yday = have_mon = have_mday = 0; | |
266 | |
267 while (*fmt != '\0') | |
268 { | |
269 /* A white space in the format string matches 0 more or white | |
270 space in the input string. */ | |
271 if (isspace (*fmt)) | |
272 { | |
273 while (isspace (*rp)) | |
274 ++rp; | |
275 ++fmt; | |
276 continue; | |
277 } | |
278 | |
279 /* Any character but `%' must be matched by the same character | |
280 in the iput string. */ | |
281 if (*fmt != '%') | |
282 { | |
283 match_char (*fmt++, *rp++); | |
284 continue; | |
285 } | |
286 | |
287 ++fmt; | |
288 #ifndef _NL_CURRENT | |
289 /* We need this for handling the `E' modifier. */ | |
290 start_over: | |
291 #endif | |
292 | |
293 /* Make back up of current processing pointer. */ | |
294 rp_backup = rp; | |
295 | |
296 switch (*fmt++) | |
297 { | |
298 case '%': | |
299 /* Match the `%' character itself. */ | |
300 match_char ('%', *rp++); | |
301 break; | |
302 case 'a': | |
303 case 'A': | |
304 /* Match day of week. */ | |
305 for (cnt = 0; cnt < 7; ++cnt) | |
306 { | |
307 #ifdef _NL_CURRENT | |
308 if (*decided !=raw) | |
309 { | |
310 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp)) | |
311 { | |
312 if (*decided == not | |
313 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt), | |
314 weekday_name[cnt])) | |
315 *decided = loc; | |
316 break; | |
317 } | |
318 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp)) | |
319 { | |
320 if (*decided == not | |
321 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), | |
322 ab_weekday_name[cnt])) | |
323 *decided = loc; | |
324 break; | |
325 } | |
326 } | |
327 #endif | |
328 if (*decided != loc | |
329 && (match_string (weekday_name[cnt], rp) | |
330 || match_string (ab_weekday_name[cnt], rp))) | |
331 { | |
332 *decided = raw; | |
333 break; | |
334 } | |
335 } | |
336 if (cnt == 7) | |
337 /* Does not match a weekday name. */ | |
338 return NULL; | |
339 tm->tm_wday = cnt; | |
340 have_wday = 1; | |
341 break; | |
342 case 'b': | |
343 case 'B': | |
344 case 'h': | |
345 /* Match month name. */ | |
346 for (cnt = 0; cnt < 12; ++cnt) | |
347 { | |
348 #ifdef _NL_CURRENT | |
349 if (*decided !=raw) | |
350 { | |
351 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp)) | |
352 { | |
353 if (*decided == not | |
354 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt), | |
355 month_name[cnt])) | |
356 *decided = loc; | |
357 break; | |
358 } | |
359 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp)) | |
360 { | |
361 if (*decided == not | |
362 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), | |
363 ab_month_name[cnt])) | |
364 *decided = loc; | |
365 break; | |
366 } | |
367 } | |
368 #endif | |
369 if (match_string (month_name[cnt], rp) | |
370 || match_string (ab_month_name[cnt], rp)) | |
371 { | |
372 *decided = raw; | |
373 break; | |
374 } | |
375 } | |
376 if (cnt == 12) | |
377 /* Does not match a month name. */ | |
378 return NULL; | |
379 tm->tm_mon = cnt; | |
380 want_xday = 1; | |
381 break; | |
382 case 'c': | |
383 /* Match locale's date and time format. */ | |
384 #ifdef _NL_CURRENT | |
385 if (*decided != raw) | |
386 { | |
387 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT))) | |
388 { | |
389 if (*decided == loc) | |
390 return NULL; | |
391 else | |
392 rp = rp_backup; | |
393 } | |
394 else | |
395 { | |
396 if (*decided == not && | |
397 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT)) | |
398 *decided = loc; | |
399 want_xday = 1; | |
400 break; | |
401 } | |
402 *decided = raw; | |
403 } | |
404 #endif | |
405 if (!recursive (HERE_D_T_FMT)) | |
406 return NULL; | |
407 want_xday = 1; | |
408 break; | |
409 case 'C': | |
410 /* Match century number. */ | |
411 #ifdef _NL_CURRENT | |
412 match_century: | |
413 #endif | |
414 get_number (0, 99, 2); | |
415 century = val; | |
416 want_xday = 1; | |
417 break; | |
418 case 'd': | |
419 case 'e': | |
420 /* Match day of month. */ | |
421 get_number (1, 31, 2); | |
422 tm->tm_mday = val; | |
423 have_mday = 1; | |
424 want_xday = 1; | |
425 break; | |
426 case 'F': | |
427 if (!recursive ("%Y-%m-%d")) | |
428 return NULL; | |
429 want_xday = 1; | |
430 break; | |
431 case 'x': | |
432 #ifdef _NL_CURRENT | |
433 if (*decided != raw) | |
434 { | |
435 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT))) | |
436 { | |
437 if (*decided == loc) | |
438 return NULL; | |
439 else | |
440 rp = rp_backup; | |
441 } | |
442 else | |
443 { | |
444 if (*decided == not | |
445 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT)) | |
446 *decided = loc; | |
447 want_xday = 1; | |
448 break; | |
449 } | |
450 *decided = raw; | |
451 } | |
452 #endif | |
453 /* Fall through. */ | |
454 case 'D': | |
455 /* Match standard day format. */ | |
456 if (!recursive (HERE_D_FMT)) | |
457 return NULL; | |
458 want_xday = 1; | |
459 break; | |
460 case 'k': | |
461 case 'H': | |
462 /* Match hour in 24-hour clock. */ | |
463 get_number (0, 23, 2); | |
464 tm->tm_hour = val; | |
465 have_I = 0; | |
466 break; | |
467 case 'I': | |
468 /* Match hour in 12-hour clock. */ | |
469 get_number (1, 12, 2); | |
470 tm->tm_hour = val % 12; | |
471 have_I = 1; | |
472 break; | |
473 case 'j': | |
474 /* Match day number of year. */ | |
475 get_number (1, 366, 3); | |
476 tm->tm_yday = val - 1; | |
477 have_yday = 1; | |
478 break; | |
479 case 'm': | |
480 /* Match number of month. */ | |
481 get_number (1, 12, 2); | |
482 tm->tm_mon = val - 1; | |
483 have_mon = 1; | |
484 want_xday = 1; | |
485 break; | |
486 case 'M': | |
487 /* Match minute. */ | |
488 get_number (0, 59, 2); | |
489 tm->tm_min = val; | |
490 break; | |
491 case 'n': | |
492 case 't': | |
493 /* Match any white space. */ | |
494 while (isspace (*rp)) | |
495 ++rp; | |
496 break; | |
497 case 'p': | |
498 /* Match locale's equivalent of AM/PM. */ | |
499 #ifdef _NL_CURRENT | |
500 if (*decided != raw) | |
501 { | |
502 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp)) | |
503 { | |
504 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR)) | |
505 *decided = loc; | |
506 break; | |
507 } | |
508 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp)) | |
509 { | |
510 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR)) | |
511 *decided = loc; | |
512 is_pm = 1; | |
513 break; | |
514 } | |
515 *decided = raw; | |
516 } | |
517 #endif | |
518 if (!match_string (HERE_AM_STR, rp)) | |
519 if (match_string (HERE_PM_STR, rp)) | |
520 is_pm = 1; | |
521 else | |
522 return NULL; | |
523 break; | |
524 case 'r': | |
525 #ifdef _NL_CURRENT | |
526 if (*decided != raw) | |
527 { | |
528 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM))) | |
529 { | |
530 if (*decided == loc) | |
531 return NULL; | |
532 else | |
533 rp = rp_backup; | |
534 } | |
535 else | |
536 { | |
537 if (*decided == not && | |
538 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM), | |
539 HERE_T_FMT_AMPM)) | |
540 *decided = loc; | |
541 break; | |
542 } | |
543 *decided = raw; | |
544 } | |
545 #endif | |
546 if (!recursive (HERE_T_FMT_AMPM)) | |
547 return NULL; | |
548 break; | |
549 case 'R': | |
550 if (!recursive ("%H:%M")) | |
551 return NULL; | |
552 break; | |
553 case 's': | |
554 { | |
555 /* The number of seconds may be very high so we cannot use | |
556 the `get_number' macro. Instead read the number | |
557 character for character and construct the result while | |
558 doing this. */ | |
559 time_t secs = 0; | |
560 if (*rp < '0' || *rp > '9') | |
561 /* We need at least one digit. */ | |
562 return NULL; | |
563 | |
564 do | |
565 { | |
566 secs *= 10; | |
567 secs += *rp++ - '0'; | |
568 } | |
569 while (*rp >= '0' && *rp <= '9'); | |
570 | |
571 if (localtime_r (&secs, tm) == NULL) | |
572 /* Error in function. */ | |
573 return NULL; | |
574 } | |
575 break; | |
576 case 'S': | |
577 get_number (0, 61, 2); | |
578 tm->tm_sec = val; | |
579 break; | |
580 case 'X': | |
581 #ifdef _NL_CURRENT | |
582 if (*decided != raw) | |
583 { | |
584 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT))) | |
585 { | |
586 if (*decided == loc) | |
587 return NULL; | |
588 else | |
589 rp = rp_backup; | |
590 } | |
591 else | |
592 { | |
593 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT)) | |
594 *decided = loc; | |
595 break; | |
596 } | |
597 *decided = raw; | |
598 } | |
599 #endif | |
600 /* Fall through. */ | |
601 case 'T': | |
602 if (!recursive (HERE_T_FMT)) | |
603 return NULL; | |
604 break; | |
605 case 'u': | |
606 get_number (1, 7, 1); | |
607 tm->tm_wday = val % 7; | |
608 have_wday = 1; | |
609 break; | |
610 case 'g': | |
611 get_number (0, 99, 2); | |
612 /* XXX This cannot determine any field in TM. */ | |
613 break; | |
614 case 'G': | |
615 if (*rp < '0' || *rp > '9') | |
616 return NULL; | |
617 /* XXX Ignore the number since we would need some more | |
618 information to compute a real date. */ | |
619 do | |
620 ++rp; | |
621 while (*rp >= '0' && *rp <= '9'); | |
622 break; | |
623 case 'U': | |
624 case 'V': | |
625 case 'W': | |
626 get_number (0, 53, 2); | |
627 /* XXX This cannot determine any field in TM without some | |
628 information. */ | |
629 break; | |
630 case 'w': | |
631 /* Match number of weekday. */ | |
632 get_number (0, 6, 1); | |
633 tm->tm_wday = val; | |
634 have_wday = 1; | |
635 break; | |
636 case 'y': | |
637 #ifdef _NL_CURRENT | |
638 match_year_in_century: | |
639 #endif | |
640 /* Match year within century. */ | |
641 get_number (0, 99, 2); | |
642 /* The "Year 2000: The Millennium Rollover" paper suggests that | |
643 values in the range 69-99 refer to the twentieth century. */ | |
644 tm->tm_year = val >= 69 ? val : val + 100; | |
645 /* Indicate that we want to use the century, if specified. */ | |
646 want_century = 1; | |
647 want_xday = 1; | |
648 break; | |
649 case 'Y': | |
650 /* Match year including century number. */ | |
651 get_number (0, 9999, 4); | |
652 tm->tm_year = val - 1900; | |
653 want_century = 0; | |
654 want_xday = 1; | |
655 break; | |
656 case 'Z': | |
657 /* XXX How to handle this? */ | |
658 break; | |
659 case 'E': | |
660 #ifdef _NL_CURRENT | |
661 switch (*fmt++) | |
662 { | |
663 case 'c': | |
664 /* Match locale's alternate date and time format. */ | |
665 if (*decided != raw) | |
666 { | |
667 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT); | |
668 | |
669 if (*fmt == '\0') | |
670 fmt = _NL_CURRENT (LC_TIME, D_T_FMT); | |
671 | |
672 if (!recursive (fmt)) | |
673 { | |
674 if (*decided == loc) | |
675 return NULL; | |
676 else | |
677 rp = rp_backup; | |
678 } | |
679 else | |
680 { | |
681 if (strcmp (fmt, HERE_D_T_FMT)) | |
682 *decided = loc; | |
683 want_xday = 1; | |
684 break; | |
685 } | |
686 *decided = raw; | |
687 } | |
688 /* The C locale has no era information, so use the | |
689 normal representation. */ | |
690 if (!recursive (HERE_D_T_FMT)) | |
691 return NULL; | |
692 want_xday = 1; | |
693 break; | |
694 case 'C': | |
695 if (*decided != raw) | |
696 { | |
697 if (era_cnt >= 0) | |
698 { | |
699 era = _nl_select_era_entry (era_cnt); | |
700 if (match_string (era->era_name, rp)) | |
701 { | |
702 *decided = loc; | |
703 break; | |
704 } | |
705 else | |
706 return NULL; | |
707 } | |
708 else | |
709 { | |
710 num_eras = _NL_CURRENT_WORD (LC_TIME, | |
711 _NL_TIME_ERA_NUM_ENTRIES); | |
712 for (era_cnt = 0; era_cnt < (int) num_eras; | |
713 ++era_cnt, rp = rp_backup) | |
714 { | |
715 era = _nl_select_era_entry (era_cnt); | |
716 if (match_string (era->era_name, rp)) | |
717 { | |
718 *decided = loc; | |
719 break; | |
720 } | |
721 } | |
722 if (era_cnt == (int) num_eras) | |
723 { | |
724 era_cnt = -1; | |
725 if (*decided == loc) | |
726 return NULL; | |
727 } | |
728 else | |
729 break; | |
730 } | |
731 | |
732 *decided = raw; | |
733 } | |
734 /* The C locale has no era information, so use the | |
735 normal representation. */ | |
736 goto match_century; | |
737 case 'y': | |
738 if (*decided == raw) | |
739 goto match_year_in_century; | |
740 | |
741 get_number(0, 9999, 4); | |
742 tm->tm_year = val; | |
743 want_era = 1; | |
744 want_xday = 1; | |
745 break; | |
746 case 'Y': | |
747 if (*decided != raw) | |
748 { | |
749 num_eras = _NL_CURRENT_WORD (LC_TIME, | |
750 _NL_TIME_ERA_NUM_ENTRIES); | |
751 for (era_cnt = 0; era_cnt < (int) num_eras; | |
752 ++era_cnt, rp = rp_backup) | |
753 { | |
754 era = _nl_select_era_entry (era_cnt); | |
755 if (recursive (era->era_format)) | |
756 break; | |
757 } | |
758 if (era_cnt == (int) num_eras) | |
759 { | |
760 era_cnt = -1; | |
761 if (*decided == loc) | |
762 return NULL; | |
763 else | |
764 rp = rp_backup; | |
765 } | |
766 else | |
767 { | |
768 *decided = loc; | |
769 era_cnt = -1; | |
770 break; | |
771 } | |
772 | |
773 *decided = raw; | |
774 } | |
775 get_number (0, 9999, 4); | |
776 tm->tm_year = val - 1900; | |
777 want_century = 0; | |
778 want_xday = 1; | |
779 break; | |
780 case 'x': | |
781 if (*decided != raw) | |
782 { | |
783 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT); | |
784 | |
785 if (*fmt == '\0') | |
786 fmt = _NL_CURRENT (LC_TIME, D_FMT); | |
787 | |
788 if (!recursive (fmt)) | |
789 { | |
790 if (*decided == loc) | |
791 return NULL; | |
792 else | |
793 rp = rp_backup; | |
794 } | |
795 else | |
796 { | |
797 if (strcmp (fmt, HERE_D_FMT)) | |
798 *decided = loc; | |
799 break; | |
800 } | |
801 *decided = raw; | |
802 } | |
803 if (!recursive (HERE_D_FMT)) | |
804 return NULL; | |
805 break; | |
806 case 'X': | |
807 if (*decided != raw) | |
808 { | |
809 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT); | |
810 | |
811 if (*fmt == '\0') | |
812 fmt = _NL_CURRENT (LC_TIME, T_FMT); | |
813 | |
814 if (!recursive (fmt)) | |
815 { | |
816 if (*decided == loc) | |
817 return NULL; | |
818 else | |
819 rp = rp_backup; | |
820 } | |
821 else | |
822 { | |
823 if (strcmp (fmt, HERE_T_FMT)) | |
824 *decided = loc; | |
825 break; | |
826 } | |
827 *decided = raw; | |
828 } | |
829 if (!recursive (HERE_T_FMT)) | |
830 return NULL; | |
831 break; | |
832 default: | |
833 return NULL; | |
834 } | |
835 break; | |
836 #else | |
837 /* We have no information about the era format. Just use | |
838 the normal format. */ | |
839 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y' | |
840 && *fmt != 'x' && *fmt != 'X') | |
841 /* This is an illegal format. */ | |
842 return NULL; | |
843 | |
844 goto start_over; | |
845 #endif | |
846 case 'O': | |
847 switch (*fmt++) | |
848 { | |
849 case 'd': | |
850 case 'e': | |
851 /* Match day of month using alternate numeric symbols. */ | |
852 get_alt_number (1, 31, 2); | |
853 tm->tm_mday = val; | |
854 have_mday = 1; | |
855 want_xday = 1; | |
856 break; | |
857 case 'H': | |
858 /* Match hour in 24-hour clock using alternate numeric | |
859 symbols. */ | |
860 get_alt_number (0, 23, 2); | |
861 tm->tm_hour = val; | |
862 have_I = 0; | |
863 break; | |
864 case 'I': | |
865 /* Match hour in 12-hour clock using alternate numeric | |
866 symbols. */ | |
867 get_alt_number (1, 12, 2); | |
868 tm->tm_hour = val - 1; | |
869 have_I = 1; | |
870 break; | |
871 case 'm': | |
872 /* Match month using alternate numeric symbols. */ | |
873 get_alt_number (1, 12, 2); | |
874 tm->tm_mon = val - 1; | |
875 have_mon = 1; | |
876 want_xday = 1; | |
877 break; | |
878 case 'M': | |
879 /* Match minutes using alternate numeric symbols. */ | |
880 get_alt_number (0, 59, 2); | |
881 tm->tm_min = val; | |
882 break; | |
883 case 'S': | |
884 /* Match seconds using alternate numeric symbols. */ | |
885 get_alt_number (0, 61, 2); | |
886 tm->tm_sec = val; | |
887 break; | |
888 case 'U': | |
889 case 'V': | |
890 case 'W': | |
891 get_alt_number (0, 53, 2); | |
892 /* XXX This cannot determine any field in TM without | |
893 further information. */ | |
894 break; | |
895 case 'w': | |
896 /* Match number of weekday using alternate numeric symbols. */ | |
897 get_alt_number (0, 6, 1); | |
898 tm->tm_wday = val; | |
899 have_wday = 1; | |
900 break; | |
901 case 'y': | |
902 /* Match year within century using alternate numeric symbols. */ | |
903 get_alt_number (0, 99, 2); | |
904 tm->tm_year = val >= 69 ? val : val + 100; | |
905 want_xday = 1; | |
906 break; | |
907 default: | |
908 return NULL; | |
909 } | |
910 break; | |
911 default: | |
912 return NULL; | |
913 } | |
914 } | |
915 | |
916 if (have_I && is_pm) | |
917 tm->tm_hour += 12; | |
918 | |
919 if (century != -1) | |
920 { | |
921 if (want_century) | |
922 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100; | |
923 else | |
924 /* Only the century, but not the year. Strange, but so be it. */ | |
925 tm->tm_year = (century - 19) * 100; | |
926 } | |
927 | |
928 #ifdef _NL_CURRENT | |
929 if (era_cnt != -1) | |
930 { | |
931 era = _nl_select_era_entry(era_cnt); | |
932 if (want_era) | |
933 tm->tm_year = (era->start_date[0] | |
934 + ((tm->tm_year - era->offset) | |
935 * era->absolute_direction)); | |
936 else | |
937 /* Era start year assumed. */ | |
938 tm->tm_year = era->start_date[0]; | |
939 } | |
940 else | |
941 #endif | |
942 if (want_era) | |
943 return NULL; | |
944 | |
945 if (want_xday && !have_wday) | |
946 { | |
947 if ( !(have_mon && have_mday) && have_yday) | |
948 { | |
949 /* We don't have tm_mon and/or tm_mday, compute them. */ | |
950 int t_mon = 0; | |
951 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday) | |
952 t_mon++; | |
953 if (!have_mon) | |
954 tm->tm_mon = t_mon - 1; | |
955 if (!have_mday) | |
956 tm->tm_mday = | |
957 (tm->tm_yday | |
958 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1); | |
959 } | |
960 day_of_the_week (tm); | |
961 } | |
962 if (want_xday && !have_yday) | |
963 day_of_the_year (tm); | |
964 | |
965 return (char *) rp; | |
966 } | |
967 | |
968 | |
969 char * | |
970 strptime (buf, format, tm) | |
971 const char *buf; | |
972 const char *format; | |
973 struct tm *tm; | |
974 { | |
975 enum locale_status decided; | |
976 | |
977 #ifdef _NL_CURRENT | |
978 decided = not; | |
979 #else | |
980 decided = raw; | |
981 #endif | |
982 return strptime_internal (buf, format, tm, &decided, -1); | |
983 } |