fix strftime %s not to zero pad with default width=2
[musl] / src / time / wcsftime.c
1 #include <wchar.h>
2 #include <time.h>
3 #include <locale.h>
4 #include "libc.h"
5
6 const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc);
7
8 size_t __wcsftime_l(wchar_t *restrict s, size_t n, const wchar_t *restrict f, const struct tm *restrict tm, locale_t loc)
9 {
10         size_t l, k;
11         char buf[100];
12         wchar_t wbuf[100];
13         wchar_t *p;
14         const char *t_mb;
15         const wchar_t *t;
16         int plus;
17         unsigned long width;
18         for (l=0; l<n; f++) {
19                 if (!*f) {
20                         s[l] = 0;
21                         return l;
22                 }
23                 if (*f != '%') {
24                         s[l++] = *f;
25                         continue;
26                 }
27                 f++;
28                 if ((plus = (*f == '+'))) f++;
29                 width = wcstoul(f, &p, 10);
30                 if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
31                         if (!width && p!=f) width = 1;
32                 } else {
33                         width = 0;
34                 }
35                 f = p;
36                 if (*f == 'E' || *f == 'O') f++;
37                 t_mb = __strftime_fmt_1(&buf, &k, *f, tm, loc);
38                 if (!t_mb) break;
39                 k = mbstowcs(wbuf, t_mb, sizeof wbuf / sizeof *wbuf);
40                 if (k == (size_t)-1) return 0;
41                 t = wbuf;
42                 if (width) {
43                         for (; *t=='+' || *t=='-' || (*t=='0'&&t[1]); t++, k--);
44                         width--;
45                         if (plus && tm->tm_year >= 10000-1900)
46                                 s[l++] = '+';
47                         else if (tm->tm_year < -1900)
48                                 s[l++] = '-';
49                         else
50                                 width++;
51                         for (; width > k && l < n; width--)
52                                 s[l++] = '0';
53                 }
54                 if (k >= n-l) k = n-l;
55                 wmemcpy(s+l, t, k);
56                 l += k;
57         }
58         if (n) {
59                 if (l==n) l=n-1;
60                 s[l] = 0;
61         }
62         return 0;
63 }
64
65 size_t wcsftime(wchar_t *restrict wcs, size_t n, const wchar_t *restrict f, const struct tm *restrict tm)
66 {
67         return __wcsftime_l(wcs, n, f, tm, 0);
68 }
69
70 weak_alias(__wcsftime_l, wcsftime_l);