tst-strftime2.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Verify the behavior of strftime on alternative representation for
  2. year.
  3. Copyright (C) 2019-2026 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #include <array_length.h>
  17. #include <stdbool.h>
  18. #include <support/check.h>
  19. #include <stdlib.h>
  20. #include <locale.h>
  21. #include <time.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. static const char *locales[] =
  25. {
  26. "ja_JP.UTF-8", "lo_LA.UTF-8", "th_TH.UTF-8",
  27. "zh_TW.UTF-8", "cmn_TW.UTF-8", "hak_TW.UTF-8",
  28. "nan_TW.UTF-8", "lzh_TW.UTF-8"
  29. };
  30. /* Must match locale index into locales array. */
  31. enum
  32. {
  33. ja_JP, lo_LA, th_TH,
  34. zh_TW, cmn_TW, hak_TW, nan_TW, lzh_TW
  35. };
  36. static const char *formats[] = { "%EY", "%_EY", "%-EY" };
  37. typedef struct
  38. {
  39. const int d, m, y;
  40. } date_t;
  41. static const date_t dates[] =
  42. {
  43. { 1, 4, 1910 },
  44. { 31, 12, 1911 },
  45. { 1, 1, 1912 },
  46. { 1, 4, 1913 },
  47. { 1, 4, 1988 },
  48. { 7, 1, 1989 },
  49. { 8, 1, 1989 },
  50. { 1, 4, 1990 },
  51. { 1, 4, 1997 },
  52. { 1, 4, 1998 },
  53. { 1, 4, 2010 },
  54. { 1, 4, 2011 },
  55. { 30, 4, 2019 },
  56. { 1, 5, 2019 }
  57. };
  58. static char ref[array_length (locales)][array_length (formats)]
  59. [array_length (dates)][100];
  60. static bool
  61. is_before (const int i, const int d, const int m, const int y)
  62. {
  63. if (dates[i].y < y)
  64. return true;
  65. else if (dates[i].y > y)
  66. return false;
  67. else if (dates[i].m < m)
  68. return true;
  69. else if (dates[i].m > m)
  70. return false;
  71. else
  72. return dates[i].d < d;
  73. }
  74. static void
  75. mkreftable (void)
  76. {
  77. int i, j, k, yr;
  78. const char *era, *sfx;
  79. /* Japanese era year to be checked. */
  80. static const int yrj[] =
  81. {
  82. 43, 44, 45, 2,
  83. 63, 64, 1, 2, 9, 10, 22, 23, 31, 1
  84. };
  85. /* Buddhist calendar year to be checked. */
  86. static const int yrb[] =
  87. {
  88. 2453, 2454, 2455, 2456,
  89. 2531, 2532, 2532, 2533, 2540, 2541, 2553, 2554, 2562, 2562
  90. };
  91. /* R.O.C. calendar year to be checked. Negative number is prior to
  92. Minguo counting up. */
  93. static const int yrc[] =
  94. {
  95. -2, -1, 1, 2,
  96. 77, 78, 78, 79, 86, 87, 99, 100, 108, 108
  97. };
  98. for (i = 0; i < array_length (locales); i++)
  99. for (j = 0; j < array_length (formats); j++)
  100. for (k = 0; k < array_length (dates); k++)
  101. {
  102. if (i == ja_JP)
  103. {
  104. era = (is_before (k, 30, 7, 1912)) ? "\u660e\u6cbb"
  105. : (is_before (k, 25, 12, 1926)) ? "\u5927\u6b63"
  106. : (is_before (k, 8, 1, 1989)) ? "\u662d\u548c"
  107. : (is_before (k, 1, 5, 2019)) ? "\u5e73\u6210"
  108. : "\u4ee4\u548c";
  109. yr = yrj[k], sfx = "\u5e74";
  110. }
  111. else if (i == lo_LA)
  112. era = "\u0e9e.\u0eaa. ", yr = yrb[k], sfx = "";
  113. else if (i == th_TH)
  114. era = "\u0e1e.\u0e28. ", yr = yrb[k], sfx = "";
  115. else if (i == zh_TW || i == cmn_TW || i == hak_TW
  116. || i == nan_TW || i == lzh_TW)
  117. {
  118. era = (is_before (k, 1, 1, 1912)) ? "\u6c11\u524d"
  119. : "\u6c11\u570b";
  120. yr = yrc[k], sfx = "\u5e74";
  121. }
  122. else
  123. FAIL_EXIT1 ("Invalid table index!");
  124. if (yr == 1)
  125. sprintf (ref[i][j][k], "%s\u5143%s", era, sfx);
  126. else if (j == 0)
  127. sprintf (ref[i][j][k], "%s%02d%s", era, abs (yr), sfx);
  128. else if (j == 1)
  129. sprintf (ref[i][j][k], "%s%2d%s", era, abs (yr), sfx);
  130. else if (j == 2)
  131. sprintf (ref[i][j][k], "%s%d%s", era, abs (yr), sfx);
  132. else
  133. FAIL_EXIT1 ("Invalid table index!");
  134. }
  135. }
  136. static int
  137. do_test (void)
  138. {
  139. int i, j, k, result = 0;
  140. struct tm ttm;
  141. char date[11], buf[100];
  142. size_t r, e;
  143. mkreftable ();
  144. for (i = 0; i < array_length (locales); i++)
  145. {
  146. if (setlocale (LC_ALL, locales[i]) == NULL)
  147. {
  148. printf ("locale %s does not exist, skipping...\n", locales[i]);
  149. continue;
  150. }
  151. printf ("[%s]\n", locales[i]);
  152. for (j = 0; j < array_length (formats); j++)
  153. {
  154. for (k = 0; k < array_length (dates); k++)
  155. {
  156. ttm.tm_mday = dates[k].d;
  157. ttm.tm_mon = dates[k].m - 1;
  158. ttm.tm_year = dates[k].y - 1900;
  159. strftime (date, sizeof (date), "%F", &ttm);
  160. r = strftime (buf, sizeof (buf), formats[j], &ttm);
  161. e = strlen (ref[i][j][k]);
  162. printf ("%s\t\"%s\"\t\"%s\"", date, formats[j], buf);
  163. if (strcmp (buf, ref[i][j][k]) != 0)
  164. {
  165. printf ("\tshould be \"%s\"", ref[i][j][k]);
  166. if (r != e)
  167. printf ("\tgot: %zu, expected: %zu", r, e);
  168. result = 1;
  169. }
  170. else
  171. printf ("\tOK");
  172. putchar ('\n');
  173. }
  174. putchar ('\n');
  175. }
  176. }
  177. return result;
  178. }
  179. #include <support/test-driver.c>