新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > C語(yǔ)言獲取編譯日期時(shí)間和系統(tǒng)時(shí)間

C語(yǔ)言獲取編譯日期時(shí)間和系統(tǒng)時(shí)間

作者: 時(shí)間:2016-11-27 來(lái)源:網(wǎng)絡(luò) 收藏
int GetCompileDateTime(char *szDateTime)
{
const intMONTH_PER_YEAR=12;
const char szEnglishMonth[MONTH_PER_YEAR][4]={ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
char szTmpDate[40]={0};
char szTmpTime[20]={0};
char szMonth[4]={0};
int iYear,iMonth,iDay,iHour,iMin,iSec;//,,

//獲取編譯日期、時(shí)間
sprintf(szTmpDate,"%s",__DATE__); //"Sep 18 2010"
sprintf(szTmpTime,"%s",__TIME__);//"10:59:19"

sscanf(szTmpDate,"%s %d %d",szMonth,&iDay,&iYear);
sscanf(szTmpTime,"%d:%d:%d",&iHour,&iMin,&iSec);

for(int i=0;MONTH_PER_YEAR;i++)
{
if(strncmp(szMonth,szEnglishMonth[i],3)==0)
{
iMonth=i+1;
break;
}
}

printf("%d,%d,%d,%d,%d,%d",iYear,iMonth,iDay,iHour,iMin,iSec);
sprintf(szDateTime,"dddddd",iYear,iMonth,iDay,iHour,iMin,iSec);
return 0;
}
typedefstruct
{
unsignedintYear;
unsignedintMonth;
unsignedintDate;
unsignedintHours;
unsignedintMinutes;
unsignedintSeconds;
}BuildDateTime;

constunsignedcharMonthStr[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

voidGetBuildDateTime(BuildDateTime*p_BuildDateTime)
{
unsignedchartemp_str[4]={0,0,0,0},i=0;

sscanf(__DATE__,"%s - M",temp_str,&(p_BuildDateTime->Date),&(p_BuildDateTime->Year));
sscanf(__TIME__,"-:-:-",&(p_BuildDateTime->Hours),&(p_BuildDateTime->Minutes),&(p_BuildDateTime->Seconds));
for(i=0;i<12;i++)
{
if(temp_str[0]==MonthStr[i][0]&&temp_str[1]==MonthStr[i][1]&&temp_str[2]==MonthStr[i][2])
{
p_BuildDateTime->Month=i+1;
break;
}
}
}
使用方法示例:
BuildDateTimeMyBuildDateTime;

GetBuildDateTime(&MyBuildDateTime);

RTC_YEAR=MyBuildDateTime.Year;
RTC_MONTH=MyBuildDateTime.Month;
RTC_DATE=MyBuildDateTime.Date;
RTC_HOURS=MyBuildDateTime.Hours;
RTC_MINUTES=MyBuildDateTime.Minutes;
RTC_SECONDS=MyBuildDateTime.Seconds;

__LINE__ 當(dāng)前語(yǔ)句所在的行號(hào), 以10進(jìn)制整數(shù)標(biāo)注.
__FILE__ 當(dāng)前源文件的文件名, 以字符串常量標(biāo)注.
__DATE__ 程序被編譯的日期, 以"Mmm dd yyyy"格式的字符串標(biāo)注.
__TIME__ 程序被編譯的時(shí)間, 以"hh:mm:ss"格式的字符串標(biāo)注, 該時(shí)間由asctime返回.

__STDC__ 如果當(dāng)前編譯器符合ISO標(biāo)準(zhǔn), 那么該宏的值為1
__STDC_VERSION__ 如果當(dāng)前編譯器符合C89, 那么它被定義為199409L, 如果符合C99, 那么被定義為199901L.

__STDC_HOSTED__ 如果當(dāng)前系統(tǒng)是"本地系統(tǒng)(hosted)", 那么它被定義為1. 本地系統(tǒng)表示當(dāng)前系統(tǒng)擁有完整的標(biāo)準(zhǔn)C庫(kù).

本文引用地址:http://m.butianyuan.cn/article/201611/322574.htm



評(píng)論


技術(shù)專區(qū)

關(guān)閉