1 /* Convert a string representation of time to a time value.
2 Copyright (C) 1996-2000, 2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 /* XXX This version of the implementation is not really complete.
22 Some of the fields cannot add information alone. But if seeing
23 some of them in the same format (such as year, week and weekday)
24 this is enough information for determining the date. */
37 # include "../locale/localeinfo.h"
42 # if defined __GNUC__ || (defined __STDC__ && __STDC__)
43 # define __P(args) args
50 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
52 # define localtime_r __localtime_r
54 /* Approximate localtime_r as best we can in its absence. */
55 # define localtime_r my_localtime_r
56 static struct tm
*localtime_r
__P ((const time_t *, struct tm
*));
62 struct tm
*l
= localtime (t
);
69 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
72 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
73 #if defined __GNUC__ && __GNUC__ >= 2
74 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
75 # define match_string(cs1, s2) \
76 ({ size_t len = strlen (cs1); \
77 int result = __strncasecmp_l ((cs1), (s2), len, locale) == 0; \
78 if (result) (s2) += len; \
81 # define match_string(cs1, s2) \
82 ({ size_t len = strlen (cs1); \
83 int result = strncasecmp ((cs1), (s2), len) == 0; \
84 if (result) (s2) += len; \
88 /* Oh come on. Get a reasonable compiler. */
89 # define match_string(cs1, s2) \
90 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
92 /* We intentionally do not use isdigit() for testing because this will
93 lead to problems with the wide character version. */
94 #define get_number(from, to, n) \
100 if (*rp < '0' || *rp > '9') \
104 val += *rp++ - '0'; \
105 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
106 if (val < from || val > to) \
110 # define get_alt_number(from, to, n) \
112 __label__ do_normal; \
114 if (*decided != raw) \
116 val = _nl_parse_alt_digit (&rp HELPER_LOCALE_ARG); \
117 if (val == -1 && *decided != loc) \
122 if (val < from || val > to) \
128 get_number (from, to, n); \
133 # define get_alt_number(from, to, n) \
134 /* We don't have the alternate representation. */ \
135 get_number(from, to, n)
137 #define recursive(new_fmt) \
138 (*(new_fmt) != '\0' \
139 && (rp = strptime_internal (rp, (new_fmt), tm, \
140 decided, era_cnt LOCALE_ARG)) != NULL)
144 /* This is defined in locale/C-time.c in the GNU libc. */
145 extern const struct locale_data _nl_C_LC_TIME attribute_hidden
;
147 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
148 # define ab_weekday_name \
149 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
150 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
151 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
152 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
153 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
154 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
155 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
156 # define HERE_T_FMT_AMPM \
157 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
158 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
160 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
162 static char const weekday_name
[][10] =
164 "Sunday", "Monday", "Tuesday", "Wednesday",
165 "Thursday", "Friday", "Saturday"
167 static char const ab_weekday_name
[][4] =
169 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
171 static char const month_name
[][10] =
173 "January", "February", "March", "April", "May", "June",
174 "July", "August", "September", "October", "November", "December"
176 static char const ab_month_name
[][4] =
178 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
179 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
181 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
182 # define HERE_D_FMT "%m/%d/%y"
183 # define HERE_AM_STR "AM"
184 # define HERE_PM_STR "PM"
185 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
186 # define HERE_T_FMT "%H:%M:%S"
188 static const unsigned short int __mon_yday
[2][13] =
191 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
193 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
197 #if defined _LIBC && defined USE_IN_EXTENDED_LOCALE_MODEL
198 /* We use this code also for the extended locale handling where the
199 function gets as an additional argument the locale which has to be
200 used. To access the values we have to redefine the _NL_CURRENT
202 # define strptime __strptime_l
204 # define _NL_CURRENT(category, item) \
205 (current->values[_NL_ITEM_INDEX (item)].string)
206 # undef _NL_CURRENT_WORD
207 # define _NL_CURRENT_WORD(category, item) \
208 (current->values[_NL_ITEM_INDEX (item)].word)
209 # define LOCALE_PARAM , locale
210 # define LOCALE_ARG , locale
211 # define LOCALE_PARAM_PROTO , __locale_t locale
212 # define LOCALE_PARAM_DECL __locale_t locale;
213 # define HELPER_LOCALE_ARG , current
214 # define ISSPACE(Ch) __isspace_l (Ch, locale)
216 # define LOCALE_PARAM
218 # define LOCALE_PARAM_DECL
219 # define LOCALE_PARAM_PROTO
221 # define HELPER_LOCALE_ARG , _NL_CURRENT_DATA (LC_TIME)
223 # define HELPER_LOCALE_ARG
225 # define ISSPACE(Ch) isspace (Ch)
229 /* Status of lookup: do we use the locale data or the raw data? */
230 enum locale_status
{ not, loc
, raw
};
234 /* Nonzero if YEAR is a leap year (every 4 years,
235 except every 100th isn't, and every 400th is). */
236 # define __isleap(year) \
237 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
240 /* Compute the day of the week. */
242 day_of_the_week (struct tm
*tm
)
244 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
245 the difference between this data in the one on TM and so determine
247 int corr_year
= 1900 + tm
->tm_year
- (tm
->tm_mon
< 2);
249 + (365 * (tm
->tm_year
- 70))
251 - ((corr_year
/ 4) / 25) + ((corr_year
/ 4) % 25 < 0)
252 + (((corr_year
/ 4) / 25) / 4)
253 + __mon_yday
[0][tm
->tm_mon
]
255 tm
->tm_wday
= ((wday
% 7) + 7) % 7;
258 /* Compute the day of the year. */
260 day_of_the_year (struct tm
*tm
)
262 tm
->tm_yday
= (__mon_yday
[__isleap (1900 + tm
->tm_year
)][tm
->tm_mon
]
263 + (tm
->tm_mday
- 1));
271 strptime_internal
__P ((const char *rp
, const char *fmt
, struct tm
*tm
,
272 enum locale_status
*decided
, int era_cnt
273 LOCALE_PARAM_PROTO
));
279 strptime_internal (rp
, fmt
, tm
, decided
, era_cnt LOCALE_PARAM
)
283 enum locale_status
*decided
;
287 #if defined _LIBC && defined USE_IN_EXTENDED_LOCALE_MODEL
288 struct locale_data
*const current
= locale
->__locales
[LC_TIME
];
291 const char *rp_backup
;
295 int century
, want_century
;
297 int have_wday
, want_xday
;
299 int have_mon
, have_mday
;
300 int have_uweek
, have_wweek
;
303 struct era_entry
*era
;
312 have_wday
= want_xday
= have_yday
= have_mon
= have_mday
= have_uweek
= 0;
317 /* A white space in the format string matches 0 more or white
318 space in the input string. */
321 while (ISSPACE (*rp
))
327 /* Any character but `%' must be matched by the same character
328 in the iput string. */
331 match_char (*fmt
++, *rp
++);
337 /* We need this for handling the `E' modifier. */
341 /* Make back up of current processing pointer. */
347 /* Match the `%' character itself. */
348 match_char ('%', *rp
++);
352 /* Match day of week. */
353 for (cnt
= 0; cnt
< 7; ++cnt
)
358 if (match_string (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
), rp
))
361 && strcmp (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
),
366 if (match_string (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
), rp
))
369 && strcmp (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
),
370 ab_weekday_name
[cnt
]))
377 && (match_string (weekday_name
[cnt
], rp
)
378 || match_string (ab_weekday_name
[cnt
], rp
)))
385 /* Does not match a weekday name. */
393 /* Match month name. */
394 for (cnt
= 0; cnt
< 12; ++cnt
)
399 if (match_string (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
), rp
))
402 && strcmp (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
),
407 if (match_string (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
), rp
))
410 && strcmp (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
),
417 if (match_string (month_name
[cnt
], rp
)
418 || match_string (ab_month_name
[cnt
], rp
))
425 /* Does not match a month name. */
431 /* Match locale's date and time format. */
435 if (!recursive (_NL_CURRENT (LC_TIME
, D_T_FMT
)))
444 if (*decided
== not &&
445 strcmp (_NL_CURRENT (LC_TIME
, D_T_FMT
), HERE_D_T_FMT
))
453 if (!recursive (HERE_D_T_FMT
))
458 /* Match century number. */
460 get_number (0, 99, 2);
466 /* Match day of month. */
467 get_number (1, 31, 2);
473 if (!recursive ("%Y-%m-%d"))
481 if (!recursive (_NL_CURRENT (LC_TIME
, D_FMT
)))
491 && strcmp (_NL_CURRENT (LC_TIME
, D_FMT
), HERE_D_FMT
))
501 /* Match standard day format. */
502 if (!recursive (HERE_D_FMT
))
508 /* Match hour in 24-hour clock. */
509 get_number (0, 23, 2);
514 /* Match hour in 12-hour clock. GNU extension. */
516 /* Match hour in 12-hour clock. */
517 get_number (1, 12, 2);
518 tm
->tm_hour
= val
% 12;
522 /* Match day number of year. */
523 get_number (1, 366, 3);
524 tm
->tm_yday
= val
- 1;
528 /* Match number of month. */
529 get_number (1, 12, 2);
530 tm
->tm_mon
= val
- 1;
536 get_number (0, 59, 2);
541 /* Match any white space. */
542 while (ISSPACE (*rp
))
546 /* Match locale's equivalent of AM/PM. */
550 if (match_string (_NL_CURRENT (LC_TIME
, AM_STR
), rp
))
552 if (strcmp (_NL_CURRENT (LC_TIME
, AM_STR
), HERE_AM_STR
))
556 if (match_string (_NL_CURRENT (LC_TIME
, PM_STR
), rp
))
558 if (strcmp (_NL_CURRENT (LC_TIME
, PM_STR
), HERE_PM_STR
))
566 if (!match_string (HERE_AM_STR
, rp
))
567 if (match_string (HERE_PM_STR
, rp
))
576 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
585 if (*decided
== not &&
586 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
594 if (!recursive (HERE_T_FMT_AMPM
))
598 if (!recursive ("%H:%M"))
603 /* The number of seconds may be very high so we cannot use
604 the `get_number' macro. Instead read the number
605 character for character and construct the result while
608 if (*rp
< '0' || *rp
> '9')
609 /* We need at least one digit. */
617 while (*rp
>= '0' && *rp
<= '9');
619 if (localtime_r (&secs
, tm
) == NULL
)
620 /* Error in function. */
625 get_number (0, 61, 2);
632 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
641 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
650 if (!recursive (HERE_T_FMT
))
654 get_number (1, 7, 1);
655 tm
->tm_wday
= val
% 7;
659 get_number (0, 99, 2);
660 /* XXX This cannot determine any field in TM. */
663 if (*rp
< '0' || *rp
> '9')
665 /* XXX Ignore the number since we would need some more
666 information to compute a real date. */
669 while (*rp
>= '0' && *rp
<= '9');
672 get_number (0, 53, 2);
677 get_number (0, 53, 2);
682 get_number (0, 53, 2);
683 /* XXX This cannot determine any field in TM without some
687 /* Match number of weekday. */
688 get_number (0, 6, 1);
693 match_year_in_century
:
694 /* Match year within century. */
695 get_number (0, 99, 2);
696 /* The "Year 2000: The Millennium Rollover" paper suggests that
697 values in the range 69-99 refer to the twentieth century. */
698 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
699 /* Indicate that we want to use the century, if specified. */
704 /* Match year including century number. */
705 get_number (0, 9999, 4);
706 tm
->tm_year
= val
- 1900;
711 /* XXX How to handle this? */
718 /* Match locale's alternate date and time format. */
721 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
724 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
726 if (!recursive (fmt
))
735 if (strcmp (fmt
, HERE_D_T_FMT
))
742 /* The C locale has no era information, so use the
743 normal representation. */
744 if (!recursive (HERE_D_T_FMT
))
753 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
754 if (era
!= NULL
&& match_string (era
->era_name
, rp
))
764 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
765 _NL_TIME_ERA_NUM_ENTRIES
);
766 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
767 ++era_cnt
, rp
= rp_backup
)
769 era
= _nl_select_era_entry (era_cnt
771 if (era
!= NULL
&& match_string (era
->era_name
, rp
))
777 if (era_cnt
== (int) num_eras
)
789 /* The C locale has no era information, so use the
790 normal representation. */
794 goto match_year_in_century
;
796 get_number(0, 9999, 4);
805 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
806 _NL_TIME_ERA_NUM_ENTRIES
);
807 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
808 ++era_cnt
, rp
= rp_backup
)
810 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
811 if (era
!= NULL
&& recursive (era
->era_format
))
814 if (era_cnt
== (int) num_eras
)
831 get_number (0, 9999, 4);
832 tm
->tm_year
= val
- 1900;
839 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
842 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
844 if (!recursive (fmt
))
853 if (strcmp (fmt
, HERE_D_FMT
))
859 if (!recursive (HERE_D_FMT
))
865 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
868 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
870 if (!recursive (fmt
))
879 if (strcmp (fmt
, HERE_T_FMT
))
885 if (!recursive (HERE_T_FMT
))
893 /* We have no information about the era format. Just use
894 the normal format. */
895 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
896 && *fmt
!= 'x' && *fmt
!= 'X')
897 /* This is an illegal format. */
907 /* Match day of month using alternate numeric symbols. */
908 get_alt_number (1, 31, 2);
914 /* Match hour in 24-hour clock using alternate numeric
916 get_alt_number (0, 23, 2);
921 /* Match hour in 12-hour clock using alternate numeric
923 get_alt_number (1, 12, 2);
924 tm
->tm_hour
= val
% 12;
928 /* Match month using alternate numeric symbols. */
929 get_alt_number (1, 12, 2);
930 tm
->tm_mon
= val
- 1;
935 /* Match minutes using alternate numeric symbols. */
936 get_alt_number (0, 59, 2);
940 /* Match seconds using alternate numeric symbols. */
941 get_alt_number (0, 61, 2);
945 get_alt_number (0, 53, 2);
950 get_alt_number (0, 53, 2);
955 get_alt_number (0, 53, 2);
956 /* XXX This cannot determine any field in TM without
957 further information. */
960 /* Match number of weekday using alternate numeric symbols. */
961 get_alt_number (0, 6, 1);
966 /* Match year within century using alternate numeric symbols. */
967 get_alt_number (0, 99, 2);
968 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
986 tm
->tm_year
= tm
->tm_year
% 100 + (century
- 19) * 100;
988 /* Only the century, but not the year. Strange, but so be it. */
989 tm
->tm_year
= (century
- 19) * 100;
994 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
998 tm
->tm_year
= (era
->start_date
[0]
999 + ((tm
->tm_year
- era
->offset
)
1000 * era
->absolute_direction
));
1002 /* Era start year assumed. */
1003 tm
->tm_year
= era
->start_date
[0];
1008 /* No era found but we have seen an E modifier. Rectify some
1010 if (want_century
&& century
== -1 && tm
->tm_year
< 69)
1014 if (want_xday
&& !have_wday
)
1016 if ( !(have_mon
&& have_mday
) && have_yday
)
1018 /* We don't have tm_mon and/or tm_mday, compute them. */
1020 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
] <= tm
->tm_yday
)
1023 tm
->tm_mon
= t_mon
- 1;
1027 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
1029 day_of_the_week (tm
);
1032 if (want_xday
&& !have_yday
)
1033 day_of_the_year (tm
);
1035 if ((have_uweek
|| have_wweek
) && have_wday
)
1037 int save_wday
= tm
->tm_wday
;
1038 int save_mday
= tm
->tm_mday
;
1039 int save_mon
= tm
->tm_mon
;
1040 int w_offset
= have_uweek
? 0 : 1;
1044 day_of_the_week (tm
);
1046 tm
->tm_mday
= save_mday
;
1048 tm
->tm_mon
= save_mon
;
1051 tm
->tm_yday
= ((7 - (tm
->tm_wday
- w_offset
)) % 7
1053 + save_wday
- w_offset
);
1055 if (!have_mday
|| !have_mon
)
1058 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
]
1062 tm
->tm_mon
= t_mon
- 1;
1066 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
1069 tm
->tm_wday
= save_wday
;
1077 strptime (buf
, format
, tm LOCALE_PARAM
)
1083 enum locale_status decided
;
1090 return strptime_internal (buf
, format
, tm
, &decided
, -1 LOCALE_ARG
);
1092 #if defined _LIBC && !defined USE_IN_EXTENDED_LOCALE_MODEL
1093 libc_hidden_def (strptime
)