[IAR笔记] - 自动生成编译时间 - 宏定义
- 关于编程时, 总是需要有个更改版本号, 用来标识最新修改时间和修改内容; 或者,设定日期时间时,需要一个初始默认值;
- 因为太过频繁手动修改, 感觉很不方便, 想让iar自动生成一个编译日期并写入程序中.
- 百度查到确实有这么2个宏定义
__DATE__
和 __TIME__
, 不过这2个都是字符串形式, 需要额外的转换. - 如果还需要在程序上电后自行转换, 就很多余了. 我看到有人使用宏定义转换, 这样编译后就能得到想要的数据格式.
- 我加以修改了一下, 得出下面的代码. 留作笔记, 日后有需要直接拿来用;
#ifndef _SYSTEM_TIME_H_
#define _SYSTEM_TIME_H_
#define BUILD_YEAR_CH0 (__DATE__[ 7 ])
#define BUILD_YEAR_CH1 (__DATE__[ 8 ])
#define BUILD_YEAR_CH2 (__DATE__[ 9 ])
#define BUILD_YEAR_CH3 (__DATE__[10 ])
#define BUILD_MONTH_IS_JAN (__DATE__[ 0 ] == 'J' && __DATE__[ 1 ] == 'a' && __DATE__[ 2 ] == 'n')
#define BUILD_MONTH_IS_FEB (__DATE__[ 0 ] == 'F')
#define BUILD_MONTH_IS_MAR (__DATE__[ 0 ] == 'M' && __DATE__[ 1 ] == 'a' && __DATE__[ 2 ] == 'r')
#define BUILD_MONTH_IS_APR (__DATE__[ 0 ] == 'A' && __DATE__[ 1 ] == 'p')
#define BUILD_MONTH_IS_MAY (__DATE__[ 0 ] == 'M' && __DATE__[ 1 ] == 'a' && __DATE__[ 2 ] == 'y')
#define BUILD_MONTH_IS_JUN (__DATE__[ 0 ] == 'J' && __DATE__[ 1 ] == 'u' && __DATE__[ 2 ] == 'n')
#define BUILD_MONTH_IS_JUL (__DATE__[ 0 ] == 'J' && __DATE__[ 1 ] == 'u' && __DATE__[ 2 ] == 'l')
#define BUILD_MONTH_IS_AUG (__DATE__[ 0 ] == 'A' && __DATE__[ 1 ] == 'u')
#define BUILD_MONTH_IS_SEP (__DATE__[ 0 ] == 'S')
#define BUILD_MONTH_IS_OCT (__DATE__[ 0 ] == 'O')
#define BUILD_MONTH_IS_NOV (__DATE__[ 0 ] == 'N')
#define BUILD_MONTH_IS_DEC (__DATE__[ 0 ] == 'D')
#define BUILD_MONTH_CH0 \
((BUILD_MONTH_IS_OCT || BUILD_MONTH_IS_NOV || BUILD_MONTH_IS_DEC) ? '1' : '0')
#define BUILD_MONTH_CH1 \
( \
( BUILD_MONTH_IS_JAN ) ? '1' : \
( BUILD_MONTH_IS_FEB ) ? '2' : \
( BUILD_MONTH_IS_MAR ) ? '3' : \
( BUILD_MONTH_IS_APR ) ? '4' : \
( BUILD_MONTH_IS_MAY ) ? '5' : \
( BUILD_MONTH_IS_JUN ) ? '6' : \
( BUILD_MONTH_IS_JUL ) ? '7' : \
( BUILD_MONTH_IS_AUG ) ? '8' : \
( BUILD_MONTH_IS_SEP ) ? '9' : \
( BUILD_MONTH_IS_OCT ) ? '0' : \
( BUILD_MONTH_IS_NOV ) ? '1' : \
( BUILD_MONTH_IS_DEC ) ? '2' : \
'?' \
)
#define BUILD_DAY_CH0 ((__DATE__[ 4 ] >= '0') ? (__DATE__[ 4 ]) : '0')
#define BUILD_DAY_CH1 (__DATE__[ 5 ])
#define BUILD_HOUR_CH0 (__TIME__[ 0 ])
#define BUILD_HOUR_CH1 (__TIME__[ 1 ])
#define BUILD_MIN_CH0 (__TIME__[ 3 ])
#define BUILD_MIN_CH1 (__TIME__[ 4 ])
#define BUILD_SEC_CH0 (__TIME__[ 6 ])
#define BUILD_SEC_CH1 (__TIME__[ 7 ])
#define BUILD_TIME_STRING {\
BUILD_YEAR_CH0, BUILD_YEAR_CH1, BUILD_YEAR_CH2, BUILD_YEAR_CH3, \
BUILD_MONTH_CH0, BUILD_MONTH_CH1, \
BUILD_DAY_CH0, BUILD_DAY_CH1, \
BUILD_HOUR_CH0, BUILD_HOUR_CH1, \
BUILD_MIN_CH0, BUILD_MIN_CH1, \
BUILD_SEC_CH0, BUILD_SEC_CH1, }
#define BUILD_YEAR_CH0_N (BUILD_YEAR_CH0 - '0')
#define BUILD_YEAR_CH1_N (BUILD_YEAR_CH1 - '0')
#define BUILD_YEAR_CH2_N (BUILD_YEAR_CH2 - '0')
#define BUILD_YEAR_CH3_N (BUILD_YEAR_CH3 - '0')
#define BUILD_YEAR_N (BUILD_YEAR_CH0_N * 1000 + BUILD_YEAR_CH1_N * 100 + BUILD_YEAR_CH2_N * 10 + BUILD_YEAR_CH3_N * 1)
#define BUILD_YEAR2_N (BUILD_YEAR_CH2_N * 10 + BUILD_YEAR_CH3_N * 1)
#define BUILD_MONTH_CH0_N (BUILD_MONTH_CH0 - '0')
#define BUILD_MONTH_CH1_N (BUILD_MONTH_CH1 - '0')
#define BUILD_MONTH_N (BUILD_MONTH_CH0_N * 10 + BUILD_MONTH_CH1_N * 1)
#define BUILD_DAY_CH0_N (BUILD_DAY_CH0 - '0')
#define BUILD_DAY_CH1_N (BUILD_DAY_CH1 - '0')
#define BUILD_DAY_N (BUILD_DAY_CH0_N * 10 + BUILD_DAY_CH1_N * 1)
#define BUILD_HOUR_CH0_N (BUILD_HOUR_CH0 - '0')
#define BUILD_HOUR_CH1_N (BUILD_HOUR_CH1 - '0')
#define BUILD_HOUR_N (BUILD_HOUR_CH0_N * 10 + BUILD_HOUR_CH1_N * 1)
#define BUILD_MIN_CH0_N (BUILD_MIN_CH0 - '0')
#define BUILD_MIN_CH1_N (BUILD_MIN_CH1 - '0')
#define BUILD_MIN_N (BUILD_MIN_CH0_N * 10 + BUILD_MIN_CH1_N * 1)
#define BUILD_SEC_CH0_N (BUILD_SEC_CH0 - '0')
#define BUILD_SEC_CH1_N (BUILD_SEC_CH1 - '0')
#define BUILD_SEC_N (BUILD_SEC_CH0_N * 10 + BUILD_SEC_CH1_N * 1)
#define BUILD_DATE_HEX ((BUILD_DAY_CH1_N << 4*0) | (BUILD_DAY_CH0_N << 4*1) | \
(BUILD_MONTH_CH1_N << 4*2) | (BUILD_MONTH_CH0_N << 4*3) | \
(BUILD_YEAR_CH3_N << 4*4) | (BUILD_YEAR_CH2_N << 4*5) )
#define BUILD_TIME_HEX ((BUILD_SEC_CH1_N << 4*0) | (BUILD_SEC_CH0_N << 4*1) | \
(BUILD_MIN_CH1_N << 4*2) | (BUILD_MIN_CH0_N << 4*3) | \
(BUILD_HOUR_CH1_N << 4*4) | (BUILD_HOUR_CH0_N << 4*5) )
#define BUILD_YM_HEX ((BUILD_MONTH_CH1_N << 4*0) | (BUILD_MONTH_CH0_N << 4*1) | \
(BUILD_YEAR_CH3_N << 4*2) | (BUILD_YEAR_CH2_N << 4*3) )
#define BUILD_DH_HEX ((BUILD_HOUR_CH1_N << 4*0) | (BUILD_HOUR_CH0_N << 4*1) | \
(BUILD_DAY_CH1_N << 4*2) | (BUILD_DAY_CH0_N << 4*3) )
#define BUILD_MS_HEX ((BUILD_SEC_CH1_N << 4*0) | (BUILD_SEC_CH0_N << 4*1) | \
(BUILD_MIN_CH1_N << 4*2) | (BUILD_MIN_CH0_N << 4*3) )
#endif