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) \
91 if (*rp < '0' || *rp > '9') \
96 } while (val * 10 <= to && *rp >= '0' && *rp <= '9'); \
97 if (val < from || val > to) \
101 # define get_alt_number(from, to) \
103 if (*decided != raw) \
105 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
107 while (*alts != '\0') \
109 size_t len = strlen (alts); \
110 if (strncasecmp (alts, rp, len) == 0) \
112 alts = strchr (alts, '\0') + 1; \
117 if (*decided == loc && val != 0) \
126 get_number (from, to); \
129 # define get_alt_number(from, to) \
130 /* We don't have the alternate representation. */ \
133 #define recursive(new_fmt) \
134 (*(new_fmt) != '\0' \
135 && (rp = strptime_internal (rp, (new_fmt), tm, decided)) != NULL)
139 /* This is defined in locale/C-time.c in the GNU libc. */
140 extern const struct locale_data _nl_C_LC_TIME
;
141 extern const unsigned short int __mon_yday
[2][13];
143 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
144 # define ab_weekday_name \
145 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
146 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
147 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
148 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
149 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
150 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
151 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
152 # define HERE_T_FMT_AMPM \
153 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
154 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
156 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
158 static char const weekday_name
[][10] =
160 "Sunday", "Monday", "Tuesday", "Wednesday",
161 "Thursday", "Friday", "Saturday"
163 static char const ab_weekday_name
[][4] =
165 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
167 static char const month_name
[][10] =
169 "January", "February", "March", "April", "May", "June",
170 "July", "August", "September", "October", "November", "December"
172 static char const ab_month_name
[][4] =
174 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
175 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
177 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
178 # define HERE_D_FMT "%m/%d/%y"
179 # define HERE_AM_STR "AM"
180 # define HERE_PM_STR "PM"
181 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
182 # define HERE_T_FMT "%H:%M:%S"
184 const unsigned short int __mon_yday
[1][13] =
187 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
189 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
193 /* Status of lookup: do we use the locale data or the raw data? */
194 enum locale_status
{ not, loc
, raw
};
198 /* Nonzero if YEAR is a leap year (every 4 years,
199 except every 100th isn't, and every 400th is). */
200 # define __isleap(year) \
201 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
204 /* Compute the day of the week. */
206 day_of_the_week (struct tm
*tm
)
208 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
209 the difference between this data in the one on TM and so determine
211 int corr_year
= 1900 + tm
->tm_year
- (tm
->tm_mon
< 2);
213 + (365 * (tm
->tm_year
- 70))
215 - ((corr_year
/ 4) / 25) + ((corr_year
/ 4) % 25 < 0)
216 + (((corr_year
/ 4) / 25) / 4)
217 + __mon_yday
[0][tm
->tm_mon
]
219 tm
->tm_wday
= wday
% 7;
222 /* Compute the day of the year. */
224 day_of_the_year (struct tm
*tm
)
226 tm
->tm_yday
= (__mon_yday
[__isleap (1900 + tm
->tm_year
)][tm
->tm_mon
]
227 + (tm
->tm_mday
- 1));
234 strptime_internal
__P ((const char *buf
, const char *format
, struct tm
*tm
,
235 enum locale_status
*decided
));
241 strptime_internal (buf
, format
, tm
, decided
)
245 enum locale_status
*decided
;
252 int century
, want_century
;
253 int have_wday
, want_xday
;
261 have_wday
= want_xday
= have_yday
= 0;
265 /* A white space in the format string matches 0 more or white
266 space in the input string. */
269 while (isspace (*rp
))
275 /* Any character but `%' must be matched by the same character
276 in the iput string. */
279 match_char (*fmt
++, *rp
++);
285 /* We need this for handling the `E' modifier. */
291 /* Match the `%' character itself. */
292 match_char ('%', *rp
++);
296 /* Match day of week. */
297 for (cnt
= 0; cnt
< 7; ++cnt
)
302 if (match_string (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
), rp
))
305 && strcmp (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
),
310 if (match_string (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
), rp
))
313 && strcmp (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
),
314 ab_weekday_name
[cnt
]))
321 && (match_string (weekday_name
[cnt
], rp
)
322 || match_string (ab_weekday_name
[cnt
], rp
)))
329 /* Does not match a weekday name. */
337 /* Match month name. */
338 for (cnt
= 0; cnt
< 12; ++cnt
)
343 if (match_string (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
), rp
))
346 && strcmp (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
),
351 if (match_string (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
), rp
))
354 && strcmp (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
),
361 if (match_string (month_name
[cnt
], rp
)
362 || match_string (ab_month_name
[cnt
], rp
))
369 /* Does not match a month name. */
375 /* Match locale's date and time format. */
379 if (!recursive (_NL_CURRENT (LC_TIME
, D_T_FMT
)))
386 if (*decided
== not &&
387 strcmp (_NL_CURRENT (LC_TIME
, D_T_FMT
), HERE_D_T_FMT
))
395 if (!recursive (HERE_D_T_FMT
))
400 /* Match century number. */
407 /* Match day of month. */
413 if (!recursive ("%Y-%m-%d"))
421 if (!recursive (_NL_CURRENT (LC_TIME
, D_FMT
)))
429 && strcmp (_NL_CURRENT (LC_TIME
, D_FMT
), HERE_D_FMT
))
439 /* Match standard day format. */
440 if (!recursive (HERE_D_FMT
))
446 /* Match hour in 24-hour clock. */
452 /* Match hour in 12-hour clock. */
454 tm
->tm_hour
= val
% 12;
458 /* Match day number of year. */
460 tm
->tm_yday
= val
- 1;
464 /* Match number of month. */
466 tm
->tm_mon
= val
- 1;
476 /* Match any white space. */
477 while (isspace (*rp
))
481 /* Match locale's equivalent of AM/PM. */
485 if (match_string (_NL_CURRENT (LC_TIME
, AM_STR
), rp
))
487 if (strcmp (_NL_CURRENT (LC_TIME
, AM_STR
), HERE_AM_STR
))
491 if (match_string (_NL_CURRENT (LC_TIME
, PM_STR
), rp
))
493 if (strcmp (_NL_CURRENT (LC_TIME
, PM_STR
), HERE_PM_STR
))
501 if (!match_string (HERE_AM_STR
, rp
))
502 if (match_string (HERE_PM_STR
, rp
))
511 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
518 if (*decided
== not &&
519 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
527 if (!recursive (HERE_T_FMT_AMPM
))
531 if (!recursive ("%H:%M"))
536 /* The number of seconds may be very high so we cannot use
537 the `get_number' macro. Instead read the number
538 character for character and construct the result while
541 if (*rp
< '0' || *rp
> '9')
542 /* We need at least one digit. */
550 while (*rp
>= '0' && *rp
<= '9');
552 if (localtime_r (&secs
, tm
) == NULL
)
553 /* Error in function. */
565 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
572 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
581 if (!recursive (HERE_T_FMT
))
586 tm
->tm_wday
= val
% 7;
591 /* XXX This cannot determine any field in TM. */
594 if (*rp
< '0' || *rp
> '9')
596 /* XXX Ignore the number since we would need some more
597 information to compute a real date. */
600 while (*rp
>= '0' && *rp
<= '9');
606 /* XXX This cannot determine any field in TM without some
610 /* Match number of weekday. */
616 /* Match year within century. */
618 /* The "Year 2000: The Millennium Rollover" paper suggests that
619 values in the range 69-99 refer to the twentieth century. */
620 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
621 /* Indicate that we want to use the century, if specified. */
626 /* Match year including century number. */
627 get_number (0, 9999);
628 tm
->tm_year
= val
- 1900;
633 /* XXX How to handle this? */
640 /* Match locale's alternate date and time format. */
643 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
646 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
648 if (!recursive (fmt
))
655 if (strcmp (fmt
, HERE_D_T_FMT
))
662 /* The C locale has no era information, so use the
663 normal representation. */
664 if (!recursive (HERE_D_T_FMT
))
671 /* Match name of base year in locale's alternate
673 /* XXX This is currently not implemented. It should
674 use the value _NL_CURRENT (LC_TIME, ERA). */
679 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
682 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
684 if (!recursive (fmt
))
691 if (strcmp (fmt
, HERE_D_FMT
))
697 if (!recursive (HERE_D_FMT
))
703 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
706 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
708 if (!recursive (fmt
))
715 if (strcmp (fmt
, HERE_T_FMT
))
721 if (!recursive (HERE_T_FMT
))
729 /* We have no information about the era format. Just use
730 the normal format. */
731 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
732 && *fmt
!= 'x' && *fmt
!= 'X')
733 /* This is an illegal format. */
743 /* Match day of month using alternate numeric symbols. */
744 get_alt_number (1, 31);
749 /* Match hour in 24-hour clock using alternate numeric
751 get_alt_number (0, 23);
756 /* Match hour in 12-hour clock using alternate numeric
758 get_alt_number (1, 12);
759 tm
->tm_hour
= val
- 1;
763 /* Match month using alternate numeric symbols. */
764 get_alt_number (1, 12);
765 tm
->tm_mon
= val
- 1;
769 /* Match minutes using alternate numeric symbols. */
770 get_alt_number (0, 59);
774 /* Match seconds using alternate numeric symbols. */
775 get_alt_number (0, 61);
781 get_alt_number (0, 53);
782 /* XXX This cannot determine any field in TM without
783 further information. */
786 /* Match number of weekday using alternate numeric symbols. */
787 get_alt_number (0, 6);
792 /* Match year within century using alternate numeric symbols. */
793 get_alt_number (0, 99);
794 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
809 if (want_century
&& century
!= -1)
810 tm
->tm_year
= tm
->tm_year
% 100 + (century
- 19) * 100;
812 if (want_xday
&& !have_wday
)
813 day_of_the_week (tm
);
814 if (want_xday
&& !have_yday
)
815 day_of_the_year (tm
);
822 strptime (buf
, format
, tm
)
827 enum locale_status decided
;
833 return strptime_internal (buf
, format
, tm
, &decided
);