Skip to content

Instantly share code, notes, and snippets.

@krig
Created February 20, 2013 09:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krig/4994280 to your computer and use it in GitHub Desktop.
Save krig/4994280 to your computer and use it in GitHub Desktop.
Function to parse an ISO 8601 string with timezone suffix Z (UTC) to a time_t value.
#include <time.h>
/* parses only YYYY-MM-DDTHH:MM:SSZ */
time_t parseiso8601utc(const char *date) {
struct tm tt = {0};
double seconds;
if (sscanf(date, "%04d-%02d-%02dT%02d:%02d:%lfZ",
&tt.tm_year, &tt.tm_mon, &tt.tm_mday,
&tt.tm_hour, &tt.tm_min, &seconds) != 6)
return -1;
tt.tm_sec = (int) seconds;
tt.tm_mon -= 1;
tt.tm_year -= 1900;
tt.tm_isdst =-1;
return mktime(&tt) - timezone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment