我有以下毫秒/微秒精度字符串来解析为某种提升日期时间。
std::string cell ="20091201 00:00:04.437";
我看过有关方面的文档。像这样的东西
date_input_facet* f = new date_input_facet();
f->format("%Y%m%d %F *");
但我不知道如何使用它们。
我用从 StackOverflow 清除的代码尝试了这个程序,但我无法让毫秒显示:
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include <boost/algorithm/string.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time.hpp>
namespace bt = boost::posix_time;
const std::locale formats[] =
{
std::locale(std::locale::classic(),new bt::time_input_facet("%Y%m%d %H:%M:%S.f")),
std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d %H:%M:%S")),
std::locale(std::locale::classic(),new bt::time_input_facet("%Y/%m/%d %H:%M:%S")),
std::locale(std::locale::classic(),new bt::time_input_facet("%d.%m.%Y %H:%M:%S")),
std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d"))
};
const size_t formats_n = sizeof(formats) / sizeof(formats[0]);
std::time_t pt_to_time_t(const bt::ptime& pt)
{
bt::ptime timet_start(boost::gregorian::date(1970,1,1));
bt::time_duration diff = pt - timet_start;
return diff.ticks()/bt::time_duration::rep_type::ticks_per_second;
}
void seconds_from_epoch(const std::string& s)
{
bt::ptime pt;
for(size_t i = 0; i < formats_n; ++i)
{
std::istringstream is(s);
is.imbue(formats[i]);
is >> pt;
if(pt != bt::ptime()) break;
}
bt::time_duration td = pt.time_of_day();
long fs = td.fractional_seconds();
std::cout << " ptime is " << pt << '\n';
std::cout << " seconds from epoch are " << pt_to_time_t(pt) << " " << fs << '\n';
}
int main(int, char *argv[])
{
std::string cell ="20091201 00:00:04.437";
seconds_from_epoch(cell);
int enterAnumber;
std::
cin >> enterAnumber;
}