# Thread: difference between 2 SYSTEMTIME's

1. ## difference between 2 SYSTEMTIME's

i have a SYSTEMTIME (timeTo) with a date/time in the future (say, a week from today), im trying to find out the difference between timeTo and the current time (timeFrom) as a fraction. something like: 1.99 days.

i read through MSDN and found out that i cant use the values in the structure:

It is not recommended that you add and subtract values from the SYSTEMTIME structure to obtain relative times. Instead, you should

· Convert the SYSTEMTIME structure to a FILETIME structure.
· Copy the resulting FILETIME structure to a LARGE_INTEGER structure.
· Use normal 64-bit arithmetic on the LARGE_INTEGER value.
i did all that, heres the code i have:

Code:
```float CalculateTimeLeft(SYSTEMTIME timeTo)
{
LARGE_INTEGER nFrom, nTo;
__int64 n64From, n64To, nDiff;
SYSTEMTIME timeFrom;
FILETIME fileTimeTemp;
float fReturn = 0.0f;

GetSystemTime(&timeFrom); // current time

SystemTimeToFileTime(&timeFrom, &fileTimeTemp);
nFrom.HighPart = fileTimeTemp.dwHighDateTime;
nFrom.LowPart = fileTimeTemp.dwLowDateTime;

n64From = (__int64)&nFrom; // timeFrom as an int

SystemTimeToFileTime(&timeTo, &fileTimeTemp);
nTo.HighPart = fileTimeTemp.dwHighDateTime;
nTo.LowPart = fileTimeTemp.dwLowDateTime;

n64To = (__int64)&nTo;  // timeTo as an int

nDiff = n64To - n64From; // difference should be positive

if (nDiff > 0) // it never makes it here
{
cout << "ndiff > 0" << endl;
fReturn = nDiff / 1000.0f / 60.0f / 60.0f / 24.0f; // not sure of the math yet
}
else
{
cout << "ndiff <= 0" << endl;
}

return fReturn;
}```
the problem is, the difference between the 2 times is never > 0, even though im 100% sure that timeTo > timeFrom.

also, this is the first time im working with LARGE_INTEGERS, so i convert them to 64-bit ints so i can use them as normal ints.

2. Use

Code:
`n64From = nFrom.QuadPart;`
Instead of casting it to a __int64.

3. that worked, thanks for the help

4. To add some details, this:
Code:
`	n64From = (__int64)&nFrom; // timeFrom as an int`
is casting the address of nFrom to an __int64. If you wanted to cast nFrom to an __int64, you would use:
Code:
`	n64From = *((__int64*) &nFrom); // timeFrom as an int`
On another note, the types long long and unsigned long long are preferred to __int64 as they are supported on many compilers and platforms.

5. n64From = *((__int64*) &nFrom); // timeFrom as an int
thanks for that, i was wondering why that didnt work for me.

anyway, heres the code with the modifications for anyone whos interested in it.
i had to add a '/ 10000.0f' to the formula because apparently FILETIME is in 100 nanosecond intervals.

Code:
```float CalculateTimeLeft(SYSTEMTIME timeTo)
{
LARGE_INTEGER nFrom, nTo;
SYSTEMTIME timeFrom;
FILETIME timeTemp;
float fReturn = 0.0f;

GetSystemTime(&timeFrom);

SystemTimeToFileTime(&timeFrom, &timeTemp);
nFrom.HighPart = timeTemp.dwHighDateTime;
nFrom.LowPart = timeTemp.dwLowDateTime;

SystemTimeToFileTime(&timeTo, &timeTemp);
nTo.HighPart = timeTemp.dwHighDateTime;
nTo.LowPart = timeTemp.dwLowDateTime;