在系统中使用编译时刻的时间,写了一段代码,记录如下,以供参考:
static int rtc_default_start_year = 2014;static int rtc_default_start_month = 7;
static int rtc_default_start_day = 30;
static int rtc_default_start_hour = 18;
static int rtc_default_start_minute = 18;
static int rtc_default_start_second = 18;
{
/* use compiling time */
int i;
char month_buf[16];
const unsigned char MonthStr[12][5] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov","Dec"};
pr_debug(KERN_DEBUG "__DATE__: %s\n", __DATE__);
pr_debug(KERN_DEBUG "__TIME__: %s\n", __TIME__);
sscanf(__DATE__, "%s %d %d\n", month_buf,
&rtc_default_start_day, &rtc_default_start_year);
sscanf(__TIME__, "%d:%d:%d\n", &rtc_default_start_hour,
&rtc_default_start_minute, &rtc_default_start_second);
for (i = 0; i < sizeof(MonthStr)/sizeof(MonthStr[0]); i++)
{
if (0 == strcmp(MonthStr[i], month_buf))
rtc_default_start_month = i + 1;
}
if ((i > 12) || (i <= 0))
rtc_default_start_month = 1;
printk(KERN_DEBUG "year:%d, month:%d, day:%d, hour:%d\n, minute:%d, second:%d",
rtc_default_start_year, rtc_default_start_month,
rtc_default_start_day, rtc_default_start_hour
rtc_default_start_minute, rtc_default_start_second);
}
其中的编译宏__DATE__和 __TIME__的值的格式可参考下面:
__DATE__: Aug 6 2014
__TIME__: 16:16:43