Namespaces
Variants
Views
Actions

Difference between revisions of "c/chrono/ctime"

From cppreference.com
< c‎ | chrono
(_s)
m (References; language links; grammar)
Line 12: Line 12:
 
:* {{tt|buffer}} or {{tt|time}} is a null pointer
 
:* {{tt|buffer}} or {{tt|time}} is a null pointer
 
:* {{tt|bufsz}} is less than {{c|26}} or greater than {{lc|RSIZE_MAX}}
 
:* {{tt|bufsz}} is less than {{c|26}} or greater than {{lc|RSIZE_MAX}}
:As all bounds-checked functions, {{tt|ctime_s}} is only guaranteed to be available if {{c|__STDC_LIB_EXT1__}} is defined by the implementation and if the user defines {{c|__STDC_WANT_LIB_EXT1__}} to the integer constant {{c|1}} before including {{tt|time.h}}.
+
:As with all bounds-checked functions, {{tt|ctime_s}} is only guaranteed to be available if {{c|__STDC_LIB_EXT1__}} is defined by the implementation and if the user defines {{c|__STDC_WANT_LIB_EXT1__}} to the integer constant {{c|1}} before including {{tt|time.h}}.
  
 
The resulting string has the following format:
 
The resulting string has the following format:
Line 59: Line 59:
 
Wed Oct  9 10:33:09 2013
 
Wed Oct  9 10:33:09 2013
 
}}
 
}}
 +
 +
===References===
 +
{{ref std c11}}
 +
{{ref std | section=7.27.3.2  | title=The ctime function | p=393}}
 +
{{ref std | section=K.3.8.2.2 | title=The ctime_s function | p=626}}
 +
{{ref std c99}}
 +
{{ref std | section=7.23.3.2  | title=The ctime function | p=342}}
 +
{{ref std c89}}
 +
{{ref std | section= | title=}}
 +
{{ref std end}}
  
 
===See also===
 
===See also===
Line 67: Line 77:
 
{{dsc end}}
 
{{dsc end}}
  
 +
[[ar:c/chrono/ctime]]
 +
[[cs:c/chrono/ctime]]
 
[[de:c/chrono/ctime]]
 
[[de:c/chrono/ctime]]
 
[[es:c/chrono/ctime]]
 
[[es:c/chrono/ctime]]
Line 72: Line 84:
 
[[it:c/chrono/ctime]]
 
[[it:c/chrono/ctime]]
 
[[ja:c/chrono/ctime]]
 
[[ja:c/chrono/ctime]]
 +
[[ko:c/chrono/ctime]]
 +
[[pl:c/chrono/ctime]]
 
[[pt:c/chrono/ctime]]
 
[[pt:c/chrono/ctime]]
 
[[ru:c/chrono/ctime]]
 
[[ru:c/chrono/ctime]]
 +
[[tr:c/chrono/ctime]]
 
[[zh:c/chrono/ctime]]
 
[[zh:c/chrono/ctime]]

Revision as of 08:20, 18 February 2015

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)
1) Converts given time since epoch to a calendar local time and then to a textual representation, as if by calling asctime(localtime(time)).
2) Same as (1), except that the function is equivalent to 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
As with all bounds-checked functions, 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.

Contents

Parameters

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

Return value

1) pointer to a static null-terminated character string holding the textual representation of date and time. 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 (in which case the string representatino of time has been written out to the array pointed to by 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.

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 may be undefined for the values of time_t that result in the string longer than 25 characters (e.g. year 10000)

Example

#include <time.h>
#include <stdio.h>
 
int main(void)
{
    time_t result = time(NULL);
    printf("%s", ctime(&result));
}

Output:

Wed Oct  9 10:33:09 2013

References

  • 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):

See also

(deprecated in C23)(C11)
converts a tm object to a textual representation
(function) [edit]
converts a tm object to custom textual representation
(function) [edit]