1 /* Convert a string representation of time to a time value.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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 License as
8 published by the Free Software Foundation; either version 3 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 Lesser General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 see <http://www.gnu.org/licenses/>. */
20 /* XXX This version of the implementation is not really complete.
21 Some of the fields cannot add information alone. But if seeing
22 some of them in the same format (such as year, week and weekday)
23 this is enough information for determining the date. */
26 #include "system/locale.h"
27 #include "system/time.h"
30 # if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
31 # define __P(args) args
37 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
39 # define localtime_r __localtime_r
41 /* Approximate localtime_r as best we can in its absence. */
42 # define localtime_r my_localtime_r
43 static struct tm
*localtime_r
__P ((const time_t *, struct tm
*));
49 struct tm
*l
= localtime (t
);
56 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
59 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
60 #if defined __GNUC__ && __GNUC__ >= 2
61 # define match_string(cs1, s2) \
62 ({ size_t len = strlen (cs1); \
63 int result = strncasecmp ((cs1), (s2), len) == 0; \
64 if (result) (s2) += len; \
67 /* Oh come on. Get a reasonable compiler. */
68 # define match_string(cs1, s2) \
69 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
71 /* We intentionally do not use isdigit() for testing because this will
72 lead to problems with the wide character version. */
73 #define get_number(from, to, n) \
79 if (*rp < '0' || *rp > '9') \
84 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
85 if (val < from || val > to) \
89 # define get_alt_number(from, to, n) \
91 __label__ do_normal; \
92 if (*decided != raw) \
94 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
102 while (*alts != '\0') \
104 size_t len = strlen (alts); \
105 if (strncasecmp (alts, rp, len) == 0) \
112 if (*decided == not && ! any) \
114 /* If we haven't read anything it's an error. */ \
117 /* Correct the premature multiplication. */ \
123 } while (--__n > 0 && val * 10 <= to); \
124 if (val < from || val > to) \
130 get_number (from, to, n); \
135 # define get_alt_number(from, to, n) \
136 /* We don't have the alternate representation. */ \
137 get_number(from, to, n)
139 #define recursive(new_fmt) \
140 (*(new_fmt) != '\0' \
141 && (rp = strptime_internal (rp, (new_fmt), tm, decided, era_cnt)) != NULL)
145 /* This is defined in locale/C-time.c in the GNU libc. */
146 extern const struct locale_data _nl_C_LC_TIME
;
147 extern const unsigned short int __mon_yday
[2][13];
149 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
150 # define ab_weekday_name \
151 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
152 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
153 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
154 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
155 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
156 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
157 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
158 # define HERE_T_FMT_AMPM \
159 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
160 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
162 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
164 static char const weekday_name
[][10] =
166 "Sunday", "Monday", "Tuesday", "Wednesday",
167 "Thursday", "Friday", "Saturday"
169 static char const ab_weekday_name
[][4] =
171 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
173 static char const month_name
[][10] =
175 "January", "February", "March", "April", "May", "June",
176 "July", "August", "September", "October", "November", "December"
178 static char const ab_month_name
[][4] =
180 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
181 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
183 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
184 # define HERE_D_FMT "%m/%d/%y"
185 # define HERE_AM_STR "AM"
186 # define HERE_PM_STR "PM"
187 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
188 # define HERE_T_FMT "%H:%M:%S"
190 static const unsigned short int __mon_yday
[2][13] =
193 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
195 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
199 /* Status of lookup: do we use the locale data or the raw data? */
200 enum locale_status
{ not, loc
, raw
};
204 /* Nonzero if YEAR is a leap year (every 4 years,
205 except every 100th isn't, and every 400th is). */
206 # define __isleap(year) \
207 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
210 /* Compute the day of the week. */
212 day_of_the_week (struct tm
*tm
)
214 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
215 the difference between this data in the one on TM and so determine
217 int corr_year
= 1900 + tm
->tm_year
- (tm
->tm_mon
< 2);
219 + (365 * (tm
->tm_year
- 70))
221 - ((corr_year
/ 4) / 25) + ((corr_year
/ 4) % 25 < 0)
222 + (((corr_year
/ 4) / 25) / 4)
223 + __mon_yday
[0][tm
->tm_mon
]
225 tm
->tm_wday
= ((wday
% 7) + 7) % 7;
228 /* Compute the day of the year. */
230 day_of_the_year (struct tm
*tm
)
232 tm
->tm_yday
= (__mon_yday
[__isleap (1900 + tm
->tm_year
)][tm
->tm_mon
]
233 + (tm
->tm_mday
- 1));
240 strptime_internal
__P ((const char *rp
, const char *fmt
, struct tm
*tm
,
241 enum locale_status
*decided
, int era_cnt
));
247 strptime_internal (rp
, fmt
, tm
, decided
, era_cnt
)
251 enum locale_status
*decided
;
254 const char *rp_backup
;
258 int century
, want_century
;
260 int have_wday
, want_xday
;
262 int have_mon
, have_mday
;
266 struct era_entry
*era
;
274 have_wday
= want_xday
= have_yday
= have_mon
= have_mday
= 0;
278 /* A white space in the format string matches 0 more or white
279 space in the input string. */
282 while (isspace (*rp
))
288 /* Any character but `%' must be matched by the same character
289 in the iput string. */
292 match_char (*fmt
++, *rp
++);
298 /* We need this for handling the `E' modifier. */
302 /* Make back up of current processing pointer. */
308 /* Match the `%' character itself. */
309 match_char ('%', *rp
++);
313 /* Match day of week. */
314 for (cnt
= 0; cnt
< 7; ++cnt
)
319 if (match_string (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
), rp
))
322 && strcmp (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
),
327 if (match_string (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
), rp
))
330 && strcmp (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
),
331 ab_weekday_name
[cnt
]))
338 && (match_string (weekday_name
[cnt
], rp
)
339 || match_string (ab_weekday_name
[cnt
], rp
)))
346 /* Does not match a weekday name. */
354 /* Match month name. */
355 for (cnt
= 0; cnt
< 12; ++cnt
)
360 if (match_string (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
), rp
))
363 && strcmp (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
),
368 if (match_string (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
), rp
))
371 && strcmp (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
),
378 if (match_string (month_name
[cnt
], rp
)
379 || match_string (ab_month_name
[cnt
], rp
))
386 /* Does not match a month name. */
392 /* Match locale's date and time format. */
396 if (!recursive (_NL_CURRENT (LC_TIME
, D_T_FMT
)))
405 if (*decided
== not &&
406 strcmp (_NL_CURRENT (LC_TIME
, D_T_FMT
), HERE_D_T_FMT
))
414 if (!recursive (HERE_D_T_FMT
))
419 /* Match century number. */
423 get_number (0, 99, 2);
429 /* Match day of month. */
430 get_number (1, 31, 2);
436 if (!recursive ("%Y-%m-%d"))
444 if (!recursive (_NL_CURRENT (LC_TIME
, D_FMT
)))
454 && strcmp (_NL_CURRENT (LC_TIME
, D_FMT
), HERE_D_FMT
))
464 /* Match standard day format. */
465 if (!recursive (HERE_D_FMT
))
471 /* Match hour in 24-hour clock. */
472 get_number (0, 23, 2);
477 /* Match hour in 12-hour clock. */
478 get_number (1, 12, 2);
479 tm
->tm_hour
= val
% 12;
483 /* Match day number of year. */
484 get_number (1, 366, 3);
485 tm
->tm_yday
= val
- 1;
489 /* Match number of month. */
490 get_number (1, 12, 2);
491 tm
->tm_mon
= val
- 1;
497 get_number (0, 59, 2);
502 /* Match any white space. */
503 while (isspace (*rp
))
507 /* Match locale's equivalent of AM/PM. */
511 if (match_string (_NL_CURRENT (LC_TIME
, AM_STR
), rp
))
513 if (strcmp (_NL_CURRENT (LC_TIME
, AM_STR
), HERE_AM_STR
))
517 if (match_string (_NL_CURRENT (LC_TIME
, PM_STR
), rp
))
519 if (strcmp (_NL_CURRENT (LC_TIME
, PM_STR
), HERE_PM_STR
))
527 if (!match_string (HERE_AM_STR
, rp
)) {
528 if (match_string (HERE_PM_STR
, rp
)) {
539 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
548 if (*decided
== not &&
549 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
557 if (!recursive (HERE_T_FMT_AMPM
))
561 if (!recursive ("%H:%M"))
566 /* The number of seconds may be very high so we cannot use
567 the `get_number' macro. Instead read the number
568 character for character and construct the result while
571 if (*rp
< '0' || *rp
> '9')
572 /* We need at least one digit. */
580 while (*rp
>= '0' && *rp
<= '9');
582 if (localtime_r (&secs
, tm
) == NULL
)
583 /* Error in function. */
588 get_number (0, 61, 2);
595 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
604 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
613 if (!recursive (HERE_T_FMT
))
617 get_number (1, 7, 1);
618 tm
->tm_wday
= val
% 7;
622 get_number (0, 99, 2);
623 /* XXX This cannot determine any field in TM. */
626 if (*rp
< '0' || *rp
> '9')
628 /* XXX Ignore the number since we would need some more
629 information to compute a real date. */
632 while (*rp
>= '0' && *rp
<= '9');
637 get_number (0, 53, 2);
638 /* XXX This cannot determine any field in TM without some
642 /* Match number of weekday. */
643 get_number (0, 6, 1);
649 match_year_in_century
:
651 /* Match year within century. */
652 get_number (0, 99, 2);
653 /* The "Year 2000: The Millennium Rollover" paper suggests that
654 values in the range 69-99 refer to the twentieth century. */
655 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
656 /* Indicate that we want to use the century, if specified. */
661 /* Match year including century number. */
662 get_number (0, 9999, 4);
663 tm
->tm_year
= val
- 1900;
668 /* XXX How to handle this? */
675 /* Match locale's alternate date and time format. */
678 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
681 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
683 if (!recursive (fmt
))
692 if (strcmp (fmt
, HERE_D_T_FMT
))
699 /* The C locale has no era information, so use the
700 normal representation. */
701 if (!recursive (HERE_D_T_FMT
))
710 era
= _nl_select_era_entry (era_cnt
);
711 if (match_string (era
->era_name
, rp
))
721 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
722 _NL_TIME_ERA_NUM_ENTRIES
);
723 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
724 ++era_cnt
, rp
= rp_backup
)
726 era
= _nl_select_era_entry (era_cnt
);
727 if (match_string (era
->era_name
, rp
))
733 if (era_cnt
== (int) num_eras
)
745 /* The C locale has no era information, so use the
746 normal representation. */
750 goto match_year_in_century
;
752 get_number(0, 9999, 4);
760 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
761 _NL_TIME_ERA_NUM_ENTRIES
);
762 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
763 ++era_cnt
, rp
= rp_backup
)
765 era
= _nl_select_era_entry (era_cnt
);
766 if (recursive (era
->era_format
))
769 if (era_cnt
== (int) num_eras
)
786 get_number (0, 9999, 4);
787 tm
->tm_year
= val
- 1900;
794 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
797 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
799 if (!recursive (fmt
))
808 if (strcmp (fmt
, HERE_D_FMT
))
814 if (!recursive (HERE_D_FMT
))
820 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
823 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
825 if (!recursive (fmt
))
834 if (strcmp (fmt
, HERE_T_FMT
))
840 if (!recursive (HERE_T_FMT
))
848 /* We have no information about the era format. Just use
849 the normal format. */
850 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
851 && *fmt
!= 'x' && *fmt
!= 'X')
852 /* This is an illegal format. */
862 /* Match day of month using alternate numeric symbols. */
863 get_alt_number (1, 31, 2);
869 /* Match hour in 24-hour clock using alternate numeric
871 get_alt_number (0, 23, 2);
876 /* Match hour in 12-hour clock using alternate numeric
878 get_alt_number (1, 12, 2);
879 tm
->tm_hour
= val
- 1;
883 /* Match month using alternate numeric symbols. */
884 get_alt_number (1, 12, 2);
885 tm
->tm_mon
= val
- 1;
890 /* Match minutes using alternate numeric symbols. */
891 get_alt_number (0, 59, 2);
895 /* Match seconds using alternate numeric symbols. */
896 get_alt_number (0, 61, 2);
902 get_alt_number (0, 53, 2);
903 /* XXX This cannot determine any field in TM without
904 further information. */
907 /* Match number of weekday using alternate numeric symbols. */
908 get_alt_number (0, 6, 1);
913 /* Match year within century using alternate numeric symbols. */
914 get_alt_number (0, 99, 2);
915 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
933 tm
->tm_year
= tm
->tm_year
% 100 + (century
- 19) * 100;
935 /* Only the century, but not the year. Strange, but so be it. */
936 tm
->tm_year
= (century
- 19) * 100;
942 era
= _nl_select_era_entry(era_cnt
);
944 tm
->tm_year
= (era
->start_date
[0]
945 + ((tm
->tm_year
- era
->offset
)
946 * era
->absolute_direction
));
948 /* Era start year assumed. */
949 tm
->tm_year
= era
->start_date
[0];
956 if (want_xday
&& !have_wday
)
958 if ( !(have_mon
&& have_mday
) && have_yday
)
960 /* We don't have tm_mon and/or tm_mday, compute them. */
962 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
] <= tm
->tm_yday
)
965 tm
->tm_mon
= t_mon
- 1;
969 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
971 day_of_the_week (tm
);
973 if (want_xday
&& !have_yday
)
974 day_of_the_year (tm
);
976 return discard_const_p(char, rp
);
980 char *rep_strptime(const char *buf
, const char *format
, struct tm
*tm
)
982 enum locale_status decided
;
989 return strptime_internal (buf
, format
, tm
, &decided
, -1);