| 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-filesystem, no-localization, no-tzdb |
| 11 | // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME |
| 12 | |
| 13 | // TODO FMT This test should not require std::to_chars(floating-point) |
| 14 | // XFAIL: availability-fp_to_chars-missing |
| 15 | |
| 16 | // XFAIL: libcpp-has-no-experimental-tzdb |
| 17 | // XFAIL: availability-tzdb-missing |
| 18 | |
| 19 | // REQUIRES: locale.fr_FR.UTF-8 |
| 20 | // REQUIRES: locale.ja_JP.UTF-8 |
| 21 | |
| 22 | // <chrono> |
| 23 | |
| 24 | // using utc_time = ...; |
| 25 | |
| 26 | // template<class charT, class traits, class Duration> |
| 27 | // basic_ostream<charT, traits>& |
| 28 | // operator<<(basic_ostream<charT, traits>& os, const utc_time<Duration>& tp); |
| 29 | |
| 30 | #include <chrono> |
| 31 | #include <cassert> |
| 32 | #include <ratio> |
| 33 | #include <sstream> |
| 34 | |
| 35 | #include "make_string.h" |
| 36 | #include "platform_support.h" // locale name macros |
| 37 | #include "test_macros.h" |
| 38 | |
| 39 | #define SV(S) MAKE_STRING_VIEW(CharT, S) |
| 40 | |
| 41 | template <class CharT, class Duration> |
| 42 | static std::basic_string<CharT> stream_c_locale(std::chrono::utc_time<Duration> time_point) { |
| 43 | std::basic_stringstream<CharT> sstr; |
| 44 | sstr << std::fixed << time_point; |
| 45 | return sstr.str(); |
| 46 | } |
| 47 | |
| 48 | template <class CharT, class Duration> |
| 49 | static std::basic_string<CharT> stream_fr_FR_locale(std::chrono::utc_time<Duration> time_point) { |
| 50 | std::basic_stringstream<CharT> sstr; |
| 51 | const std::locale locale(LOCALE_fr_FR_UTF_8); |
| 52 | sstr.imbue(locale); |
| 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_ja_JP_locale(std::chrono::utc_time<Duration> time_point) { |
| 59 | std::basic_stringstream<CharT> sstr; |
| 60 | const std::locale locale(LOCALE_ja_JP_UTF_8); |
| 61 | sstr.imbue(locale); |
| 62 | sstr << std::fixed << time_point; |
| 63 | return sstr.str(); |
| 64 | } |
| 65 | |
| 66 | template <class CharT> |
| 67 | static void test_c() { |
| 68 | using namespace std::literals::chrono_literals; |
| 69 | |
| 70 | assert(stream_c_locale<CharT>(std::chrono::utc_time<std::chrono::nanoseconds>{946'688'523'123'456'789ns}) == |
| 71 | SV("2000-01-01 01:01:41.123456789" )); |
| 72 | assert(stream_c_locale<CharT>(std::chrono::utc_time<std::chrono::microseconds>{946'688'523'123'456us}) == |
| 73 | SV("2000-01-01 01:01:41.123456" )); |
| 74 | |
| 75 | assert(stream_c_locale<CharT>(std::chrono::utc_time<std::chrono::milliseconds>{946'684'822'123ms}) == |
| 76 | SV("2000-01-01 00:00:00.123" )); |
| 77 | assert(stream_c_locale<CharT>(std::chrono::utc_seconds{1'234'567'890s}) == SV("2009-02-13 23:31:06" )); |
| 78 | assert(stream_c_locale<CharT>(std::chrono::utc_time<std::chrono::minutes>{20'576'131min}) == |
| 79 | SV("2009-02-13 23:30:36" )); |
| 80 | assert(stream_c_locale<CharT>(std::chrono::utc_time<std::chrono::hours>{342'935h}) == SV("2009-02-13 22:59:36" )); |
| 81 | |
| 82 | assert(stream_c_locale<CharT>(std::chrono::utc_time<std::chrono::duration<signed char, std::ratio<2, 1>>>{ |
| 83 | std::chrono::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1970-01-01 00:02:00" )); |
| 84 | assert(stream_c_locale<CharT>(std::chrono::utc_time<std::chrono::duration<short, std::ratio<1, 2>>>{ |
| 85 | std::chrono::duration<short, std::ratio<1, 2>>{3600}}) == SV("1970-01-01 00:30:00.0" )); |
| 86 | assert(stream_c_locale<CharT>(std::chrono::utc_time<std::chrono::duration<int, std::ratio<1, 4>>>{ |
| 87 | std::chrono::duration<int, std::ratio<1, 4>>{3600}}) == SV("1970-01-01 00:15:00.00" )); |
| 88 | assert(stream_c_locale<CharT>(std::chrono::utc_time<std::chrono::duration<long, std::ratio<1, 10>>>{ |
| 89 | std::chrono::duration<long, std::ratio<1, 10>>{36611}}) == SV("1970-01-01 01:01:01.1" )); |
| 90 | assert(stream_c_locale<CharT>(std::chrono::utc_time<std::chrono::duration<long long, std::ratio<1, 100>>>{ |
| 91 | std::chrono::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:06.10" )); |
| 92 | } |
| 93 | |
| 94 | template <class CharT> |
| 95 | static void test_fr_FR() { |
| 96 | using namespace std::literals::chrono_literals; |
| 97 | |
| 98 | assert(stream_fr_FR_locale<CharT>(std::chrono::utc_time<std::chrono::nanoseconds>{946'688'523'123'456'789ns}) == |
| 99 | SV("2000-01-01 01:01:41,123456789" )); |
| 100 | assert(stream_fr_FR_locale<CharT>(std::chrono::utc_time<std::chrono::microseconds>{946'688'523'123'456us}) == |
| 101 | SV("2000-01-01 01:01:41,123456" )); |
| 102 | |
| 103 | assert(stream_fr_FR_locale<CharT>(std::chrono::utc_time<std::chrono::milliseconds>{946'684'822'123ms}) == |
| 104 | SV("2000-01-01 00:00:00,123" )); |
| 105 | assert(stream_fr_FR_locale<CharT>(std::chrono::utc_seconds{1'234'567'890s}) == SV("2009-02-13 23:31:06" )); |
| 106 | assert(stream_fr_FR_locale<CharT>(std::chrono::utc_time<std::chrono::minutes>{20'576'131min}) == |
| 107 | SV("2009-02-13 23:30:36" )); |
| 108 | assert(stream_fr_FR_locale<CharT>(std::chrono::utc_time<std::chrono::hours>{342'935h}) == SV("2009-02-13 22:59:36" )); |
| 109 | |
| 110 | assert(stream_fr_FR_locale<CharT>(std::chrono::utc_time<std::chrono::duration<signed char, std::ratio<2, 1>>>{ |
| 111 | std::chrono::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1970-01-01 00:02:00" )); |
| 112 | assert(stream_fr_FR_locale<CharT>(std::chrono::utc_time<std::chrono::duration<short, std::ratio<1, 2>>>{ |
| 113 | std::chrono::duration<short, std::ratio<1, 2>>{3600}}) == SV("1970-01-01 00:30:00,0" )); |
| 114 | assert(stream_fr_FR_locale<CharT>(std::chrono::utc_time<std::chrono::duration<int, std::ratio<1, 4>>>{ |
| 115 | std::chrono::duration<int, std::ratio<1, 4>>{3600}}) == SV("1970-01-01 00:15:00,00" )); |
| 116 | assert(stream_fr_FR_locale<CharT>(std::chrono::utc_time<std::chrono::duration<long, std::ratio<1, 10>>>{ |
| 117 | std::chrono::duration<long, std::ratio<1, 10>>{36611}}) == SV("1970-01-01 01:01:01,1" )); |
| 118 | assert(stream_fr_FR_locale<CharT>(std::chrono::utc_time<std::chrono::duration<long long, std::ratio<1, 100>>>{ |
| 119 | std::chrono::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:06,10" )); |
| 120 | } |
| 121 | |
| 122 | template <class CharT> |
| 123 | static void test_ja_JP() { |
| 124 | using namespace std::literals::chrono_literals; |
| 125 | |
| 126 | assert(stream_ja_JP_locale<CharT>(std::chrono::utc_time<std::chrono::nanoseconds>{946'688'523'123'456'789ns}) == |
| 127 | SV("2000-01-01 01:01:41.123456789" )); |
| 128 | assert(stream_ja_JP_locale<CharT>(std::chrono::utc_time<std::chrono::microseconds>{946'688'523'123'456us}) == |
| 129 | SV("2000-01-01 01:01:41.123456" )); |
| 130 | |
| 131 | assert(stream_ja_JP_locale<CharT>(std::chrono::utc_time<std::chrono::milliseconds>{946'684'822'123ms}) == |
| 132 | SV("2000-01-01 00:00:00.123" )); |
| 133 | assert(stream_ja_JP_locale<CharT>(std::chrono::utc_seconds{1'234'567'890s}) == SV("2009-02-13 23:31:06" )); |
| 134 | assert(stream_ja_JP_locale<CharT>(std::chrono::utc_time<std::chrono::minutes>{20'576'131min}) == |
| 135 | SV("2009-02-13 23:30:36" )); |
| 136 | assert(stream_ja_JP_locale<CharT>(std::chrono::utc_time<std::chrono::hours>{342'935h}) == SV("2009-02-13 22:59:36" )); |
| 137 | |
| 138 | assert(stream_ja_JP_locale<CharT>(std::chrono::utc_time<std::chrono::duration<signed char, std::ratio<2, 1>>>{ |
| 139 | std::chrono::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1970-01-01 00:02:00" )); |
| 140 | assert(stream_ja_JP_locale<CharT>(std::chrono::utc_time<std::chrono::duration<short, std::ratio<1, 2>>>{ |
| 141 | std::chrono::duration<short, std::ratio<1, 2>>{3600}}) == SV("1970-01-01 00:30:00.0" )); |
| 142 | assert(stream_ja_JP_locale<CharT>(std::chrono::utc_time<std::chrono::duration<int, std::ratio<1, 4>>>{ |
| 143 | std::chrono::duration<int, std::ratio<1, 4>>{3600}}) == SV("1970-01-01 00:15:00.00" )); |
| 144 | assert(stream_ja_JP_locale<CharT>(std::chrono::utc_time<std::chrono::duration<long, std::ratio<1, 10>>>{ |
| 145 | std::chrono::duration<long, std::ratio<1, 10>>{36611}}) == SV("1970-01-01 01:01:01.1" )); |
| 146 | assert(stream_ja_JP_locale<CharT>(std::chrono::utc_time<std::chrono::duration<long long, std::ratio<1, 100>>>{ |
| 147 | std::chrono::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:06.10" )); |
| 148 | } |
| 149 | |
| 150 | template <class CharT> |
| 151 | static void test() { |
| 152 | test_c<CharT>(); |
| 153 | test_fr_FR<CharT>(); |
| 154 | test_ja_JP<CharT>(); |
| 155 | } |
| 156 | |
| 157 | int main(int, char**) { |
| 158 | test<char>(); |
| 159 | |
| 160 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 161 | test<wchar_t>(); |
| 162 | #endif |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |