What is the equivalence of the Java's System.currentTimeMillis()
in C?
time() function in CThis function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds. If second is not a null pointer, the returned value is also stored in the object pointed to by second.
date +"%T. %6N" returns the current time with nanoseconds rounded to the first 6 digits, which is microseconds. date +"%T. %3N" returns the current time with nanoseconds rounded to the first 3 digits, which is milliseconds.
Check time.h
, perhaps something like the gettimeofday()
function.
You can do something like
struct timeval now;
gettimeofday(&now, NULL);
Then you can extract time by getting values from now.tv_sec
and now.tv_usec
.
On Linux and other Unix-like systems, you should use clock_gettime(CLOCK_MONOTONIC). If this is not available (e.g. Linux 2.4), you can fall back to gettimeofday(). The latter has the drawback of being affected by clock adjustments.
On Windows, you can use QueryPerformanceCounter().
This code of mine abstracts all of the above into a simple interface that returns the number of milliseconds as an int64_t. Note that the millisecond values returned are intended only for relative use (e.g. timeouts), and are not relative to any particular time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With