CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2010
    Posts
    75

    Time functions on windows

    Hi

    are there any windows equivalents to getimeofday() and timersub() ? i have tried GetLocalTime() but the "wSecond" is just in the range 0 to 59 ;-(

    Any suggestions

    Thanks in advanced.

  2. #2
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Time functions on windows

    "wSecond" is just in the range 0 to 59 ;-(
    what do you want ?

  3. #3
    Join Date
    Jan 2010
    Posts
    75

    Re: Time functions on windows

    i am calculating a transfer and simply just want to keep track of seconds. e.g obtain start time, then obtain time again after every recv() then do sub-second precision.

    If i use GetLocalTime(), assuming i started at eg. 1:30:59, i could receive later at 1:31:00, and end up calculating the transfer took -59 seconds...

  4. #4
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Time functions on windows

    means you want to evaluate the 1:31:00 - 1:30:59 ?

    why dont you just add 60 to the result. Here 60 + (-59) = 1sec.

    put this in a if statement while wSecond is 0

  5. #5
    Join Date
    Apr 2007
    Posts
    162

    Re: Time functions on windows

    Sounds like a perfect use for epoch time.
    Check out http://www.epochconverter.com/progra...unctions-c.php

  6. #6
    Join Date
    Jan 2010
    Posts
    75

    Re: Time functions on windows

    What about subsecond precision on windows? Are there any functions which can handle this? Like timersub() on unix?

  7. #7
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Time functions on windows

    Quote Originally Posted by RipRage View Post
    What about subsecond precision on windows? Are there any functions which can handle this? Like timersub() on unix?
    The precision of a timer call is only about 15 milliseconds. So if you want to measure one single call you probably won't get any difference. You either need to use a high precision timer which is hardware dependent or measure a multiple of the same call, e. g. 100000 times. If doing the latter you normally call GetTickCount() rather then GetLocalTime, where the result already is in milliseconds.

  8. #8
    Join Date
    Jan 2010
    Posts
    75

    Re: Time functions on windows

    Thank you for your replies.

    I have also seen QueryPerformanceCounter(), is it possible to do sub second precision using this function ? If so can anyone suggest any information on using it.

    Many thanks!

  9. #9
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Time functions on windows

    You might try to call the function passing a pointer to a LARGE_INTEGER variable.

    If the returned value is 0 your hardware doesn't support a high performance counter.

    Code:
    static LARGE_INTEGER liempty = { 0 };
    
    LARGE_INTEGER li = { 0 };
    
    if (QueryPerformanceCounter(&li) == FALSE || li == liempty)
    {
        // no performance counter available
    If the call succeeds the 'li' would get a 64-bit integer which counts the closest ticks the performance counter would support, probably in nanoseconds.

  10. #10
    Join Date
    Jan 2010
    Posts
    75

    Re: Time functions on windows

    Ah yes, that's great, many thanks!

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