如何将日期时间转换为unix时间戳在C? [英] how to convert datetime to unix timestamp in c?

查看:441
本文介绍了如何将日期时间转换为unix时间戳在C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的情况是:我得到的日期时间格式YYYY-MM-DD HH:MM:SS与libexif。为了尽量减少成本节约,我想转换日期时间到UNIX时间戳或类似的只花费64位或32位。有没有用C任何明确的方式?

the scenario is: I get datetime in format "YYYY-MM-DD HH:MM:SS" with libexif. To minimize the saving cost, I wanna convert the datetime to unix timestamp or alike which only cost 64bit or 32bit. Is there any explicit way with c?

推荐答案

您可以尝试的 strptime 组合和 mktime

struct tm tm;
time_t epoch;
if ( strptime(timestamp, "%Y-%m-%d %H:%M:%S", &tm) != NULL )
  epoch = mktime(&tm);
else
  // badness

这篇关于如何将日期时间转换为unix时间戳在C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆