rework langinfo code for ABI compat and for use by time code
[musl] / src / time / wcsftime.c
1 #include <wchar.h>
2 #include <time.h>
3 #include <string.h>
4 #include <locale.h>
5
6 size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct tm *restrict, locale_t);
7
8 size_t __wcsftime_l(wchar_t *restrict wcs, size_t n, const wchar_t *restrict f, const struct tm *restrict tm, locale_t loc)
9 {
10         size_t k, n0=n;
11         char out[100], in[4];
12         while (*f) {
13                 if (!n) return 0;
14                 if (*f != '%') {
15                         *wcs++ = *f++;
16                         n--;
17                         continue;
18                 }
19                 in[2] = in[3] = 0;
20                 in[0] = *f++;
21                 if (strchr("EO", (in[1]=*f++)))
22                         in[2] = *f++;
23                 k = __strftime_l(out, sizeof out, in, tm, loc);
24                 if (!k) return 0;
25                 k = mbsrtowcs(wcs, (const char *[]){out}, n, 0);
26                 if (k==(size_t)-1) return 0;
27                 wcs += k;
28                 n -= k;
29         }
30         if (!n) return 0;
31         *wcs++ = 0;
32         return n0-n;
33 }
34
35 size_t wcsftime(wchar_t *restrict wcs, size_t n, const wchar_t *restrict f, const struct tm *restrict tm)
36 {
37         return __wcsftime_l(wcs, n, f, tm, LC_GLOBAL_LOCALE);
38 }