How to get the time in milliseconds since epoch time from boost::posix_time::ptime
Solution 1
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));
Solution 2
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.
Solution 3
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();
Kiril
CEO and Co-Founder of ST6.io E-mail: click to reveal e-mail
Updated on June 12, 2022Comments
-
Kiril almost 2 years
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 (constructedfrom_time_t
) is still incorrect: "2012-Oct-02 10:09:36". -
Kiril over 12 yearsYes, I have a C# and a C++ program that are supposed to transfer a time since epoch and I just realized that the C# long is 8 bytes, while the C++ long is 4 bytes. With that said,
long long
is in fact a positive number, but when I try to constructptime
by callingfrom_time_t
on the result I get an incorrect date again: "2012-Oct-02 10:09:36". -
ildjarn over 12 years@Lirik : Your question only states that you want the number of milliseconds since epoch -- where does
time_t
come in? I.e., what data type do you really ultimately want this in? -
Kiril over 12 yearsI want to be able to go back and forth between time in milliseconds (
long long
) andptime
. The time in milliseconds is offset from epoch. -
Kiril over 12 yearshow do I know if I built datetime with nanosecond resolution? How is it built by default, i.e. I didn't change anything, what's the default resolution?
-
ildjarn over 12 years@Lirik : Quoting the docs -- "By default the posix_time system uses a single 64 bit integer internally to provide a microsecond level resolution. As an alternative, a combination of a 64 bit integer and a 32 bit integer (96 bit resolution) can be used to provide nano-second level resolutions."
-
Kiril over 12 yearsgotcha! Thanks for the help! :)