Debian Codegraph @

libstdc++-v3/testsuite/std/time/format/localized.cc

raw
1 // { dg-do run { target c++20 } }
2 // { dg-require-namedlocale "es_ES.ISO8859-1" }
3 // { dg-require-namedlocale "fr_FR.ISO8859-1" }
4 // { dg-require-namedlocale "en_US.ISO8859-1" }
5 // { dg-require-namedlocale "en_US.ISO8859-15" }
6 // { dg-require-namedlocale "en_US.UTF-8" }
7
8 // P2419R2
9 // Clarify handling of encodings in localized formatting of chrono types
10
11 // Localized date-time strings such as "février" should be converted to UTF-8
12 // if the locale uses a different encoding.
13
14 #include <chrono>
15 #include <format>
16 #include <locale>
17 #include <stdio.h>
18 #include <testsuite_hooks.h>
19
20 void
21 test_ru()
22 {
23 bool ok = false;
24 #if __cpp_exceptions
25 std::locale loc;
26 try
27 {
28 loc = std::locale("ru_UA.KOI8-U");
29 ok = true;
30 }
31 catch (const std::runtime_error&)
32 {
33 try
34 {
35 loc = std::locale("ru_RU.KOI8-R");
36 ok = true;
37 }
38 catch (const std::runtime_error&)
39 {
40 }
41 }
42 #endif
43 if (ok)
44 {
45 auto s = std::format(loc, "День недели: {:L}", std::chrono::Monday);
46 VERIFY( s == "День недели: Пн" || s == "День недели: пн" );
47 }
48 else
49 puts("NOTE: test_ru(): skipped unsupported locales");
50 }
51
52 void
53 test_es()
54 {
55 std::locale loc(ISO_8859(1,es_ES));
56 auto s = std::format(loc, "Día de la semana: {:L%A %a}",
57 std::chrono::Wednesday);
58 if (s.back() == '.') // FreeBSD has this in the %a string
59 s.pop_back();
60 VERIFY( s == "Día de la semana: miércoles mié" );
61 }
62
63 void
64 test_fr()
65 {
66 std::locale loc(ISO_8859(1,fr_FR));
67 auto s = std::format(loc, "Six mois après {0:L%b}, c'est {1:L%B}.",
68 std::chrono::February, std::chrono::August);
69 VERIFY( s == "Six mois après févr., c'est août." );
70 }
71
72 void
73 test_en()
74 {
75 using namespace std::chrono;
76
77 for (auto l : {ISO_8859(1,en_US), ISO_8859(15,en_US), "en_US.UTF-8", "C"})
78 {
79 std::locale loc(l);
80 auto s = std::format(loc, "{:L%b %B %a %A}", sys_days(2024y/July/30));
81 VERIFY( s == "Jul July Tue Tuesday" );
82 }
83 }
84
85 void
86 test_locale_imbued()
87 {
88 // Custom time_put facet which returns %b string for %Om.
89 // The %b string will come from io.getloc() which should be
90 // the formatting locale using by std::format.
91 struct TimePut : std::time_put<char>
92 {
93 iter_type
94 do_put(iter_type out, std::ios_base& io, char_type fill, const tm* t,
95 char format, char modifier) const override
96 {
97 if (format == 'm' && modifier == 'O')
98 format = 'b';
99 return std::time_put<char>::do_put(out, io, fill, t, format, 0);
100 }
101 };
102
103 auto m = std::chrono::March;
104
105 std::locale fr(ISO_8859(1,fr_FR));
106 std::locale fr2(fr, new TimePut);
107 auto s1 = std::format(fr2, "{:L%Om}", m); // should be %b in fr_FR locale
108 VERIFY( s1 == std::format(fr, "{:L}", m) );
109
110 std::locale es(ISO_8859(1,es_ES));
111 std::locale es2(es, new TimePut);
112 auto s2 = std::format(es2, "{:L%Om}", m); // should be %b in es_ES locale
113 VERIFY( s2 == std::format(es, "{:L}", m) );
114 }
115
116 int main()
117 {
118 test_ru();
119 test_es();
120 test_fr();
121 test_en();
122 test_locale_imbued();
123 }