views:

3143

answers:

3

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?

+2  A: 

Convert each part of the date/time into an integer to populate a struct tm, then convert that into a time_t using mktime.

Mark Ransom
+10  A: 

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
Kjetil Jorgensen
I knew there had to be something like strptime, but I couldn't find it. I use Microsoft 99% of the time, and they don't support it.
Mark Ransom
+1: I was thinking of strptime, but I couldn't remember the function name for it.
R. Bemrose
As a side note, if you're using Windows, see this question: http://stackoverflow.com/questions/321849/strptime-equivalent-on-windows
R. Bemrose
A: 

Here is a wired solution in c/pseudo code I just hacked together. Good luck!

char * runner = NULL;
char *string_orig = "YYYY-MM-DD HH:MM:SS";
time_t time = 0;
struct tm tmp;

use strstr(string_orig, "-") and atoi foreach

  tmp->tm_year .. 
  tmp->tm_mon  .. 
  tmp->tm_mday ..  
  tmp->tm_hour  .. 
  tmp->tm_min  .. 
  tmp->tm_sec .. 

with *runner as help

time = mktime(&tm)
merkuro