Unify and simplify fractional decimal seconds formatting

* Many of the ideas and some of the code herein is credited to
  Adrian Colomitchi

* Decouple fractional decimal seconds formatting from time_of_day
  formatting so that it can be more easily used elsewhere in the
  future.

* Include super-second durations such as nanocenturies and
  microfortnights in the class of durations that will get formatted
  with fractional decimal seconds.

* If a duration is exactly representable with fractional decimal
  seconds not exceeding 18 decimal fractional digits, format it
  exactly.  Otherwise format it to 6 fractional decimal digits
  (microseconds precision).  The number 18 is chosen as this is the
  limit of std::ratio using 64 bits (i.e. atto).

* The above bullet implies that durations with ratio<1, 4> will now
  be formatted with 2 fractional decimal digits instead of 1.
  ratio<1, 8> will be formatted with 3, and ratio<1, 3> with 6.

* Unify the implementation into one C++11 implementation that works
  equally well with C++14.

* Drive-by fix a couple formatting bugs dealing with negative
  durations.

* Deprecate the make_time functions taking unsigned md by removing
  their documentation.  Also deprecate the corresponding time_of_day
  constructors taking unsigned md.

* This change paves the way for future formatting improvements.
This commit is contained in:
Howard Hinnant
2016-11-24 19:43:35 -05:00
parent d99e39f5d8
commit 5eb10b747f

187
date.html
View File

@@ -26,7 +26,7 @@
<br/>
<br/>
<a href="mailto:howard.hinnant@gmail.com">Howard E. Hinnant</a><br/>
2016-09-13<br/>
2016-11-24<br/>
</address>
<hr/>
<h1 align=center><code>date</code></h1>
@@ -1573,7 +1573,7 @@ and predictable.
<tr><td>&nbsp;</td><td><a href="#_40"><code>constexpr year_month_weekday_last operator/(const month_weekday_last&amp; mwdl, int y) noexcept;</code></a></td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td><code>time_of_day</code><br/> factory functions</td><td>&nbsp;</td></tr>
<tr><td><code>time_of_day</code><br/> factory function</td><td>&nbsp;</td></tr>
<tr><td>&nbsp;</td><td><a href="#_41">
<pre>
template &lt;class Rep, class Period&gt;
@@ -1582,38 +1582,6 @@ time_of_day&lt;std::chrono::duration&lt;Rep, Period&gt;&gt;
make_time(std::chrono::duration&lt;Rep, Period&gt; d) noexcept;
</pre></a></td></tr>
<tr><td>&nbsp;</td><td><a href="#_42">
<pre>
constexpr
time_of_day&lt;std::chrono::hours&gt;
make_time(std::chrono::hours h, unsigned md) noexcept;
</pre></a></td></tr>
<tr><td>&nbsp;</td><td><a href="#_43">
<pre>
constexpr
time_of_day&lt;std::chrono::minutes&gt;
make_time(std::chrono::hours h, std::chrono::minutes m, unsigned md) noexcept;
</pre></a></td></tr>
<tr><td>&nbsp;</td><td><a href="#_44">
<pre>
constexpr
time_of_day&lt;std::chrono::seconds&gt;
make_time(std::chrono::hours h, std::chrono::minutes m, std::chrono::seconds s,
unsigned md) noexcept;
</pre></a></td></tr>
<tr><td>&nbsp;</td><td><a href="#_45">
<pre>
template &lt;class Rep, class Period,
class = std::enable_if_t&lt;std::ratio_less&lt;Period, std::ratio&lt;1&gt;&gt;::value&gt;&gt;
constexpr
time_of_day&lt;std::chrono::duration&lt;Rep, Period&gt;&gt;
make_time(std::chrono::hours h, std::chrono::minutes m, std::chrono::seconds s,
std::chrono::duration&lt;Rep, Period&gt; sub_s, unsigned md) noexcept
</pre></a></td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>convenience<br/>streaming operators</td><td>&nbsp;</td></tr>
@@ -6332,8 +6300,8 @@ This specialization handles hours:minutes:seconds since midnight.
time_of_day&lt;std::chrono::duration&lt;Rep, Period&gt;&gt;
</pre>
<blockquote><p>
This specialization is restricted to <code>Period</code>s that are shorter than 1
second. Typical uses are with milliseconds, microseconds and nanoseconds. This
This specialization is restricted to <code>Period</code>s that fractions of a second
to represent. Typical uses are with milliseconds, microseconds and nanoseconds. This
specialization handles hours:minute:seconds.fractional_seconds since midnight.
</p></blockquote>
</li>
@@ -6359,7 +6327,6 @@ public:
using precision = std::chrono::hours;
constexpr explicit time_of_day(std::chrono::hours since_midnight) noexcept;
constexpr time_of_day(std::chrono::hours h, unsigned md) noexcept;
constexpr std::chrono::hours hours() const noexcept;
constexpr unsigned mode() const noexcept;
@@ -6387,24 +6354,6 @@ corresponding to <code>since_midnight</code> hours after 00:00:00.
</p>
</blockquote>
<pre>
constexpr time_of_day&lt;std::chrono::hours&gt;::time_of_day(std::chrono::hours h, unsigned md) noexcept;
</pre>
<blockquote>
<p>
<i>Preconditions:</i> <code>md == am</code> or <code>md == pm</code>.
</p>
<p>
<i>Effects:</i> Constructs an object of type <code>time_of_day</code> in 12-hour format
corresponding to <code>h</code> hours after 00:00:00.
</p>
<p>
<i>Postconditions:</i> <code>hours()</code> returns <code>h</code>, and <code>mode()</code>
returns <code>md</code>.
</p>
</blockquote>
<pre>
constexpr std::chrono::hours time_of_day&lt;std::chrono::hours&gt;::hours() const noexcept;
</pre>
@@ -6505,8 +6454,6 @@ public:
using precision = std::chrono::minutes;
constexpr explicit time_of_day(std::chrono::minutes since_midnight) noexcept;
constexpr time_of_day(std::chrono::hours h, std::chrono::minutes m,
unsigned md) noexcept;
constexpr std::chrono::hours hours() const noexcept;
constexpr std::chrono::minutes minutes() const noexcept;
@@ -6537,25 +6484,6 @@ integral number of minutes <code>since_midnight</code> is after (00:00:00 +
</p>
</blockquote>
<pre>
constexpr time_of_day&lt;std::chrono::minutes&gt;::time_of_day(std::chrono::hours h, std::chrono::minutes m,
unsigned md) noexcept;
</pre>
<blockquote>
<p>
<i>Preconditions:</i> <code>md == am</code> or <code>md == pm</code>.
</p>
<p>
<i>Effects:</i> Constructs an object of type <code>time_of_day</code> in 12-hour format
corresponding to <code>h</code> hours and <code>m</code> minutes after 00:00:00.
</p>
<p>
<i>Postconditions:</i> <code>hours()</code> returns <code>h</code>, <code>minutes()</code>
returns <code>m</code>, and <code>mode()</code> returns <code>md</code>.
</p>
</blockquote>
<pre>
constexpr std::chrono::hours time_of_day&lt;std::chrono::minutes&gt;::hours() const noexcept;
</pre>
@@ -6666,8 +6594,6 @@ public:
using precision = std::chrono::seconds;
constexpr explicit time_of_day(std::chrono::seconds since_midnight) noexcept;
constexpr time_of_day(std::chrono::hours h, std::chrono::minutes m,
std::chrono::seconds s, unsigned md) noexcept;
constexpr std::chrono::hours hours() const noexcept;
constexpr std::chrono::minutes minutes() const noexcept;
@@ -6701,27 +6627,6 @@ integral number of minutes <code>since_midnight</code> is after (00:00:00 +
</p>
</blockquote>
<pre>
constexpr time_of_day&lt;std::chrono::seconds&gt;::time_of_day(std::chrono::hours h, std::chrono::minutes m,
std::chrono::seconds s, unsigned md) noexcept;
</pre>
<blockquote>
<p>
<i>Preconditions:</i> <code>md == am</code> or <code>md == pm</code>.
</p>
<p>
<i>Effects:</i> Constructs an object of type <code>time_of_day</code> in 12-hour format
corresponding to <code>h</code> hours, <code>m</code> minutes, and <code>s</code> seconds
after 00:00:00.
</p>
<p>
<i>Postconditions:</i> <code>hours()</code> returns <code>h</code>. <code>minutes()</code>
returns <code>m</code>. <code>seconds()</code> returns <code>s</code>.
<code>mode()</code> returns <code>md</code>.
</p>
</blockquote>
<pre>
constexpr std::chrono::hours time_of_day&lt;std::chrono::seconds&gt;::hours() const noexcept;
</pre>
@@ -6839,11 +6744,9 @@ that no leading zero is output for hours less than 10.
time_of_day&lt;std::chrono::duration&lt;Rep, Period&gt;&gt;
{
public:
using precision = std::chrono::duration&lt;Rep, Period&gt;;
using precision = <i>The decimal-based duration used to format</i>
constexpr explicit time_of_day(precision since_midnight) noexcept;
constexpr time_of_day(std::chrono::hours h, std::chrono::minutes m,
std::chrono::seconds s, precision sub_s, unsigned md) noexcept;
constexpr explicit time_of_day(std::chrono::duration&lt;Rep, Period&gt; since_midnight) noexcept;
constexpr std::chrono::hours hours() const noexcept;
constexpr std::chrono::minutes minutes() const noexcept;
@@ -6864,7 +6767,8 @@ This specialization shall not exist unless <code>Period &lt; 1s</code>.
</p>
<pre>
constexpr explicit time_of_day&lt;std::chrono::duration&lt;Rep, Period&gt;&gt;::time_of_day(precision since_midnight) noexcept;
constexpr explicit
time_of_day&lt;std::chrono::duration&lt;Rep, Period&gt;&gt;::time_of_day(std::chrono::duration&lt;Rep, Period&gt; since_midnight) noexcept;
</pre>
<blockquote>
@@ -6885,29 +6789,6 @@ returns <code>0</code>.
</p>
</blockquote>
<pre>
constexpr time_of_day&lt;std::chrono::duration&lt;Rep, Period&gt;&gt;::time_of_day(std::chrono::hours h, std::chrono::minutes m,
std::chrono::seconds s, precision sub_s,
unsigned md) noexcept;
</pre>
<blockquote>
<p>
<i>Preconditions:</i> <code>md == am</code> or <code>md == pm</code>.
</p>
<p>
<i>Effects:</i> Constructs an object of type <code>time_of_day</code> in 12-hour format
corresponding to <code>h</code> hours, <code>m</code> minutes, and <code>s + sub_s</code>
seconds after 00:00:00.
</p>
<p>
<i>Postconditions:</i> <code>hours()</code> returns <code>h</code>. <code>minutes()</code>
returns <code>m</code>. <code>seconds()</code> returns <code>s</code>.
<code>subseconds()</code> returns <code>sub_s</code>. <code>mode()</code> returns
<code>md</code>.
</p>
</blockquote>
<pre>
constexpr std::chrono::hours time_of_day&lt;std::chrono::duration&lt;Rep, Period&gt;&gt;::hours() const noexcept;
</pre>
@@ -7053,58 +6934,6 @@ make_time(std::chrono::duration&lt;Rep, Period&gt; d) noexcept;
</p>
</blockquote>
<a name="_42"></a><pre>
constexpr
time_of_day&lt;std::chrono::hours&gt;
make_time(std::chrono::hours h, unsigned md) noexcept;
</pre>
<blockquote>
<p>
<i>Returns:</i> <code>time_of_day&lt;std::chrono::hours&gt;(h, md)</code>.
</p>
</blockquote>
<a name="_43"></a><pre>
constexpr
time_of_day&lt;std::chrono::minutes&gt;
make_time(std::chrono::hours h, std::chrono::minutes m, unsigned md) noexcept;
</pre>
<blockquote>
<p>
<i>Returns:</i> <code>time_of_day&lt;std::chrono::minutes&gt;(h, m, md)</code>.
</p>
</blockquote>
<a name="_44"></a><pre>
constexpr
time_of_day&lt;std::chrono::seconds&gt;
make_time(std::chrono::hours h, std::chrono::minutes m, std::chrono::seconds s,
unsigned md) noexcept;
</pre>
<blockquote>
<p>
<i>Returns:</i> <code>time_of_day&lt;std::chrono::seconds&gt;(h, m, s, md)</code>.
</p>
</blockquote>
<a name="_45"></a><pre>
template &lt;class Rep, class Period,
class = std::enable_if_t&lt;std::ratio_less&lt;Period, std::ratio&lt;1&gt;&gt;::value&gt;&gt;
constexpr
time_of_day&lt;std::chrono::duration&lt;Rep, Period&gt;&gt;
make_time(std::chrono::hours h, std::chrono::minutes m, std::chrono::seconds s,
std::chrono::duration&lt;Rep, Period&gt; sub_s, unsigned md) noexcept
</pre>
<blockquote>
<p>
<i>Returns:</i> <code>time_of_day&lt;std::chrono::duration&lt;Rep, Period&gt;&gt;(h, m, s, sub_s, md)</code>.
</p>
</blockquote>
</blockquote>
<a name="_46"></a><pre>