|
-
October 14th, 2005, 12:58 AM
#1
time interval in milliseconds
Hi ,
I need to calculate difference between two time intervals. I have to set a timer for this time interval using SetTimer(......)
iam using SYSTEMTIME,FILETIME and ULARGE_INTEGER for this.
Iam getting the current time(start time) using GetLocalTime(...) and setting the end time in SYSTEMTIME structure for 31st Dec,2005 as follows:
l_SystemTime.wDay=31;
l_SystemTime.wDayOfWeek=0;
l_SystemTime.wHour=0;
l_SystemTime.wMilliseconds=0;
l_SystemTime.wMinute=0;
l_SystemTime.wMonth=12;
l_SystemTime.wSecond=0;
l_SystemTime.wYear=2005;
i have converted both start and end times to FILETIME and then to ULARGE_INTEGER for subtracting. iam subtracting high and low parts of both the time intervals separately(or can i subtract the quadparts?) .i wanted to know if the difference here can be obtained in milli seconds, so that i can directly use this value for setting a timer. If yes, how do I extract the value as SetTimer takes the interval as UINT?
Can anyone help me on this
Regards,
Anu
-
October 14th, 2005, 02:20 AM
#2
Re: time interval in milliseconds
If you really need millisecond-resolution, use GetTickCount(). There's a reference:
http://msdn.microsoft.com/library/de...ttickcount.asp
-
October 14th, 2005, 03:44 AM
#3
Re: time interval in milliseconds
Thank u for the reply.
But the GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. I need to set the timer for a particular time. for that i have to obtain the difference between the current (local) system time and the specified time. Is there any API that can do this?
-
October 14th, 2005, 05:01 AM
#4
Re: time interval in milliseconds
 Originally Posted by allu_anuradha
Thank u for the reply.
But the GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. I need to set the timer for a particular time. for that i have to obtain the difference between the current (local) system time and the specified time. Is there any API that can do this?
No There is no API specified for this.
anyway just follow this link u will get your answere
http://www.codeproject.com/datetime/winapi_datetime_ops.asp
-
October 14th, 2005, 08:27 AM
#5
Re: time interval in milliseconds
Your first assumptions are correct (see also my example below), i.e. to get the time interval in milliseconds you may:
- use SystemTimeToFileTime to "convert" SYSTEMTIME to FILETIME;
- because it's not so "convenient" to obtain the time interval from FILETIME, copy the FILETIME members in a ULARGE_INTEGER structure;
- substract QuadPart members of ULARGE_INTEGER and obtain the time interval.
Last one is not:
- store the obtained time interval in an UINT variable; this may be and may be not possible (without loss of data) depending of UINT_MAX value (see UINT_MAX defined in LIMITS.H); for sure from this very moment its not enough for an 32-bit UINT.
Code:
// get current local time
SYSTEMTIME stNow;
::GetLocalTime(&stNow);
// set time to 01/01/2006 0:0:0 - 000
SYSTEMTIME stTo;
::ZeroMemory(&stTo, sizeof(SYSTEMTIME));
stTo.wYear = 2006;
stTo.wMonth = 1;
stTo.wDay = 1;
// convert each to FILETIME
FILETIME ftNow, ftTo;
::SystemTimeToFileTime(&stNow, &ftNow);
::SystemTimeToFileTime(&stTo, &ftTo);
// copy in FILETIME in ULARGE_INTEGER structure
ULARGE_INTEGER uliNow, uliTo;
uliNow.LowPart = ftNow.dwLowDateTime;
uliNow.HighPart = ftNow.dwHighDateTime;
uliTo.LowPart = ftTo.dwLowDateTime;
uliTo.HighPart = ftTo.dwHighDateTime;
// substract QuadPart and get time left in milliseconds
ULONGLONG ullLeft = uliTo.QuadPart - uliNow.QuadPart;
// just for testing: can we cast to UINT?
if(ullLeft <= (ULONGLONG)UINT_MAX)
{
// Maybe, but not always.
::MessageBox(NULL, _T("Fits UINT"), _T("Test"), MB_OK);
}
LPTSTR pszBuff = new TCHAR[128];
_stprintf(pszBuff, _T("Estimated time left: %I64d ms"), ullLeft);
::MessageBox(NULL, pszBuff, _T("Test"), MB_OK);
delete[] pszBuff;
BTW, why do you want to pass the time interval until 31 December 2005 in milliseconds. You want to wake up with 1 millisecond precision? 
Well, just as an ideea: Set a timer with, let's say, 1 second resolution (value 1000 for uElapse argument); in WM_TIMER handler take the current time and compare with final time; when this time is reached... "bang!". Of course some precsion is lost, but still is good.
-
October 14th, 2005, 09:44 AM
#6
Re: time interval in milliseconds
 Originally Posted by ovidiucucu
Well, just as an ideea: Set a timer with, let's say, 1 second resolution (value 1000 for uElapse argument); in WM_TIMER handler take the current time and compare with final time; when this time is reached... "bang!". Of course some precsion is lost, but still is good. 
But WM_TIMER is a low priority message. There's no guarantee that the handler will get invoked every one second.
-
October 14th, 2005, 10:01 AM
#7
Re: time interval in milliseconds
 Originally Posted by Wombat
But WM_TIMER is a low priority message. There's no guarantee that the handler will get invoked every one second. 
Correct. But what we gonna do without the hope?..
-
October 16th, 2005, 11:19 PM
#8
Re: time interval in milliseconds
Hi ovidiucucu,
I tried checking if ullLeft <= (ULONGLONG)UINT_MAX as given in your code. But that doesnt seem to work.
Regards,
Anu
-
October 17th, 2005, 01:05 AM
#9
Re: time interval in milliseconds
 Originally Posted by allu_anuradha
Hi ovidiucucu,
I tried checking if ullLeft <= (ULONGLONG)UINT_MAX as given in your code. But that doesnt seem to work.
Regards,
Anu
Doesn't work what?
I put that test in the example to simple demonstrate that you cannot store the milliseconds from now till 31 December 2005 in a UINT variable.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|