在 Linux 中获取自 epoch 以来的秒数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14032865/
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-06 18:07:12  来源:igfitidea点击:

Get seconds since epoch in Linux

c++linuxwindowstime

提问by Boris

Is there cross-platform solution to get seconds since epoch, for windows i use

对于我使用的 Windows,是否有跨平台解决方案来获得自纪元以来的秒数

long long NativesGetTimeInSeconds()
{
    return time (NULL);
}

But how to get on Linux?

但是如何进入 Linux 呢?

采纳答案by Zeta

You're already using it: std::time(0)(don't forget to #include <ctime>). However, whether std::timeactually returns the time since epoch isn't specified in the standard (C11, referenced by the C++ standard):

您已经在使用它了:(std::time(0)不要忘记#include <ctime>)。但是,std::time标准(C11,由 C++ 标准引用)中未指定是否实际返回自纪元以来的时间:

7.27.2.4 The timefunction

Synopsis

#include <time.h>
time_t time(time_t *timer);

Description

The time function determines the current calendar time. The encoding of the value is unspecified.[emphasis mine]

7.27.2.4time函数

概要

#include <time.h>
time_t time(time_t *timer);

描述

时间功能确定当前日历时间。 未指定值的编码。[强调我的]

For C++, C++11 and later provide time_since_epoch. However, only in C++20 and later the epoch of std::chrono::system_clockwas specified to be Unix Time, and it is unspecified and therefore possibly non-portable in previous standards.

对于 C++,C++11 及更高版本提供time_since_epoch. 然而,只有在 C++20 和更高版本中, 的时代std::chrono::system_clock才被指定为 Unix 时间,并且它是未指定的,因此在以前的标准中可能是不可移植的。

Still, on Linux the std::chrono::system_clockwill usually use Unix Time even in C++11, C++14 and C++17, so you can use the following code:

尽管如此,在 Linux 上,std::chrono::system_clock即使在 C++11、C++14 和 C++17 中,通常也会使用 Unix Time,因此您可以使用以下代码:

#include <chrono>

// make the decltype slightly easier to the eye
using seconds_t = std::chrono::seconds;

// return the same type as seconds.count() below does.
// note: C++14 makes this a lot easier.
decltype(seconds_t().count()) get_seconds_since_epoch()
{
    // get the current time
    const auto now     = std::chrono::system_clock::now();

    // transform the time into a duration since the epoch
    const auto epoch   = now.time_since_epoch();

    // cast the duration into seconds
    const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(epoch);

    // return the number of seconds
    return seconds.count();
}

回答by Mats Petersson

The native Linux function for getting time is gettimeofday()[there are some other flavours too], but that gets you the time in seconds and nanoseconds, which is more than you need, so I would suggest that you continue to use time(). [Of course, time()is implemented by calling gettimeofday()somewhere down the line - but I don't see the benefit of having two different pieces of code that does exactly the same thing - and if you wanted that, you'd be using GetSystemTime()or some such on Windows [not sure that's the right name, it's been a while since I programmed on Windows]

用于获取时间的本机 Linux 函数是gettimeofday()[还有其他一些风格],但这可以为您提供以秒和纳秒为单位的时间,这超出了您的需要,因此我建议您继续使用time(). [当然,time()是通过调用gettimeofday()某处来实现的- 但我没有看到让两段不同的代码做完全相同的事情的好处 - 如果你想要那样,你会使用GetSystemTime()或类似的Windows [不确定那是正确的名字,我已经有一段时间没有在 Windows 上编程了]

回答by Quentin Perez

In C.

在 C.

time(NULL);

In C++.

在 C++ 中。

std::time(0);

And the return value of time is : time_tnot long long

而时间的返回值为:time_tnot long long