#include
#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
获取编译时间的C程序
本文介绍了一个简单的C程序,该程序能够读取并打印出源文件的编译时间。通过解析__DATE__和__TIME__宏,程序能够准确地输出年、月、日、小时、分钟和秒。此方法适用于需要记录编译时刻的应用场景。
3687

被折叠的 条评论
为什么被折叠?



