verify( -di, WIDEN("-40ms") );
res = std::format(WIDEN("{:>6}"), -di);
VERIFY( res == WIDEN(" -40ms") );
+}
+
+template<typename _CharT>
+void
+test_duration_fp()
+{
+ std::basic_string<_CharT> res;
const duration<double> df(11.22);
verify( df, WIDEN("11.22s") );
verify( -df, WIDEN("-11.22s") );
res = std::format(WIDEN("{:=^12}"), -df);
VERIFY( res == WIDEN("==-11.22s===") );
+
+ // precision accepted but ignored
+ res = std::format(WIDEN("{:.6}"), df);
+ VERIFY( res == WIDEN("11.22s") );
}
template<typename _CharT>
WIDEN("-14322:24:54.111222333") );
}
+template<typename _CharT>
+void
+test_hh_mm_ss_fp()
+{
+ duration<double> dt = 22h + 24min + 54s + 111222333ns;
+ // period controls number of subseconds
+ verify( hms<nanoseconds>(dt),
+ WIDEN("22:24:54.111222333") );
+ verify( hms<microseconds>(dt),
+ WIDEN("22:24:54.111222") );
+ verify( hms<milliseconds>(dt),
+ WIDEN("22:24:54.111") );
+ verify( hms<deciseconds>(dt),
+ WIDEN("22:24:54.1") );
+ verify( hms<seconds>(dt),
+ WIDEN("22:24:54") );
+ verify( hms<nanoseconds>(-dt),
+ WIDEN("-22:24:54.111222333") );
+ verify( hms<microseconds>(-dt),
+ WIDEN("-22:24:54.111222") );
+ verify( hms<milliseconds>(-dt),
+ WIDEN("-22:24:54.111") );
+ verify( hms<deciseconds>(-dt),
+ WIDEN("-22:24:54.1") );
+ verify( hms<seconds>(-dt),
+ WIDEN("-22:24:54") );
+
+ // but hour and minutes are preserved
+ verify( hms<minutes>(dt),
+ WIDEN("22:24:54") );
+ verify( hms<hours>(dt),
+ WIDEN("22:24:54") );
+ verify( hms<minutes>(-dt),
+ WIDEN("-22:24:54") );
+ verify( hms<hours>(-dt),
+ WIDEN("-22:24:54") );
+}
+
template<typename _CharT>
void
test_hh_mm_ss_cust()
test_durations()
{
test_duration<CharT>();
+ test_duration_fp<CharT>();
test_duration_cust<CharT>();
test_hh_mm_ss<CharT>();
+ test_hh_mm_ss_fp<CharT>();
test_hh_mm_ss_cust<CharT>();
}
--- /dev/null
+// { dg-do run { target c++20 } }
+
+#include <chrono>
+#include <ranges>
+#include <testsuite_hooks.h>
+
+using namespace std::chrono;
+
+#define WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
+#define WIDEN(S) WIDEN_(_CharT, S)
+
+template<typename _CharT>
+void
+test_empty()
+{
+ std::basic_string<_CharT> res;
+
+ const duration<double> d(33.111222);
+ res = std::format(WIDEN("{:.3}"), d);
+ VERIFY( res == WIDEN("33.1112s") );
+ res = std::format(WIDEN("{:.6}"), d);
+ VERIFY( res == WIDEN("33.1112s") );
+ res = std::format(WIDEN("{:.9}"), d);
+ VERIFY( res == WIDEN("33.1112s") );
+
+ // Uses ostream operator<<
+ const duration<double, std::nano> nd = d;
+ res = std::format(WIDEN("{:.3}"), nd);
+ VERIFY( res == WIDEN("3.31112e+10ns") );
+ res = std::format(WIDEN("{:.6}"), nd);
+ VERIFY( res == WIDEN("3.31112e+10ns") );
+ res = std::format(WIDEN("{:.9}"), nd);
+ VERIFY( res == WIDEN("3.31112e+10ns") );
+}
+
+template<typename _CharT>
+void
+test_Q()
+{
+ std::basic_string<_CharT> res;
+
+ const duration<double> d(7.111222);
+ res = std::format(WIDEN("{:.3%Q}"), d);
+ VERIFY( res == WIDEN("7.111222") );
+ res = std::format(WIDEN("{:.6%Q}"), d);
+ VERIFY( res == WIDEN("7.111222") );
+ res = std::format(WIDEN("{:.9%Q}"), d);
+ VERIFY( res == WIDEN("7.111222") );
+
+ const duration<double, std::nano> nd = d;
+ res = std::format(WIDEN("{:.3%Q}"), nd);
+ VERIFY( res == WIDEN("7111222000") );
+ res = std::format(WIDEN("{:.6%Q}"), nd);
+ VERIFY( res == WIDEN("7111222000") );
+ res = std::format(WIDEN("{:.9%Q}"), nd);
+ VERIFY( res == WIDEN("7111222000") );
+}
+
+template<typename _CharT>
+void
+test_S()
+{
+ std::basic_string<_CharT> res;
+
+ // Precision is ignored, but period affects output
+ const duration<double> d(5.111222);
+ res = std::format(WIDEN("{:.3%S}"), d);
+ VERIFY( res == WIDEN("05") );
+ res = std::format(WIDEN("{:.6%S}"), d);
+ VERIFY( res == WIDEN("05") );
+ res = std::format(WIDEN("{:.9%S}"), d);
+ VERIFY( res == WIDEN("05") );
+
+ const duration<double, std::milli> md = d;
+ res = std::format(WIDEN("{:.3%S}"), md);
+ VERIFY( res == WIDEN("05.111") );
+ res = std::format(WIDEN("{:.6%S}"), md);
+ VERIFY( res == WIDEN("05.111") );
+ res = std::format(WIDEN("{:.9%S}"), md);
+ VERIFY( res == WIDEN("05.111") );
+
+ const duration<double, std::nano> nd = d;
+ res = std::format(WIDEN("{:.3%S}"), nd);
+ VERIFY( res == WIDEN("05.111222000") );
+ res = std::format(WIDEN("{:.6%S}"), nd);
+ VERIFY( res == WIDEN("05.111222000") );
+ res = std::format(WIDEN("{:.9%S}"), nd);
+ VERIFY( res == WIDEN("05.111222000") );
+}
+
+template<typename CharT>
+void
+test_all()
+{
+ test_empty<CharT>();
+ test_Q<CharT>();
+ test_S<CharT>();
+}
+
+int main()
+{
+ test_all<char>();
+
+#ifdef _GLIBCXX_USE_WCHAR_T
+ test_all<wchar_t>();
+#endif // _GLIBCXX_USE_WCHAR_T
+}