从Linux开始,获得自纪元以来的秒数

时间:2022-04-01 15:59:30

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呢?

3 个解决方案

#1


15  

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

你已经在使用它:std :: time(0)(别忘了#include )。但是,std :: time是否实际返回了自标准未指定的时间(C11,由C ++标准引用):

7.27.2.4 The time function

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]

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

C++11 provides time_since_epoch, but the epoch depends on the used clock. Still, you can get the number of seconds:

C ++ 11提供了time_since_epoch,但是epoch取决于使用的时钟。不过,您可以获得秒数:

#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();
}

#2


12  

In C.

time(NULL);

In C++.

std::time(0);

And the return value of time is : time_t not long long

并且时间的返回值是:time_t不长

#3


2  

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()来实现的 - 但是我没有看到让两个不同的代码完全相同的好处 - 如果你想要的话,那你就是在Windows上使用GetSystemTime()或其他类似的东西[不确定这是正确的名称,因为我在Windows上编程已经有一段时间了]

#1


15  

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

你已经在使用它:std :: time(0)(别忘了#include )。但是,std :: time是否实际返回了自标准未指定的时间(C11,由C ++标准引用):

7.27.2.4 The time function

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]

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

C++11 provides time_since_epoch, but the epoch depends on the used clock. Still, you can get the number of seconds:

C ++ 11提供了time_since_epoch,但是epoch取决于使用的时钟。不过,您可以获得秒数:

#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();
}

#2


12  

In C.

time(NULL);

In C++.

std::time(0);

And the return value of time is : time_t not long long

并且时间的返回值是:time_t不长

#3


2  

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()来实现的 - 但是我没有看到让两个不同的代码完全相同的好处 - 如果你想要的话,那你就是在Windows上使用GetSystemTime()或其他类似的东西[不确定这是正确的名称,因为我在Windows上编程已经有一段时间了]