在Java中,我们可以使用System.currentTimeMillis()以纪元时间为单位获取当前时间戳,以毫秒为单位 -
当前时间与UTC时间1970年1月1日午夜之间的差值,以毫秒为单位.
在C++中如何获得相同的东西?
目前我用它来获取当前时间戳 -
struct timeval tp;
gettimeofday(&tp, NULL);
long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds
cout << ms << endl;
Run Code Online (Sandbox Code Playgroud)
看起来对不对?
我正在使用以下代码来获取C ++中的当前时间。
std::time_t t = std::time(nullptr);
std::time(&t);
std::cout << std::put_time(std::localtime(&t), "%X,");
Run Code Online (Sandbox Code Playgroud)
但是,这给了我时间HH :: MM :: SS。但是对于我的应用程序,我还想包含毫秒数。反正有使用std :: put_time获得类似HH :: MM :: SS :: msecs的东西吗?
或者在C ++程序中有几种方法可以使系统时间达到毫秒精度?