<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 934: duration is missing operator%</title>
<meta property="og:title" content="Issue 934: duration is missing operator%">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue934.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++11">C++11</a> status.</em></p>
<h3 id="934"><a href="lwg-defects.html#934">934</a>. <code>duration</code> is missing <code>operator%</code></h3>
<p><b>Section:</b> 30.5 <a href="https://wg21.link/time.duration">[time.duration]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Terry Golubiewski <b>Opened:</b> 2008-11-30 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#time.duration">issues</a> in [time.duration].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>

<p><b>Addresses US 81</b></p>

<p>
<code>duration</code> is missing <code>operator%</code>.  This operator is convenient
for computing where in a time frame a given <code>duration</code> lies.  A
motivating example is converting a <code>duration</code> into a "broken-down"
time duration such as hours::minutes::seconds:
</p>

<blockquote><pre>
class ClockTime
{
    typedef std::chrono::hours hours;
    typedef std::chrono::minutes minutes;
    typedef std::chrono::seconds seconds;
public:
    hours hours_;
    minutes minutes_;
    seconds seconds_;

    template &lt;class Rep, class Period&gt;
      explicit ClockTime(const std::chrono::duration&lt;Rep, Period&gt;&amp; d)
        : hours_  (std::chrono::duration_cast&lt;hours&gt;  (d)),
          minutes_(std::chrono::duration_cast&lt;minutes&gt;(d % hours(1))),
          seconds_(std::chrono::duration_cast&lt;seconds&gt;(d % minutes(1)))
          {}
};
</pre></blockquote>

<p><i>[
Summit:
]</i></p>


<blockquote><p>
Agree except that there is a typo in the proposed resolution. The member
operators should be <code>operator%=</code>.
</p></blockquote>

<p><i>[
Batavia (2009-05):
]</i></p>

<blockquote><p>
We agree with the proposed resolution.
Move to Tentatively Ready.
</p></blockquote>

<p><i>[
2009-07 Frankfurt
]</i></p>


<blockquote><p>
Moved from Tentatively Ready to Open only because the wording needs to be
improved for enable_if type constraining, possibly following Robert's
formula.
</p></blockquote>

<p><i>[
2009-07 Frankfurt:
]</i></p>


<blockquote>
<p>
Howard to open a separate issue (<a href="lwg-defects.html#1177" title="Improve &quot;diagnostic required&quot; wording (Status: C++11)">1177</a><sup><a href="https://cplusplus.github.io/LWG/issue1177" title="Latest snapshot">(i)</a></sup>) to handle the removal of member
functions from overload sets, provide wording, and possibly demonstrate
how this can be implemented using enable_if (see <a href="lwg-defects.html#947" title="duration arithmetic: contradictory requirements (Status: Resolved)">947</a><sup><a href="https://cplusplus.github.io/LWG/issue947" title="Latest snapshot">(i)</a></sup>).
</p>
<p>
Move to Ready.
</p>
</blockquote>



<p id="res-934"><b>Proposed resolution:</b></p>
<p>
Add to the synopsis in 30 <a href="https://wg21.link/time">[time]</a>:
</p>

<blockquote><pre>
template &lt;class Rep1, class Period, class Rep2&gt;
  duration&lt;typename common_type&lt;Rep1, Rep2&gt;::type, Period&gt;
  operator%(const duration&lt;Rep1, Period&gt;&amp; d, const Rep2&amp; s);
template &lt;class Rep1, class Period1, class Rep2, class Period2&gt;
  typename common_type&lt;duration&lt;Rep1, Period1&gt;, duration&lt;Rep2, Period2&gt;&gt;::type
  operator%(const duration&lt;Rep1, Period1&gt;&amp; lhs, const duration&lt;Rep2, Period2&gt;&amp; rhs);
</pre></blockquote>

<p>
Add to the synopsis of <code>duration</code> in 30.5 <a href="https://wg21.link/time.duration">[time.duration]</a>:
</p>

<blockquote><pre>
template &lt;class Rep, class Period = ratio&lt;1&gt;&gt;
class duration {
public:
  ...
  <ins>duration&amp; operator%=(const rep&amp; rhs);</ins>
  <ins>duration&amp; operator%=(const duration&amp; d);</ins>
  ...
};
</pre></blockquote>

<p>
Add to 30.5.4 <a href="https://wg21.link/time.duration.arithmetic">[time.duration.arithmetic]</a>:
</p>

<blockquote>
<pre>
duration&amp; operator%=(const rep&amp; rhs);
</pre>
<blockquote>
<p>
<i>Effects:</i> <code>rep_ %= rhs</code>.
</p>
<p>
<i>Returns:</i> <code>*this</code>.
</p>
</blockquote>

<pre>
duration&amp; operator%=(const duration&amp; d);
</pre>
<blockquote>
<p>
<i>Effects:</i> <code>rep_ %= d.count()</code>.
</p>
<p>
<i>Returns:</i> <code>*this</code>.
</p>
</blockquote>
</blockquote>

<p>
Add to 30.5.6 <a href="https://wg21.link/time.duration.nonmember">[time.duration.nonmember]</a>:
</p>

<blockquote>

<pre>
template &lt;class Rep1, class Period, class Rep2&gt;
  duration&lt;typename common_type&lt;Rep1, Rep2&gt;::type, Period&gt;
  operator%(const duration&lt;Rep1, Period&gt;&amp; d, const Rep2&amp; s);
</pre>
<blockquote>
<p>
<i>Requires:</i> <code>Rep2</code> shall be implicitly convertible to <code>CR(Rep1, Rep2)</code> and
<code>Rep2</code> shall not be an instantiation of <code>duration</code>. Diagnostic required.
</p>
<p>
<i>Returns:</i> <code>duration&lt;CR, Period&gt;(d) %= s</code>.
</p>
</blockquote>

<pre>
template &lt;class Rep1, class Period1, class Rep2, class Period2&gt;
  typename common_type&lt;duration&lt;Rep1, Period1&gt;, duration&lt;Rep2, Period2&gt;&gt;::type
  operator%(const duration&lt;Rep1, Period1&gt;&amp; lhs, const duration&lt;Rep2, Period2&gt;&amp; rhs);
</pre>
<blockquote>
<p>
<i>Returns:</i> <code>common_type&lt;duration&lt;Rep1, Period1&gt;, duration&lt;Rep2, Period2&gt;&gt;::type(lhs) %= rhs</code>.
</p>
</blockquote>

</blockquote>






</body>
</html>
