stdcerr
stdcerr

Reputation: 15598

How to get seconds from boost::posix_time::ptime

First off: I'm a very new to Boost and somewhat new to C++, too. I',m working with a function where I get the following boost::posix_time::ptime time passed as an argument and I want to extract the POSIX time aka "seconds since epoch" from this as long. How do I do this?

Upvotes: 1

Views: 2010

Answers (1)

VLL
VLL

Reputation: 10155

You can convert ptime to std::tm, and convert that to time_t using mktime():

#include <ctime>
#include <boost/date_time/posix_time/posix_time.hpp>

boost::posix_time::ptime time;
std::tm time_tm = to_tm(time);
time_t posix_time = mktime(&time_tm);

As rafix07 pointed out, there is also to_time_t() in boost/date_time/posix_time/conversion.hpp, which does this conversion directly. I did not find documentation about it, but I checked the source code and it exists at least in Boost 1.66.

Upvotes: 2

Related Questions