Boost logo

Boost Users :

Subject: Re: [Boost-users] [date-time] boost::time_duration wrapping
From: Marshall Clow (mclow.lists_at_[hidden])
Date: 2014-08-07 00:17:32


On Aug 6, 2014, at 7:07 PM, Justin Muncaster <jmuncaster_at_[hidden]> wrote:

> 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 <boost/date_time.hpp>
> #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_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net