1 /* Convert a string representation of time to a time value.
2 Copyright (C) 1996, 1997, 1998 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. */
37 # 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) \
88 if (*rp < '0' || *rp > '9') \
93 } while (val * 10 <= to && *rp >= '0' && *rp <= '9'); \
94 if (val < from || val > to) \
98 # define get_alt_number(from, to) \
100 if (*decided != raw) \
102 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
104 while (*alts != '\0') \
106 size_t len = strlen (alts); \
107 if (strncasecmp (alts, rp, len) == 0) \
109 alts = strchr (alts, '\0') + 1; \
114 if (*decided == loc && val != 0) \
123 get_number (from, to); \
126 # define get_alt_number(from, to) \
127 /* We don't have the alternate representation. */ \
130 #define recursive(new_fmt) \
131 (*(new_fmt) != '\0' \
132 && (rp = strptime_internal (rp, (new_fmt), tm, decided)) != NULL)
136 /* This is defined in locale/C-time.c in the GNU libc. */
137 extern const struct locale_data _nl_C_LC_TIME
;
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_T_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"
181 /* Status of lookup: do we use the locale data or the raw data? */
182 enum locale_status
{ not, loc
, raw
};
188 strptime_internal
__P ((const char *buf
, const char *format
, struct tm
*tm
,
189 enum locale_status
*decided
));
195 strptime_internal (buf
, format
, tm
, decided
)
199 enum locale_status
*decided
;
213 /* A white space in the format string matches 0 more or white
214 space in the input string. */
217 while (isspace (*rp
))
223 /* Any character but `%' must be matched by the same character
224 in the iput string. */
227 match_char (*fmt
++, *rp
++);
233 /* We need this for handling the `E' modifier. */
239 /* Match the `%' character itself. */
240 match_char ('%', *rp
++);
244 /* Match day of week. */
245 for (cnt
= 0; cnt
< 7; ++cnt
)
250 if (match_string (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
), rp
))
253 && strcmp (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
),
258 if (match_string (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
), rp
))
261 && strcmp (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
),
262 ab_weekday_name
[cnt
]))
269 && (match_string (weekday_name
[cnt
], rp
)
270 || match_string (ab_weekday_name
[cnt
], rp
)))
277 /* Does not match a weekday name. */
284 /* Match month name. */
285 for (cnt
= 0; cnt
< 12; ++cnt
)
290 if (match_string (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
), rp
))
293 && strcmp (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
),
298 if (match_string (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
), rp
))
301 && strcmp (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
),
308 if (match_string (month_name
[cnt
], rp
)
309 || match_string (ab_month_name
[cnt
], rp
))
316 /* Does not match a month name. */
321 /* Match locale's date and time format. */
325 if (!recursive (_NL_CURRENT (LC_TIME
, D_T_FMT
)))
332 if (*decided
== not &&
333 strcmp (_NL_CURRENT (LC_TIME
, D_T_FMT
), HERE_D_T_FMT
))
340 if (!recursive (HERE_D_T_FMT
))
344 /* Match century number. */
346 /* We don't need the number. */
350 /* Match day of month. */
358 if (!recursive (_NL_CURRENT (LC_TIME
, D_FMT
)))
366 && strcmp (_NL_CURRENT (LC_TIME
, D_FMT
), HERE_D_FMT
))
375 /* Match standard day format. */
376 if (!recursive (HERE_D_FMT
))
380 /* Match hour in 24-hour clock. */
386 /* Match hour in 12-hour clock. */
388 tm
->tm_hour
= val
% 12;
392 /* Match day number of year. */
394 tm
->tm_yday
= val
- 1;
397 /* Match number of month. */
399 tm
->tm_mon
= val
- 1;
408 /* Match any white space. */
409 while (isspace (*rp
))
413 /* Match locale's equivalent of AM/PM. */
417 if (match_string (_NL_CURRENT (LC_TIME
, AM_STR
), rp
))
419 if (strcmp (_NL_CURRENT (LC_TIME
, AM_STR
), HERE_AM_STR
))
423 if (match_string (_NL_CURRENT (LC_TIME
, PM_STR
), rp
))
425 if (strcmp (_NL_CURRENT (LC_TIME
, PM_STR
), HERE_PM_STR
))
433 if (!match_string (HERE_AM_STR
, rp
))
434 if (match_string (HERE_PM_STR
, rp
))
443 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
450 if (*decided
== not &&
451 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
459 if (!recursive (HERE_T_FMT_AMPM
))
463 if (!recursive ("%H:%M"))
468 /* The number of seconds may be very high so we cannot use
469 the `get_number' macro. Instead read the number
470 character for character and construct the result while
473 if (*rp
< '0' || *rp
> '9')
474 /* We need at least one digit. */
482 while (*rp
>= '0' && *rp
<= '9');
484 if (localtime_r (&secs
, tm
) == NULL
)
485 /* Error in function. */
497 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
504 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
513 if (!recursive (HERE_T_FMT
))
518 tm
->tm_wday
= val
% 7;
522 /* XXX This cannot determine any field in TM. */
525 if (*rp
< '0' || *rp
> '9')
527 /* XXX Ignore the number since we would need some more
528 information to compute a real date. */
531 while (*rp
>= '0' && *rp
<= '9');
537 /* XXX This cannot determine any field in TM without some
541 /* Match number of weekday. */
546 /* Match year within century. */
548 /* The "Year 2000 :The Millennium Rollover" paper suggests that
549 values in the range 69-99 refer to the twentieth century. */
550 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
553 /* Match year including century number. */
554 get_number (0, 9999);
555 tm
->tm_year
= val
- 1900;
558 /* XXX How to handle this? */
565 /* Match locale's alternate date and time format. */
568 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
571 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
573 if (!recursive (fmt
))
580 if (strcmp (fmt
, HERE_D_T_FMT
))
586 /* The C locale has no era information, so use the
587 normal representation. */
588 if (!recursive (HERE_D_T_FMT
))
594 /* Match name of base year in locale's alternate
596 /* XXX This is currently not implemented. It should
597 use the value _NL_CURRENT (LC_TIME, ERA). */
602 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
605 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
607 if (!recursive (fmt
))
614 if (strcmp (fmt
, HERE_D_FMT
))
620 if (!recursive (HERE_D_FMT
))
626 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
629 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
631 if (!recursive (fmt
))
638 if (strcmp (fmt
, HERE_T_FMT
))
644 if (!recursive (HERE_T_FMT
))
652 /* We have no information about the era format. Just use
653 the normal format. */
654 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
655 && *fmt
!= 'x' && *fmt
!= 'X')
656 /* This is an illegal format. */
666 /* Match day of month using alternate numeric symbols. */
667 get_alt_number (1, 31);
671 /* Match hour in 24-hour clock using alternate numeric
673 get_alt_number (0, 23);
678 /* Match hour in 12-hour clock using alternate numeric
680 get_alt_number (1, 12);
681 tm
->tm_hour
= val
- 1;
685 /* Match month using alternate numeric symbols. */
686 get_alt_number (1, 12);
687 tm
->tm_mon
= val
- 1;
690 /* Match minutes using alternate numeric symbols. */
691 get_alt_number (0, 59);
695 /* Match seconds using alternate numeric symbols. */
696 get_alt_number (0, 61);
702 get_alt_number (0, 53);
703 /* XXX This cannot determine any field in TM without
704 further information. */
707 /* Match number of weekday using alternate numeric symbols. */
708 get_alt_number (0, 6);
712 /* Match year within century using alternate numeric symbols. */
713 get_alt_number (0, 99);
714 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
733 strptime (buf
, format
, tm
)
738 enum locale_status decided
;
744 return strptime_internal (buf
, format
, tm
, &decided
);