Boost C++ Libraries: Ticket #5753: previous_weekday() returns the very same day https://svn.boost.org/trac10/ticket/5753 <p> We have a bug that seems to comes from boost::date_time::previous_weekday() (probably next_weekday() too) usage in case we want to go from a weekday to another same weekday: </p> <pre class="wiki"> #include &lt;iostream&gt; #include &lt;boost/date_time.hpp&gt; #include &lt;boost/date_time/gregorian/gregorian.hpp&gt; int main() { using namespace boost::gregorian; auto today = date( 2011, Jul, 12 ); auto last_tuesday = boost::date_time::previous_weekday( today, greg_weekday(boost::date_time::Tuesday) ); std::cout &lt;&lt; today &lt;&lt; '\n'; std::cout &lt;&lt; last_tuesday; std::cin.ignore(); return 0; } </pre><p> This example runs but return the same date twice instead of giving the tuesday before the given date (that is already a tuesday). Compiled with VS2010SP1 (boost 1.46.1) and GCC4.4(boost 1.44.0 - didn't see the difference in the code) What we first supposed was it will go to the tuesday of the previous week if we already are tuesday. It appears to not work this way. </p> <p> Is it the wanted behaviour? If it is, I suggest the documentation to be enhanced to warn the user about the case when you provide a date that is the same provided weekday. </p> <p> The code of boost::date_time::previous_weekday() (as far as I understand it) suggests that it should go to the previous week's weekday instead of returning the same day: </p> <pre class="wiki"> template&lt;class date_type, class weekday_type&gt; inline date_type previous_weekday(const date_type&amp; d, const weekday_type&amp; wd) { return d - days_before_weekday(d, wd); } </pre><p> and then : </p> <pre class="wiki"> template&lt;typename date_type, class weekday_type&gt; inline typename date_type::duration_type days_before_weekday(const date_type&amp; d, const weekday_type&amp; wd) { typedef typename date_type::duration_type duration_type; duration_type wks(0); duration_type dd(wd.as_number() - d.day_of_week().as_number()); // 1 if(dd.days() &gt; 0){ // 2 wks = duration_type(7); } // we want a number of days, not an offset. The value returned must // be zero or larger. return (-dd + wks); // 3 } </pre><p> Correct me if I'm wrong : 2. checks if the generated day is the same weekday than the provided one, right? So if I'm correct, there might be a problem in 1 or 3? </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5753 Trac 1.4.3 Marshall Clow Sat, 15 Mar 2014 15:42:07 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/5753#comment:1 https://svn.boost.org/trac10/ticket/5753#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> Here's my take on the code in <code>days_before_weekday</code>: </p> <p> <code>1</code>: Calculates a number in the range 0 .. 6 for the next time the specified day of the week rolls around. Note that asking for Tuesday on Tuesday gives you 0. </p> <p> <code>2</code>: If we got a number &gt; 0, then we're looking into the future. Set the <code>wks</code> variable to 7. </p> <p> <code>3</code>: Step <code>wks</code> days into the future, and then subtract off the number of days we calculated in step 1. </p> <p> Note that the comment at the end of the routine explicitly says that this function can return zero (i.e, zero days from <code>d</code>). I don't see a bug in this code (though I would be tempted to write it like this - <em>warning: untested code</em>): </p> <pre class="wiki"> typedef typename date_type::duration_type duration_type; duration_type dd(wd.as_number() - d.day_of_week().as_number()); // 1 return dd ? duration_type(7) - dd ? 0; </pre><p> I don't think there's a bug in this code. The behavior "return 0 when asking for last Tuesday on Tuesday" is quite deliberate. Whether or not that <em>should</em> be the behavior is a different question, and that's a discussion that should be had on the boost mailing list. </p> <p> I'll take a look at the documentation; see what the best place to add a note is. Closing this as "not a bug". If you disagree, please re-open it. </p> Ticket