On this page
asctime, asctime_s
Defined in header <time.h> |
||
|---|---|---|
| (1) | ||
|
(until C23) | |
|
(since C23) | |
|
(2) | (since C11) |
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 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%2dhh- 2-digit hour fromtimeptr->tm_houras if printed bysprintfusing%.2dmm- 2-digit minute fromtimeptr->tm_minas if printed bysprintfusing%.2dss- 2-digit second fromtimeptr->tm_secas if printed bysprintfusing%.2dyyyy- 4-digit year fromtimeptr->tm_year + 1900as if printed bysprintfusing%4d
*time_ptr is outside its normal range
time_ptr->tm_year has more than 4 digits or is less than the year 1000.
| This function is deprecated and should not be used in new code. | (since C23) |
buf, which is guaranteed to be null-terminated, and the following errors are detected at runtime and call the currently installed constraint handler function:
-
bufortime_ptris a null pointerbufszis less than 26 or greater thanRSIZE_MAX- not all members of
*time_ptrare within their normal ranges - the year indicated by
time_ptr->tm_yearis less than 0 or greater than 9999
-
As with all bounds-checked functions,
asctime_sonly 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 constant1before including<time.h>.
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 |
Return value
asctime and ctime, and may be overwritten on each invocation of any of those functions.
buf[0] is set to zero (unless buf is a null pointer or bufsz is zero or greater than RSIZE_MAX).
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.
Example
#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>
#include <stdio.h>
int main(void)
{
struct tm tm = *localtime(&(time_t){time(NULL)});
printf("%s", asctime(&tm));
#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
References
- C17 standard (ISO/IEC 9899:2018):
- 7.27.2.1 The asctime function (p: 287)
- K.3.8.2.1 The asctime_s function (p: 453-454)
- C11 standard (ISO/IEC 9899:2011):
- 7.27.2.1 The asctime function (p: 392-393)
- K.3.8.2.1 The asctime_s function (p: 624-625)
- C99 standard (ISO/IEC 9899:1999):
- 7.23.3.1 The asctime function (p: 341-342)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.12.3.1 The asctime function
See also
|
(deprecated in C23)(C11)
|
converts a time_t object to a textual representation (function) |
converts a tm object to custom textual representation (function) |
|
C++ documentation for asctime |
|
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/c/chrono/asctime