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".
Presumably you’re on a platform on which
longis smaller than 64 bits.Let’s assume it’s 32 bits – in that case, the maximum value of a
longis 2147483648. However, it’s been ~1312000000000 milliseconds since epoch, solongis clearly insufficient to hold this value and consequently you’re seeing overflow.I’d do something like this instead:
Creating a
ptimefrom the specified number of milliseconds has the same problem –ptimeworks in terms oflongand you have along long– so you’ll essentially need to do the reverse: