Boost logo

Boost Users :

Subject: [Boost-users] [date-time] converting milliseconds to time_duration - overflow issue
From: Johan Ditmar (johanditmar_at_[hidden])
Date: 2010-05-18 08:04:37


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
 
                                               
_________________________________________________________________
http://clk.atdmt.com/UKM/go/195013117/direct/01/



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