Boost :: date_time (boost-145) using a 64-bit uint with microsecond calculations without truncation

I use date_time for abstract platform features. and I need to create a 64-bit microsec uint64_t resolution that will be used during serialization. I do not understand what is going wrong below.

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

using namespace boost::posix_time;
using boost::uint64_t;

ptime UNIX_EPOCH(boost::gregorian::date(1970,1,1));

int main() {
    ptime current_time = microsec_clock::universal_time();

    std::cout << "original time: "<< current_time << std::endl;

    long microsec_since_epoch = ((current_time -UNIX_EPOCH).total_microseconds());

    ptime output_ptime = UNIX_EPOCH + microseconds(microsec_since_epoch);
        std::cout << "Deserialized time : " << output_ptime << std::endl;

    std::cout << "Microsecond output: " << microsec_since_epoch << std::endl;

    std::cout << "Microsecond to second arithmetic: "
        << microsec_since_epoch/(10*10*10*10*10*10) << std::endl;

    std::cout << "Microsecond to tiume_duration, back to microsecond : " <<
        microseconds(microsec_since_epoch).total_microseconds() << std::endl;


    return 0;
}

Here is the result I get.

original time: 2010-Dec-17 09:52:06.737123
Deserialized time : 1970-Jan-16 03:10:41.577454
Microsecond output: 1292579526737123
Microsecond to second arithmetic: 1292579526
Microsecond to tiume_duration, back to microsecond : 1307441577454

When I switch to using total_seconds()and + seconds(..)Dissapear --ie Problems, the input changes to:

2010-Dec-15 18:26:22.606978
2010-Dec-15 18:26:22

date_time claims to use a 64-bit type internally, and 2^64Γ· (10^6Γ—3600Γ—24Γ—365) ~= 584942even 2^60Γ· (10^6Γ—3600Γ—24Γ—365) ~= 36558.

The first lines from Wikipedia can be said about Posix time

Unix POSIX - , , (UTC) 1 1970 .

40 ?

64- , boost:: date_time?

- edit1 hans -

, duration.total_microseconds(). 1292576572566904 Γ· (10 ^ 6 Γ— 3600 Γ— 24 Γ— 365) ~ = 40,98 . .

- edit2 - "" . , , .

, .

+5
2

, , () . :

#define MICROSEC 1000000

uint64_t sec_epoch = microsec_since_epoch / MICROSEC;
uint64_t mod_micro_epoch= microsec_since_epoch % MICROSEC;

ptime new_method = UNIX_EPOCH  + seconds(sec_epoch) + microseconds(mod_micro_epoch);

std::cout << "Deserialization with new method: " << new_method << std::endl;
+2

total_microseconds() - tick_type, . , 32- . , 40 .

+1

All Articles