diff options
Diffstat (limited to 'devdocs/c/chrono%2Flocaltime.html')
| -rw-r--r-- | devdocs/c/chrono%2Flocaltime.html | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/devdocs/c/chrono%2Flocaltime.html b/devdocs/c/chrono%2Flocaltime.html new file mode 100644 index 00000000..75756995 --- /dev/null +++ b/devdocs/c/chrono%2Flocaltime.html @@ -0,0 +1,72 @@ + <h1 id="firstHeading" class="firstHeading">localtime, localtime_r, localtime_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><time.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">struct tm *localtime ( const time_t *timer );</pre> +</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">struct tm *localtime_r( const time_t *timer, struct tm *buf );</pre> +</td> <td> (2) </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">struct tm *localtime_s( const time_t *restrict timer, struct tm *restrict buf );</pre> +</td> <td> (3) </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 (a <code><a href="time_t" title="c/chrono/time t">time_t</a></code> value pointed to by <code>timer</code>) into calendar time, expressed in local time, in the <a href="tm" title="c/chrono/tm"><code>struct tm</code></a> format. The result is stored in static storage and a pointer to that static storage is returned.</div> <div class="t-li1"> +<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that the function uses user-provided storage <code>buf</code> for the result.</div> <div class="t-li1"> +<span class="t-li">3)</span> Same as <span class="t-v">(1)</span>, except that the function uses user-provided storage <code>buf</code> for the result and that 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>timer</code> or <code>buf</code> is a null pointer </li></ul> </dd> +<dd>As with all bounds-checked functions, <code>localtime_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><time.h></code></a>.</dd> +</dl> +</div> <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 to convert </td> +</tr> <tr class="t-par"> <td> buf </td> <td> - </td> <td> pointer to a <code><span class="kw1">struct</span> <a href="http://en.cppreference.com/w/c/chrono/tm"><span class="kw522">tm</span></a></code> object to store the result </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1"> +<span class="t-li">1)</span> pointer to a static internal <code><a href="tm" title="c/chrono/tm">tm</a></code> object on success, or null pointer otherwise. The structure may be shared between <code><a href="gmtime" title="c/chrono/gmtime">gmtime</a></code>, <code>localtime</code>, and <code><a href="ctime" title="c/chrono/ctime">ctime</a></code> and may be overwritten on each invocation.</div> <div class="t-li1"> +<span class="t-li">2-3)</span> copy of the <code>buf</code> pointer, or null pointer on error (which may be a runtime constraint violation or a failure to convert the specified time to local calendar time)</div> <h3 id="Notes"> Notes</h3> <p>The function <code>localtime</code> may not be thread-safe.</p> +<p>POSIX requires that <code>localtime</code> and <code>localtime_r</code> set <code><a href="../error/errno" title="c/error/errno">errno</a></code> to <code><a href="../error/errno_macros" title="c/error/errno macros">EOVERFLOW</a></code> if it fails because the argument is too large.</p> +<p><a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html">POSIX specifies</a> that the timezone information is determined by <code>localtime</code> and <code>localtime_r</code> as if by calling <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/tzset.html"><code>tzset</code></a>, which reads the environment variable <code>TZ</code>.</p> +<p>The implementation of <code>localtime_s</code> in <a rel="nofollow" class="external text" href="https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/localtime-s-localtime32-s-localtime64-s?view=vs-2019">Microsoft CRT</a> is incompatible with the C standard since it has reversed parameter order and returns <code>errno_t</code>.</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 +#define _XOPEN_SOURCE // for putenv +#include <time.h> +#include <stdio.h> +#include <stdlib.h> // for putenv + +int main(void) +{ + time_t t = time(NULL); + printf("UTC: %s", asctime(gmtime(&t))); + printf("local: %s", asctime(localtime(&t))); + // POSIX-specific + putenv("TZ=Asia/Singapore"); + printf("Singapore: %s", asctime(localtime(&t))); + +#ifdef __STDC_LIB_EXT1__ + struct tm buf; + char str[26]; + asctime_s(str,sizeof str,gmtime_s(&t, &buf)); + printf("UTC: %s", str); + asctime_s(str,sizeof str,localtime_s(&t, &buf)); + printf("local: %s", str); +#endif +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">UTC: Fri Sep 15 14:22:05 2017 +local: Fri Sep 15 14:22:05 2017 +Singapore: Fri Sep 15 22:22:05 2017 +UTC: Fri Sep 15 14:22:05 2017 +local: Fri Sep 15 14:22:05 2017</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul> +<li> 7.27.3.4 The localtime function (p: 288) </li> +<li> K.3.8.2.4 The localtime_s function (p: 455) </li> +</ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 7.27.3.4 The localtime function (p: 394) </li> +<li> K.3.8.2.4 The localtime_s function (p: 627) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.23.3.4 The localtime function (p: 343) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.12.3.4 The localtime function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="gmtime" title="c/chrono/gmtime"> <span class="t-lines"><span>gmtime</span><span>gmtime_r</span><span>gmtime_s</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c23">(C23)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> converts time since epoch to calendar time expressed as Coordinated Universal Time (UTC) <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/localtime" title="cpp/chrono/c/localtime">C++ documentation</a></span> for <code>localtime</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/chrono/localtime" class="_attribution-link">https://en.cppreference.com/w/c/chrono/localtime</a> + </p> +</div> |
