C++ 使用spirit将日期时间字符串解析为时间值
我需要使用C++ 使用spirit将日期时间字符串解析为时间值,c++,parsing,boost-spirit,C++,Parsing,Boost Spirit,我需要使用boost::spirit将日期时间字符串(如2012-12-21 12:10:35)解析为time\t值。以下是我的代码片段: tc_ = lexeme[int_[phx::ref(tm_.tm_year)=(_1-1900)]>>'-' >>int_[phx::ref(tm_.tm_mon)=(_1-1)]>>'-' >>int_[phx::
boost::spirit
将日期时间字符串(如2012-12-21 12:10:35)解析为time\t
值。以下是我的代码片段:
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::rule
的qi
规则,tm
是类型为struct tm
的成员变量
代码可以编译,但不起作用。似乎根本没有调用mktime()
。我做错了什么?< /P> < P>你可以在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;
}
#包括
#包括
#包括
使用名称空间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});//
字符串strTmp;
strTmp=“2010-12-15 15:25:46”;
std::smatch匹配;
std::regex_搜索(strTmp,match,txt_regex);
if(正则表达式匹配(strTmp,txt正则表达式))
库特感谢安迪的编辑。我是这里的新手。我可以添加一个附件吗?我想上传一个cpp文件,让问题更清楚。