Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | chrono‎ | c
m (Example: p=true: cout `now`)
m (Notes: lc)
Line 30: Line 30:
 
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.
 
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===

Revision as of 01:52, 26 July 2021

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

Example

#include <ctime>
#include <iostream>
 
int main()
{
    std::time_t result = std::time(nullptr);
    std::cout << std::ctime(&result);
}

Possible output:

Tue Dec 27 17:21:29 2011

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