获取程序编译时间, 用于自动生成版本号

时间:2022-03-05 00:20:01

我们写软件的,总希望程序有变动时可以自动生成版本号,不用手动去改,这样多方便啊。VS编译器就可实现这个功能。如下所示:




/*
获取程序编译时间, 用于自动生成版本号很好
__DATE__ "Mar 25 2013"
"Mar 1 2013"

__TIME__ "21:42:20"


*/
void GetProgramCompileTime(SYSTEMTIME &st)
{

TCHAR szDate[20];
ZeroMemory(szDate, sizeof(szDate));
ZeroMemory(&st, sizeof(SYSTEMTIME));
_stprintf(szDate, TEXT("%s"), __DATE__);

st.wYear = _tcstoul(szDate+7, 0, 10);

st.wDay = szDate[5] - TEXT('0');
if (szDate[4] != TEXT(' '))
{
st.wDay += (szDate[4] - TEXT('0')) * 10;
}

switch(szDate[0])
{
case TEXT('A'):
if (szDate[1] == TEXT('p'))
{
st.wMonth = 4;
}
else
{
st.wMonth = 8;
}
break;

case TEXT('D'):
st.wMonth = 12;
break;

case TEXT('F'):
st.wMonth = 2;
break;

case TEXT('J'):
if (szDate[1] == TEXT('a'))
{
st.wMonth = 1;
}
else
{
if (szDate[2] == TEXT('n'))
{
st.wMonth = 6;
}
else
{
st.wMonth = 7;
}
}
break;

case TEXT('M'):
if (szDate[2] == TEXT('r'))
{
st.wMonth = 3;
}
else
{
st.wMonth = 5;
}
break;

case TEXT('N'):
st.wMonth = 11;
break;

case TEXT('O'):
st.wMonth = 10;
break;

case TEXT('S'):
st.wMonth = 9;
break;
}

ZeroMemory(szDate, sizeof(szDate));
sprintf(szDate, TEXT("%s"), __TIME__);

st.wHour = (szDate[0] -TEXT('0')) * 10 + (szDate[1] - TEXT('0'));
st.wMinute = (szDate[3] -TEXT('0')) * 10 + (szDate[4] - TEXT('0'));
st.wSecond = (szDate[6] -TEXT('0')) * 10 + (szDate[7] - TEXT('0'));
return;
}

GetProgramCompileTime 这个函数把编译时间获取出来,保存到一个SYSTEMTIME的结构体里,这样就可以随便用啦。