gpt4 book ai didi

c++ - 如何将日期时间 + 时间偏移量的字符串解析为 boost::posix_time::ptime?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:22:27 25 4
gpt4 key购买 nike

我有一个字符串“2011-10-20T09:30:10-05:00”

有人知道我如何使用 boost::date_time 库解析它吗?

最佳答案

好的,我找到答案了

代码(VS)

它将字符串转换为 local_date_time,但对我来说这是可以接受的:

#pragma warning(push)
#pragma warning(disable:4244)
#pragma warning(disable:4245)
#include <boost/date_time/local_time/local_time.hpp>
#pragma warning(pop)

#include <iostream>
#include <string>

int main()
{
using namespace std;
using namespace boost::local_time;

istringstream ss("2011-10-20T09:30:10-05:00");
ss.exceptions(ios_base::failbit);
local_time_input_facet* facet = new local_time_input_facet("%Y-%m-%dT%H:%M:%S%ZP");
ss.imbue(locale(ss.getloc(), facet));

local_date_time ldt(not_a_date_time);
ss >> ldt; // do the parse

std::cout <<
ldt.to_string() <<
"\noffset is: " <<
to_simple_string(ldt.zone()->base_utc_offset()) <<
std::endl;
}

也许有人会需要它

关于c++ - 如何将日期时间 + 时间偏移量的字符串解析为 boost::posix_time::ptime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8451097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com