strftime: fix underlying format string in %z format
[musl] / src / time / strftime.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <langinfo.h>
5 #include <locale.h>
6 #include <time.h>
7 #include <limits.h>
8 #include "locale_impl.h"
9 #include "libc.h"
10 #include "time_impl.h"
11
12 const char *__nl_langinfo_l(nl_item, locale_t);
13
14 static int is_leap(int y)
15 {
16         /* Avoid overflow */
17         if (y>INT_MAX-1900) y -= 2000;
18         y += 1900;
19         return !(y%4) && ((y%100) || !(y%400));
20 }
21
22 static int week_num(const struct tm *tm)
23 {
24         int val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%7) / 7;
25         /* If 1 Jan is just 1-3 days past Monday,
26          * the previous week is also in this year. */
27         if ((tm->tm_wday + 371U - tm->tm_yday - 2) % 7 <= 2)
28                 val++;
29         if (!val) {
30                 val = 52;
31                 /* If 31 December of prev year a Thursday,
32                  * or Friday of a leap year, then the
33                  * prev year has 53 weeks. */
34                 int dec31 = (tm->tm_wday + 7U - tm->tm_yday - 1) % 7;
35                 if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1)))
36                         val++;
37         } else if (val == 53) {
38                 /* If 1 January is not a Thursday, and not
39                  * a Wednesday of a leap year, then this
40                  * year has only 52 weeks. */
41                 int jan1 = (tm->tm_wday + 371U - tm->tm_yday) % 7;
42                 if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
43                         val = 1;
44         }
45         return val;
46 }
47
48 const char *__tm_to_tzname(const struct tm *);
49 size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct tm *restrict, locale_t);
50
51 const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc, int pad)
52 {
53         nl_item item;
54         long long val;
55         const char *fmt = "-";
56         int width = 2, def_pad = '0';
57
58         switch (f) {
59         case 'a':
60                 if (tm->tm_wday > 6U) goto string;
61                 item = ABDAY_1 + tm->tm_wday;
62                 goto nl_strcat;
63         case 'A':
64                 if (tm->tm_wday > 6U) goto string;
65                 item = DAY_1 + tm->tm_wday;
66                 goto nl_strcat;
67         case 'h':
68         case 'b':
69                 if (tm->tm_mon > 11U) goto string;
70                 item = ABMON_1 + tm->tm_mon;
71                 goto nl_strcat;
72         case 'B':
73                 if (tm->tm_mon > 11U) goto string;
74                 item = MON_1 + tm->tm_mon;
75                 goto nl_strcat;
76         case 'c':
77                 item = D_T_FMT;
78                 goto nl_strftime;
79         case 'C':
80                 val = (1900LL+tm->tm_year) / 100;
81                 goto number;
82         case 'e':
83                 def_pad = '_';
84         case 'd':
85                 val = tm->tm_mday;
86                 goto number;
87         case 'D':
88                 fmt = "%m/%d/%y";
89                 goto recu_strftime;
90         case 'F':
91                 fmt = "%Y-%m-%d";
92                 goto recu_strftime;
93         case 'g':
94         case 'G':
95                 val = tm->tm_year + 1900LL;
96                 if (tm->tm_yday < 3 && week_num(tm) != 1) val--;
97                 else if (tm->tm_yday > 360 && week_num(tm) == 1) val++;
98                 if (f=='g') val %= 100;
99                 else width = 4;
100                 goto number;
101         case 'H':
102                 val = tm->tm_hour;
103                 goto number;
104         case 'I':
105                 val = tm->tm_hour;
106                 if (!val) val = 12;
107                 else if (val > 12) val -= 12;
108                 goto number;
109         case 'j':
110                 val = tm->tm_yday+1;
111                 width = 3;
112                 goto number;
113         case 'm':
114                 val = tm->tm_mon+1;
115                 goto number;
116         case 'M':
117                 val = tm->tm_min;
118                 goto number;
119         case 'n':
120                 *l = 1;
121                 return "\n";
122         case 'p':
123                 item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
124                 goto nl_strcat;
125         case 'r':
126                 item = T_FMT_AMPM;
127                 goto nl_strftime;
128         case 'R':
129                 fmt = "%H:%M";
130                 goto recu_strftime;
131         case 's':
132                 val = __tm_to_secs(tm) - tm->__tm_gmtoff;
133                 width = 1;
134                 goto number;
135         case 'S':
136                 val = tm->tm_sec;
137                 goto number;
138         case 't':
139                 *l = 1;
140                 return "\t";
141         case 'T':
142                 fmt = "%H:%M:%S";
143                 goto recu_strftime;
144         case 'u':
145                 val = tm->tm_wday ? tm->tm_wday : 7;
146                 width = 1;
147                 goto number;
148         case 'U':
149                 val = (tm->tm_yday + 7U - tm->tm_wday) / 7;
150                 goto number;
151         case 'W':
152                 val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%7) / 7;
153                 goto number;
154         case 'V':
155                 val = week_num(tm);
156                 goto number;
157         case 'w':
158                 val = tm->tm_wday;
159                 width = 1;
160                 goto number;
161         case 'x':
162                 item = D_FMT;
163                 goto nl_strftime;
164         case 'X':
165                 item = T_FMT;
166                 goto nl_strftime;
167         case 'y':
168                 val = (tm->tm_year + 1900LL) % 100;
169                 if (val < 0) val = -val;
170                 goto number;
171         case 'Y':
172                 val = tm->tm_year + 1900LL;
173                 if (val >= 10000) {
174                         *l = snprintf(*s, sizeof *s, "+%lld", val);
175                         return *s;
176                 }
177                 width = 4;
178                 goto number;
179         case 'z':
180                 if (tm->tm_isdst < 0) {
181                         *l = 0;
182                         return "";
183                 }
184                 *l = snprintf(*s, sizeof *s, "%+.2ld%.2d",
185                         (tm->__tm_gmtoff)/3600,
186                         abs(tm->__tm_gmtoff%3600)/60);
187                 return *s;
188         case 'Z':
189                 if (tm->tm_isdst < 0) {
190                         *l = 0;
191                         return "";
192                 }
193                 fmt = __tm_to_tzname(tm);
194                 goto string;
195         case '%':
196                 *l = 1;
197                 return "%";
198         default:
199                 return 0;
200         }
201 number:
202         switch (pad ? pad : def_pad) {
203         case '-': *l = snprintf(*s, sizeof *s, "%lld", val); break;
204         case '_': *l = snprintf(*s, sizeof *s, "%*lld", width, val); break;
205         case '0':
206         default:  *l = snprintf(*s, sizeof *s, "%0*lld", width, val); break;
207         }
208         return *s;
209 nl_strcat:
210         fmt = __nl_langinfo_l(item, loc);
211 string:
212         *l = strlen(fmt);
213         return fmt;
214 nl_strftime:
215         fmt = __nl_langinfo_l(item, loc);
216 recu_strftime:
217         *l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
218         if (!*l) return 0;
219         return *s;
220 }
221
222 size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
223 {
224         size_t l, k;
225         char buf[100];
226         char *p;
227         const char *t;
228         int pad, plus;
229         unsigned long width;
230         for (l=0; l<n; f++) {
231                 if (!*f) {
232                         s[l] = 0;
233                         return l;
234                 }
235                 if (*f != '%') {
236                         s[l++] = *f;
237                         continue;
238                 }
239                 f++;
240                 pad = 0;
241                 if (*f == '-' || *f == '_' || *f == '0') pad = *f++;
242                 if ((plus = (*f == '+'))) f++;
243                 width = strtoul(f, &p, 10);
244                 if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
245                         if (!width && p!=f) width = 1;
246                 } else {
247                         width = 0;
248                 }
249                 f = p;
250                 if (*f == 'E' || *f == 'O') f++;
251                 t = __strftime_fmt_1(&buf, &k, *f, tm, loc, pad);
252                 if (!t) break;
253                 if (width) {
254                         /* Trim off any sign and leading zeros, then
255                          * count remaining digits to determine behavior
256                          * for the + flag. */
257                         if (*t=='+' || *t=='-') t++, k--;
258                         for (; *t=='0' && t[1]-'0'<10U; t++, k--);
259                         if (width < k) width = k;
260                         size_t d;
261                         for (d=0; t[d]-'0'<10U; d++);
262                         if (tm->tm_year < -1900) {
263                                 s[l++] = '-';
264                                 width--;
265                         } else if (plus && d+(width-k) >= (*p=='C'?3:5)) {
266                                 s[l++] = '+';
267                                 width--;
268                         }
269                         for (; width > k && l < n; width--)
270                                 s[l++] = '0';
271                 }
272                 if (k > n-l) k = n-l;
273                 memcpy(s+l, t, k);
274                 l += k;
275         }
276         if (n) {
277                 if (l==n) l=n-1;
278                 s[l] = 0;
279         }
280         return 0;
281 }
282
283 size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
284 {
285         return __strftime_l(s, n, f, tm, CURRENT_LOCALE);
286 }
287
288 weak_alias(__strftime_l, strftime_l);