如何从 boost::posix_time::ptime 获取秒数
How to get seconds from boost::posix_time::ptime
首先:我是 Boost 的新手,也是 C++ 的新手。
我正在使用一个函数,在该函数中我将以下 boost::posix_time::ptime time
作为参数传递,我想从中提取 POSIX
时间又名 "seconds since epoch" 作为 long
。我该怎么做?
您可以将 ptime
转换为 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);
正如rafix07所指出的,boost/date_time/posix_time/conversion.hpp
中也有to_time_t()
,它直接进行了这种转换。我没有找到关于它的文档,但我检查了源代码,它至少存在于 Boost 1.66 中。
首先:我是 Boost 的新手,也是 C++ 的新手。
我正在使用一个函数,在该函数中我将以下 boost::posix_time::ptime time
作为参数传递,我想从中提取 POSIX
时间又名 "seconds since epoch" 作为 long
。我该怎么做?
您可以将 ptime
转换为 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);
正如rafix07所指出的,boost/date_time/posix_time/conversion.hpp
中也有to_time_t()
,它直接进行了这种转换。我没有找到关于它的文档,但我检查了源代码,它至少存在于 Boost 1.66 中。