C programming time.h function - struct tm *gmtime(const time_t *timer)

htt‮//:sp‬www.theitroad.com

The gmtime() function is a part of the standard C library and is declared in the <time.h> header file. It is used to convert a time value of type time_t (which represents the number of seconds since the Epoch) to a struct tm representation, which provides a more human-readable format for representing the time.

The function signature is:

struct tm *gmtime(const time_t *timer);

The gmtime() function takes a single argument, a pointer to a time_t variable that contains the time value to be converted. It returns a pointer to a struct tm that contains the broken-down time representation of the input time value in Coordinated Universal Time (UTC).

Here is an example usage of gmtime():

#include <stdio.h>
#include <time.h>

int main() {
    time_t current_time;
    struct tm *time_info;

    // Get the current time
    time(&current_time);

    // Convert the current time to a struct tm representation
    time_info = gmtime(&current_time);

    // Print the time in a human-readable format
    printf("The current time is %d:%02d:%02d UTC.\n",
           time_info->tm_hour, time_info->tm_min, time_info->tm_sec);

    return 0;
}

In this example, the time() function is used to get the current time, and gmtime() is used to convert the time to a struct tm representation. The resulting struct tm is then used to print the time in a human-readable format.

Note that gmtime() returns a pointer to a static internal struct tm object, so it is not thread-safe. If you need to use the result of gmtime() in a multithreaded program, you should make a copy of the result before calling gmtime() again.