在UNIX派生系统中,time.h中有一个名为strptime()的函数.它使用类似于scanf().
scanf()如果你知道日期的格式,你可以使用一个电话.
IE
char *dateString = "2008-12-10";
struct tm * parsedTime;
int year, month, day;
// ex: 2009-10-29
if(sscanf(dateString, "%d-%d-%d", &year, &month, &day) != EOF){
time_t rawTime;
time(&rawTime);
parsedTime = localtime(&rawTime);
// tm_year is years since 1900
parsedTime->tm_year = year - 1900;
// tm_months is months since january
parsedTime->tm_mon = month - 1;
parsedTime->tm_mday = day;
}
Run Code Online (Sandbox Code Playgroud)
除此之外,我不知道任何C99的char *到struct tm的功能.