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