std::timespec_get

Header: <ctime>

  1. Modifies the std::timespec object pointed to by ts to hold the current calendar time in the time base base.

# Declarations

int timespec_get( std::timespec* ts, int base );

(since C++17)

#define TIME_UTC /* implementation-defined */

(since C++17)

# Parameters

# Return value

The value of base if successful, zero otherwise.

# Notes

The POSIX function clock_gettime(CLOCK_REALTIME, ts) may also be used to populate a std::timespec with the time since the Epoch.

# Example

#include <ctime>
#include <iostream>
 
int main()
{
    std::timespec ts;
    std::timespec_get(&ts, TIME_UTC);
    char buf[100];
    std::strftime(buf, sizeof buf, "%D %T", std::gmtime(&ts.tv_sec));
    std::cout << "Current time: " << buf << '.' << ts.tv_nsec << " UTC\n";
}

# See also