Windows にはstrptimeが存在しないようなので、特定の形式だけサポート。
手抜きなので、注意。
#include <stdio.h> #include <time.h> static time_t get_t_time_from_string(char *timebuf) { int res; int year, month, day, hour, minute, second = 0; char wday[36]; struct tm tm; if (strlen(timebuf)>36) { return 0; } /* old version */ res = sscanf(timebuf, "%d/%d/%d %d:%d", &year, &month, &day, &hour, &minute); /* format error */ if (res < 1) { return 0; } if (year > 1999) { res = sscanf(timebuf, "%d/%d/%d%s %d:%d:%d", &year, &month, &day, wday, &hour, &minute, &second); if (res < 7) { return 0; } } else { if (res < 5) { return 0; } year = year + 2000; } tm.tm_sec = second; tm.tm_min = minute; tm.tm_hour = hour; tm.tm_mday = day; tm.tm_mon = month - 1; tm.tm_year = year - 1900; tm.tm_isdst = -1; return mktime(&tm); } int main(int argc, char **argv) { time_t t1; time_t t2; struct tm *tm1; struct tm *tm2; char buf1[256]; char buf2[256]; t1 = get_t_time_from_string("02/03/13 22:59"); t2 = get_t_time_from_string("2006/08/05(土) 06:40:09"); printf("t1=%d\n", t1); printf("t2=%d\n", t2); tm1 = localtime(&t1); strftime(buf1, sizeof(buf1), "%a %Y-%m-%d %H:%M:%S %Z", tm1); printf("%s\n", buf1); tm2 = localtime(&t2); strftime(buf2, sizeof(buf2), "%a %Y-%m-%d %H:%M:%S %Z", tm2); printf("%s\n", buf2); return 0; }