1 /* Copyright (C) 2002, 2004-2005, 2007, 2009-2012 Free Software Foundation,
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
28 # include <langinfo.h>
35 # include "../locale/localeinfo.h"
39 enum ptime_locale_status { not, loc, raw };
44 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
45 #if defined _LIBC && defined __GNUC__ && __GNUC__ >= 2
46 # define match_string(cs1, s2) \
47 ({ size_t len = strlen (cs1); \
48 int result = __strncasecmp_l ((cs1), (s2), len, locale) == 0; \
49 if (result) (s2) += len; \
52 /* Oh come on. Get a reasonable compiler. */
53 # define match_string(cs1, s2) \
54 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
56 /* We intentionally do not use isdigit() for testing because this will
57 lead to problems with the wide character version. */
58 #define get_number(from, to, n) \
64 if (*rp < '0' || *rp > '9') \
69 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
70 if (val < from || val > to) \
74 # define get_alt_number(from, to, n) \
76 __label__ do_normal; \
78 if (*decided != raw) \
80 val = _nl_parse_alt_digit (&rp HELPER_LOCALE_ARG); \
81 if (val == -1 && *decided != loc) \
86 if (val < from || val > to) \
92 get_number (from, to, n); \
97 # define get_alt_number(from, to, n) \
98 /* We don't have the alternate representation. */ \
99 get_number(from, to, n)
101 #define recursive(new_fmt) \
102 (*(new_fmt) != '\0' \
103 && (rp = __strptime_internal (rp, (new_fmt), tm, \
104 decided, era_cnt LOCALE_ARG)) != NULL)
108 /* This is defined in locale/C-time.c in the GNU libc. */
109 extern const struct locale_data _nl_C_LC_TIME attribute_hidden;
111 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
112 # define ab_weekday_name \
113 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
114 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
115 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
116 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
117 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
118 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
119 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
120 # define HERE_T_FMT_AMPM \
121 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
122 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
124 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
126 static char const weekday_name[][10] =
128 "Sunday", "Monday", "Tuesday", "Wednesday",
129 "Thursday", "Friday", "Saturday"
131 static char const ab_weekday_name[][4] =
133 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
135 static char const month_name[][10] =
137 "January", "February", "March", "April", "May", "June",
138 "July", "August", "September", "October", "November", "December"
140 static char const ab_month_name[][4] =
142 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
143 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
145 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
146 # define HERE_D_FMT "%m/%d/%y"
147 # define HERE_AM_STR "AM"
148 # define HERE_PM_STR "PM"
149 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
150 # define HERE_T_FMT "%H:%M:%S"
152 static const unsigned short int __mon_yday[2][13] =
155 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
157 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
162 /* We use this code also for the extended locale handling where the
163 function gets as an additional argument the locale which has to be
164 used. To access the values we have to redefine the _NL_CURRENT
166 # define strptime __strptime_l
168 # define _NL_CURRENT(category, item) \
169 (current->values[_NL_ITEM_INDEX (item)].string)
170 # undef _NL_CURRENT_WORD
171 # define _NL_CURRENT_WORD(category, item) \
172 (current->values[_NL_ITEM_INDEX (item)].word)
173 # define LOCALE_PARAM , locale
174 # define LOCALE_ARG , locale
175 # define LOCALE_PARAM_PROTO , __locale_t locale
176 # define LOCALE_PARAM_DECL __locale_t locale;
177 # define HELPER_LOCALE_ARG , current
178 # define ISSPACE(Ch) __isspace_l (Ch, locale)
180 # define LOCALE_PARAM
182 # define LOCALE_PARAM_DECL
183 # define LOCALE_PARAM_PROTO
184 # define HELPER_LOCALE_ARG
185 # define ISSPACE(Ch) isspace (Ch)
192 /* Nonzero if YEAR is a leap year (every 4 years,
193 except every 100th isn't, and every 400th is). */
194 # define __isleap(year) \
195 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
198 /* Compute the day of the week. */
200 day_of_the_week (struct tm *tm)
202 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
203 difference between this data in the one on TM and so determine
205 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
207 + (365 * (tm->tm_year - 70))
209 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
210 + (((corr_year / 4) / 25) / 4)
211 + __mon_yday[0][tm->tm_mon]
213 tm->tm_wday = ((wday % 7) + 7) % 7;
216 /* Compute the day of the year. */
218 day_of_the_year (struct tm *tm)
220 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
221 + (tm->tm_mday - 1));
231 __strptime_internal (rp, fmt, tm, decided, era_cnt LOCALE_PARAM)
235 enum ptime_locale_status *decided;
240 struct locale_data *const current = locale->__locales[LC_TIME];
246 int century, want_century;
248 int have_wday, want_xday;
250 int have_mon, have_mday;
251 int have_uweek, have_wweek;
255 struct era_entry *era = NULL;
256 const char *rp_backup;
265 have_wday = want_xday = have_yday = have_mon = have_mday = have_uweek = 0;
270 /* A white space in the format string matches 0 more or white
271 space in the input string. */
274 while (ISSPACE (*rp))
280 /* Any character but '%' must be matched by the same character
281 in the iput string. */
284 match_char (*fmt++, *rp++);
290 /* We need this for handling the 'E' modifier. */
293 /* Make back up of current processing pointer. */
300 /* Match the '%' character itself. */
301 match_char ('%', *rp++);
305 /* Match day of week. */
306 for (cnt = 0; cnt < 7; ++cnt)
311 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
314 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
319 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
322 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
323 ab_weekday_name[cnt]))
330 && (match_string (weekday_name[cnt], rp)
331 || match_string (ab_weekday_name[cnt], rp)))
338 /* Does not match a weekday name. */
346 /* Match month name. */
347 for (cnt = 0; cnt < 12; ++cnt)
352 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
355 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
360 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
363 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
370 if (match_string (month_name[cnt], rp)
371 || match_string (ab_month_name[cnt], rp))
378 /* Does not match a month name. */
384 /* Match locale's date and time format. */
388 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
397 if (*decided == not &&
398 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
406 if (!recursive (HERE_D_T_FMT))
411 /* Match century number. */
415 get_number (0, 99, 2);
421 /* Match day of month. */
422 get_number (1, 31, 2);
428 if (!recursive ("%Y-%m-%d"))
436 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
446 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
456 /* Match standard day format. */
457 if (!recursive (HERE_D_FMT))
463 /* Match hour in 24-hour clock. */
464 get_number (0, 23, 2);
469 /* Match hour in 12-hour clock. GNU extension. */
471 /* Match hour in 12-hour clock. */
472 get_number (1, 12, 2);
473 tm->tm_hour = val % 12;
477 /* Match day number of year. */
478 get_number (1, 366, 3);
479 tm->tm_yday = val - 1;
483 /* Match number of month. */
484 get_number (1, 12, 2);
485 tm->tm_mon = val - 1;
491 get_number (0, 59, 2);
496 /* Match any white space. */
497 while (ISSPACE (*rp))
501 /* Match locale's equivalent of AM/PM. */
505 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
507 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
511 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
513 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
521 if (!match_string (HERE_AM_STR, rp))
523 if (match_string (HERE_PM_STR, rp))
533 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
542 if (*decided == not &&
543 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
551 if (!recursive (HERE_T_FMT_AMPM))
555 if (!recursive ("%H:%M"))
560 /* The number of seconds may be very high so we cannot use
561 the 'get_number' macro. Instead read the number
562 character for character and construct the result while
565 if (*rp < '0' || *rp > '9')
566 /* We need at least one digit. */
574 while (*rp >= '0' && *rp <= '9');
576 if (localtime_r (&secs, tm) == NULL)
577 /* Error in function. */
582 get_number (0, 61, 2);
589 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
598 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
607 if (!recursive (HERE_T_FMT))
611 get_number (1, 7, 1);
612 tm->tm_wday = val % 7;
616 get_number (0, 99, 2);
617 /* XXX This cannot determine any field in TM. */
620 if (*rp < '0' || *rp > '9')
622 /* XXX Ignore the number since we would need some more
623 information to compute a real date. */
626 while (*rp >= '0' && *rp <= '9');
629 get_number (0, 53, 2);
634 get_number (0, 53, 2);
639 get_number (0, 53, 2);
640 /* XXX This cannot determine any field in TM without some
644 /* Match number of weekday. */
645 get_number (0, 6, 1);
651 match_year_in_century:
653 /* Match year within century. */
654 get_number (0, 99, 2);
655 /* The "Year 2000: The Millennium Rollover" paper suggests that
656 values in the range 69-99 refer to the twentieth century. */
657 tm->tm_year = val >= 69 ? val : val + 100;
658 /* Indicate that we want to use the century, if specified. */
663 /* Match year including century number. */
664 get_number (0, 9999, 4);
665 tm->tm_year = val - 1900;
670 /* XXX How to handle this? */
673 /* We recognize two formats: if two digits are given, these
674 specify hours. If fours digits are used, minutes are
677 #if defined _LIBC || HAVE_TM_GMTOFF
685 if (*rp != '+' && *rp != '-')
687 #if defined _LIBC || HAVE_TM_GMTOFF
691 while (n < 4 && *rp >= '0' && *rp <= '9')
693 val = val * 10 + *rp++ - '0';
699 /* Only two or four digits recognized. */
703 /* We have to convert the minutes into decimal. */
706 val = (val / 100) * 100 + ((val % 100) * 50) / 30;
710 #if defined _LIBC || HAVE_TM_GMTOFF
711 tm->tm_gmtoff = (val * 3600) / 100;
713 tm->tm_gmtoff = -tm->tm_gmtoff;
722 /* Match locale's alternate date and time format. */
725 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
728 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
730 if (!recursive (fmt))
739 if (strcmp (fmt, HERE_D_T_FMT))
746 /* The C locale has no era information, so use the
747 normal representation. */
748 if (!recursive (HERE_D_T_FMT))
757 era = _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG);
758 if (era != NULL && match_string (era->era_name, rp))
767 num_eras = _NL_CURRENT_WORD (LC_TIME,
768 _NL_TIME_ERA_NUM_ENTRIES);
769 for (era_cnt = 0; era_cnt < (int) num_eras;
770 ++era_cnt, rp = rp_backup)
772 era = _nl_select_era_entry (era_cnt
774 if (era != NULL && match_string (era->era_name, rp))
780 if (era_cnt != (int) num_eras)
789 /* The C locale has no era information, so use the
790 normal representation. */
795 get_number(0, 9999, 4);
803 assert (*decided == loc);
805 era = _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG);
809 int delta = ((tm->tm_year - era->offset)
810 * era->absolute_direction);
812 && delta < (((int64_t) era->stop_date[0]
813 - (int64_t) era->start_date[0])
814 * era->absolute_direction));
822 num_eras = _NL_CURRENT_WORD (LC_TIME,
823 _NL_TIME_ERA_NUM_ENTRIES);
824 for (era_cnt = 0; era_cnt < (int) num_eras; ++era_cnt)
826 era = _nl_select_era_entry (era_cnt
830 int delta = ((tm->tm_year - era->offset)
831 * era->absolute_direction);
833 && delta < (((int64_t) era->stop_date[0]
834 - (int64_t) era->start_date[0])
835 * era->absolute_direction))
842 if (era_cnt != (int) num_eras)
852 goto match_year_in_century;
856 num_eras = _NL_CURRENT_WORD (LC_TIME,
857 _NL_TIME_ERA_NUM_ENTRIES);
858 for (era_cnt = 0; era_cnt < (int) num_eras;
859 ++era_cnt, rp = rp_backup)
861 era = _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG);
862 if (era != NULL && recursive (era->era_format))
865 if (era_cnt == (int) num_eras)
882 get_number (0, 9999, 4);
883 tm->tm_year = val - 1900;
890 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
893 fmt = _NL_CURRENT (LC_TIME, D_FMT);
895 if (!recursive (fmt))
904 if (strcmp (fmt, HERE_D_FMT))
910 if (!recursive (HERE_D_FMT))
916 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
919 fmt = _NL_CURRENT (LC_TIME, T_FMT);
921 if (!recursive (fmt))
930 if (strcmp (fmt, HERE_T_FMT))
936 if (!recursive (HERE_T_FMT))
944 /* We have no information about the era format. Just use
945 the normal format. */
946 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
947 && *fmt != 'x' && *fmt != 'X')
948 /* This is an illegal format. */
958 /* Match day of month using alternate numeric symbols. */
959 get_alt_number (1, 31, 2);
965 /* Match hour in 24-hour clock using alternate numeric
967 get_alt_number (0, 23, 2);
972 /* Match hour in 12-hour clock using alternate numeric
974 get_alt_number (1, 12, 2);
975 tm->tm_hour = val % 12;
979 /* Match month using alternate numeric symbols. */
980 get_alt_number (1, 12, 2);
981 tm->tm_mon = val - 1;
986 /* Match minutes using alternate numeric symbols. */
987 get_alt_number (0, 59, 2);
991 /* Match seconds using alternate numeric symbols. */
992 get_alt_number (0, 61, 2);
996 get_alt_number (0, 53, 2);
1001 get_alt_number (0, 53, 2);
1006 get_alt_number (0, 53, 2);
1007 /* XXX This cannot determine any field in TM without
1008 further information. */
1011 /* Match number of weekday using alternate numeric symbols. */
1012 get_alt_number (0, 6, 1);
1017 /* Match year within century using alternate numeric symbols. */
1018 get_alt_number (0, 99, 2);
1019 tm->tm_year = val >= 69 ? val : val + 100;
1031 if (have_I && is_pm)
1037 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100;
1039 /* Only the century, but not the year. Strange, but so be it. */
1040 tm->tm_year = (century - 19) * 100;
1046 era = _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG);
1050 tm->tm_year = (era->start_date[0]
1051 + ((tm->tm_year - era->offset)
1052 * era->absolute_direction));
1054 /* Era start year assumed. */
1055 tm->tm_year = era->start_date[0];
1061 /* No era found but we have seen an E modifier. Rectify some
1063 if (want_century && century == -1 && tm->tm_year < 69)
1067 if (want_xday && !have_wday)
1069 if ( !(have_mon && have_mday) && have_yday)
1071 /* We don't have tm_mon and/or tm_mday, compute them. */
1073 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
1076 tm->tm_mon = t_mon - 1;
1080 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
1082 day_of_the_week (tm);
1085 if (want_xday && !have_yday)
1086 day_of_the_year (tm);
1088 if ((have_uweek || have_wweek) && have_wday)
1090 int save_wday = tm->tm_wday;
1091 int save_mday = tm->tm_mday;
1092 int save_mon = tm->tm_mon;
1093 int w_offset = have_uweek ? 0 : 1;
1097 day_of_the_week (tm);
1099 tm->tm_mday = save_mday;
1101 tm->tm_mon = save_mon;
1104 tm->tm_yday = ((7 - (tm->tm_wday - w_offset)) % 7
1106 + save_wday - w_offset);
1108 if (!have_mday || !have_mon)
1111 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon]
1115 tm->tm_mon = t_mon - 1;
1119 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
1122 tm->tm_wday = save_wday;
1130 strptime (buf, format, tm LOCALE_PARAM)
1131 const char *restrict buf;
1132 const char *restrict format;
1133 struct tm *restrict tm;
1136 enum ptime_locale_status decided;
1143 return __strptime_internal (buf, format, tm, &decided, -1 LOCALE_ARG);
1147 weak_alias (__strptime_l, strptime_l)