select: overhaul for time64
[musl.git] / src / time / wcsftime.c
blob8e1437b33f9ce808597adcdf8db033514cb7fea8
1 #include <wchar.h>
2 #include <time.h>
3 #include <locale.h>
4 #include "locale_impl.h"
5 #include "time_impl.h"
7 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 size_t l, k;
10 char buf[100];
11 wchar_t wbuf[100];
12 wchar_t *p;
13 const char *t_mb;
14 const wchar_t *t;
15 int pad, plus;
16 unsigned long width;
17 for (l=0; l<n; f++) {
18 if (!*f) {
19 s[l] = 0;
20 return l;
22 if (*f != '%') {
23 s[l++] = *f;
24 continue;
26 f++;
27 pad = 0;
28 if (*f == '-' || *f == '_' || *f == '0') pad = *f++;
29 if ((plus = (*f == '+'))) f++;
30 width = wcstoul(f, &p, 10);
31 if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
32 if (!width && p!=f) width = 1;
33 } else {
34 width = 0;
36 f = p;
37 if (*f == 'E' || *f == 'O') f++;
38 t_mb = __strftime_fmt_1(&buf, &k, *f, tm, loc, pad);
39 if (!t_mb) break;
40 k = mbstowcs(wbuf, t_mb, sizeof wbuf / sizeof *wbuf);
41 if (k == (size_t)-1) return 0;
42 t = wbuf;
43 if (width) {
44 for (; *t=='+' || *t=='-' || (*t=='0'&&t[1]); t++, k--);
45 width--;
46 if (plus && tm->tm_year >= 10000-1900)
47 s[l++] = '+';
48 else if (tm->tm_year < -1900)
49 s[l++] = '-';
50 else
51 width++;
52 for (; width > k && l < n; width--)
53 s[l++] = '0';
55 if (k >= n-l) k = n-l;
56 wmemcpy(s+l, t, k);
57 l += k;
59 if (n) {
60 if (l==n) l=n-1;
61 s[l] = 0;
63 return 0;
66 size_t wcsftime(wchar_t *restrict wcs, size_t n, const wchar_t *restrict f, const struct tm *restrict tm)
68 return __wcsftime_l(wcs, n, f, tm, CURRENT_LOCALE);
71 weak_alias(__wcsftime_l, wcsftime_l);