1 /* Convert a string representation of time to a time value.
2 Copyright (C) 1996-2000, 2001 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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, n) \
92 if (*rp < '0' || *rp > '9') \
97 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
98 if (val < from || val > to) \
102 # define get_alt_number(from, to, n) \
104 __label__ do_normal; \
105 if (*decided != raw) \
107 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
115 while (*alts != '\0') \
117 size_t len = strlen (alts); \
118 if (strncasecmp (alts, rp, len) == 0) \
125 if (*decided == not && ! any) \
127 /* If we haven't read anything it's an error. */ \
130 /* Correct the premature multiplication. */ \
136 } while (--__n > 0 && val * 10 <= to); \
137 if (val < from || val > to) \
143 get_number (from, to, n); \
148 # define get_alt_number(from, to, n) \
149 /* We don't have the alternate representation. */ \
150 get_number(from, to, n)
152 #define recursive(new_fmt) \
153 (*(new_fmt) != '\0' \
154 && (rp = strptime_internal (rp, (new_fmt), tm, decided, era_cnt)) != NULL)
158 /* This is defined in locale/C-time.c in the GNU libc. */
159 extern const struct locale_data _nl_C_LC_TIME
;
160 extern const unsigned short int __mon_yday
[2][13];
162 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
163 # define ab_weekday_name \
164 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
165 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
166 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
167 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
168 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
169 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
170 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
171 # define HERE_T_FMT_AMPM \
172 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
173 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
175 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
177 static char const weekday_name
[][10] =
179 "Sunday", "Monday", "Tuesday", "Wednesday",
180 "Thursday", "Friday", "Saturday"
182 static char const ab_weekday_name
[][4] =
184 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
186 static char const month_name
[][10] =
188 "January", "February", "March", "April", "May", "June",
189 "July", "August", "September", "October", "November", "December"
191 static char const ab_month_name
[][4] =
193 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
194 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
196 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
197 # define HERE_D_FMT "%m/%d/%y"
198 # define HERE_AM_STR "AM"
199 # define HERE_PM_STR "PM"
200 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
201 # define HERE_T_FMT "%H:%M:%S"
203 const unsigned short int __mon_yday
[2][13] =
206 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
208 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
212 /* Status of lookup: do we use the locale data or the raw data? */
213 enum locale_status
{ not, loc
, raw
};
217 /* Nonzero if YEAR is a leap year (every 4 years,
218 except every 100th isn't, and every 400th is). */
219 # define __isleap(year) \
220 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
223 /* Compute the day of the week. */
225 day_of_the_week (struct tm
*tm
)
227 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
228 the difference between this data in the one on TM and so determine
230 int corr_year
= 1900 + tm
->tm_year
- (tm
->tm_mon
< 2);
232 + (365 * (tm
->tm_year
- 70))
234 - ((corr_year
/ 4) / 25) + ((corr_year
/ 4) % 25 < 0)
235 + (((corr_year
/ 4) / 25) / 4)
236 + __mon_yday
[0][tm
->tm_mon
]
238 tm
->tm_wday
= ((wday
% 7) + 7) % 7;
241 /* Compute the day of the year. */
243 day_of_the_year (struct tm
*tm
)
245 tm
->tm_yday
= (__mon_yday
[__isleap (1900 + tm
->tm_year
)][tm
->tm_mon
]
246 + (tm
->tm_mday
- 1));
253 strptime_internal
__P ((const char *rp
, const char *fmt
, struct tm
*tm
,
254 enum locale_status
*decided
, int era_cnt
));
260 strptime_internal (rp
, fmt
, tm
, decided
, era_cnt
)
264 enum locale_status
*decided
;
267 const char *rp_backup
;
271 int century
, want_century
;
273 int have_wday
, want_xday
;
275 int have_mon
, have_mday
;
276 int have_uweek
, have_wweek
;
279 struct era_entry
*era
;
288 have_wday
= want_xday
= have_yday
= have_mon
= have_mday
= have_uweek
= 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. */
436 get_number (0, 99, 2);
442 /* Match day of month. */
443 get_number (1, 31, 2);
449 if (!recursive ("%Y-%m-%d"))
457 if (!recursive (_NL_CURRENT (LC_TIME
, D_FMT
)))
467 && strcmp (_NL_CURRENT (LC_TIME
, D_FMT
), HERE_D_FMT
))
477 /* Match standard day format. */
478 if (!recursive (HERE_D_FMT
))
484 /* Match hour in 24-hour clock. */
485 get_number (0, 23, 2);
490 /* Match hour in 12-hour clock. */
491 get_number (1, 12, 2);
492 tm
->tm_hour
= val
% 12;
496 /* Match day number of year. */
497 get_number (1, 366, 3);
498 tm
->tm_yday
= val
- 1;
502 /* Match number of month. */
503 get_number (1, 12, 2);
504 tm
->tm_mon
= val
- 1;
510 get_number (0, 59, 2);
515 /* Match any white space. */
516 while (isspace (*rp
))
520 /* Match locale's equivalent of AM/PM. */
524 if (match_string (_NL_CURRENT (LC_TIME
, AM_STR
), rp
))
526 if (strcmp (_NL_CURRENT (LC_TIME
, AM_STR
), HERE_AM_STR
))
530 if (match_string (_NL_CURRENT (LC_TIME
, PM_STR
), rp
))
532 if (strcmp (_NL_CURRENT (LC_TIME
, PM_STR
), HERE_PM_STR
))
540 if (!match_string (HERE_AM_STR
, rp
))
541 if (match_string (HERE_PM_STR
, rp
))
550 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
559 if (*decided
== not &&
560 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
568 if (!recursive (HERE_T_FMT_AMPM
))
572 if (!recursive ("%H:%M"))
577 /* The number of seconds may be very high so we cannot use
578 the `get_number' macro. Instead read the number
579 character for character and construct the result while
582 if (*rp
< '0' || *rp
> '9')
583 /* We need at least one digit. */
591 while (*rp
>= '0' && *rp
<= '9');
593 if (localtime_r (&secs
, tm
) == NULL
)
594 /* Error in function. */
599 get_number (0, 61, 2);
606 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
615 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
624 if (!recursive (HERE_T_FMT
))
628 get_number (1, 7, 1);
629 tm
->tm_wday
= val
% 7;
633 get_number (0, 99, 2);
634 /* XXX This cannot determine any field in TM. */
637 if (*rp
< '0' || *rp
> '9')
639 /* XXX Ignore the number since we would need some more
640 information to compute a real date. */
643 while (*rp
>= '0' && *rp
<= '9');
646 get_number (0, 53, 2);
651 get_number (0, 53, 2);
656 get_number (0, 53, 2);
657 /* XXX This cannot determine any field in TM without some
661 /* Match number of weekday. */
662 get_number (0, 6, 1);
667 match_year_in_century
:
668 /* Match year within century. */
669 get_number (0, 99, 2);
670 /* The "Year 2000: The Millennium Rollover" paper suggests that
671 values in the range 69-99 refer to the twentieth century. */
672 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
673 /* Indicate that we want to use the century, if specified. */
678 /* Match year including century number. */
679 get_number (0, 9999, 4);
680 tm
->tm_year
= val
- 1900;
685 /* XXX How to handle this? */
692 /* Match locale's alternate date and time format. */
695 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
698 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
700 if (!recursive (fmt
))
709 if (strcmp (fmt
, HERE_D_T_FMT
))
716 /* The C locale has no era information, so use the
717 normal representation. */
718 if (!recursive (HERE_D_T_FMT
))
727 era
= _nl_select_era_entry (era_cnt
);
728 if (match_string (era
->era_name
, rp
))
738 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
739 _NL_TIME_ERA_NUM_ENTRIES
);
740 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
741 ++era_cnt
, rp
= rp_backup
)
743 era
= _nl_select_era_entry (era_cnt
);
744 if (match_string (era
->era_name
, rp
))
750 if (era_cnt
== (int) num_eras
)
762 /* The C locale has no era information, so use the
763 normal representation. */
767 goto match_year_in_century
;
769 get_number(0, 9999, 4);
777 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
778 _NL_TIME_ERA_NUM_ENTRIES
);
779 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
780 ++era_cnt
, rp
= rp_backup
)
782 era
= _nl_select_era_entry (era_cnt
);
783 if (recursive (era
->era_format
))
786 if (era_cnt
== (int) num_eras
)
803 get_number (0, 9999, 4);
804 tm
->tm_year
= val
- 1900;
811 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
814 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
816 if (!recursive (fmt
))
825 if (strcmp (fmt
, HERE_D_FMT
))
831 if (!recursive (HERE_D_FMT
))
837 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
840 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
842 if (!recursive (fmt
))
851 if (strcmp (fmt
, HERE_T_FMT
))
857 if (!recursive (HERE_T_FMT
))
865 /* We have no information about the era format. Just use
866 the normal format. */
867 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
868 && *fmt
!= 'x' && *fmt
!= 'X')
869 /* This is an illegal format. */
879 /* Match day of month using alternate numeric symbols. */
880 get_alt_number (1, 31, 2);
886 /* Match hour in 24-hour clock using alternate numeric
888 get_alt_number (0, 23, 2);
893 /* Match hour in 12-hour clock using alternate numeric
895 get_alt_number (1, 12, 2);
896 tm
->tm_hour
= val
- 1;
900 /* Match month using alternate numeric symbols. */
901 get_alt_number (1, 12, 2);
902 tm
->tm_mon
= val
- 1;
907 /* Match minutes using alternate numeric symbols. */
908 get_alt_number (0, 59, 2);
912 /* Match seconds using alternate numeric symbols. */
913 get_alt_number (0, 61, 2);
917 get_alt_number (0, 53, 2);
922 get_alt_number (0, 53, 2);
927 get_alt_number (0, 53, 2);
928 /* XXX This cannot determine any field in TM without
929 further information. */
932 /* Match number of weekday using alternate numeric symbols. */
933 get_alt_number (0, 6, 1);
938 /* Match year within century using alternate numeric symbols. */
939 get_alt_number (0, 99, 2);
940 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
958 tm
->tm_year
= tm
->tm_year
% 100 + (century
- 19) * 100;
960 /* Only the century, but not the year. Strange, but so be it. */
961 tm
->tm_year
= (century
- 19) * 100;
966 era
= _nl_select_era_entry(era_cnt
);
968 tm
->tm_year
= (era
->start_date
[0]
969 + ((tm
->tm_year
- era
->offset
)
970 * era
->absolute_direction
));
972 /* Era start year assumed. */
973 tm
->tm_year
= era
->start_date
[0];
979 if (want_xday
&& !have_wday
)
981 if ( !(have_mon
&& have_mday
) && have_yday
)
983 /* We don't have tm_mon and/or tm_mday, compute them. */
985 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
] <= tm
->tm_yday
)
988 tm
->tm_mon
= t_mon
- 1;
992 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
994 day_of_the_week (tm
);
996 if (want_xday
&& !have_yday
)
997 day_of_the_year (tm
);
998 if ((have_uweek
|| have_wweek
) && have_wday
)
1000 int save_wday
= tm
->tm_wday
;
1001 int save_mday
= tm
->tm_mday
;
1002 int save_mon
= tm
->tm_mon
;
1003 int w_offset
= have_uweek
? 0 : 1;
1007 day_of_the_week (tm
);
1009 tm
->tm_mday
= save_mday
;
1011 tm
->tm_mon
= save_mon
;
1014 tm
->tm_yday
= ((7 - (tm
->tm_wday
- w_offset
)) % 7
1016 + save_wday
- w_offset
);
1018 if (!have_mday
|| !have_mon
)
1021 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
]
1025 tm
->tm_mon
= t_mon
- 1;
1029 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
1032 tm
->tm_wday
= save_wday
;
1040 strptime (buf
, format
, tm
)
1045 enum locale_status decided
;
1052 return strptime_internal (buf
, format
, tm
, &decided
, -1);