Getting milliseconds accuracy current time in Qt

qt get current time
qt time interval
qtime
qt epoch time to datetime
qt time span
qdatetime difference
pyqt5 qtime
c get current time in milliseconds

Qt documentation about QTime::currentTime() says :

Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.

But is there any way to get this time with milliseconds accuracy in windows 7?

Timer resolution may vary on different platforms and readings may not be accurate. If you need to get high-resolution, accurate timestamps on Windows 7, it provides QPC API:

https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408%28v=vs.85%29.aspx

GetSystemTimePreciseAsFileTime is claimed to provide system time with <1us resolution.

But that's only about accurate timestamp. If you need to actually do something with 1 ms latency (ex. handle an event), you need a RTOS, not a desktop clunker.

QTime Class, Correspondingly, the number of seconds or milliseconds between two times can be h, The hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) zzz, The fractional part of the second, to millisecond precision, including Furthermore, currentTime() only increases within each day; it shall drop by 24 hours each  Returns the number of milliseconds from this time to t. If t is earlier than this time, the number of milliseconds returned is negative. Because QTime measures time within a day and there are 86400 seconds in a day, the result is always between -86400000 and 86400000 ms. Returns 0 if either time is invalid.

You can use QDateTime class and convert the current time with the appropriate format:

QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss,zzz")

where 'z' corresponds to miliseconds accuracy.

QTime Class, It can read the current time from the system clock and measure a span of elapsed time. the underlying operating system; not all systems provide 1-millisecond accuracy. h, the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display​). The QTime class provides clock time functions. A QTime object contains a clock time, i.e. the number of hours, minutes, seconds, and milliseconds since midnight. It can read the current time from the system clock and measure a span of elapsed time.

you can use the functionality provided by time.h header file in C/C++.

#include <time.h> 
clock_t start, end; 
double cpu_time_used; 
int main()
{
    start = clock();
    /* Do the work. */ 
    end = clock(); 
    cpu_time_used = ((double)(end-start)/ CLOCKS_PER_SEC);
}

Qt 4.8: QTime Class Reference, It can read the current time from the system clock and measure a span of elapsed time. the underlying operating system; not all systems provide 1-millisecond accuracy. h, the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display​). This site provides the current time in milliseconds elapsed since the UNIX epoch (Jan 1, 1970) as well as in other common formats including local / UTC time comparisons. You can also convert milliseconds to date & time and the other way around.

One common way would be to scale up whatever you are doing and do it 10-100 times in a row, that way you would be able get a more accurate time reading of whatever you are doing, by dividing the result by 10-100.

But getting millisecond precise readings of your time is pretty much useless because you don't have 100% of the cpu time, which means that your readings will have much greater variance than just 1 millisecond if the OS gives another process computing time while you are doing your actions.

QTime Class, QTime currentTime ( Qt::TimeSpec ts ) accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy. It is simple because of the Private Type SYSTEMTIME. Any of the items in that type can be used. Just change tSystem.Milliseconds in the line with sRet = Format(tSystem.Milliseconds, “000”) to another value. Here are possible functions. VBA Seconds Function. This VBA Routine returns the seconds of the system time.

time in microseconds, How can i get current time in microseconds but from 1.january.0001. something as Most timers are not really accurate down to microseconds. Getting milliseconds accuracy current time in Qt. Qt documentation about QTime::currentTime() says : Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.

Getting current time with milliseconds, When you call gettimeofday it gives you the number of seconds since EPOCH too​, so you don't need to call time again. And when you use output of localtime as  Time in milliseconds using Date class: 1508484583259 Time in milliseconds using Calendar: 1508484583267 Getting time in milliseconds in Java 8: 1508484583331 2. Get Time in Milliseconds for the Given date and time. In this example, we have given a date and time and we are displaying the given time in Milliseconds.

QDateTime Class | Qt Core, This allows you to define a datetime in a named time zone adhering h, the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) zzz, the fractional part of the second, to millisecond precision,  In this post, we will discuss how to measure elapsed time of a C++ program in seconds, milliseconds, microseconds and nanoseconds using chrono library. Since C++11, the best way to measure elapsed time in C++ is by using the chrono library which deal with time.

Comments
  • Is this what you are looking for? stackoverflow.com/questions/3729169/…
  • @Donald yes something like that but I read this question and answers now and couldn't understand the solution actualy
  • at the moment I think this is the best solution, thank you
  • this is not what I'm looking for, this is something like QTime::elapsed() that wont solve my problem, I need system time in milliseconds accuracy. but thanks anyway.
  • ohh...its okk. I thought you are looking for this in C/C++.
  • thank for your response but It's not a synchronous operation and I need to calculate 1-milliseconds accurate between my data and I don't think milliseconds time accuracy need to have 100% of CPU's time. there must be a way.
  • @ATN Just remember that the delay between the data arriving and your process being notified of its arrival will be random and can be on the order of hundreds of milliseconds in bad cases. Whatever timing data you get must be processed statistically, using an appropriate error model.