wcsftime.c 909 字节
Newer Older
R
Rich Felker 已提交
1 2 3
#include <wchar.h>
#include <time.h>
#include <string.h>
R
Rich Felker 已提交
4
#include <locale.h>
R
Rich Felker 已提交
5

R
Rich Felker 已提交
6 7 8
size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct tm *restrict, locale_t);

size_t __wcsftime_l(wchar_t *restrict wcs, size_t n, const wchar_t *restrict f, const struct tm *restrict tm, locale_t loc)
R
Rich Felker 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22
{
	size_t k, n0=n;
	char out[100], in[4];
	while (*f) {
		if (!n) return 0;
		if (*f != '%') {
			*wcs++ = *f++;
			n--;
			continue;
		}
		in[2] = in[3] = 0;
		in[0] = *f++;
		if (strchr("EO", (in[1]=*f++)))
			in[2] = *f++;
R
Rich Felker 已提交
23
		k = __strftime_l(out, sizeof out, in, tm, loc);
R
Rich Felker 已提交
24 25 26 27 28 29 30 31 32 33 34
		if (!k) return 0;
		k = mbsrtowcs(wcs, (const char *[]){out}, n, 0);
		if (k==(size_t)-1) return 0;
		wcs += k;
		n -= k;
	}
	if (!n) return 0;
	*wcs++ = 0;
	return n0-n;
}

R
Rich Felker 已提交
35 36
size_t wcsftime(wchar_t *restrict wcs, size_t n, const wchar_t *restrict f, const struct tm *restrict tm)
{
37
	return __wcsftime_l(wcs, n, f, tm, 0);
R
Rich Felker 已提交
38
}