asctime(), asctime_r()

Updated: April 19, 2023

Convert time information to a string

Synopsis:

#include <time.h>

char* asctime( const struct tm* timeptr );

char* asctime_r( const struct tm* timeptr, 
                 char* buf );

Arguments:

timeptr
A pointer to a tm structure that contains the time that you want to convert to a string.
buf
(asctime_r() only) A buffer in which asctime_r() can store the resulting string. This buffer must be large enough to hold at least 26 characters.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The asctime() and asctime_r() functions convert the time information in the structure pointed to by timeptr into a string containing exactly 26 characters, in the form:

Tue May  7 10:40:27 2002\n\0
Note: The asctime() function places the result string in a static buffer that's reused every time you call asctime() or ctime(). Calling gmtime() or localtime() could also change the date in this static buffer.

The result string for asctime_r() is contained in the buffer pointed to by buf.

All fields have a constant width. The newline character ('\n') and a NUL character ('\0') occupy the last two positions of the string.

Returns:

A pointer to the character string result, or NULL if an error occurred.

Classification:

asctime() is ANSI, POSIX 1003.1; asctime_r() is POSIX 1003.1 OB. These functions are marked as obsolescent, and may be removed from a future version of the standard.

Table 1. asctime()
Safety:  
Cancellation point No
Interrupt handler No
Signal handler No
Thread No
Table 2. asctime_r()
Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

Caveats:

The asctime() and ctime() functions place their results in a static buffer that's reused for each call to asctime() or ctime().