Solved

SYSTEMTIME arithmetic

Posted on 2000-03-27
6
2,003 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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