我有一个指向字符串的指针,(char *)作为输入。日期/时间如下所示:
星期六,2010年4月10日19:30:00
我只对日期感兴趣,而不是时间感兴趣。
我用我想要的格式创建了一个“input_facet”:
boost::date_time::date_input_facet inFmt("%a %d %b %Y");
但我不知道该怎么做。最后我想从字符串中创建一个日期对象。我很确定我的输入方面和格式是正确的,但我不知道如何使用它。
感谢。
答案 0 :(得分:3)
由于日期可能会发生时区差异,您无法始终忽略字符串的时间部分。
time_input_facet<>
.date()
方法// $ g++ *.cc -lboost_date_time && ./a.out
#include <iostream>
#include <locale>
#include <sstream>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main() {
using namespace std;
using boost::local_time::local_time_input_facet;
using boost::posix_time::ptime;
stringstream ss;
ss << "Sat, 10 Apr 2010 19:30:00";
ss.imbue(locale(locale::classic(),
new local_time_input_facet("%a, %d %b %Y " "%H:%M:%S")));
ptime t;
ss.exceptions(ios::failbit);
ss >> t;
cout << "date: " << t.date() << '\n' ;
}
运行它:
$ g++ *.cc -lboost_date_time && ./a.out
date: 2010-Apr-10