initial check-in, version 0.5.0
[oweals/musl.git] / src / time / strptime.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <langinfo.h>
4 #include <time.h>
5
6 const char *__langinfo(nl_item);
7
8 char *strptime(const char *s, const char *f, struct tm *tm)
9 {
10         return NULL;
11 }
12
13 #if 0
14
15 char *strptime(const char *s, const char *f, struct tm *tm)
16 {
17         nl_item item;
18         int *dest;
19         const char *fmt;
20         for (; *f; f++) {
21                 if (isspace(*f)) goto whitespace;
22                 if (*f == '%') {
23 do_fmt:
24                 switch (*++f) {
25                 case '%':
26                         goto literal;
27                 case 'E':
28                 case 'O':
29                         goto do_fmt;
30                 case 'a':
31                         item = ABDAY_1 + tm->tm_wday;
32                         goto nl_strcat;
33                 case 'A':
34                         item = DAY_1 + tm->tm_wday;
35                         goto nl_strcat;
36                 case 'h':
37                 case 'b':
38                         item = ABMON_1 + tm->tm_mon;
39                         goto nl_strcat;
40                 case 'B':
41                         item = MON_1 + tm->tm_mon;
42                         goto nl_strcat;
43                 case 'c':
44                         item = D_T_FMT;
45                         goto nl_strftime;
46                 case 'C':
47                         val = (1900+tm->tm_year) / 100;
48                         fmt = "%02d";
49                         goto number;
50                 case 'd':
51                         val = tm->tm_mday;
52                         fmt = "%02d";
53                         goto number;
54                 case 'D':
55                         fmt = "%m/%d/%y";
56                         goto recu_strftime;
57                 case 'e':
58                         val = tm->tm_mday;
59                         fmt = "%2d";
60                         goto number;
61                 case 'F':
62                         fmt = "%Y-%m-%d";
63                         goto recu_strftime;
64                 case 'g':
65                         // FIXME
66                         val = 0; //week_based_year(tm)%100;
67                         fmt = "%02d";
68                         goto number;
69                 case 'G':
70                         // FIXME
71                         val = 0; //week_based_year(tm);
72                         fmt = "%04d";
73                         goto number;
74                 case 'H':
75                         val = tm->tm_hour;
76                         fmt = "%02d";
77                         goto number;
78                 case 'I':
79                         val = tm->tm_hour;
80                         if (!val) val = 12;
81                         else if (val > 12) val -= 12;
82                         fmt = "%02d";
83                         goto number;
84                 case 'j':
85                         val = tm->tm_yday+1;
86                         fmt = "%03d";
87                         goto number;
88                 case 'm':
89                         val = tm->tm_mon+1;
90                         fmt = "%02d";
91                         goto number;
92                 case 'M':
93                         val = tm->tm_min;
94                         fmt = "%02d";
95                         goto number;
96                 case 'n':
97                 case 't':
98                         goto whitespace;
99                 case 'p':
100                         item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
101                         goto nl_strcat;
102                 case 'r':
103                         item = T_FMT_AMPM;
104                         goto nl_strftime;
105                 case 'R':
106                         fmt = "%H:%M";
107                         goto recu_strftime;
108                 case 'S':
109                         val = tm->tm_sec;
110                         fmt = "%02d";
111                         goto number;
112                 case 'T':
113                         fmt = "%H:%M:%S";
114                         goto recu_strftime;
115                 case 'u':
116                         val = tm->tm_wday ? tm->tm_wday : 7;
117                         fmt = "%d";
118                         goto number;
119                 case 'U':
120                 case 'V':
121                 case 'W':
122                         // FIXME: week number mess..
123                         continue;
124                 case 'w':
125                         val = tm->tm_wday;
126                         fmt = "%d";
127                         goto number;
128                 case 'x':
129                         item = D_FMT;
130                         goto nl_strftime;
131                 case 'X':
132                         item = T_FMT;
133                         goto nl_strftime;
134                 case 'y':
135                         val = tm->tm_year % 100;
136                         fmt = "%02d";
137                         goto number;
138                 case 'Y':
139                         val = tm->tm_year + 1900;
140                         fmt = "%04d";
141                         goto number;
142                 case 'z':
143                         if (tm->tm_isdst < 0) continue;
144                         val = timezone + (tm->tm_isdst) ? __dst_offset : 0;
145                         l += snprintf(s+l, n-l, "%+02d%02d", val/60, abs(val%60));
146                         continue;
147                 case 'Z':
148                         if (tm->tm_isdst < 0 || !tzname[0] || !tzname[0][0])
149                                 continue;
150                         l += snprintf(s+l, n-l, "%s", tzname[!!tm->tm_isdst]);
151                         continue;
152                 }
153                 default:
154                         return NULL;
155                 }
156 literal:
157                 if (*s++ != *f) return NULL;
158                 continue;
159 whitespace:
160                 while(isspace(*s)) s++;
161                 continue;
162 number:
163                 l += snprintf(s+l, n-l, fmt, val);
164                 continue;
165 nl_strcat:
166                 l += snprintf(s+l, n-l, "%s", __langinfo(item));
167                 continue;
168 nl_strftime:
169                 fmt = __langinfo(item);
170 recu_strftime:
171                 l += strftime(s+l, n-l, fmt, tm);
172         }
173         if (l >= n) return 0;
174         s[l] = 0;
175         return l;
176 }
177
178 #endif