#include <stdio.h>#define MONTH_PER_YEAR 12 // 一年12月
#define YEAR_MONTH_DAY 20 // 年月日缓存大小
#define HOUR_MINUTES_SEC 20 // 时分秒缓存大小void GetCompileTime(void)
{const char year_month[MONTH_PER_YEAR][4] ={ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};char compile_date[YEAR_MONTH_DAY] = {0}, compile_time[HOUR_MINUTES_SEC] = {0}, i;char str_month[4] = {0};int year, month, day, hour, minutes, seconds;sprintf(compile_date, "%s", __DATE__); // "Aug 23 2016"sprintf(compile_time, "%s", __TIME__); // "10:59:19"sscanf(compile_date, "%s %d %d", str_month, &day, &year);sscanf(compile_time, "%d:%d:%d", &hour, &minutes, &seconds);for(i = 0; i < MONTH_PER_YEAR; ++i){if(strncmp(str_month, year_month[i], 3) == 0){month = i + 1;break;}}printf("Compile time is = %d-%d-%d %d:%d:%d\n", year, month, day, hour, minutes, seconds);
}int main(void)
{GetCompileTime();return 0;
}
root@libang-virtual-machine:~/test/test# gcc compile.c root@libang-virtual-machine:~/test/test# ./a.out
Compile time is = 2016-8-23 14:43:18