gmtime() Function in C

Last Updated : 8 Aug, 2026

The gmtime() function converts a time_t value representing calendar time into a struct tm containing its individual time components in UTC (GMT). It is defined in the <time.h> header file in C.

  • Converts the time represented by time_t into a struct tm in UTC.
  • Provides individual components such as seconds, minutes, hours, day, month, and year for easy access.
C
#include <stdio.h>
#include <time.h>
#define CST (+8)
#define IND (-5)

int main()
{

    // object
    time_t current_time;

    // pointer
    struct tm* ptime;

    // use time function
    time(&current_time);

    // gets the current-time
    ptime = gmtime(&current_time);

    // print the current time
    printf("Current time:\n");

    printf("Beijing ( China ):%2d:%02d:%02d\n",
           (ptime->tm_hour + CST) % 24, ptime->tm_min,
           ptime->tm_sec);

    printf("Delhi ( India ):%2d:%02d:%02d\n",
           (ptime->tm_hour + IND) % 24, ptime->tm_min,
           ptime->tm_sec);
    return 0;
}

Output
Current time:
Beijing ( China ):20:00:04
Delhi ( India ): 7:00:04

Syntax

tm* gmtime ( const time_t* current_time )

  • The hours can be accessed using tm_hour
  • The minutes can be accessed using tm_min
  • The seconds can be accessed using tm_sec

Parameters

  • current_time: It specifies a pointer to a time_t object.

Return Value

  • On Success, returns a pointer to a tm object.
  • Otherwise, the Null pointer is returned.

Advantages of gmtime()

The gmtime() function is useful for converting calendar time into individual UTC-based time components.

  • Provides an easy way to access hours, minutes, seconds, day, month, and year separately.
  • Useful for applications that need to work with UTC/GMT time consistently across different time zones.

Limitations of gmtime()

The gmtime() function also has some limitations when working with time values.

  • Returns UTC time only and does not directly provide the local time of a specific time zone.
  • The returned struct tm may be stored in static memory and can be overwritten by subsequent time-related function calls.
  • It may not be thread-safe on some implementations, so extra care is required in multi-threaded programs.

Applications of gmtime()

The gmtime() function is useful in applications where time needs to be converted and processed in UTC.

  • Converting timestamps to UTC for consistent time representation.
  • Recording events in logs using a common time zone.
  • Comparing timestamps from different time zones.
  • Processing time-related data in distributed applications.
Comment