[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2055
  • Last Modified:

SYSTEMTIME arithmetic

I want to obtain the time difference between 2 SYSTEMTIME variables. I'm recommended to convert them to ULARGE_INTEGER before doing subtraction. But I don't know how to do ULARGE_INTEGER arithmetics. Please suggest some ways to get the SYSTEMTIME difference. I would also like to know how can I add 5 minutes to a SYSTEMTIME variable containing the current time. Thank you.
0
chanmo
Asked:
chanmo
1 Solution
 
chanmoAuthor Commented:
Adjusted points from 7 to 22
0
 
PacmanCommented:
chanmo,
a easier structure for your purpose is FILETIME.
You can convert a SYSTEMTIME-object into FILETIME with
SystemTimeToFileTime()

then cast it to LARGE_INTEGER and do your operations.
See SDK doc of FILETIME for further information
0
 
chanmoAuthor Commented:
I know I can convert SYSTEMTIME to FILETIME. My problem is I don't know how to do ULARGE_INTEGER (or LARGE_INTEGER) arithmetic operations. Is it that I should cast the member of LARGE_INTEGER to __int64 and then use the operators? When I did this to calculate the difference between 2 SYSTEMTIME differed by 5 seconds, the result was 1601/1/1 00:00:05. So I want to know whether there is a better way to obtain "5 seconds" as result. I also want to know what I should do if I want to add 5 minutes to a SYSTEMTIME returned by GetLocalTime().
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
nutsnutsCommented:
If you are using MFC, try to use CTime class, it's quite convenient.
0
 
JMuCommented:
Here is the code I used in one of my work project. Firts I wrote a function to get local time as LARGE_INTEGER.

void _GetLocalTime_LARGE_INTEGER( LARGE_INTEGER& liNow )
{
      SYSTEMTIME stNow;
      ::GetLocalTime( &stNow );

      FILETIME ftNow;
      ::SystemTimeToFileTime( &stNow, &ftNow );

      liNow.LowPart = ftNow.dwLowDateTime;
      liNow.HighPart = ftNow.dwHighDateTime;
}

Then I compared:

                  LARGE_INTEGER liNow;
                  ::_GetLocalTime_LARGE_INTEGER( liNow );

                  __int64 delay = 10;      // 1 microsecond
                  delay *= 1000;            // 1 millisecond
                  delay *= 1000;            // 1 second
                  delay *= 60;            // 1 minute
                  delay *= 20;            // 20 minutes

                  if ( sqlProblemReportTime.QuadPart + delay < liNow.QuadPart )
{
                        sqlProblemReportTime.QuadPart = 0;
}

The resolution of FileTime is 100 nano seconds.

For difference you have to convert both systemtime values to file time and then subtract earlier time from later time and compare result to 5 seconds. To calculate 5 seconds use following code:

__int64 delay = 10;      // 1 microsecond
delay *= 1000;            // 1 millisecond
delay *= 5000;            // 5 seconds

or precalculate it and use the value or use following code:

__int64 delay = 10*1000*1000*5;

Compiler will treat it as a single value.

Then
if ( difference < delay )
  ...

JMu
0
 
chanmoAuthor Commented:
Thank you very much for your help!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now