| Defined in header
|
||
| (1) | ||
char* ctime( const time_t* timer ); |
(until C23) | |
| [[deprecated]] char* ctime( const time_t* timer ); |
(since C23) | |
| errno_t ctime_s( char *buf, rsize_t bufsz, const time_t* timer ); |
(2) | (since C11) |
asctime(localtime(timer)) or asctime(localtime_r(timer, &(struct tm){0}))(since C23). This function is deprecated and should not be used in new code.(since C23)asctime_s(buf, bufsz, localtime_s(timer, &(struct tm){0})), and the following errors are detected at runtime and call the currently installed constraint handler function:
-
bufortimeris a null pointer -
bufszis less than26or greater than RSIZE_MAX
-
- As with all bounds-checked functions,
ctime_sis 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 constant1before including<time.h>.
The resulting string has the following format:
Www Mmm dd hh:mm:ss yyyy\nWww- the day of the week (one ofMon,Tue,Wed,Thu,Fri,Sat,Sun).Mmm- the month (one ofJan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec).dd- the day of the monthhh- hoursmm- minutesss- secondsyyyy- years
The function does not support localization.
Parameters
| timer | - | pointer to a time_t object specifying the time to print |
| buf | - | pointer to the first element of a char array of size at least bufsz
|
| bufsz | - | max number of bytes to output, typically the size of the buffer pointed to by buf
|
Return value
ctime, and may be overwritten on each invocation of any of those functions.buf), or non-zero on failure (in which case, the terminating null character is always written to buf[0] unless buf is a null pointer or bufsz is zero or greater than RSIZE_MAX.Notes
ctime returns a pointer to static data and is not thread-safe. In addition, it modifies the static tm object which may be shared with gmtime and localtime. POSIX marks this function obsolete and recommends strftime instead. The C standard also recommends strftime instead of ctime and ctime_s because strftime is more flexible and locale-sensitive.
The behavior of ctime is undefined for the values of time_t that result in the string longer than 25 characters (e.g. year 10000).
Example
#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t result = time(NULL);
printf("%s", ctime(&result));
#ifdef __STDC_LIB_EXT1__
char str[26];
ctime_s(str,sizeof str,&result);
printf("%s", str);
#endif
}Tue May 26 21:51:03 2015
Tue May 26 21:51:03 2015References
- C17 standard (ISO/IEC 9899:2018):
- 7.27.3.2 The ctime function (p: 287-288)
- K.3.8.2.2 The ctime_s function (p: 454)
- C11 standard (ISO/IEC 9899:2011):
- 7.27.3.2 The ctime function (p: 393)
- K.3.8.2.2 The ctime_s function (p: 626)
- C99 standard (ISO/IEC 9899:1999):
- 7.23.3.2 The ctime function (p: 342)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.12.3.2 The ctime function
See also
| (deprecated in C23)(C11) |
converts a tm object to a textual representation (function) |
| converts a tm object to custom textual representation (function) | |
| C++ documentation for ctime
| |
Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.