Overview #
The gmtime() function converts a calendar time stored as a time_t value into a broken-down UTC (Coordinated Universal Time).
Like localtime(), it returns a pointer to a struct tm containing individual date and time components. However, the returned time is expressed in UTC, not the local time zone.
Header File #
#include <time.h>
Man Pages #
man 3 gmtime
Function Prototype #
struct tm *gmtime(const time_t *timer);
Parameters #
| Parameter | Description |
|---|---|
timer |
Pointer to a time_t object containing the calendar time to be converted. |
Return Value #
On success, gmtime() returns a pointer to a struct tm containing the UTC date and time.
On failure, it returns:
NULL
Data Types #
struct tm #
struct tm stores the individual components of a date and time.
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
| Member | Description |
|---|---|
tm_sec |
Seconds (0โ60) |
tm_min |
Minutes (0โ59) |
tm_hour |
Hours (0โ23) |
tm_mday |
Day of the month (1โ31) |
tm_mon |
Month since January (0โ11) |
tm_year |
Years since 1900 |
tm_wday |
Days since Sunday (0โ6) |
tm_yday |
Days since January 1 (0โ365) |
tm_isdst |
Daylight Saving Time flag |
Examples #
Example 1 โ Display UTC Date and Time #
main.c #
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t current_time;
struct tm *utc;
current_time = time(NULL);
utc = gmtime(¤t_time);
printf("Date : %02d-%02d-%04d\n",
utc->tm_mday,
utc->tm_mon + 1,
utc->tm_year + 1900);
printf("Time : %02d:%02d:%02d UTC\n",
utc->tm_hour,
utc->tm_min,
utc->tm_sec);
return 0;
}
Output #
Date : 27-07-2026
Time : 10:15:32 UTC
The output will be different on your system.
Explanation #
current_time = time(NULL);
Obtains the current calendar time.
utc = gmtime(¤t_time);
Converts the calendar time into a struct tm representing the UTC date and time.
utc->tm_mon + 1
tm_mon stores months in the range 0โ11, where January is 0. Adding 1 converts it to the familiar range 1โ12.
utc->tm_year + 1900
tm_year stores the number of years since 1900. Adding 1900 gives the actual year.
Example 2 โ Compare Local Time and UTC #
main.c #
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t current_time;
struct tm *local;
struct tm *utc;
current_time = time(NULL);
local = localtime(¤t_time);
utc = gmtime(¤t_time);
printf("Local Time : %02d:%02d:%02d\n",
local->tm_hour,
local->tm_min,
local->tm_sec);
printf("UTC Time : %02d:%02d:%02d\n",
utc->tm_hour,
utc->tm_min,
utc->tm_sec);
return 0;
}
Output #
Local Time : 15:45:32
UTC Time : 10:15:32
The output will vary depending on your local time zone.
Explanation #
local = localtime(¤t_time);
Converts the calendar time into the local date and time.
utc = gmtime(¤t_time);
Converts the same calendar time into UTC.
The difference between the two values corresponds to your system’s configured time zone.
Notes #
gmtime()converts atime_tvalue into UTC (Coordinated Universal Time).- It returns a pointer to a
struct tm. tm_monranges from 0 to 11, so add1when displaying the month.tm_yearrepresents the number of years since 1900, so add1900to obtain the actual year.- The returned pointer refers to statically allocated memory managed by the C library.
- Each call to
gmtime()may overwrite the result of a previous call. - Do not modify or free the returned structure.
Related Functions #
time()localtime()ctime()mktime()strftime()