Boost C++ Libraries: Ticket #3470: Bug in boost::date_time::c_time::localtime(), boost::date_time::c_time::gmtime() https://svn.boost.org/trac10/ticket/3470 <blockquote> <p> The code below fails on asserts on MSVC 8 </p> <pre class="wiki"> const std::time_t tt = std::time(0); std::tm tms; tms.tm_hour = 28; const std::tm * ptm = boost::date_time::c_time::localtime(&amp;tt, &amp;tms); assert(&amp;tms == ptm); assert(28 != tms.tm_hour); </pre></blockquote> <blockquote> <p> The proposed patch: </p> <pre class="wiki">Index: c_time.hpp =================================================================== --- c_time.hpp (revision 56335) +++ c_time.hpp (working copy) @@ -80,18 +80,20 @@ inline static std::tm* localtime(const std::time_t* t, std::tm* result) { - result = std::localtime(t); - if (!result) + std::tm* const tmp = std::localtime(t); + if (!tmp) boost::throw_exception(std::runtime_error("could not convert calendar time to local time")); + *result = *tmp; return result; } //! requires a pointer to a user created std::tm struct inline static std::tm* gmtime(const std::time_t* t, std::tm* result) { - result = std::gmtime(t); - if (!result) + std::tm* const tmp = std::gmtime(t); + if (!tmp) boost::throw_exception(std::runtime_error("could not convert calendar time to UTC time")); + *result = *tmp; return result; } #if (defined(_MSC_VER) &amp;&amp; (_MSC_VER &gt;= 1400)) </pre></blockquote> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3470 Trac 1.4.3 Andrey Semashev Mon, 21 Sep 2009 14:53:38 GMT severity changed https://svn.boost.org/trac10/ticket/3470#comment:1 https://svn.boost.org/trac10/ticket/3470#comment:1 <ul> <li><strong>severity</strong> <span class="trac-field-old">Showstopper</span> → <span class="trac-field-new">Problem</span> </li> </ul> <p> Hmm... According to the docs the functions should return the pointer to tm passed as an argument, so technically this is a bug. However, I don't see a good rationale for this requirement as it only introduces unnecessary data copying. Also, this code is there for ages and no one has brought it up. Maybe we're better off correcting the docs. And a new, more C++-style interface might also be a good idea. </p> <p> Do you have a rationale for this change? A motivating example, perhaps? </p> Ticket Andrey Semashev Sat, 03 Oct 2009 10:21:10 GMT <link>https://svn.boost.org/trac10/ticket/3470#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3470#comment:2</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/56548" title="Refs #3470. Modified documentation for c_time functions to reflect the ...">[56548]</a>) Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3470" title="#3470: Bugs: Bug in boost::date_time::c_time::localtime(), ... (closed: fixed)">#3470</a>. Modified documentation for c_time functions to reflect the actual behavior. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Sat, 03 Oct 2009 10:23:00 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3470#comment:3 https://svn.boost.org/trac10/ticket/3470#comment:3 <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">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/56549" title="Fixes #3470. Merged from trunk revision 56548.">[56549]</a>) Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3470" title="#3470: Bugs: Bug in boost::date_time::c_time::localtime(), ... (closed: fixed)">#3470</a>. Merged from trunk revision 56548. </p> Ticket