This is a copy-and-paste-capable C example of the answer @amwinter posted, although I did not use sscanf()
- the *scanf()
family of functions is IMO too perverse to do robust parsing with:
(headers and error checking omitted to keep the example short enough to prevent a vertical scroll bar from getting created)
// format will be YYYYmmddHHMMSSZ
const char *notAfter = getNotAfterStringFromX509Cert( x509 );
struct tm notAfterTm = { 0 };
#ifdef _WIN32
char buffer[ 8 ];
memset( buffer, 0, sizeof( buffer ) );
strncpy( buffer, notAfter, 4 );
notAfterTm.tm_year = strtol( buffer, NULL, 10 ) - 1900;
memset( buffer, 0, sizeof( buffer ) );
strncpy( buffer, notAfter + 4, 2 );
notAfterTm.tm_mon = strtol( buffer, NULL, 10 ) - 1;
memset( buffer, 0, sizeof( buffer ) );
strncpy( buffer, notAfter + 6, 2 );
notAfterTm.tm_mday = strtol( buffer, NULL, 10 );
memset( buffer, 0, sizeof( buffer ) );
strncpy( buffer, notAfter + 8, 2 );
notAfterTm.tm_hour = strtol( buffer, NULL, 10 );
memset( buffer, 0, sizeof( buffer ) );
strncpy( buffer, notAfter + 10, 2 );
notAfterTm.tm_min = strtol( buffer, NULL, 10 );
memset( buffer, 0, sizeof( buffer ) );
strncpy( buffer, notAfter + 12, 2 );
notAfterTm.tm_sec = strtol( buffer, NULL, 10 );
time_t result = mktime( ¬AfterTm );
This is a really simple case, where the input string is in a format known exactly, so it's extremely easy to parse.