std::timespec_get(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::timespec_get(3) πŸ’–

Ok bestie, this page is from the C++ Standard Libary β€” yes, that’s the spelling in the header, and we’re keeping it 😭. Manual name: std::timespec_get(3). Let’s get into it. πŸŽ€

✨ NAME ✨

std::timespec_get - std::timespec_get. Literally the name repeats, and honestly? Iconic. πŸ’…


🌸 Synopsis 🌸

Defined in header <ctime> β€” that’s where this cutie lives.

int timespec_get( std::timespec* ts, int base ); (1) (since C++17)
#define TIME_UTC /* implementation-defined */    (2) (since C++17)

Now the deets, bestie:

  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. Keep an eye out πŸ‘€

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 πŸ’«

It gives you back the value of base if successful, and zero otherwise. Simple, clean, slay. ✨


πŸ¦‹ Notes πŸ¦‹

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


πŸŽ€ Example πŸŽ€

// Run this code β€” go off, bestie:

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

Possible output:

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

🌈 See also 🌈

  • timespec β€” time in seconds and nanoseconds (C++17) (struct)
  • time β€” returns the current time of the system as time since epoch (function)
  • C documentation for timespec_get

πŸ’– That’s the whole page, bestie β€” source/version: http://cppreference.com; date: 2024.06.10; manual: std::timespec_get(3). Mwah! πŸ’–

πŸ“¦ where this page comes from