std::timespec(3)

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

πŸ’– NAME πŸ’–

okay bestie, welcome to std::timespec(3) from the C++ Standard Libary (sic β€” their spelling, not mine 😭). The NAME section is literally:

std::timespec - std::timespec

...and yes, that's the whole thing πŸ˜­πŸ’….

🌸 Synopsis 🌸

Defined in header <ctime>:

Defined in header <ctime>
struct timespec;           (since C++17)

This icon is since C++17 ✨. It's a structure holding an interval broken down into seconds and nanoseconds. πŸ•°οΈ

πŸŽ€ Member objects πŸŽ€

  • std::time_t tv_sec β€” whole seconds β€” >= 0 ⏱️
  • long tv_nsec β€” nanoseconds β€” [0, 999999999] ⚑

The declaration order of tv_sec and tv_nsec is unspecified. πŸ™ˆ Implementation may add other data members to timespec. No cap.

🚨 Notes 🚨

The type of tv_nsec is long long on some platforms, which is currently non-conforming in C++, but is allowed in C since C23. πŸ’€ So yeah, platform vibes.

πŸ’» Example πŸ’»

The page says // Run this code πŸƒβ€β™€οΈπŸ’¨:

        #include <ctime>
        #include <iostream>

        int main()
        {
            std::timespec ts;
            std::timespec_get(&ts, TIME_UTC);
            char buff[0x80];
            std::strftime(buff, sizeof buff, "%D %T", std::gmtime(&ts.tv_sec));

        //  auto [sec, nsec] = ts; // UB: structured bindings should not be
       used because the
                                   // declaration order and data member list
       are unspecified

            std::cout << "Current time: " << buff << " (UTC)\n"
                      << "Raw timespec.tv_sec: " << ts.tv_sec << '\n'
                      << "Raw timespec.tv_nsec: " << ts.tv_nsec << '\n';
        }

✨ Possible output ✨

Current time: 04/06/23 12:03:31 (UTC)
Raw timespec.tv_sec: 1680782611
Raw timespec.tv_nsec: 678437213

πŸ‘€ See also πŸ‘€

  • timespec_get β€” returns the calendar time in seconds and nanoseconds based on a given time base (C++17) (function) πŸ”₯
  • tm β€” calendar time type (class) πŸ—“οΈ
  • C documentation for timespec πŸ“š

source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::timespec(3) β€” slay, bestie, that's the whole page πŸ’…βœ¨

πŸ“¦ where this page comes from