c++boostposixboost-date-time

C++ - Calculate the millisecond from ptime in total seconds


How do i calculate the millisecond difference from the following Ptime ,I am using boost::ptime I'm trying to calculate the time_duration in milliseconds to find the difference. i get value like 999975 but expected value is 975

ptime PreviousgpsTime = Mon Jun 28 17:07:10.054 2021 ptime NextgpsTime = Mon Jun 28 17:07:11.025 2021

  double totalDiff = (NextgpsTime-PreviousgpsTime).total_milliseconds();

How to fix this and get the actual time duration.


Solution

  • Live On Coliru:

    #include <boost/date_time/posix_time/posix_time.hpp>
    
    int main()
    {
        using namespace boost::posix_time;
    
        ptime PreviousgpsTime = time_from_string("2021-Jun-28 17:07:10.054");
        ptime NextgpsTime     = time_from_string("2021-Jun-28 17:07:11.025");
    
        long totalDiff = (NextgpsTime - PreviousgpsTime).total_milliseconds();
    
        std::cout << "From " << PreviousgpsTime << " to " << NextgpsTime << " is " << totalDiff << "ms\n";
    }
    

    Prints

    From 2021-Jun-28 17:07:10.054000 to 2021-Jun-28 17:07:11.025000 is 971ms