305 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710// UNSUPPORTED: no-localization11// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME12 13// TODO FMT This test should not require std::to_chars(floating-point)14// XFAIL: availability-fp_to_chars-missing15 16// REQUIRES: locale.fr_FR.UTF-817// REQUIRES: locale.ja_JP.UTF-818 19// <chrono>20 21// class system_clock;22 23// template<class charT, class traits, class Duration>24// basic_ostream<charT, traits>&25// operator<<(basic_ostream<charT, traits>& os, const sys_time<Duration>& tp);26 27#include <chrono>28#include <cassert>29#include <ratio>30#include <sstream>31 32#include "make_string.h"33#include "platform_support.h" // locale name macros34#include "test_macros.h"35 36#define SV(S) MAKE_STRING_VIEW(CharT, S)37 38// Modeled after the system clock's usings, this make adapating the39// tests easier.40template <class Duration>41using file_time = std::chrono::time_point<std::chrono::file_clock, Duration>;42using file_seconds = file_time<std::chrono::seconds>;43using file_days = file_time<std::chrono::days>;44 45template <class CharT, class Duration>46static std::basic_string<CharT> stream_c_locale(file_time<Duration> time_point) {47 std::basic_stringstream<CharT> sstr;48 sstr << std::fixed << time_point;49 return sstr.str();50}51 52template <class CharT, class Duration>53static std::basic_string<CharT> stream_fr_FR_locale(file_time<Duration> time_point) {54 std::basic_stringstream<CharT> sstr;55 const std::locale locale(LOCALE_fr_FR_UTF_8);56 sstr.imbue(locale);57 sstr << std::fixed << time_point;58 return sstr.str();59}60 61template <class CharT, class Duration>62static std::basic_string<CharT> stream_ja_JP_locale(file_time<Duration> time_point) {63 std::basic_stringstream<CharT> sstr;64 const std::locale locale(LOCALE_ja_JP_UTF_8);65 sstr.imbue(locale);66 sstr << std::fixed << time_point;67 return sstr.str();68}69 70template <class CharT>71static void test_c() {72 using namespace std::literals::chrono_literals;73 74 assert(stream_c_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{-946'688'523'123'456'789ns}) ==75 SV("1940-01-01 22:57:56.876543211"));76 77 assert(stream_c_locale<CharT>(std::chrono::file_time<std::chrono::microseconds>{-946'688'523'123'456us}) ==78 SV("1940-01-01 22:57:56.876544"));79 80 assert(stream_c_locale<CharT>(std::chrono::file_time<std::chrono::milliseconds>{-946'688'523'123ms}) ==81 SV("1940-01-01 22:57:56.877"));82 83 assert(stream_c_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{-1000000000ns}) ==84 SV("1969-12-31 23:59:59.000000000"));85 86 assert(stream_c_locale<CharT>(std::chrono::file_time<std::chrono::microseconds>{-1000000us}) ==87 SV("1969-12-31 23:59:59.000000"));88 89 assert(stream_c_locale<CharT>(std::chrono::file_time<std::chrono::milliseconds>{-1000ms}) ==90 SV("1969-12-31 23:59:59.000"));91 92 assert(stream_c_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{-1ns}) ==93 SV("1969-12-31 23:59:59.999999999"));94 95 assert(stream_c_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{0ns}) ==96 SV("1970-01-01 00:00:00.000000000"));97 98 assert(stream_c_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{1ns}) ==99 SV("1970-01-01 00:00:00.000000001"));100 101 assert(stream_c_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{1000000000ns}) ==102 SV("1970-01-01 00:00:01.000000000"));103 104 assert(stream_c_locale<CharT>(std::chrono::file_time<std::chrono::microseconds>{1000000us}) ==105 SV("1970-01-01 00:00:01.000000"));106 107 assert(stream_c_locale<CharT>(std::chrono::file_time<std::chrono::milliseconds>{1000ms}) ==108 SV("1970-01-01 00:00:01.000"));109 110 assert(stream_c_locale<CharT>(file_time<std::chrono::nanoseconds>{946'688'523'123'456'789ns}) ==111 SV("2000-01-01 01:02:03.123456789"));112 113 assert(stream_c_locale<CharT>(file_time<std::chrono::microseconds>{946'688'523'123'456us}) ==114 SV("2000-01-01 01:02:03.123456"));115 116 assert(stream_c_locale<CharT>(file_time<std::chrono::milliseconds>{946'684'800'123ms}) ==117 SV("2000-01-01 00:00:00.123"));118 assert(stream_c_locale<CharT>(file_seconds{1'234'567'890s}) == SV("2009-02-13 23:31:30"));119 assert(stream_c_locale<CharT>(file_time<std::chrono::minutes>{20'576'131min}) == SV("2009-02-13 23:31:00"));120 assert(stream_c_locale<CharT>(file_time<std::chrono::hours>{342'935h}) == SV("2009-02-13 23:00:00"));121 assert(stream_c_locale<CharT>(file_days{std::chrono::days{14'288}}) == SV("2009-02-13 00:00:00"));122 assert(stream_c_locale<CharT>(file_time<std::chrono::weeks>{std::chrono::weeks{2041}}) == SV("2009-02-12 00:00:00"));123 124 assert(stream_c_locale<CharT>(file_time<std::chrono::duration<signed char, std::ratio<2, 1>>>{125 std::chrono::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1970-01-01 00:02:00"));126 assert(stream_c_locale<CharT>(file_time<std::chrono::duration<short, std::ratio<1, 2>>>{127 std::chrono::duration<short, std::ratio<1, 2>>{3600}}) == SV("1970-01-01 00:30:00.0"));128 assert(stream_c_locale<CharT>(file_time<std::chrono::duration<int, std::ratio<1, 4>>>{129 std::chrono::duration<int, std::ratio<1, 4>>{3600}}) == SV("1970-01-01 00:15:00.00"));130 assert(stream_c_locale<CharT>(file_time<std::chrono::duration<long, std::ratio<1, 10>>>{131 std::chrono::duration<long, std::ratio<1, 10>>{36611}}) == SV("1970-01-01 01:01:01.1"));132 assert(stream_c_locale<CharT>(file_time<std::chrono::duration<long long, std::ratio<1, 100>>>{133 std::chrono::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:30.10"));134 135 assert(stream_c_locale<CharT>(file_time<std::chrono::duration<float, std::ratio<1, 1>>>{136 std::chrono::duration<float, std::ratio<1, 1>>{123.456}}) == SV("1970-01-01 00:02:03"));137 assert(stream_c_locale<CharT>(file_time<std::chrono::duration<double, std::ratio<1, 10>>>{138 std::chrono::duration<double, std::ratio<1, 10>>{123.456}}) == SV("1970-01-01 00:00:12.3"));139 assert(stream_c_locale<CharT>(file_time<std::chrono::duration<long double, std::ratio<1, 100>>>{140 std::chrono::duration<long double, std::ratio<1, 100>>{123.456}}) == SV("1970-01-01 00:00:01.23"));141}142 143template <class CharT>144static void test_fr_FR() {145 using namespace std::literals::chrono_literals;146 147 assert(stream_fr_FR_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{-946'688'523'123'456'789ns}) ==148 SV("1940-01-01 22:57:56,876543211"));149 150 assert(stream_fr_FR_locale<CharT>(std::chrono::file_time<std::chrono::microseconds>{-946'688'523'123'456us}) ==151 SV("1940-01-01 22:57:56,876544"));152 153 assert(stream_fr_FR_locale<CharT>(std::chrono::file_time<std::chrono::milliseconds>{-946'688'523'123ms}) ==154 SV("1940-01-01 22:57:56,877"));155 156 assert(stream_fr_FR_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{-1000000000ns}) ==157 SV("1969-12-31 23:59:59,000000000"));158 159 assert(stream_fr_FR_locale<CharT>(std::chrono::file_time<std::chrono::microseconds>{-1000000us}) ==160 SV("1969-12-31 23:59:59,000000"));161 162 assert(stream_fr_FR_locale<CharT>(std::chrono::file_time<std::chrono::milliseconds>{-1000ms}) ==163 SV("1969-12-31 23:59:59,000"));164 165 assert(stream_fr_FR_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{-1ns}) ==166 SV("1969-12-31 23:59:59,999999999"));167 168 assert(stream_fr_FR_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{0ns}) ==169 SV("1970-01-01 00:00:00,000000000"));170 171 assert(stream_fr_FR_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{1ns}) ==172 SV("1970-01-01 00:00:00,000000001"));173 174 assert(stream_fr_FR_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{1000000000ns}) ==175 SV("1970-01-01 00:00:01,000000000"));176 177 assert(stream_fr_FR_locale<CharT>(std::chrono::file_time<std::chrono::microseconds>{1000000us}) ==178 SV("1970-01-01 00:00:01,000000"));179 180 assert(stream_fr_FR_locale<CharT>(std::chrono::file_time<std::chrono::milliseconds>{1000ms}) ==181 SV("1970-01-01 00:00:01,000"));182 183 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::nanoseconds>{946'688'523'123'456'789ns}) ==184 SV("2000-01-01 01:02:03,123456789"));185 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::microseconds>{946'688'523'123'456us}) ==186 SV("2000-01-01 01:02:03,123456"));187 188 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::milliseconds>{946'684'800'123ms}) ==189 SV("2000-01-01 00:00:00,123"));190 assert(stream_fr_FR_locale<CharT>(file_seconds{1'234'567'890s}) == SV("2009-02-13 23:31:30"));191 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::minutes>{20'576'131min}) == SV("2009-02-13 23:31:00"));192 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::hours>{342'935h}) == SV("2009-02-13 23:00:00"));193 assert(stream_fr_FR_locale<CharT>(file_days{std::chrono::days{14'288}}) == SV("2009-02-13 00:00:00"));194 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::weeks>{std::chrono::weeks{2041}}) ==195 SV("2009-02-12 00:00:00"));196 197 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<signed char, std::ratio<2, 1>>>{198 std::chrono::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1970-01-01 00:02:00"));199 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<short, std::ratio<1, 2>>>{200 std::chrono::duration<short, std::ratio<1, 2>>{3600}}) == SV("1970-01-01 00:30:00,0"));201 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<int, std::ratio<1, 4>>>{202 std::chrono::duration<int, std::ratio<1, 4>>{3600}}) == SV("1970-01-01 00:15:00,00"));203 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<long, std::ratio<1, 10>>>{204 std::chrono::duration<long, std::ratio<1, 10>>{36611}}) == SV("1970-01-01 01:01:01,1"));205 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<long long, std::ratio<1, 100>>>{206 std::chrono::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:30,10"));207 208 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<float, std::ratio<1, 1>>>{209 std::chrono::duration<float, std::ratio<1, 1>>{123.456}}) == SV("1970-01-01 00:02:03"));210 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<double, std::ratio<1, 10>>>{211 std::chrono::duration<double, std::ratio<1, 10>>{123.456}}) == SV("1970-01-01 00:00:12,3"));212 assert(stream_fr_FR_locale<CharT>(file_time<std::chrono::duration<long double, std::ratio<1, 100>>>{213 std::chrono::duration<long double, std::ratio<1, 100>>{123.456}}) == SV("1970-01-01 00:00:01,23"));214}215 216template <class CharT>217static void test_ja_JP() {218 using namespace std::literals::chrono_literals;219 220 assert(stream_ja_JP_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{-946'688'523'123'456'789ns}) ==221 SV("1940-01-01 22:57:56.876543211"));222 223 assert(stream_ja_JP_locale<CharT>(std::chrono::file_time<std::chrono::microseconds>{-946'688'523'123'456us}) ==224 SV("1940-01-01 22:57:56.876544"));225 226 assert(stream_ja_JP_locale<CharT>(std::chrono::file_time<std::chrono::milliseconds>{-946'688'523'123ms}) ==227 SV("1940-01-01 22:57:56.877"));228 229 assert(stream_ja_JP_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{-1000000000ns}) ==230 SV("1969-12-31 23:59:59.000000000"));231 232 assert(stream_ja_JP_locale<CharT>(std::chrono::file_time<std::chrono::microseconds>{-1000000us}) ==233 SV("1969-12-31 23:59:59.000000"));234 235 assert(stream_ja_JP_locale<CharT>(std::chrono::file_time<std::chrono::milliseconds>{-1000ms}) ==236 SV("1969-12-31 23:59:59.000"));237 238 assert(stream_ja_JP_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{-1ns}) ==239 SV("1969-12-31 23:59:59.999999999"));240 241 assert(stream_ja_JP_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{0ns}) ==242 SV("1970-01-01 00:00:00.000000000"));243 244 assert(stream_ja_JP_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{1ns}) ==245 SV("1970-01-01 00:00:00.000000001"));246 247 assert(stream_ja_JP_locale<CharT>(std::chrono::file_time<std::chrono::nanoseconds>{1000000000ns}) ==248 SV("1970-01-01 00:00:01.000000000"));249 250 assert(stream_ja_JP_locale<CharT>(std::chrono::file_time<std::chrono::microseconds>{1000000us}) ==251 SV("1970-01-01 00:00:01.000000"));252 253 assert(stream_ja_JP_locale<CharT>(std::chrono::file_time<std::chrono::milliseconds>{1000ms}) ==254 SV("1970-01-01 00:00:01.000"));255 256 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::nanoseconds>{946'688'523'123'456'789ns}) ==257 SV("2000-01-01 01:02:03.123456789"));258 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::microseconds>{946'688'523'123'456us}) ==259 SV("2000-01-01 01:02:03.123456"));260 261 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::milliseconds>{946'684'800'123ms}) ==262 SV("2000-01-01 00:00:00.123"));263 assert(stream_ja_JP_locale<CharT>(file_seconds{1'234'567'890s}) == SV("2009-02-13 23:31:30"));264 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::minutes>{20'576'131min}) == SV("2009-02-13 23:31:00"));265 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::hours>{342'935h}) == SV("2009-02-13 23:00:00"));266 assert(stream_ja_JP_locale<CharT>(file_days{std::chrono::days{14'288}}) == SV("2009-02-13 00:00:00"));267 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::weeks>{std::chrono::weeks{2041}}) ==268 SV("2009-02-12 00:00:00"));269 270 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<signed char, std::ratio<2, 1>>>{271 std::chrono::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1970-01-01 00:02:00"));272 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<short, std::ratio<1, 2>>>{273 std::chrono::duration<short, std::ratio<1, 2>>{3600}}) == SV("1970-01-01 00:30:00.0"));274 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<int, std::ratio<1, 4>>>{275 std::chrono::duration<int, std::ratio<1, 4>>{3600}}) == SV("1970-01-01 00:15:00.00"));276 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<long, std::ratio<1, 10>>>{277 std::chrono::duration<long, std::ratio<1, 10>>{36611}}) == SV("1970-01-01 01:01:01.1"));278 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<long long, std::ratio<1, 100>>>{279 std::chrono::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("2009-02-13 23:31:30.10"));280 281 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<float, std::ratio<1, 1>>>{282 std::chrono::duration<float, std::ratio<1, 1>>{123.456}}) == SV("1970-01-01 00:02:03"));283 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<double, std::ratio<1, 10>>>{284 std::chrono::duration<double, std::ratio<1, 10>>{123.456}}) == SV("1970-01-01 00:00:12.3"));285 assert(stream_ja_JP_locale<CharT>(file_time<std::chrono::duration<long double, std::ratio<1, 100>>>{286 std::chrono::duration<long double, std::ratio<1, 100>>{123.456}}) == SV("1970-01-01 00:00:01.23"));287}288 289template <class CharT>290static void test() {291 test_c<CharT>();292 test_fr_FR<CharT>();293 test_ja_JP<CharT>();294}295 296int main(int, char**) {297 test<char>();298 299#ifndef TEST_HAS_NO_WIDE_CHARACTERS300 test<wchar_t>();301#endif302 303 return 0;304}305