std::asctime(3)
π NAME π
okay bestie, welcome to std::asctime(3) β the C++ Standard Libraryβs fixed-format date/time-to-text girlie. std::asctime is literally the function name and the whole vibe: it converts calendar time into a string. π
β¨ Synopsis β¨
Defined in header <ctime> π
char* asctime( const std::tm* time_ptr );
okay so this function converts a given calendar time std::tm to a textual representation in this fixed 25-character form: Www Mmm dd hh:mm:ss yyyy\n. π°οΈ
Wwwβ three-letter English abbreviated day of the week fromtime_ptr->tm_wday, one ofMon,Tue,Wed,Thu,Fri,Sat,Sun. ποΈMmmβ three-letter English abbreviated month name fromtime_ptr->tm_mon, one ofJan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec. πΈddβ 2-digit day of the month fromtimeptr->tm_mdayas if printed bysprintfusing%2d. β¨hhβ 2-digit hour fromtimeptr->tm_houras if printed bysprintfusing%.2d. β°mmβ 2-digit minute fromtimeptr->tm_minas if printed bysprintfusing%.2d. π°οΈssβ 2-digit second fromtimeptr->tm_secas if printed bysprintfusing%.2d. β±οΈyyyyβ 4-digit year fromtimeptr->tm_year + 1900as if printed bysprintfusing%4d. ποΈ
π¨ Undefined behavior alert: The behavior is undefined if any member of *time_ptr is outside its normal range. bestie, do not play with this. π
π¨ Another undefined behavior alert: The behavior is undefined if the calendar year indicated by time_ptr->tm_year has more than 4 digits or is less than the year 1000. literally do not. π
The function does not support localization, and the newline character cannot be removed. π
The function modifies static storage and is not thread-safe. π©
π«Ά Parameters π«Ά
time_ptrβ pointer to astd::tmobject specifying the time to print. π
π Return value π
You get back a 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. π© POSIX marks this function obsolete and recommends locale-dependent std::strftime instead. In std::locale("C") the std::strftime format string "%c\n" will be an exact match to std::asctime output, while in other locales the format string "%a %b %e %H:%M:%S %Y\n" will be a potentially closer but not always exact match. π¦
POSIX limits undefined behaviors only to the cases when the output string would be longer than 25 characters, when timeptr->tm_wday or timeptr->tm_mon are not within the expected ranges, or when timeptr->tm_year exceeds INT_MAX-1990. π
Some implementations handle timeptr->tm_mday == 0 as meaning the last day of the preceding month. π
π¦ Example π¦
// Run this code
#include <ctime>
#include <iomanip>
#include <iostream>
int main()
{
const std::time_t now = std::time(nullptr);
for (const char* localeName : {"C", "en_US.utf8", "de_DE.utf8", "ja_JP.utf8"})
{
std::cout << "locale " << localeName << ":\n" << std::left;
std::locale::global(std::locale(localeName));
std::cout << std::setw(40) << " asctime" << std::asctime(std::localtime(&now));
// strftime output for comparison:
char buf[64];
if (strftime(buf, sizeof buf, "%c\n", std::localtime(&now)))
std::cout << std::setw(40) << " strftime %c" << buf;
if (strftime(buf, sizeof buf, "%a %b %e %H:%M:%S %Y\n", std::localtime(&now)))
std::cout << std::setw(40) << " strftime %a %b %e %H:%M:%S %Y" << buf;
std::cout << '\n';
}
}
π Possible output π
locale C:
asctime Wed Nov 4 00:45:01 2020
strftime %c Wed Nov 4 00:45:01 2020
strftime %a %b %e %H:%M:%S %Y Wed Nov 4 00:45:01 2020
locale en_US.utf8:
asctime Wed Nov 4 00:45:01 2020
strftime %c Wed 04 Nov 2020 12:45:01 AM UTC
strftime %a %b %e %H:%M:%S %Y Wed Nov 4 00:45:01 2020
locale de_DE.utf8:
asctime Wed Nov 4 00:45:01 2020
strftime %c Mi 04 Nov 2020 00:45:01 UTC
strftime %a %b %e %H:%M:%S %Y Mi Nov 4 00:45:01 2020
locale ja_JP.utf8:
asctime Wed Nov 4 00:45:01 2020
strftime %c 2020εΉ΄11ζ04ζ₯ 00ζ45ε01η§
strftime %a %b %e %H:%M:%S %Y ζ°΄ 11ζ 4 00:45:01 2020
π See also π
ctimeβ converts astd::time_tobject 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
asctime. π¦
source: http://cppreference.com β¨ version/date: 2024.06.10 β¨ page: std::asctime(3) β thatβs the whole manual, bestie. mwah π
π¦ 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