tst-ftime_l.c 2.7 KB

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