Solved

SYSTEMTIME arithmetic

Posted on 2000-03-27
6
2,007 Views
Last Modified: 2013-12-03
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
Comment
Question by:chanmo
6 Comments
 

Author Comment

by:chanmo
ID: 2663637
Adjusted points from 7 to 22
0
 
LVL 9

Expert Comment

by:Pacman
ID: 2663667
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
 

Author Comment

by:chanmo
ID: 2664445
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
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.

 
LVL 1

Expert Comment

by:nutsnuts
ID: 2665195
If you are using MFC, try to use CTime class, it's quite convenient.
0
 
LVL 1

Accepted Solution

by:
JMu earned 22 total points
ID: 2665624
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
 

Author Comment

by:chanmo
ID: 2667564
Thank you very much for your help!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question