[C/C++] Get Compile Time in C Language|Program Print Compile Time

Print firmware compile date and time
printf("Firmware compile time:%s %s\n", __DATE__, __TIME__);
result

Firmware compile time:Feb 11 2020 19:41:48
1 The
implementation method is to use C language predefined macros.
The ANSIC standard defines predefined macros that can be used in C language:
1. __ LINE__: insert the current source code line number
in the source code 2. __ FILE __: insert the current source code file name in the source code
3. __ DATE __ : Insert the current compilation date into the source code (note that it is distinguished from the current system date)
4. __ TIME __: Insert the current compilation time into the source code (note that it is distinguished from the current system time)

The identifiers __LINE__ and __FILE__ are usually used to debug programs; the
identifiers __DATE__ and __TIME__ are usually used to add a time stamp to the compiled program to distinguish different versions of the program;
these four are Pre-compiled macro, not included in the header file
__FILE__ is the file name of the currently compiled file is a string
__TIME__ is the compilation time format of the currently compiled file is hh:mm:ss is the string
__DATE__ is the current compilation The format of the compilation date of the file is Mmm:dd:yyyy, the string
__LINE__ is the number of lines where the macro statement is called, and it is a decimal number.

Original link: https://blog.csdn.net/toopoo/article/details/104269471

void Get_Compile_Date_Base(uint8_t *Year, uint8_t *Month, uint8_t *Day)
{
	const char *pMonth[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
	const char Date[12] = __DATE__;//取编译时间
	uint8_t i;
	for(i = 0; i < 12; i++)if(memcmp(Date, pMonth[i], 3) == 0)*Month = i + 1, i = 12;
	*Year = (uint8_t)atoi(Date + 9); //Date[9]为2位年份,Date[7]为完整年份
	*Day = (uint8_t)atoi(Date + 4);
}
char g_date_buf[10];
char* Get_Compile_Date(void)
{
	uint8_t  Year, Month, Day;
	Get_Compile_Date_Base(&Year, &Month, &Day);//取编译时间
	sprintf(g_date_buf, "%02d%02d%02d", Year, Month, Day);//任意格式化
	return g_date_buf;
}

https://blog.csdn.net/zhuohui307317684/article/details/106689835

Guess you like

Origin blog.csdn.net/bandaoyu/article/details/114704021