]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 2002-2014 Free Software Foundation, Inc. |
60f20c19 RM |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU Lesser General Public | |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Lesser General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
60f20c19 | 17 | |
ccadf7b5 UD |
18 | #ifdef HAVE_CONFIG_H |
19 | # include <config.h> | |
20 | #endif | |
60f20c19 | 21 | |
1750bc8d | 22 | #include <assert.h> |
ccadf7b5 UD |
23 | #include <ctype.h> |
24 | #include <langinfo.h> | |
25 | #include <limits.h> | |
26 | #include <string.h> | |
27 | #include <time.h> | |
f2d4c706 | 28 | #include <stdbool.h> |
ccadf7b5 UD |
29 | |
30 | #ifdef _LIBC | |
31 | # include "../locale/localeinfo.h" | |
32 | #endif | |
33 | ||
34 | ||
ccadf7b5 UD |
35 | #if ! HAVE_LOCALTIME_R && ! defined localtime_r |
36 | # ifdef _LIBC | |
37 | # define localtime_r __localtime_r | |
38 | # else | |
39 | /* Approximate localtime_r as best we can in its absence. */ | |
40 | # define localtime_r my_localtime_r | |
cc9f65ce | 41 | static struct tm *localtime_r (const time_t *, struct tm *); |
ccadf7b5 UD |
42 | static struct tm * |
43 | localtime_r (t, tp) | |
44 | const time_t *t; | |
45 | struct tm *tp; | |
46 | { | |
47 | struct tm *l = localtime (t); | |
48 | if (! l) | |
49 | return 0; | |
50 | *tp = *l; | |
51 | return tp; | |
52 | } | |
53 | # endif /* ! _LIBC */ | |
54 | #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */ | |
55 | ||
56 | ||
57 | #define match_char(ch1, ch2) if (ch1 != ch2) return NULL | |
58 | #if defined __GNUC__ && __GNUC__ >= 2 | |
59 | # define match_string(cs1, s2) \ | |
60 | ({ size_t len = strlen (cs1); \ | |
61 | int result = __strncasecmp_l ((cs1), (s2), len, locale) == 0; \ | |
62 | if (result) (s2) += len; \ | |
63 | result; }) | |
64 | #else | |
65 | /* Oh come on. Get a reasonable compiler. */ | |
66 | # define match_string(cs1, s2) \ | |
67 | (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1)) | |
68 | #endif | |
69 | /* We intentionally do not use isdigit() for testing because this will | |
70 | lead to problems with the wide character version. */ | |
71 | #define get_number(from, to, n) \ | |
72 | do { \ | |
73 | int __n = n; \ | |
74 | val = 0; \ | |
ff491d14 | 75 | while (ISSPACE (*rp)) \ |
ccadf7b5 UD |
76 | ++rp; \ |
77 | if (*rp < '0' || *rp > '9') \ | |
78 | return NULL; \ | |
79 | do { \ | |
80 | val *= 10; \ | |
81 | val += *rp++ - '0'; \ | |
82 | } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \ | |
83 | if (val < from || val > to) \ | |
84 | return NULL; \ | |
85 | } while (0) | |
86 | #ifdef _NL_CURRENT | |
87 | # define get_alt_number(from, to, n) \ | |
88 | ({ \ | |
89 | __label__ do_normal; \ | |
90 | \ | |
fa69dc9c | 91 | if (s.decided != raw) \ |
ccadf7b5 UD |
92 | { \ |
93 | val = _nl_parse_alt_digit (&rp HELPER_LOCALE_ARG); \ | |
fa69dc9c | 94 | if (val == -1 && s.decided != loc) \ |
ccadf7b5 | 95 | { \ |
fa69dc9c | 96 | s.decided = loc; \ |
ccadf7b5 UD |
97 | goto do_normal; \ |
98 | } \ | |
99 | if (val < from || val > to) \ | |
100 | return NULL; \ | |
101 | } \ | |
102 | else \ | |
103 | { \ | |
104 | do_normal: \ | |
105 | get_number (from, to, n); \ | |
106 | } \ | |
107 | 0; \ | |
108 | }) | |
109 | #else | |
110 | # define get_alt_number(from, to, n) \ | |
111 | /* We don't have the alternate representation. */ \ | |
112 | get_number(from, to, n) | |
113 | #endif | |
114 | #define recursive(new_fmt) \ | |
115 | (*(new_fmt) != '\0' \ | |
fa69dc9c | 116 | && (rp = __strptime_internal (rp, (new_fmt), tm, &s LOCALE_ARG)) != NULL) |
ccadf7b5 UD |
117 | |
118 | ||
119 | #ifdef _LIBC | |
120 | /* This is defined in locale/C-time.c in the GNU libc. */ | |
f095bb72 | 121 | extern const struct __locale_data _nl_C_LC_TIME attribute_hidden; |
ccadf7b5 UD |
122 | |
123 | # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string) | |
124 | # define ab_weekday_name \ | |
125 | (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string) | |
126 | # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string) | |
127 | # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string) | |
128 | # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string) | |
129 | # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string) | |
130 | # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string) | |
131 | # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string) | |
132 | # define HERE_T_FMT_AMPM \ | |
133 | (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string) | |
134 | # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string) | |
135 | ||
136 | # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n) | |
137 | #else | |
138 | static char const weekday_name[][10] = | |
139 | { | |
140 | "Sunday", "Monday", "Tuesday", "Wednesday", | |
141 | "Thursday", "Friday", "Saturday" | |
142 | }; | |
143 | static char const ab_weekday_name[][4] = | |
144 | { | |
145 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" | |
146 | }; | |
147 | static char const month_name[][10] = | |
148 | { | |
149 | "January", "February", "March", "April", "May", "June", | |
150 | "July", "August", "September", "October", "November", "December" | |
151 | }; | |
152 | static char const ab_month_name[][4] = | |
153 | { | |
154 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
155 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
156 | }; | |
157 | # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y" | |
158 | # define HERE_D_FMT "%m/%d/%y" | |
159 | # define HERE_AM_STR "AM" | |
160 | # define HERE_PM_STR "PM" | |
161 | # define HERE_T_FMT_AMPM "%I:%M:%S %p" | |
162 | # define HERE_T_FMT "%H:%M:%S" | |
163 | ||
164 | static const unsigned short int __mon_yday[2][13] = | |
165 | { | |
166 | /* Normal years. */ | |
167 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, | |
168 | /* Leap years. */ | |
169 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } | |
170 | }; | |
171 | #endif | |
172 | ||
173 | #if defined _LIBC | |
174 | /* We use this code also for the extended locale handling where the | |
175 | function gets as an additional argument the locale which has to be | |
176 | used. To access the values we have to redefine the _NL_CURRENT | |
177 | macro. */ | |
178 | # define strptime __strptime_l | |
179 | # undef _NL_CURRENT | |
180 | # define _NL_CURRENT(category, item) \ | |
181 | (current->values[_NL_ITEM_INDEX (item)].string) | |
182 | # undef _NL_CURRENT_WORD | |
183 | # define _NL_CURRENT_WORD(category, item) \ | |
184 | (current->values[_NL_ITEM_INDEX (item)].word) | |
185 | # define LOCALE_PARAM , locale | |
186 | # define LOCALE_ARG , locale | |
187 | # define LOCALE_PARAM_PROTO , __locale_t locale | |
188 | # define LOCALE_PARAM_DECL __locale_t locale; | |
189 | # define HELPER_LOCALE_ARG , current | |
190 | # define ISSPACE(Ch) __isspace_l (Ch, locale) | |
191 | #else | |
192 | # define LOCALE_PARAM | |
193 | # define LOCALE_ARG | |
194 | # define LOCALE_PARAM_DECL | |
195 | # define LOCALE_PARAM_PROTO | |
196 | # define HELPER_LOCALE_ARG | |
197 | # define ISSPACE(Ch) isspace (Ch) | |
198 | #endif | |
199 | ||
200 | ||
201 | ||
202 | ||
203 | #ifndef __isleap | |
204 | /* Nonzero if YEAR is a leap year (every 4 years, | |
205 | except every 100th isn't, and every 400th is). */ | |
206 | # define __isleap(year) \ | |
207 | ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) | |
208 | #endif | |
209 | ||
210 | /* Compute the day of the week. */ | |
211 | static void | |
212 | day_of_the_week (struct tm *tm) | |
213 | { | |
214 | /* We know that January 1st 1970 was a Thursday (= 4). Compute the | |
ded5b9b7 | 215 | difference between this data in the one on TM and so determine |
ccadf7b5 UD |
216 | the weekday. */ |
217 | int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2); | |
218 | int wday = (-473 | |
219 | + (365 * (tm->tm_year - 70)) | |
220 | + (corr_year / 4) | |
221 | - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0) | |
222 | + (((corr_year / 4) / 25) / 4) | |
223 | + __mon_yday[0][tm->tm_mon] | |
224 | + tm->tm_mday - 1); | |
225 | tm->tm_wday = ((wday % 7) + 7) % 7; | |
226 | } | |
227 | ||
228 | /* Compute the day of the year. */ | |
229 | static void | |
230 | day_of_the_year (struct tm *tm) | |
231 | { | |
232 | tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon] | |
233 | + (tm->tm_mday - 1)); | |
234 | } | |
235 | ||
236 | ||
237 | #ifdef _LIBC | |
238 | char * | |
239 | internal_function | |
240 | #else | |
241 | static char * | |
242 | #endif | |
fa69dc9c | 243 | __strptime_internal (rp, fmt, tmp, statep LOCALE_PARAM) |
ccadf7b5 UD |
244 | const char *rp; |
245 | const char *fmt; | |
fa69dc9c UD |
246 | struct tm *tmp; |
247 | void *statep; | |
ccadf7b5 UD |
248 | LOCALE_PARAM_DECL |
249 | { | |
250 | #ifdef _LIBC | |
f095bb72 | 251 | struct __locale_data *const current = locale->__locales[LC_TIME]; |
ccadf7b5 UD |
252 | #endif |
253 | ||
254 | const char *rp_backup; | |
f98c2d06 | 255 | const char *rp_longest; |
ccadf7b5 | 256 | int cnt; |
f98c2d06 | 257 | int cnt_longest; |
ccadf7b5 | 258 | size_t val; |
ccadf7b5 | 259 | size_t num_eras; |
fa69dc9c UD |
260 | struct era_entry *era = NULL; |
261 | enum ptime_locale_status { not, loc, raw } decided_longest; | |
262 | struct __strptime_state | |
263 | { | |
264 | unsigned int have_I : 1; | |
265 | unsigned int have_wday : 1; | |
266 | unsigned int have_yday : 1; | |
267 | unsigned int have_mon : 1; | |
268 | unsigned int have_mday : 1; | |
269 | unsigned int have_uweek : 1; | |
270 | unsigned int have_wweek : 1; | |
271 | unsigned int is_pm : 1; | |
272 | unsigned int want_century : 1; | |
273 | unsigned int want_era : 1; | |
274 | unsigned int want_xday : 1; | |
275 | enum ptime_locale_status decided : 2; | |
276 | signed char week_no; | |
277 | signed char century; | |
278 | int era_cnt; | |
279 | } s; | |
280 | struct tm tmb; | |
281 | struct tm *tm; | |
282 | ||
283 | if (statep == NULL) | |
284 | { | |
285 | memset (&s, 0, sizeof (s)); | |
286 | s.century = -1; | |
287 | s.era_cnt = -1; | |
288 | #ifdef _NL_CURRENT | |
289 | s.decided = not; | |
290 | #else | |
291 | s.decided = raw; | |
292 | #endif | |
293 | tm = tmp; | |
294 | } | |
295 | else | |
296 | { | |
297 | s = *(struct __strptime_state *) statep; | |
298 | tmb = *tmp; | |
299 | tm = &tmb; | |
300 | } | |
ccadf7b5 UD |
301 | |
302 | while (*fmt != '\0') | |
303 | { | |
304 | /* A white space in the format string matches 0 more or white | |
305 | space in the input string. */ | |
306 | if (ISSPACE (*fmt)) | |
307 | { | |
308 | while (ISSPACE (*rp)) | |
309 | ++rp; | |
310 | ++fmt; | |
311 | continue; | |
312 | } | |
313 | ||
314 | /* Any character but `%' must be matched by the same character | |
315 | in the iput string. */ | |
316 | if (*fmt != '%') | |
317 | { | |
318 | match_char (*fmt++, *rp++); | |
319 | continue; | |
320 | } | |
321 | ||
322 | ++fmt; | |
19e3372b OB |
323 | /* We discard strftime modifiers. */ |
324 | while (*fmt == '-' || *fmt == '_' || *fmt == '0' | |
325 | || *fmt == '^' || *fmt == '#') | |
326 | ++fmt; | |
327 | ||
328 | /* And field width. */ | |
329 | while (*fmt >= '0' && *fmt <= '9') | |
330 | ++fmt; | |
ae1ad762 | 331 | |
ccadf7b5 UD |
332 | #ifndef _NL_CURRENT |
333 | /* We need this for handling the `E' modifier. */ | |
334 | start_over: | |
335 | #endif | |
336 | ||
337 | /* Make back up of current processing pointer. */ | |
338 | rp_backup = rp; | |
339 | ||
340 | switch (*fmt++) | |
341 | { | |
342 | case '%': | |
343 | /* Match the `%' character itself. */ | |
344 | match_char ('%', *rp++); | |
345 | break; | |
346 | case 'a': | |
347 | case 'A': | |
348 | /* Match day of week. */ | |
f98c2d06 | 349 | rp_longest = NULL; |
fa69dc9c | 350 | decided_longest = s.decided; |
f98c2d06 | 351 | cnt_longest = -1; |
ccadf7b5 UD |
352 | for (cnt = 0; cnt < 7; ++cnt) |
353 | { | |
f98c2d06 | 354 | const char *trp; |
ccadf7b5 | 355 | #ifdef _NL_CURRENT |
fa69dc9c | 356 | if (s.decided !=raw) |
ccadf7b5 | 357 | { |
f98c2d06 UD |
358 | trp = rp; |
359 | if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), trp) | |
360 | && trp > rp_longest) | |
ccadf7b5 | 361 | { |
f98c2d06 UD |
362 | rp_longest = trp; |
363 | cnt_longest = cnt; | |
fa69dc9c | 364 | if (s.decided == not |
ccadf7b5 UD |
365 | && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt), |
366 | weekday_name[cnt])) | |
f98c2d06 | 367 | decided_longest = loc; |
ccadf7b5 | 368 | } |
f98c2d06 UD |
369 | trp = rp; |
370 | if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), trp) | |
371 | && trp > rp_longest) | |
ccadf7b5 | 372 | { |
f98c2d06 UD |
373 | rp_longest = trp; |
374 | cnt_longest = cnt; | |
fa69dc9c | 375 | if (s.decided == not |
ccadf7b5 UD |
376 | && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), |
377 | ab_weekday_name[cnt])) | |
f98c2d06 | 378 | decided_longest = loc; |
ccadf7b5 UD |
379 | } |
380 | } | |
381 | #endif | |
fa69dc9c | 382 | if (s.decided != loc |
f98c2d06 UD |
383 | && (((trp = rp, match_string (weekday_name[cnt], trp)) |
384 | && trp > rp_longest) | |
385 | || ((trp = rp, match_string (ab_weekday_name[cnt], rp)) | |
386 | && trp > rp_longest))) | |
ccadf7b5 | 387 | { |
f98c2d06 UD |
388 | rp_longest = trp; |
389 | cnt_longest = cnt; | |
390 | decided_longest = raw; | |
ccadf7b5 UD |
391 | } |
392 | } | |
f98c2d06 | 393 | if (rp_longest == NULL) |
ccadf7b5 UD |
394 | /* Does not match a weekday name. */ |
395 | return NULL; | |
f98c2d06 | 396 | rp = rp_longest; |
fa69dc9c | 397 | s.decided = decided_longest; |
f98c2d06 | 398 | tm->tm_wday = cnt_longest; |
fa69dc9c | 399 | s.have_wday = 1; |
ccadf7b5 UD |
400 | break; |
401 | case 'b': | |
402 | case 'B': | |
403 | case 'h': | |
404 | /* Match month name. */ | |
f98c2d06 | 405 | rp_longest = NULL; |
fa69dc9c | 406 | decided_longest = s.decided; |
f98c2d06 | 407 | cnt_longest = -1; |
ccadf7b5 UD |
408 | for (cnt = 0; cnt < 12; ++cnt) |
409 | { | |
f98c2d06 | 410 | const char *trp; |
ccadf7b5 | 411 | #ifdef _NL_CURRENT |
fa69dc9c | 412 | if (s.decided !=raw) |
ccadf7b5 | 413 | { |
f98c2d06 UD |
414 | trp = rp; |
415 | if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), trp) | |
416 | && trp > rp_longest) | |
ccadf7b5 | 417 | { |
f98c2d06 UD |
418 | rp_longest = trp; |
419 | cnt_longest = cnt; | |
fa69dc9c | 420 | if (s.decided == not |
ccadf7b5 UD |
421 | && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt), |
422 | month_name[cnt])) | |
f98c2d06 | 423 | decided_longest = loc; |
ccadf7b5 | 424 | } |
f98c2d06 UD |
425 | trp = rp; |
426 | if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), trp) | |
427 | && trp > rp_longest) | |
ccadf7b5 | 428 | { |
f98c2d06 UD |
429 | rp_longest = trp; |
430 | cnt_longest = cnt; | |
fa69dc9c | 431 | if (s.decided == not |
ccadf7b5 UD |
432 | && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), |
433 | ab_month_name[cnt])) | |
f98c2d06 | 434 | decided_longest = loc; |
ccadf7b5 UD |
435 | } |
436 | } | |
437 | #endif | |
fa69dc9c | 438 | if (s.decided != loc |
f98c2d06 UD |
439 | && (((trp = rp, match_string (month_name[cnt], trp)) |
440 | && trp > rp_longest) | |
441 | || ((trp = rp, match_string (ab_month_name[cnt], trp)) | |
442 | && trp > rp_longest))) | |
ccadf7b5 | 443 | { |
f98c2d06 UD |
444 | rp_longest = trp; |
445 | cnt_longest = cnt; | |
446 | decided_longest = raw; | |
ccadf7b5 UD |
447 | } |
448 | } | |
f98c2d06 | 449 | if (rp_longest == NULL) |
ccadf7b5 UD |
450 | /* Does not match a month name. */ |
451 | return NULL; | |
f98c2d06 | 452 | rp = rp_longest; |
fa69dc9c | 453 | s.decided = decided_longest; |
f98c2d06 | 454 | tm->tm_mon = cnt_longest; |
fa69dc9c UD |
455 | s.have_mon = 1; |
456 | s.want_xday = 1; | |
ccadf7b5 UD |
457 | break; |
458 | case 'c': | |
459 | /* Match locale's date and time format. */ | |
460 | #ifdef _NL_CURRENT | |
fa69dc9c | 461 | if (s.decided != raw) |
ccadf7b5 UD |
462 | { |
463 | if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT))) | |
464 | { | |
fa69dc9c | 465 | if (s.decided == loc) |
ccadf7b5 UD |
466 | return NULL; |
467 | else | |
468 | rp = rp_backup; | |
469 | } | |
470 | else | |
471 | { | |
fa69dc9c | 472 | if (s.decided == not && |
ccadf7b5 | 473 | strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT)) |
fa69dc9c UD |
474 | s.decided = loc; |
475 | s.want_xday = 1; | |
ccadf7b5 UD |
476 | break; |
477 | } | |
fa69dc9c | 478 | s.decided = raw; |
ccadf7b5 UD |
479 | } |
480 | #endif | |
481 | if (!recursive (HERE_D_T_FMT)) | |
482 | return NULL; | |
fa69dc9c | 483 | s.want_xday = 1; |
ccadf7b5 UD |
484 | break; |
485 | case 'C': | |
486 | /* Match century number. */ | |
487 | match_century: | |
488 | get_number (0, 99, 2); | |
fa69dc9c UD |
489 | s.century = val; |
490 | s.want_xday = 1; | |
ccadf7b5 UD |
491 | break; |
492 | case 'd': | |
493 | case 'e': | |
494 | /* Match day of month. */ | |
495 | get_number (1, 31, 2); | |
496 | tm->tm_mday = val; | |
fa69dc9c UD |
497 | s.have_mday = 1; |
498 | s.want_xday = 1; | |
ccadf7b5 UD |
499 | break; |
500 | case 'F': | |
501 | if (!recursive ("%Y-%m-%d")) | |
502 | return NULL; | |
fa69dc9c | 503 | s.want_xday = 1; |
ccadf7b5 UD |
504 | break; |
505 | case 'x': | |
506 | #ifdef _NL_CURRENT | |
fa69dc9c | 507 | if (s.decided != raw) |
ccadf7b5 UD |
508 | { |
509 | if (!recursive (_NL_CURRENT (LC_TIME, D_FMT))) | |
510 | { | |
fa69dc9c | 511 | if (s.decided == loc) |
ccadf7b5 UD |
512 | return NULL; |
513 | else | |
514 | rp = rp_backup; | |
515 | } | |
516 | else | |
517 | { | |
fa69dc9c | 518 | if (s.decided == not |
ccadf7b5 | 519 | && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT)) |
fa69dc9c UD |
520 | s.decided = loc; |
521 | s.want_xday = 1; | |
ccadf7b5 UD |
522 | break; |
523 | } | |
fa69dc9c | 524 | s.decided = raw; |
ccadf7b5 UD |
525 | } |
526 | #endif | |
527 | /* Fall through. */ | |
528 | case 'D': | |
529 | /* Match standard day format. */ | |
530 | if (!recursive (HERE_D_FMT)) | |
531 | return NULL; | |
fa69dc9c | 532 | s.want_xday = 1; |
ccadf7b5 UD |
533 | break; |
534 | case 'k': | |
535 | case 'H': | |
536 | /* Match hour in 24-hour clock. */ | |
537 | get_number (0, 23, 2); | |
538 | tm->tm_hour = val; | |
fa69dc9c | 539 | s.have_I = 0; |
ccadf7b5 UD |
540 | break; |
541 | case 'l': | |
542 | /* Match hour in 12-hour clock. GNU extension. */ | |
543 | case 'I': | |
544 | /* Match hour in 12-hour clock. */ | |
545 | get_number (1, 12, 2); | |
546 | tm->tm_hour = val % 12; | |
fa69dc9c | 547 | s.have_I = 1; |
ccadf7b5 UD |
548 | break; |
549 | case 'j': | |
550 | /* Match day number of year. */ | |
551 | get_number (1, 366, 3); | |
552 | tm->tm_yday = val - 1; | |
fa69dc9c | 553 | s.have_yday = 1; |
ccadf7b5 UD |
554 | break; |
555 | case 'm': | |
556 | /* Match number of month. */ | |
557 | get_number (1, 12, 2); | |
558 | tm->tm_mon = val - 1; | |
fa69dc9c UD |
559 | s.have_mon = 1; |
560 | s.want_xday = 1; | |
ccadf7b5 UD |
561 | break; |
562 | case 'M': | |
563 | /* Match minute. */ | |
564 | get_number (0, 59, 2); | |
565 | tm->tm_min = val; | |
566 | break; | |
567 | case 'n': | |
568 | case 't': | |
569 | /* Match any white space. */ | |
570 | while (ISSPACE (*rp)) | |
571 | ++rp; | |
572 | break; | |
573 | case 'p': | |
574 | /* Match locale's equivalent of AM/PM. */ | |
575 | #ifdef _NL_CURRENT | |
fa69dc9c | 576 | if (s.decided != raw) |
ccadf7b5 UD |
577 | { |
578 | if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp)) | |
579 | { | |
580 | if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR)) | |
fa69dc9c UD |
581 | s.decided = loc; |
582 | s.is_pm = 0; | |
ccadf7b5 UD |
583 | break; |
584 | } | |
585 | if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp)) | |
586 | { | |
587 | if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR)) | |
fa69dc9c UD |
588 | s.decided = loc; |
589 | s.is_pm = 1; | |
ccadf7b5 UD |
590 | break; |
591 | } | |
fa69dc9c | 592 | s.decided = raw; |
ccadf7b5 UD |
593 | } |
594 | #endif | |
595 | if (!match_string (HERE_AM_STR, rp)) | |
9fbdeb41 UD |
596 | { |
597 | if (match_string (HERE_PM_STR, rp)) | |
fa69dc9c | 598 | s.is_pm = 1; |
9fbdeb41 UD |
599 | else |
600 | return NULL; | |
9fbdeb41 | 601 | } |
fa69dc9c UD |
602 | else |
603 | s.is_pm = 0; | |
73102f50 | 604 | break; |
ccadf7b5 UD |
605 | case 'r': |
606 | #ifdef _NL_CURRENT | |
fa69dc9c | 607 | if (s.decided != raw) |
ccadf7b5 UD |
608 | { |
609 | if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM))) | |
610 | { | |
fa69dc9c | 611 | if (s.decided == loc) |
ccadf7b5 UD |
612 | return NULL; |
613 | else | |
614 | rp = rp_backup; | |
615 | } | |
616 | else | |
617 | { | |
fa69dc9c | 618 | if (s.decided == not && |
ccadf7b5 UD |
619 | strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM), |
620 | HERE_T_FMT_AMPM)) | |
fa69dc9c | 621 | s.decided = loc; |
ccadf7b5 UD |
622 | break; |
623 | } | |
fa69dc9c | 624 | s.decided = raw; |
ccadf7b5 UD |
625 | } |
626 | #endif | |
627 | if (!recursive (HERE_T_FMT_AMPM)) | |
628 | return NULL; | |
629 | break; | |
630 | case 'R': | |
631 | if (!recursive ("%H:%M")) | |
632 | return NULL; | |
633 | break; | |
634 | case 's': | |
635 | { | |
636 | /* The number of seconds may be very high so we cannot use | |
637 | the `get_number' macro. Instead read the number | |
638 | character for character and construct the result while | |
639 | doing this. */ | |
640 | time_t secs = 0; | |
641 | if (*rp < '0' || *rp > '9') | |
642 | /* We need at least one digit. */ | |
643 | return NULL; | |
644 | ||
645 | do | |
646 | { | |
647 | secs *= 10; | |
648 | secs += *rp++ - '0'; | |
649 | } | |
650 | while (*rp >= '0' && *rp <= '9'); | |
651 | ||
652 | if (localtime_r (&secs, tm) == NULL) | |
653 | /* Error in function. */ | |
654 | return NULL; | |
655 | } | |
656 | break; | |
657 | case 'S': | |
658 | get_number (0, 61, 2); | |
659 | tm->tm_sec = val; | |
660 | break; | |
661 | case 'X': | |
662 | #ifdef _NL_CURRENT | |
fa69dc9c | 663 | if (s.decided != raw) |
ccadf7b5 UD |
664 | { |
665 | if (!recursive (_NL_CURRENT (LC_TIME, T_FMT))) | |
666 | { | |
fa69dc9c | 667 | if (s.decided == loc) |
ccadf7b5 UD |
668 | return NULL; |
669 | else | |
670 | rp = rp_backup; | |
671 | } | |
672 | else | |
673 | { | |
674 | if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT)) | |
fa69dc9c | 675 | s.decided = loc; |
ccadf7b5 UD |
676 | break; |
677 | } | |
fa69dc9c | 678 | s.decided = raw; |
ccadf7b5 UD |
679 | } |
680 | #endif | |
681 | /* Fall through. */ | |
682 | case 'T': | |
683 | if (!recursive (HERE_T_FMT)) | |
684 | return NULL; | |
685 | break; | |
686 | case 'u': | |
687 | get_number (1, 7, 1); | |
688 | tm->tm_wday = val % 7; | |
fa69dc9c | 689 | s.have_wday = 1; |
ccadf7b5 UD |
690 | break; |
691 | case 'g': | |
692 | get_number (0, 99, 2); | |
693 | /* XXX This cannot determine any field in TM. */ | |
694 | break; | |
695 | case 'G': | |
696 | if (*rp < '0' || *rp > '9') | |
697 | return NULL; | |
698 | /* XXX Ignore the number since we would need some more | |
699 | information to compute a real date. */ | |
700 | do | |
701 | ++rp; | |
702 | while (*rp >= '0' && *rp <= '9'); | |
703 | break; | |
704 | case 'U': | |
705 | get_number (0, 53, 2); | |
fa69dc9c UD |
706 | s.week_no = val; |
707 | s.have_uweek = 1; | |
ccadf7b5 UD |
708 | break; |
709 | case 'W': | |
710 | get_number (0, 53, 2); | |
fa69dc9c UD |
711 | s.week_no = val; |
712 | s.have_wweek = 1; | |
ccadf7b5 UD |
713 | break; |
714 | case 'V': | |
715 | get_number (0, 53, 2); | |
716 | /* XXX This cannot determine any field in TM without some | |
717 | information. */ | |
718 | break; | |
719 | case 'w': | |
720 | /* Match number of weekday. */ | |
721 | get_number (0, 6, 1); | |
722 | tm->tm_wday = val; | |
fa69dc9c | 723 | s.have_wday = 1; |
ccadf7b5 UD |
724 | break; |
725 | case 'y': | |
726 | match_year_in_century: | |
727 | /* Match year within century. */ | |
728 | get_number (0, 99, 2); | |
729 | /* The "Year 2000: The Millennium Rollover" paper suggests that | |
730 | values in the range 69-99 refer to the twentieth century. */ | |
731 | tm->tm_year = val >= 69 ? val : val + 100; | |
732 | /* Indicate that we want to use the century, if specified. */ | |
fa69dc9c UD |
733 | s.want_century = 1; |
734 | s.want_xday = 1; | |
ccadf7b5 UD |
735 | break; |
736 | case 'Y': | |
737 | /* Match year including century number. */ | |
738 | get_number (0, 9999, 4); | |
739 | tm->tm_year = val - 1900; | |
fa69dc9c UD |
740 | s.want_century = 0; |
741 | s.want_xday = 1; | |
ccadf7b5 UD |
742 | break; |
743 | case 'Z': | |
ddc7e412 OB |
744 | /* Read timezone but perform no conversion. */ |
745 | while (ISSPACE (*rp)) | |
746 | rp++; | |
747 | while (!ISSPACE (*rp) && *rp != '\0') | |
748 | rp++; | |
ccadf7b5 | 749 | break; |
935f3e67 UD |
750 | case 'z': |
751 | /* We recognize two formats: if two digits are given, these | |
752 | specify hours. If fours digits are used, minutes are | |
753 | also specified. */ | |
754 | { | |
755 | val = 0; | |
ff491d14 | 756 | while (ISSPACE (*rp)) |
935f3e67 UD |
757 | ++rp; |
758 | if (*rp != '+' && *rp != '-') | |
759 | return NULL; | |
760 | bool neg = *rp++ == '-'; | |
761 | int n = 0; | |
762 | while (n < 4 && *rp >= '0' && *rp <= '9') | |
763 | { | |
764 | val = val * 10 + *rp++ - '0'; | |
765 | ++n; | |
766 | } | |
767 | if (n == 2) | |
768 | val *= 100; | |
769 | else if (n != 4) | |
770 | /* Only two or four digits recognized. */ | |
771 | return NULL; | |
772 | else | |
773 | { | |
774 | /* We have to convert the minutes into decimal. */ | |
775 | if (val % 100 >= 60) | |
776 | return NULL; | |
777 | val = (val / 100) * 100 + ((val % 100) * 50) / 30; | |
778 | } | |
779 | if (val > 1200) | |
780 | return NULL; | |
781 | tm->tm_gmtoff = (val * 3600) / 100; | |
782 | if (neg) | |
783 | tm->tm_gmtoff = -tm->tm_gmtoff; | |
784 | } | |
785 | break; | |
ccadf7b5 UD |
786 | case 'E': |
787 | #ifdef _NL_CURRENT | |
788 | switch (*fmt++) | |
789 | { | |
790 | case 'c': | |
791 | /* Match locale's alternate date and time format. */ | |
fa69dc9c | 792 | if (s.decided != raw) |
ccadf7b5 UD |
793 | { |
794 | const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT); | |
795 | ||
796 | if (*fmt == '\0') | |
797 | fmt = _NL_CURRENT (LC_TIME, D_T_FMT); | |
798 | ||
799 | if (!recursive (fmt)) | |
800 | { | |
fa69dc9c | 801 | if (s.decided == loc) |
ccadf7b5 UD |
802 | return NULL; |
803 | else | |
804 | rp = rp_backup; | |
805 | } | |
806 | else | |
807 | { | |
808 | if (strcmp (fmt, HERE_D_T_FMT)) | |
fa69dc9c UD |
809 | s.decided = loc; |
810 | s.want_xday = 1; | |
ccadf7b5 UD |
811 | break; |
812 | } | |
fa69dc9c | 813 | s.decided = raw; |
ccadf7b5 UD |
814 | } |
815 | /* The C locale has no era information, so use the | |
816 | normal representation. */ | |
817 | if (!recursive (HERE_D_T_FMT)) | |
818 | return NULL; | |
fa69dc9c | 819 | s.want_xday = 1; |
ccadf7b5 UD |
820 | break; |
821 | case 'C': | |
fa69dc9c | 822 | if (s.decided != raw) |
ccadf7b5 | 823 | { |
fa69dc9c | 824 | if (s.era_cnt >= 0) |
ccadf7b5 | 825 | { |
fa69dc9c | 826 | era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG); |
ccadf7b5 UD |
827 | if (era != NULL && match_string (era->era_name, rp)) |
828 | { | |
fa69dc9c | 829 | s.decided = loc; |
ccadf7b5 UD |
830 | break; |
831 | } | |
832 | else | |
833 | return NULL; | |
834 | } | |
1750bc8d UD |
835 | |
836 | num_eras = _NL_CURRENT_WORD (LC_TIME, | |
837 | _NL_TIME_ERA_NUM_ENTRIES); | |
fa69dc9c UD |
838 | for (s.era_cnt = 0; s.era_cnt < (int) num_eras; |
839 | ++s.era_cnt, rp = rp_backup) | |
ccadf7b5 | 840 | { |
fa69dc9c | 841 | era = _nl_select_era_entry (s.era_cnt |
1750bc8d UD |
842 | HELPER_LOCALE_ARG); |
843 | if (era != NULL && match_string (era->era_name, rp)) | |
ccadf7b5 | 844 | { |
fa69dc9c | 845 | s.decided = loc; |
1750bc8d | 846 | break; |
ccadf7b5 | 847 | } |
ccadf7b5 | 848 | } |
fa69dc9c | 849 | if (s.era_cnt != (int) num_eras) |
1750bc8d UD |
850 | break; |
851 | ||
fa69dc9c UD |
852 | s.era_cnt = -1; |
853 | if (s.decided == loc) | |
1750bc8d | 854 | return NULL; |
ccadf7b5 | 855 | |
fa69dc9c | 856 | s.decided = raw; |
ccadf7b5 UD |
857 | } |
858 | /* The C locale has no era information, so use the | |
859 | normal representation. */ | |
860 | goto match_century; | |
861 | case 'y': | |
fa69dc9c | 862 | if (s.decided != raw) |
1750bc8d UD |
863 | { |
864 | get_number(0, 9999, 4); | |
865 | tm->tm_year = val; | |
fa69dc9c UD |
866 | s.want_era = 1; |
867 | s.want_xday = 1; | |
868 | s.want_century = 1; | |
ccadf7b5 | 869 | |
fa69dc9c | 870 | if (s.era_cnt >= 0) |
1750bc8d | 871 | { |
fa69dc9c | 872 | assert (s.decided == loc); |
1750bc8d | 873 | |
fa69dc9c | 874 | era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG); |
1750bc8d UD |
875 | bool match = false; |
876 | if (era != NULL) | |
877 | { | |
878 | int delta = ((tm->tm_year - era->offset) | |
879 | * era->absolute_direction); | |
880 | match = (delta >= 0 | |
881 | && delta < (((int64_t) era->stop_date[0] | |
882 | - (int64_t) era->start_date[0]) | |
883 | * era->absolute_direction)); | |
884 | } | |
885 | if (! match) | |
886 | return NULL; | |
887 | ||
888 | break; | |
889 | } | |
890 | ||
891 | num_eras = _NL_CURRENT_WORD (LC_TIME, | |
892 | _NL_TIME_ERA_NUM_ENTRIES); | |
fa69dc9c | 893 | for (s.era_cnt = 0; s.era_cnt < (int) num_eras; ++s.era_cnt) |
1750bc8d | 894 | { |
fa69dc9c | 895 | era = _nl_select_era_entry (s.era_cnt |
1750bc8d UD |
896 | HELPER_LOCALE_ARG); |
897 | if (era != NULL) | |
898 | { | |
899 | int delta = ((tm->tm_year - era->offset) | |
900 | * era->absolute_direction); | |
901 | if (delta >= 0 | |
902 | && delta < (((int64_t) era->stop_date[0] | |
903 | - (int64_t) era->start_date[0]) | |
904 | * era->absolute_direction)) | |
905 | { | |
fa69dc9c | 906 | s.decided = loc; |
1750bc8d UD |
907 | break; |
908 | } | |
909 | } | |
910 | } | |
fa69dc9c | 911 | if (s.era_cnt != (int) num_eras) |
1750bc8d UD |
912 | break; |
913 | ||
fa69dc9c UD |
914 | s.era_cnt = -1; |
915 | if (s.decided == loc) | |
1750bc8d UD |
916 | return NULL; |
917 | ||
fa69dc9c | 918 | s.decided = raw; |
1750bc8d UD |
919 | } |
920 | ||
921 | goto match_year_in_century; | |
ccadf7b5 | 922 | case 'Y': |
fa69dc9c | 923 | if (s.decided != raw) |
ccadf7b5 UD |
924 | { |
925 | num_eras = _NL_CURRENT_WORD (LC_TIME, | |
926 | _NL_TIME_ERA_NUM_ENTRIES); | |
fa69dc9c UD |
927 | for (s.era_cnt = 0; s.era_cnt < (int) num_eras; |
928 | ++s.era_cnt, rp = rp_backup) | |
ccadf7b5 | 929 | { |
fa69dc9c | 930 | era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG); |
ccadf7b5 UD |
931 | if (era != NULL && recursive (era->era_format)) |
932 | break; | |
933 | } | |
fa69dc9c | 934 | if (s.era_cnt == (int) num_eras) |
ccadf7b5 | 935 | { |
fa69dc9c UD |
936 | s.era_cnt = -1; |
937 | if (s.decided == loc) | |
ccadf7b5 UD |
938 | return NULL; |
939 | else | |
940 | rp = rp_backup; | |
941 | } | |
942 | else | |
943 | { | |
fa69dc9c | 944 | s.decided = loc; |
ccadf7b5 UD |
945 | break; |
946 | } | |
947 | ||
fa69dc9c | 948 | s.decided = raw; |
ccadf7b5 UD |
949 | } |
950 | get_number (0, 9999, 4); | |
951 | tm->tm_year = val - 1900; | |
fa69dc9c UD |
952 | s.want_century = 0; |
953 | s.want_xday = 1; | |
ccadf7b5 UD |
954 | break; |
955 | case 'x': | |
fa69dc9c | 956 | if (s.decided != raw) |
ccadf7b5 UD |
957 | { |
958 | const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT); | |
959 | ||
960 | if (*fmt == '\0') | |
961 | fmt = _NL_CURRENT (LC_TIME, D_FMT); | |
962 | ||
963 | if (!recursive (fmt)) | |
964 | { | |
fa69dc9c | 965 | if (s.decided == loc) |
ccadf7b5 UD |
966 | return NULL; |
967 | else | |
968 | rp = rp_backup; | |
969 | } | |
970 | else | |
971 | { | |
972 | if (strcmp (fmt, HERE_D_FMT)) | |
fa69dc9c | 973 | s.decided = loc; |
ccadf7b5 UD |
974 | break; |
975 | } | |
fa69dc9c | 976 | s.decided = raw; |
ccadf7b5 UD |
977 | } |
978 | if (!recursive (HERE_D_FMT)) | |
979 | return NULL; | |
980 | break; | |
981 | case 'X': | |
fa69dc9c | 982 | if (s.decided != raw) |
ccadf7b5 UD |
983 | { |
984 | const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT); | |
985 | ||
986 | if (*fmt == '\0') | |
987 | fmt = _NL_CURRENT (LC_TIME, T_FMT); | |
988 | ||
989 | if (!recursive (fmt)) | |
990 | { | |
fa69dc9c | 991 | if (s.decided == loc) |
ccadf7b5 UD |
992 | return NULL; |
993 | else | |
994 | rp = rp_backup; | |
995 | } | |
996 | else | |
997 | { | |
998 | if (strcmp (fmt, HERE_T_FMT)) | |
fa69dc9c | 999 | s.decided = loc; |
ccadf7b5 UD |
1000 | break; |
1001 | } | |
fa69dc9c | 1002 | s.decided = raw; |
ccadf7b5 UD |
1003 | } |
1004 | if (!recursive (HERE_T_FMT)) | |
1005 | return NULL; | |
1006 | break; | |
1007 | default: | |
1008 | return NULL; | |
1009 | } | |
1010 | break; | |
1011 | #else | |
1012 | /* We have no information about the era format. Just use | |
1013 | the normal format. */ | |
1014 | if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y' | |
1015 | && *fmt != 'x' && *fmt != 'X') | |
1016 | /* This is an illegal format. */ | |
1017 | return NULL; | |
1018 | ||
1019 | goto start_over; | |
1020 | #endif | |
1021 | case 'O': | |
1022 | switch (*fmt++) | |
1023 | { | |
1024 | case 'd': | |
1025 | case 'e': | |
1026 | /* Match day of month using alternate numeric symbols. */ | |
1027 | get_alt_number (1, 31, 2); | |
1028 | tm->tm_mday = val; | |
fa69dc9c UD |
1029 | s.have_mday = 1; |
1030 | s.want_xday = 1; | |
ccadf7b5 UD |
1031 | break; |
1032 | case 'H': | |
1033 | /* Match hour in 24-hour clock using alternate numeric | |
1034 | symbols. */ | |
1035 | get_alt_number (0, 23, 2); | |
1036 | tm->tm_hour = val; | |
fa69dc9c | 1037 | s.have_I = 0; |
ccadf7b5 UD |
1038 | break; |
1039 | case 'I': | |
1040 | /* Match hour in 12-hour clock using alternate numeric | |
1041 | symbols. */ | |
1042 | get_alt_number (1, 12, 2); | |
1043 | tm->tm_hour = val % 12; | |
fa69dc9c | 1044 | s.have_I = 1; |
ccadf7b5 UD |
1045 | break; |
1046 | case 'm': | |
1047 | /* Match month using alternate numeric symbols. */ | |
1048 | get_alt_number (1, 12, 2); | |
1049 | tm->tm_mon = val - 1; | |
fa69dc9c UD |
1050 | s.have_mon = 1; |
1051 | s.want_xday = 1; | |
ccadf7b5 UD |
1052 | break; |
1053 | case 'M': | |
1054 | /* Match minutes using alternate numeric symbols. */ | |
1055 | get_alt_number (0, 59, 2); | |
1056 | tm->tm_min = val; | |
1057 | break; | |
1058 | case 'S': | |
1059 | /* Match seconds using alternate numeric symbols. */ | |
1060 | get_alt_number (0, 61, 2); | |
1061 | tm->tm_sec = val; | |
1062 | break; | |
1063 | case 'U': | |
1064 | get_alt_number (0, 53, 2); | |
fa69dc9c UD |
1065 | s.week_no = val; |
1066 | s.have_uweek = 1; | |
ccadf7b5 UD |
1067 | break; |
1068 | case 'W': | |
1069 | get_alt_number (0, 53, 2); | |
fa69dc9c UD |
1070 | s.week_no = val; |
1071 | s.have_wweek = 1; | |
ccadf7b5 UD |
1072 | break; |
1073 | case 'V': | |
1074 | get_alt_number (0, 53, 2); | |
1075 | /* XXX This cannot determine any field in TM without | |
1076 | further information. */ | |
1077 | break; | |
1078 | case 'w': | |
1079 | /* Match number of weekday using alternate numeric symbols. */ | |
1080 | get_alt_number (0, 6, 1); | |
1081 | tm->tm_wday = val; | |
fa69dc9c | 1082 | s.have_wday = 1; |
ccadf7b5 UD |
1083 | break; |
1084 | case 'y': | |
1085 | /* Match year within century using alternate numeric symbols. */ | |
1086 | get_alt_number (0, 99, 2); | |
1087 | tm->tm_year = val >= 69 ? val : val + 100; | |
fa69dc9c | 1088 | s.want_xday = 1; |
ccadf7b5 UD |
1089 | break; |
1090 | default: | |
1091 | return NULL; | |
1092 | } | |
1093 | break; | |
1094 | default: | |
1095 | return NULL; | |
1096 | } | |
1097 | } | |
1098 | ||
fa69dc9c UD |
1099 | if (statep != NULL) |
1100 | { | |
1101 | /* Recursive invocation, returning success, so | |
1102 | update parent's struct tm and state. */ | |
1103 | *(struct __strptime_state *) statep = s; | |
1104 | *tmp = tmb; | |
1105 | return (char *) rp; | |
1106 | } | |
1107 | ||
1108 | if (s.have_I && s.is_pm) | |
ccadf7b5 UD |
1109 | tm->tm_hour += 12; |
1110 | ||
fa69dc9c | 1111 | if (s.century != -1) |
ccadf7b5 | 1112 | { |
fa69dc9c UD |
1113 | if (s.want_century) |
1114 | tm->tm_year = tm->tm_year % 100 + (s.century - 19) * 100; | |
ccadf7b5 UD |
1115 | else |
1116 | /* Only the century, but not the year. Strange, but so be it. */ | |
fa69dc9c | 1117 | tm->tm_year = (s.century - 19) * 100; |
ccadf7b5 UD |
1118 | } |
1119 | ||
fa69dc9c | 1120 | if (s.era_cnt != -1) |
ccadf7b5 | 1121 | { |
fa69dc9c | 1122 | era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG); |
ccadf7b5 UD |
1123 | if (era == NULL) |
1124 | return NULL; | |
fa69dc9c | 1125 | if (s.want_era) |
ccadf7b5 UD |
1126 | tm->tm_year = (era->start_date[0] |
1127 | + ((tm->tm_year - era->offset) | |
1128 | * era->absolute_direction)); | |
1129 | else | |
1130 | /* Era start year assumed. */ | |
1131 | tm->tm_year = era->start_date[0]; | |
1132 | } | |
1133 | else | |
fa69dc9c | 1134 | if (s.want_era) |
ccadf7b5 UD |
1135 | { |
1136 | /* No era found but we have seen an E modifier. Rectify some | |
1137 | values. */ | |
fa69dc9c | 1138 | if (s.want_century && s.century == -1 && tm->tm_year < 69) |
ccadf7b5 UD |
1139 | tm->tm_year += 100; |
1140 | } | |
1141 | ||
fa69dc9c | 1142 | if (s.want_xday && !s.have_wday) |
ccadf7b5 | 1143 | { |
fa69dc9c | 1144 | if ( !(s.have_mon && s.have_mday) && s.have_yday) |
ccadf7b5 UD |
1145 | { |
1146 | /* We don't have tm_mon and/or tm_mday, compute them. */ | |
1147 | int t_mon = 0; | |
1148 | while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday) | |
1149 | t_mon++; | |
fa69dc9c | 1150 | if (!s.have_mon) |
ccadf7b5 | 1151 | tm->tm_mon = t_mon - 1; |
fa69dc9c | 1152 | if (!s.have_mday) |
ccadf7b5 UD |
1153 | tm->tm_mday = |
1154 | (tm->tm_yday | |
1155 | - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1); | |
fa69dc9c UD |
1156 | s.have_mon = 1; |
1157 | s.have_mday = 1; | |
ccadf7b5 | 1158 | } |
00458b5b | 1159 | /* Don't crash in day_of_the_week if tm_mon is uninitialized. */ |
fa69dc9c | 1160 | if (s.have_mon || (unsigned) tm->tm_mon <= 11) |
00458b5b | 1161 | day_of_the_week (tm); |
ccadf7b5 UD |
1162 | } |
1163 | ||
fa69dc9c | 1164 | if (s.want_xday && !s.have_yday && (s.have_mon || (unsigned) tm->tm_mon <= 11)) |
ccadf7b5 UD |
1165 | day_of_the_year (tm); |
1166 | ||
fa69dc9c | 1167 | if ((s.have_uweek || s.have_wweek) && s.have_wday) |
ccadf7b5 UD |
1168 | { |
1169 | int save_wday = tm->tm_wday; | |
1170 | int save_mday = tm->tm_mday; | |
1171 | int save_mon = tm->tm_mon; | |
fa69dc9c | 1172 | int w_offset = s.have_uweek ? 0 : 1; |
ccadf7b5 UD |
1173 | |
1174 | tm->tm_mday = 1; | |
1175 | tm->tm_mon = 0; | |
1176 | day_of_the_week (tm); | |
fa69dc9c | 1177 | if (s.have_mday) |
ccadf7b5 | 1178 | tm->tm_mday = save_mday; |
fa69dc9c | 1179 | if (s.have_mon) |
ccadf7b5 UD |
1180 | tm->tm_mon = save_mon; |
1181 | ||
fa69dc9c | 1182 | if (!s.have_yday) |
ccadf7b5 | 1183 | tm->tm_yday = ((7 - (tm->tm_wday - w_offset)) % 7 |
840e2943 AS |
1184 | + (s.week_no - 1) * 7 |
1185 | + (save_wday - w_offset + 7) % 7); | |
ccadf7b5 | 1186 | |
fa69dc9c | 1187 | if (!s.have_mday || !s.have_mon) |
ccadf7b5 UD |
1188 | { |
1189 | int t_mon = 0; | |
1190 | while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] | |
1191 | <= tm->tm_yday) | |
1192 | t_mon++; | |
fa69dc9c | 1193 | if (!s.have_mon) |
ccadf7b5 | 1194 | tm->tm_mon = t_mon - 1; |
fa69dc9c | 1195 | if (!s.have_mday) |
ccadf7b5 UD |
1196 | tm->tm_mday = |
1197 | (tm->tm_yday | |
1198 | - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1); | |
1199 | } | |
1200 | ||
1201 | tm->tm_wday = save_wday; | |
1202 | } | |
1203 | ||
1204 | return (char *) rp; | |
1205 | } | |
1206 | ||
1207 | ||
1208 | char * | |
1209 | strptime (buf, format, tm LOCALE_PARAM) | |
1210 | const char *buf; | |
1211 | const char *format; | |
1212 | struct tm *tm; | |
1213 | LOCALE_PARAM_DECL | |
1214 | { | |
fa69dc9c | 1215 | return __strptime_internal (buf, format, tm, NULL LOCALE_ARG); |
ccadf7b5 UD |
1216 | } |
1217 | ||
1218 | #ifdef _LIBC | |
60f20c19 | 1219 | weak_alias (__strptime_l, strptime_l) |
ccadf7b5 | 1220 | #endif |