4 Convert a string representation of time to a time value.
5 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
6 This file is part of the GNU C Library.
7 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
9 The GNU C Library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
14 The GNU C Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with the GNU C Library; see the file COPYING.LIB. If not,
21 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301 USA. */
24 /* XXX This version of the implementation is not really complete.
25 Some of the fields cannot add information alone. But if seeing
26 some of them in the same format (such as year, week and weekday)
27 this is enough information for determining the date. */
37 # include "../locale/localeinfo.h"
43 # if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
44 # 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
;
279 struct era_entry
*era
;
287 have_wday
= want_xday
= have_yday
= have_mon
= have_mday
= 0;
291 /* A white space in the format string matches 0 more or white
292 space in the input string. */
295 while (isspace (*rp
))
301 /* Any character but `%' must be matched by the same character
302 in the iput string. */
305 match_char (*fmt
++, *rp
++);
311 /* We need this for handling the `E' modifier. */
315 /* Make back up of current processing pointer. */
321 /* Match the `%' character itself. */
322 match_char ('%', *rp
++);
326 /* Match day of week. */
327 for (cnt
= 0; cnt
< 7; ++cnt
)
332 if (match_string (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
), rp
))
335 && strcmp (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
),
340 if (match_string (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
), rp
))
343 && strcmp (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
),
344 ab_weekday_name
[cnt
]))
351 && (match_string (weekday_name
[cnt
], rp
)
352 || match_string (ab_weekday_name
[cnt
], rp
)))
359 /* Does not match a weekday name. */
367 /* Match month name. */
368 for (cnt
= 0; cnt
< 12; ++cnt
)
373 if (match_string (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
), rp
))
376 && strcmp (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
),
381 if (match_string (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
), rp
))
384 && strcmp (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
),
391 if (match_string (month_name
[cnt
], rp
)
392 || match_string (ab_month_name
[cnt
], rp
))
399 /* Does not match a month name. */
405 /* Match locale's date and time format. */
409 if (!recursive (_NL_CURRENT (LC_TIME
, D_T_FMT
)))
418 if (*decided
== not &&
419 strcmp (_NL_CURRENT (LC_TIME
, D_T_FMT
), HERE_D_T_FMT
))
427 if (!recursive (HERE_D_T_FMT
))
432 /* 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
))
551 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
560 if (*decided
== not &&
561 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
569 if (!recursive (HERE_T_FMT_AMPM
))
573 if (!recursive ("%H:%M"))
578 /* The number of seconds may be very high so we cannot use
579 the `get_number' macro. Instead read the number
580 character for character and construct the result while
583 if (*rp
< '0' || *rp
> '9')
584 /* We need at least one digit. */
592 while (*rp
>= '0' && *rp
<= '9');
594 if (localtime_r (&secs
, tm
) == NULL
)
595 /* Error in function. */
600 get_number (0, 61, 2);
607 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
616 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
625 if (!recursive (HERE_T_FMT
))
629 get_number (1, 7, 1);
630 tm
->tm_wday
= val
% 7;
634 get_number (0, 99, 2);
635 /* XXX This cannot determine any field in TM. */
638 if (*rp
< '0' || *rp
> '9')
640 /* XXX Ignore the number since we would need some more
641 information to compute a real date. */
644 while (*rp
>= '0' && *rp
<= '9');
649 get_number (0, 53, 2);
650 /* XXX This cannot determine any field in TM without some
654 /* Match number of weekday. */
655 get_number (0, 6, 1);
661 match_year_in_century
:
663 /* Match year within century. */
664 get_number (0, 99, 2);
665 /* The "Year 2000: The Millennium Rollover" paper suggests that
666 values in the range 69-99 refer to the twentieth century. */
667 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
668 /* Indicate that we want to use the century, if specified. */
673 /* Match year including century number. */
674 get_number (0, 9999, 4);
675 tm
->tm_year
= val
- 1900;
680 /* XXX How to handle this? */
687 /* Match locale's alternate date and time format. */
690 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
693 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
695 if (!recursive (fmt
))
704 if (strcmp (fmt
, HERE_D_T_FMT
))
711 /* The C locale has no era information, so use the
712 normal representation. */
713 if (!recursive (HERE_D_T_FMT
))
722 era
= _nl_select_era_entry (era_cnt
);
723 if (match_string (era
->era_name
, rp
))
733 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
734 _NL_TIME_ERA_NUM_ENTRIES
);
735 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
736 ++era_cnt
, rp
= rp_backup
)
738 era
= _nl_select_era_entry (era_cnt
);
739 if (match_string (era
->era_name
, rp
))
745 if (era_cnt
== (int) num_eras
)
757 /* The C locale has no era information, so use the
758 normal representation. */
762 goto match_year_in_century
;
764 get_number(0, 9999, 4);
772 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
773 _NL_TIME_ERA_NUM_ENTRIES
);
774 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
775 ++era_cnt
, rp
= rp_backup
)
777 era
= _nl_select_era_entry (era_cnt
);
778 if (recursive (era
->era_format
))
781 if (era_cnt
== (int) num_eras
)
798 get_number (0, 9999, 4);
799 tm
->tm_year
= val
- 1900;
806 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
809 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
811 if (!recursive (fmt
))
820 if (strcmp (fmt
, HERE_D_FMT
))
826 if (!recursive (HERE_D_FMT
))
832 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
835 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
837 if (!recursive (fmt
))
846 if (strcmp (fmt
, HERE_T_FMT
))
852 if (!recursive (HERE_T_FMT
))
860 /* We have no information about the era format. Just use
861 the normal format. */
862 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
863 && *fmt
!= 'x' && *fmt
!= 'X')
864 /* This is an illegal format. */
874 /* Match day of month using alternate numeric symbols. */
875 get_alt_number (1, 31, 2);
881 /* Match hour in 24-hour clock using alternate numeric
883 get_alt_number (0, 23, 2);
888 /* Match hour in 12-hour clock using alternate numeric
890 get_alt_number (1, 12, 2);
891 tm
->tm_hour
= val
- 1;
895 /* Match month using alternate numeric symbols. */
896 get_alt_number (1, 12, 2);
897 tm
->tm_mon
= val
- 1;
902 /* Match minutes using alternate numeric symbols. */
903 get_alt_number (0, 59, 2);
907 /* Match seconds using alternate numeric symbols. */
908 get_alt_number (0, 61, 2);
914 get_alt_number (0, 53, 2);
915 /* XXX This cannot determine any field in TM without
916 further information. */
919 /* Match number of weekday using alternate numeric symbols. */
920 get_alt_number (0, 6, 1);
925 /* Match year within century using alternate numeric symbols. */
926 get_alt_number (0, 99, 2);
927 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
945 tm
->tm_year
= tm
->tm_year
% 100 + (century
- 19) * 100;
947 /* Only the century, but not the year. Strange, but so be it. */
948 tm
->tm_year
= (century
- 19) * 100;
954 era
= _nl_select_era_entry(era_cnt
);
956 tm
->tm_year
= (era
->start_date
[0]
957 + ((tm
->tm_year
- era
->offset
)
958 * era
->absolute_direction
));
960 /* Era start year assumed. */
961 tm
->tm_year
= era
->start_date
[0];
968 if (want_xday
&& !have_wday
)
970 if ( !(have_mon
&& have_mday
) && have_yday
)
972 /* We don't have tm_mon and/or tm_mday, compute them. */
974 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
] <= tm
->tm_yday
)
977 tm
->tm_mon
= t_mon
- 1;
981 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
983 day_of_the_week (tm
);
985 if (want_xday
&& !have_yday
)
986 day_of_the_year (tm
);
993 strptime (buf
, format
, tm
)
998 enum locale_status decided
;
1005 return strptime_internal (buf
, format
, tm
, &decided
, -1);