Solved

SYSTEMTIME arithmetic

Posted on 2000-03-27
6
1,992 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and 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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now