如何将日期时间转换为unix时间戳在C?

如何将日期时间转换为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