1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Copyright (C) 2002, 2004-2005, 2007, 2009-2011 Free Software Foundation,
5 This file is part of the GNU C Library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
30 # include <langinfo.h>
37 # include "../locale/localeinfo.h"
41 enum ptime_locale_status { not, loc, raw };
46 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
47 #if defined _LIBC && defined __GNUC__ && __GNUC__ >= 2
48 # define match_string(cs1, s2) \
49 ({ size_t len = strlen (cs1); \
50 int result = __strncasecmp_l ((cs1), (s2), len, locale) == 0; \
51 if (result) (s2) += len; \
54 /* Oh come on. Get a reasonable compiler. */
55 # define match_string(cs1, s2) \
56 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
58 /* We intentionally do not use isdigit() for testing because this will
59 lead to problems with the wide character version. */
60 #define get_number(from, to, n) \
66 if (*rp < '0' || *rp > '9') \
71 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
72 if (val < from || val > to) \
76 # define get_alt_number(from, to, n) \
78 __label__ do_normal; \
80 if (*decided != raw) \
82 val = _nl_parse_alt_digit (&rp HELPER_LOCALE_ARG); \
83 if (val == -1 && *decided != loc) \
88 if (val < from || val > to) \
94 get_number (from, to, n); \
99 # define get_alt_number(from, to, n) \
100 /* We don't have the alternate representation. */ \
101 get_number(from, to, n)
103 #define recursive(new_fmt) \
104 (*(new_fmt) != '\0' \
105 && (rp = __strptime_internal (rp, (new_fmt), tm, \
106 decided, era_cnt LOCALE_ARG)) != NULL)
110 /* This is defined in locale/C-time.c in the GNU libc. */
111 extern const struct locale_data _nl_C_LC_TIME attribute_hidden;
113 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
114 # define ab_weekday_name \
115 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
116 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
117 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
118 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
119 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
120 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
121 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
122 # define HERE_T_FMT_AMPM \
123 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
124 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
126 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
128 static char const weekday_name[][10] =
130 "Sunday", "Monday", "Tuesday", "Wednesday",
131 "Thursday", "Friday", "Saturday"
133 static char const ab_weekday_name[][4] =
135 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
137 static char const month_name[][10] =
139 "January", "February", "March", "April", "May", "June",
140 "July", "August", "September", "October", "November", "December"
142 static char const ab_month_name[][4] =
144 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
145 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
147 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
148 # define HERE_D_FMT "%m/%d/%y"
149 # define HERE_AM_STR "AM"
150 # define HERE_PM_STR "PM"
151 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
152 # define HERE_T_FMT "%H:%M:%S"
154 static const unsigned short int __mon_yday[2][13] =
157 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
159 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
164 /* We use this code also for the extended locale handling where the
165 function gets as an additional argument the locale which has to be
166 used. To access the values we have to redefine the _NL_CURRENT
168 # define strptime __strptime_l
170 # define _NL_CURRENT(category, item) \
171 (current->values[_NL_ITEM_INDEX (item)].string)
172 # undef _NL_CURRENT_WORD
173 # define _NL_CURRENT_WORD(category, item) \
174 (current->values[_NL_ITEM_INDEX (item)].word)
175 # define LOCALE_PARAM , locale
176 # define LOCALE_ARG , locale
177 # define LOCALE_PARAM_PROTO , __locale_t locale
178 # define LOCALE_PARAM_DECL __locale_t locale;
179 # define HELPER_LOCALE_ARG , current
180 # define ISSPACE(Ch) __isspace_l (Ch, locale)
182 # define LOCALE_PARAM
184 # define LOCALE_PARAM_DECL
185 # define LOCALE_PARAM_PROTO
186 # define HELPER_LOCALE_ARG
187 # define ISSPACE(Ch) isspace (Ch)
194 /* Nonzero if YEAR is a leap year (every 4 years,
195 except every 100th isn't, and every 400th is). */
196 # define __isleap(year) \
197 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
200 /* Compute the day of the week. */
202 day_of_the_week (struct tm *tm)
204 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
205 difference between this data in the one on TM and so determine
207 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
209 + (365 * (tm->tm_year - 70))
211 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
212 + (((corr_year / 4) / 25) / 4)
213 + __mon_yday[0][tm->tm_mon]
215 tm->tm_wday = ((wday % 7) + 7) % 7;
218 /* Compute the day of the year. */
220 day_of_the_year (struct tm *tm)
222 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
223 + (tm->tm_mday - 1));
233 __strptime_internal (rp, fmt, tm, decided, era_cnt LOCALE_PARAM)
237 enum ptime_locale_status *decided;
242 struct locale_data *const current = locale->__locales[LC_TIME];
245 const char *rp_backup;
249 int century, want_century;
251 int have_wday, want_xday;
253 int have_mon, have_mday;
254 int have_uweek, have_wweek;
259 struct era_entry *era;
268 have_wday = want_xday = have_yday = have_mon = have_mday = have_uweek = 0;
273 /* A white space in the format string matches 0 more or white
274 space in the input string. */
277 while (ISSPACE (*rp))
283 /* Any character but `%' must be matched by the same character
284 in the iput string. */
287 match_char (*fmt++, *rp++);
293 /* We need this for handling the `E' modifier. */
297 /* Make back up of current processing pointer. */
303 /* Match the `%' character itself. */
304 match_char ('%', *rp++);
308 /* Match day of week. */
309 for (cnt = 0; cnt < 7; ++cnt)
314 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
317 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
322 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
325 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
326 ab_weekday_name[cnt]))
333 && (match_string (weekday_name[cnt], rp)
334 || match_string (ab_weekday_name[cnt], rp)))
341 /* Does not match a weekday name. */
349 /* Match month name. */
350 for (cnt = 0; cnt < 12; ++cnt)
355 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
358 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
363 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
366 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
373 if (match_string (month_name[cnt], rp)
374 || match_string (ab_month_name[cnt], rp))
381 /* Does not match a month name. */
387 /* Match locale's date and time format. */
391 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
400 if (*decided == not &&
401 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
409 if (!recursive (HERE_D_T_FMT))
414 /* Match century number. */
418 get_number (0, 99, 2);
424 /* Match day of month. */
425 get_number (1, 31, 2);
431 if (!recursive ("%Y-%m-%d"))
439 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
449 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
459 /* Match standard day format. */
460 if (!recursive (HERE_D_FMT))
466 /* Match hour in 24-hour clock. */
467 get_number (0, 23, 2);
472 /* Match hour in 12-hour clock. GNU extension. */
474 /* Match hour in 12-hour clock. */
475 get_number (1, 12, 2);
476 tm->tm_hour = val % 12;
480 /* Match day number of year. */
481 get_number (1, 366, 3);
482 tm->tm_yday = val - 1;
486 /* Match number of month. */
487 get_number (1, 12, 2);
488 tm->tm_mon = val - 1;
494 get_number (0, 59, 2);
499 /* Match any white space. */
500 while (ISSPACE (*rp))
504 /* Match locale's equivalent of AM/PM. */
508 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
510 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
514 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
516 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
524 if (!match_string (HERE_AM_STR, rp))
526 if (match_string (HERE_PM_STR, rp))
536 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
545 if (*decided == not &&
546 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
554 if (!recursive (HERE_T_FMT_AMPM))
558 if (!recursive ("%H:%M"))
563 /* The number of seconds may be very high so we cannot use
564 the `get_number' macro. Instead read the number
565 character for character and construct the result while
568 if (*rp < '0' || *rp > '9')
569 /* We need at least one digit. */
577 while (*rp >= '0' && *rp <= '9');
579 if (localtime_r (&secs, tm) == NULL)
580 /* Error in function. */
585 get_number (0, 61, 2);
592 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
601 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
610 if (!recursive (HERE_T_FMT))
614 get_number (1, 7, 1);
615 tm->tm_wday = val % 7;
619 get_number (0, 99, 2);
620 /* XXX This cannot determine any field in TM. */
623 if (*rp < '0' || *rp > '9')
625 /* XXX Ignore the number since we would need some more
626 information to compute a real date. */
629 while (*rp >= '0' && *rp <= '9');
632 get_number (0, 53, 2);
637 get_number (0, 53, 2);
642 get_number (0, 53, 2);
643 /* XXX This cannot determine any field in TM without some
647 /* Match number of weekday. */
648 get_number (0, 6, 1);
654 match_year_in_century:
656 /* Match year within century. */
657 get_number (0, 99, 2);
658 /* The "Year 2000: The Millennium Rollover" paper suggests that
659 values in the range 69-99 refer to the twentieth century. */
660 tm->tm_year = val >= 69 ? val : val + 100;
661 /* Indicate that we want to use the century, if specified. */
666 /* Match year including century number. */
667 get_number (0, 9999, 4);
668 tm->tm_year = val - 1900;
673 /* XXX How to handle this? */
676 /* We recognize two formats: if two digits are given, these
677 specify hours. If fours digits are used, minutes are
686 if (*rp != '+' && *rp != '-')
690 while (n < 4 && *rp >= '0' && *rp <= '9')
692 val = val * 10 + *rp++ - '0';
698 /* Only two or four digits recognized. */
702 /* We have to convert the minutes into decimal. */
705 val = (val / 100) * 100 + ((val % 100) * 50) / 30;
709 #if defined _LIBC || HAVE_TM_GMTOFF
710 tm->tm_gmtoff = (val * 3600) / 100;
712 tm->tm_gmtoff = -tm->tm_gmtoff;
721 /* Match locale's alternate date and time format. */
724 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
727 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
729 if (!recursive (fmt))
738 if (strcmp (fmt, HERE_D_T_FMT))
745 /* The C locale has no era information, so use the
746 normal representation. */
747 if (!recursive (HERE_D_T_FMT))
756 era = _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG);
757 if (era != NULL && match_string (era->era_name, rp))
766 num_eras = _NL_CURRENT_WORD (LC_TIME,
767 _NL_TIME_ERA_NUM_ENTRIES);
768 for (era_cnt = 0; era_cnt < (int) num_eras;
769 ++era_cnt, rp = rp_backup)
771 era = _nl_select_era_entry (era_cnt
773 if (era != NULL && match_string (era->era_name, rp))
779 if (era_cnt != (int) num_eras)
788 /* The C locale has no era information, so use the
789 normal representation. */
794 get_number(0, 9999, 4);
802 assert (*decided == loc);
804 era = _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG);
808 int delta = ((tm->tm_year - era->offset)
809 * era->absolute_direction);
811 && delta < (((int64_t) era->stop_date[0]
812 - (int64_t) era->start_date[0])
813 * era->absolute_direction));
821 num_eras = _NL_CURRENT_WORD (LC_TIME,
822 _NL_TIME_ERA_NUM_ENTRIES);
823 for (era_cnt = 0; era_cnt < (int) num_eras; ++era_cnt)
825 era = _nl_select_era_entry (era_cnt
829 int delta = ((tm->tm_year - era->offset)
830 * era->absolute_direction);
832 && delta < (((int64_t) era->stop_date[0]
833 - (int64_t) era->start_date[0])
834 * era->absolute_direction))
841 if (era_cnt != (int) num_eras)
851 goto match_year_in_century;
855 num_eras = _NL_CURRENT_WORD (LC_TIME,
856 _NL_TIME_ERA_NUM_ENTRIES);
857 for (era_cnt = 0; era_cnt < (int) num_eras;
858 ++era_cnt, rp = rp_backup)
860 era = _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG);
861 if (era != NULL && recursive (era->era_format))
864 if (era_cnt == (int) num_eras)
881 get_number (0, 9999, 4);
882 tm->tm_year = val - 1900;
889 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
892 fmt = _NL_CURRENT (LC_TIME, D_FMT);
894 if (!recursive (fmt))
903 if (strcmp (fmt, HERE_D_FMT))
909 if (!recursive (HERE_D_FMT))
915 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
918 fmt = _NL_CURRENT (LC_TIME, T_FMT);
920 if (!recursive (fmt))
929 if (strcmp (fmt, HERE_T_FMT))
935 if (!recursive (HERE_T_FMT))
943 /* We have no information about the era format. Just use
944 the normal format. */
945 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
946 && *fmt != 'x' && *fmt != 'X')
947 /* This is an illegal format. */
957 /* Match day of month using alternate numeric symbols. */
958 get_alt_number (1, 31, 2);
964 /* Match hour in 24-hour clock using alternate numeric
966 get_alt_number (0, 23, 2);
971 /* Match hour in 12-hour clock using alternate numeric
973 get_alt_number (1, 12, 2);
974 tm->tm_hour = val % 12;
978 /* Match month using alternate numeric symbols. */
979 get_alt_number (1, 12, 2);
980 tm->tm_mon = val - 1;
985 /* Match minutes using alternate numeric symbols. */
986 get_alt_number (0, 59, 2);
990 /* Match seconds using alternate numeric symbols. */
991 get_alt_number (0, 61, 2);
995 get_alt_number (0, 53, 2);
1000 get_alt_number (0, 53, 2);
1005 get_alt_number (0, 53, 2);
1006 /* XXX This cannot determine any field in TM without
1007 further information. */
1010 /* Match number of weekday using alternate numeric symbols. */
1011 get_alt_number (0, 6, 1);
1016 /* Match year within century using alternate numeric symbols. */
1017 get_alt_number (0, 99, 2);
1018 tm->tm_year = val >= 69 ? val : val + 100;
1030 if (have_I && is_pm)
1036 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100;
1038 /* Only the century, but not the year. Strange, but so be it. */
1039 tm->tm_year = (century - 19) * 100;
1045 era = _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG);
1049 tm->tm_year = (era->start_date[0]
1050 + ((tm->tm_year - era->offset)
1051 * era->absolute_direction));
1053 /* Era start year assumed. */
1054 tm->tm_year = era->start_date[0];
1060 /* No era found but we have seen an E modifier. Rectify some
1062 if (want_century && century == -1 && tm->tm_year < 69)
1066 if (want_xday && !have_wday)
1068 if ( !(have_mon && have_mday) && have_yday)
1070 /* We don't have tm_mon and/or tm_mday, compute them. */
1072 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
1075 tm->tm_mon = t_mon - 1;
1079 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
1081 day_of_the_week (tm);
1084 if (want_xday && !have_yday)
1085 day_of_the_year (tm);
1087 if ((have_uweek || have_wweek) && have_wday)
1089 int save_wday = tm->tm_wday;
1090 int save_mday = tm->tm_mday;
1091 int save_mon = tm->tm_mon;
1092 int w_offset = have_uweek ? 0 : 1;
1096 day_of_the_week (tm);
1098 tm->tm_mday = save_mday;
1100 tm->tm_mon = save_mon;
1103 tm->tm_yday = ((7 - (tm->tm_wday - w_offset)) % 7
1105 + save_wday - w_offset);
1107 if (!have_mday || !have_mon)
1110 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon]
1114 tm->tm_mon = t_mon - 1;
1118 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
1121 tm->tm_wday = save_wday;
1129 strptime (buf, format, tm LOCALE_PARAM)
1130 const char *restrict buf;
1131 const char *restrict format;
1132 struct tm *restrict tm;
1135 enum ptime_locale_status decided;
1142 return __strptime_internal (buf, format, tm, &decided, -1 LOCALE_ARG);
1146 weak_alias (__strptime_l, strptime_l)