16

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

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

But how to get on Linux?

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
Boris
  • 329
  • 2
  • 4
  • 12

4 Answers4

28

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):

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]

For C++, C++11 and later provide time_since_epoch. However, before C++20 the epoch of std::chrono::system_clock was unspecified and therefore possibly non-portable in previous standards.

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

#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();
}
KetZoomer
  • 2,701
  • 3
  • 15
  • 43
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • This is not guaranteed to give you the time since epoch. The documentation just says that this is 'usually' the case. Not portable. – Ant6n Apr 14 '14 at 18:37
  • To clarify, several years later: As of C++20, *std::chrono::system_clock* (the latter method demonstrated) is now guaranteed by the standard to be based on the Unix Epoch. – Jamin Grey Jul 18 '19 at 04:23
  • @JaminGrey Thanks for the heads up. I added a note about C++20. – Zeta Jul 18 '19 at 16:24
16

In C.

time(NULL);

In C++.

std::time(0);

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

Quentin Perez
  • 2,833
  • 20
  • 22
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]

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • There is also `clock_gettime` and reading `time(7)` man page is helpful – Basile Starynkevitch Dec 25 '12 at 22:48
  • And it still fetches nanoseconds: http://lxr.linux.no/#linux+v3.7.1/arch/x86/vdso/vclock_gettime.c#L149 called from http://lxr.linux.no/#linux+v3.7.1/arch/x86/vdso/vclock_gettime.c#L175 (assuming we're talking x86, at least) – Mats Petersson Dec 25 '12 at 22:59
2

The Simple, Portable, and Proper Approach

#include <ctime>

long CurrentTimeInSeconds()
{
     return (long)std::time(0); //Returns UTC in Seconds
}