C将Datetime字符串转换为Epoch

是否有一个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.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...