std::ctime(3)
π
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 ofMon,Tue,Wed,Thu,Fri,Sat,Sun).Mmmβ the month (one ofJan,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 astd::time_tobject 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 astd::tmobject to a textual representation (function)strftimeβ converts astd::tmobject 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms