如何使用boost :: date_time从time_t获取本地日期时间?

Kla*_*aim 1 c++ datetime boost boost-date-time

我正在使用boost :: date_time并且我得到了一个time_t,它是由库使用C标准库中的time()函数生成的.

我正在寻找从那个时间开始获取当地时间的方法.我正在阅读文档,如果没有提供时区就找不到任何方法,我不知道因为它取决于机器的语言环境,我找不到任何方法从中获取一个.

我错过了什么?

ild*_*arn 12

提高::了posix_time :: from_time_t()

#include <ctime>
#include <ostream>
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/local_time_adjustor.hpp>
#include <boost/date_time/c_local_time_adjustor.hpp>

boost::posix_time::ptime local_ptime_from_utc_time_t(std::time_t const t)
{
    using boost::date_time::c_local_adjustor;
    using boost::posix_time::from_time_t;
    using boost::posix_time::ptime;
    return c_local_adjustor<ptime>::utc_to_local(from_time_t(t));
}

int main()
{
    using boost::posix_time::to_simple_string;
    using boost::posix_time::from_time_t;

    std::time_t t;
    std::time(&t); // initalize t as appropriate
    std::cout
        << "utc:   "
        << to_simple_string(from_time_t(t))
        << "\nlocal: "
        << to_simple_string(local_ptime_from_utc_time_t(t))
        << std::endl;
}
Run Code Online (Sandbox Code Playgroud)