2 Convert a string representation of time to a time value.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301 USA. */
22 /* XXX This version of the implementation is not really complete.
23 Some of the fields cannot add information alone. But if seeing
24 some of them in the same format (such as year, week and weekday)
25 this is enough information for determining the date. */
34 #ifndef HAVE_STRNCASECMP
35 #include "wsutil/strncasecmp.h"
39 # include "../locale/localeinfo.h"
45 # if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
46 # define __P(args) args
52 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
54 # define localtime_r __localtime_r
56 /* Approximate localtime_r as best we can in its absence. */
57 # define localtime_r my_localtime_r
58 static struct tm *localtime_r __P ((const time_t *, struct tm *));
64 struct tm *l = localtime (t);
71 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
74 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
75 #if defined __GNUC__ && __GNUC__ >= 2
76 # define match_string(cs1, s2) \
77 ({ size_t len = strlen (cs1); \
78 int result = strncasecmp ((cs1), (s2), len) == 0; \
79 if (result) (s2) += len; \
82 /* Oh come on. Get a reasonable compiler. */
83 # define match_string(cs1, s2) \
84 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
86 /* We intentionally do not use isdigit() for testing because this will
87 lead to problems with the wide character version. */
88 #define get_number(from, to, n) \
94 if (*rp < '0' || *rp > '9') \
99 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
100 if (val < from || val > to) \
104 # define get_alt_number(from, to, n) \
106 __label__ do_normal; \
107 if (*decided != raw) \
109 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
117 while (*alts != '\0') \
119 size_t len = strlen (alts); \
120 if (strncasecmp (alts, rp, len) == 0) \
127 if (*decided == not && ! any) \
129 /* If we haven't read anything it's an error. */ \
132 /* Correct the premature multiplication. */ \
138 } while (--__n > 0 && val * 10 <= to); \
139 if (val < from || val > to) \
145 get_number (from, to, n); \
150 # define get_alt_number(from, to, n) \
151 /* We don't have the alternate representation. */ \
152 get_number(from, to, n)
154 #define recursive(new_fmt) \
155 (*(new_fmt) != '\0' \
156 && (rp = strptime_internal (rp, (new_fmt), tm, decided, era_cnt)) != NULL)
160 /* This is defined in locale/C-time.c in the GNU libc. */
161 extern const struct locale_data _nl_C_LC_TIME;
162 extern const unsigned short int __mon_yday[2][13];
164 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
165 # define ab_weekday_name \
166 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
167 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
168 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
169 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
170 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
171 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
172 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
173 # define HERE_T_FMT_AMPM \
174 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
175 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
177 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
179 static char const weekday_name[][10] =
181 "Sunday", "Monday", "Tuesday", "Wednesday",
182 "Thursday", "Friday", "Saturday"
184 static char const ab_weekday_name[][4] =
186 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
188 static char const month_name[][10] =
190 "January", "February", "March", "April", "May", "June",
191 "July", "August", "September", "October", "November", "December"
193 static char const ab_month_name[][4] =
195 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
196 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
198 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
199 # define HERE_D_FMT "%m/%d/%y"
200 # define HERE_AM_STR "AM"
201 # define HERE_PM_STR "PM"
202 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
203 # define HERE_T_FMT "%H:%M:%S"
205 const unsigned short int __mon_yday[2][13] =
208 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
210 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
214 /* Status of lookup: do we use the locale data or the raw data? */
215 enum locale_status { not, loc, raw };
219 /* Nonzero if YEAR is a leap year (every 4 years,
220 except every 100th isn't, and every 400th is). */
221 # define __isleap(year) \
222 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
225 /* Compute the day of the week. */
227 day_of_the_week (struct tm *tm)
229 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
230 the difference between this data in the one on TM and so determine
232 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
234 + (365 * (tm->tm_year - 70))
236 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
237 + (((corr_year / 4) / 25) / 4)
238 + __mon_yday[0][tm->tm_mon]
240 tm->tm_wday = ((wday % 7) + 7) % 7;
243 /* Compute the day of the year. */
245 day_of_the_year (struct tm *tm)
247 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
248 + (tm->tm_mday - 1));
255 strptime_internal __P ((const char *rp, const char *fmt, struct tm *tm,
256 enum locale_status *decided, int era_cnt));
262 strptime_internal (rp, fmt, tm, decided, era_cnt)
266 enum locale_status *decided;
269 const char *rp_backup;
273 int century, want_century;
275 int have_wday, want_xday;
277 int have_mon, have_mday;
281 struct era_entry *era;
289 have_wday = want_xday = have_yday = have_mon = have_mday = 0;
293 /* A white space in the format string matches 0 more or white
294 space in the input string. */
297 while (isspace (*rp))
303 /* Any character but `%' must be matched by the same character
304 in the iput string. */
307 match_char (*fmt++, *rp++);
313 /* We need this for handling the `E' modifier. */
317 /* Make back up of current processing pointer. */
323 /* Match the `%' character itself. */
324 match_char ('%', *rp++);
328 /* Match day of week. */
329 for (cnt = 0; cnt < 7; ++cnt)
334 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
337 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
342 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
345 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
346 ab_weekday_name[cnt]))
353 && (match_string (weekday_name[cnt], rp)
354 || match_string (ab_weekday_name[cnt], rp)))
361 /* Does not match a weekday name. */
369 /* Match month name. */
370 for (cnt = 0; cnt < 12; ++cnt)
375 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
378 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
383 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
386 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
393 if (match_string (month_name[cnt], rp)
394 || match_string (ab_month_name[cnt], rp))
401 /* Does not match a month name. */
407 /* Match locale's date and time format. */
411 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
420 if (*decided == not &&
421 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
429 if (!recursive (HERE_D_T_FMT))
434 /* Match century number. */
438 get_number (0, 99, 2);
444 /* Match day of month. */
445 get_number (1, 31, 2);
451 if (!recursive ("%Y-%m-%d"))
459 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
469 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
479 /* Match standard day format. */
480 if (!recursive (HERE_D_FMT))
486 /* Match hour in 24-hour clock. */
487 get_number (0, 23, 2);
492 /* Match hour in 12-hour clock. */
493 get_number (1, 12, 2);
494 tm->tm_hour = val % 12;
498 /* Match day number of year. */
499 get_number (1, 366, 3);
500 tm->tm_yday = val - 1;
504 /* Match number of month. */
505 get_number (1, 12, 2);
506 tm->tm_mon = val - 1;
512 get_number (0, 59, 2);
517 /* Match any white space. */
518 while (isspace (*rp))
522 /* Match locale's equivalent of AM/PM. */
526 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
528 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
532 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
534 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
542 if (!match_string (HERE_AM_STR, rp)) {
543 if (match_string (HERE_PM_STR, rp))
553 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
562 if (*decided == not &&
563 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
571 if (!recursive (HERE_T_FMT_AMPM))
575 if (!recursive ("%H:%M"))
580 /* The number of seconds may be very high so we cannot use
581 the `get_number' macro. Instead read the number
582 character for character and construct the result while
585 if (*rp < '0' || *rp > '9')
586 /* We need at least one digit. */
594 while (*rp >= '0' && *rp <= '9');
596 if (localtime_r (&secs, tm) == NULL)
597 /* Error in function. */
602 get_number (0, 61, 2);
609 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
618 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
627 if (!recursive (HERE_T_FMT))
631 get_number (1, 7, 1);
632 tm->tm_wday = val % 7;
636 get_number (0, 99, 2);
637 /* XXX This cannot determine any field in TM. */
640 if (*rp < '0' || *rp > '9')
642 /* XXX Ignore the number since we would need some more
643 information to compute a real date. */
646 while (*rp >= '0' && *rp <= '9');
651 get_number (0, 53, 2);
652 /* XXX This cannot determine any field in TM without some
656 /* Match number of weekday. */
657 get_number (0, 6, 1);
663 match_year_in_century:
665 /* Match year within century. */
666 get_number (0, 99, 2);
667 /* The "Year 2000: The Millennium Rollover" paper suggests that
668 values in the range 69-99 refer to the twentieth century. */
669 tm->tm_year = val >= 69 ? val : val + 100;
670 /* Indicate that we want to use the century, if specified. */
675 /* Match year including century number. */
676 get_number (0, 9999, 4);
677 tm->tm_year = val - 1900;
682 /* XXX How to handle this? */
689 /* Match locale's alternate date and time format. */
692 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
695 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
697 if (!recursive (fmt))
706 if (strcmp (fmt, HERE_D_T_FMT))
713 /* The C locale has no era information, so use the
714 normal representation. */
715 if (!recursive (HERE_D_T_FMT))
724 era = _nl_select_era_entry (era_cnt);
725 if (match_string (era->era_name, rp))
735 num_eras = _NL_CURRENT_WORD (LC_TIME,
736 _NL_TIME_ERA_NUM_ENTRIES);
737 for (era_cnt = 0; era_cnt < (int) num_eras;
738 ++era_cnt, rp = rp_backup)
740 era = _nl_select_era_entry (era_cnt);
741 if (match_string (era->era_name, rp))
747 if (era_cnt == (int) num_eras)
759 /* The C locale has no era information, so use the
760 normal representation. */
764 goto match_year_in_century;
766 get_number(0, 9999, 4);
774 num_eras = _NL_CURRENT_WORD (LC_TIME,
775 _NL_TIME_ERA_NUM_ENTRIES);
776 for (era_cnt = 0; era_cnt < (int) num_eras;
777 ++era_cnt, rp = rp_backup)
779 era = _nl_select_era_entry (era_cnt);
780 if (recursive (era->era_format))
783 if (era_cnt == (int) num_eras)
800 get_number (0, 9999, 4);
801 tm->tm_year = val - 1900;
808 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
811 fmt = _NL_CURRENT (LC_TIME, D_FMT);
813 if (!recursive (fmt))
822 if (strcmp (fmt, HERE_D_FMT))
828 if (!recursive (HERE_D_FMT))
834 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
837 fmt = _NL_CURRENT (LC_TIME, T_FMT);
839 if (!recursive (fmt))
848 if (strcmp (fmt, HERE_T_FMT))
854 if (!recursive (HERE_T_FMT))
862 /* We have no information about the era format. Just use
863 the normal format. */
864 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
865 && *fmt != 'x' && *fmt != 'X')
866 /* This is an illegal format. */
876 /* Match day of month using alternate numeric symbols. */
877 get_alt_number (1, 31, 2);
883 /* Match hour in 24-hour clock using alternate numeric
885 get_alt_number (0, 23, 2);
890 /* Match hour in 12-hour clock using alternate numeric
892 get_alt_number (1, 12, 2);
893 tm->tm_hour = val - 1;
897 /* Match month using alternate numeric symbols. */
898 get_alt_number (1, 12, 2);
899 tm->tm_mon = val - 1;
904 /* Match minutes using alternate numeric symbols. */
905 get_alt_number (0, 59, 2);
909 /* Match seconds using alternate numeric symbols. */
910 get_alt_number (0, 61, 2);
916 get_alt_number (0, 53, 2);
917 /* XXX This cannot determine any field in TM without
918 further information. */
921 /* Match number of weekday using alternate numeric symbols. */
922 get_alt_number (0, 6, 1);
927 /* Match year within century using alternate numeric symbols. */
928 get_alt_number (0, 99, 2);
929 tm->tm_year = val >= 69 ? val : val + 100;
947 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100;
949 /* Only the century, but not the year. Strange, but so be it. */
950 tm->tm_year = (century - 19) * 100;
956 era = _nl_select_era_entry(era_cnt);
958 tm->tm_year = (era->start_date[0]
959 + ((tm->tm_year - era->offset)
960 * era->absolute_direction));
962 /* Era start year assumed. */
963 tm->tm_year = era->start_date[0];
970 if (want_xday && !have_wday)
972 if ( !(have_mon && have_mday) && have_yday)
974 /* We don't have tm_mon and/or tm_mday, compute them. */
976 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
979 tm->tm_mon = t_mon - 1;
983 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
985 day_of_the_week (tm);
987 if (want_xday && !have_yday)
988 day_of_the_year (tm);
995 strptime (buf, format, tm)
1000 enum locale_status decided;
1007 return strptime_internal (buf, format, tm, &decided, -1);
1011 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1016 * indent-tabs-mode: nil
1019 * ex: set shiftwidth=2 tabstop=8 expandtab:
1020 * :indentSize=2:tabSize=8:noTabs=true: