Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get seconds since epoch in Linux

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?

like image 997
Boris Avatar asked Dec 25 '12 18:12

Boris


People also ask

How do I convert epoch time to seconds?

Epoch Time Difference FormulaMultiply the two dates' absolute difference by 86400 to get the Epoch Time in seconds – using the example dates above, is 319080600.

How many seconds have passed since the epoch?

At this time there are ~63828841399 seconds since year 0 (6.383*1010). Visit the homepage to convert to/from seconds since 1/1/1970.

How does Linux calculate epoch time?

Convert from human-readable date to epoch long epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.

Is epoch time in seconds?

In computing, Unix time (also known as Epoch time, Posix time, seconds since the Epoch, Unix timestamp or UNIX Epoch time) is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, excluding leap seconds. The Unix epoch is 00:00:00 UTC on 1 January 1970.


4 Answers

In C.

time(NULL);

In C++.

std::time(0);

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

like image 122
Quentin Perez Avatar answered Oct 14 '22 06:10

Quentin Perez


The Simple, Portable, and Proper Approach

#include <ctime>

long CurrentTimeInSeconds()
{
     return (long)std::time(0); //Returns UTC in Seconds
}
like image 34
Triangle4 Avatar answered Sep 23 '22 12:09

Triangle4


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();
}
like image 27
Zeta Avatar answered Oct 14 '22 06:10

Zeta


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]

like image 2
Mats Petersson Avatar answered Oct 14 '22 05:10

Mats Petersson