This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++20 status.
operator<< for floating-point durationsSection: 30.5.11 [time.duration.io] Status: C++20 Submitter: United States Opened: 2019-11-05 Last modified: 2021-02-25
Priority: 0
View all other issues in [time.duration.io].
View all issues with C++20 status.
Discussion:
Addresses US 334
operator<< for floating-point durations always produces output with six digits
after the decimal point, and doesn't use the stream's locale either.
Proposed change:
Rewrite the specification to not rely onto_string() for floating-point formatting.
[2019-11 Status to Ready during Wednesday morning issue processing in Belfast.]
Proposed resolution:
This wording is relative to N4835.
Modify 30.5.11 [time.duration.io] as indicated:
template<class charT, class traits, class Rep, class Period> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const duration<Rep, Period>& d);-2- Effects:
-1- Requires:Repis an integral type whose integer conversion rank (6.9.6 [conv.rank]) is greater than or equal to that ofshort, or a floating-point type.charTischarorwchar_t.Forms aInserts the durationbasic_string<charT, traits>fromd.count()usingto_stringifcharTischar, orto_wstringifcharTiswchar_t. Appends the units suffix described below to thebasic_string. Inserts the resultingbasic_stringintoos. [Note: This specification ensures that the result of this streaming operation will obey the width and alignment properties of the stream. — end note]donto the streamosas if it were implemented as follows:-3-basic_ostringstream<charT, traits> s; s.flags(os.flags()); s.imbue(os.getloc()); s.precision(os.precision()); s << d.count() << units_suffix; return os << s.str();The units suffixunits_suffixdepends on the typePeriod::typeas follows:In the list above the use of
(3.1) — If
Period::typeisatto,the suffixunits_suffixis"as".(3.2) — Otherwise, if
Period::typeisfemto,the suffixunits_suffixis"fs".(3.3) — Otherwise, if
Period::typeispico,the suffixunits_suffixis"ps".(3.4) — Otherwise, if
Period::typeisnano,the suffixunits_suffixis"ns".(3.5) — Otherwise, if
Period::typeismicro,the suffixunits_suffixis"µs"("\u00b5\u0073").(3.6) — Otherwise, if
Period::typeismilli,the suffixunits_suffixis"ms".(3.7) — Otherwise, if
Period::typeiscenti,the suffixunits_suffixis"cs".(3.8) — Otherwise, if
Period::typeisdeci,the suffixunits_suffixis"ds".(3.9) — Otherwise, if
Period::typeisratio<1>,the suffixunits_suffixis"s".(3.10) — Otherwise, if
Period::typeisdeca,the suffixunits_suffixis"das".(3.11) — Otherwise, if
Period::typeishecto,the suffixunits_suffixis"hs".(3.12) — Otherwise, if
Period::typeiskilo,the suffixunits_suffixis"ks".(3.13) — Otherwise, if
Period::typeismega,the suffixunits_suffixis"Ms".(3.14) — Otherwise, if
Period::typeisgiga,the suffixunits_suffixis"Gs".(3.15) — Otherwise, if
Period::typeistera,the suffixunits_suffixis"Ts".(3.16) — Otherwise, if
Period::typeispeta,the suffixunits_suffixis"Ps".(3.17) — Otherwise, if
Period::typeisexa,the suffixunits_suffixis"Es".(3.18) — Otherwise, if
Period::typeisratio<60>,the suffixunits_suffixis"min".(3.19) — Otherwise, if
Period::typeisratio<3600>,the suffixunits_suffixis"h".(3.20) — Otherwise, if
Period::typeisratio<86400>,the suffixunits_suffixis"d".(3.21) — Otherwise, if
Period::type::den == 1,the suffixunits_suffixis"[num]s".(3.22) — Otherwise,
the suffixunits_suffixis"[num/den]s".numanddenrefer to the static data members ofPeriod::type, which are converted to arrays ofcharTusing a decimal conversion with no leading zeroes. -4- IfPeriod::typeismicro, but the character U+00B5 cannot be represented in the encoding used forcharT,the unit suffixunits_suffix"us"is used instead of"µs". -5- Returns:os.