Boost C++ Libraries: Ticket #4543: ptime: year 2038 problem https://svn.boost.org/trac10/ticket/4543 <p> At several locations in the code, the number of seconds, minutes, hours, etc is explicitly limited to the value range of <code>long</code>, for instance in <code>posix_time/conversion.hpp</code>: </p> <pre class="wiki"> inline ptime from_time_t(std::time_t t) { ptime start(gregorian::date(1970,1,1)); return start + seconds(static_cast&lt;long&gt;(t)); } </pre><p> As a result, for example -4260211200 is represented by some ptime in the year 1971, but it should be 1835. </p> <p> Or, in other words Boost.<a class="missing wiki">DateTime</a> has the year 2038 problem. </p> <p> Regards, </p> <p> Roland </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4543 Trac 1.4.3 ookami1@… Sat, 18 Sep 2010 17:35:31 GMT <link>https://svn.boost.org/trac10/ticket/4543#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4543#comment:1</guid> <description> <p> as a side note: </p> <p> I wonder whether date_time could (or should) be brought fully in line with ISO 8601. Not yet supported is </p> <ul><li>day-of-week numbering 1-7 </li><li>proleptic usage back to year 1 B.C. </li></ul><p> Roland Bock's observation adds to this list. </p> <p> cheers </p> <p> Wolf Lammen </p> </description> <category>Ticket</category> </item> <item> <author>pjohnmeyer@…</author> <pubDate>Wed, 07 Mar 2012 22:04:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4543#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4543#comment:2</guid> <description> <p> I am curious what the path forward is on this bug, which still exists in 1.48. I have implemented my own version of "from_time_t" that we use on our project that extends the useful life of the function significantly. </p> <p> The most confusing part of this problem, in my mind, is that you can achieve valid and invalid times by performing essentially the same set of operations. See below: </p> <pre class="wiki"> std::time_t theT = std::time_t(LONG_MAX) + std::time_t(100000); auto tm1 = boost::posix_time::from_time_t(theT); auto tm2 = boost::posix_time::from_time_t(LONG_MAX) + boost::posix_time::seconds(100000); auto tm3 = myproject::from_time_t(theT); </pre><p> <code>tm1</code> is 1901-Dec-15 00:32:31, because the <code>from_time_t</code> cast to <code>long</code> (which is 32-bits on my platform, MSVC) causes the positive value <code>LONG_MAX + 100000</code> to be interpreted as a negative value. </p> <p> <code>tm2</code> and <code>tm3</code> both evaluate, correctly, to 2038-Jan-20 07:00:47. <code>tm2</code> at a glance does nothing mathematically different; it simply adds in the additional 100000 seconds after <code>from_time_t</code> is called. </p> <p> Here is the implementation of <code>myproject::from_time_t</code>. Yes it has magic numbers that I calculated by working backwards from the inner workings of the <code>posix_time</code> code, and it doesn't address the full range of possible values of a 64-bit <code>time_t</code>. It does, however, extend the max value to 9,012,505,233,654 seconds since the epoch. The impacts this has elsewhere, however, I have not yet examined -- I know that, for example, converting extremely high values to a gregorian date will cause a bad year exception. </p> <pre class="wiki"> boost::posix_time::ptime myproject::from_time_t(std::time_t time) { return boost::posix_time::ptime( static_cast&lt;boost::posix_time::ptime::time_rep_type&gt;( 210866803200000000ll + (time * 1000000) ) } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>az_sw_dude</dc:creator> <pubDate>Sun, 01 Jul 2012 18:23:00 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4543#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4543#comment:3</guid> <description> <p> This is a real issue, however, it goes beyond the implementation of this method. The seconds type itself has a limited range and is subject to this overflow. So this needs to be addressed more generally in the library. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>az_sw_dude</dc:creator> <pubDate>Sun, 01 Jul 2012 18:23:18 GMT</pubDate> <title>status changed; milestone deleted https://svn.boost.org/trac10/ticket/4543#comment:4 https://svn.boost.org/trac10/ticket/4543#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-deleted">Boost 1.44.0</span> </li> </ul> Ticket Jim King <jim.king@…> Wed, 16 Sep 2015 13:56:33 GMT <link>https://svn.boost.org/trac10/ticket/4543#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4543#comment:5</guid> <description> <p> I found that in time_resolution_traits the variable type being used is a boost::int32_t. I changed this to std::time_t and the problems went away. This did require rebuilding some things. I was able to simplify to_time_t and from_time_t down to: </p> <pre class="wiki"> //! Function that converts a time_t into a ptime. inline ptime from_time_t(std::time_t t) { return ptime(gregorian::date(1970,1,1)) + seconds(t); } //! Function that converts a ptime into a time_t inline std::time_t to_time_t(const ptime&amp; pt) { return (pt - ptime(gregorian::date(1970,1,1))).total_seconds(); } </pre><p> What I am uncertain of is the effect this has on any backwards compatibility or i/o conversions for streaming. Thoughts? </p> </description> <category>Ticket</category> </item> <item> <author>Jim King <jim.king@…></author> <pubDate>Sat, 14 Nov 2015 15:09:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4543#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4543#comment:6</guid> <description> <p> Here are the changes I made. They are not yet in boost 1.60 but I believe they should be considered: </p> <pre class="wiki">root@dvm5:/usr/local/src# diff boost_1_60_0_b1/boost/date_time/time_resolution_traits.hpp boost_1_59_0/boost/date_time/time_resolution_traits.hpp 12c12 &lt; --- &gt; #include &lt;ctime&gt; 71c71 &lt; typename var_type = boost::int32_t &gt; --- &gt; typename var_type = std::time_t &gt; </pre><pre class="wiki">root@dvm5:/usr/local/src# diff boost_1_60_0_b1/boost/date_time/posix_time/conversion.hpp boost_1_59_0/boost/date_time/posix_time/conversion.hpp 12a13 &gt; #include &lt;boost/cstdint.hpp&gt; 24d24 &lt; 29,30c29 &lt; ptime start(gregorian::date(1970,1,1)); &lt; return start + seconds(static_cast&lt;long&gt;(t)); --- &gt; return ptime(gregorian::date(1970,1,1)) + seconds(t); 35c34 &lt; std::time_t to_time_t(ptime pt) --- &gt; std::time_t to_time_t(const ptime&amp; pt) 37,38c36 &lt; time_duration dur = pt - ptime(gregorian::date(1970,1,1)); &lt; return std::time_t(dur.total_seconds()); --- &gt; return (pt - ptime(gregorian::date(1970,1,1))).total_seconds(); </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>TMx</dc:creator> <pubDate>Tue, 15 Mar 2016 06:52:32 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4543#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4543#comment:7</guid> <description> <p> We were also stumbling across that issue, and I would consider it a serious one. Since this is open for 6 years now, I wonder when is it planned to fix it? Will it be part of 1.61? </p> <p> Thanks, Thomas </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 21 Feb 2017 20:57:28 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4543#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4543#comment:8</guid> <description> <p> Hi, </p> <p> Can any Boost date_time maintainer consider the above patch provided by Jim King. It also fixes the year 2038 issues I have when using to_time_t. </p> <p> Cheers, Romain </p> </description> <category>Ticket</category> </item> <item> <author>jim.king@…</author> <pubDate>Tue, 21 Feb 2017 21:51:19 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4543#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4543#comment:9</guid> <description> <p> <a class="ext-link" href="https://github.com/boostorg/date_time/pull/35"><span class="icon">​</span>https://github.com/boostorg/date_time/pull/35</a> </p> <p> I just realized there's no unit test that covers the original issue or the fix. Hopefully whomever is pulling in the fix into their module can add that. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Mon, 27 Mar 2017 22:30:23 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4543#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4543#comment:10</guid> <description> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2818" title="#2818: Bugs: boost::posix_time::from_time_t() assumes boost::int32_t == long (closed: fixed)">#2818</a> is even older than this bug, but it's the same. Any chance to be fixed in this decade? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 28 Mar 2017 16:22:15 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4543#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4543#comment:11</guid> <description> <p> This may need to be brought to the attention of someone who organizes the boost project as a whole. This issue is serious and hasn't even been assigned to a milestone for 7 years. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Edward Diener</dc:creator> <pubDate>Tue, 28 Mar 2017 21:03:12 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4543#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4543#comment:12</guid> <description> <p> I merged the PR into the 'develop' branch but it may not make it into Boost 1.64, which is in beta testing. The original date_time author has not been active in Boost for a while so others, including myself, are trying to pay attention to bug reports and PRs for date_time. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>James E. King, III</dc:creator> <pubDate>Fri, 22 Dec 2017 22:03:49 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4543#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4543#comment:13</guid> <description> <p> For folks who have commented on this issue in the past, the fix in 1.66.0 caused a serialization problem as the output was not properly versioned (no changes were made to accomodate the older 32-bit values), see <a class="ext-link" href="https://github.com/boostorg/date_time/issues/56"><span class="icon">​</span>https://github.com/boostorg/date_time/issues/56</a> for details. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>James E. King, III</dc:creator> <pubDate>Thu, 18 Jan 2018 14:09:06 GMT</pubDate> <title>owner, status changed; milestone set https://svn.boost.org/trac10/ticket/4543#comment:14 https://svn.boost.org/trac10/ticket/4543#comment:14 <ul> <li><strong>owner</strong> changed from <span class="trac-author">az_sw_dude</span> to <span class="trac-author">James E. King, III</span> </li> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">new</span> </li> <li><strong>milestone</strong> → <span class="trac-field-new">Boost 1.67.0</span> </li> </ul> <p> All known y2038 issues are resolved in 1.67.0 so I am marking it as the milestone. These are covered by a variety of other trac issues. </p> Ticket James E. King, III Sun, 28 Jan 2018 16:23:40 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/4543#comment:15 https://svn.boost.org/trac10/ticket/4543#comment:15 <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> Fix merged to master; resolved. </p> Ticket