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"
42 # if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
43 # define __P(args) args
49 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
51 # define localtime_r __localtime_r
53 /* Approximate localtime_r as best we can in its absence. */
54 # define localtime_r my_localtime_r
55 static struct tm *localtime_r __P ((const time_t *, struct tm *));
61 struct tm *l = localtime (t);
68 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
71 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
72 #if defined __GNUC__ && __GNUC__ >= 2
73 # define match_string(cs1, s2) \
74 ({ size_t len = strlen (cs1); \
75 int result = strncasecmp ((cs1), (s2), len) == 0; \
76 if (result) (s2) += len; \
79 /* Oh come on. Get a reasonable compiler. */
80 # define match_string(cs1, s2) \
81 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
83 /* We intentionally do not use isdigit() for testing because this will
84 lead to problems with the wide character version. */
85 #define get_number(from, to, n) \
91 if (*rp < '0' || *rp > '9') \
96 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
97 if (val < from || val > to) \
101 # define get_alt_number(from, to, n) \
103 __label__ do_normal; \
104 if (*decided != raw) \
106 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
114 while (*alts != '\0') \
116 size_t len = strlen (alts); \
117 if (strncasecmp (alts, rp, len) == 0) \
124 if (*decided == not && ! any) \
126 /* If we haven't read anything it's an error. */ \
129 /* Correct the premature multiplication. */ \
135 } while (--__n > 0 && val * 10 <= to); \
136 if (val < from || val > to) \
142 get_number (from, to, n); \
147 # define get_alt_number(from, to, n) \
148 /* We don't have the alternate representation. */ \
149 get_number(from, to, n)
151 #define recursive(new_fmt) \
152 (*(new_fmt) != '\0' \
153 && (rp = strptime_internal (rp, (new_fmt), tm, decided, era_cnt)) != NULL)
157 /* This is defined in locale/C-time.c in the GNU libc. */
158 extern const struct locale_data _nl_C_LC_TIME;
159 extern const unsigned short int __mon_yday[2][13];
161 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
162 # define ab_weekday_name \
163 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
164 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
165 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
166 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
167 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
168 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
169 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
170 # define HERE_T_FMT_AMPM \
171 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
172 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
174 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
176 static char const weekday_name[][10] =
178 "Sunday", "Monday", "Tuesday", "Wednesday",
179 "Thursday", "Friday", "Saturday"
181 static char const ab_weekday_name[][4] =
183 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
185 static char const month_name[][10] =
187 "January", "February", "March", "April", "May", "June",
188 "July", "August", "September", "October", "November", "December"
190 static char const ab_month_name[][4] =
192 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
193 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
195 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
196 # define HERE_D_FMT "%m/%d/%y"
197 # define HERE_AM_STR "AM"
198 # define HERE_PM_STR "PM"
199 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
200 # define HERE_T_FMT "%H:%M:%S"
202 const unsigned short int __mon_yday[2][13] =
205 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
207 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
211 /* Status of lookup: do we use the locale data or the raw data? */
212 enum locale_status { not, loc, raw };
216 /* Nonzero if YEAR is a leap year (every 4 years,
217 except every 100th isn't, and every 400th is). */
218 # define __isleap(year) \
219 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
222 /* Compute the day of the week. */
224 day_of_the_week (struct tm *tm)
226 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
227 the difference between this data in the one on TM and so determine
229 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
231 + (365 * (tm->tm_year - 70))
233 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
234 + (((corr_year / 4) / 25) / 4)
235 + __mon_yday[0][tm->tm_mon]
237 tm->tm_wday = ((wday % 7) + 7) % 7;
240 /* Compute the day of the year. */
242 day_of_the_year (struct tm *tm)
244 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
245 + (tm->tm_mday - 1));
252 strptime_internal __P ((const char *rp, const char *fmt, struct tm *tm,
253 enum locale_status *decided, int era_cnt));
259 strptime_internal (rp, fmt, tm, decided, era_cnt)
263 enum locale_status *decided;
266 const char *rp_backup;
270 int century, want_century;
272 int have_wday, want_xday;
274 int have_mon, have_mday;
278 struct era_entry *era;
286 have_wday = want_xday = have_yday = have_mon = have_mday = 0;
290 /* A white space in the format string matches 0 more or white
291 space in the input string. */
294 while (isspace (*rp))
300 /* Any character but `%' must be matched by the same character
301 in the iput string. */
304 match_char (*fmt++, *rp++);
310 /* We need this for handling the `E' modifier. */
314 /* Make back up of current processing pointer. */
320 /* Match the `%' character itself. */
321 match_char ('%', *rp++);
325 /* Match day of week. */
326 for (cnt = 0; cnt < 7; ++cnt)
331 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
334 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
339 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
342 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
343 ab_weekday_name[cnt]))
350 && (match_string (weekday_name[cnt], rp)
351 || match_string (ab_weekday_name[cnt], rp)))
358 /* Does not match a weekday name. */
366 /* Match month name. */
367 for (cnt = 0; cnt < 12; ++cnt)
372 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
375 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
380 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
383 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
390 if (match_string (month_name[cnt], rp)
391 || match_string (ab_month_name[cnt], rp))
398 /* Does not match a month name. */
404 /* Match locale's date and time format. */
408 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
417 if (*decided == not &&
418 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
426 if (!recursive (HERE_D_T_FMT))
431 /* 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. */
490 get_number (1, 12, 2);
491 tm->tm_hour = val % 12;
495 /* Match day number of year. */
496 get_number (1, 366, 3);
497 tm->tm_yday = val - 1;
501 /* Match number of month. */
502 get_number (1, 12, 2);
503 tm->tm_mon = val - 1;
509 get_number (0, 59, 2);
514 /* Match any white space. */
515 while (isspace (*rp))
519 /* Match locale's equivalent of AM/PM. */
523 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
525 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
529 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
531 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
539 if (!match_string (HERE_AM_STR, rp))
540 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');
592 if (localtime_r (&secs, tm) == NULL)
593 /* Error in function. */
598 get_number (0, 61, 2);
605 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
614 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
623 if (!recursive (HERE_T_FMT))
627 get_number (1, 7, 1);
628 tm->tm_wday = val % 7;
632 get_number (0, 99, 2);
633 /* XXX This cannot determine any field in TM. */
636 if (*rp < '0' || *rp > '9')
638 /* XXX Ignore the number since we would need some more
639 information to compute a real date. */
642 while (*rp >= '0' && *rp <= '9');
647 get_number (0, 53, 2);
648 /* XXX This cannot determine any field in TM without some
652 /* Match number of weekday. */
653 get_number (0, 6, 1);
659 match_year_in_century:
661 /* Match year within century. */
662 get_number (0, 99, 2);
663 /* The "Year 2000: The Millennium Rollover" paper suggests that
664 values in the range 69-99 refer to the twentieth century. */
665 tm->tm_year = val >= 69 ? val : val + 100;
666 /* Indicate that we want to use the century, if specified. */
671 /* Match year including century number. */
672 get_number (0, 9999, 4);
673 tm->tm_year = val - 1900;
678 /* XXX How to handle this? */
685 /* Match locale's alternate date and time format. */
688 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
691 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
693 if (!recursive (fmt))
702 if (strcmp (fmt, HERE_D_T_FMT))
709 /* The C locale has no era information, so use the
710 normal representation. */
711 if (!recursive (HERE_D_T_FMT))
720 era = _nl_select_era_entry (era_cnt);
721 if (match_string (era->era_name, rp))
731 num_eras = _NL_CURRENT_WORD (LC_TIME,
732 _NL_TIME_ERA_NUM_ENTRIES);
733 for (era_cnt = 0; era_cnt < (int) num_eras;
734 ++era_cnt, rp = rp_backup)
736 era = _nl_select_era_entry (era_cnt);
737 if (match_string (era->era_name, rp))
743 if (era_cnt == (int) num_eras)
755 /* The C locale has no era information, so use the
756 normal representation. */
760 goto match_year_in_century;
762 get_number(0, 9999, 4);
770 num_eras = _NL_CURRENT_WORD (LC_TIME,
771 _NL_TIME_ERA_NUM_ENTRIES);
772 for (era_cnt = 0; era_cnt < (int) num_eras;
773 ++era_cnt, rp = rp_backup)
775 era = _nl_select_era_entry (era_cnt);
776 if (recursive (era->era_format))
779 if (era_cnt == (int) num_eras)
796 get_number (0, 9999, 4);
797 tm->tm_year = val - 1900;
804 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
807 fmt = _NL_CURRENT (LC_TIME, D_FMT);
809 if (!recursive (fmt))
818 if (strcmp (fmt, HERE_D_FMT))
824 if (!recursive (HERE_D_FMT))
830 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
833 fmt = _NL_CURRENT (LC_TIME, T_FMT);
835 if (!recursive (fmt))
844 if (strcmp (fmt, HERE_T_FMT))
850 if (!recursive (HERE_T_FMT))
858 /* We have no information about the era format. Just use
859 the normal format. */
860 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
861 && *fmt != 'x' && *fmt != 'X')
862 /* This is an illegal format. */
872 /* Match day of month using alternate numeric symbols. */
873 get_alt_number (1, 31, 2);
879 /* Match hour in 24-hour clock using alternate numeric
881 get_alt_number (0, 23, 2);
886 /* Match hour in 12-hour clock using alternate numeric
888 get_alt_number (1, 12, 2);
889 tm->tm_hour = val - 1;
893 /* Match month using alternate numeric symbols. */
894 get_alt_number (1, 12, 2);
895 tm->tm_mon = val - 1;
900 /* Match minutes using alternate numeric symbols. */
901 get_alt_number (0, 59, 2);
905 /* Match seconds using alternate numeric symbols. */
906 get_alt_number (0, 61, 2);
912 get_alt_number (0, 53, 2);
913 /* XXX This cannot determine any field in TM without
914 further information. */
917 /* Match number of weekday using alternate numeric symbols. */
918 get_alt_number (0, 6, 1);
923 /* Match year within century using alternate numeric symbols. */
924 get_alt_number (0, 99, 2);
925 tm->tm_year = val >= 69 ? val : val + 100;
943 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100;
945 /* Only the century, but not the year. Strange, but so be it. */
946 tm->tm_year = (century - 19) * 100;
952 era = _nl_select_era_entry(era_cnt);
954 tm->tm_year = (era->start_date[0]
955 + ((tm->tm_year - era->offset)
956 * era->absolute_direction));
958 /* Era start year assumed. */
959 tm->tm_year = era->start_date[0];
966 if (want_xday && !have_wday)
968 if ( !(have_mon && have_mday) && have_yday)
970 /* We don't have tm_mon and/or tm_mday, compute them. */
972 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
975 tm->tm_mon = t_mon - 1;
979 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
981 day_of_the_week (tm);
983 if (want_xday && !have_yday)
984 day_of_the_year (tm);
991 strptime (buf, format, tm)
996 enum locale_status decided;
1003 return strptime_internal (buf, format, tm, &decided, -1);