微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/c/111642.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐