std::asctime - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::asctime

From cppreference.com
< cpp‎ | chrono‎ | c

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

- `Www` - three-letter English abbreviated day of the week from time_ptr->tm_wday, one of `Mon`, `Tue`, `Wed`, `Thu`, `Fri`, `Sat`, `Sun`.
- `Mmm` - three-letter English abbreviated month name from time_ptr->tm_mon, one of `Jan`, `Feb`, `Mar`, `Apr`, `May`, `Jun`, `Jul`, `Aug`, `Sep`, `Oct`, `Nov`, `Dec`.
- `dd` - 2-digit day of the month from timeptr->tm_mday as if printed by sprintf using %2d
- `hh` - 2-digit hour from timeptr->tm_hour as if printed by sprintf using %.2d
- `mm` - 2-digit minute from timeptr->tm_min as if printed by sprintf using %.2d
- `ss` - 2-digit second from timeptr->tm_sec as if printed by sprintf using %.2d
- `yyyy` - 4-digit year from timeptr->tm_year + 1900 as if printed by sprintf using %4d

The behavior is undefined if any member of *time_ptr is outside its normal range

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.

The function does not support localization, and the newline character cannot be removed.

The function modifies static storage and is not thread-safe.

### [edit] Parameters

time_ptr | - | pointer to a std::tm object specifying the time to print

### [edit] 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.

### [edit] 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.

### [edit] Example

Run this code

    #include <ctime>
    #include <iostream>

    int main()
    {
        std::time_t result = std::time(NULL);
        std::cout << std::asctime(std::localtime(&result));
    }

Output:

    Tue Dec 27 16:45:52 2011

### [edit] See also

ctime | converts a `time_t` object to a textual representation (function) [edit]
strftime | converts a `tm` object to custom textual representation (function) [edit]
put_time (C++11) | formats and outputs a date/time value according to the specified format (function template) [edit]
C documentation for asctime

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/chrono/c/asctime&oldid=76278"