C++ 如何以 Java 获取的方式获取自 1970 年以来以毫秒为单位的当前时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19555121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:57:20  来源:igfitidea点击:

How to get current timestamp in milliseconds since 1970 just the way Java gets

c++timestamp

提问by AKIWEB

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

在 Java 中,我们可以使用System.currentTimeMillis()自纪元时间以来以毫秒为单位的当前时间戳,即 -

the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

当前时间与 UTC 时间 1970 年 1 月 1 日午夜之间的差值(以毫秒为单位)。

In C++ how to get the same thing?

在 C++ 中如何得到同样的东西?

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;

This looks right or not?

这看起来对还是不对?

回答by Oz.

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

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

#include <chrono>

// ...

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

回答by Trying

use <sys/time.h>

<sys/time.h>

struct timeval tp;
gettimeofday(&tp, NULL);
long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;

refer this.

参考这个

回答by kayleeFrye_onDeck

This answer is pretty similar to Oz.'s, using <chrono>for C++ -- I didn't grab it from Oz. though...

这个答案与Oz.'s非常相似,<chrono>用于 C++——我没有从 Oz 那里获取它。尽管...

I picked up the original snippet at the bottom of this page, and slightly modified it to be a complete console app. I love using this lil' ol' thing. It's fantastic if you do a lot of scripting and need a reliable tool in Windows to get the epoch in actual milliseconds without resorting to using VB, or some less modern, less reader-friendly code.

我选择了本页底部的原始代码段,并将其稍作修改,使其成为一个完整的控制台应用程序。我喜欢使用这个小东西。如果您编写了大量脚本并且需要在 Windows 中使用可靠的工具来在实际毫秒内获得纪元,而无需使用 VB 或一些不太现代、不太友好的代码,那就太棒了。

#include <chrono>
#include <iostream>

int main() {
    unsigned __int64 now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
    std::cout << now << std::endl;
    return 0;
}

回答by rli

If using gettimeofday you have to cast to long long otherwise you will get overflows and thus not the real number of milliseconds since the epoch: long int msint = tp.tv_sec * 1000 + tp.tv_usec / 1000; will give you a number like 767990892 which is round 8 days after the epoch ;-).

如果使用 gettimeofday 你必须转换为 long long 否则你会溢出,因此不是自纪元以来的实际毫秒数:long int msint = tp.tv_sec * 1000 + tp.tv_usec / 1000; 会给你一个像 767990892 这样的数字,这是纪元后 8 天左右;-)。

int main(int argc, char* argv[])
{
    struct timeval tp;
    gettimeofday(&tp, NULL);
    long long mslong = (long long) tp.tv_sec * 1000L + tp.tv_usec / 1000; //get current timestamp in milliseconds
    std::cout << mslong << std::endl;
}

回答by Alessandro Pezzato

Since C++11 you can use std::chrono:

从 C++11 开始,您可以使用std::chrono

  • get current system time: std::chrono::system_clock::now()
  • get time since epoch: .time_since_epoch()
  • translate the underlying unit to milliseconds: duration_cast<milliseconds>(d)
  • translate std::chrono::millisecondsto integer (uint64_tto avoid overflow)
  • 获取当前系统时间: std::chrono::system_clock::now()
  • 获取自纪元以来的时间: .time_since_epoch()
  • 将底层单位转换为毫秒: duration_cast<milliseconds>(d)
  • 转换std::chrono::milliseconds为整数(uint64_t以避免溢出)
#include <chrono>
#include <cstdint>
#include <iostream>

uint64_t timeSinceEpochMillisec() {
  using namespace std::chrono;
  return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}

int main() {
  std::cout << timeSinceEpochMillisec() << std::endl;
  return 0;
}

回答by Owen

Include <ctime>and use the timefunction.

包含<ctime>并使用该time函数。