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?
Asked
Active
Viewed 1,531 times
0

stdcerr
- 13,725
- 25
- 71
- 128
-
1Use `to_time_t` function [see more](https://stackoverflow.com/questions/4461586/how-do-i-convert-boostposix-timeptime-to-time-t) – rafix07 Jun 13 '18 at 05:11
1 Answers
1
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.

VLL
- 9,634
- 1
- 29
- 54