[date-time] boost::time_duration wrapping
Hello,
I’m having issues with wrapping of boost::time_duration derived types. I can’t find anywhere in the documentation for when the time_duration types are supposed to wrap around.
I have attached some sample source code below. The output is:
SecCount = 80000000 is OK.
Strange
Stranger
Problematic
SecCount = 80000001 is STRANGE.
I expect the output to be
SecCount = 80000000 is OK.
SecCount = 80000001 is OK.
The problem we ran into had to do with seconds and milliseconds having different maximum permissible values. Any idea why boost::posix_time::seconds goes negative at 0x80000001 while milliseconds is not until much later?
Thanks,
Justin
//
// test.cpp
//
#include
On Aug 6, 2014, at 7:07 PM, Justin Muncaster
Hello,
I’m having issues with wrapping of boost::time_duration derived types. I can’t find anywhere in the documentation for when the time_duration types are supposed to wrap around.
If the duration types are based on signed integers (which I believe they are), then "wrapping around” is undefined behavior. Once you’ve invoked undefined behavior, then you can’t expect *anything*, or, as I say, “there are no wrong answers” — Marshall
I have attached some sample source code below. The output is: SecCount = 80000000 is OK. Strange Stranger Problematic SecCount = 80000001 is STRANGE.
I expect the output to be SecCount = 80000000 is OK. SecCount = 80000001 is OK.
The problem we ran into had to do with seconds and milliseconds having different maximum permissible values. Any idea why boost::posix_time::seconds goes negative at 0x80000001 while milliseconds is not until much later?
Thanks,
Justin
// // test.cpp //
#include
#include <iostream> using namespace std; using namespace boost::posix_time;
//------------------------------------------------------- //------------------------------------------------------- void Strange(long SecCount) { long MilliCount = 1000 * SecCount;
bool IsStrange = false;
if (SecCount >= 0 && seconds(SecCount).is_negative()) { cout << "Strange" << endl; IsStrange = true;
if (!milliseconds(MilliCount).is_negative()) { // If seconds wraps, why doesn't milliseconds? cout << "Stranger" << endl; IsStrange = true; } }
if (seconds(SecCount) != milliseconds(MilliCount)) { cout << "Problematic" << endl; IsStrange = true; }
if (!IsStrange) { cout << "SecCount = " << SecCount << " is OK." << endl; } else { cout << "SecCount = " << SecCount << " is STRANGE." << endl; } }
//------------------------------------------------------- //------------------------------------------------------- int main() { cout << hex; Strange(0x80000000); Strange(0x80000001); return 0; }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Justin Muncaster
-
Marshall Clow