std::time(3)

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

πŸ’… std::time(3) β€” std::time πŸ’…

okay bestie, welcome to the std::time(3) page from the C++ Standard Library docs ✨ this one is short, iconic, and very β€œwhat time is it actually tho” energy πŸ•°οΈπŸ’–


🌸 NAME 🌸

std::time β€” std::time (yes, the NAME section is just doing its thing, and we respect it πŸ’…)


✨ Synopsis ✨

Defined in header <ctime>:

std::time_t time( std::time_t* arg );

Returns the current calendar time encoded as a std::time_t object, and also stores it in the object pointed to by arg, unless arg is a null pointer. no cap 🫢


πŸŽ€ Parameters πŸŽ€

  • arg β€” pointer to a std::time_t object to store the time, or a null pointer πŸŽ€

πŸ’– Return value πŸ’–

Current calendar time encoded as std::time_t object on success, (std::time_t)(-1) on error. If arg is not null, the return value is also stored in the object pointed to by arg. periodt πŸ’–


🧚 Notes 🧚

Okay so the encoding of calendar time in std::time_t is unspecified, but most systems conform to the POSIX specification and return a value of integral type holding 86400 times the number of calendar days since the Epoch plus the number of seconds that have passed since the last midnight UTC 🌈

Most notably, POSIX time does not (and can not) take leap seconds into account, so that this integral value is not equal to the number of S.I. seconds that have passed since the epoch, but rather is reduced with the number of leap seconds that have occurred since the epoch 😭

Implementations in which std::time_t is a 32-bit signed integer (many historical implementations) fail in the year 2038. big yikes πŸ’€

πŸ“ Example πŸ“

// Run this code

#include <ctime>
#include <iostream>

int main()
{
    std::time_t result = std::time(nullptr);
    std::cout << std::asctime(std::localtime(&result))
              << result << " seconds since the Epoch\n";
}

⭐ Possible output ⭐

Wed Sep 21 10:27:52 2011
1316615272 seconds since the Epoch

πŸ”₯ See also πŸ”₯

  • timespec_get β€” returns the calendar time in seconds and nanoseconds based on a given time base (C++17) (function) ⏱️
  • localtime β€” converts time since epoch to calendar time expressed as local time (function) 🌸
  • gmtime β€” converts time since epoch to calendar time expressed as Universal Coordinated Time (function) 🌍
  • system_clock β€” wall clock time from the system-wide realtime clock (C++11) (class) πŸ‘‘
  • C documentation for time 🫢

xoxo, http://cppreference.com πŸ’– 2024.06.10 πŸ’– std::time(3) 🌈✨

πŸ“¦ where this page comes from