<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3272: %I%p should parse/format duration since midnight</title>
<meta property="og:title" content="Issue 3272: %I%p should parse/format duration since midnight">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3272.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++20">C++20</a> status.</em></p>
<h3 id="3272"><a href="lwg-defects.html#3272">3272</a>. <code>%I%p</code> should parse/format <code>duration</code> since midnight</h3>
<p><b>Section:</b> 30.12 <a href="https://wg21.link/time.format">[time.format]</a>, 30.13 <a href="https://wg21.link/time.parse">[time.parse]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2019-09-02 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>0
</p>
<p><b>View other</b> <a href="lwg-index-open.html#time.format">active issues</a> in [time.format].</p>
<p><b>View all other</b> <a href="lwg-index.html#time.format">issues</a> in [time.format].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
It is clear how <code>"%I%p"</code> parses and formats time points. What is not clear is how these 
flags interact with durations. We should treat durations as "elapsed time since midnight". For example:
</p>
<blockquote>
<pre>
#include &lt;chrono&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

int main()
{
  using namespace std;
  using namespace std::chrono;
  <i>// Format</i>
  {
    <i>// format time_point with %I%p</i>
    cout &lt;&lt; format("{:%F %I%p}", sys_days{2019_y/August/10}+14h) &lt;&lt; '\n';
  }
  {
    <i>// format duration with %I%p</i>
    cout &lt;&lt; format("{:%I%p}", 14h) &lt;&lt; '\n';
  }

  <i>// Parse</i>
  {
    <i>// Parse %I%p as day-of-year combined with an hour into a time_point</i>
    istringstream in{"2019-08-10 2pm"};
    system_clock::time_point tp;
    in &gt;&gt; parse("%F %I%p", tp);
    cout &lt;&lt; tp &lt;&lt; '\n';
  }
  {
    <i>// Parse %I%p as number of hours into a duration</i>
    istringstream in{"2pm"};
    hours d;
    in &gt;&gt; parse("%I%p", d);
    cout &lt;&lt; d &lt;&lt; '\n';
  }
}
</pre>
</blockquote>
<p>
Output:
</p>
<blockquote>
<pre>
2019-08-10 02PM
02PM
2019-08-10 14:00:00.000000
14h
</pre>
</blockquote>

<p><i>[2019-10 Status set to 'Tentatively Ready' after reflector discussion]</i></p>



<p id="res-3272"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4830">N4830</a>.</p>

<ol>
<li><p>Modify 30.12 <a href="https://wg21.link/time.format">[time.format]</a> as indicated:</p>

<blockquote>
<p>
-3- Unless explicitly requested, the result of formatting a chrono type does not contain time zone abbreviation
and time zone offset information. If the information is available, the conversion specifiers <code>%Z</code> and 
<code>%z</code> will format this information (respectively). [<i>Note:</i> If the information is not available and 
a <code>%Z</code> or <code>%z</code> conversion specifier appears in the <i>chrono-format-spec</i>, an exception of 
type <code>format_error</code> is thrown, as described above. &mdash; <i>end note</i>]
<p/>
<ins>-?- If the type being formatted does not contain the information that the format flag needs, an exception 
of type <code>format_error</code> is thrown. [<i>Example:</i> A <code>duration</code> does not contain enough 
information to format as a <code>weekday</code> &mdash; <i>end example</i>]. However if a flag refers to a 
"time of day" (e.g. <code>%H</code>, <code>%I</code>, <code>%p</code>, etc.), then a specialization of <code>duration</code> 
is interpreted as the time of day elapsed since midnight.</ins>
</p>
</blockquote>

</li>

<li><p>Modify 30.13 <a href="https://wg21.link/time.parse">[time.parse]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> The modification of 30.13 <a href="https://wg21.link/time.parse">[time.parse]</a> p1 is intended to be non-conflictingly 
mergeable with the change suggested by LWG <a href="lwg-defects.html#3269" title="Parse manipulators do not specify the result of the extraction from stream (Status: C++20)">3269</a><sup><a href="https://cplusplus.github.io/LWG/issue3269" title="Latest snapshot">(i)</a></sup> and LWG <a href="lwg-closed.html#3271" title="Parsing functions should save and restore stream format state (Status: NAD)">3271</a><sup><a href="https://cplusplus.github.io/LWG/issue3271" title="Latest snapshot">(i)</a></sup> at the same paragraph.]
</p>
</blockquote>

<blockquote>
<p> 
-1- Each parse overload specified in this subclause calls <code>from_stream</code> unqualified, 
so as to enable argument dependent lookup (6.5.4 <a href="https://wg21.link/basic.lookup.argdep">[basic.lookup.argdep]</a>).
<ins>In the following paragraphs, let <code>is</code> denote an object of type 
<code>basic_istream&lt;charT, traits&gt;</code>, where <code>charT</code> and
<code>traits</code> are template parameters in that context.</ins>
<p/>
[&hellip;]
<p/>
-10- All <code>from_stream</code> overloads behave as unformatted input functions, except that they have an 
unspecified effect on the value returned by subsequent calls to <code>basic_istream&lt;&gt;::gcount()</code>.
Each overload takes a format string containing ordinary characters and flags which have special meaning. 
Each flag begins with a <code>%</code>. Some flags can be modified by <code>E</code> or <code>O</code>. During parsing 
each flag interprets characters as parts of date and time types according to Table [tab:time.parse.spec]. 
Some flags can be modified by a width parameter given as a positive decimal integer called out as 
<code><i>N</i></code> below which governs how many characters are parsed from the stream in interpreting the 
flag. All characters in the format string that are not represented in Table [tab:time.parse.spec], except 
for white space, are parsed unchanged from the stream. A white space character matches zero or more white
space characters in the input stream.
<p/>
<ins>-?- If the type being parsed can not represent the information that the format flag refers to, 
<code>is.setstate(ios_base::failbit)</code> is called. [<i>Example:</i> A <code>duration</code> cannot represent a 
<code>weekday</code> &mdash; <i>end example</i>]. However if a flag refers to a "time of day" (e.g. <code>%H</code>, 
<code>%I</code>, <code>%p</code>, etc.), then a specialization of <code>duration</code> is parsed as the time of day 
elapsed since midnight.</ins>
</p>
</blockquote>

</li>
</ol>





</body>
</html>
