1 /* Copyright (C) 2002, 2004-2005, 2007, 2009-2017 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, see <http://www.gnu.org/licenses/>. */
27 # include <langinfo.h>
34 # include "../locale/localeinfo.h"
38 enum ptime_locale_status
{ not, loc
, raw
};
43 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
44 #if defined _LIBC && defined __GNUC__ && __GNUC__ >= 2
45 # define match_string(cs1, s2) \
46 ({ size_t len = strlen (cs1); \
47 int result = __strncasecmp_l ((cs1), (s2), len, locale) == 0; \
48 if (result) (s2) += len; \
51 /* Oh come on. Get a reasonable compiler. */
52 # define match_string(cs1, s2) \
53 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
55 /* We intentionally do not use isdigit() for testing because this will
56 lead to problems with the wide character version. */
57 #define get_number(from, to, n) \
63 if (*rp < '0' || *rp > '9') \
68 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
69 if (val < from || val > to) \
73 # define get_alt_number(from, to, n) \
75 __label__ do_normal; \
77 if (*decided != raw) \
79 val = _nl_parse_alt_digit (&rp HELPER_LOCALE_ARG); \
80 if (val == -1 && *decided != loc) \
85 if (val < from || val > to) \
91 get_number (from, to, n); \
96 # define get_alt_number(from, to, n) \
97 /* We don't have the alternate representation. */ \
98 get_number(from, to, n)
100 #define recursive(new_fmt) \
101 (*(new_fmt) != '\0' \
102 && (rp = __strptime_internal (rp, (new_fmt), tm, \
103 decided, era_cnt LOCALE_ARG)) != NULL)
107 /* This is defined in locale/C-time.c in the GNU libc. */
108 extern const struct locale_data _nl_C_LC_TIME attribute_hidden
;
110 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
111 # define ab_weekday_name \
112 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
113 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
114 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
115 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
116 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
117 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
118 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
119 # define HERE_T_FMT_AMPM \
120 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
121 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
123 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
125 static char const weekday_name
[][10] =
127 "Sunday", "Monday", "Tuesday", "Wednesday",
128 "Thursday", "Friday", "Saturday"
130 static char const ab_weekday_name
[][4] =
132 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
134 static char const month_name
[][10] =
136 "January", "February", "March", "April", "May", "June",
137 "July", "August", "September", "October", "November", "December"
139 static char const ab_month_name
[][4] =
141 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
142 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
144 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
145 # define HERE_D_FMT "%m/%d/%y"
146 # define HERE_AM_STR "AM"
147 # define HERE_PM_STR "PM"
148 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
149 # define HERE_T_FMT "%H:%M:%S"
151 static const unsigned short int __mon_yday
[2][13] =
154 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
156 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
161 /* We use this code also for the extended locale handling where the
162 function gets as an additional argument the locale which has to be
163 used. To access the values we have to redefine the _NL_CURRENT
165 # define strptime __strptime_l
167 # define _NL_CURRENT(category, item) \
168 (current->values[_NL_ITEM_INDEX (item)].string)
169 # undef _NL_CURRENT_WORD
170 # define _NL_CURRENT_WORD(category, item) \
171 (current->values[_NL_ITEM_INDEX (item)].word)
172 # define LOCALE_PARAM , locale
173 # define LOCALE_ARG , locale
174 # define LOCALE_PARAM_PROTO , __locale_t locale
175 # define LOCALE_PARAM_DECL __locale_t locale;
176 # define HELPER_LOCALE_ARG , current
177 # define ISSPACE(Ch) __isspace_l (Ch, locale)
179 # define LOCALE_PARAM
181 # define LOCALE_PARAM_DECL
182 # define LOCALE_PARAM_PROTO
183 # define HELPER_LOCALE_ARG
184 # define ISSPACE(Ch) isspace (Ch)
191 /* Nonzero if YEAR is a leap year (every 4 years,
192 except every 100th isn't, and every 400th is). */
193 # define __isleap(year) \
194 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
197 /* Compute the day of the week. */
199 day_of_the_week (struct tm
*tm
)
201 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
202 difference between this data in the one on TM and so determine
204 int corr_year
= 1900 + tm
->tm_year
- (tm
->tm_mon
< 2);
206 + (365 * (tm
->tm_year
- 70))
208 - ((corr_year
/ 4) / 25) + ((corr_year
/ 4) % 25 < 0)
209 + (((corr_year
/ 4) / 25) / 4)
210 + __mon_yday
[0][tm
->tm_mon
]
212 tm
->tm_wday
= ((wday
% 7) + 7) % 7;
215 /* Compute the day of the year. */
217 day_of_the_year (struct tm
*tm
)
219 tm
->tm_yday
= (__mon_yday
[__isleap (1900 + tm
->tm_year
)][tm
->tm_mon
]
220 + (tm
->tm_mday
- 1));
230 __strptime_internal (rp
, fmt
, tm
, decided
, era_cnt LOCALE_PARAM
)
234 enum ptime_locale_status
*decided
;
239 struct locale_data
*const current
= locale
->__locales
[LC_TIME
];
245 int century
, want_century
;
247 int have_wday
, want_xday
;
249 int have_mon
, have_mday
;
250 int have_uweek
, have_wweek
;
254 struct era_entry
*era
= NULL
;
255 const char *rp_backup
;
264 have_wday
= want_xday
= have_yday
= have_mon
= have_mday
= have_uweek
= 0;
269 /* A white space in the format string matches 0 more or white
270 space in the input string. */
273 while (ISSPACE (*rp
))
279 /* Any character but '%' must be matched by the same character
280 in the iput string. */
283 match_char (*fmt
++, *rp
++);
289 /* We need this for handling the 'E' modifier. */
292 /* Make back up of current processing pointer. */
299 /* Match the '%' character itself. */
300 match_char ('%', *rp
++);
304 /* Match day of week. */
305 for (cnt
= 0; cnt
< 7; ++cnt
)
310 if (match_string (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
), rp
))
313 && strcmp (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
),
318 if (match_string (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
), rp
))
321 && strcmp (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
),
322 ab_weekday_name
[cnt
]))
329 && (match_string (weekday_name
[cnt
], rp
)
330 || match_string (ab_weekday_name
[cnt
], rp
)))
337 /* Does not match a weekday name. */
345 /* Match month name. */
346 for (cnt
= 0; cnt
< 12; ++cnt
)
351 if (match_string (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
), rp
))
354 && strcmp (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
),
359 if (match_string (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
), rp
))
362 && strcmp (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
),
369 if (match_string (month_name
[cnt
], rp
)
370 || match_string (ab_month_name
[cnt
], rp
))
377 /* Does not match a month name. */
383 /* Match locale's date and time format. */
387 if (!recursive (_NL_CURRENT (LC_TIME
, D_T_FMT
)))
396 if (*decided
== not &&
397 strcmp (_NL_CURRENT (LC_TIME
, D_T_FMT
), HERE_D_T_FMT
))
405 if (!recursive (HERE_D_T_FMT
))
410 /* Match century number. */
414 get_number (0, 99, 2);
420 /* Match day of month. */
421 get_number (1, 31, 2);
427 if (!recursive ("%Y-%m-%d"))
435 if (!recursive (_NL_CURRENT (LC_TIME
, D_FMT
)))
445 && strcmp (_NL_CURRENT (LC_TIME
, D_FMT
), HERE_D_FMT
))
455 /* Match standard day format. */
456 if (!recursive (HERE_D_FMT
))
462 /* Match hour in 24-hour clock. */
463 get_number (0, 23, 2);
468 /* Match hour in 12-hour clock. GNU extension. */
470 /* Match hour in 12-hour clock. */
471 get_number (1, 12, 2);
472 tm
->tm_hour
= val
% 12;
476 /* Match day number of year. */
477 get_number (1, 366, 3);
478 tm
->tm_yday
= val
- 1;
482 /* Match number of month. */
483 get_number (1, 12, 2);
484 tm
->tm_mon
= val
- 1;
490 get_number (0, 59, 2);
495 /* Match any white space. */
496 while (ISSPACE (*rp
))
500 /* Match locale's equivalent of AM/PM. */
504 if (match_string (_NL_CURRENT (LC_TIME
, AM_STR
), rp
))
506 if (strcmp (_NL_CURRENT (LC_TIME
, AM_STR
), HERE_AM_STR
))
510 if (match_string (_NL_CURRENT (LC_TIME
, PM_STR
), rp
))
512 if (strcmp (_NL_CURRENT (LC_TIME
, PM_STR
), HERE_PM_STR
))
520 if (!match_string (HERE_AM_STR
, rp
))
522 if (match_string (HERE_PM_STR
, rp
))
529 /* Match quarter of year. GNU extension. */
530 get_number (1, 4, 1);
531 tm
->tm_mon
= (val
- 1) * 3;
541 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
550 if (*decided
== not &&
551 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
559 if (!recursive (HERE_T_FMT_AMPM
))
563 if (!recursive ("%H:%M"))
568 /* The number of seconds may be very high so we cannot use
569 the 'get_number' macro. Instead read the number
570 character for character and construct the result while
573 if (*rp
< '0' || *rp
> '9')
574 /* We need at least one digit. */
582 while (*rp
>= '0' && *rp
<= '9');
584 if (localtime_r (&secs
, tm
) == NULL
)
585 /* Error in function. */
590 get_number (0, 61, 2);
597 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
606 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
615 if (!recursive (HERE_T_FMT
))
619 get_number (1, 7, 1);
620 tm
->tm_wday
= val
% 7;
624 get_number (0, 99, 2);
625 /* XXX This cannot determine any field in TM. */
628 if (*rp
< '0' || *rp
> '9')
630 /* XXX Ignore the number since we would need some more
631 information to compute a real date. */
634 while (*rp
>= '0' && *rp
<= '9');
637 get_number (0, 53, 2);
642 get_number (0, 53, 2);
647 get_number (0, 53, 2);
648 /* XXX This cannot determine any field in TM without some
652 /* Match number of weekday. */
653 get_number (0, 6, 1);
659 match_year_in_century
:
661 /* Match year within century. */
662 get_number (0, 99, 2);
663 /* The "Year 2000: The Millennium Rollover" paper suggests that
664 values in the range 69-99 refer to the twentieth century. */
665 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
666 /* Indicate that we want to use the century, if specified. */
671 /* Match year including century number. */
672 get_number (0, 9999, 4);
673 tm
->tm_year
= val
- 1900;
678 /* XXX How to handle this? */
681 /* We recognize two formats: if two digits are given, these
682 specify hours. If fours digits are used, minutes are
691 if (*rp
!= '+' && *rp
!= '-')
695 while (n
< 4 && *rp
>= '0' && *rp
<= '9')
697 val
= val
* 10 + *rp
++ - '0';
703 /* Only two or four digits recognized. */
707 /* We have to convert the minutes into decimal. */
710 val
= (val
/ 100) * 100 + ((val
% 100) * 50) / 30;
714 #if defined _LIBC || HAVE_TM_GMTOFF
715 tm
->tm_gmtoff
= (val
* 3600) / 100;
717 tm
->tm_gmtoff
= -tm
->tm_gmtoff
;
726 /* Match locale's alternate date and time format. */
729 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
732 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
734 if (!recursive (fmt
))
743 if (strcmp (fmt
, HERE_D_T_FMT
))
750 /* The C locale has no era information, so use the
751 normal representation. */
752 if (!recursive (HERE_D_T_FMT
))
761 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
762 if (era
!= NULL
&& match_string (era
->era_name
, rp
))
771 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
772 _NL_TIME_ERA_NUM_ENTRIES
);
773 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
774 ++era_cnt
, rp
= rp_backup
)
776 era
= _nl_select_era_entry (era_cnt
778 if (era
!= NULL
&& match_string (era
->era_name
, rp
))
784 if (era_cnt
!= (int) num_eras
)
793 /* The C locale has no era information, so use the
794 normal representation. */
799 get_number(0, 9999, 4);
807 assert (*decided
== loc
);
809 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
813 int delta
= ((tm
->tm_year
- era
->offset
)
814 * era
->absolute_direction
);
816 && delta
< (((int64_t) era
->stop_date
[0]
817 - (int64_t) era
->start_date
[0])
818 * era
->absolute_direction
));
826 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
827 _NL_TIME_ERA_NUM_ENTRIES
);
828 for (era_cnt
= 0; era_cnt
< (int) num_eras
; ++era_cnt
)
830 era
= _nl_select_era_entry (era_cnt
834 int delta
= ((tm
->tm_year
- era
->offset
)
835 * era
->absolute_direction
);
837 && delta
< (((int64_t) era
->stop_date
[0]
838 - (int64_t) era
->start_date
[0])
839 * era
->absolute_direction
))
846 if (era_cnt
!= (int) num_eras
)
856 goto match_year_in_century
;
860 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
861 _NL_TIME_ERA_NUM_ENTRIES
);
862 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
863 ++era_cnt
, rp
= rp_backup
)
865 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
866 if (era
!= NULL
&& recursive (era
->era_format
))
869 if (era_cnt
== (int) num_eras
)
886 get_number (0, 9999, 4);
887 tm
->tm_year
= val
- 1900;
894 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
897 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
899 if (!recursive (fmt
))
908 if (strcmp (fmt
, HERE_D_FMT
))
914 if (!recursive (HERE_D_FMT
))
920 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
923 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
925 if (!recursive (fmt
))
934 if (strcmp (fmt
, HERE_T_FMT
))
940 if (!recursive (HERE_T_FMT
))
948 /* We have no information about the era format. Just use
949 the normal format. */
950 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
951 && *fmt
!= 'x' && *fmt
!= 'X')
952 /* This is an illegal format. */
962 /* Match day of month using alternate numeric symbols. */
963 get_alt_number (1, 31, 2);
969 /* Match hour in 24-hour clock using alternate numeric
971 get_alt_number (0, 23, 2);
976 /* Match hour in 12-hour clock using alternate numeric
978 get_alt_number (1, 12, 2);
979 tm
->tm_hour
= val
% 12;
983 /* Match month using alternate numeric symbols. */
984 get_alt_number (1, 12, 2);
985 tm
->tm_mon
= val
- 1;
990 /* Match minutes using alternate numeric symbols. */
991 get_alt_number (0, 59, 2);
995 /* Match quarter using alternate numeric symbols. */
996 get_alt_number (1, 4, 1);
997 tm
->tm_mon
= (val
- 1) * 3;
1004 /* Match seconds using alternate numeric symbols. */
1005 get_alt_number (0, 61, 2);
1009 get_alt_number (0, 53, 2);
1014 get_alt_number (0, 53, 2);
1019 get_alt_number (0, 53, 2);
1020 /* XXX This cannot determine any field in TM without
1021 further information. */
1024 /* Match number of weekday using alternate numeric symbols. */
1025 get_alt_number (0, 6, 1);
1030 /* Match year within century using alternate numeric symbols. */
1031 get_alt_number (0, 99, 2);
1032 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
1044 if (have_I
&& is_pm
)
1050 tm
->tm_year
= tm
->tm_year
% 100 + (century
- 19) * 100;
1052 /* Only the century, but not the year. Strange, but so be it. */
1053 tm
->tm_year
= (century
- 19) * 100;
1059 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
1063 tm
->tm_year
= (era
->start_date
[0]
1064 + ((tm
->tm_year
- era
->offset
)
1065 * era
->absolute_direction
));
1067 /* Era start year assumed. */
1068 tm
->tm_year
= era
->start_date
[0];
1074 /* No era found but we have seen an E modifier. Rectify some
1076 if (want_century
&& century
== -1 && tm
->tm_year
< 69)
1080 if (want_xday
&& !have_wday
)
1082 if ( !(have_mon
&& have_mday
) && have_yday
)
1084 /* We don't have tm_mon and/or tm_mday, compute them. */
1086 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
] <= tm
->tm_yday
)
1089 tm
->tm_mon
= t_mon
- 1;
1093 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
1095 day_of_the_week (tm
);
1098 if (want_xday
&& !have_yday
)
1099 day_of_the_year (tm
);
1101 if ((have_uweek
|| have_wweek
) && have_wday
)
1103 int save_wday
= tm
->tm_wday
;
1104 int save_mday
= tm
->tm_mday
;
1105 int save_mon
= tm
->tm_mon
;
1106 int w_offset
= have_uweek
? 0 : 1;
1110 day_of_the_week (tm
);
1112 tm
->tm_mday
= save_mday
;
1114 tm
->tm_mon
= save_mon
;
1117 tm
->tm_yday
= ((7 - (tm
->tm_wday
- w_offset
)) % 7
1119 + save_wday
- w_offset
);
1121 if (!have_mday
|| !have_mon
)
1124 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
]
1128 tm
->tm_mon
= t_mon
- 1;
1132 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
1135 tm
->tm_wday
= save_wday
;
1143 strptime (buf
, format
, tm LOCALE_PARAM
)
1144 const char *restrict buf
;
1145 const char *restrict format
;
1146 struct tm
*restrict tm
;
1149 enum ptime_locale_status decided
;
1156 return __strptime_internal (buf
, format
, tm
, &decided
, -1 LOCALE_ARG
);
1160 weak_alias (__strptime_l
, strptime_l
)