1 /* Convert a string representation of time to a time value.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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. */
36 # include "../locale/localeinfo.h"
41 # if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
42 # define __P(args) args
48 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
50 # define localtime_r __localtime_r
52 /* Approximate localtime_r as best we can in its absence. */
53 # define localtime_r my_localtime_r
54 static struct tm *localtime_r __P ((const time_t *, struct tm *));
60 struct tm *l = localtime (t);
67 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
70 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
71 #if defined __GNUC__ && __GNUC__ >= 2
72 # define match_string(cs1, s2) \
73 ({ size_t len = strlen (cs1); \
74 int result = strncasecmp ((cs1), (s2), len) == 0; \
75 if (result) (s2) += len; \
78 /* Oh come on. Get a reasonable compiler. */
79 # define match_string(cs1, s2) \
80 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
82 /* We intentionally do not use isdigit() for testing because this will
83 lead to problems with the wide character version. */
84 #define get_number(from, to, n) \
90 if (*rp < '0' || *rp > '9') \
95 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
96 if (val < from || val > to) \
100 # define get_alt_number(from, to, n) \
102 __label__ do_normal; \
103 if (*decided != raw) \
105 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
113 while (*alts != '\0') \
115 size_t len = strlen (alts); \
116 if (strncasecmp (alts, rp, len) == 0) \
123 if (*decided == not && ! any) \
125 /* If we haven't read anything it's an error. */ \
128 /* Correct the premature multiplication. */ \
134 } while (--__n > 0 && val * 10 <= to); \
135 if (val < from || val > to) \
141 get_number (from, to, n); \
146 # define get_alt_number(from, to, n) \
147 /* We don't have the alternate representation. */ \
148 get_number(from, to, n)
150 #define recursive(new_fmt) \
151 (*(new_fmt) != '\0' \
152 && (rp = strptime_internal (rp, (new_fmt), tm, decided, era_cnt)) != NULL)
156 /* This is defined in locale/C-time.c in the GNU libc. */
157 extern const struct locale_data _nl_C_LC_TIME;
158 extern const unsigned short int __mon_yday[2][13];
160 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
161 # define ab_weekday_name \
162 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
163 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
164 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
165 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
166 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
167 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
168 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
169 # define HERE_T_FMT_AMPM \
170 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
171 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
173 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
175 static char const weekday_name[][10] =
177 "Sunday", "Monday", "Tuesday", "Wednesday",
178 "Thursday", "Friday", "Saturday"
180 static char const ab_weekday_name[][4] =
182 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
184 static char const month_name[][10] =
186 "January", "February", "March", "April", "May", "June",
187 "July", "August", "September", "October", "November", "December"
189 static char const ab_month_name[][4] =
191 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
192 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
194 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
195 # define HERE_D_FMT "%m/%d/%y"
196 # define HERE_AM_STR "AM"
197 # define HERE_PM_STR "PM"
198 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
199 # define HERE_T_FMT "%H:%M:%S"
201 const unsigned short int __mon_yday[2][13] =
204 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
206 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
210 /* Status of lookup: do we use the locale data or the raw data? */
211 enum locale_status { not, loc, raw };
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));
251 strptime_internal __P ((const char *rp, const char *fmt, struct tm *tm,
252 enum locale_status *decided, int era_cnt));
258 strptime_internal (rp, fmt, tm, decided, era_cnt)
262 enum locale_status *decided;
265 const char *rp_backup;
269 int century, want_century;
271 int have_wday, want_xday;
273 int have_mon, have_mday;
277 struct era_entry *era;
285 have_wday = want_xday = have_yday = have_mon = have_mday = 0;
289 /* A white space in the format string matches 0 more or white
290 space in the input string. */
293 while (isspace (*rp))
299 /* Any character but `%' must be matched by the same character
300 in the iput string. */
303 match_char (*fmt++, *rp++);
309 /* We need this for handling the `E' modifier. */
313 /* Make back up of current processing pointer. */
319 /* Match the `%' character itself. */
320 match_char ('%', *rp++);
324 /* Match day of week. */
325 for (cnt = 0; cnt < 7; ++cnt)
330 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
333 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
338 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
341 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
342 ab_weekday_name[cnt]))
349 && (match_string (weekday_name[cnt], rp)
350 || match_string (ab_weekday_name[cnt], rp)))
357 /* Does not match a weekday name. */
365 /* Match month name. */
366 for (cnt = 0; cnt < 12; ++cnt)
371 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
374 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
379 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
382 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
389 if (match_string (month_name[cnt], rp)
390 || match_string (ab_month_name[cnt], rp))
397 /* Does not match a month name. */
403 /* Match locale's date and time format. */
407 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
416 if (*decided == not &&
417 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
425 if (!recursive (HERE_D_T_FMT))
430 /* Match century number. */
434 get_number (0, 99, 2);
440 /* Match day of month. */
441 get_number (1, 31, 2);
447 if (!recursive ("%Y-%m-%d"))
455 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
465 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
475 /* Match standard day format. */
476 if (!recursive (HERE_D_FMT))
482 /* Match hour in 24-hour clock. */
483 get_number (0, 23, 2);
488 /* Match hour in 12-hour clock. */
489 get_number (1, 12, 2);
490 tm->tm_hour = val % 12;
494 /* Match day number of year. */
495 get_number (1, 366, 3);
496 tm->tm_yday = val - 1;
500 /* Match number of month. */
501 get_number (1, 12, 2);
502 tm->tm_mon = val - 1;
508 get_number (0, 59, 2);
513 /* Match any white space. */
514 while (isspace (*rp))
518 /* Match locale's equivalent of AM/PM. */
522 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
524 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
528 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
530 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
538 if (!match_string (HERE_AM_STR, rp))
539 if (match_string (HERE_PM_STR, rp))
548 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
557 if (*decided == not &&
558 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
566 if (!recursive (HERE_T_FMT_AMPM))
570 if (!recursive ("%H:%M"))
575 /* The number of seconds may be very high so we cannot use
576 the `get_number' macro. Instead read the number
577 character for character and construct the result while
580 if (*rp < '0' || *rp > '9')
581 /* We need at least one digit. */
589 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');
646 get_number (0, 53, 2);
647 /* XXX This cannot determine any field in TM without some
651 /* Match number of weekday. */
652 get_number (0, 6, 1);
658 match_year_in_century:
660 /* Match year within century. */
661 get_number (0, 99, 2);
662 /* The "Year 2000: The Millennium Rollover" paper suggests that
663 values in the range 69-99 refer to the twentieth century. */
664 tm->tm_year = val >= 69 ? val : val + 100;
665 /* Indicate that we want to use the century, if specified. */
670 /* Match year including century number. */
671 get_number (0, 9999, 4);
672 tm->tm_year = val - 1900;
677 /* XXX How to handle this? */
684 /* Match locale's alternate date and time format. */
687 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
690 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
692 if (!recursive (fmt))
701 if (strcmp (fmt, HERE_D_T_FMT))
708 /* The C locale has no era information, so use the
709 normal representation. */
710 if (!recursive (HERE_D_T_FMT))
719 era = _nl_select_era_entry (era_cnt);
720 if (match_string (era->era_name, rp))
730 num_eras = _NL_CURRENT_WORD (LC_TIME,
731 _NL_TIME_ERA_NUM_ENTRIES);
732 for (era_cnt = 0; era_cnt < (int) num_eras;
733 ++era_cnt, rp = rp_backup)
735 era = _nl_select_era_entry (era_cnt);
736 if (match_string (era->era_name, rp))
742 if (era_cnt == (int) num_eras)
754 /* The C locale has no era information, so use the
755 normal representation. */
759 goto match_year_in_century;
761 get_number(0, 9999, 4);
769 num_eras = _NL_CURRENT_WORD (LC_TIME,
770 _NL_TIME_ERA_NUM_ENTRIES);
771 for (era_cnt = 0; era_cnt < (int) num_eras;
772 ++era_cnt, rp = rp_backup)
774 era = _nl_select_era_entry (era_cnt);
775 if (recursive (era->era_format))
778 if (era_cnt == (int) num_eras)
795 get_number (0, 9999, 4);
796 tm->tm_year = val - 1900;
803 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
806 fmt = _NL_CURRENT (LC_TIME, D_FMT);
808 if (!recursive (fmt))
817 if (strcmp (fmt, HERE_D_FMT))
823 if (!recursive (HERE_D_FMT))
829 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
832 fmt = _NL_CURRENT (LC_TIME, T_FMT);
834 if (!recursive (fmt))
843 if (strcmp (fmt, HERE_T_FMT))
849 if (!recursive (HERE_T_FMT))
857 /* We have no information about the era format. Just use
858 the normal format. */
859 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
860 && *fmt != 'x' && *fmt != 'X')
861 /* This is an illegal format. */
871 /* Match day of month using alternate numeric symbols. */
872 get_alt_number (1, 31, 2);
878 /* Match hour in 24-hour clock using alternate numeric
880 get_alt_number (0, 23, 2);
885 /* Match hour in 12-hour clock using alternate numeric
887 get_alt_number (1, 12, 2);
888 tm->tm_hour = val - 1;
892 /* Match month using alternate numeric symbols. */
893 get_alt_number (1, 12, 2);
894 tm->tm_mon = val - 1;
899 /* Match minutes using alternate numeric symbols. */
900 get_alt_number (0, 59, 2);
904 /* Match seconds using alternate numeric symbols. */
905 get_alt_number (0, 61, 2);
911 get_alt_number (0, 53, 2);
912 /* XXX This cannot determine any field in TM without
913 further information. */
916 /* Match number of weekday using alternate numeric symbols. */
917 get_alt_number (0, 6, 1);
922 /* Match year within century using alternate numeric symbols. */
923 get_alt_number (0, 99, 2);
924 tm->tm_year = val >= 69 ? val : val + 100;
942 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100;
944 /* Only the century, but not the year. Strange, but so be it. */
945 tm->tm_year = (century - 19) * 100;
951 era = _nl_select_era_entry(era_cnt);
953 tm->tm_year = (era->start_date[0]
954 + ((tm->tm_year - era->offset)
955 * era->absolute_direction));
957 /* Era start year assumed. */
958 tm->tm_year = era->start_date[0];
965 if (want_xday && !have_wday)
967 if ( !(have_mon && have_mday) && have_yday)
969 /* We don't have tm_mon and/or tm_mday, compute them. */
971 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
974 tm->tm_mon = t_mon - 1;
978 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
980 day_of_the_week (tm);
982 if (want_xday && !have_yday)
983 day_of_the_year (tm);
990 strptime (buf, format, tm)
995 enum locale_status decided;
1002 return strptime_internal (buf, format, tm, &decided, -1);