std::ctime(3)

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

πŸ’… std::ctime(3) β€” C++ Standard Libary πŸ’…

okay bestie, welcome to the std::ctime(3) page from the C++ Standard Libary. It’s giving β€œturn a std::time_t into a cute lil calendar string,” and we are keeping every detail exactly where it belongs. ✨


πŸŽ€ NAME πŸŽ€

std::ctime - std::ctime


πŸ¦‹ Synopsis πŸ¦‹

Defined in header <ctime>

char* ctime( const std::time_t* time );

Converts given time since epoch to a calendar local time and then to a textual representation, as if by calling std::asctime(std::localtime(time)). The resulting string has the following format:

Www Mmm dd hh:mm:ss yyyy\n
  • Www β€” the day of the week (one of Mon, Tue, Wed, Thu, Fri, Sat, Sun).
  • Mmm β€” the month (one of Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).
  • dd β€” the day of the month.
  • hh β€” hours.
  • mm β€” minutes.
  • ss β€” seconds.
  • yyyy β€” years.

The function does not support localization. 🫢


πŸ“ Parameters πŸ“

  • time β€” pointer to a std::time_t object specifying the time to print.

🫢 Return value 🫢

Pointer to a static null-terminated character string holding the textual representation of date and time. The string may be shared between std::asctime and std::ctime, and may be overwritten on each invocation of any of those functions. 😭


πŸ’€ Notes πŸ’€

This function returns a pointer to static data and is not thread-safe. In addition, it modifies the static std::tm object which may be shared with std::gmtime and std::localtime. POSIX marks this function obsolete and recommends std::strftime instead.

🚨 The behavior may be undefined for the values of std::time_t that result in the string longer than 25 characters (e.g. year 10000). Not me getting nervous at year 10000 πŸ’€

πŸ§ͺ Example πŸ§ͺ

Run this code:

// Run this code

#include <cassert>
#include <cstring>
#include <ctime>
#include <iostream>

int main()
{
    std::time_t result = std::time(nullptr);
    std::cout << std::ctime(&result);

    char buffer[32];
    std::strncpy(buffer, std::ctime(&result), 26);
    assert('\n' == buffer[std::strlen(buffer) - 1]);
    std::cout << buffer;
}

🌈 Possible output 🌈

Mon Oct 11 17:10:55 2021
Mon Oct 11 17:10:55 2021

πŸ‘€ See also πŸ‘€

  • asctime β€” converts a std::tm object to a textual representation (function)
  • strftime β€” converts a std::tm object to custom textual representation (function)
  • put_time β€” formats and outputs a date/time value according to the specified format (C++11) (function template)
  • C documentation for ctime

source/version/date vibes: http://cppreference.com Β· 2024.06.10 Β· page std::ctime(3) β€” mwah, stay safe and don’t trust static buffers πŸ’–

πŸ“¦ where this page comes from