如何使用boost::date_time从time_t中获取本地日期时间
How to get a local date-time from a time_t with boost::date_time?
我正在使用boost::date_time,我得到了一个time_t,它是由一个库使用C标准库中的time()函数生成的。
我正在寻找一种从时间中获取当地时间的方法。我正在阅读文档,如果不提供时区,我找不到任何方法来做到这一点,我不知道时区,因为它取决于机器的区域设置,我也找不到从中获取时区的任何方法。
我错过了什么?
boost::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;
}
对于此任务,我将忽略boost::date_time
,只使用标准库中的localtime
(或localtime_r
,如果可用)。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- Boost Spirit,获取迭代器内部语义动作
- 如何从 boost::p osix_time::p time 获取秒数
- 如何从格式化字符串中获取 Boost::p osix_time::p time
- 如何使用"time.h"库(仅)以毫秒为单位获取当前时间?
- c++:如何用time和localtime获取实际时间