1 /* Copyright (C) 2002, 2004, 2005, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
32 # include "../locale/localeinfo.h"
37 # if defined __GNUC__ || (defined __STDC__ && __STDC__)
38 # define __P(args) args
45 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
47 # define localtime_r __localtime_r
49 /* Approximate localtime_r as best we can in its absence. */
50 # define localtime_r my_localtime_r
51 static struct tm *localtime_r __P ((const time_t *, struct tm *));
57 struct tm *l = localtime (t);
64 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
67 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
68 #if defined __GNUC__ && __GNUC__ >= 2
69 # define match_string(cs1, s2) \
70 ({ size_t len = strlen (cs1); \
71 int result = __strncasecmp_l ((cs1), (s2), len, locale) == 0; \
72 if (result) (s2) += len; \
75 /* Oh come on. Get a reasonable compiler. */
76 # define match_string(cs1, s2) \
77 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
79 /* We intentionally do not use isdigit() for testing because this will
80 lead to problems with the wide character version. */
81 #define get_number(from, to, n) \
87 if (*rp < '0' || *rp > '9') \
92 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
93 if (val < from || val > to) \
97 # define get_alt_number(from, to, n) \
99 __label__ do_normal; \
101 if (s.decided != raw) \
103 val = _nl_parse_alt_digit (&rp HELPER_LOCALE_ARG); \
104 if (val == -1 && s.decided != loc) \
109 if (val < from || val > to) \
115 get_number (from, to, n); \
120 # define get_alt_number(from, to, n) \
121 /* We don't have the alternate representation. */ \
122 get_number(from, to, n)
124 #define recursive(new_fmt) \
125 (*(new_fmt) != '\0' \
126 && (rp = __strptime_internal (rp, (new_fmt), tm, &s LOCALE_ARG)) != NULL)
130 /* This is defined in locale/C-time.c in the GNU libc. */
131 extern const struct locale_data _nl_C_LC_TIME attribute_hidden;
133 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
134 # define ab_weekday_name \
135 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
136 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
137 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
138 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
139 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
140 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
141 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
142 # define HERE_T_FMT_AMPM \
143 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
144 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
146 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
148 static char const weekday_name[][10] =
150 "Sunday", "Monday", "Tuesday", "Wednesday",
151 "Thursday", "Friday", "Saturday"
153 static char const ab_weekday_name[][4] =
155 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
157 static char const month_name[][10] =
159 "January", "February", "March", "April", "May", "June",
160 "July", "August", "September", "October", "November", "December"
162 static char const ab_month_name[][4] =
164 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
165 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
167 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
168 # define HERE_D_FMT "%m/%d/%y"
169 # define HERE_AM_STR "AM"
170 # define HERE_PM_STR "PM"
171 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
172 # define HERE_T_FMT "%H:%M:%S"
174 static const unsigned short int __mon_yday[2][13] =
177 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
179 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
184 /* We use this code also for the extended locale handling where the
185 function gets as an additional argument the locale which has to be
186 used. To access the values we have to redefine the _NL_CURRENT
188 # define strptime __strptime_l
190 # define _NL_CURRENT(category, item) \
191 (current->values[_NL_ITEM_INDEX (item)].string)
192 # undef _NL_CURRENT_WORD
193 # define _NL_CURRENT_WORD(category, item) \
194 (current->values[_NL_ITEM_INDEX (item)].word)
195 # define LOCALE_PARAM , locale
196 # define LOCALE_ARG , locale
197 # define LOCALE_PARAM_PROTO , __locale_t locale
198 # define LOCALE_PARAM_DECL __locale_t locale;
199 # define HELPER_LOCALE_ARG , current
200 # define ISSPACE(Ch) __isspace_l (Ch, locale)
202 # define LOCALE_PARAM
204 # define LOCALE_PARAM_DECL
205 # define LOCALE_PARAM_PROTO
206 # define HELPER_LOCALE_ARG
207 # define ISSPACE(Ch) isspace (Ch)
214 /* Nonzero if YEAR is a leap year (every 4 years,
215 except every 100th isn't, and every 400th is). */
216 # define __isleap(year) \
217 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
220 /* Compute the day of the week. */
222 day_of_the_week (struct tm *tm)
224 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
225 the difference between this data in the one on TM and so determine
227 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
229 + (365 * (tm->tm_year - 70))
231 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
232 + (((corr_year / 4) / 25) / 4)
233 + __mon_yday[0][tm->tm_mon]
235 tm->tm_wday = ((wday % 7) + 7) % 7;
238 /* Compute the day of the year. */
240 day_of_the_year (struct tm *tm)
242 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
243 + (tm->tm_mday - 1));
253 __strptime_internal (rp, fmt, tmp, statep LOCALE_PARAM)
261 struct locale_data *const current = locale->__locales[LC_TIME];
264 const char *rp_backup;
265 const char *rp_longest;
270 struct era_entry *era = NULL;
271 enum ptime_locale_status { not, loc, raw } decided_longest;
272 struct __strptime_state
274 unsigned int have_I : 1;
275 unsigned int have_wday : 1;
276 unsigned int have_yday : 1;
277 unsigned int have_mon : 1;
278 unsigned int have_mday : 1;
279 unsigned int have_uweek : 1;
280 unsigned int have_wweek : 1;
281 unsigned int is_pm : 1;
282 unsigned int want_century : 1;
283 unsigned int want_era : 1;
284 unsigned int want_xday : 1;
285 enum ptime_locale_status decided : 2;
295 memset (&s, 0, sizeof (s));
307 s = *(struct __strptime_state *) statep;
314 /* A white space in the format string matches 0 more or white
315 space in the input string. */
318 while (ISSPACE (*rp))
324 /* Any character but `%' must be matched by the same character
325 in the iput string. */
328 match_char (*fmt++, *rp++);
334 /* We need this for handling the `E' modifier. */
338 /* Make back up of current processing pointer. */
344 /* Match the `%' character itself. */
345 match_char ('%', *rp++);
349 /* Match day of week. */
351 decided_longest = s.decided;
353 for (cnt = 0; cnt < 7; ++cnt)
360 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), trp)
366 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
368 decided_longest = loc;
371 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), trp)
377 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
378 ab_weekday_name[cnt]))
379 decided_longest = loc;
384 && (((trp = rp, match_string (weekday_name[cnt], trp))
386 || ((trp = rp, match_string (ab_weekday_name[cnt], rp))
387 && trp > rp_longest)))
391 decided_longest = raw;
394 if (rp_longest == NULL)
395 /* Does not match a weekday name. */
398 s.decided = decided_longest;
399 tm->tm_wday = cnt_longest;
405 /* Match month name. */
407 decided_longest = s.decided;
409 for (cnt = 0; cnt < 12; ++cnt)
416 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), trp)
422 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
424 decided_longest = loc;
427 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), trp)
433 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
435 decided_longest = loc;
440 && (((trp = rp, match_string (month_name[cnt], trp))
442 || ((trp = rp, match_string (ab_month_name[cnt], trp))
443 && trp > rp_longest)))
447 decided_longest = raw;
450 if (rp_longest == NULL)
451 /* Does not match a month name. */
454 s.decided = decided_longest;
455 tm->tm_mon = cnt_longest;
460 /* Match locale's date and time format. */
462 if (s.decided != raw)
464 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
466 if (s.decided == loc)
473 if (s.decided == not &&
474 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
482 if (!recursive (HERE_D_T_FMT))
487 /* Match century number. */
489 get_number (0, 99, 2);
495 /* Match day of month. */
496 get_number (1, 31, 2);
502 if (!recursive ("%Y-%m-%d"))
508 if (s.decided != raw)
510 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
512 if (s.decided == loc)
520 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
530 /* Match standard day format. */
531 if (!recursive (HERE_D_FMT))
537 /* Match hour in 24-hour clock. */
538 get_number (0, 23, 2);
543 /* Match hour in 12-hour clock. GNU extension. */
545 /* Match hour in 12-hour clock. */
546 get_number (1, 12, 2);
547 tm->tm_hour = val % 12;
551 /* Match day number of year. */
552 get_number (1, 366, 3);
553 tm->tm_yday = val - 1;
557 /* Match number of month. */
558 get_number (1, 12, 2);
559 tm->tm_mon = val - 1;
565 get_number (0, 59, 2);
570 /* Match any white space. */
571 while (ISSPACE (*rp))
575 /* Match locale's equivalent of AM/PM. */
577 if (s.decided != raw)
579 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
581 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
586 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
588 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
596 if (!match_string (HERE_AM_STR, rp))
598 if (match_string (HERE_PM_STR, rp))
608 if (s.decided != raw)
610 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
612 if (s.decided == loc)
619 if (s.decided == not &&
620 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
628 if (!recursive (HERE_T_FMT_AMPM))
632 if (!recursive ("%H:%M"))
637 /* The number of seconds may be very high so we cannot use
638 the `get_number' macro. Instead read the number
639 character for character and construct the result while
642 if (*rp < '0' || *rp > '9')
643 /* We need at least one digit. */
651 while (*rp >= '0' && *rp <= '9');
653 if (localtime_r (&secs, tm) == NULL)
654 /* Error in function. */
659 get_number (0, 61, 2);
664 if (s.decided != raw)
666 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
668 if (s.decided == loc)
675 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
684 if (!recursive (HERE_T_FMT))
688 get_number (1, 7, 1);
689 tm->tm_wday = val % 7;
693 get_number (0, 99, 2);
694 /* XXX This cannot determine any field in TM. */
697 if (*rp < '0' || *rp > '9')
699 /* XXX Ignore the number since we would need some more
700 information to compute a real date. */
703 while (*rp >= '0' && *rp <= '9');
706 get_number (0, 53, 2);
711 get_number (0, 53, 2);
716 get_number (0, 53, 2);
717 /* XXX This cannot determine any field in TM without some
721 /* Match number of weekday. */
722 get_number (0, 6, 1);
727 match_year_in_century:
728 /* Match year within century. */
729 get_number (0, 99, 2);
730 /* The "Year 2000: The Millennium Rollover" paper suggests that
731 values in the range 69-99 refer to the twentieth century. */
732 tm->tm_year = val >= 69 ? val : val + 100;
733 /* Indicate that we want to use the century, if specified. */
738 /* Match year including century number. */
739 get_number (0, 9999, 4);
740 tm->tm_year = val - 1900;
745 /* XXX How to handle this? */
748 /* We recognize two formats: if two digits are given, these
749 specify hours. If fours digits are used, minutes are
755 if (*rp != '+' && *rp != '-')
757 bool neg = *rp++ == '-';
759 while (n < 4 && *rp >= '0' && *rp <= '9')
761 val = val * 10 + *rp++ - '0';
767 /* Only two or four digits recognized. */
771 /* We have to convert the minutes into decimal. */
774 val = (val / 100) * 100 + ((val % 100) * 50) / 30;
778 tm->tm_gmtoff = (val * 3600) / 100;
780 tm->tm_gmtoff = -tm->tm_gmtoff;
788 /* Match locale's alternate date and time format. */
789 if (s.decided != raw)
791 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
794 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
796 if (!recursive (fmt))
798 if (s.decided == loc)
805 if (strcmp (fmt, HERE_D_T_FMT))
812 /* The C locale has no era information, so use the
813 normal representation. */
814 if (!recursive (HERE_D_T_FMT))
819 if (s.decided != raw)
823 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
824 if (era != NULL && match_string (era->era_name, rp))
833 num_eras = _NL_CURRENT_WORD (LC_TIME,
834 _NL_TIME_ERA_NUM_ENTRIES);
835 for (s.era_cnt = 0; s.era_cnt < (int) num_eras;
836 ++s.era_cnt, rp = rp_backup)
838 era = _nl_select_era_entry (s.era_cnt
840 if (era != NULL && match_string (era->era_name, rp))
846 if (s.era_cnt != (int) num_eras)
850 if (s.decided == loc)
855 /* The C locale has no era information, so use the
856 normal representation. */
859 if (s.decided != raw)
861 get_number(0, 9999, 4);
869 assert (s.decided == loc);
871 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
875 int delta = ((tm->tm_year - era->offset)
876 * era->absolute_direction);
878 && delta < (((int64_t) era->stop_date[0]
879 - (int64_t) era->start_date[0])
880 * era->absolute_direction));
888 num_eras = _NL_CURRENT_WORD (LC_TIME,
889 _NL_TIME_ERA_NUM_ENTRIES);
890 for (s.era_cnt = 0; s.era_cnt < (int) num_eras; ++s.era_cnt)
892 era = _nl_select_era_entry (s.era_cnt
896 int delta = ((tm->tm_year - era->offset)
897 * era->absolute_direction);
899 && delta < (((int64_t) era->stop_date[0]
900 - (int64_t) era->start_date[0])
901 * era->absolute_direction))
908 if (s.era_cnt != (int) num_eras)
912 if (s.decided == loc)
918 goto match_year_in_century;
920 if (s.decided != raw)
922 num_eras = _NL_CURRENT_WORD (LC_TIME,
923 _NL_TIME_ERA_NUM_ENTRIES);
924 for (s.era_cnt = 0; s.era_cnt < (int) num_eras;
925 ++s.era_cnt, rp = rp_backup)
927 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
928 if (era != NULL && recursive (era->era_format))
931 if (s.era_cnt == (int) num_eras)
934 if (s.decided == loc)
948 get_number (0, 9999, 4);
949 tm->tm_year = val - 1900;
954 if (s.decided != raw)
956 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
959 fmt = _NL_CURRENT (LC_TIME, D_FMT);
961 if (!recursive (fmt))
963 if (s.decided == loc)
970 if (strcmp (fmt, HERE_D_FMT))
976 if (!recursive (HERE_D_FMT))
980 if (s.decided != raw)
982 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
985 fmt = _NL_CURRENT (LC_TIME, T_FMT);
987 if (!recursive (fmt))
989 if (s.decided == loc)
996 if (strcmp (fmt, HERE_T_FMT))
1002 if (!recursive (HERE_T_FMT))
1010 /* We have no information about the era format. Just use
1011 the normal format. */
1012 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
1013 && *fmt != 'x' && *fmt != 'X')
1014 /* This is an illegal format. */
1024 /* Match day of month using alternate numeric symbols. */
1025 get_alt_number (1, 31, 2);
1031 /* Match hour in 24-hour clock using alternate numeric
1033 get_alt_number (0, 23, 2);
1038 /* Match hour in 12-hour clock using alternate numeric
1040 get_alt_number (1, 12, 2);
1041 tm->tm_hour = val % 12;
1045 /* Match month using alternate numeric symbols. */
1046 get_alt_number (1, 12, 2);
1047 tm->tm_mon = val - 1;
1052 /* Match minutes using alternate numeric symbols. */
1053 get_alt_number (0, 59, 2);
1057 /* Match seconds using alternate numeric symbols. */
1058 get_alt_number (0, 61, 2);
1062 get_alt_number (0, 53, 2);
1067 get_alt_number (0, 53, 2);
1072 get_alt_number (0, 53, 2);
1073 /* XXX This cannot determine any field in TM without
1074 further information. */
1077 /* Match number of weekday using alternate numeric symbols. */
1078 get_alt_number (0, 6, 1);
1083 /* Match year within century using alternate numeric symbols. */
1084 get_alt_number (0, 99, 2);
1085 tm->tm_year = val >= 69 ? val : val + 100;
1099 /* Recursive invocation, returning success, so
1100 update parent's struct tm and state. */
1101 *(struct __strptime_state *) statep = s;
1106 if (s.have_I && s.is_pm)
1109 if (s.century != -1)
1112 tm->tm_year = tm->tm_year % 100 + (s.century - 19) * 100;
1114 /* Only the century, but not the year. Strange, but so be it. */
1115 tm->tm_year = (s.century - 19) * 100;
1118 if (s.era_cnt != -1)
1120 era = _nl_select_era_entry (s.era_cnt HELPER_LOCALE_ARG);
1124 tm->tm_year = (era->start_date[0]
1125 + ((tm->tm_year - era->offset)
1126 * era->absolute_direction));
1128 /* Era start year assumed. */
1129 tm->tm_year = era->start_date[0];
1134 /* No era found but we have seen an E modifier. Rectify some
1136 if (s.want_century && s.century == -1 && tm->tm_year < 69)
1140 if (s.want_xday && !s.have_wday)
1142 if ( !(s.have_mon && s.have_mday) && s.have_yday)
1144 /* We don't have tm_mon and/or tm_mday, compute them. */
1146 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
1149 tm->tm_mon = t_mon - 1;
1153 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
1157 /* Don't crash in day_of_the_week if tm_mon is uninitialized. */
1158 if (s.have_mon || (unsigned) tm->tm_mon <= 11)
1159 day_of_the_week (tm);
1162 if (s.want_xday && !s.have_yday && (s.have_mon || (unsigned) tm->tm_mon <= 11))
1163 day_of_the_year (tm);
1165 if ((s.have_uweek || s.have_wweek) && s.have_wday)
1167 int save_wday = tm->tm_wday;
1168 int save_mday = tm->tm_mday;
1169 int save_mon = tm->tm_mon;
1170 int w_offset = s.have_uweek ? 0 : 1;
1174 day_of_the_week (tm);
1176 tm->tm_mday = save_mday;
1178 tm->tm_mon = save_mon;
1181 tm->tm_yday = ((7 - (tm->tm_wday - w_offset)) % 7
1182 + (s.week_no - 1) *7
1183 + save_wday - w_offset);
1185 if (!s.have_mday || !s.have_mon)
1188 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon]
1192 tm->tm_mon = t_mon - 1;
1196 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
1199 tm->tm_wday = save_wday;
1207 strptime (buf, format, tm LOCALE_PARAM)
1213 return __strptime_internal (buf, format, tm, NULL LOCALE_ARG);
1217 weak_alias (__strptime_l, strptime_l)