C语言获取编译日期时间和系统时间
{
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;//,,
//获取编译日期、时间
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;
constunsigned
char
MonthStr[12][4]
=
{"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov","Dec"};
voidGetBuildDateTime(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__ 当前语句所在的行号, 以10进制整数标注.
__FILE__ 当前源文件的文件名, 以字符串常量标注.
__DATE__ 程序被编译的日期, 以"Mmm dd yyyy"格式的字符串标注.
__TIME__ 程序被编译的时间, 以"hh:mm:ss"格式的字符串标注, 该时间由asctime返回.
__STDC__ 如果当前编译器符合ISO标准, 那么该宏的值为1
__STDC_VERSION__ 如果当前编译器符合C89, 那么它被定义为199409L, 如果符合C99, 那么被定义为199901L.
__STDC_HOSTED__ 如果当前系统是"本地系统(hosted)", 那么它被定义为1. 本地系统表示当前系统拥有完整的标准C库.
评论