Boost C++ Libraries: Ticket #7868: chrono_io parses time incorrectly (1.53 and 1.52) https://svn.boost.org/trac10/ticket/7868 <p> streaming the result of </p> <blockquote> <p> boost::chrono::system_clock::now() </p> </blockquote> <p> into a stringstream, and back to a </p> <blockquote> <p> boost::chrono::system_clock::time_point </p> </blockquote> <p> gives wrong results. </p> <p> With boost 1.52 the error is exactly one second (when using the default formats) </p> <p> With boost 1.53 beta (of today) the parsed times are semi-random around 1974. </p> <p> With user-defined formats the error behaves a little differently: </p> <p> In my example (attached) the time is correct up to the seconds, which are always parsed as 01 both with boost 1.52 and 1.53. </p> <blockquote> <p> cheers, Johan Lundberg </p> </blockquote> <hr /> <p> Full example: </p> <pre class="wiki"> #define BOOST_CHRONO_VERSION 2 #define BOOST_CHRONO_PROVIDES_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT 1 #include &lt;sstream&gt; #include &lt;iostream&gt; #include &lt;boost/chrono/chrono_io.hpp&gt; #include &lt;boost/chrono/floor.hpp&gt; #include &lt;boost/chrono/round.hpp&gt; #include &lt;boost/chrono/ceil.hpp&gt; int main() { using namespace boost; using namespace boost::chrono; boost::chrono::system_clock::time_point atnow= boost::chrono::system_clock::now(); { std::stringstream strm; std::stringstream strm2; // does not change anything: strm&lt;&lt;time_fmt(boost::chrono::timezone::utc); // does not change anything: strm2&lt;&lt;time_fmt(boost::chrono::timezone::utc); boost::chrono::system_clock::time_point atnow2; strm&lt;&lt;atnow&lt;&lt;std::endl; std::cout &lt;&lt; "A:" &lt;&lt; strm.str()&lt;&lt; std::endl; strm&gt;&gt;atnow2; strm2&lt;&lt;atnow2&lt;&lt;std::endl; std::cout &lt;&lt; "B:" &lt;&lt; strm2.str()&lt;&lt; std::endl; // 1 sec wrong: std::cout &lt;&lt; "diff:" &lt;&lt; boost::chrono::duration_cast&lt;microseconds&gt;(atnow2 - atnow).count() &lt;&lt;std::endl; std::stringstream formatted; formatted &lt;&lt; time_fmt(boost::chrono::timezone::utc, "%Y-%m-%d %H:%M:%S"); formatted &lt;&lt; "actual:"&lt;&lt; atnow &lt;&lt;std::endl;; formatted &lt;&lt; "parsed:"&lt;&lt; atnow2 &lt;&lt;std::endl;; std::cout &lt;&lt; formatted.str(); } { std::stringstream strm; std::stringstream strm2; boost::chrono::system_clock::time_point atnow2; // the final second mark is always parsed as 01 strm&lt;&lt;time_fmt(boost::chrono::timezone::utc, "%Y-%m-%d %H:%M:%S"); strm2&lt;&lt;time_fmt(boost::chrono::timezone::utc, "%Y-%m-%d %H:%M:%S"); strm&lt;&lt;atnow&lt;&lt;std::endl; std::cout &lt;&lt; "actual:" &lt;&lt; strm.str()&lt;&lt; std::endl; strm&gt;&gt;atnow2; strm2&lt;&lt;atnow2&lt;&lt;std::endl; // the final second mark is always parsed as 01 std::cout &lt;&lt; "parsed:" &lt;&lt; strm2.str()&lt;&lt; std::endl; } } </pre><hr /> <p> Details: </p> <p> Compiled boost with gcc4.7.2 on Linux. I tried compiling boost both with and without cxxflags=-std=gnu++11, same result. </p> <p> I also tried compiling my example with and without -std=c++11 </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7868 Trac 1.4.3 viboes Fri, 18 Jan 2013 20:50:59 GMT status changed https://svn.boost.org/trac10/ticket/7868#comment:1 https://svn.boost.org/trac10/ticket/7868#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> Hi, </p> <p> I think that I have found the fix. Please could you try adding in boost/chrono/io/time_point_io.hpp the following missed initialization. </p> <pre class="wiki"> tm.tm_sec=0; </pre><p> before </p> <pre class="wiki">#if defined BOOST_CHRONO_USES_INTERNAL_TIME_GET const detail::time_get&lt;CharT&gt;&amp; dtg(tg); dtg.get(is, 0, is, err, &amp;tm, pb, pe); #else tg.get(is, 0, is, err, &amp;tm, pb, pe); #endif </pre> Ticket viboes Fri, 18 Jan 2013 21:11:57 GMT milestone changed https://svn.boost.org/trac10/ticket/7868#comment:2 https://svn.boost.org/trac10/ticket/7868#comment:2 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.53.0</span> </li> </ul> <p> Here it is a patch to fix it. </p> <pre class="wiki">svn diff boost/chrono Index: boost/chrono/config.hpp =================================================================== --- boost/chrono/config.hpp (revision 82527) +++ boost/chrono/config.hpp (working copy) @@ -28,7 +28,7 @@ #if ! defined BOOST_CHRONO_PROVIDES_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT \ &amp;&amp; ! defined BOOST_CHRONO_DONT_PROVIDE_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT -# define BOOST_CHRONO_DONT_PROVIDE_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT +# define BOOST_CHRONO_PROVIDES_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT #endif Index: boost/chrono/io/time_point_io.hpp =================================================================== --- boost/chrono/io/time_point_io.hpp (revision 82527) +++ boost/chrono/io/time_point_io.hpp (working copy) @@ -936,6 +936,7 @@ { '%', 'Y', '-', '%', 'm', '-', '%', 'd', ' ', '%', 'H', ':', '%', 'M', ':' }; pb = pattern; pe = pb + sizeof (pattern) / sizeof(CharT); + tm.tm_sec=0; #if defined BOOST_CHRONO_USES_INTERNAL_TIME_GET const detail::time_get&lt;CharT&gt;&amp; dtg(tg); dtg.get(is, 0, is, err, &amp;tm, pb, pe); </pre> Ticket viboes Fri, 18 Jan 2013 22:03:04 GMT <link>https://svn.boost.org/trac10/ticket/7868#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7868#comment:3</guid> <description> <p> Committed in trunk revision <a class="changeset" href="https://svn.boost.org/trac10/changeset/82541" title="Chrono: fixed 7868 + some warnings">[82541]</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 20 Jan 2013 19:42:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7868#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7868#comment:4</guid> <description> <p> There is yet an issue with gmtime on Windows when the time_t parameter is &lt; 0. I have added an internal implementation for this function in Changeset <a class="changeset" href="https://svn.boost.org/trac10/changeset/82562" title="Chrono: Added internal_gmtime">[82562]</a>. </p> <p> Then I have made that system clock counts periods "since January 1, 1970, UTC" And activated the use of the internal gmtime function on windows in Changeset <a class="changeset" href="https://svn.boost.org/trac10/changeset/82563" title="Chrono: make use of internal_gmtime on windows and make system_clock ...">[82563]</a>. </p> <p> This should fix the issue. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 27 Jan 2013 12:34:38 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/7868#comment:5 https://svn.boost.org/trac10/ticket/7868#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Changeset <a class="changeset" href="https://svn.boost.org/trac10/changeset/82612" title="Chrono: merge [82562][82663].">[82612]</a> Chrono: merge <a class="changeset" href="https://svn.boost.org/trac10/changeset/82562" title="Chrono: Added internal_gmtime">[82562]</a><a class="changeset" href="https://svn.boost.org/trac10/changeset/82663" title="Chrono: fix #7940">[82663]</a>. </p> Ticket lundberj@… Sun, 30 Jun 2013 23:00:30 GMT status, version, milestone changed; resolution deleted https://svn.boost.org/trac10/ticket/7868#comment:6 https://svn.boost.org/trac10/ticket/7868#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>version</strong> <span class="trac-field-old">Boost 1.52.0</span> → <span class="trac-field-new">Boost 1.54.0</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">fixed</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.53.0</span> → <span class="trac-field-new">Boost 1.54.0</span> </li> </ul> <p> I'm sorry to say that the bug still remains with beta 1.54 (beta0). Specifically the second part of the test code I posted, where the final seconds are always the same, and do not reflect the real time. </p> <p> Other formats, such as "%Y-%m- %S %d %H:%M" seems to quietly fail. </p> <p> In addition, documentation for this seems to be missing (at least I could not find it): What are the allowed markers (%S and friends) and what syntax must a format string have? (such as this one for date_time_io: <a href="http://www.boost.org/doc/libs/1_53_0/doc/html/date_time/date_time_io.html">http://www.boost.org/doc/libs/1_53_0/doc/html/date_time/date_time_io.html</a> ) </p> <p> As and example, I wanted to find out the syntax for manually defining the default format, such as: 2013-06-30 20:01:15.584512 +0000 but I could not find out how to specify the fractional seconds or the time zone mark. Those are things a user should be able to find out. </p> Ticket viboes Sun, 07 Jul 2013 11:28:32 GMT milestone changed https://svn.boost.org/trac10/ticket/7868#comment:7 https://svn.boost.org/trac10/ticket/7868#comment:7 <ul> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.54.0</span> → <span class="trac-field-new">Boost 1.55.0</span> </li> </ul> <p> You are right. The ticket is not completely fixed. There was some comments in the time_point parsing input. The following patch should solve this particular issue, but I gues that I would need to complete the unit tets </p> <pre class="wiki">Index: io/time_point_io.hpp =================================================================== --- io/time_point_io.hpp (revision 84889) +++ io/time_point_io.hpp (working copy) @@ -255,9 +255,9 @@ // b = that_.get(b, e, iob, err, tm, fm, fm + sizeof(fm)/sizeof(fm[0])); // } // break; -// case 'S': -// that_.get_second(tm-&gt;tm_sec, b, e, err, ct); -// break; + case 'S': + get_second(tm-&gt;tm_sec, b, e, err, ct); + break; // case 'T': // { // const char_type fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'}; </pre> Ticket viboes Sun, 07 Jul 2013 11:38:21 GMT <link>https://svn.boost.org/trac10/ticket/7868#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7868#comment:8</guid> <description> <p> Committed revision <a class="changeset" href="https://svn.boost.org/trac10/changeset/84967" title="Chrono: Complete #7868.">[84967]</a>. Committed revision <a class="changeset" href="https://svn.boost.org/trac10/changeset/84973" title="Chrono: Manage with more time_point io format flags and tests.">[84973]</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 13 Sep 2013 19:31:49 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/7868#comment:9 https://svn.boost.org/trac10/ticket/7868#comment:9 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Changeset <a class="changeset" href="https://svn.boost.org/trac10/changeset/85610" title="Chrono: merge timepoint io improvements.">[85610]</a> </p> Ticket Johan Lundberg <lundberj@…> Sun, 15 Sep 2013 08:40:31 GMT <link>https://svn.boost.org/trac10/ticket/7868#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7868#comment:10</guid> <description> <p> Great! It also passes my tests. </p> <p> A while back I reported compiler warnings on initializing the tm struct. The preferred solution at other places (<a class="ext-link" href="https://svn.boost.org/trac/boost/changeset/80797/branches/release/boost/date_time/date_facet.hpp"><span class="icon">​</span>https://svn.boost.org/trac/boost/changeset/80797/branches/release/boost/date_time/date_facet.hpp</a>) is </p> <blockquote> <p> std::tm dtm; std::memset(&amp;dtm, 0, sizeof(dtm)); </p> </blockquote> <p> That also initialize any allowed but unspecified members in addition to the ones required by the standard. </p> <p> I still miss the fractional seconds part. Perhaps this is a feature-request, but that would make it possible to make full use of these facilities in a much wider range of applications. It would also allow to manually specify variants of the default format which does have fractional seconds, "1970-01-01 00:02:46.666667 +0000". </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 15 Sep 2013 16:21:10 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7868#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7868#comment:11</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/7868#comment:10" title="Comment 10">Johan Lundberg &lt;lundberj@…&gt;</a>: </p> <blockquote class="citation"> <p> Great! It also passes my tests. </p> <p> A while back I reported compiler warnings on initializing the tm struct. The preferred solution at other places (<a class="ext-link" href="https://svn.boost.org/trac/boost/changeset/80797/branches/release/boost/date_time/date_facet.hpp"><span class="icon">​</span>https://svn.boost.org/trac/boost/changeset/80797/branches/release/boost/date_time/date_facet.hpp</a>) is </p> <blockquote> <p> std::tm dtm; std::memset(&amp;dtm, 0, sizeof(dtm)); </p> </blockquote> <p> That also initialize any allowed but unspecified members in addition to the ones required by the standard. </p> </blockquote> <p> Please create a new bug ticket for that. I will fix it. </p> <blockquote class="citation"> <p> I still miss the fractional seconds part. Perhaps this is a feature-request, but that would make it possible to make full use of these facilities in a much wider range of applications. It would also allow to manually specify variants of the default format which does have fractional seconds, "1970-01-01 00:02:46.666667 +0000". </p> </blockquote> <p> Please create a new feature request for any changes you need. </p> </description> <category>Ticket</category> </item> </channel> </rss>