Solved

SYSTEMTIME arithmetic

Posted on 2000-03-27
6
2,018 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 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