Namespaces
Variants

std::timespec_getres

From cppreference.com
< cpp | chrono | c
 
 
 
C-style date and time utilities
Functions
Time manipulation
Format conversions
(deprecated in C++26)
(deprecated in C++26)
(C++26)
Constants
Types
(C++17)
 
Defined in header <ctime>
int timespec_getres( std::timespec *ts, int base );
(since C++26)

If ts is non-null and base is supported by std::timespec_get, modifies *ts to hold the resolution of time provided by std::timespec_get for base. For each supported base, multiple calls to std::timespec_getres during the same program execution have identical results.

Parameters

ts - pointer to an object of type std::timespec
base - TIME_UTC or another nonzero integer value indicating the time base

Return value

The value of base if base is supported, zero otherwise.

Notes

The POSIX function clock_getres(clock_id, ts) may also be used to populate a std::timespec with the resolution of time identified by clock_id.

Example

#include <cstdio>
#include <ctime>

int main()
{
    char buff[128];
    std::timespec ts;

    if (const int res = std::timespec_getres(&ts, TIME_UTC); res == TIME_UTC)
    {
        std::tm timer;
        std::strftime(buff, sizeof(buff), "%D %T", std::gmtime_r(&ts.tv_sec, &timer));
        std::printf("Time resolution info: %s.%09ld UTC\n", buff, ts.tv_nsec);
    }
    else
        std::printf("TIME_UTC base is not supported.");
}

Possible output:

Time resolution info: 01/01/70 00:00:00.000000001 UTC

See also

(C++17)
time in seconds and nanoseconds
(struct)[edit]
returns the calendar time in seconds and nanoseconds based on a given time base
(function) [edit]
returns the current time of the system as time since epoch
(function) [edit]
C documentation for timespec_getres
Morty Proxy This is a proxified and sanitized view of the page, visit original site.