CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2005
    Posts
    5

    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

  2. #2
    Join Date
    Oct 2005
    Posts
    199

    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

  3. #3
    Join Date
    Jul 2005
    Posts
    5

    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?

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: time interval in milliseconds

    Quote 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

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Jan 2004
    Posts
    206

    Re: time interval in milliseconds

    Quote 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.

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: time interval in milliseconds

    Quote 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?..
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  8. #8
    Join Date
    Jul 2005
    Posts
    5

    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

  9. #9
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: time interval in milliseconds

    Quote 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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured