Wednesday, August 31, 2022

[SOLVED] how to convert datetime to unix timestamp in c?

Issue

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?


Solution

You could try a combination of strptime and mktime

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


Answered By - Kjetil Joergensen
Answer Checked By - Dawn Plyler (WPSolving Volunteer)