Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/chrono/c/ctime"

From cppreference.com
< cpp‎ | chrono‎ | c
m (Text replace - "<!-- ======== --> " to "")
(+example, details, consistent std::)
Line 2: Line 2:
 
{{cpp/chrono/c/sidebar}}
 
{{cpp/chrono/c/sidebar}}
 
{{ddcl | header=ctime |
 
{{ddcl | header=ctime |
char *ctime( const time_t *time );
+
char* ctime( const std::time_t* time );
 
}}
 
}}
  
Converts given time since epoch to a calendar local time and then to a textual representation. The resulting string has the following format:
+
Converts given time since epoch to a calendar local time and then to a textual representation, as if by calling {{cpp|std::asctime(std::localtime(time))}}. The resulting string has the following format:
  
 
{{source|Www Mmm dd hh:mm:ss yyyy}}
 
{{source|Www Mmm dd hh:mm:ss yyyy}}
Line 21: Line 21:
 
===Parameters===
 
===Parameters===
 
{{param list begin}}
 
{{param list begin}}
{{param list item | time | pointer to a {{rlpt|time_t}} object specifying the time to print}}
+
{{param list item | time | pointer to a {{cpp|std::time_t}} object specifying the time to print}}
 
{{param list end}}
 
{{param list end}}
  
 
===Return value===
 
===Return value===
  
pointer to a static character string. The string is shared among {{rlpf|ctime}} and {{rlpf|asctime}} and is overwritten on each invocation.
+
pointer to a static null-terminated character string holding the textual representation of date and time. The string may be shared between {{tt|std::asctime}} and {{cpp|std::ctime}}, and may be overwritten on each invocation of any of those functions.
 +
 
 +
===Notes===
 +
This function returns a pointer to static data and is not thread-safe. In addition, it modifies the static {{cpp|std::tm}} object which may be shared with {{cpp|std::gmtime}} and {{cpp|std::localtime}}. POSIX marks this function obsolete and recommends {{cpp|std::strftime}} instead.
 +
 
 +
The behavior may be undefined for the values of time_t that result in the string longer than 25 characters (e.g. year 10000)
  
 
===Example===
 
===Example===
Line 32: Line 37:
 
  |
 
  |
 
  | code=
 
  | code=
 +
#include <ctime>
 +
#include <iostream>
 +
int main()
 +
{
 +
    std::time_t result = std::time(NULL);
 +
    std::cout << std::ctime(&result);
 +
}
 
  | output=
 
  | output=
 +
Tue Dec 27 17:21:29 2011
 
}}
 
}}
  
Line 39: Line 52:
 
{{dcl list template | cpp/chrono/c/dcl list asctime}}
 
{{dcl list template | cpp/chrono/c/dcl list asctime}}
 
{{dcl list template | cpp/chrono/c/dcl list strftime}}
 
{{dcl list template | cpp/chrono/c/dcl list strftime}}
 +
{{dcl list template | cpp/io/manip/dcl list put_time}}
 
{{dcl list end}}
 
{{dcl list end}}

Revision as of 14:24, 27 December 2011

Template:cpp/chrono/c/sidebar

Defined in header <ctime>
char* ctime( const std::time_t* time );

Converts given time since epoch to a calendar local time and then to a textual representation, as if by calling Template:cpp. The resulting string has the following format:

Www Mmm dd hh:mm:ss yyyy
  • 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 Template:cpp object specifying the time to print

Return value

pointer to a static null-terminated character string holding the textual representation of date and time. The string may be shared between std::asctime and Template:cpp, and may be overwritten on each invocation of any of those functions.

Notes

This function returns a pointer to static data and is not thread-safe. In addition, it modifies the static Template:cpp object which may be shared with Template:cpp and Template:cpp. POSIX marks this function obsolete and recommends Template:cpp instead.

The behavior may be undefined for the values of time_t that result in the string longer than 25 characters (e.g. year 10000)

Example

Template:example cpp

See also

Template:cpp/chrono/c/dcl list asctimeTemplate:cpp/chrono/c/dcl list strftimeTemplate:cpp/io/manip/dcl list put time