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