#include <sys/time.h>
/**
* @brief provide same output with the native function in java called
* currentTimeMillis().
*/
int64_t currentTimeMillis() {
struct timeval time;
gettimeofday(&time, NULL);
int64_t s1 = (int64_t)(time.tv_sec) * 1000;
int64_t s2 = (time.tv_usec / 1000);
return s1 + s2;
}
Run Code Online (Sandbox Code Playgroud)
我像System.currentTimeMillis()在 Java 中一样编写这个函数,并且它们具有相同的输出。
请参阅此线程:http://cboard.cprogramming.com/c-programming/51247-current-system-time-milliseconds.html
它说 time() 函数精确到秒,更深的精度需要其他库......