VariantTimeToSystemTime with milliseconds << Back



The documentation for VariantTimeToSystemTime fails to mention that the API call will ALWAYS round the millisecond part to the nearest second. For example, supposed the variant time represents April 23rd, 23:59:59.688, the SYSTEMTIME you get will be April 24th, 00:00:00.000. Let's create a version of VariantTimeToSystemTime that will preserve the millisecond part!

INT VariantTimeToSystemTimeEx(DOUBLE vDateTime, LPSYSTEMTIME lpSystemTime)
{
	DOUBLE vDate, vTime;
	INT iResult;
	int iDate, iTime;
	iDate = (int)vDateTime;
	vDate = iDate;
	vTime = vDateTime - vDate;
	if (iDate < 0) vTime = -vTime;
	iResult = VariantTimeToSystemTime(vDate, lpSystemTime);
	if (iResult)
	{
		vTime *= 864000000.0;
		iTime = (int)vTime;
		BOOL bRoundUp = (iTime % 10) >= 5; //Cheap trick to Round without using CRT
		iTime /= 10;
		if (bRoundUp) iTime++;
		lpSystemTime->wMilliseconds = iTime % 1000;
		iTime /= 1000;
		lpSystemTime->wSecond = iTime % 60;
		iTime /= 60;
		lpSystemTime->wMinute = iTime % 60;
		lpSystemTime->wHour = iTime / 60;
	}
	return iResult;
}
Free Web Hosting