Reputation: 369
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?
Upvotes: 19
Views: 45867
Reputation: 1
Here's the ultimate solution to get seconds (as integer or as a double), or to get milliseconds or whatever you want
#include <chrono>
template <typename Duration, typename Clock>
Duration get_duration_since_epoch()
{
const auto tp = std::chrono::time_point_cast<Duration>(Clock::now());
return tp.time_since_epoch();
}
Usage:
// std::chrono::seconds since epoch
get_duration_since_epoch<std::chrono::seconds, std::chrono::system_clock>()
// get seconds since epoch as integer-type
get_duration_since_epoch<std::chrono::seconds, std::chrono::system_clock>().count()
using double_sec_t = std::chrono::duration<double, std::chrono::seconds::period>;
// double_sec_t since epoch
get_duration_since_epoch<double_sec_t, std::chrono::system_clock>()
// get seconds since epoch as double-type
get_duration_since_epoch<double_sec_t, std::chrono::system_clock>().count()
// std::chrono::milliseconds since epoch
get_duration_since_epoch<std::chrono::milliseconds, std::chrono::system_clock>()
// get milliseconds since epoch as integer-type
get_duration_since_epoch<std::chrono::milliseconds, std::chrono::system_clock>().count()
https://wandbox.org/permlink/HULwKXGyc5m0pIVQ
Upvotes: 0
Reputation:
The Simple, Portable, and Proper Approach
#include <ctime>
long CurrentTimeInSeconds()
{
return (long)std::time(0); //Returns UTC in Seconds
}
Upvotes: 2
Reputation: 105876
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
functionSynopsis
#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();
}
Upvotes: 31
Reputation: 2903
In C.
time(NULL);
In C++.
std::time(0);
And the return value of time is : time_t not long long
Upvotes: 17
Reputation: 129314
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]
Upvotes: 2