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 Library 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 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. */
27 #include "system/locale.h"
28 #include "system/time.h"
31 # if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
32 # define __P(args) args
38 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
40 # define localtime_r __localtime_r
42 /* Approximate localtime_r as best we can in its absence. */
43 # define localtime_r my_localtime_r
44 static struct tm
*localtime_r
__P ((const time_t *, struct tm
*));
50 struct tm
*l
= localtime (t
);
57 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
60 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
61 #if defined __GNUC__ && __GNUC__ >= 2
62 # define match_string(cs1, s2) \
63 ({ size_t len = strlen (cs1); \
64 int result = strncasecmp ((cs1), (s2), len) == 0; \
65 if (result) (s2) += len; \
68 /* Oh come on. Get a reasonable compiler. */
69 # define match_string(cs1, s2) \
70 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
72 /* We intentionally do not use isdigit() for testing because this will
73 lead to problems with the wide character version. */
74 #define get_number(from, to, n) \
80 if (*rp < '0' || *rp > '9') \
85 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
86 if (val < from || val > to) \
90 # define get_alt_number(from, to, n) \
92 __label__ do_normal; \
93 if (*decided != raw) \
95 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
103 while (*alts != '\0') \
105 size_t len = strlen (alts); \
106 if (strncasecmp (alts, rp, len) == 0) \
113 if (*decided == not && ! any) \
115 /* If we haven't read anything it's an error. */ \
118 /* Correct the premature multiplication. */ \
124 } while (--__n > 0 && val * 10 <= to); \
125 if (val < from || val > to) \
131 get_number (from, to, n); \
136 # define get_alt_number(from, to, n) \
137 /* We don't have the alternate representation. */ \
138 get_number(from, to, n)
140 #define recursive(new_fmt) \
141 (*(new_fmt) != '\0' \
142 && (rp = strptime_internal (rp, (new_fmt), tm, decided, era_cnt)) != NULL)
146 /* This is defined in locale/C-time.c in the GNU libc. */
147 extern const struct locale_data _nl_C_LC_TIME
;
148 extern const unsigned short int __mon_yday
[2][13];
150 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
151 # define ab_weekday_name \
152 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
153 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
154 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
155 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
156 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
157 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
158 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
159 # define HERE_T_FMT_AMPM \
160 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
161 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
163 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
165 static char const weekday_name
[][10] =
167 "Sunday", "Monday", "Tuesday", "Wednesday",
168 "Thursday", "Friday", "Saturday"
170 static char const ab_weekday_name
[][4] =
172 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
174 static char const month_name
[][10] =
176 "January", "February", "March", "April", "May", "June",
177 "July", "August", "September", "October", "November", "December"
179 static char const ab_month_name
[][4] =
181 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
182 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
184 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
185 # define HERE_D_FMT "%m/%d/%y"
186 # define HERE_AM_STR "AM"
187 # define HERE_PM_STR "PM"
188 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
189 # define HERE_T_FMT "%H:%M:%S"
191 static const unsigned short int __mon_yday
[2][13] =
194 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
196 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
200 /* Status of lookup: do we use the locale data or the raw data? */
201 enum locale_status
{ not, loc
, raw
};
205 /* Nonzero if YEAR is a leap year (every 4 years,
206 except every 100th isn't, and every 400th is). */
207 # define __isleap(year) \
208 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
211 /* Compute the day of the week. */
213 day_of_the_week (struct tm
*tm
)
215 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
216 the difference between this data in the one on TM and so determine
218 int corr_year
= 1900 + tm
->tm_year
- (tm
->tm_mon
< 2);
220 + (365 * (tm
->tm_year
- 70))
222 - ((corr_year
/ 4) / 25) + ((corr_year
/ 4) % 25 < 0)
223 + (((corr_year
/ 4) / 25) / 4)
224 + __mon_yday
[0][tm
->tm_mon
]
226 tm
->tm_wday
= ((wday
% 7) + 7) % 7;
229 /* Compute the day of the year. */
231 day_of_the_year (struct tm
*tm
)
233 tm
->tm_yday
= (__mon_yday
[__isleap (1900 + tm
->tm_year
)][tm
->tm_mon
]
234 + (tm
->tm_mday
- 1));
241 strptime_internal
__P ((const char *rp
, const char *fmt
, struct tm
*tm
,
242 enum locale_status
*decided
, int era_cnt
));
248 strptime_internal (rp
, fmt
, tm
, decided
, era_cnt
)
252 enum locale_status
*decided
;
255 const char *rp_backup
;
259 int century
, want_century
;
261 int have_wday
, want_xday
;
263 int have_mon
, have_mday
;
267 struct era_entry
*era
;
275 have_wday
= want_xday
= have_yday
= have_mon
= have_mday
= 0;
279 /* A white space in the format string matches 0 more or white
280 space in the input string. */
283 while (isspace (*rp
))
289 /* Any character but `%' must be matched by the same character
290 in the iput string. */
293 match_char (*fmt
++, *rp
++);
299 /* We need this for handling the `E' modifier. */
303 /* Make back up of current processing pointer. */
309 /* Match the `%' character itself. */
310 match_char ('%', *rp
++);
314 /* Match day of week. */
315 for (cnt
= 0; cnt
< 7; ++cnt
)
320 if (match_string (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
), rp
))
323 && strcmp (_NL_CURRENT (LC_TIME
, DAY_1
+ cnt
),
328 if (match_string (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
), rp
))
331 && strcmp (_NL_CURRENT (LC_TIME
, ABDAY_1
+ cnt
),
332 ab_weekday_name
[cnt
]))
339 && (match_string (weekday_name
[cnt
], rp
)
340 || match_string (ab_weekday_name
[cnt
], rp
)))
347 /* Does not match a weekday name. */
355 /* Match month name. */
356 for (cnt
= 0; cnt
< 12; ++cnt
)
361 if (match_string (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
), rp
))
364 && strcmp (_NL_CURRENT (LC_TIME
, MON_1
+ cnt
),
369 if (match_string (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
), rp
))
372 && strcmp (_NL_CURRENT (LC_TIME
, ABMON_1
+ cnt
),
379 if (match_string (month_name
[cnt
], rp
)
380 || match_string (ab_month_name
[cnt
], rp
))
387 /* Does not match a month name. */
393 /* Match locale's date and time format. */
397 if (!recursive (_NL_CURRENT (LC_TIME
, D_T_FMT
)))
406 if (*decided
== not &&
407 strcmp (_NL_CURRENT (LC_TIME
, D_T_FMT
), HERE_D_T_FMT
))
415 if (!recursive (HERE_D_T_FMT
))
420 /* Match century number. */
424 get_number (0, 99, 2);
430 /* Match day of month. */
431 get_number (1, 31, 2);
437 if (!recursive ("%Y-%m-%d"))
445 if (!recursive (_NL_CURRENT (LC_TIME
, D_FMT
)))
455 && strcmp (_NL_CURRENT (LC_TIME
, D_FMT
), HERE_D_FMT
))
465 /* Match standard day format. */
466 if (!recursive (HERE_D_FMT
))
472 /* Match hour in 24-hour clock. */
473 get_number (0, 23, 2);
478 /* Match hour in 12-hour clock. */
479 get_number (1, 12, 2);
480 tm
->tm_hour
= val
% 12;
484 /* Match day number of year. */
485 get_number (1, 366, 3);
486 tm
->tm_yday
= val
- 1;
490 /* Match number of month. */
491 get_number (1, 12, 2);
492 tm
->tm_mon
= val
- 1;
498 get_number (0, 59, 2);
503 /* Match any white space. */
504 while (isspace (*rp
))
508 /* Match locale's equivalent of AM/PM. */
512 if (match_string (_NL_CURRENT (LC_TIME
, AM_STR
), rp
))
514 if (strcmp (_NL_CURRENT (LC_TIME
, AM_STR
), HERE_AM_STR
))
518 if (match_string (_NL_CURRENT (LC_TIME
, PM_STR
), rp
))
520 if (strcmp (_NL_CURRENT (LC_TIME
, PM_STR
), HERE_PM_STR
))
528 if (!match_string (HERE_AM_STR
, rp
)) {
529 if (match_string (HERE_PM_STR
, rp
)) {
540 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
)))
549 if (*decided
== not &&
550 strcmp (_NL_CURRENT (LC_TIME
, T_FMT_AMPM
),
558 if (!recursive (HERE_T_FMT_AMPM
))
562 if (!recursive ("%H:%M"))
567 /* The number of seconds may be very high so we cannot use
568 the `get_number' macro. Instead read the number
569 character for character and construct the result while
572 if (*rp
< '0' || *rp
> '9')
573 /* We need at least one digit. */
581 while (*rp
>= '0' && *rp
<= '9');
583 if (localtime_r (&secs
, tm
) == NULL
)
584 /* Error in function. */
589 get_number (0, 61, 2);
596 if (!recursive (_NL_CURRENT (LC_TIME
, T_FMT
)))
605 if (strcmp (_NL_CURRENT (LC_TIME
, T_FMT
), HERE_T_FMT
))
614 if (!recursive (HERE_T_FMT
))
618 get_number (1, 7, 1);
619 tm
->tm_wday
= val
% 7;
623 get_number (0, 99, 2);
624 /* XXX This cannot determine any field in TM. */
627 if (*rp
< '0' || *rp
> '9')
629 /* XXX Ignore the number since we would need some more
630 information to compute a real date. */
633 while (*rp
>= '0' && *rp
<= '9');
638 get_number (0, 53, 2);
639 /* XXX This cannot determine any field in TM without some
643 /* Match number of weekday. */
644 get_number (0, 6, 1);
650 match_year_in_century
:
652 /* Match year within century. */
653 get_number (0, 99, 2);
654 /* The "Year 2000: The Millennium Rollover" paper suggests that
655 values in the range 69-99 refer to the twentieth century. */
656 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
657 /* Indicate that we want to use the century, if specified. */
662 /* Match year including century number. */
663 get_number (0, 9999, 4);
664 tm
->tm_year
= val
- 1900;
669 /* XXX How to handle this? */
676 /* Match locale's alternate date and time format. */
679 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_T_FMT
);
682 fmt
= _NL_CURRENT (LC_TIME
, D_T_FMT
);
684 if (!recursive (fmt
))
693 if (strcmp (fmt
, HERE_D_T_FMT
))
700 /* The C locale has no era information, so use the
701 normal representation. */
702 if (!recursive (HERE_D_T_FMT
))
711 era
= _nl_select_era_entry (era_cnt
);
712 if (match_string (era
->era_name
, rp
))
722 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
723 _NL_TIME_ERA_NUM_ENTRIES
);
724 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
725 ++era_cnt
, rp
= rp_backup
)
727 era
= _nl_select_era_entry (era_cnt
);
728 if (match_string (era
->era_name
, rp
))
734 if (era_cnt
== (int) num_eras
)
746 /* The C locale has no era information, so use the
747 normal representation. */
751 goto match_year_in_century
;
753 get_number(0, 9999, 4);
761 num_eras
= _NL_CURRENT_WORD (LC_TIME
,
762 _NL_TIME_ERA_NUM_ENTRIES
);
763 for (era_cnt
= 0; era_cnt
< (int) num_eras
;
764 ++era_cnt
, rp
= rp_backup
)
766 era
= _nl_select_era_entry (era_cnt
);
767 if (recursive (era
->era_format
))
770 if (era_cnt
== (int) num_eras
)
787 get_number (0, 9999, 4);
788 tm
->tm_year
= val
- 1900;
795 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_D_FMT
);
798 fmt
= _NL_CURRENT (LC_TIME
, D_FMT
);
800 if (!recursive (fmt
))
809 if (strcmp (fmt
, HERE_D_FMT
))
815 if (!recursive (HERE_D_FMT
))
821 const char *fmt
= _NL_CURRENT (LC_TIME
, ERA_T_FMT
);
824 fmt
= _NL_CURRENT (LC_TIME
, T_FMT
);
826 if (!recursive (fmt
))
835 if (strcmp (fmt
, HERE_T_FMT
))
841 if (!recursive (HERE_T_FMT
))
849 /* We have no information about the era format. Just use
850 the normal format. */
851 if (*fmt
!= 'c' && *fmt
!= 'C' && *fmt
!= 'y' && *fmt
!= 'Y'
852 && *fmt
!= 'x' && *fmt
!= 'X')
853 /* This is an illegal format. */
863 /* Match day of month using alternate numeric symbols. */
864 get_alt_number (1, 31, 2);
870 /* Match hour in 24-hour clock using alternate numeric
872 get_alt_number (0, 23, 2);
877 /* Match hour in 12-hour clock using alternate numeric
879 get_alt_number (1, 12, 2);
880 tm
->tm_hour
= val
- 1;
884 /* Match month using alternate numeric symbols. */
885 get_alt_number (1, 12, 2);
886 tm
->tm_mon
= val
- 1;
891 /* Match minutes using alternate numeric symbols. */
892 get_alt_number (0, 59, 2);
896 /* Match seconds using alternate numeric symbols. */
897 get_alt_number (0, 61, 2);
903 get_alt_number (0, 53, 2);
904 /* XXX This cannot determine any field in TM without
905 further information. */
908 /* Match number of weekday using alternate numeric symbols. */
909 get_alt_number (0, 6, 1);
914 /* Match year within century using alternate numeric symbols. */
915 get_alt_number (0, 99, 2);
916 tm
->tm_year
= val
>= 69 ? val
: val
+ 100;
934 tm
->tm_year
= tm
->tm_year
% 100 + (century
- 19) * 100;
936 /* Only the century, but not the year. Strange, but so be it. */
937 tm
->tm_year
= (century
- 19) * 100;
943 era
= _nl_select_era_entry(era_cnt
);
945 tm
->tm_year
= (era
->start_date
[0]
946 + ((tm
->tm_year
- era
->offset
)
947 * era
->absolute_direction
));
949 /* Era start year assumed. */
950 tm
->tm_year
= era
->start_date
[0];
957 if (want_xday
&& !have_wday
)
959 if ( !(have_mon
&& have_mday
) && have_yday
)
961 /* We don't have tm_mon and/or tm_mday, compute them. */
963 while (__mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
] <= tm
->tm_yday
)
966 tm
->tm_mon
= t_mon
- 1;
970 - __mon_yday
[__isleap(1900 + tm
->tm_year
)][t_mon
- 1] + 1);
972 day_of_the_week (tm
);
974 if (want_xday
&& !have_yday
)
975 day_of_the_year (tm
);
977 return discard_const_p(char, rp
);
981 char *rep_strptime(const char *buf
, const char *format
, struct tm
*tm
)
983 enum locale_status decided
;
990 return strptime_internal (buf
, format
, tm
, &decided
, -1);