165 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// REQUIRES: std-at-least-c++2010// UNSUPPORTED: no-filesystem, no-localization, no-tzdb11// 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// XFAIL: libcpp-has-no-experimental-tzdb17// XFAIL: availability-tzdb-missing18 19// REQUIRES: locale.fr_FR.UTF-820// REQUIRES: locale.ja_JP.UTF-821 22// <chrono>23 24// class tai_clock;25 26// template<class charT, class traits, class Duration>27// basic_ostream<charT, traits>&28// operator<<(basic_ostream<charT, traits>& os, const tai_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 macros37#include "test_macros.h"38 39#define SV(S) MAKE_STRING_VIEW(CharT, S)40 41template <class CharT, class Duration>42static std::basic_string<CharT> stream_c_locale(std::chrono::tai_time<Duration> time_point) {43 std::basic_stringstream<CharT> sstr;44 sstr << std::fixed << time_point;45 return sstr.str();46}47 48template <class CharT, class Duration>49static std::basic_string<CharT> stream_fr_FR_locale(std::chrono::tai_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 57template <class CharT, class Duration>58static std::basic_string<CharT> stream_ja_JP_locale(std::chrono::tai_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 66template <class CharT>67static void test_c() {68 using namespace std::literals::chrono_literals;69 namespace cr = std::chrono;70 71 assert(stream_c_locale<CharT>(cr::tai_time<cr::nanoseconds>{946'688'523'123'456'789ns}) ==72 SV("1988-01-01 01:02:03.123456789"));73 assert(stream_c_locale<CharT>(cr::tai_time<cr::microseconds>{946'688'523'123'456us}) ==74 SV("1988-01-01 01:02:03.123456"));75 76 assert(stream_c_locale<CharT>(cr::tai_time<cr::milliseconds>{946'684'822'123ms}) == SV("1988-01-01 00:00:22.123"));77 assert(stream_c_locale<CharT>(cr::tai_seconds{1'234'567'890s}) == SV("1997-02-13 23:31:30"));78 assert(stream_c_locale<CharT>(cr::tai_time<cr::minutes>{20'576'131min}) == SV("1997-02-13 23:31:00"));79 assert(stream_c_locale<CharT>(cr::tai_time<cr::hours>{342'935h}) == SV("1997-02-13 23:00:00"));80 81 assert(stream_c_locale<CharT>(cr::tai_time<cr::duration<signed char, std::ratio<2, 1>>>{82 cr::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1958-01-01 00:02:00"));83 assert(stream_c_locale<CharT>(cr::tai_time<cr::duration<short, std::ratio<1, 2>>>{84 cr::duration<short, std::ratio<1, 2>>{3600}}) == SV("1958-01-01 00:30:00.0"));85 assert(stream_c_locale<CharT>(cr::tai_time<cr::duration<int, std::ratio<1, 4>>>{86 cr::duration<int, std::ratio<1, 4>>{3600}}) == SV("1958-01-01 00:15:00.00"));87 assert(stream_c_locale<CharT>(cr::tai_time<cr::duration<long, std::ratio<1, 10>>>{88 cr::duration<long, std::ratio<1, 10>>{36611}}) == SV("1958-01-01 01:01:01.1"));89 assert(stream_c_locale<CharT>(cr::tai_time<cr::duration<long long, std::ratio<1, 100>>>{90 cr::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("1997-02-13 23:31:30.10"));91}92 93template <class CharT>94static void test_fr_FR() {95 using namespace std::literals::chrono_literals;96 namespace cr = std::chrono;97 98 assert(stream_fr_FR_locale<CharT>(cr::tai_time<cr::nanoseconds>{946'688'523'123'456'789ns}) ==99 SV("1988-01-01 01:02:03,123456789"));100 assert(stream_fr_FR_locale<CharT>(cr::tai_time<cr::microseconds>{946'688'523'123'456us}) ==101 SV("1988-01-01 01:02:03,123456"));102 103 assert(stream_fr_FR_locale<CharT>(cr::tai_time<cr::milliseconds>{946'684'822'123ms}) ==104 SV("1988-01-01 00:00:22,123"));105 assert(stream_fr_FR_locale<CharT>(cr::tai_seconds{1'234'567'890s}) == SV("1997-02-13 23:31:30"));106 assert(stream_fr_FR_locale<CharT>(cr::tai_time<cr::minutes>{20'576'131min}) == SV("1997-02-13 23:31:00"));107 assert(stream_fr_FR_locale<CharT>(cr::tai_time<cr::hours>{342'935h}) == SV("1997-02-13 23:00:00"));108 109 assert(stream_fr_FR_locale<CharT>(cr::tai_time<cr::duration<signed char, std::ratio<2, 1>>>{110 cr::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1958-01-01 00:02:00"));111 assert(stream_fr_FR_locale<CharT>(cr::tai_time<cr::duration<short, std::ratio<1, 2>>>{112 cr::duration<short, std::ratio<1, 2>>{3600}}) == SV("1958-01-01 00:30:00,0"));113 assert(stream_fr_FR_locale<CharT>(cr::tai_time<cr::duration<int, std::ratio<1, 4>>>{114 cr::duration<int, std::ratio<1, 4>>{3600}}) == SV("1958-01-01 00:15:00,00"));115 assert(stream_fr_FR_locale<CharT>(cr::tai_time<cr::duration<long, std::ratio<1, 10>>>{116 cr::duration<long, std::ratio<1, 10>>{36611}}) == SV("1958-01-01 01:01:01,1"));117 assert(stream_fr_FR_locale<CharT>(cr::tai_time<cr::duration<long long, std::ratio<1, 100>>>{118 cr::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("1997-02-13 23:31:30,10"));119}120 121template <class CharT>122static void test_ja_JP() {123 using namespace std::literals::chrono_literals;124 namespace cr = std::chrono;125 126 assert(stream_ja_JP_locale<CharT>(cr::tai_time<cr::nanoseconds>{946'688'523'123'456'789ns}) ==127 SV("1988-01-01 01:02:03.123456789"));128 assert(stream_ja_JP_locale<CharT>(cr::tai_time<cr::microseconds>{946'688'523'123'456us}) ==129 SV("1988-01-01 01:02:03.123456"));130 131 assert(stream_ja_JP_locale<CharT>(cr::tai_time<cr::milliseconds>{946'684'822'123ms}) ==132 SV("1988-01-01 00:00:22.123"));133 assert(stream_ja_JP_locale<CharT>(cr::tai_seconds{1'234'567'890s}) == SV("1997-02-13 23:31:30"));134 assert(stream_ja_JP_locale<CharT>(cr::tai_time<cr::minutes>{20'576'131min}) == SV("1997-02-13 23:31:00"));135 assert(stream_ja_JP_locale<CharT>(cr::tai_time<cr::hours>{342'935h}) == SV("1997-02-13 23:00:00"));136 137 assert(stream_ja_JP_locale<CharT>(cr::tai_time<cr::duration<signed char, std::ratio<2, 1>>>{138 cr::duration<signed char, std::ratio<2, 1>>{60}}) == SV("1958-01-01 00:02:00"));139 assert(stream_ja_JP_locale<CharT>(cr::tai_time<cr::duration<short, std::ratio<1, 2>>>{140 cr::duration<short, std::ratio<1, 2>>{3600}}) == SV("1958-01-01 00:30:00.0"));141 assert(stream_ja_JP_locale<CharT>(cr::tai_time<cr::duration<int, std::ratio<1, 4>>>{142 cr::duration<int, std::ratio<1, 4>>{3600}}) == SV("1958-01-01 00:15:00.00"));143 assert(stream_ja_JP_locale<CharT>(cr::tai_time<cr::duration<long, std::ratio<1, 10>>>{144 cr::duration<long, std::ratio<1, 10>>{36611}}) == SV("1958-01-01 01:01:01.1"));145 assert(stream_ja_JP_locale<CharT>(cr::tai_time<cr::duration<long long, std::ratio<1, 100>>>{146 cr::duration<long long, std::ratio<1, 100>>{12'345'678'9010}}) == SV("1997-02-13 23:31:30.10"));147}148 149template <class CharT>150static void test() {151 test_c<CharT>();152 test_fr_FR<CharT>();153 test_ja_JP<CharT>();154}155 156int main(int, char**) {157 test<char>();158 159#ifndef TEST_HAS_NO_WIDE_CHARACTERS160 test<wchar_t>();161#endif162 163 return 0;164}165