Sample代码如下:
#define FCT_SW_VERSION_MAJOR 2
#define FCT_SW_VERSION_MINOR 3// 定义全局变量存储编译时间和日期
static const char g_compileTime[] = __TIME__;
static const char g_compileDate[] = __DATE__;void Com_GetFctSwVersion(uint8_t *major, uint8_t *minor)
{if ((major == NULL) || (minor == NULL)) {return;}*major = FCT_SW_VERSION_MAJOR;*minor = FCT_SW_VERSION_MINOR;Uart_Log(LOG_LEVEL_INFO, "FCT SW Version: %d.%d\n", FCT_SW_VERSION_MAJOR, FCT_SW_VERSION_MINOR);return;
}int32_t Com_GetFctCompileTime(char *time, uint8_t len)
{if ((time == NULL) || (len < 6)) {Uart_Log(LOG_LEVEL_ERROR, "Invalid parameter.\n");return 1;}int year = 0;int day = 0;char month[4] = {0};uint32_t month_num;const char *month_names[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};// 解析编译日期字符串,获取年月日if (sscanf(g_compileDate, "%3s %d %d", month, &day, &year) != 3) {Uart_Log(LOG_LEVEL_ERROR, "Parse compile date failed.\n");return 2;}for (int i = 0; i < 12; i++) {if (strcmp(month, month_names[i]) == 0) {month_num = i + 1; // 月份从 1 开始break;}}// 解析编译时间字符串,获取时分秒int hour, minute, second;if (sscanf(g_compileTime, "%d:%d:%d", &hour, &minute, &second) != 3) {Uart_Log(LOG_LEVEL_ERROR, "Parse compile time failed.\n");return 3;}time[0] = (uint8_t)(year - 2000);time[1] = (uint8_t)(month_num & 0x000000ff);time[2] = (uint8_t)(day & 0x000000ff);time[3] = (uint8_t)(hour & 0x000000ff);time[4] = (uint8_t)(minute & 0x000000ff);time[5] = (uint8_t)(second & 0x000000ff);Uart_Log(LOG_LEVEL_INFO, "Compile Time: %d-%d-%d %d:%d:%d\n", time[0] + 2000, time[1], time[2], time[3], time[4], time[5]);return 0;
}