Difference between revisions of "c/chrono/gmtime"
From cppreference.com
(Fix typo in parameter name) |
m (fmt) |
||
Line 2: | Line 2: | ||
{{c/chrono/navbar}} | {{c/chrono/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | time.h }} | + | {{dcl header|time.h}} |
− | {{dcl | num=1 | | + | {{dcl|num=1| |
− | struct tm *gmtime ( const time_t *timer ); | + | struct tm* gmtime ( const time_t* timer ); |
}} | }} | ||
− | {{dcl | num=2 | since=c23 | | + | {{dcl|num=2|since=c23| |
− | struct tm *gmtime_r( const time_t *timer, struct tm *buf ); | + | struct tm* gmtime_r( const time_t* timer, struct tm* buf ); |
}} | }} | ||
− | {{dcl | num=3 | since=c11 | | + | {{dcl|num=3|since=c11| |
− | struct tm *gmtime_s( const time_t *restrict timer, struct tm *restrict buf ); | + | struct tm* gmtime_s( const time_t* restrict timer, struct tm* restrict buf ); |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | @1@ Converts given time since epoch (a {{lc|time_t}} value pointed to by {{ | + | @1@ Converts given time since epoch (a {{lc|time_t}} value pointed to by {{c|timer}}) into calendar time, expressed in Coordinated Universal Time (UTC) in the {{rlpt|tm|struct tm}} format. The result is stored in static storage and a pointer to that static storage is returned. |
− | @2@ Same as {{v|1}}, except that the function uses user-provided storage {{ | + | @2@ Same as {{v|1}}, except that the function uses user-provided storage {{c|buf}} for the result. |
− | @3@ Same as {{v|1}}, except that the function uses user-provided storage {{ | + | @3@ Same as {{v|1}}, except that the function uses user-provided storage {{c|buf}} for the result and that the following errors are detected at runtime and call the currently installed [[c/error/set_constraint_handler_s|constraint handler]] function: |
− | :* {{ | + | :* {{c|timer}} or {{c|buf}} is a null pointer |
:{{c/ext1 availability|gmtime_s}} | :{{c/ext1 availability|gmtime_s}} | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | timer | pointer to a {{lc|time_t}} object to convert}} | + | {{par|timer|pointer to a {{lc|time_t}} object to convert}} |
− | {{par | buf | pointer to a {{c|struct tm}} object to store the result}} | + | {{par|buf|pointer to a {{c|struct tm}} object to store the result}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
@1@ pointer to a static internal {{lc|tm}} object on success, or null pointer otherwise. The structure may be shared between {{tt|gmtime}}, {{lc|localtime}}, and {{lc|ctime}} and may be overwritten on each invocation. | @1@ pointer to a static internal {{lc|tm}} object on success, or null pointer otherwise. The structure may be shared between {{tt|gmtime}}, {{lc|localtime}}, and {{lc|ctime}} and may be overwritten on each invocation. | ||
− | @2 | + | @2,3@ copy of the {{c|buf}} pointer, or null pointer on error (which may be a runtime constraint violation or a failure to convert the specified time to UTC). |
===Notes=== | ===Notes=== | ||
Line 41: | Line 41: | ||
===References=== | ===References=== | ||
+ | {{ref std c23}} | ||
+ | {{ref std|section=7.27.3.3|title=The gmtime function|p=TBD}} | ||
+ | {{ref std|section=K.3.8.2.3|title=The gmtime_s function|p=TBD}} | ||
+ | {{ref std end}} | ||
{{ref std c17}} | {{ref std c17}} | ||
− | {{ref std | section=7.27.3.3 | + | {{ref std|section=7.27.3.3|title=The gmtime function|p=288}} |
− | {{ref std | section=K.3.8.2.3 | title=The gmtime_s function | p=454-455}} | + | {{ref std|section=K.3.8.2.3|title=The gmtime_s function|p=454-455}} |
{{ref std end}} | {{ref std end}} | ||
{{ref std c11}} | {{ref std c11}} | ||
− | {{ref std | section=7.27.3.3 | + | {{ref std|section=7.27.3.3|title=The gmtime function|p=393-394}} |
− | {{ref std | section=K.3.8.2.3 | title=The gmtime_s function | p=626-627}} | + | {{ref std|section=K.3.8.2.3|title=The gmtime_s function|p=626-627}} |
{{ref std end}} | {{ref std end}} | ||
{{ref std c99}} | {{ref std c99}} | ||
− | {{ref std | section=7.23.3.3 | + | {{ref std|section=7.23.3.3|title=The gmtime function|p=343}} |
{{ref std end}} | {{ref std end}} | ||
{{ref std c89}} | {{ref std c89}} | ||
− | {{ref std | section=4.12.3.3 | + | {{ref std|section=4.12.3.3|title=The gmtime function}} |
{{ref std end}} | {{ref std end}} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | c/chrono/dsc localtime}} | + | {{dsc inc|c/chrono/dsc localtime}} |
− | {{dsc see cpp | cpp/chrono/c/gmtime}} | + | {{dsc see cpp|cpp/chrono/c/gmtime}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|ar|cs|de|es|fr|it|ja|ko|pl|pt|ru|tr|zh}} | {{langlinks|ar|cs|de|es|fr|it|ja|ko|pl|pt|ru|tr|zh}} |
Latest revision as of 06:19, 7 June 2024
Defined in header <time.h>
|
||
(1) | ||
(2) | (since C23) | |
(3) | (since C11) | |
1) Converts given time since epoch (a time_t value pointed to by timer) into calendar time, expressed in Coordinated Universal Time (UTC) in the
struct tm
format. The result is stored in static storage and a pointer to that static storage is returned.2) Same as (1), except that the function uses user-provided storage buf for the result.
3) Same as (1), except that the function uses user-provided storage buf for the result and that the following errors are detected at runtime and call the currently installed constraint handler function:
- timer or buf is a null pointer
- As with all bounds-checked functions,
gmtime_s
is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <time.h>.
Contents |
[edit] Parameters
timer | - | pointer to a time_t object to convert |
buf | - | pointer to a struct tm object to store the result |
[edit] Return value
1) pointer to a static internal tm object on success, or null pointer otherwise. The structure may be shared between
gmtime
, localtime, and ctime and may be overwritten on each invocation.2,3) copy of the buf pointer, or null pointer on error (which may be a runtime constraint violation or a failure to convert the specified time to UTC).
[edit] Notes
gmtime
may not be thread-safe.
POSIX requires that gmtime
and gmtime_r
set errno to EOVERFLOW if they fail because the argument is too large.
The implementation of gmtime_s
in Microsoft CRT is incompatible with the C standard since it has reversed parameter order.
[edit] Example
Run this code
#define __STDC_WANT_LIB_EXT1__ 1 #define _XOPEN_SOURCE // for putenv #include <stdio.h> #include <stdlib.h> // for putenv #include <time.h> 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 }
Possible output:
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
[edit] References
- C23 standard (ISO/IEC 9899:2024):
- 7.27.3.3 The gmtime function (p: TBD)
- K.3.8.2.3 The gmtime_s function (p: TBD)
- C17 standard (ISO/IEC 9899:2018):
- 7.27.3.3 The gmtime function (p: 288)
- K.3.8.2.3 The gmtime_s function (p: 454-455)
- C11 standard (ISO/IEC 9899:2011):
- 7.27.3.3 The gmtime function (p: 393-394)
- K.3.8.2.3 The gmtime_s function (p: 626-627)
- C99 standard (ISO/IEC 9899:1999):
- 7.23.3.3 The gmtime function (p: 343)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.12.3.3 The gmtime function
[edit] See also
(C23)(C11) |
converts time since epoch to calendar time expressed as local time (function) |
C++ documentation for gmtime
|