|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 10 | +// UNSUPPORTED: no-localization |
| 11 | +// UNSUPPORTED: libcpp-has-no-incomplete-format |
| 12 | + |
| 13 | +// TODO FMT Investigate Windows issues. |
| 14 | +// UNSUPPORTED msvc, target={{.+}}-windows-gnu |
| 15 | +// TODO FMT It seems GCC uses too much memory in the CI and fails. |
| 16 | +// UNSUPPORTED: gcc-12 |
| 17 | + |
| 18 | +// TODO FMT This test should not require std::to_chars(floating-point) |
| 19 | +// XFAIL: availability-fp_to_chars-missing |
| 20 | + |
| 21 | +// REQUIRES: locale.fr_FR.UTF-8 |
| 22 | +// REQUIRES: locale.ja_JP.UTF-8 |
| 23 | + |
| 24 | +// <chrono> |
| 25 | + |
| 26 | +// class system_clock; |
| 27 | + |
| 28 | +// template<class charT, class traits, class Duration> |
| 29 | +// basic_ostream<charT, traits>& |
| 30 | +// operator<<(basic_ostream<charT, traits>& os, const sys_time<Duration>& tp); |
| 31 | + |
| 32 | +#include <chrono> |
| 33 | +#include <cassert> |
| 34 | +#include <ratio> |
| 35 | +#include <sstream> |
| 36 | + |
| 37 | +#include "make_string.h" |
| 38 | +#include "platform_support.h" // locale name macros |
| 39 | +#include "test_macros.h" |
| 40 | + |
| 41 | +#define SV(S) MAKE_STRING_VIEW(CharT, S) |
| 42 | + |
| 43 | +// Modeled after the system clock's usings, this make adapating the |
| 44 | +// tests easier. |
| 45 | +template <class Duration> |
| 46 | +using file_time = std::chrono::time_point<std::chrono::file_clock, Duration>; |
| 47 | +using file_seconds = file_time<std::chrono::seconds>; |
| 48 | +using file_days = file_time<std::chrono::days>; |
| 49 | + |
| 50 | +template <class CharT, class Duration> |
| 51 | +static std::basic_string<CharT> stream_c_locale(file_time<Duration> time_point) { |
| 52 | + std::basic_stringstream<CharT> sstr; |
| 53 | + sstr << std::fixed << time_point; |
| 54 | + return sstr.str(); |
| 55 | +} |
| 56 | + |
| 57 | +template <class CharT, class Duration> |
| 58 | +static std::basic_string<CharT> stream_fr_FR_locale(file_time<Duration> time_point) { |
| 59 | + std::basic_stringstream<CharT> sstr; |
| 60 | + const std::locale locale(LOCALE_fr_FR_UTF_8); |
| 61 | + sstr.imbue(locale); |
| 62 | + sstr << std::fixed << time_point; |
| 63 | + return sstr.str(); |
| 64 | +} |
| 65 | + |
| 66 | +template <class CharT, class Duration> |
| 67 | +static std::basic_string<CharT> stream_ja_JP_locale(file_time<Duration> time_point) { |
| 68 | + std::basic_stringstream<CharT> sstr; |
| 69 | + const std::locale locale(LOCALE_ja_JP_UTF_8); |
| 70 | + sstr.imbue(locale); |
| 71 | + sstr << std::fixed << time_point; |
| 72 | + return sstr.str(); |
| 73 | +} |
| 74 | + |
| 75 | +template <class CharT> |
| 76 | +static void test_c() { |
| 77 | + using namespace std::literals::chrono_literals; |
| 78 | + |
| 79 | + assert(stream_c_locale<CharT>(file_time<std::chrono::nanoseconds>{946'688'523'123'456'789ns}) == |
| 80 | + SV("2000-01-01 01:02:03.123456789")); |
| 81 | + assert(stream_c_locale<CharT>(file_time<std::chrono::microseconds>{946'688'523'123'456us}) == |
| 82 | + SV("2000-01-01 01:02:03.123456")); |
| 83 | + |
| 84 | + assert(stream_c_locale<CharT>(file_time<std::chrono::milliseconds>{946'684'800'123ms}) == |
| 85 | + SV("2000-01-01 00:00:00.123")); |
| 86 | + assert(stream_c_locale<CharT>(file_seconds{1'234'567'890s}) == SV("2009-02-13 23:31:30")); |
| 87 | + assert(stream_c_locale<CharT>(file_time<std::chrono::minutes>{20'576'131min}) == SV("2009-02-13 23:31:00")); |
| 88 | + assert(stream_c_locale<CharT>(file_time<std::chrono::hours>{342'935h}) == SV("2009-02-13 23:00:00")); |
| 89 | + assert(stream_c_locale<CharT>(file_days{std::chrono::days{14'288}}) == SV("2009-02-13 00:00:00")); |
| 90 | + assert(stream_c_locale<CharT>(file_time<std::chrono::weeks>{std::chrono::weeks{2041}}) == SV("2009-02-12 00:00:00")); |
| 91 | + |
| 92 | + assert(stream_c_locale<CharT>(file_time<std::chrono::duration<signed char, std::ratio<2, 1>>>{ |
| 93 | + std::chrono::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1970-01-01 00:02:00")); |
| 94 | + assert(stream_c_locale<CharT>(file_time<std::chrono::duration<short, std::ratio<1, 2>>>{ |
| 95 | + std::chrono::duration<short, std::ratio<1, 2>>{3600}}) == SV("1970-01-01 00:30:00.0")); |
| 96 | + assert(stream_c_locale<CharT>(file_time<std::chrono::duration<int, std::ratio<1, 4>>>{ |
| 97 | + std::chrono::duration<int, std::ratio<1, 4>>{3600}}) == SV("1970-01-01 00:15:00.00")); |
| 98 | + assert(stream_c_locale<CharT>(file_time<std::chrono::duration<long, std::ratio<1, 10>>>{ |
| 99 | + std::chrono::duration<long, std::ratio<1, 10>>{36611}}) == SV("1970-01-01 01:01:01.1")); |
| 100 | + assert(stream_c_locale<CharT>(file_time<std::chrono::duration<long long, std::ratio<1, 100>>>{ |
| 101 | + std::chrono::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:30.10")); |
| 102 | + |
| 103 | + assert(stream_c_locale<CharT>(file_time<std::chrono::duration<float, std::ratio<1, 1>>>{ |
| 104 | + std::chrono::duration<float, std::ratio<1, 1>>{123.456}}) == SV("1970-01-01 00:02:03")); |
| 105 | + assert(stream_c_locale<CharT>(file_time<std::chrono::duration<double, std::ratio<1, 10>>>{ |
| 106 | + std::chrono::duration<double, std::ratio<1, 10>>{123.456}}) == SV("1970-01-01 00:00:12.3")); |
| 107 | + assert(stream_c_locale<CharT>(file_time<std::chrono::duration<long double, std::ratio<1, 100>>>{ |
| 108 | + std::chrono::duration<long double, std::ratio<1, 100>>{123.456}}) == SV("1970-01-01 00:00:01.23")); |
| 109 | +} |
| 110 | + |
| 111 | +template <class CharT> |
| 112 | +static void test_fr_FR() { |
| 113 | + using namespace std::literals::chrono_literals; |
| 114 | + |
| 115 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::nanoseconds>{946'688'523'123'456'789ns}) == |
| 116 | + SV("2000-01-01 01:02:03,123456789")); |
| 117 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::microseconds>{946'688'523'123'456us}) == |
| 118 | + SV("2000-01-01 01:02:03,123456")); |
| 119 | + |
| 120 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::milliseconds>{946'684'800'123ms}) == |
| 121 | + SV("2000-01-01 00:00:00,123")); |
| 122 | + assert(stream_fr_FR_locale<CharT>(file_seconds{1'234'567'890s}) == SV("2009-02-13 23:31:30")); |
| 123 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::minutes>{20'576'131min}) == SV("2009-02-13 23:31:00")); |
| 124 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::hours>{342'935h}) == SV("2009-02-13 23:00:00")); |
| 125 | + assert(stream_fr_FR_locale<CharT>(file_days{std::chrono::days{14'288}}) == SV("2009-02-13 00:00:00")); |
| 126 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::weeks>{std::chrono::weeks{2041}}) == |
| 127 | + SV("2009-02-12 00:00:00")); |
| 128 | + |
| 129 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<signed char, std::ratio<2, 1>>>{ |
| 130 | + std::chrono::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1970-01-01 00:02:00")); |
| 131 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<short, std::ratio<1, 2>>>{ |
| 132 | + std::chrono::duration<short, std::ratio<1, 2>>{3600}}) == SV("1970-01-01 00:30:00,0")); |
| 133 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<int, std::ratio<1, 4>>>{ |
| 134 | + std::chrono::duration<int, std::ratio<1, 4>>{3600}}) == SV("1970-01-01 00:15:00,00")); |
| 135 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<long, std::ratio<1, 10>>>{ |
| 136 | + std::chrono::duration<long, std::ratio<1, 10>>{36611}}) == SV("1970-01-01 01:01:01,1")); |
| 137 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<long long, std::ratio<1, 100>>>{ |
| 138 | + std::chrono::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:30,10")); |
| 139 | + |
| 140 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<float, std::ratio<1, 1>>>{ |
| 141 | + std::chrono::duration<float, std::ratio<1, 1>>{123.456}}) == SV("1970-01-01 00:02:03")); |
| 142 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<double, std::ratio<1, 10>>>{ |
| 143 | + std::chrono::duration<double, std::ratio<1, 10>>{123.456}}) == SV("1970-01-01 00:00:12,3")); |
| 144 | + assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<long double, std::ratio<1, 100>>>{ |
| 145 | + std::chrono::duration<long double, std::ratio<1, 100>>{123.456}}) == SV("1970-01-01 00:00:01,23")); |
| 146 | +} |
| 147 | + |
| 148 | +template <class CharT> |
| 149 | +static void test_ja_JP() { |
| 150 | + using namespace std::literals::chrono_literals; |
| 151 | + |
| 152 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::nanoseconds>{946'688'523'123'456'789ns}) == |
| 153 | + SV("2000-01-01 01:02:03.123456789")); |
| 154 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::microseconds>{946'688'523'123'456us}) == |
| 155 | + SV("2000-01-01 01:02:03.123456")); |
| 156 | + |
| 157 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::milliseconds>{946'684'800'123ms}) == |
| 158 | + SV("2000-01-01 00:00:00.123")); |
| 159 | + assert(stream_ja_JP_locale<CharT>(file_seconds{1'234'567'890s}) == SV("2009-02-13 23:31:30")); |
| 160 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::minutes>{20'576'131min}) == SV("2009-02-13 23:31:00")); |
| 161 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::hours>{342'935h}) == SV("2009-02-13 23:00:00")); |
| 162 | + assert(stream_ja_JP_locale<CharT>(file_days{std::chrono::days{14'288}}) == SV("2009-02-13 00:00:00")); |
| 163 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::weeks>{std::chrono::weeks{2041}}) == |
| 164 | + SV("2009-02-12 00:00:00")); |
| 165 | + |
| 166 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<signed char, std::ratio<2, 1>>>{ |
| 167 | + std::chrono::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1970-01-01 00:02:00")); |
| 168 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<short, std::ratio<1, 2>>>{ |
| 169 | + std::chrono::duration<short, std::ratio<1, 2>>{3600}}) == SV("1970-01-01 00:30:00.0")); |
| 170 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<int, std::ratio<1, 4>>>{ |
| 171 | + std::chrono::duration<int, std::ratio<1, 4>>{3600}}) == SV("1970-01-01 00:15:00.00")); |
| 172 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<long, std::ratio<1, 10>>>{ |
| 173 | + std::chrono::duration<long, std::ratio<1, 10>>{36611}}) == SV("1970-01-01 01:01:01.1")); |
| 174 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<long long, std::ratio<1, 100>>>{ |
| 175 | + std::chrono::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:30.10")); |
| 176 | + |
| 177 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<float, std::ratio<1, 1>>>{ |
| 178 | + std::chrono::duration<float, std::ratio<1, 1>>{123.456}}) == SV("1970-01-01 00:02:03")); |
| 179 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<double, std::ratio<1, 10>>>{ |
| 180 | + std::chrono::duration<double, std::ratio<1, 10>>{123.456}}) == SV("1970-01-01 00:00:12.3")); |
| 181 | + assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<long double, std::ratio<1, 100>>>{ |
| 182 | + std::chrono::duration<long double, std::ratio<1, 100>>{123.456}}) == SV("1970-01-01 00:00:01.23")); |
| 183 | +} |
| 184 | + |
| 185 | +template <class CharT> |
| 186 | +static void test() { |
| 187 | + test_c<CharT>(); |
| 188 | + test_fr_FR<CharT>(); |
| 189 | + test_ja_JP<CharT>(); |
| 190 | +} |
| 191 | + |
| 192 | +int main(int, char**) { |
| 193 | + test<char>(); |
| 194 | + |
| 195 | +#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 196 | + test<wchar_t>(); |
| 197 | +#endif |
| 198 | + |
| 199 | + return 0; |
| 200 | +} |
0 commit comments