3

Consider this piece of code (compiled with VS2015, using boost 1.60:

#include <boost/date_time/posix_time/posix_time_duration.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>

int main( int argc, char* argv[] )
{
    uint64_t val = static_cast<uint64_t>( 5000000000 * 1000000 );
    std::cout << val << std::endl;
    boost::posix_time::time_duration the = boost::posix_time::microseconds( val );
    std::cout << the << std::endl;
    auto res3 = the.total_seconds();
    std::cout << res3 << std::endl;
    return 0;
}

It outputs:

5000000000000000
1388888:53:20
705032704

Two first lines are OK. However, last line should report 5000000000 (5000000000000000 microseconds is 5000000000 seconds....which is exactly (1388888*3600+53*60+20)).

Where does this 705032704 comes from??

1 Answer 1

4

It's a stupid cast issue in boost:

sec_type total_seconds() const
{
  return static_cast<sec_type>(ticks() / ticks_per_second());
}

ticks() and ticks_per_second() both return int64_t. (ticks() / ticks_per_second()) is evaluated to 5000000000. But, as boost::posix_time::time_duration::sec_type is int32_t...the static_cast transforms 5000000000 (out of int32_t range...max value being 4294967295) into 705032704 (=5000000000-4294967295-1)).

5
  • Time to submit a bug report! :)
    – Borgleader
    May 18, 2016 at 12:31
  • The documentation explicitly says the return type is long. Should've been the first place you looked. May 18, 2016 at 12:32
  • @Borgleader There already is one: Bug 3109. May 18, 2016 at 12:37
  • @sleeptightpupper Opened 7 years ago. Last modified 4 years ago Oh wow... Maybe a new one is a good idea :P
    – Borgleader
    May 18, 2016 at 12:39
  • @Borgleader Here are more: one, two. Instead of submitting duplicate bug reports I would turn on -Wsign-conversion and find more productive ways of working around the problem. May 18, 2016 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.