asctime, asctime_s - cppreference.com (original) (raw)

Defined in header <time.h>
(1)
char* asctime( const struct tm* time_ptr ); (until C23)
[[deprecated]] char* asctime( const struct tm* time_ptr ); (since C23)
errno_t asctime_s( char* buf, rsize_t bufsz, const struct tm* time_ptr ); (2) (since C11)
  1. Converts given calendar time tm to a textual representation of the following fixed 25-character form: Www Mmm dd hh:mm:ss yyyy\n

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.

This function is deprecated and should not be used in new code. (since C23)
  1. Same as (1), except that the message is written into user-provided storage buf, which is guaranteed to be null-terminated, and the following errors are detected at runtime and call the currently installed constraint handler function:

As with all bounds-checked functions, asctime_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <time.h>.

[edit] Parameters

time_ptr - pointer to a tm object specifying the time to print
buf - pointer to a user-supplied buffer at least 26 bytes in length
bufsz - size of the user-supplied buffer

[edit] Return value

  1. pointer to a static null-terminated character string holding the textual representation of date and time as described above. The string may be shared between asctime and ctime, and may be overwritten on each invocation of any of those functions.

  2. zero on success, non-zero on failure, in which case buf[0] is set to zero (unless buf is a null pointer or bufsz is zero or greater than RSIZE_MAX).

[edit] Notes

asctime returns a pointer to static data and is not thread-safe. POSIX marks this function obsolete and recommends strftime instead. The C standard also recommends strftime instead of asctime and asctime_s because strftime is more flexible and locale-sensitive.

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

#define STDC_WANT_LIB_EXT1 1 #include <stdio.h> #include <time.h>   int main(void) { struct tm tm = *localtime(&(time_t){time(NULL)}); printf("%s", asctime(&tm)); // note implicit trailing '\n'   #ifdef STDC_LIB_EXT1 char str[26]; asctime_s(str, sizeof str, &tm); printf("%s", str); #endif }

Possible output:

Tue May 26 21:51:50 2015 Tue May 26 21:51:50 2015

[edit] References

[edit] See also