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 on
Here's a ready snippet when strptime is not available:
#include
#include
#include
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
.