本文介绍了如何获取当前时间戳自1970年以来的毫秒数,就是Java获得的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我们可以使用 System.currentTimeMillis()来获取当前时间戳,以毫秒为单位,从纪元时间开始 -

In Java, we can use System.currentTimeMillis() to get the current timestamp in Milliseconds since epoch time which is -

在C ++中如何获得相同的东西?

In C++ how to get the same thing?

目前我使用这个来获取当前的时间戳 -

Currently I am using this to get the current timestamp -

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;

这看起来是否正确?

推荐答案

如果您有权访问C ++ 11库,请查看库。您可以使用它来获取自Unix时代以来的毫秒数,如下所示:

If you have access to the C++ 11 libraries, check out the std::chrono library. You can use it to get the milliseconds since the Unix Epoch like this:

#include <chrono>

// ...

using namespace std::chrono;
milliseconds ms = duration_cast< milliseconds >(
    system_clock::now().time_since_epoch()
);

这篇关于如何获取当前时间戳自1970年以来的毫秒数,就是Java获得的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:43