Boost logo

Boost Users :

Subject: [Boost-users] [date-time] boost::time_duration wrapping
From: Justin Muncaster (jmuncaster_at_[hidden])
Date: 2014-08-06 22:07:54


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 <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 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