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

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

问题描述

场景是:我使用 libexif 获取格式为YYYY-MM-DD HH:MM:SS"的日期时间.为了最大限度地减少节省成本,我想将日期时间转换为仅花费 64 位或 32 位的 unix 时间戳等.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?

推荐答案

你可以试试 strptimemktime

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

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

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