C语言 __DATE__ __TIME__ Keil IAR 编译时间格式化

库文件

#include "stdlib.h"//字符转数字int atoi (const char * str);

 

void Get_Compile_Time(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);

}

 

调用方法

uint8_t  Year, Month, Day;

char *Str;

Get_Compile_Time(&Year, &Month, &Day);//取编译时间

sprintf(Str, "V%d.%d.%d", Year, Month, Day);//任意格式化

你可能感兴趣的:(C语言 __DATE__ __TIME__ Keil IAR 编译时间格式化)