2 /* Copyright (C) 2002, 2004, 2005, 2007 Free Software Foundation, Inc.
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 * Windows does not have a locatime_r,
40 * it doesn't need it as localtime() is thread safe.
41 * This wrapper should 'fake' localtime_r
44 localtime_r (const time_t *timer
, struct tm
*result
)
46 struct tm
*local_result
;
47 /* This should work as it is thread safe on winblows */
48 local_result
= localtime (timer
);
50 if (local_result
== NULL
|| result
== NULL
)
53 memcpy (result
, local_result
, sizeof (result
));
58 enum ptime_locale_status
{ not, loc
, raw
};
63 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
64 #if defined _LIBC && defined __GNUC__ && __GNUC__ >= 2
65 # define match_string(cs1, s2) \
66 ({ size_t len = strlen (cs1); \
67 int result = __strncasecmp_l ((cs1), (s2), len, locale) == 0; \
68 if (result) (s2) += len; \
71 /* Oh come on. Get a reasonable compiler. */
72 # define match_string(cs1, s2) \
73 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
75 /* We intentionally do not use isdigit() for testing because this will
76 lead to problems with the wide character version. */
77 #define get_number(from, to, n) \
83 if (*rp < '0' || *rp > '9') \
88 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
89 if (val < from || val > to) \
93 # define get_alt_number(from, to, n) \
95 __label__ do_normal; \
97 if (*decided != raw) \
99 val = _nl_parse_alt_digit (&rp HELPER_LOCALE_ARG); \
100 if (val == -1 && *decided != loc) \
105 if (val < from || val > to) \
111 get_number (from, to, n); \
116 # define get_alt_number(from, to, n) \
117 /* We don't have the alternate representation. */ \
118 get_number(from, to, n)
120 #define recursive(new_fmt) \
121 (*(new_fmt) != '\0' \
122 && (rp = __strptime_internal (rp, (new_fmt), tm, \
123 decided, era_cnt LOCALE_ARG)) != NULL)
127 /* This is defined in locale/C-time.c in the GNU libc. */
128 extern const struct locale_data _nl_C_LC_TIME attribute_hidden
;
130 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
131 # define ab_weekday_name \
132 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
133 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
134 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
135 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
136 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
137 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
138 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
139 # define HERE_T_FMT_AMPM \
140 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
141 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
143 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
145 static char const weekday_name
[][10] =
147 "Sunday", "Monday", "Tuesday", "Wednesday",
148 "Thursday", "Friday", "Saturday"
150 static char const ab_weekday_name
[][4] =
152 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
154 static char const month_name
[][10] =
156 "January", "February", "March", "April", "May", "June",
157 "July", "August", "September", "October", "November", "December"
159 static char const ab_month_name
[][4] =
161 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
162 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
164 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
165 # define HERE_D_FMT "%m/%d/%y"
166 # define HERE_AM_STR "AM"
167 # define HERE_PM_STR "PM"
168 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
169 # define HERE_T_FMT "%H:%M:%S"
171 static const unsigned short int __mon_yday
[2][13] =
174 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
176 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
181 /* We use this code also for the extended locale handling where the
182 function gets as an additional argument the locale which has to be
183 used. To access the values we have to redefine the _NL_CURRENT
185 # define strptime __strptime_l
187 # define _NL_CURRENT(category, item) \
188 (current->values[_NL_ITEM_INDEX (item)].string)
189 # undef _NL_CURRENT_WORD
190 # define _NL_CURRENT_WORD(category, item) \
191 (current->values[_NL_ITEM_INDEX (item)].word)
192 # define LOCALE_PARAM , locale
193 # define LOCALE_ARG , locale
194 # define LOCALE_PARAM_PROTO , __locale_t locale
195 # define LOCALE_PARAM_DECL __locale_t locale;
196 # define HELPER_LOCALE_ARG , current
197 # define ISSPACE(Ch) __isspace_l (Ch, locale)
199 # define LOCALE_PARAM
201 # define LOCALE_PARAM_DECL
202 # define LOCALE_PARAM_PROTO
203 # define HELPER_LOCALE_ARG
204 # define ISSPACE(Ch) isspace (Ch)
211 /* Nonzero if YEAR is a leap year (every 4 years,
212 except every 100th isn't, and every 400th is). */
213 # define __isleap(year) \
214 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
217 /* Compute the day of the week. */
219 day_of_the_week (struct tm
*tm
)
221 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
222 the difference between this data in the one on TM and so determine
224 int corr_year
= 1900 + tm
->tm_year
- (tm
->tm_mon
< 2);
226 + (365 * (tm
->tm_year
- 70))
228 - ((corr_year
/ 4) / 25) + ((corr_year
/ 4) % 25 < 0)
229 + (((corr_year
/ 4) / 25) / 4)
230 + __mon_yday
[0][tm
->tm_mon
]
232 tm
->tm_wday
= ((wday
% 7) + 7) % 7;
235 /* Compute the day of the year. */
237 day_of_the_year (struct tm
*tm
)
239 tm
->tm_yday
= (__mon_yday
[__isleap (1900 + tm
->tm_year
)][tm
->tm_mon
]
240 + (tm
->tm_mday
- 1));
250 __strptime_internal (rp
, fmt
, tm
, decided
, era_cnt LOCALE_PARAM
)
254 enum ptime_locale_status
*decided
;
259 struct locale_data
*const current
= locale
->__locales
[LC_TIME
];
262 const char *rp_backup
;
266 int century
, want_century
;
268 int have_wday
, want_xday
;
270 int have_mon
, have_mday
;
271 int have_uweek
, have_wweek
;
274 struct era_entry
*era
;
283 have_wday
= want_xday
= have_yday
= have_mon
= have_mday
= have_uweek
= 0;
288 /* A white space in the format string matches 0 more or white
289 space in the input string. */
292 while (ISSPACE (*rp
))
298 /* Any character but `%' must be matched by the same character
299 in the iput string. */
302 match_char (*fmt
++, *rp
++);
308 /* We need this for handling the `E' modifier. */
312 /* Make back up of current processing pointer. */
318 /* Match the `%' character itself. */
319 match_char ('%', *rp
++);
323 /* Match day of week. */
324 for (cnt
= 0; cnt
< 7; ++cnt
)
329 if (match_string (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
), rp
))
332 && strcmp (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
),
337 if (match_string (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
), rp
))
340 && strcmp (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
),
341 ab_weekday_name
[cnt
]))
348 && (match_string (weekday_name
[cnt
], rp
)
349 || match_string (ab_weekday_name
[cnt
], rp
)))
356 /* Does not match a weekday name. */
364 /* Match month name. */
365 for (cnt
= 0; cnt
< 12; ++cnt
)
370 if (match_string (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
), rp
))
373 && strcmp (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
),
378 if (match_string (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
), rp
))
381 && strcmp (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
),
388 if (match_string (month_name
[cnt
], rp
)
389 || match_string (ab_month_name
[cnt
], rp
))
396 /* Does not match a month name. */
402 /* Match locale's date and time format. */
406 if (!recursive (_NL_CURRENT (LC_TIME
, D_T_FMT
)))
415 if (*decided
== not &&
416 strcmp (_NL_CURRENT (LC_TIME
, D_T_FMT
), HERE_D_T_FMT
))
424 if (!recursive (HERE_D_T_FMT
))
429 /* Match century number. */
431 get_number (0, 99, 2);
437 /* Match day of month. */
438 get_number (1, 31, 2);
444 if (!recursive ("%Y-%m-%d"))
452 if (!recursive (_NL_CURRENT (LC_TIME
, D_FMT
)))
462 && strcmp (_NL_CURRENT (LC_TIME
, D_FMT
), HERE_D_FMT
))
472 /* Match standard day format. */
473 if (!recursive (HERE_D_FMT
))
479 /* Match hour in 24-hour clock. */
480 get_number (0, 23, 2);
485 /* Match hour in 12-hour clock. GNU extension. */
487 /* Match hour in 12-hour clock. */
488 get_number (1, 12, 2);
489 tm
->tm_hour
= val
% 12;
493 /* Match day number of year. */
494 get_number (1, 366, 3);
495 tm
->tm_yday
= val
- 1;
499 /* Match number of month. */
500 get_number (1, 12, 2);
501 tm
->tm_mon
= val
- 1;
507 get_number (0, 59, 2);
512 /* Match any white space. */
513 while (ISSPACE (*rp
))
517 /* Match locale's equivalent of AM/PM. */
521 if (match_string (_NL_CURRENT (LC_TIME
, AM_STR
), rp
))
523 if (strcmp (_NL_CURRENT (LC_TIME
, AM_STR
), HERE_AM_STR
))
527 if (match_string (_NL_CURRENT (LC_TIME
, PM_STR
), rp
))
529 if (strcmp (_NL_CURRENT (LC_TIME
, PM_STR
), HERE_PM_STR
))
537 if (!match_string (HERE_AM_STR
, rp
))
539 if (match_string (HERE_PM_STR
, rp
))
549 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
558 if (*decided
== not &&
559 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
567 if (!recursive (HERE_T_FMT_AMPM
))
571 if (!recursive ("%H:%M"))
576 /* The number of seconds may be very high so we cannot use
577 the `get_number' macro. Instead read the number
578 character for character and construct the result while
581 if (*rp
< '0' || *rp
> '9')
582 /* We need at least one digit. */
590 while (*rp
>= '0' && *rp
<= '9');
591 if (localtime_r (&secs
, tm
) == NULL
)
592 /* Error in function. */
597 get_number (0, 61, 2);
604 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
613 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
622 if (!recursive (HERE_T_FMT
))
626 get_number (1, 7, 1);
627 tm
->tm_wday
= val
% 7;
631 get_number (0, 99, 2);
632 /* XXX This cannot determine any field in TM. */
635 if (*rp
< '0' || *rp
> '9')
637 /* XXX Ignore the number since we would need some more
638 information to compute a real date. */
641 while (*rp
>= '0' && *rp
<= '9');
644 get_number (0, 53, 2);
649 get_number (0, 53, 2);
654 get_number (0, 53, 2);
655 /* XXX This cannot determine any field in TM without some
659 /* Match number of weekday. */
660 get_number (0, 6, 1);
665 match_year_in_century
:
666 /* Match year within century. */
667 get_number (0, 99, 2);
668 /* The "Year 2000: The Millennium Rollover" paper suggests that
669 values in the range 69-99 refer to the twentieth century. */
670 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
671 /* Indicate that we want to use the century, if specified. */
676 /* Match year including century number. */
677 get_number (0, 9999, 4);
678 tm
->tm_year
= val
- 1900;
683 /* XXX How to handle this? */
686 /* We recognize two formats: if two digits are given, these
687 specify hours. If fours digits are used, minutes are
696 if (*rp
!= '+' && *rp
!= '-')
700 while (n
< 4 && *rp
>= '0' && *rp
<= '9')
702 val
= val
* 10 + *rp
++ - '0';
708 /* Only two or four digits recognized. */
712 /* We have to convert the minutes into decimal. */
715 val
= (val
/ 100) * 100 + ((val
% 100) * 50) / 30;
719 #if defined _LIBC || HAVE_TM_GMTOFF
720 tm
->tm_gmtoff
= (val
* 3600) / 100;
722 tm
->tm_gmtoff
= -tm
->tm_gmtoff
;
731 /* Match locale's alternate date and time format. */
734 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
737 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
739 if (!recursive (fmt
))
748 if (strcmp (fmt
, HERE_D_T_FMT
))
755 /* The C locale has no era information, so use the
756 normal representation. */
757 if (!recursive (HERE_D_T_FMT
))
766 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
767 if (era
!= NULL
&& match_string (era
->era_name
, rp
))
776 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
777 _NL_TIME_ERA_NUM_ENTRIES
);
778 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
779 ++era_cnt
, rp
= rp_backup
)
781 era
= _nl_select_era_entry (era_cnt
783 if (era
!= NULL
&& match_string (era
->era_name
, rp
))
789 if (era_cnt
!= (int) num_eras
)
798 /* The C locale has no era information, so use the
799 normal representation. */
804 get_number(0, 9999, 4);
812 assert (*decided
== loc
);
814 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
818 int delta
= ((tm
->tm_year
- era
->offset
)
819 * era
->absolute_direction
);
821 && delta
< (((int64_t) era
->stop_date
[0]
822 - (int64_t) era
->start_date
[0])
823 * era
->absolute_direction
));
831 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
832 _NL_TIME_ERA_NUM_ENTRIES
);
833 for (era_cnt
= 0; era_cnt
< (int) num_eras
; ++era_cnt
)
835 era
= _nl_select_era_entry (era_cnt
839 int delta
= ((tm
->tm_year
- era
->offset
)
840 * era
->absolute_direction
);
842 && delta
< (((int64_t) era
->stop_date
[0]
843 - (int64_t) era
->start_date
[0])
844 * era
->absolute_direction
))
851 if (era_cnt
!= (int) num_eras
)
861 goto match_year_in_century
;
865 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
866 _NL_TIME_ERA_NUM_ENTRIES
);
867 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
868 ++era_cnt
, rp
= rp_backup
)
870 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
871 if (era
!= NULL
&& recursive (era
->era_format
))
874 if (era_cnt
== (int) num_eras
)
891 get_number (0, 9999, 4);
892 tm
->tm_year
= val
- 1900;
899 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
902 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
904 if (!recursive (fmt
))
913 if (strcmp (fmt
, HERE_D_FMT
))
919 if (!recursive (HERE_D_FMT
))
925 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
928 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
930 if (!recursive (fmt
))
939 if (strcmp (fmt
, HERE_T_FMT
))
945 if (!recursive (HERE_T_FMT
))
953 /* We have no information about the era format. Just use
954 the normal format. */
955 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
956 && *fmt
!= 'x' && *fmt
!= 'X')
957 /* This is an illegal format. */
967 /* Match day of month using alternate numeric symbols. */
968 get_alt_number (1, 31, 2);
974 /* Match hour in 24-hour clock using alternate numeric
976 get_alt_number (0, 23, 2);
981 /* Match hour in 12-hour clock using alternate numeric
983 get_alt_number (1, 12, 2);
984 tm
->tm_hour
= val
% 12;
988 /* Match month using alternate numeric symbols. */
989 get_alt_number (1, 12, 2);
990 tm
->tm_mon
= val
- 1;
995 /* Match minutes using alternate numeric symbols. */
996 get_alt_number (0, 59, 2);
1000 /* Match seconds using alternate numeric symbols. */
1001 get_alt_number (0, 61, 2);
1005 get_alt_number (0, 53, 2);
1010 get_alt_number (0, 53, 2);
1015 get_alt_number (0, 53, 2);
1016 /* XXX This cannot determine any field in TM without
1017 further information. */
1020 /* Match number of weekday using alternate numeric symbols. */
1021 get_alt_number (0, 6, 1);
1026 /* Match year within century using alternate numeric symbols. */
1027 get_alt_number (0, 99, 2);
1028 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
1040 if (have_I
&& is_pm
)
1046 tm
->tm_year
= tm
->tm_year
% 100 + (century
- 19) * 100;
1048 /* Only the century, but not the year. Strange, but so be it. */
1049 tm
->tm_year
= (century
- 19) * 100;
1055 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
1059 tm
->tm_year
= (era
->start_date
[0]
1060 + ((tm
->tm_year
- era
->offset
)
1061 * era
->absolute_direction
));
1063 /* Era start year assumed. */
1064 tm
->tm_year
= era
->start_date
[0];
1070 /* No era found but we have seen an E modifier. Rectify some
1072 if (want_century
&& century
== -1 && tm
->tm_year
< 69)
1076 if (want_xday
&& !have_wday
)
1078 if ( !(have_mon
&& have_mday
) && have_yday
)
1080 /* We don't have tm_mon and/or tm_mday, compute them. */
1082 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
] <= tm
->tm_yday
)
1085 tm
->tm_mon
= t_mon
- 1;
1089 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
1091 day_of_the_week (tm
);
1094 if (want_xday
&& !have_yday
)
1095 day_of_the_year (tm
);
1097 if ((have_uweek
|| have_wweek
) && have_wday
)
1099 int save_wday
= tm
->tm_wday
;
1100 int save_mday
= tm
->tm_mday
;
1101 int save_mon
= tm
->tm_mon
;
1102 int w_offset
= have_uweek
? 0 : 1;
1106 day_of_the_week (tm
);
1108 tm
->tm_mday
= save_mday
;
1110 tm
->tm_mon
= save_mon
;
1113 tm
->tm_yday
= ((7 - (tm
->tm_wday
- w_offset
)) % 7
1115 + save_wday
- w_offset
);
1117 if (!have_mday
|| !have_mon
)
1120 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
]
1124 tm
->tm_mon
= t_mon
- 1;
1128 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
1131 tm
->tm_wday
= save_wday
;
1139 strptime (buf
, format
, tm LOCALE_PARAM
)
1145 enum ptime_locale_status decided
;
1152 return __strptime_internal (buf
, format
, tm
, &decided
, -1 LOCALE_ARG
);
1155 weak_alias (__strptime_l
, strptime_l
)