c++ - boost::posix_time::time_duration overflow? -


i have following code unix time posix_time

boost::posix_time::ptime time1(boost::gregorian::date(9999,12,31)); boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));  boost::posix_time::time_duration diff = time1-epoch; cout<<"a: "<<time1<<endl; cout<<"b: "<<epoch<<endl; cout<<"c: "<<diff<<endl;  unix_time = diff.total_seconds() 

gives me output

9999-dec-31 00:00:00 1970-jan-01 00:00:00 -1347834:03:51.933722624 

now diff should not negative number. how can calculate right? there overflow?

(btw - used coliru output below, , local gcc 5.3.1 boost 1.60 reproduces)

the problem wanting number in seconds, let's try basic math (and see if right!) :)

a: 9999-dec-31 00:00:00 b: 1970-jan-01 00:00:00 c: 70389504:00:00 

so difference (expressed in hours) 70389504 hours. in seconds is:

70389504 * 60 * 60 => 253402214400 seconds

now, in internals of library, there type def sec_type defaults boost::int32_t. unless set int64_t, above value overflow.

as how override this, it's not possible unless hack date_time library, , change default in time_resolution_traits.h boost::int32_t boost::int64_.. (there way, haven't investigated code in detail enough tell be..)

now real problem seems have set -dboost_date_time_posix_time_std_config - suspect want nanosecond precision? if so, think have reduce date range supported.