summaryrefslogtreecommitdiff
path: root/devdocs/c/chrono%2Fctime.html
blob: f68df0e78ff2de1cb54c56aabe069caa4767c060 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    <h1 id="firstHeading" class="firstHeading">ctime, ctime_s</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;time.h&gt;</code> </th> <th> </th> <th> </th> </tr>  <tr class="t-dcl-rev-aux"> <td></td> <td rowspan="3">(1)</td> <td></td> </tr> <tr class="t-dcl t-until-c23"> <td><pre data-language="c">char*                ctime( const time_t* timer );</pre></td>  <td><span class="t-mark-rev t-until-c23">(until C23)</span></td> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">[[deprecated]] char* ctime( const time_t* timer );</pre>
</td>  <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr>  <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">errno_t ctime_s( char *buf, rsize_t bufsz, const time_t* timer );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr>  </table> <div class="t-li1">
<span class="t-li">1)</span> Converts given time since epoch to a calendar local time and then to a textual representation, as if by calling <code><a href="http://en.cppreference.com/w/c/chrono/asctime"><span class="kw515">asctime</span></a><span class="br0">(</span><a href="http://en.cppreference.com/w/c/chrono/localtime"><span class="kw519">localtime</span></a><span class="br0">(</span>timer<span class="br0">)</span><span class="br0">)</span></code><span class="t-rev-inl t-since-c23"><span> or <code><a href="http://en.cppreference.com/w/c/chrono/asctime"><span class="kw515">asctime</span></a><span class="br0">(</span>localtime_r<span class="br0">(</span>timer, <span class="sy3">&amp;</span><span class="br0">(</span><span class="kw1">struct</span> <a href="http://en.cppreference.com/w/c/chrono/tm"><span class="kw522">tm</span></a><span class="br0">)</span><span class="br0">{</span><span class="nu0">0</span><span class="br0">}</span><span class="br0">)</span><span class="br0">)</span></code></span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span>. <span class="t-rev-inl t-since-c23"><span>This function is deprecated and should not be used in new code.</span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span>
</div> <div class="t-li1">
<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that the function is equivalent to <code>asctime_s<span class="br0">(</span>buf, bufsz, localtime_s<span class="br0">(</span>timer, <span class="sy3">&amp;</span><span class="br0">(</span><span class="kw1">struct</span> <a href="http://en.cppreference.com/w/c/chrono/tm"><span class="kw522">tm</span></a><span class="br0">)</span><span class="br0">{</span><span class="nu0">0</span><span class="br0">}</span><span class="br0">)</span><span class="br0">)</span></code>, and the following errors are detected at runtime and call the currently installed <a href="../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function: <dl>
<dd>
<ul>
<li> <code>buf</code> or <code>timer</code> is a null pointer </li>
<li> <code>bufsz</code> is less than <code>26</code> or greater than <code>RSIZE_MAX</code> </li>
</ul> </dd>
<dd>As with all bounds-checked functions, <code>ctime_s</code> only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../chrono" title="c/chrono"><code>&lt;time.h&gt;</code></a>.</dd>
</dl>
</div> <p>The resulting string has the following format:</p>
<div class="c source-c"><pre data-language="c">Www Mmm dd hh:mm:ss yyyy\n</pre></div> <ul>
<li>
<code>Www</code> - the day of the week (one of <code>Mon</code>, <code>Tue</code>, <code>Wed</code>, <code>Thu</code>, <code>Fri</code>, <code>Sat</code>, <code>Sun</code>). </li>
<li>
<code>Mmm</code> - the month (one of <code>Jan</code>, <code>Feb</code>, <code>Mar</code>, <code>Apr</code>, <code>May</code>, <code>Jun</code>, <code>Jul</code>, <code>Aug</code>, <code>Sep</code>, <code>Oct</code>, <code>Nov</code>, <code>Dec</code>). </li>
<li>
<code>dd</code> - the day of the month </li>
<li>
<code>hh</code> - hours </li>
<li>
<code>mm</code> - minutes </li>
<li>
<code>ss</code> - seconds </li>
<li>
<code>yyyy</code> - years </li>
</ul> <p>The function does not support localization.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> timer </td> <td> - </td> <td> pointer to a <code><a href="time_t" title="c/chrono/time t">time_t</a></code> object specifying the time to print </td>
</tr> <tr class="t-par"> <td> buf </td> <td> - </td> <td> pointer to the first element of a char array of size at least <code>bufsz</code> </td>
</tr> <tr class="t-par"> <td> bufsz </td> <td> - </td> <td> max number of bytes to output, typically the size of the buffer pointed to by <code>buf</code> </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1">
<span class="t-li">1)</span> pointer to a static null-terminated character string holding the textual representation of date and time. The string may be shared between <code><a href="asctime" title="c/chrono/asctime">asctime</a></code> and <code>ctime</code>, and may be overwritten on each invocation of any of those functions.</div> <div class="t-li1">
<span class="t-li">2)</span> zero on success (in which case the string representation of time has been written out to the array pointed to by <code>buf</code>), or non-zero on failure (in which case, the terminating null character is always written to <code>buf[0]</code> unless <code>buf</code> is a null pointer or <code>bufsz</code> is zero or greater than <code>RSIZE_MAX</code>.</div> <h3 id="Notes"> Notes</h3> <p><code>ctime</code> returns a pointer to static data and is not thread-safe. In addition, it modifies the static <code><a href="tm" title="c/chrono/tm">tm</a></code> object which may be shared with <code><a href="gmtime" title="c/chrono/gmtime">gmtime</a></code> and <code><a href="localtime" title="c/chrono/localtime">localtime</a></code>. POSIX marks this function obsolete and recommends <code><a href="strftime" title="c/chrono/strftime">strftime</a></code> instead. The C standard also recommends <code><a href="strftime" title="c/chrono/strftime">strftime</a></code> instead of <code>ctime</code> and <code>ctime_s</code> because <code>strftime</code> is more flexible and locale-sensitive.</p>
<p>The behavior of <code>ctime</code> is undefined for the values of <code><a href="time_t" title="c/chrono/time t">time_t</a></code> that result in the string longer than 25 characters (e.g. year 10000).</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#define __STDC_WANT_LIB_EXT1__ 1
#include &lt;time.h&gt;
#include &lt;stdio.h&gt;
 
int main(void)
{
    time_t result = time(NULL);
    printf("%s", ctime(&amp;result));
 
#ifdef __STDC_LIB_EXT1__
    char str[26];
    ctime_s(str,sizeof str,&amp;result);
    printf("%s", str);
#endif
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Tue May 26 21:51:03 2015
Tue May 26 21:51:03 2015</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.27.3.2 The ctime function (p: 287-288) </li>
<li> K.3.8.2.2 The ctime_s function (p: 454) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.27.3.2 The ctime function (p: 393) </li>
<li> K.3.8.2.2 The ctime_s function (p: 626) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.23.3.2 The ctime function (p: 342) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.12.3.2 The ctime function </li></ul>
</ul>                 <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="asctime" title="c/chrono/asctime"> <span class="t-lines"><span>asctime</span><span>asctime_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-deprecated-c23">(deprecated in C23)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> converts a <code><a href="tm" title="c/chrono/tm">tm</a></code> object to a textual representation <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="strftime" title="c/chrono/strftime"> <span class="t-lines"><span>strftime</span></span></a></div> </td> <td> converts a <code><a href="tm" title="c/chrono/tm">tm</a></code> object to custom textual representation <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/chrono/c/ctime" title="cpp/chrono/c/ctime">C++ documentation</a></span> for <code>ctime</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/chrono/ctime" class="_attribution-link">https://en.cppreference.com/w/c/chrono/ctime</a>
  </p>
</div>