wcsftime.c 1.5 KB

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