Defined in header <time.h> | ||
---|---|---|
char* ctime( const time_t* time ); | (1) | |
errno_t ctime_s(char *buffer, rsize_t bufsz, const time_t *time); | (2) | (since C11) |
asctime(localtime(time))
.asctime_s(buffer, bufsz, localtime_s(time, &(struct tm){0}))
, and the following errors are detected at runtime and call the currently installed constraint handler function: buffer
or time
is a null pointer bufsz
is less than 26
or greater than RSIZE_MAX
ctime_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
.The resulting string has the following format:
Www Mmm dd hh:mm:ss yyyy\n
Www
- the day of the week (one of Mon
, Tue
, Wed
, Thu
, Fri
, Sat
, Sun
). Mmm
- the month (one of Jan
, Feb
, Mar
, Apr
, May
, Jun
, Jul
, Aug
, Sep
, Oct
, Nov
, Dec
). dd
- the day of the month hh
- hours mm
- minutes ss
- seconds yyyy
- years The function does not support localization.
time | - | pointer to a time_t object specifying the time to print |
buffer | - | pointer to an 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 buffer |
asctime
and ctime
, and may be overwritten on each invocation of any of those functions.buffer
), or non-zero on failure (in which case, the terminating null character is always written to buffer[0]
unless buffer
is a null pointer or bufsz
is zero or greater than RSIZE_MAX.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
may be undefined for the values of time_t that result in the string longer than 25 characters (e.g. year 10000).
#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 }
Output:
Tue May 26 21:51:03 2015 Tue May 26 21:51:03 2015
(C11) | converts a tm object to a textual representation (function) |
converts a tm object to custom textual representation (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/c/chrono/ctime