tst-ftime_l.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <features.h>
  6. #ifdef __UCLIBC_HAS_XLOCALE__
  7. #include <locale.h>
  8. #include <wchar.h>
  9. int
  10. main (void)
  11. {
  12. locale_t l;
  13. locale_t old;
  14. struct tm tm;
  15. char buf[1000];
  16. wchar_t wbuf[1000];
  17. int result = 0;
  18. size_t n;
  19. l = newlocale (LC_ALL_MASK, "de_DE.ISO-8859-1", NULL);
  20. if (l == NULL)
  21. {
  22. puts ("newlocale failed");
  23. exit (1);
  24. }
  25. memset (&tm, '\0', sizeof (tm));
  26. tm.tm_year = 102;
  27. tm.tm_mon = 2;
  28. tm.tm_mday = 1;
  29. if (strftime (buf, sizeof (buf), "%e %^B %Y", &tm) == 0)
  30. {
  31. puts ("initial strftime failed");
  32. exit (1);
  33. }
  34. if (strcmp (buf, " 1 MARCH 2002") != 0)
  35. {
  36. printf ("initial strftime: expected \"%s\", got \"%s\"\n",
  37. " 1 MARCH 2002", buf);
  38. result = 1;
  39. }
  40. else
  41. printf ("got \"%s\"\n", buf);
  42. /* Now using the extended locale model. */
  43. if (strftime_l (buf, sizeof (buf), "%e %^B %Y", &tm, l) == 0)
  44. {
  45. puts ("strftime_l failed");
  46. result = 1;
  47. }
  48. else if (strcmp (buf, " 1 M\xc4RZ 2002") != 0)
  49. {
  50. printf ("strftime_l: expected \"%s\", got \"%s\"\n",
  51. " 1 M\xc4RZ 2002", buf);
  52. result = 1;
  53. }
  54. else
  55. {
  56. setlocale (LC_ALL, "de_DE.ISO-8859-1");
  57. printf ("got \"%s\"\n", buf);
  58. setlocale (LC_ALL, "C");
  59. }
  60. /* And the wide character version. */
  61. if (wcsftime_l (wbuf, sizeof (wbuf) / sizeof (wbuf[0]), L"%e %^B %Y", &tm, l)
  62. == 0)
  63. {
  64. puts ("wcsftime_l failed");
  65. result = 1;
  66. }
  67. else if (wcscmp (wbuf, L" 1 M\x00c4RZ 2002") != 0)
  68. {
  69. printf ("wcsftime_l: expected \"%ls\", got \"%ls\"\n",
  70. L" 1 M\x00c4RZ 2002", wbuf);
  71. result = 1;
  72. }
  73. else
  74. {
  75. setlocale (LC_ALL, "de_DE.ISO-8859-1");
  76. printf ("got \"%ls\"\n", wbuf);
  77. setlocale (LC_ALL, "C");
  78. }
  79. old = uselocale (l);
  80. n = strftime (buf, sizeof (buf), "%e %^B %Y", &tm);
  81. /* Switch back. */
  82. (void) uselocale (old);
  83. if (n == 0)
  84. {
  85. puts ("strftime after first uselocale failed");
  86. result = 1;
  87. }
  88. else if (strcmp (buf, " 1 M\xc4RZ 2002") != 0)
  89. {
  90. printf ("strftime in non-C locale: expected \"%s\", got \"%s\"\n",
  91. " 1 M\xc4RZ 2002", buf);
  92. result = 1;
  93. }
  94. else
  95. {
  96. setlocale (LC_ALL, "de_DE.ISO-8859-1");
  97. printf ("got \"%s\"\n", buf);
  98. setlocale (LC_ALL, "C");
  99. }
  100. if (strftime (buf, sizeof (buf), "%e %^B %Y", &tm) == 0)
  101. {
  102. puts ("strftime after second uselocale failed");
  103. result = 1;
  104. }
  105. else if (strcmp (buf, " 1 MARCH 2002") != 0)
  106. {
  107. printf ("initial strftime: expected \"%s\", got \"%s\"\n",
  108. " 1 MARCH 2002", buf);
  109. result = 1;
  110. }
  111. else
  112. printf ("got \"%s\"\n", buf);
  113. return result;
  114. }
  115. #else
  116. int main(void)
  117. {
  118. puts("Test requires WCHAR support; skipping");
  119. return 0;
  120. }
  121. #endif