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; \
106 if (*decided != raw) \
108 val = _nl_parse_alt_digit (&rp); \
109 if (val == -1 && *decided != loc) \
114 if (val < from || val > to) \
120 get_number (from, to, n); \
125 # define get_alt_number(from, to, n) \
126 /* We don't have the alternate representation. */ \
127 get_number(from, to, n)
129 #define recursive(new_fmt) \
130 (*(new_fmt) != '\0' \
131 && (rp = strptime_internal (rp, (new_fmt), tm, decided, era_cnt)) != NULL)
135 /* This is defined in locale/C-time.c in the GNU libc. */
136 extern const struct locale_data _nl_C_LC_TIME
;
137 extern const unsigned short int __mon_yday
[2][13];
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_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"
180 const unsigned short int __mon_yday
[2][13] =
183 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
185 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
189 /* Status of lookup: do we use the locale data or the raw data? */
190 enum locale_status
{ not, loc
, raw
};
194 /* Nonzero if YEAR is a leap year (every 4 years,
195 except every 100th isn't, and every 400th is). */
196 # define __isleap(year) \
197 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
200 /* Compute the day of the week. */
202 day_of_the_week (struct tm
*tm
)
204 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
205 the difference between this data in the one on TM and so determine
207 int corr_year
= 1900 + tm
->tm_year
- (tm
->tm_mon
< 2);
209 + (365 * (tm
->tm_year
- 70))
211 - ((corr_year
/ 4) / 25) + ((corr_year
/ 4) % 25 < 0)
212 + (((corr_year
/ 4) / 25) / 4)
213 + __mon_yday
[0][tm
->tm_mon
]
215 tm
->tm_wday
= ((wday
% 7) + 7) % 7;
218 /* Compute the day of the year. */
220 day_of_the_year (struct tm
*tm
)
222 tm
->tm_yday
= (__mon_yday
[__isleap (1900 + tm
->tm_year
)][tm
->tm_mon
]
223 + (tm
->tm_mday
- 1));
230 strptime_internal
__P ((const char *rp
, const char *fmt
, struct tm
*tm
,
231 enum locale_status
*decided
, int era_cnt
));
237 strptime_internal (rp
, fmt
, tm
, decided
, era_cnt
)
241 enum locale_status
*decided
;
244 const char *rp_backup
;
248 int century
, want_century
;
250 int have_wday
, want_xday
;
252 int have_mon
, have_mday
;
253 int have_uweek
, have_wweek
;
256 struct era_entry
*era
;
265 have_wday
= want_xday
= have_yday
= have_mon
= have_mday
= have_uweek
= 0;
270 /* A white space in the format string matches 0 more or white
271 space in the input string. */
274 while (isspace (*rp
))
280 /* Any character but `%' must be matched by the same character
281 in the iput string. */
284 match_char (*fmt
++, *rp
++);
290 /* We need this for handling the `E' modifier. */
294 /* Make back up of current processing pointer. */
300 /* Match the `%' character itself. */
301 match_char ('%', *rp
++);
305 /* Match day of week. */
306 for (cnt
= 0; cnt
< 7; ++cnt
)
311 if (match_string (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
), rp
))
314 && strcmp (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
),
319 if (match_string (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
), rp
))
322 && strcmp (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
),
323 ab_weekday_name
[cnt
]))
330 && (match_string (weekday_name
[cnt
], rp
)
331 || match_string (ab_weekday_name
[cnt
], rp
)))
338 /* Does not match a weekday name. */
346 /* Match month name. */
347 for (cnt
= 0; cnt
< 12; ++cnt
)
352 if (match_string (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
), rp
))
355 && strcmp (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
),
360 if (match_string (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
), rp
))
363 && strcmp (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
),
370 if (match_string (month_name
[cnt
], rp
)
371 || match_string (ab_month_name
[cnt
], rp
))
378 /* Does not match a month name. */
384 /* Match locale's date and time format. */
388 if (!recursive (_NL_CURRENT (LC_TIME
, D_T_FMT
)))
397 if (*decided
== not &&
398 strcmp (_NL_CURRENT (LC_TIME
, D_T_FMT
), HERE_D_T_FMT
))
406 if (!recursive (HERE_D_T_FMT
))
411 /* Match century number. */
413 get_number (0, 99, 2);
419 /* Match day of month. */
420 get_number (1, 31, 2);
426 if (!recursive ("%Y-%m-%d"))
434 if (!recursive (_NL_CURRENT (LC_TIME
, D_FMT
)))
444 && strcmp (_NL_CURRENT (LC_TIME
, D_FMT
), HERE_D_FMT
))
454 /* Match standard day format. */
455 if (!recursive (HERE_D_FMT
))
461 /* Match hour in 24-hour clock. */
462 get_number (0, 23, 2);
467 /* Match hour in 12-hour clock. GNU extension. */
469 /* Match hour in 12-hour clock. */
470 get_number (1, 12, 2);
471 tm
->tm_hour
= val
% 12;
475 /* Match day number of year. */
476 get_number (1, 366, 3);
477 tm
->tm_yday
= val
- 1;
481 /* Match number of month. */
482 get_number (1, 12, 2);
483 tm
->tm_mon
= val
- 1;
489 get_number (0, 59, 2);
494 /* Match any white space. */
495 while (isspace (*rp
))
499 /* Match locale's equivalent of AM/PM. */
503 if (match_string (_NL_CURRENT (LC_TIME
, AM_STR
), rp
))
505 if (strcmp (_NL_CURRENT (LC_TIME
, AM_STR
), HERE_AM_STR
))
509 if (match_string (_NL_CURRENT (LC_TIME
, PM_STR
), rp
))
511 if (strcmp (_NL_CURRENT (LC_TIME
, PM_STR
), HERE_PM_STR
))
519 if (!match_string (HERE_AM_STR
, rp
))
520 if (match_string (HERE_PM_STR
, rp
))
529 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
538 if (*decided
== not &&
539 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
547 if (!recursive (HERE_T_FMT_AMPM
))
551 if (!recursive ("%H:%M"))
556 /* The number of seconds may be very high so we cannot use
557 the `get_number' macro. Instead read the number
558 character for character and construct the result while
561 if (*rp
< '0' || *rp
> '9')
562 /* We need at least one digit. */
570 while (*rp
>= '0' && *rp
<= '9');
572 if (localtime_r (&secs
, tm
) == NULL
)
573 /* Error in function. */
578 get_number (0, 61, 2);
585 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
594 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
603 if (!recursive (HERE_T_FMT
))
607 get_number (1, 7, 1);
608 tm
->tm_wday
= val
% 7;
612 get_number (0, 99, 2);
613 /* XXX This cannot determine any field in TM. */
616 if (*rp
< '0' || *rp
> '9')
618 /* XXX Ignore the number since we would need some more
619 information to compute a real date. */
622 while (*rp
>= '0' && *rp
<= '9');
625 get_number (0, 53, 2);
630 get_number (0, 53, 2);
635 get_number (0, 53, 2);
636 /* XXX This cannot determine any field in TM without some
640 /* Match number of weekday. */
641 get_number (0, 6, 1);
646 match_year_in_century
:
647 /* Match year within century. */
648 get_number (0, 99, 2);
649 /* The "Year 2000: The Millennium Rollover" paper suggests that
650 values in the range 69-99 refer to the twentieth century. */
651 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
652 /* Indicate that we want to use the century, if specified. */
657 /* Match year including century number. */
658 get_number (0, 9999, 4);
659 tm
->tm_year
= val
- 1900;
664 /* XXX How to handle this? */
671 /* Match locale's alternate date and time format. */
674 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
677 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
679 if (!recursive (fmt
))
688 if (strcmp (fmt
, HERE_D_T_FMT
))
695 /* The C locale has no era information, so use the
696 normal representation. */
697 if (!recursive (HERE_D_T_FMT
))
706 era
= _nl_select_era_entry (era_cnt
);
707 if (match_string (era
->era_name
, rp
))
717 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
718 _NL_TIME_ERA_NUM_ENTRIES
);
719 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
720 ++era_cnt
, rp
= rp_backup
)
722 era
= _nl_select_era_entry (era_cnt
);
723 if (match_string (era
->era_name
, rp
))
729 if (era_cnt
== (int) num_eras
)
741 /* The C locale has no era information, so use the
742 normal representation. */
746 goto match_year_in_century
;
748 get_number(0, 9999, 4);
757 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
758 _NL_TIME_ERA_NUM_ENTRIES
);
759 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
760 ++era_cnt
, rp
= rp_backup
)
762 era
= _nl_select_era_entry (era_cnt
);
763 if (recursive (era
->era_format
))
766 if (era_cnt
== (int) num_eras
)
783 get_number (0, 9999, 4);
784 tm
->tm_year
= val
- 1900;
791 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
794 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
796 if (!recursive (fmt
))
805 if (strcmp (fmt
, HERE_D_FMT
))
811 if (!recursive (HERE_D_FMT
))
817 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
820 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
822 if (!recursive (fmt
))
831 if (strcmp (fmt
, HERE_T_FMT
))
837 if (!recursive (HERE_T_FMT
))
845 /* We have no information about the era format. Just use
846 the normal format. */
847 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
848 && *fmt
!= 'x' && *fmt
!= 'X')
849 /* This is an illegal format. */
859 /* Match day of month using alternate numeric symbols. */
860 get_alt_number (1, 31, 2);
866 /* Match hour in 24-hour clock using alternate numeric
868 get_alt_number (0, 23, 2);
873 /* Match hour in 12-hour clock using alternate numeric
875 get_alt_number (1, 12, 2);
876 tm
->tm_hour
= val
% 12;
880 /* Match month using alternate numeric symbols. */
881 get_alt_number (1, 12, 2);
882 tm
->tm_mon
= val
- 1;
887 /* Match minutes using alternate numeric symbols. */
888 get_alt_number (0, 59, 2);
892 /* Match seconds using alternate numeric symbols. */
893 get_alt_number (0, 61, 2);
897 get_alt_number (0, 53, 2);
902 get_alt_number (0, 53, 2);
907 get_alt_number (0, 53, 2);
908 /* XXX This cannot determine any field in TM without
909 further information. */
912 /* Match number of weekday using alternate numeric symbols. */
913 get_alt_number (0, 6, 1);
918 /* Match year within century using alternate numeric symbols. */
919 get_alt_number (0, 99, 2);
920 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
938 tm
->tm_year
= tm
->tm_year
% 100 + (century
- 19) * 100;
940 /* Only the century, but not the year. Strange, but so be it. */
941 tm
->tm_year
= (century
- 19) * 100;
946 era
= _nl_select_era_entry (era_cnt
);
948 tm
->tm_year
= (era
->start_date
[0]
949 + ((tm
->tm_year
- era
->offset
)
950 * era
->absolute_direction
));
952 /* Era start year assumed. */
953 tm
->tm_year
= era
->start_date
[0];
958 /* No era found but we have seen an E modifier. Rectify some
960 if (want_century
&& century
== -1 && tm
->tm_year
< 69)
964 if (want_xday
&& !have_wday
)
966 if ( !(have_mon
&& have_mday
) && have_yday
)
968 /* We don't have tm_mon and/or tm_mday, compute them. */
970 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
] <= tm
->tm_yday
)
973 tm
->tm_mon
= t_mon
- 1;
977 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
979 day_of_the_week (tm
);
982 if (want_xday
&& !have_yday
)
983 day_of_the_year (tm
);
985 if ((have_uweek
|| have_wweek
) && have_wday
)
987 int save_wday
= tm
->tm_wday
;
988 int save_mday
= tm
->tm_mday
;
989 int save_mon
= tm
->tm_mon
;
990 int w_offset
= have_uweek
? 0 : 1;
994 day_of_the_week (tm
);
996 tm
->tm_mday
= save_mday
;
998 tm
->tm_mon
= save_mon
;
1001 tm
->tm_yday
= ((7 - (tm
->tm_wday
- w_offset
)) % 7
1003 + save_wday
- w_offset
);
1005 if (!have_mday
|| !have_mon
)
1008 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
]
1012 tm
->tm_mon
= t_mon
- 1;
1016 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
1019 tm
->tm_wday
= save_wday
;
1027 strptime (buf
, format
, tm
)
1032 enum locale_status decided
;
1039 return strptime_internal (buf
, format
, tm
, &decided
, -1);