C語(yǔ)言獲取編譯日期時(shí)間和系統(tǒng)時(shí)間
{
const int MONTH_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;
}
}
typedef struct
{
unsigned int Year;
unsigned int Month;
unsigned int Date;
unsigned int Hours;
unsigned int Minutes;
unsigned int Seconds;
}BuildDateTime;
const unsigned char MonthStr[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"};
void GetBuildDateTime(BuildDateTime *p_BuildDateTime)
{
unsigned char temp_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;
}
}
}
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;
{
}BuildDateTime;
const
void
{
}
使用方法示例:
BuildDateTime MyBuildDateTime;
GetBuildDateTime(&MyBuildDateTime);
RTC_YEAR
RTC_MONTH
RTC_DATE
RTC_HOURS
RTC_MINUTES
RTC_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ù).
評(píng)論