5

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".

Community
  • 1
  • 1
Kiril
  • 39,672
  • 31
  • 167
  • 226
  • What is the size of `long` on your platform? – ildjarn Aug 11 '11 at 00:34
  • How big is long on your machine? 40 years in milliseconds is around 2^40 milliseconds. Try googling `log(40+years/1+millisecond)/log(2)`. – Cascabel Aug 11 '11 at 00:34
  • Ah... Windows, long is 4 bytes, I should be using long long. My program has 2 components a C# and a C++; a long in C# is 8 bytes but in C++ it's only 4 so it messed things up a bit. – Kiril Aug 11 '11 at 00:37
  • @Lirik : `from_time_t` takes a `time_t`, which is often a typedef for `long`. So you're back at square one. What type do you actually _want_? – ildjarn Aug 11 '11 at 01:23
  • I want to take a `long long` and convert it to a `ptime`. – Kiril Aug 11 '11 at 02:41

3 Answers3

6

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));
ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • Yes, 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 construct `ptime` by calling `from_time_t` on the result I get an incorrect date again: "2012-Oct-02 10:09:36". – Kiril Aug 11 '11 at 00:43
  • @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? – ildjarn Aug 11 '11 at 00:51
  • I want to be able to go back and forth between time in milliseconds (`long long`) and `ptime`. The time in milliseconds is offset from epoch. – Kiril Aug 11 '11 at 02:43
  • how 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? – Kiril Aug 11 '11 at 14:55
  • @Lirik : Quoting [the docs](http://www.boost.org/doc/libs/release/doc/html/date_time/details.html#date_time.buildinfo) -- "*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.*" – ildjarn Aug 11 '11 at 15:38
4

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.

Therefore
  • 367
  • 2
  • 13
1

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();
Arvid
  • 10,915
  • 1
  • 32
  • 40