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

source code of glibc/time/tst-strftime2.c