The gmtime function in C converts the calendar time pointed to by the timer into a broken-down time, expressed as UTC. That means it uses the value of the time_t
object to fill a tm structure
with the values that represent the corresponding time, expressed in Coordinated Universal Time (UTC) or GMT timezone.
Syntax of gmtime:
The prototype of the gmtime() function is below:
#include <time.h> struct tm *gmtime(const time_t *timer);
Parameters:
The gmtime function takes one parameter.
timer = Pointer to a time_t
object to convert.
Returns:
The gmtime functions return a pointer to the broken-down time, or a null pointer if the specified time cannot be converted to UTC. That means,
On Success: The gmtime() function returns a pointer to a tm object.
On Failure: A null pointer is returned.
Note:
In gmtime() result is stored in static storage.
Example Program:
The below code explains how the gmtime() function works in C.
#include <stdio.h> #include <time.h> int main() { // object time_t current_time; // use time function time(¤t_time); // call gmtime() struct tm* ptime = gmtime(¤t_time); printf("UTC: %2d:%02d:%02d\n", ptime->tm_hour, ptime->tm_min, ptime->tm_sec); return 0; }
Output:
UTC: 16:01:58
How does the above code work?
In the above code, created one time_t
objects to store the times. When the program starts execution, the time() function stores the current time in the current_time variable.
// object time_t current_time; // use time function time(¤t_time);
Now in the last call, the gmtime() function store the broken-down time into a pointer to the tm object.
// call gmtime() struct tm* ptime = gmtime(¤t_time);
Recommended Post:
- difftime Function in C.
- Check date validity in C.
- C program to find the number of days in a month.
- Print a calendar for a given month for a year.
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- Find the number of days between two given dates.
- Count of Leap Years in a given year range.
- Find the prime number using the C program.
- Using the C program to check valid date (date is valid or not)
- Check expiry date Using C program
- C program to print day name of week
- Convert number of days in terms of Years, Weeks and Days using C program
- C program to find century for a year.