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

    Question how to extract the milliseconds

    i have been searching the net for 3hours now on how to solve my problem about how to extract the milliseconds from an input but still found no answers. sigh! . Im trying my luck here that maybe experts have an idea about this.

    the input i have is in type time_t. We know we can get the hours, minutes and seconds thru localtime call. but how about the milliseconds?

    I saw ftime, gettimeofday, clock_gettime but theyre not helping me, they all return the current time.


    can you help me?

    Thanks in advance.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: how to extract the milliseconds

    Quote Originally Posted by jp185088 View Post
    the input i have is in type time_t. We know we can get the hours, minutes and seconds thru localtime call. but how about the milliseconds?
    I don't think time_t stores milliseconds. It may be compiler dependent, but I think in most cases it only stores seconds.
    If you need more accurate time measures have a look at http://www.codeguru.com/forum/showthread.php?t=291294.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    May 2010
    Posts
    16

    Re: how to extract the milliseconds

    oh i see. it was originally in type long long then i converted it to unix time using

    Code:
    void timevmstounix( long long *vmstimein, time_t *epochout )
      {
      long long timevalue = *vmstimein;
    
      timevalue -= 0x07c95674beb4000ull;
      timevalue /= 10000000;
      *epochout = (time_t) timevalue;
      
      return;
      }
    so if time_t only holds until seconds, then timevalue above should not be divided by 10000000? am i right?

    then if i divide the timevalue by only 10000, i should get the milliseconds. is it?
    Last edited by jp185088; July 26th, 2011 at 09:33 AM.

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    Re: how to extract the milliseconds

    Use the High performance timer! This is a high resolution timer,
    that can do microseconds too!

    QueryPerformanceFrequency will return counts per second.

    PHP Code:
    int ntime=0;
    LARGE_INTEGER ntime1,ntime2;
    LARGE_INTEGER freq;
    QueryPerformanceFrequency(&freq);
    QueryPerformanceCounter(&ntime1);
    //...do workIn hero
    dosomefunction();
    QueryPerformanceCounter(&ntime2);
    //get ntime in milliseconds
    ntime = (ntime2.QuadPart-ntime1.QuadPart)/(freq.QuadPart/1000);
    //Or for microseconds:
    //ntime = (ntime2.QuadPart-ntime1.QuadPart)/(freq.QuadPart/1000000); 
    HTH,

    ahoodin
    Last edited by ahoodin; July 26th, 2011 at 11:53 AM.
    ahoodin
    To keep the plot moving, that's why.

  5. #5
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    Re: how to extract the milliseconds

    If your using windows, you can use the COleDateTime class which uses a double to represent time from some epoch. The COleDateTime class has the function GetAsSystemTime to read the date/time into a SystemTime which holds milliseconds. Unfortunately the milliseconds are not read. This is a bug that Microsoft is aware of. However, there is a fix for this if you want to go this route by way of VariantTime.

    http://www.codeproject.com/KB/dateti...WMillisec.aspx

    This system works for me.

  6. #6
    Join Date
    Mar 2001
    Posts
    2,529

    Re: how to extract the milliseconds

    My method will get you microseconds or even nanoseconds as it retrieves the value from the high-resolution performance calculator.
    It is a better method.
    Last edited by ahoodin; July 26th, 2011 at 12:43 PM.
    ahoodin
    To keep the plot moving, that's why.

  7. #7
    Join Date
    May 2010
    Posts
    16

    Thumbs up Re: how to extract the milliseconds

    thanks to all your replies. I am coding in linux 64bit. sorry i didnt mention it on my first post.

    I already got the answer to my problem. My reply to D_Drmmr as you can see solved the problem! how clever. hehe

    Hope this thread can help to anyone asking something like this soon. Thank you guys!

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how to extract the milliseconds

    Quote Originally Posted by jp185088 View Post
    thanks to all your replies. I am coding in linux 64bit. sorry i didnt mention it on my first post.


    So why did you post in the Visual C++ forum??? This forum is for Microsoft Visual C++, not Linux. Next time, post in the non-Visual C+ forum.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    May 2010
    Posts
    16

    Re: how to extract the milliseconds

    oh. maybe the moderators can help me transfer this thread to the correct forum.

    Thanks!

  10. #10
    Join Date
    Mar 2001
    Posts
    2,529

    Re: how to extract the milliseconds

    Quote Originally Posted by jp185088 View Post
    oh. maybe the moderators can help me transfer this thread to the correct forum.

    Thanks!
    The SPAM file.
    ahoodin
    To keep the plot moving, that's why.

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