加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C将Datetime字符串转换为Epoch

发布时间:2020-12-16 03:23:13 所属栏目:百科 来源:网络整理
导读:是否有一个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 help
是否有一个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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读