__date__和__time__是C中内置的2个比较有用的宏,它可以返回程序编译的日期和时间。对版本控制时比较有用。本身是struct tm* 类型的,可以使用strftime()函数进行转换
printf("%s--%s",__TIME__,__DATE__);
static time_t getDateFromMacro(char const *time) {
char s_month[5];
int month, day, year;
struct tm t = {0};
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
sscanf(time, "%s %d %d", s_month, &day, &year);
month = (strstr(month_names, s_month)-month_names)/3;
t.tm_mon = month;
t.tm_mday = day;
t.tm_year = year - 1900;
t.tm_isdst = -1;
return mktime(&t);
}
转换成time_t后,就已经基本上大路了。time_t包含在time.h里
#ifndef __TIME_T
#define __TIME_T
typedef long time_t;
/* 用来保存从1970年1月1日0时0分0秒到现在时刻的秒数!
用time()这个函数获取*/
#endif
#if((__DATE__[0] == ‘J‘)&&(__DATE__[1] == ‘a‘)&&(__DATE__[2] == ‘n‘))
#define MONTHNUM 1
#elif((__DATE__[0] == ‘F‘)&&(__DATE__[1] == ‘e‘)&&(__DATE__[2] == ‘b‘))
#define MONTHNUM 2
#endif
#if((__DATE__[0] == ‘M‘)&&(__DATE__[1] == ‘a‘)&&(__DATE__[2] == ‘r‘))
#define MONTHNUM 3
#elif((__DATE__[0] == ‘A‘)&&(__DATE__[1] == ‘p‘)&&(__DATE__[2] == ‘r‘))
#define MONTHNUM 4
#endif
#if((__DATE__[0] == ‘M‘)&&(__DATE__[1] == ‘a‘)&&(__DATE__[2] == ‘y‘))
#define MONTHNUM 5
#elif((__DATE__[0] == ‘J‘)&&(__DATE__[1] == ‘u‘)&&(__DATE__[2] == ‘n‘))
#define MONTHNUM 6
#endif
#if((__DATE__[0] == ‘J‘)&&(__DATE__[1] == ‘u‘)&&(__DATE__[2] == ‘l‘))
#define MONTHNUM 7
#elif((__DATE__[0] == ‘A‘)&&(__DATE__[1] == ‘u‘)&&(__DATE__[2] == ‘g‘))
#define MONTHNUM 8
#endif
#if((__DATE__[0] == ‘S‘)&&(__DATE__[1] == ‘e‘)&&(__DATE__[2] == ‘p‘))
#define MONTHNUM 9
#elif((__DATE__[0] == ‘O‘)&&(__DATE__[1] == ‘c‘)&&(__DATE__[2] == ‘t‘))
#define MONTHNUM 0x10
#endif
#if((__DATE__[0] == ‘N‘)&&(__DATE__[1] == ‘o‘)&&(__DATE__[2] == ‘v‘))
#define MONTHNUM 0x11
#elif((__DATE__[0] == ‘D‘)&&(__DATE__[1] == ‘e‘)&&(__DATE__[2] == ‘c‘))
#define MONTHNUM 0x12
#endif
const unsigned char Version_Date[] = {
(((__DATE__[9]-‘0‘)*16)+(__DATE__[10]-‘0‘)),
MONTHNUM,
(((__DATE__[4]-‘0‘)*16)+(__DATE__[5]-‘0‘)),
};
const unsigned char Version_Time[] = {
(((__TIME__[0]-‘0‘)*16)+(__TIME__[1]-‘0‘)),
(((__TIME__[3]-‘0‘)*16)+(__TIME__[4]-‘0‘)),
(((__TIME__[6]-‘0‘)*16)+(__TIME__[7]-‘0‘)),
};