Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | chrono‎ | c
m (Text replace - "{{example cpp" to "{{example")
m (fmt, headers sorted)
 
(12 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{cpp/title| ctime}}
+
{{cpp/title|ctime}}
{{cpp/chrono/c/sidebar}}
+
{{cpp/chrono/c/navbar}}
{{ddcl | header=ctime |
+
{{ddcl|header=ctime|
 
char* ctime( const std::time_t* time );
 
char* ctime( const std::time_t* time );
 
}}
 
}}
Line 7: Line 7:
 
Converts given time since epoch to a calendar local time and then to a textual representation, as if by calling {{c|std::asctime(std::localtime(time))}}. 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 {{c|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\n}}
  
*{{tt|Www}} - the day of the week (one of {{tt|Mon}}, {{tt|Tue}}, {{tt|Wed}}, {{tt|Thu}}, {{tt|Fri}}, {{tt|Sat}}, {{tt|Sun}}).
+
* {{tt|Www}} - the day of the week (one of {{tt|Mon}}, {{tt|Tue}}, {{tt|Wed}}, {{tt|Thu}}, {{tt|Fri}}, {{tt|Sat}}, {{tt|Sun}}).
*{{tt|Mmm}} - the month (one of {{tt|Jan}}, {{tt|Feb}}, {{tt|Mar}}, {{tt|Apr}}, {{tt|May}}, {{tt|Jun}}, {{tt|Jul}}, {{tt|Aug}}, {{tt|Sep}}, {{tt|Oct}}, {{tt|Nov}}, {{tt|Dec}}).
+
* {{tt|Mmm}} - the month (one of {{tt|Jan}}, {{tt|Feb}}, {{tt|Mar}}, {{tt|Apr}}, {{tt|May}}, {{tt|Jun}}, {{tt|Jul}}, {{tt|Aug}}, {{tt|Sep}}, {{tt|Oct}}, {{tt|Nov}}, {{tt|Dec}}).
*{{tt|dd}} - the day of the month
+
* {{tt|dd}} - the day of the month.
*{{tt|hh}} - hours
+
* {{tt|hh}} - hours.
*{{tt|mm}} - minutes
+
* {{tt|mm}} - minutes.
*{{tt|ss}} - seconds  
+
* {{tt|ss}} - seconds.
*{{tt|yyyy}} - years
+
* {{tt|yyyy}} - years.
  
 
The function does not support localization.
 
The function does not support localization.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | time | pointer to a {{c|std::time_t}} object specifying the time to print}}
+
{{par|time|pointer to a {{lc|std::time_t}} object specifying the time to print}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
 
+
Pointer to a static null-terminated character string holding the textual representation of date and time. The string may be shared between {{lc|std::asctime}} and {{tt|std::ctime}}, and may be overwritten on each invocation of any of those functions.
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 {{c|std::ctime}}, and may be overwritten on each invocation of any of those functions.
+
  
 
===Notes===
 
===Notes===
This function returns a pointer to static data and is not thread-safe. In addition, it modifies the static {{c|std::tm}} object which may be shared with {{c|std::gmtime}} and {{c|std::localtime}}. POSIX marks this function obsolete and recommends {{c|std::strftime}} instead.
+
This function returns a pointer to static data and is not thread-safe. In addition, it modifies the static {{lc|std::tm}} object which may be shared with {{lc|std::gmtime}} and {{lc|std::localtime}}. POSIX marks this function obsolete and recommends {{lc|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)
+
The behavior may be undefined for the values of {{lc|std::time_t}} that result in the string longer than 25 characters (e.g. year 10000).
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <cassert>
 +
#include <cstring>
 
#include <ctime>
 
#include <ctime>
 
#include <iostream>
 
#include <iostream>
 +
 
int main()
 
int main()
 
{
 
{
     std::time_t result = std::time(NULL);
+
     std::time_t result = std::time(nullptr);
 
     std::cout << std::ctime(&result);
 
     std::cout << std::ctime(&result);
 +
 +
    char buffer[32];
 +
    std::strncpy(buffer, std::ctime(&result), 26);
 +
    assert('\n' == buffer[std::strlen(buffer) - 1]);
 +
    std::cout << buffer;
 
}
 
}
| output=
+
|p=true
Tue Dec 27 17:21:29 2011
+
|output=
 +
Mon Oct 11 17:10:55 2021
 +
Mon Oct 11 17:10:55 2021
 
}}
 
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/chrono/c/dcl list asctime}}
+
{{dsc inc|cpp/chrono/c/dsc asctime}}
{{dcl list template | cpp/chrono/c/dcl list strftime}}
+
{{dsc inc|cpp/chrono/c/dsc strftime}}
{{dcl list template | cpp/io/manip/dcl list put_time}}
+
{{dsc inc|cpp/io/manip/dsc put_time}}
{{dcl list end}}
+
{{dsc see c|c/chrono/ctime}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 22:19, 19 August 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
C-style date and time utilities
Functions
Time manipulation
Format conversions
ctime
Constants
Types
(C++17)
 
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 std::asctime(std::localtime(time)). 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

[edit] Parameters

time - pointer to a std::time_t object specifying the time to print

[edit] 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 std::ctime, and may be overwritten on each invocation of any of those functions.

[edit] Notes

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

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

[edit] Example

#include <cassert>
#include <cstring>
#include <ctime>
#include <iostream>
 
int main()
{
    std::time_t result = std::time(nullptr);
    std::cout << std::ctime(&result);
 
    char buffer[32];
    std::strncpy(buffer, std::ctime(&result), 26);
    assert('\n' == buffer[std::strlen(buffer) - 1]);
    std::cout << buffer;
}

Possible output:

Mon Oct 11 17:10:55 2021
Mon Oct 11 17:10:55 2021

[edit] See also

converts a std::tm object to a textual representation
(function) [edit]
converts a std::tm object to custom textual representation
(function) [edit]
(C++11)
formats and outputs a date/time value according to the specified format
(function template) [edit]
C documentation for ctime