以C为单位获取当前时间(以毫秒为单位)?

use*_*454 8 c

System.currentTimeMillis()C 语言中Java的等价性是什么?

Lee*_*Lee 5

#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 中一样编写这个函数,并且它们具有相同的输出。


apn*_*ton 0

请参阅此线程:http://cboard.cprogramming.com/c-programming/51247-current-system-time-milliseconds.html

它说 time() 函数精确到秒,更深的精度需要其他库......