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
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) \
89 if (*rp < '0' || *rp > '9') \
94 } while (val * 10 <= to && *rp >= '0' && *rp <= '9'); \
95 if (val < from || val > to) \
99 # define get_alt_number(from, to) \
101 if (*decided != raw) \
103 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
105 while (*alts != '\0') \
107 size_t len = strlen (alts); \
108 if (strncasecmp (alts, rp, len) == 0) \
110 alts = strchr (alts, '\0') + 1; \
115 if (*decided == loc && val != 0) \
124 get_number (from, to); \
127 # define get_alt_number(from, to) \
128 /* We don't have the alternate representation. */ \
131 #define recursive(new_fmt) \
132 (*(new_fmt) != '\0' \
133 && (rp = strptime_internal (rp, (new_fmt), tm, decided)) != NULL)
137 /* This is defined in locale/C-time.c in the GNU libc. */
138 extern const struct locale_data _nl_C_LC_TIME
;
139 extern const unsigned short int __mon_yday
[2][13];
141 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
142 # define ab_weekday_name \
143 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
144 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
145 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
146 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
147 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
148 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
149 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
150 # define HERE_T_FMT_AMPM \
151 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
152 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
154 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
156 static char const weekday_name
[][10] =
158 "Sunday", "Monday", "Tuesday", "Wednesday",
159 "Thursday", "Friday", "Saturday"
161 static char const ab_weekday_name
[][4] =
163 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
165 static char const month_name
[][10] =
167 "January", "February", "March", "April", "May", "June",
168 "July", "August", "September", "October", "November", "December"
170 static char const ab_month_name
[][4] =
172 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
173 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
175 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
176 # define HERE_D_FMT "%m/%d/%y"
177 # define HERE_AM_STR "AM"
178 # define HERE_PM_STR "PM"
179 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
180 # define HERE_T_FMT "%H:%M:%S"
182 const unsigned short int __mon_yday
[1][13] =
185 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
187 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
191 /* Status of lookup: do we use the locale data or the raw data? */
192 enum locale_status
{ not, loc
, raw
};
196 /* Nonzero if YEAR is a leap year (every 4 years,
197 except every 100th isn't, and every 400th is). */
198 # define __isleap(year) \
199 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
202 /* Compute the day of the week. */
204 day_of_the_week (struct tm
*tm
)
206 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
207 the difference between this data in the one on TM and so determine
209 int corr_year
= 1900 + tm
->tm_year
- (tm
->tm_mon
< 2);
211 + (365 * (tm
->tm_year
- 70))
213 - ((corr_year
/ 4) / 25) + ((corr_year
/ 4) % 25 < 0)
214 + (((corr_year
/ 4) / 25) / 4)
215 + __mon_yday
[0][tm
->tm_mon
]
217 tm
->tm_wday
= wday
% 7;
220 /* Compute the day of the year. */
222 day_of_the_year (struct tm
*tm
)
224 tm
->tm_yday
= (__mon_yday
[__isleap (1900 + tm
->tm_year
)][tm
->tm_mon
]
225 + (tm
->tm_mday
- 1));
232 strptime_internal
__P ((const char *buf
, const char *format
, struct tm
*tm
,
233 enum locale_status
*decided
));
239 strptime_internal (buf
, format
, tm
, decided
)
243 enum locale_status
*decided
;
250 int century
, want_century
;
251 int have_wday
, want_xday
;
259 have_wday
= want_xday
= have_yday
= 0;
263 /* A white space in the format string matches 0 more or white
264 space in the input string. */
267 while (isspace (*rp
))
273 /* Any character but `%' must be matched by the same character
274 in the iput string. */
277 match_char (*fmt
++, *rp
++);
283 /* We need this for handling the `E' modifier. */
289 /* Match the `%' character itself. */
290 match_char ('%', *rp
++);
294 /* Match day of week. */
295 for (cnt
= 0; cnt
< 7; ++cnt
)
300 if (match_string (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
), rp
))
303 && strcmp (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
),
308 if (match_string (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
), rp
))
311 && strcmp (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
),
312 ab_weekday_name
[cnt
]))
319 && (match_string (weekday_name
[cnt
], rp
)
320 || match_string (ab_weekday_name
[cnt
], rp
)))
327 /* Does not match a weekday name. */
335 /* Match month name. */
336 for (cnt
= 0; cnt
< 12; ++cnt
)
341 if (match_string (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
), rp
))
344 && strcmp (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
),
349 if (match_string (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
), rp
))
352 && strcmp (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
),
359 if (match_string (month_name
[cnt
], rp
)
360 || match_string (ab_month_name
[cnt
], rp
))
367 /* Does not match a month name. */
373 /* Match locale's date and time format. */
377 if (!recursive (_NL_CURRENT (LC_TIME
, D_T_FMT
)))
384 if (*decided
== not &&
385 strcmp (_NL_CURRENT (LC_TIME
, D_T_FMT
), HERE_D_T_FMT
))
393 if (!recursive (HERE_D_T_FMT
))
398 /* Match century number. */
405 /* Match day of month. */
413 if (!recursive (_NL_CURRENT (LC_TIME
, D_FMT
)))
421 && strcmp (_NL_CURRENT (LC_TIME
, D_FMT
), HERE_D_FMT
))
431 /* Match standard day format. */
432 if (!recursive (HERE_D_FMT
))
437 /* Match hour in 24-hour clock. */
443 /* Match hour in 12-hour clock. */
445 tm
->tm_hour
= val
% 12;
449 /* Match day number of year. */
451 tm
->tm_yday
= val
- 1;
455 /* Match number of month. */
457 tm
->tm_mon
= val
- 1;
467 /* Match any white space. */
468 while (isspace (*rp
))
472 /* Match locale's equivalent of AM/PM. */
476 if (match_string (_NL_CURRENT (LC_TIME
, AM_STR
), rp
))
478 if (strcmp (_NL_CURRENT (LC_TIME
, AM_STR
), HERE_AM_STR
))
482 if (match_string (_NL_CURRENT (LC_TIME
, PM_STR
), rp
))
484 if (strcmp (_NL_CURRENT (LC_TIME
, PM_STR
), HERE_PM_STR
))
492 if (!match_string (HERE_AM_STR
, rp
))
493 if (match_string (HERE_PM_STR
, rp
))
502 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
509 if (*decided
== not &&
510 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
518 if (!recursive (HERE_T_FMT_AMPM
))
522 if (!recursive ("%H:%M"))
527 /* The number of seconds may be very high so we cannot use
528 the `get_number' macro. Instead read the number
529 character for character and construct the result while
532 if (*rp
< '0' || *rp
> '9')
533 /* We need at least one digit. */
541 while (*rp
>= '0' && *rp
<= '9');
543 if (localtime_r (&secs
, tm
) == NULL
)
544 /* Error in function. */
556 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
563 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
572 if (!recursive (HERE_T_FMT
))
577 tm
->tm_wday
= val
% 7;
582 /* XXX This cannot determine any field in TM. */
585 if (*rp
< '0' || *rp
> '9')
587 /* XXX Ignore the number since we would need some more
588 information to compute a real date. */
591 while (*rp
>= '0' && *rp
<= '9');
597 /* XXX This cannot determine any field in TM without some
601 /* Match number of weekday. */
607 /* Match year within century. */
609 /* The "Year 2000: The Millennium Rollover" paper suggests that
610 values in the range 69-99 refer to the twentieth century. */
611 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
612 /* Indicate that we want to use the century, if specified. */
617 /* Match year including century number. */
618 get_number (0, 9999);
619 tm
->tm_year
= val
- 1900;
624 /* XXX How to handle this? */
631 /* Match locale's alternate date and time format. */
634 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
637 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
639 if (!recursive (fmt
))
646 if (strcmp (fmt
, HERE_D_T_FMT
))
653 /* The C locale has no era information, so use the
654 normal representation. */
655 if (!recursive (HERE_D_T_FMT
))
662 /* Match name of base year in locale's alternate
664 /* XXX This is currently not implemented. It should
665 use the value _NL_CURRENT (LC_TIME, ERA). */
670 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
673 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
675 if (!recursive (fmt
))
682 if (strcmp (fmt
, HERE_D_FMT
))
688 if (!recursive (HERE_D_FMT
))
694 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
697 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
699 if (!recursive (fmt
))
706 if (strcmp (fmt
, HERE_T_FMT
))
712 if (!recursive (HERE_T_FMT
))
720 /* We have no information about the era format. Just use
721 the normal format. */
722 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
723 && *fmt
!= 'x' && *fmt
!= 'X')
724 /* This is an illegal format. */
734 /* Match day of month using alternate numeric symbols. */
735 get_alt_number (1, 31);
740 /* Match hour in 24-hour clock using alternate numeric
742 get_alt_number (0, 23);
747 /* Match hour in 12-hour clock using alternate numeric
749 get_alt_number (1, 12);
750 tm
->tm_hour
= val
- 1;
754 /* Match month using alternate numeric symbols. */
755 get_alt_number (1, 12);
756 tm
->tm_mon
= val
- 1;
760 /* Match minutes using alternate numeric symbols. */
761 get_alt_number (0, 59);
765 /* Match seconds using alternate numeric symbols. */
766 get_alt_number (0, 61);
772 get_alt_number (0, 53);
773 /* XXX This cannot determine any field in TM without
774 further information. */
777 /* Match number of weekday using alternate numeric symbols. */
778 get_alt_number (0, 6);
783 /* Match year within century using alternate numeric symbols. */
784 get_alt_number (0, 99);
785 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
800 if (want_century
&& century
!= -1)
801 tm
->tm_year
= tm
->tm_year
% 100 + (century
- 19) * 100;
803 if (want_xday
&& !have_wday
)
804 day_of_the_week (tm
);
805 if (want_xday
&& !have_yday
)
806 day_of_the_year (tm
);
813 strptime (buf
, format
, tm
)
818 enum locale_status decided
;
824 return strptime_internal (buf
, format
, tm
, &decided
);