是否有一个C/C++ / STL / Boost clean方法将日期时间字符串转换为时代(以秒为单位)?
yyyy:mm:dd hh:mm:ss
解决方法
见:
Date/time conversion: string representation to time_t
和:[Boost-users] [date_time] So how come there isn’t a to_time_t helper func?
所以,显然这样的事情应该有效:
#include <boost/date_time/posix_time/posix_time.hpp> using namespace boost::posix_time; std::string ts("2002-01-20 23:59:59"); ptime t(time_from_string(ts)); ptime start(gregorian::date(1970,1,1)); time_duration dur = t - start; time_t epoch = dur.total_seconds();
但是我不认为它比Rob’s suggestion更干净:使用sscanf将数据解析成struct tm,然后调用mktime.