1 /* Copyright (C) 2002, 2004 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 (*decided != raw) \
103 val = _nl_parse_alt_digit (&rp HELPER_LOCALE_ARG); \
104 if (val == -1 && *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, \
127 decided, era_cnt LOCALE_ARG)) != NULL)
131 /* This is defined in locale/C-time.c in the GNU libc. */
132 extern const struct locale_data _nl_C_LC_TIME attribute_hidden
;
134 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
135 # define ab_weekday_name \
136 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
137 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
138 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
139 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
140 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
141 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
142 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
143 # define HERE_T_FMT_AMPM \
144 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
145 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
147 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
149 static char const weekday_name
[][10] =
151 "Sunday", "Monday", "Tuesday", "Wednesday",
152 "Thursday", "Friday", "Saturday"
154 static char const ab_weekday_name
[][4] =
156 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
158 static char const month_name
[][10] =
160 "January", "February", "March", "April", "May", "June",
161 "July", "August", "September", "October", "November", "December"
163 static char const ab_month_name
[][4] =
165 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
166 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
168 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
169 # define HERE_D_FMT "%m/%d/%y"
170 # define HERE_AM_STR "AM"
171 # define HERE_PM_STR "PM"
172 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
173 # define HERE_T_FMT "%H:%M:%S"
175 static const unsigned short int __mon_yday
[2][13] =
178 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
180 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
185 /* We use this code also for the extended locale handling where the
186 function gets as an additional argument the locale which has to be
187 used. To access the values we have to redefine the _NL_CURRENT
189 # define strptime __strptime_l
191 # define _NL_CURRENT(category, item) \
192 (current->values[_NL_ITEM_INDEX (item)].string)
193 # undef _NL_CURRENT_WORD
194 # define _NL_CURRENT_WORD(category, item) \
195 (current->values[_NL_ITEM_INDEX (item)].word)
196 # define LOCALE_PARAM , locale
197 # define LOCALE_ARG , locale
198 # define LOCALE_PARAM_PROTO , __locale_t locale
199 # define LOCALE_PARAM_DECL __locale_t locale;
200 # define HELPER_LOCALE_ARG , current
201 # define ISSPACE(Ch) __isspace_l (Ch, locale)
203 # define LOCALE_PARAM
205 # define LOCALE_PARAM_DECL
206 # define LOCALE_PARAM_PROTO
207 # define HELPER_LOCALE_ARG
208 # define ISSPACE(Ch) isspace (Ch)
215 /* Nonzero if YEAR is a leap year (every 4 years,
216 except every 100th isn't, and every 400th is). */
217 # define __isleap(year) \
218 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
221 /* Compute the day of the week. */
223 day_of_the_week (struct tm
*tm
)
225 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
226 the difference between this data in the one on TM and so determine
228 int corr_year
= 1900 + tm
->tm_year
- (tm
->tm_mon
< 2);
230 + (365 * (tm
->tm_year
- 70))
232 - ((corr_year
/ 4) / 25) + ((corr_year
/ 4) % 25 < 0)
233 + (((corr_year
/ 4) / 25) / 4)
234 + __mon_yday
[0][tm
->tm_mon
]
236 tm
->tm_wday
= ((wday
% 7) + 7) % 7;
239 /* Compute the day of the year. */
241 day_of_the_year (struct tm
*tm
)
243 tm
->tm_yday
= (__mon_yday
[__isleap (1900 + tm
->tm_year
)][tm
->tm_mon
]
244 + (tm
->tm_mday
- 1));
254 __strptime_internal (rp
, fmt
, tm
, decided
, era_cnt LOCALE_PARAM
)
258 enum ptime_locale_status
*decided
;
263 struct locale_data
*const current
= locale
->__locales
[LC_TIME
];
266 const char *rp_backup
;
270 int century
, want_century
;
272 int have_wday
, want_xday
;
274 int have_mon
, have_mday
;
275 int have_uweek
, have_wweek
;
278 struct era_entry
*era
;
287 have_wday
= want_xday
= have_yday
= have_mon
= have_mday
= have_uweek
= 0;
292 /* A white space in the format string matches 0 more or white
293 space in the input string. */
296 while (ISSPACE (*rp
))
302 /* Any character but `%' must be matched by the same character
303 in the iput string. */
306 match_char (*fmt
++, *rp
++);
312 /* We need this for handling the `E' modifier. */
316 /* Make back up of current processing pointer. */
322 /* Match the `%' character itself. */
323 match_char ('%', *rp
++);
327 /* Match day of week. */
328 for (cnt
= 0; cnt
< 7; ++cnt
)
333 if (match_string (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
), rp
))
336 && strcmp (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
),
341 if (match_string (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
), rp
))
344 && strcmp (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
),
345 ab_weekday_name
[cnt
]))
352 && (match_string (weekday_name
[cnt
], rp
)
353 || match_string (ab_weekday_name
[cnt
], rp
)))
360 /* Does not match a weekday name. */
368 /* Match month name. */
369 for (cnt
= 0; cnt
< 12; ++cnt
)
374 if (match_string (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
), rp
))
377 && strcmp (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
),
382 if (match_string (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
), rp
))
385 && strcmp (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
),
392 if (match_string (month_name
[cnt
], rp
)
393 || match_string (ab_month_name
[cnt
], rp
))
400 /* Does not match a month name. */
406 /* Match locale's date and time format. */
410 if (!recursive (_NL_CURRENT (LC_TIME
, D_T_FMT
)))
419 if (*decided
== not &&
420 strcmp (_NL_CURRENT (LC_TIME
, D_T_FMT
), HERE_D_T_FMT
))
428 if (!recursive (HERE_D_T_FMT
))
433 /* Match century number. */
435 get_number (0, 99, 2);
441 /* Match day of month. */
442 get_number (1, 31, 2);
448 if (!recursive ("%Y-%m-%d"))
456 if (!recursive (_NL_CURRENT (LC_TIME
, D_FMT
)))
466 && strcmp (_NL_CURRENT (LC_TIME
, D_FMT
), HERE_D_FMT
))
476 /* Match standard day format. */
477 if (!recursive (HERE_D_FMT
))
483 /* Match hour in 24-hour clock. */
484 get_number (0, 23, 2);
489 /* Match hour in 12-hour clock. GNU extension. */
491 /* Match hour in 12-hour clock. */
492 get_number (1, 12, 2);
493 tm
->tm_hour
= val
% 12;
497 /* Match day number of year. */
498 get_number (1, 366, 3);
499 tm
->tm_yday
= val
- 1;
503 /* Match number of month. */
504 get_number (1, 12, 2);
505 tm
->tm_mon
= val
- 1;
511 get_number (0, 59, 2);
516 /* Match any white space. */
517 while (ISSPACE (*rp
))
521 /* Match locale's equivalent of AM/PM. */
525 if (match_string (_NL_CURRENT (LC_TIME
, AM_STR
), rp
))
527 if (strcmp (_NL_CURRENT (LC_TIME
, AM_STR
), HERE_AM_STR
))
531 if (match_string (_NL_CURRENT (LC_TIME
, PM_STR
), rp
))
533 if (strcmp (_NL_CURRENT (LC_TIME
, PM_STR
), HERE_PM_STR
))
541 if (!match_string (HERE_AM_STR
, rp
))
542 if (match_string (HERE_PM_STR
, rp
))
551 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
560 if (*decided
== not &&
561 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
569 if (!recursive (HERE_T_FMT_AMPM
))
573 if (!recursive ("%H:%M"))
578 /* The number of seconds may be very high so we cannot use
579 the `get_number' macro. Instead read the number
580 character for character and construct the result while
583 if (*rp
< '0' || *rp
> '9')
584 /* We need at least one digit. */
592 while (*rp
>= '0' && *rp
<= '9');
594 if (localtime_r (&secs
, tm
) == NULL
)
595 /* Error in function. */
600 get_number (0, 61, 2);
607 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
616 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
625 if (!recursive (HERE_T_FMT
))
629 get_number (1, 7, 1);
630 tm
->tm_wday
= val
% 7;
634 get_number (0, 99, 2);
635 /* XXX This cannot determine any field in TM. */
638 if (*rp
< '0' || *rp
> '9')
640 /* XXX Ignore the number since we would need some more
641 information to compute a real date. */
644 while (*rp
>= '0' && *rp
<= '9');
647 get_number (0, 53, 2);
652 get_number (0, 53, 2);
657 get_number (0, 53, 2);
658 /* XXX This cannot determine any field in TM without some
662 /* Match number of weekday. */
663 get_number (0, 6, 1);
668 match_year_in_century
:
669 /* Match year within century. */
670 get_number (0, 99, 2);
671 /* The "Year 2000: The Millennium Rollover" paper suggests that
672 values in the range 69-99 refer to the twentieth century. */
673 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
674 /* Indicate that we want to use the century, if specified. */
679 /* Match year including century number. */
680 get_number (0, 9999, 4);
681 tm
->tm_year
= val
- 1900;
686 /* XXX How to handle this? */
693 /* Match locale's alternate date and time format. */
696 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
699 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
701 if (!recursive (fmt
))
710 if (strcmp (fmt
, HERE_D_T_FMT
))
717 /* The C locale has no era information, so use the
718 normal representation. */
719 if (!recursive (HERE_D_T_FMT
))
728 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
729 if (era
!= NULL
&& match_string (era
->era_name
, rp
))
738 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
739 _NL_TIME_ERA_NUM_ENTRIES
);
740 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
741 ++era_cnt
, rp
= rp_backup
)
743 era
= _nl_select_era_entry (era_cnt
745 if (era
!= NULL
&& match_string (era
->era_name
, rp
))
751 if (era_cnt
!= (int) num_eras
)
760 /* The C locale has no era information, so use the
761 normal representation. */
766 get_number(0, 9999, 4);
774 assert (*decided
== loc
);
776 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
780 int delta
= ((tm
->tm_year
- era
->offset
)
781 * era
->absolute_direction
);
783 && delta
< (((int64_t) era
->stop_date
[0]
784 - (int64_t) era
->start_date
[0])
785 * era
->absolute_direction
));
793 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
794 _NL_TIME_ERA_NUM_ENTRIES
);
795 for (era_cnt
= 0; era_cnt
< (int) num_eras
; ++era_cnt
)
797 era
= _nl_select_era_entry (era_cnt
801 int delta
= ((tm
->tm_year
- era
->offset
)
802 * era
->absolute_direction
);
804 && delta
< (((int64_t) era
->stop_date
[0]
805 - (int64_t) era
->start_date
[0])
806 * era
->absolute_direction
))
813 if (era_cnt
!= (int) num_eras
)
823 goto match_year_in_century
;
827 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
828 _NL_TIME_ERA_NUM_ENTRIES
);
829 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
830 ++era_cnt
, rp
= rp_backup
)
832 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
833 if (era
!= NULL
&& recursive (era
->era_format
))
836 if (era_cnt
== (int) num_eras
)
853 get_number (0, 9999, 4);
854 tm
->tm_year
= val
- 1900;
861 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
864 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
866 if (!recursive (fmt
))
875 if (strcmp (fmt
, HERE_D_FMT
))
881 if (!recursive (HERE_D_FMT
))
887 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
890 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
892 if (!recursive (fmt
))
901 if (strcmp (fmt
, HERE_T_FMT
))
907 if (!recursive (HERE_T_FMT
))
915 /* We have no information about the era format. Just use
916 the normal format. */
917 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
918 && *fmt
!= 'x' && *fmt
!= 'X')
919 /* This is an illegal format. */
929 /* Match day of month using alternate numeric symbols. */
930 get_alt_number (1, 31, 2);
936 /* Match hour in 24-hour clock using alternate numeric
938 get_alt_number (0, 23, 2);
943 /* Match hour in 12-hour clock using alternate numeric
945 get_alt_number (1, 12, 2);
946 tm
->tm_hour
= val
% 12;
950 /* Match month using alternate numeric symbols. */
951 get_alt_number (1, 12, 2);
952 tm
->tm_mon
= val
- 1;
957 /* Match minutes using alternate numeric symbols. */
958 get_alt_number (0, 59, 2);
962 /* Match seconds using alternate numeric symbols. */
963 get_alt_number (0, 61, 2);
967 get_alt_number (0, 53, 2);
972 get_alt_number (0, 53, 2);
977 get_alt_number (0, 53, 2);
978 /* XXX This cannot determine any field in TM without
979 further information. */
982 /* Match number of weekday using alternate numeric symbols. */
983 get_alt_number (0, 6, 1);
988 /* Match year within century using alternate numeric symbols. */
989 get_alt_number (0, 99, 2);
990 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
1002 if (have_I
&& is_pm
)
1008 tm
->tm_year
= tm
->tm_year
% 100 + (century
- 19) * 100;
1010 /* Only the century, but not the year. Strange, but so be it. */
1011 tm
->tm_year
= (century
- 19) * 100;
1016 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
1020 tm
->tm_year
= (era
->start_date
[0]
1021 + ((tm
->tm_year
- era
->offset
)
1022 * era
->absolute_direction
));
1024 /* Era start year assumed. */
1025 tm
->tm_year
= era
->start_date
[0];
1030 /* No era found but we have seen an E modifier. Rectify some
1032 if (want_century
&& century
== -1 && tm
->tm_year
< 69)
1036 if (want_xday
&& !have_wday
)
1038 if ( !(have_mon
&& have_mday
) && have_yday
)
1040 /* We don't have tm_mon and/or tm_mday, compute them. */
1042 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
] <= tm
->tm_yday
)
1045 tm
->tm_mon
= t_mon
- 1;
1049 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
1051 day_of_the_week (tm
);
1054 if (want_xday
&& !have_yday
)
1055 day_of_the_year (tm
);
1057 if ((have_uweek
|| have_wweek
) && have_wday
)
1059 int save_wday
= tm
->tm_wday
;
1060 int save_mday
= tm
->tm_mday
;
1061 int save_mon
= tm
->tm_mon
;
1062 int w_offset
= have_uweek
? 0 : 1;
1066 day_of_the_week (tm
);
1068 tm
->tm_mday
= save_mday
;
1070 tm
->tm_mon
= save_mon
;
1073 tm
->tm_yday
= ((7 - (tm
->tm_wday
- w_offset
)) % 7
1075 + save_wday
- w_offset
);
1077 if (!have_mday
|| !have_mon
)
1080 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
]
1084 tm
->tm_mon
= t_mon
- 1;
1088 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
1091 tm
->tm_wday
= save_wday
;
1099 strptime (buf
, format
, tm LOCALE_PARAM
)
1105 enum ptime_locale_status decided
;
1112 return __strptime_internal (buf
, format
, tm
, &decided
, -1 LOCALE_ARG
);
1116 weak_alias (__strptime_l
, strptime_l
)