C ++将Datetime字符串转换为Epoch Cleanly [英] C++ Converting a Datetime String to Epoch Cleanly
本文介绍了C ++将Datetime字符串转换为Epoch Cleanly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有C / C ++ / STL / Boost clean方法将日期时间字符串转换为时代(以秒为单位)?
Is there a C/C++/STL/Boost clean method to convert a date time string to epoch time (in seconds)?
yyyy:mm:dd hh:mm:ss
推荐答案
p>请参阅:日期/时间转换:字符串表示为time_t
And: [Boost-users] [date_time]那么怎么没有一个to_time_t helper func?
所以,显然这样的东西应该工作:
So, apparently something like this should work:
#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的建议:使用 sscanf
来解析数据转换成 struct tm
,然后调用 mktime
。
But I don't think it's much cleaner than Rob's suggestion: use sscanf
to parse the data into a struct tm
and then call mktime
.
这篇关于C ++将Datetime字符串转换为Epoch Cleanly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文