SysTutorials

Linux Manual Pages › Section 3

stdasctime(3)

std::asctime - std::asctime

Synopsis

Defined in header<ctime>
    char *asctime(const std::tm *time_ptr);

Converts given calendar time std::tm to a textual representation of the following fixed 25-character form: Www Mmm dd hh:mm:ss yyyy\n

Parameters

time_ptr - pointer to a std::tm 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. POSIX marks this function obsolete and recommends std::strftime instead. POSIX limits undefined behaviors only to 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 <iostream>
int main()
{
std::time_t result = std::time(nullptr);
std::cout << std::asctime(std::localtime(&result));
}

Output:

  Mon Apr 3 20:26:26 2017

See also

         converts a time_t object to a textual representation ctime (function)          converts a tm object to custom textual representation strftime (function) put_time formats and outputs a date/time value according to the specified format          (function template) (C++11)