<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3536: Should chrono::from_stream() assign zero to duration for failure?</title>
<meta property="og:title" content="Issue 3536: Should chrono::from_stream() assign zero to duration for failure?">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3536.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++23">C++23</a> status.</em></p>
<h3 id="3536"><a href="lwg-defects.html#3536">3536</a>. Should <code>chrono::from_stream()</code> assign zero to <code>duration</code> for failure?</h3>
<p><b>Section:</b> 30.5.11 <a href="https://wg21.link/time.duration.io">[time.duration.io]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Matt Stephanson <b>Opened:</b> 2021-03-19 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#time.duration.io">issues</a> in [time.duration.io].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The <code>duration</code> specialization of <code>from_stream</code> says in <a href="https://wg21.link/n4878">N4878</a> 
30.5.11 <a href="https://wg21.link/time.duration.io">[time.duration.io]</a>/3:
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;"><p>
If the parse parses everything specified by the parsing format flags without error, and yet none of the flags impacts a duration, 
<code>d</code> will be assigned a zero value.
</p></blockquote>
<p>
This is in contrast to the other specializations that say, for example, 30.8.3.3 <a href="https://wg21.link/time.cal.day.nonmembers">[time.cal.day.nonmembers]</a>/8:
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;"><p>
If the parse fails to decode a valid day, <code>is.setstate(ios_base::failbit)</code> is called and <code>d</code> 
is not modified.
</p></blockquote>
<p>
The wording ("none of the flags impacts a duration" vs. "parse fails to decode a valid [meow]") and semantics 
("assigned a zero value" vs. "not modified") are different, and it's not clear why that should be so. It also 
leaves unspecified what should be done in case of a parse failure, for example parsing <code>"%j"</code> from a 
stream containing <code>"meow"</code>. 30.13 <a href="https://wg21.link/time.parse">[time.parse]</a>/12 says that <code>failbit</code> should be set, 
but neither it nor 30.5.11 <a href="https://wg21.link/time.duration.io">[time.duration.io]</a>/3 mention the <code>duration</code> result if parsing fails.
<p/>
This has been discussed at the Microsoft STL project, where Howard Hinnant, coauthor of 
<a href="https://wg21.link/p0355R7">P0355R7</a> that added these functions, 
<a href="https://github.com/microsoft/STL/issues/1740#issuecomment-802414649">commented</a>:
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;">
<p>
This looks like a bug in the standard to me, due to two errors on my part.
<p/>
I believe that the standard should clearly say that if <code>failbit</code> is set, the parsed variable 
(<code>duration</code>, <code>time_point</code>, whatever) is not modified. I mistakenly believed that the 
definition of unformatted input function covered this behavior. But after review, I don't believe it 
does. Instead each extraction operator seems to say this separately.
<p/>
I also at first did not have my example implementation coded to leave the duration unchanged. So that's how 
the wording got in in the first place. Here's the commit where I fixed my implementation: 
<a href="https://github.com/HowardHinnant/date/commit/d53db7a1cb61dc2651374e5b9660ed07e6d28358">HowardHinnant/date@d53db7a</a>. 
And I failed to propagate that fix into the proposal/standard.
</p>
</blockquote>
<p>
It would be clearer and simpler for users if the <code>from_stream</code> specializations were consistent in wording 
and behavior.
<p/>
Thanks to Stephan T. Lavavej, Miya Natsuhara, and Howard Hinnant for valuable investigation and discussion 
of this issue.
</p>

<p><i>[2021-04-20; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after eight votes in favour during reflector poll.
</p>

<p><i>[2021-06-07 Approved at June 2021 virtual plenary. Status changed: Voting &rarr; WP.]</i></p>



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

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

<blockquote>
<pre>
template&lt;class charT, class traits, class Rep, class Period, class Alloc = allocator&lt;charT&gt;&gt;
  basic_istream&lt;charT, traits&gt;&amp;
    from_stream(basic_istream&lt;charT, traits&gt;&amp; is, const charT* fmt,
                duration&lt;Rep, Period&gt;&amp; d,
                basic_string&lt;charT, traits, Alloc&gt;* abbrev = nullptr,
                minutes* offset = nullptr);
</pre>
<blockquote>
<p>
-3- <i>Effects:</i> Attempts to parse the input stream <code>is</code> into the duration <code>d</code> using 
the format flags given in the NTCTS <code>fmt</code> as specified in 30.13 <a href="https://wg21.link/time.parse">[time.parse]</a>. <del>If the parse 
parses everything specified by the parsing format flags without error, and yet none of the flags impacts 
a duration, <code>d</code> will be assigned a zero value</del><ins>If the parse fails to decode a valid duration, 
<code>is.setstate(ios_base::failbit)</code> is called and <code>d</code> is not modified</ins>. If <code>%Z</code> is 
used and successfully parsed, that value will be assigned to <code>*abbrev</code> if <code>abbrev</code> is non-null. 
If <code>%z</code> (or a modified variant) is used and successfully parsed, that value will be assigned to 
<code>*offset</code> if <code>offset</code> is non-null.
</p>
</blockquote>
</blockquote>
</li>

</ol>






</body>
</html>
