Question

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?

Was it helpful?

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

OTHER TIPS

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

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)

What about sscanf?

struct tm tmVar;
char *strVar = "YYYY-MM-DD HH:MM:SS";
time_t timeVar;
if(sscanf(strVar, "%d-%d-%d %d:%d:%d", &tm.tm_year, /* the other fields */)==6)
    timeVar = mktime(&tmVar);
else
    // bad format

Here's a ready snippet when strptime is not available:

#include <stddef.h>
#include <time.h> 
#include <stdio.h> 

time_t string_to_seconds(const char *timestamp_str)
{
    struct tm tm;
    time_t seconds;
    int r;

    if (timestamp_str == NULL) {
        printf("null argument\n");
        return (time_t)-1;
    }
    r = sscanf(timestamp_str, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
    if (r != 6) {
        printf("expected %d numbers scanned in %s\n", r, timestamp_str);
        return (time_t)-1;
    }

    tm.tm_year -= 1900;
    tm.tm_mon -= 1;
    tm.tm_isdst = 0;
    seconds = mktime(&tm);
    if (seconds == (time_t)-1) {
        printf("reading time from %s failed\n", timestamp_str);
    }

    return seconds;
}

Adjust youself a string in sscanf to what you need. To ignore time zone and convert always as GMT/UTC substract a timezone (or _timezone) from seconds (timezone global is defined in time.h. DST is already ignored by zeroing tm_isdst field of tm.

Linux supports the getdate() function, which I think is more practical than calling strptime() directly. This is because the getdate() function automatically checks many formats for you. It is an equivalent to calling strptime() with various formats until the function works or all formats were tested.

// setenv() should be called only once
setenv("DATEMSK", "/usr/share/myprog/datemsk.fmt", 0);

// convert a date
struct tm * t1(getdate("2018-03-31 14:35:46"));
if(t1 == nullptr) ...handle error...
time_t date1(timegm(t1));

// convert another date
struct tm * t2(getdate("03/31/2018 14:35:46"));
if(t2 == nullptr) ...handle error...
time_t date2(timegm(t2));

Note: timegm() is similar to mktime() except that it ignores the locale and uses UTC. In most cases that's the right way to convert your dates.

The datemsk.fmt file would include at least these two formats to support the above dates:

%Y-%b-%d %H:%M:%S
%b/%d/%Y %H:%M:%S

The number of supported formats is not limited, although you may not want to have too many. It's going to be rather slow if you have too many formats. You could also dynamically manage your formats and call strptime() in a loop.

Linux also offers a getdate_r() function which is thread safe.

Man Page: http://pubs.opengroup.org/onlinepubs/7908799/xsh/getdate.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top