1570af302Sopenharmony_ci#include <wchar.h> 2570af302Sopenharmony_ci#include <time.h> 3570af302Sopenharmony_ci#include <locale.h> 4570af302Sopenharmony_ci#include "locale_impl.h" 5570af302Sopenharmony_ci#include "time_impl.h" 6570af302Sopenharmony_ci 7570af302Sopenharmony_cisize_t __wcsftime_l(wchar_t *restrict s, size_t n, const wchar_t *restrict f, const struct tm *restrict tm, locale_t loc) 8570af302Sopenharmony_ci{ 9570af302Sopenharmony_ci size_t l, k; 10570af302Sopenharmony_ci char buf[100]; 11570af302Sopenharmony_ci wchar_t wbuf[100]; 12570af302Sopenharmony_ci wchar_t *p; 13570af302Sopenharmony_ci const char *t_mb; 14570af302Sopenharmony_ci const wchar_t *t; 15570af302Sopenharmony_ci int pad, plus; 16570af302Sopenharmony_ci unsigned long width; 17570af302Sopenharmony_ci for (l=0; l<n; f++) { 18570af302Sopenharmony_ci if (!*f) { 19570af302Sopenharmony_ci s[l] = 0; 20570af302Sopenharmony_ci return l; 21570af302Sopenharmony_ci } 22570af302Sopenharmony_ci if (*f != '%') { 23570af302Sopenharmony_ci s[l++] = *f; 24570af302Sopenharmony_ci continue; 25570af302Sopenharmony_ci } 26570af302Sopenharmony_ci f++; 27570af302Sopenharmony_ci pad = 0; 28570af302Sopenharmony_ci if (*f == '-' || *f == '_' || *f == '0') pad = *f++; 29570af302Sopenharmony_ci if ((plus = (*f == '+'))) f++; 30570af302Sopenharmony_ci width = wcstoul(f, &p, 10); 31570af302Sopenharmony_ci if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') { 32570af302Sopenharmony_ci if (!width && p!=f) width = 1; 33570af302Sopenharmony_ci } else { 34570af302Sopenharmony_ci width = 0; 35570af302Sopenharmony_ci } 36570af302Sopenharmony_ci f = p; 37570af302Sopenharmony_ci if (*f == 'E' || *f == 'O') f++; 38570af302Sopenharmony_ci t_mb = __strftime_fmt_1(&buf, &k, *f, tm, loc, pad); 39570af302Sopenharmony_ci if (!t_mb) break; 40570af302Sopenharmony_ci k = mbstowcs(wbuf, t_mb, sizeof wbuf / sizeof *wbuf); 41570af302Sopenharmony_ci if (k == (size_t)-1) return 0; 42570af302Sopenharmony_ci t = wbuf; 43570af302Sopenharmony_ci if (width) { 44570af302Sopenharmony_ci for (; *t=='+' || *t=='-' || (*t=='0'&&t[1]); t++, k--); 45570af302Sopenharmony_ci width--; 46570af302Sopenharmony_ci if (plus && tm->tm_year >= 10000-1900) 47570af302Sopenharmony_ci s[l++] = '+'; 48570af302Sopenharmony_ci else if (tm->tm_year < -1900) 49570af302Sopenharmony_ci s[l++] = '-'; 50570af302Sopenharmony_ci else 51570af302Sopenharmony_ci width++; 52570af302Sopenharmony_ci for (; width > k && l < n; width--) 53570af302Sopenharmony_ci s[l++] = '0'; 54570af302Sopenharmony_ci } 55570af302Sopenharmony_ci if (k >= n-l) k = n-l; 56570af302Sopenharmony_ci wmemcpy(s+l, t, k); 57570af302Sopenharmony_ci l += k; 58570af302Sopenharmony_ci } 59570af302Sopenharmony_ci if (n) { 60570af302Sopenharmony_ci if (l==n) l=n-1; 61570af302Sopenharmony_ci s[l] = 0; 62570af302Sopenharmony_ci } 63570af302Sopenharmony_ci return 0; 64570af302Sopenharmony_ci} 65570af302Sopenharmony_ci 66570af302Sopenharmony_cisize_t wcsftime(wchar_t *restrict wcs, size_t n, const wchar_t *restrict f, const struct tm *restrict tm) 67570af302Sopenharmony_ci{ 68570af302Sopenharmony_ci return __wcsftime_l(wcs, n, f, tm, CURRENT_LOCALE); 69570af302Sopenharmony_ci} 70570af302Sopenharmony_ci 71570af302Sopenharmony_ciweak_alias(__wcsftime_l, wcsftime_l); 72