fix off-by-one in strptime %j
[musl] / src / time / strptime.c
1 #include <stdlib.h>
2 #include <langinfo.h>
3 #include <time.h>
4 #include <ctype.h>
5 #include <stddef.h>
6 #include <string.h>
7 #include <strings.h>
8
9 char *strptime(const char *restrict s, const char *restrict f, struct tm *restrict tm)
10 {
11         int i, w, neg, adj, min, range, *dest, dummy;
12         const char *ex;
13         size_t len;
14         int want_century = 0, century = 0;
15         while (*f) {
16                 if (*f != '%') {
17                         if (isspace(*f)) for (; *s && isspace(*s); s++);
18                         else if (*s != *f) return 0;
19                         else s++;
20                         f++;
21                         continue;
22                 }
23                 f++;
24                 if (*f == '+') f++;
25                 if (isdigit(*f)) {
26                         char *new_f;
27                         w=strtoul(f, &new_f, 10);
28                         f = new_f;
29                 } else {
30                         w=-1;
31                 }
32                 adj=0;
33                 switch (*f++) {
34                 case 'a': case 'A':
35                         dest = &tm->tm_wday;
36                         min = ABDAY_1;
37                         range = 7;
38                         goto symbolic_range;
39                 case 'b': case 'B': case 'h':
40                         dest = &tm->tm_mon;
41                         min = ABMON_1;
42                         range = 12;
43                         goto symbolic_range;
44                 case 'c':
45                         s = strptime(s, nl_langinfo(D_T_FMT), tm);
46                         if (!s) return 0;
47                         break;
48                 case 'C':
49                         dest = &century;
50                         if (w<0) w=2;
51                         want_century |= 2;
52                         goto numeric_digits;
53                 case 'd': case 'e':
54                         dest = &tm->tm_mday;
55                         min = 1;
56                         range = 31;
57                         goto numeric_range;
58                 case 'D':
59                         s = strptime(s, "%m/%d/%y", tm);
60                         if (!s) return 0;
61                         break;
62                 case 'H':
63                         dest = &tm->tm_hour;
64                         min = 0;
65                         range = 24;
66                         goto numeric_range;
67                 case 'I':
68                         dest = &tm->tm_hour;
69                         min = 1;
70                         range = 12;
71                         goto numeric_range;
72                 case 'j':
73                         dest = &tm->tm_yday;
74                         min = 1;
75                         range = 366;
76                         adj = 1;
77                         goto numeric_range;
78                 case 'm':
79                         dest = &tm->tm_mon;
80                         min = 1;
81                         range = 12;
82                         adj = 1;
83                         goto numeric_range;
84                 case 'M':
85                         dest = &tm->tm_min;
86                         min = 0;
87                         range = 60;
88                         goto numeric_range;
89                 case 'n': case 't':
90                         for (; *s && isspace(*s); s++);
91                         break;
92                 case 'p':
93                         ex = nl_langinfo(AM_STR);
94                         len = strlen(ex);
95                         if (!strncasecmp(s, ex, len)) {
96                                 tm->tm_hour %= 12;
97                                 break;
98                         }
99                         ex = nl_langinfo(PM_STR);
100                         len = strlen(ex);
101                         if (!strncasecmp(s, ex, len)) {
102                                 tm->tm_hour %= 12;
103                                 tm->tm_hour += 12;
104                                 break;
105                         }
106                         return 0;
107                 case 'r':
108                         s = strptime(s, nl_langinfo(T_FMT_AMPM), tm);
109                         if (!s) return 0;
110                         break;
111                 case 'R':
112                         s = strptime(s, "%H:%M", tm);
113                         if (!s) return 0;
114                         break;
115                 case 'S':
116                         dest = &tm->tm_sec;
117                         min = 0;
118                         range = 61;
119                         goto numeric_range;
120                 case 'T':
121                         s = strptime(s, "%H:%M:%S", tm);
122                         if (!s) return 0;
123                         break;
124                 case 'U':
125                 case 'W':
126                         /* Throw away result, for now. (FIXME?) */
127                         dest = &dummy;
128                         min = 0;
129                         range = 54;
130                         goto numeric_range;
131                 case 'w':
132                         dest = &tm->tm_wday;
133                         min = 0;
134                         range = 7;
135                         goto numeric_range;
136                 case 'x':
137                         s = strptime(s, nl_langinfo(D_FMT), tm);
138                         if (!s) return 0;
139                         break;
140                 case 'X':
141                         s = strptime(s, nl_langinfo(T_FMT), tm);
142                         if (!s) return 0;
143                         break;
144                 case 'y':
145                         dest = &tm->tm_year;
146                         w = 2;
147                         want_century |= 1;
148                         goto numeric_digits;
149                 case 'Y':
150                         dest = &tm->tm_year;
151                         if (w<0) w=4;
152                         adj = 1900;
153                         want_century = 0;
154                         goto numeric_digits;
155                 case '%':
156                         if (*s++ != '%') return 0;
157                         break;
158                 default:
159                         return 0;
160                 numeric_range:
161                         if (!isdigit(*s)) return 0;
162                         *dest = 0;
163                         for (i=1; i<=min+range && isdigit(*s); i*=10)
164                                 *dest = *dest * 10 + *s++ - '0';
165                         if (*dest - min >= (unsigned)range) return 0;
166                         *dest -= adj;
167                         switch((char *)dest - (char *)tm) {
168                         case offsetof(struct tm, tm_yday):
169                                 ;
170                         }
171                         goto update;
172                 numeric_digits:
173                         neg = 0;
174                         if (*s == '+') s++;
175                         else if (*s == '-') neg=1, s++;
176                         if (!isdigit(*s)) return 0;
177                         for (*dest=i=0; i<w && isdigit(*s); i++)
178                                 *dest = *dest * 10 + *s++ - '0';
179                         if (neg) *dest = -*dest;
180                         *dest -= adj;
181                         goto update;
182                 symbolic_range:
183                         for (i=2*range-1; i>=0; i--) {
184                                 ex = nl_langinfo(min+i);
185                                 len = strlen(ex);
186                                 if (strncasecmp(s, ex, len)) continue;
187                                 s += len;
188                                 *dest = i % range;
189                                 break;
190                         }
191                         if (i<0) return 0;
192                         goto update;
193                 update:
194                         //FIXME
195                         ;
196                 }
197         }
198         if (want_century) {
199                 if (want_century & 2) tm->tm_year += century * 100 - 1900;
200                 else if (tm->tm_year <= 68) tm->tm_year += 100;
201         }
202         return (char *)s;
203 }