Subject: Re: [Boost-bugs] [Boost C++ Libraries] #3109: time_duration::total_seconds() - overflow
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2012-02-08 13:28:55
#3109: time_duration::total_seconds() - overflow
-------------------------------+--------------------------------------------
Reporter: ioni@⦠| Owner: az_sw_dude
Type: Bugs | Status: new
Milestone: To Be Determined | Component: date_time
Version: Boost 1.41.0 | Severity: Problem
Resolution: | Keywords: year 2038 problem
-------------------------------+--------------------------------------------
Comment (by Klemens Schindler):
This problem still applies to Boost version 1.47 (I didn't check even
newer versions)
The following code:
{{{#!c++
using namespace boost::posix_time;
using namespace boost::gregorian;
using namespace boost::local_time;
local_date_time now(local_sec_clock::local_time(time_zone_ptr()));
local_date_time baseADTime( date(1601, 1, 1), hours(0),
time_zone_ptr(), local_date_time::NOT_DATE_TIME_ON_ERROR);
time_duration timeInterval(now.utc_time() - baseADTime.utc_time());
cout << "BaseTime: " << baseADTime << endl;
cout << "Time: " << now << endl;
cout << "MicroSeconds: " << timeInterval.total_microseconds() << endl;
cout << "MilliSeconds: " << timeInterval.total_milliseconds() << endl;
cout << "Seconds: " << timeInterval.total_seconds() << endl;
}}}
produces the following output:
{{{#!sh
BaseTime: 1601-Jan-01 00:00:00 UTC
Time: 2012-Feb-08 12:36:54 UTC
MicroSeconds: 12973178214000000
MilliSeconds: 12973178214000
Seconds: 88276326
}}}
In case you're wondering why it would be interesting to calculate the time
interval between 01-01-1601 and now: the time representation in Microsoft
Active Directory uses a time representation based on the time between 1601
and now.
It is at least counterintuitive that types with a higher
resolution/precision are able to store larger intervals. According to the
documentation
[[http://www.boost.org/doc/libs/1_47_0/doc/html/date_time/posix_time.html
| documentation]], all "total_x" methods should return the same type, that
doesn't seem to be the case.
It is probably possible to use type traits to set the internal
representation, but this is somewhat complex to do. (Thanks to Andrei
Korostelev for pointing out this possibility). In any case as far as I
know it cannot be done in an obvious/self-explanatory way.
Another proposed solution would be to add a template parameter to
total_seconds and similar methods so you can explicitly specify the
representation type. For example:
{{{#!cpp
int_fast64_t numSeconds = timeInterval.total_seconds<int_fast64_t>()
}}}
This way a user explicitly states what he wants and will not run into
surprises.
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/3109#comment:2> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:08 UTC