Hello,

 

When I convert a (large) number of milliseconds to a time_duration, I noticed a truncation error. This is shown in the following example:

 

BOOST_AUTO_TEST_CASE(test_time_duration)

{

  static const boost::posix_time::ptime ref(boost::gregorian::date(1400,1,1));

  static const boost::posix_time::ptime now(boost::gregorian::date(2010,5,18));

 

  boost::int64_t elapsed = (now-ref).total_milliseconds();

 

  boost::posix_time::time_duration d1 = boost::posix_time::millisec(elapsed);

 

  BOOST_CHECK_EQUAL(d1.total_milliseconds(), elapsed);

}

 

error in "test_time_duration": check d1.total_milliseconds() == elapsed failed [814839926290 != 19261584000000]

 

This is caused by an overflow issue in the constructor of subsecond_duration (called by boost::posix_time::millisec):

 

  template<class base_duration, boost::int64_t frac_of_second>

  class subsecond_duration : public base_duration

  {

  public:

    typedef typename base_duration::traits_type traits_type;

    explicit subsecond_duration(boost::int64_t ss) :

      base_duration(0,0,0,ss*traits_type::res_adjust()/frac_of_second)

    {}

  };

 

Shouldn't this constructor check whether traits_type::res_adjust()>frac_of_second and then do “base_duration(0,0,0,ss*(traits_type::res_adjust()/frac_of_second))” (note the brackets)?

 

Thanks,

 

Johan

 


Get a free e-mail account with Hotmail. Sign-up now.