| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 |
// See https://llvm.org/LICENSE.txt for license information. |
| 4 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 |
// |
| 6 |
//===----------------------------------------------------------------------===// |
| 7 |
|
| 8 |
// UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 9 |
// UNSUPPORTED: no-localization |
| 10 |
|
| 11 |
// TODO FMT This test should not require std::to_chars(floating-point) |
| 12 |
// XFAIL: availability-fp_to_chars-missing |
| 13 |
|
| 14 |
// XFAIL: libcpp-has-no-experimental-tzdb |
| 15 |
|
| 16 |
// REQUIRES: locale.fr_FR.UTF-8 |
| 17 |
// REQUIRES: locale.ja_JP.UTF-8 |
| 18 |
|
| 19 |
// <chrono> |
| 20 |
// |
| 21 |
// template<class charT> struct formatter<chrono::sys_info, charT>; |
| 22 |
|
| 23 |
#include <chrono> |
| 24 |
#include <format> |
| 25 |
|
| 26 |
#include <cassert> |
| 27 |
#include <concepts> |
| 28 |
#include <locale> |
| 29 |
#include <iostream> |
| 30 |
#include <type_traits> |
| 31 |
|
| 32 |
#include "formatter_tests.h" |
| 33 |
#include "make_string.h" |
| 34 |
#include "platform_support.h" // locale name macros |
| 35 |
#include "test_macros.h" |
| 36 |
|
| 37 |
template <class CharT> |
| 38 |
static void test_no_chrono_specs() { |
| 39 |
// This test libc++ specific due to |
| 40 |
// [time.zone.info.sys]/7 |
| 41 |
// Effects: Streams out the sys_info object r in an unspecified format. |
| 42 |
#ifdef _LIBCPP_VERSION |
| 43 |
using namespace std::literals::chrono_literals; |
| 44 |
namespace tz = std::chrono; |
| 45 |
|
| 46 |
std::locale::global(std::locale(LOCALE_fr_FR_UTF_8)); |
| 47 |
|
| 48 |
// Non localized output |
| 49 |
|
| 50 |
check(SV("[-10484-10-16 15:30:08, 14423-03-17 15:30:07) 00:00:00 0min \"TZ\""), |
| 51 |
SV("{}"), |
| 52 |
tz::sys_info{tz::sys_seconds::min(), tz::sys_seconds::max(), 0s, 0min, "TZ"}); |
| 53 |
|
| 54 |
check(SV("[1970-01-01 00:00:00, 2038-12-31 00:00:00) 12:23:45 -67min \"DMY\""), |
| 55 |
SV("{}"), |
| 56 |
tz::sys_info{static_cast<tz::sys_days>(tz::year_month_day{1970y, tz::January, 1d}), |
| 57 |
static_cast<tz::sys_days>(tz::year_month_day{2038y, tz::December, 31d}), |
| 58 |
12h + 23min + 45s, |
| 59 |
-67min, |
| 60 |
"DMY"}); |
| 61 |
|
| 62 |
std::locale::global(std::locale::classic()); |
| 63 |
#endif // _LIBCPP_VERSION |
| 64 |
} |
| 65 |
|
| 66 |
template <class CharT> |
| 67 |
static void test_valid_values() { |
| 68 |
using namespace std::literals::chrono_literals; |
| 69 |
|
| 70 |
constexpr std::basic_string_view<CharT> fmt = SV("{:%%z='%z'%t%%Ez='%Ez'%t%%Oz='%Oz'%t%%Z='%Z'%n}"); |
| 71 |
constexpr std::basic_string_view<CharT> lfmt = SV("{:L%%z='%z'%t%%Ez='%Ez'%t%%Oz='%Oz'%t%%Z='%Z'%n}"); |
| 72 |
|
| 73 |
const std::locale loc(LOCALE_ja_JP_UTF_8); |
| 74 |
std::locale::global(std::locale(LOCALE_fr_FR_UTF_8)); |
| 75 |
|
| 76 |
// Non localized output using C-locale |
| 77 |
check(SV("%z='-0200'\t%Ez='-02:00'\t%Oz='-02:00'\t%Z='NEG'\n"), |
| 78 |
fmt, |
| 79 |
std::chrono::sys_info{std::chrono::sys_seconds{0s}, std::chrono::sys_seconds{0s}, -2h, 0min, "NEG"}); |
| 80 |
|
| 81 |
check(SV("%z='+0000'\t%Ez='+00:00'\t%Oz='+00:00'\t%Z='ZERO'\n"), |
| 82 |
fmt, |
| 83 |
std::chrono::sys_info{std::chrono::sys_seconds{0s}, std::chrono::sys_seconds{0s}, 0s, 0min, "ZERO"}); |
| 84 |
|
| 85 |
check(SV("%z='+1115'\t%Ez='+11:15'\t%Oz='+11:15'\t%Z='POS'\n"), |
| 86 |
fmt, |
| 87 |
std::chrono::sys_info{std::chrono::sys_seconds{0s}, std::chrono::sys_seconds{0s}, 11h + 15min, 0min, "POS"}); |
| 88 |
|
| 89 |
// Use the global locale (fr_FR) |
| 90 |
check(SV("%z='-0200'\t%Ez='-02:00'\t%Oz='-02:00'\t%Z='NEG'\n"), |
| 91 |
lfmt, |
| 92 |
std::chrono::sys_info{std::chrono::sys_seconds{0s}, std::chrono::sys_seconds{0s}, -2h, 0min, "NEG"}); |
| 93 |
|
| 94 |
check(SV("%z='+0000'\t%Ez='+00:00'\t%Oz='+00:00'\t%Z='ZERO'\n"), |
| 95 |
lfmt, |
| 96 |
std::chrono::sys_info{std::chrono::sys_seconds{0s}, std::chrono::sys_seconds{0s}, 0s, 0min, "ZERO"}); |
| 97 |
|
| 98 |
check(SV("%z='+1115'\t%Ez='+11:15'\t%Oz='+11:15'\t%Z='POS'\n"), |
| 99 |
lfmt, |
| 100 |
std::chrono::sys_info{std::chrono::sys_seconds{0s}, std::chrono::sys_seconds{0s}, 11h + 15min, 0min, "POS"}); |
| 101 |
|
| 102 |
// Use supplied locale (ja_JP). |
| 103 |
check(loc, |
| 104 |
SV("%z='-0200'\t%Ez='-02:00'\t%Oz='-02:00'\t%Z='NEG'\n"), |
| 105 |
lfmt, |
| 106 |
std::chrono::sys_info{std::chrono::sys_seconds{0s}, std::chrono::sys_seconds{0s}, -2h, 0min, "NEG"}); |
| 107 |
|
| 108 |
check(loc, |
| 109 |
SV("%z='+0000'\t%Ez='+00:00'\t%Oz='+00:00'\t%Z='ZERO'\n"), |
| 110 |
lfmt, |
| 111 |
std::chrono::sys_info{std::chrono::sys_seconds{0s}, std::chrono::sys_seconds{0s}, 0s, 0min, "ZERO"}); |
| 112 |
|
| 113 |
check(loc, |
| 114 |
SV("%z='+1115'\t%Ez='+11:15'\t%Oz='+11:15'\t%Z='POS'\n"), |
| 115 |
lfmt, |
| 116 |
std::chrono::sys_info{std::chrono::sys_seconds{0s}, std::chrono::sys_seconds{0s}, 11h + 15min, 0min, "POS"}); |
| 117 |
|
| 118 |
std::locale::global(std::locale::classic()); |
| 119 |
} |
| 120 |
|
| 121 |
template <class CharT> |
| 122 |
static void test() { |
| 123 |
test_no_chrono_specs<CharT>(); |
| 124 |
test_valid_values<CharT>(); |
| 125 |
|
| 126 |
check_invalid_types<CharT>({SV("z"), SV("Z"), SV("Ez"), SV("Oz")}, std::chrono::sys_info{}); |
| 127 |
} |
| 128 |
|
| 129 |
int main(int, char**) { |
| 130 |
test<char>(); |
| 131 |
|
| 132 |
#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 133 |
test<wchar_t>(); |
| 134 |
#endif |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |