gmtime_r() — Convert Time (Restartable)

Format

#include <time.h>
struct tm *gmtime_r(const time_t *time, struct tm *result);

Language Level

XPG4

Threadsafe

Yes

Description

This function is the restartable version of gmtime().

The gmtime_r() function breaks down the time value, in seconds, and stores it in result. result is a pointer to the tm structure, defined in <time.h>. The value time is usually obtained by a call to the time() function.

The fields of the tm structure include:
tm_sec
Seconds (0-61)
tm_min
Minutes (0-59)
tm_hour
Hours (0-23)
tm_mday
Day of month (1-31)
tm_mon
Month (0-11; January = 0)
tm_year
Year (current year minus 1900)
tm_wday
Day of week (0-6; Sunday = 0)
tm_yday
Day of year (0-365; January 1 = 0)
tm_isdst
Zero if daylight saving time is not in effect; positive if daylight saving time is in effect; negative if the information is not available.

Return Value

The gmtime_r() function returns a pointer to the resulting tm structure.
Note:
  1. The range (0-61) for tm_sec allows for as many as two leap seconds.
  2. The gmtime() and localtime() functions can use a common, statically allocated buffer for the conversion. Each call to one of these functions might alter the result of the previous call. The asctime_r(), ctime_r(), gmtime_r(), and localtime_r() functions do not use a common, statically allocated buffer to hold the return string. These functions can be used in place of the asctime(), ctime(), gmtime(), and localtime() functions if reentrancy is desired.
  3. Calendar time is the number of seconds that have elapsed since EPOCH, which is 00:00:00, January 1, 1970 Universal Coordinate Time (UTC).

Example

This example uses the gmtime_r() function to adjust a time_t representation to a Coordinated Universal Time character string, and then converts it to a printable string using the asctime_r() function.
#include <stdio.h>
#include <time.h>
 
int main(void)
{
   time_t ltime;
   struct tm mytime;
   char buf[50];
 
   time(&ltime)
   printf ("Coordinated Universal Time is %s\n",
   asctime_r(gmtime_r(&ltime, &mytime), buf));
}
 
/************************  Output should be similar to:  **********
 
Coordinated Universal Time is Wed Aug 18 21:01:44 1993
*/

Related Information