gpt4 book ai didi

c++ - 用精神将日期时间字符串解析为 time_t 值

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

我需要解析像 2012-12-21 12:10:35 这样的日期时间字符串进入 time_t使用 boost::spirit 的值.这是我的代码片段:

tc_     =   lexeme[int_[phx::ref(tm_.tm_year)=(_1-1900)]>>'-'
>>int_[phx::ref(tm_.tm_mon)=(_1-1)]>>'-'
>>int_[phx::ref(tm_.tm_mday)=_1]>>+space
>>int_[phx::ref(tm_.tm_hour)=_1]>>':'
>>int_[phx::ref(tm_.tm_min)=_1]>>':'
>>int_[phx::ref(tm_.tm_sec)=_1]] [_val = (long)mktime(&tm_)];

哪里tc_qi类型规则:qi::rule<Iterator, long(), Skipper> , tm_struct tm 类型的成员变量.

代码可以编译,但无法运行。似乎mktime()根本没有被叫到。我做错了什么?

最佳答案

您可以在 C++ 11 vi 正则表达式中执行此操作。如果您的编译器足够新,这将是可移植的和标准的。

#include <iostream>
#include <string>
#include <regex>
using namespace std;

int main()
{
std::regex txt_regex("([0-9]{4})[-]([0-9]{2})[-]([0-9]{2})[ ]([0-9]{2})([:])([0-9]{2})([:])([0-9]{2})");//
string strTmp;
strTmp="2010-12-15 15:25:46";
std::smatch match;
std::regex_search( strTmp, match, txt_regex );

if(regex_match(strTmp,txt_regex))
cout<<"Ok"<<endl;
else
{
cout<<"Invalid input"<<endl;
return 0;
}
if ( match.empty() )
{
std::cout << "...no more matches" << std::endl;
return 0;
}
for ( auto x : match )
{
std::cout << "found: " << x << std::endl;
}
string str = match.suffix().str();
cout <<str <<std::endl;
return 0;
}

有了它,您可以显示要显示的字符串的不同部分,然后填充结构。

希望它能有所帮助,并像往常一样开放评论(如果有不清楚或不完整的地方)。

关于c++ - 用精神将日期时间字符串解析为 time_t 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13984204/

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