我没有找到答案,所以我写了自己的日期时间解析器:
time_t timeParse(const char *string, struct tm *datetime)
{
char dn[4], mn[4], ln[4];
int d, h, m, s, y;
sscanf(string, "%s %s %d %d:%d:%d %s %d", dn, mn, &d, &h, &m, &s, ln, &y);
datetime->tm_sec = s;
datetime->tm_min = m;
datetime->tm_hour = h;
datetime->tm_mday = d;
if (!strcmp(mn, "Jan")) datetime->tm_mon = 0;
if (!strcmp(mn, "Feb")) datetime->tm_mon = 1;
if (!strcmp(mn, "Mar")) datetime->tm_mon = 2;
if (!strcmp(mn, "Apr")) datetime->tm_mon = 3;
if (!strcmp(mn, "May")) datetime->tm_mon = 4;
if (!strcmp(mn, "Jun")) datetime->tm_mon = 5;
if (!strcmp(mn, "Jul")) datetime->tm_mon = 6;
if (!strcmp(mn, "Aug")) datetime->tm_mon = 7;
if (!strcmp(mn, "Sep")) datetime->tm_mon = 8;
if (!strcmp(mn, "Oct")) datetime->tm_mon = 9;
if (!strcmp(mn, "Nov")) datetime->tm_mon = 10;
if (!strcmp(mn, "Dec")) datetime->tm_mon = 11;
datetime->tm_year = y - 1900;
return mktime(datetime);
}
我手动填写必要的tm结构
字段,mktime
会填充其余字段:tm_wday
,tm_yday
,tm_isdst
。
您应该这样调用它:
time_t t = timeParse("Thu Dec 13 23:44:45 MSK 2012", &datetime)