How to get time in milliseconds since the era of boost :: posix_time :: ptime

I saw several other answers to SO that suggest that we can get the time from the era in milliseconds by subtracting the time of the era from the “other” time, but it doesn't work when I try:

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 point, diff is 1349172576, and it should be a positive number since the “other” time is 2011. Does anyone know what could be causing this? What is the right way to get milliseconds from an era?

In addition, I tried to build a ptime object from milliseconds:

ptime result = from_time_t(diff);

The result will then be: "1927-Apr-01 13:50:24," and it should be "2011-Aug-09 17: 27: 00.000." What is the catch here?

Update:

, , 2 , - # (8 /64- ) ++ (4 /32- ); .

, long long, , ( from_time_t) - : "2012--02 10:09:36".

+5
3

, , long 64 .

, 32 – a long 2147483648. ~ 1312000000000 , long , , , , .

- :

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

ptimeptime long, long long – :

// given long long ms
time_duration t = seconds(static_cast<long>(ms / 1000LL));
if (ms % 1000LL)
    t += milliseconds(static_cast<long>(ms % 1000LL));
+6

ildjarn:

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();

, .

+3

:

ptime other = time_from_string("2011-08-09 17:27:00.000");
time_t posix_time = (other - ptime(min_date_time)).total_seconds();
+1

All Articles