Try this. As long as the date is always well-formed, it should work...
#include "stdafx.h"
int ParseDatePart(int partNumber, char* pDate)
{
// Find the right part using space and colon as delimiters. Return -1 if we run out of parts.
int index = 0;
while (partNumber)
{
if (pDate[index] == ':' || pDate[index] == ' ') partNumber--;
if (!pDate[++index]) return -1;
}
// If the part starts with a digit, read all of the digits.
int result = 0;
while (pDate[index] >= '0' && pDate[index] <= '9')
{
result = result * 10 + pDate[index++] - '0';
if (pDate[index] < '0' || pDate[index] > '9') return result;
}
// Otherwise, read it as a month.
switch (pDate[index++])
{
case 'J':
if (pDate[index] == 'a') return 1;
if (pDate[index++] && pDate[index] == 'n') return 6;
return 7;
case 'F':
return 2;
case 'M':
if (pDate[index++] && pDate[index] == 'r') return 3;
return 5;
case 'A':
if (pDate[index] == 'p') return 4;
return 8;
case 'S':
return 9;
case 'O':
return 10;
case 'N':
return 11;
case 'D':
return 12;
}
// Return -1 if it didn't parse as either a number or a month.
return -1;
}
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("Usage: foo \r\n");
printf("Example: foo \"Date: Mon, 29 Feb 2016 12:02:00 GMT\"\r\n");
return 1;
}
char* pDate = argv[1];
int day = ParseDatePart(3, pDate);
int month = ParseDatePart(4, pDate);
int year = ParseDatePart(5, pDate);
int hour = ParseDatePart(6, pDate);
int minute = ParseDatePart(7, pDate);
int second = ParseDatePart(8, pDate);
printf("%d/%d/%d %d:%d:%d\r\n", month, day, year, hour, minute, second);
return 0;
}