difference between 2 SYSTEMTIME's

This is a discussion on difference between 2 SYSTEMTIME's within the Windows Programming forums, part of the Platform Specific Boards category; i have a SYSTEMTIME (timeTo) with a date/time in the future (say, a week from today), im trying to find ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    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. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Use

    Code:
    n64From = nFrom.QuadPart;
    Instead of casting it to a __int64.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    that worked, thanks for the help

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    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;
    
    	if (nTo.QuadPart > nFrom.QuadPart)
    	{
    		unsigned long long nDiff = nTo.QuadPart - nFrom.QuadPart;
    		fReturn = nDiff / 10000.0f / 1000.0f / 60.0f / 60.0f / 24.0f;
    	}
    	
    	return fReturn;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 05:36 PM
  3. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  4. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  5. Replies: 6
    Last Post: 08-26-2006, 11:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21