Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the time in milliseconds since epoch time from boost::posix_time::ptime

I've seen some other answers on SO that suggest we can get the time from epoch in milliseconds by subtracting the epoch time from the "other" time, but it doesn't work when I try it:

ptime epoch = time_from_string("1970-01-01 00:00:00.000");
ptime other = time_from_string("2011-08-09 17:27:00.000");

long diff = (other-epoch).total_milliseconds();

At this stage diff is -1349172576 and it should be a positive number since the "other" time is 2011. Does anybody know what might be causing this? What's the proper way to get the milliseconds since epoch?

Additionally, I've tried to construct a ptime object from milliseconds:

ptime result = from_time_t(diff);

Result then becomes: "1927-Apr-01 13:50:24" and it should be "2011-Aug-09 17:27:00.000". What's the catch here?

Update:

OK, so my mistake stems from the fact that I have 2 programs, one is C# (8 byte/64-bit long) and a C++ (4 byte/32-bit long); in any case, that interaction is not depicted here.

However, when I use long long, the value is positive but the resulting date (constructed from_time_t) is still incorrect: "2012-Oct-02 10:09:36".

like image 983
Kiril Avatar asked Aug 11 '11 00:08

Kiril


3 Answers

Presumably you're on a platform on which long is smaller than 64 bits.

Let's assume it's 32 bits – in that case, the maximum value of a long is 2147483648. However, it's been ~1312000000000 milliseconds since epoch, so long is clearly insufficient to hold this value and consequently you're seeing overflow.

I'd do something like this instead:

ptime epoch = time_from_string("1970-01-01 00:00:00.000");
ptime other = time_from_string("2011-08-09 17:27:00.000");

time_duration const diff = other - epoch;
long long ms = diff.total_seconds();
ms *= 1000LL;
ms += diff.fractional_seconds() / 1000000L; // 1000L if you didn't build datetime
                                            // with nanosecond resolution

Creating a ptime from the specified number of milliseconds has the same problem – ptime works in terms of long and you have a long long – so you'll essentially need to do the reverse:

// given long long ms
time_duration t = seconds(static_cast<long>(ms / 1000LL));
if (ms % 1000LL)
    t += milliseconds(static_cast<long>(ms % 1000LL));
like image 133
ildjarn Avatar answered Nov 15 '22 03:11

ildjarn


A shortened variation on ildjarn's great solution:

ptime epoch = time_from_string("1970-01-01 00:00:00.000");
ptime other = time_from_string("2011-08-09 17:27:00.001");

time_duration const diff = other - epoch;
long long ms = diff.total_milliseconds();

This would be independent of whether it was built with nanosecond resolution.

like image 21
Therefore Avatar answered Nov 15 '22 03:11

Therefore


you could try:

ptime other = time_from_string("2011-08-09 17:27:00.000");
time_t posix_time = (other - ptime(min_date_time)).total_seconds();
like image 38
Arvid Avatar answered Nov 15 '22 03:11

Arvid