std::timespec_get

From cppreference.com
< cpp‎ | chrono‎ | c
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
 
C-style date and time utilities
Functions
Time manipulation
timespec_get
(C++17)
Format conversions
Constants
Types
(C++17)
 
Defined in header <ctime>
int timespec_get( std::timespec* ts, int base );
(1) (since C++17)
#define TIME_UTC /* implementation-defined */
(2) (since C++17)
1) Modifies the std::timespec object pointed to by ts to hold the current calendar time in the time base base.
2) Expands to a value suitable for use as the base argument of std::timespec_get.

Other macro constants beginning with TIME_ may be provided by the implementation to indicate additional time bases.

If base is TIME_UTC, then

  • ts->tv_sec is set to the number of seconds since an implementation defined epoch, truncated to a whole value,
  • ts->tv_nsec member is set to the integral number of nanoseconds, rounded to the resolution of the system clock.

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 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 <iostream>
#include <ctime>
 
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";
}

Possible output:

Current time: 06/24/16 20:07:42.949494132 UTC

See also

(C++17)
time in seconds and nanoseconds
(struct)
returns the current time of the system as time since epoch
(function)