Skip to content

Instantly share code, notes, and snippets.

@kyprizel
Created March 20, 2014 12:29
Show Gist options
  • Select an option

  • Save kyprizel/9662696 to your computer and use it in GitHub Desktop.

Select an option

Save kyprizel/9662696 to your computer and use it in GitHub Desktop.
static time_t
ASN1_GetTimeT(ASN1_TIME* time)
{
struct tm t;
const char* str = (const char*) time->data;
memset(&t, 0, sizeof(t));
if (time->length < 14) {
goto complete;
}
if (time->type == V_ASN1_UTCTIME) { /* two digit year */
t.tm_year = (str[0] - '0') * 10 + (str[1] - '0');
if (t.tm_year < 70)
t.tm_year += 100;
} else if (time->type == V_ASN1_GENERALIZEDTIME) { /* four digit year */
t.tm_year = (str[0] - '0') * 1000 + (str[1] - '0') * 100 + (str[2] - '0') * 10 + (str[3] - '0');
t.tm_year -= 1900;
}
t.tm_mon = ((str[4] - '0') * 10 + (str[5] - '0')) - 1; // -1 since January is 0 not 1.
t.tm_mday = (str[6] - '0') * 10 + (str[7] - '0');
t.tm_hour = (str[8] - '0') * 10 + (str[9] - '0');
t.tm_min = (str[10] - '0') * 10 + (str[11] - '0');
t.tm_sec = (str[12] - '0') * 10 + (str[13] - '0');
complete:
return mktime(&t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment