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:Rep
is an integral type whose integer conversion rank (6.8.6 [conv.rank]) is greater than or equal to that ofshort
, or a floating-point type.charT
ischar
orwchar_t
.Forms aInserts the durationbasic_string<charT, traits>
fromd.count()
usingto_string
ifcharT
ischar
, orto_wstring
ifcharT
iswchar_t
. Appends the units suffix described below to thebasic_string
. Inserts the resultingbasic_string
intoos
. [Note: This specification ensures that the result of this streaming operation will obey the width and alignment properties of the stream. — end note]d
onto the streamos
as 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_suffix
depends on the typePeriod::type
as follows:In the list above the use of
(3.1) — If
Period::type
isatto
,the suffixunits_suffix
is"as"
.(3.2) — Otherwise, if
Period::type
isfemto
,the suffixunits_suffix
is"fs"
.(3.3) — Otherwise, if
Period::type
ispico
,the suffixunits_suffix
is"ps"
.(3.4) — Otherwise, if
Period::type
isnano
,the suffixunits_suffix
is"ns"
.(3.5) — Otherwise, if
Period::type
ismicro
,the suffixunits_suffix
is"µs"
("\u00b5\u0073"
).(3.6) — Otherwise, if
Period::type
ismilli
,the suffixunits_suffix
is"ms"
.(3.7) — Otherwise, if
Period::type
iscenti
,the suffixunits_suffix
is"cs"
.(3.8) — Otherwise, if
Period::type
isdeci
,the suffixunits_suffix
is"ds"
.(3.9) — Otherwise, if
Period::type
isratio<1>
,the suffixunits_suffix
is"s"
.(3.10) — Otherwise, if
Period::type
isdeca
,the suffixunits_suffix
is"das"
.(3.11) — Otherwise, if
Period::type
ishecto
,the suffixunits_suffix
is"hs"
.(3.12) — Otherwise, if
Period::type
iskilo
,the suffixunits_suffix
is"ks"
.(3.13) — Otherwise, if
Period::type
ismega
,the suffixunits_suffix
is"Ms"
.(3.14) — Otherwise, if
Period::type
isgiga
,the suffixunits_suffix
is"Gs"
.(3.15) — Otherwise, if
Period::type
istera
,the suffixunits_suffix
is"Ts"
.(3.16) — Otherwise, if
Period::type
ispeta
,the suffixunits_suffix
is"Ps"
.(3.17) — Otherwise, if
Period::type
isexa
,the suffixunits_suffix
is"Es"
.(3.18) — Otherwise, if
Period::type
isratio<60>
,the suffixunits_suffix
is"min"
.(3.19) — Otherwise, if
Period::type
isratio<3600>
,the suffixunits_suffix
is"h"
.(3.20) — Otherwise, if
Period::type
isratio<86400>
,the suffixunits_suffix
is"d"
.(3.21) — Otherwise, if
Period::type::den == 1
,the suffixunits_suffix
is"[num]s"
.(3.22) — Otherwise,
the suffixunits_suffix
is"[num/den]s"
.num
andden
refer to the static data members ofPeriod::type
, which are converted to arrays ofcharT
using a decimal conversion with no leading zeroes. -4- IfPeriod::type
ismicro
, 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
.