C++计算当前时间点间隔N天的时间(不使用CTimeSpan类)

涉及到有效期的设置,需要计算N天时间间隔的时间。其实这只是个工具代码,记录下与己方便,便是与人方便。。

#define QUADPART		10000000i64

std::string CalTime(int v_nTimeSpan)
{
	char chTime[MAX_PATH] = {0};
	SYSTEMTIME Curtime;
	SYSTEMTIME OutTime;
	GetLocalTime(&Curtime);
	LONGLONG iTimeSpan = v_nTimeSpan*QUADPART*86400;
	LARGE_INTEGER fTime;
	SystemTimeToFileTime(&Curtime, (FILETIME *)&fTime);
	fTime.QuadPart -= iTimeSpan;
	FileTimeToSystemTime((FILETIME *)&fTime, &OutTime);
	sprintf(chTime, "%4d%02d%02d", OutTime.wYear, OutTime.wMonth, OutTime.wDay);
	return chTime;
}

上面代码就只计算天数间隔,若要类似CTimeSpan( LONG lDays, int nHours, int nMins, int nSecs )计算到时分秒,则传参需要采用类似CTimeSpan类的方式构造个
CArTimeSpan(  LONG lDays, int nHours, int nMins, int nSecs  );用成员变量存储对应的天时分秒
	LONGLONG		iTimeSpan = v_ArTimeSpan.m_iDays * QUADPART * 86400 + v_ArTimeSpan.m_iHours * QUADPART * 3600 
						+ v_ArTimeSpan.m_iMins * QUADPART * 60 + v_ArTimeSpan.m_iSecs * QUADPART;
采用此计算即可


你可能感兴趣的:(编程笔记,C++)