| 1 |
// { dg-do run { target c++20 } } |
| 2 |
// { dg-require-namedlocale "aa_DJ.UTF-8" } |
| 3 |
// { dg-require-namedlocale "ar_SA.UTF-8" } |
| 4 |
// { dg-require-namedlocale "ca_AD.UTF-8" } |
| 5 |
// { dg-require-namedlocale "az_IR.UTF-8" } |
| 6 |
// { dg-require-namedlocale "my_MM.UTF-8" } |
| 7 |
|
| 8 |
#include <chrono> |
| 9 |
#include <locale> |
| 10 |
#include <span> |
| 11 |
#include <testsuite_hooks.h> |
| 12 |
|
| 13 |
|
| 14 |
template<typename ChronoType> |
| 15 |
void |
| 16 |
test_locale_formats(const ChronoType& t, |
| 17 |
std::span<const char* const> test_specifiers) |
| 18 |
{ |
| 19 |
const char *test_locales[] = { |
| 20 |
"aa_DJ.UTF-8", |
| 21 |
"ar_SA.UTF-8", |
| 22 |
"ca_AD.UTF-8", |
| 23 |
"az_IR.UTF-8", |
| 24 |
"my_MM.UTF-8", |
| 25 |
}; |
| 26 |
|
| 27 |
auto format_args = std::make_format_args(t); |
| 28 |
for (auto locale_name : test_locales) |
| 29 |
{ |
| 30 |
std::locale loc(locale_name); |
| 31 |
for (auto specifier : test_specifiers) |
| 32 |
{ |
| 33 |
auto s = std::vformat(loc, specifier, format_args); |
| 34 |
VERIFY( !s.empty() ); |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
void |
| 40 |
test_locale_formats() |
| 41 |
{ |
| 42 |
using namespace std::chrono; |
| 43 |
|
| 44 |
const char* test_specifiers[] = { |
| 45 |
"{:L%x}", "{:L%Ex}", |
| 46 |
"{:L%c}", "{:L%Ec}", |
| 47 |
"{:L%X}", "{:L%EX}", |
| 48 |
"{:L%r}", |
| 49 |
}; |
| 50 |
auto date_time_specifiers = std::span(test_specifiers); |
| 51 |
auto date_specifiers = date_time_specifiers.subspan(0, 2); |
| 52 |
auto time_specifiers = date_time_specifiers.subspan(4); |
| 53 |
|
| 54 |
auto ymd = 2020y/November/12d; |
| 55 |
test_locale_formats(ymd, date_specifiers); |
| 56 |
|
| 57 |
auto tod = 25h + 10min + 12s; |
| 58 |
test_locale_formats(tod, time_specifiers); |
| 59 |
|
| 60 |
auto tp = sys_days(ymd) + tod; |
| 61 |
test_locale_formats(tp, date_time_specifiers); |
| 62 |
} |
| 63 |
|
| 64 |
#include <stdlib.h> |
| 65 |
#include <time.h> |
| 66 |
|
| 67 |
extern "C++" int tzset(int); |
| 68 |
|
| 69 |
template<typename T = void> |
| 70 |
void maybe_set_tz() |
| 71 |
{ |
| 72 |
#ifdef _GLIBCXX_HAVE_SETENV |
| 73 |
// True if a function 'T tzset()' has been declared. |
| 74 |
if constexpr (requires (T (*p)()) { p = &tzset; }) |
| 75 |
{ |
| 76 |
setenv("TZ", "GMT0", 1); |
| 77 |
tzset(); // Set C library's global time zone to GMT0 |
| 78 |
} |
| 79 |
#endif |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
test_c_zoned() |
| 84 |
{ |
| 85 |
#if _GLIBCXX_USE_CXX11_ABI |
| 86 |
maybe_set_tz(); |
| 87 |
|
| 88 |
using namespace std::chrono; |
| 89 |
auto time = sys_days(2025y/March/19) + 15h; |
| 90 |
zoned_time z("America/New_York", time); |
| 91 |
auto s = std::format(std::locale("aa_DJ.UTF-8"), "{:L%c}", z); |
| 92 |
VERIFY( s.find("11:00") != s.npos ); |
| 93 |
|
| 94 |
// We should not print an incorrect time zone. |
| 95 |
VERIFY( s.find("GMT") == s.npos ); |
| 96 |
VERIFY( s.find("UTC") == s.npos ); |
| 97 |
|
| 98 |
#ifdef _GLIBCXX_HAVE_STRUCT_TM_TM_ZONE |
| 99 |
// When tm.tm_zone is supported, we do print the correct time zone. |
| 100 |
VERIFY( s.find(z.get_info().abbrev) != s.npos ); |
| 101 |
#endif |
| 102 |
#endif |
| 103 |
} |
| 104 |
|
| 105 |
void |
| 106 |
test_c_local() |
| 107 |
{ |
| 108 |
maybe_set_tz(); |
| 109 |
|
| 110 |
std::locale("aa_DJ.UTF-8"); |
| 111 |
using namespace std::chrono; |
| 112 |
auto time = local_days(2025y/March/19) + 11h; |
| 113 |
auto s = std::format(std::locale("aa_DJ.UTF-8"), "{:L%c}", time); |
| 114 |
// __builtin_printf("%s\n", s.c_str()); return; |
| 115 |
VERIFY( s.find("11:00") != s.npos ); |
| 116 |
|
| 117 |
// We should not print any time zone. |
| 118 |
VERIFY( s.find("GMT") == s.npos ); |
| 119 |
VERIFY( s.find("UTC") == s.npos ); |
| 120 |
|
| 121 |
#if _GLIBCXX_USE_CXX11_ABI |
| 122 |
zoned_time z(system_clock::now()); |
| 123 |
VERIFY( s.find(z.get_info().abbrev) == s.npos ); |
| 124 |
#endif |
| 125 |
} |
| 126 |
|
| 127 |
int main() |
| 128 |
{ |
| 129 |
test_locale_formats(); |
| 130 |
test_c_zoned(); |
| 131 |
test_c_local(); |
| 132 |
} |