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 # define match_string(cs1, s2) \
75 ({ size_t len = strlen (cs1); \
76 int result = strncasecmp ((cs1), (s2), len) == 0; \
77 if (result) (s2) += len; \
80 /* Oh come on. Get a reasonable compiler. */
81 # define match_string(cs1, s2) \
82 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
84 /* We intentionally do not use isdigit() for testing because this will
85 lead to problems with the wide character version. */
86 #define get_number(from, to, n) \
92 if (*rp < '0' || *rp > '9') \
97 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
98 if (val < from || val > to) \
102 # define get_alt_number(from, to, n) \
104 __label__ do_normal; \
106 if (*decided != raw) \
108 val = _nl_parse_alt_digit (&rp HELPER_LOCALE_ARG); \
109 if (val == -1 && *decided != loc) \
114 if (val < from || val > to) \
120 get_number (from, to, n); \
125 # define get_alt_number(from, to, n) \
126 /* We don't have the alternate representation. */ \
127 get_number(from, to, n)
129 #define recursive(new_fmt) \
130 (*(new_fmt) != '\0' \
131 && (rp = strptime_internal (rp, (new_fmt), tm, \
132 decided, era_cnt LOCALE_ARG)) != NULL)
136 /* This is defined in locale/C-time.c in the GNU libc. */
137 extern const struct locale_data _nl_C_LC_TIME attribute_hidden
;
139 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
140 # define ab_weekday_name \
141 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
142 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
143 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
144 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
145 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
146 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
147 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
148 # define HERE_T_FMT_AMPM \
149 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
150 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
152 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
154 static char const weekday_name
[][10] =
156 "Sunday", "Monday", "Tuesday", "Wednesday",
157 "Thursday", "Friday", "Saturday"
159 static char const ab_weekday_name
[][4] =
161 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
163 static char const month_name
[][10] =
165 "January", "February", "March", "April", "May", "June",
166 "July", "August", "September", "October", "November", "December"
168 static char const ab_month_name
[][4] =
170 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
171 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
173 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
174 # define HERE_D_FMT "%m/%d/%y"
175 # define HERE_AM_STR "AM"
176 # define HERE_PM_STR "PM"
177 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
178 # define HERE_T_FMT "%H:%M:%S"
180 const unsigned short int __mon_yday
[2][13] =
183 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
185 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
189 #if defined _LIBC && defined USE_IN_EXTENDED_LOCALE_MODEL
190 /* We use this code also for the extended locale handling where the
191 function gets as an additional argument the locale which has to be
192 used. To access the values we have to redefine the _NL_CURRENT
194 # define strptime __strptime_l
196 # define _NL_CURRENT(category, item) \
197 (current->values[_NL_ITEM_INDEX (item)].string)
198 # undef _NL_CURRENT_WORD
199 # define _NL_CURRENT_WORD(category, item) \
200 (current->values[_NL_ITEM_INDEX (item)].word)
201 # define LOCALE_PARAM , locale
202 # define LOCALE_ARG , locale
203 # define LOCALE_PARAM_PROTO , __locale_t locale
204 # define LOCALE_PARAM_DECL __locale_t locale;
205 # define HELPER_LOCALE_ARG , current
207 # define LOCALE_PARAM
209 # define LOCALE_PARAM_DECL
210 # define LOCALE_PARAM_PROTO
212 # define HELPER_LOCALE_ARG , _NL_CURRENT_DATA (LC_TIME)
214 # define HELPER_LOCALE_ARG
219 /* Status of lookup: do we use the locale data or the raw data? */
220 enum locale_status
{ not, loc
, raw
};
224 /* Nonzero if YEAR is a leap year (every 4 years,
225 except every 100th isn't, and every 400th is). */
226 # define __isleap(year) \
227 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
230 /* Compute the day of the week. */
232 day_of_the_week (struct tm
*tm
)
234 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
235 the difference between this data in the one on TM and so determine
237 int corr_year
= 1900 + tm
->tm_year
- (tm
->tm_mon
< 2);
239 + (365 * (tm
->tm_year
- 70))
241 - ((corr_year
/ 4) / 25) + ((corr_year
/ 4) % 25 < 0)
242 + (((corr_year
/ 4) / 25) / 4)
243 + __mon_yday
[0][tm
->tm_mon
]
245 tm
->tm_wday
= ((wday
% 7) + 7) % 7;
248 /* Compute the day of the year. */
250 day_of_the_year (struct tm
*tm
)
252 tm
->tm_yday
= (__mon_yday
[__isleap (1900 + tm
->tm_year
)][tm
->tm_mon
]
253 + (tm
->tm_mday
- 1));
261 strptime_internal
__P ((const char *rp
, const char *fmt
, struct tm
*tm
,
262 enum locale_status
*decided
, int era_cnt
263 LOCALE_PARAM_PROTO
));
269 strptime_internal (rp
, fmt
, tm
, decided
, era_cnt LOCALE_PARAM
)
273 enum locale_status
*decided
;
277 #if defined _LIBC && defined USE_IN_EXTENDED_LOCALE_MODEL
278 struct locale_data
*const current
= locale
->__locales
[LC_TIME
];
281 const char *rp_backup
;
285 int century
, want_century
;
287 int have_wday
, want_xday
;
289 int have_mon
, have_mday
;
290 int have_uweek
, have_wweek
;
293 struct era_entry
*era
;
302 have_wday
= want_xday
= have_yday
= have_mon
= have_mday
= have_uweek
= 0;
307 /* A white space in the format string matches 0 more or white
308 space in the input string. */
311 while (isspace (*rp
))
317 /* Any character but `%' must be matched by the same character
318 in the iput string. */
321 match_char (*fmt
++, *rp
++);
327 /* We need this for handling the `E' modifier. */
331 /* Make back up of current processing pointer. */
337 /* Match the `%' character itself. */
338 match_char ('%', *rp
++);
342 /* Match day of week. */
343 for (cnt
= 0; cnt
< 7; ++cnt
)
348 if (match_string (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
), rp
))
351 && strcmp (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
),
356 if (match_string (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
), rp
))
359 && strcmp (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
),
360 ab_weekday_name
[cnt
]))
367 && (match_string (weekday_name
[cnt
], rp
)
368 || match_string (ab_weekday_name
[cnt
], rp
)))
375 /* Does not match a weekday name. */
383 /* Match month name. */
384 for (cnt
= 0; cnt
< 12; ++cnt
)
389 if (match_string (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
), rp
))
392 && strcmp (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
),
397 if (match_string (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
), rp
))
400 && strcmp (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
),
407 if (match_string (month_name
[cnt
], rp
)
408 || match_string (ab_month_name
[cnt
], rp
))
415 /* Does not match a month name. */
421 /* Match locale's date and time format. */
425 if (!recursive (_NL_CURRENT (LC_TIME
, D_T_FMT
)))
434 if (*decided
== not &&
435 strcmp (_NL_CURRENT (LC_TIME
, D_T_FMT
), HERE_D_T_FMT
))
443 if (!recursive (HERE_D_T_FMT
))
448 /* Match century number. */
450 get_number (0, 99, 2);
456 /* Match day of month. */
457 get_number (1, 31, 2);
463 if (!recursive ("%Y-%m-%d"))
471 if (!recursive (_NL_CURRENT (LC_TIME
, D_FMT
)))
481 && strcmp (_NL_CURRENT (LC_TIME
, D_FMT
), HERE_D_FMT
))
491 /* Match standard day format. */
492 if (!recursive (HERE_D_FMT
))
498 /* Match hour in 24-hour clock. */
499 get_number (0, 23, 2);
504 /* Match hour in 12-hour clock. GNU extension. */
506 /* Match hour in 12-hour clock. */
507 get_number (1, 12, 2);
508 tm
->tm_hour
= val
% 12;
512 /* Match day number of year. */
513 get_number (1, 366, 3);
514 tm
->tm_yday
= val
- 1;
518 /* Match number of month. */
519 get_number (1, 12, 2);
520 tm
->tm_mon
= val
- 1;
526 get_number (0, 59, 2);
531 /* Match any white space. */
532 while (isspace (*rp
))
536 /* Match locale's equivalent of AM/PM. */
540 if (match_string (_NL_CURRENT (LC_TIME
, AM_STR
), rp
))
542 if (strcmp (_NL_CURRENT (LC_TIME
, AM_STR
), HERE_AM_STR
))
546 if (match_string (_NL_CURRENT (LC_TIME
, PM_STR
), rp
))
548 if (strcmp (_NL_CURRENT (LC_TIME
, PM_STR
), HERE_PM_STR
))
556 if (!match_string (HERE_AM_STR
, rp
))
557 if (match_string (HERE_PM_STR
, rp
))
566 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
575 if (*decided
== not &&
576 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
584 if (!recursive (HERE_T_FMT_AMPM
))
588 if (!recursive ("%H:%M"))
593 /* The number of seconds may be very high so we cannot use
594 the `get_number' macro. Instead read the number
595 character for character and construct the result while
598 if (*rp
< '0' || *rp
> '9')
599 /* We need at least one digit. */
607 while (*rp
>= '0' && *rp
<= '9');
609 if (localtime_r (&secs
, tm
) == NULL
)
610 /* Error in function. */
615 get_number (0, 61, 2);
622 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
631 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
640 if (!recursive (HERE_T_FMT
))
644 get_number (1, 7, 1);
645 tm
->tm_wday
= val
% 7;
649 get_number (0, 99, 2);
650 /* XXX This cannot determine any field in TM. */
653 if (*rp
< '0' || *rp
> '9')
655 /* XXX Ignore the number since we would need some more
656 information to compute a real date. */
659 while (*rp
>= '0' && *rp
<= '9');
662 get_number (0, 53, 2);
667 get_number (0, 53, 2);
672 get_number (0, 53, 2);
673 /* XXX This cannot determine any field in TM without some
677 /* Match number of weekday. */
678 get_number (0, 6, 1);
683 match_year_in_century
:
684 /* Match year within century. */
685 get_number (0, 99, 2);
686 /* The "Year 2000: The Millennium Rollover" paper suggests that
687 values in the range 69-99 refer to the twentieth century. */
688 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
689 /* Indicate that we want to use the century, if specified. */
694 /* Match year including century number. */
695 get_number (0, 9999, 4);
696 tm
->tm_year
= val
- 1900;
701 /* XXX How to handle this? */
708 /* Match locale's alternate date and time format. */
711 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
714 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
716 if (!recursive (fmt
))
725 if (strcmp (fmt
, HERE_D_T_FMT
))
732 /* The C locale has no era information, so use the
733 normal representation. */
734 if (!recursive (HERE_D_T_FMT
))
743 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
744 if (era
!= NULL
&& match_string (era
->era_name
, rp
))
754 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
755 _NL_TIME_ERA_NUM_ENTRIES
);
756 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
757 ++era_cnt
, rp
= rp_backup
)
759 era
= _nl_select_era_entry (era_cnt
761 if (era
!= NULL
&& match_string (era
->era_name
, rp
))
767 if (era_cnt
== (int) num_eras
)
779 /* The C locale has no era information, so use the
780 normal representation. */
784 goto match_year_in_century
;
786 get_number(0, 9999, 4);
795 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
796 _NL_TIME_ERA_NUM_ENTRIES
);
797 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
798 ++era_cnt
, rp
= rp_backup
)
800 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
801 if (era
!= NULL
&& recursive (era
->era_format
))
804 if (era_cnt
== (int) num_eras
)
821 get_number (0, 9999, 4);
822 tm
->tm_year
= val
- 1900;
829 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
832 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
834 if (!recursive (fmt
))
843 if (strcmp (fmt
, HERE_D_FMT
))
849 if (!recursive (HERE_D_FMT
))
855 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
858 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
860 if (!recursive (fmt
))
869 if (strcmp (fmt
, HERE_T_FMT
))
875 if (!recursive (HERE_T_FMT
))
883 /* We have no information about the era format. Just use
884 the normal format. */
885 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
886 && *fmt
!= 'x' && *fmt
!= 'X')
887 /* This is an illegal format. */
897 /* Match day of month using alternate numeric symbols. */
898 get_alt_number (1, 31, 2);
904 /* Match hour in 24-hour clock using alternate numeric
906 get_alt_number (0, 23, 2);
911 /* Match hour in 12-hour clock using alternate numeric
913 get_alt_number (1, 12, 2);
914 tm
->tm_hour
= val
% 12;
918 /* Match month using alternate numeric symbols. */
919 get_alt_number (1, 12, 2);
920 tm
->tm_mon
= val
- 1;
925 /* Match minutes using alternate numeric symbols. */
926 get_alt_number (0, 59, 2);
930 /* Match seconds using alternate numeric symbols. */
931 get_alt_number (0, 61, 2);
935 get_alt_number (0, 53, 2);
940 get_alt_number (0, 53, 2);
945 get_alt_number (0, 53, 2);
946 /* XXX This cannot determine any field in TM without
947 further information. */
950 /* Match number of weekday using alternate numeric symbols. */
951 get_alt_number (0, 6, 1);
956 /* Match year within century using alternate numeric symbols. */
957 get_alt_number (0, 99, 2);
958 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
976 tm
->tm_year
= tm
->tm_year
% 100 + (century
- 19) * 100;
978 /* Only the century, but not the year. Strange, but so be it. */
979 tm
->tm_year
= (century
- 19) * 100;
984 era
= _nl_select_era_entry (era_cnt HELPER_LOCALE_ARG
);
988 tm
->tm_year
= (era
->start_date
[0]
989 + ((tm
->tm_year
- era
->offset
)
990 * era
->absolute_direction
));
992 /* Era start year assumed. */
993 tm
->tm_year
= era
->start_date
[0];
998 /* No era found but we have seen an E modifier. Rectify some
1000 if (want_century
&& century
== -1 && tm
->tm_year
< 69)
1004 if (want_xday
&& !have_wday
)
1006 if ( !(have_mon
&& have_mday
) && have_yday
)
1008 /* We don't have tm_mon and/or tm_mday, compute them. */
1010 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
] <= tm
->tm_yday
)
1013 tm
->tm_mon
= t_mon
- 1;
1017 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
1019 day_of_the_week (tm
);
1022 if (want_xday
&& !have_yday
)
1023 day_of_the_year (tm
);
1025 if ((have_uweek
|| have_wweek
) && have_wday
)
1027 int save_wday
= tm
->tm_wday
;
1028 int save_mday
= tm
->tm_mday
;
1029 int save_mon
= tm
->tm_mon
;
1030 int w_offset
= have_uweek
? 0 : 1;
1034 day_of_the_week (tm
);
1036 tm
->tm_mday
= save_mday
;
1038 tm
->tm_mon
= save_mon
;
1041 tm
->tm_yday
= ((7 - (tm
->tm_wday
- w_offset
)) % 7
1043 + save_wday
- w_offset
);
1045 if (!have_mday
|| !have_mon
)
1048 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
]
1052 tm
->tm_mon
= t_mon
- 1;
1056 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
1059 tm
->tm_wday
= save_wday
;
1067 strptime (buf
, format
, tm LOCALE_PARAM
)
1073 enum locale_status decided
;
1080 return strptime_internal (buf
, format
, tm
, &decided
, -1 LOCALE_ARG
);
1082 #if defined _LIBC && !defined USE_IN_EXTENDED_LOCALE_MODEL
1083 libc_hidden_def (strptime
)