gmtime() function in C/C++
C++ language is used to make high-performance applications that can work efficiently. It is one of the world's most popular languages. It is an object-oriented and high-level programming language, which was developed by Bjarne Stroustrup.
Features of C++
- High-level language.
- Object-oriented programming language
- Portable
- Graphical user interference
- Simple
- Structured programming language
C++ language is similar to other languages like C, Java, and C#.
What is C language?
C is used to instruct the computer to perform tasks and operations. It was discovered by Dennis Ritchie in 1972 in the bell laboratories, and this is a mid-level and procedure-oriented programming language. It consists of many features as that of C++ and python
Features of C:
- Fast and Efficient
- Simple and Faster
- Rich in Library
- Portability
- Easy to extend
Header file time.h:
Definitions for functions to obtain and work with date and time information can be found in the time.h header file. Three time-related data types are described.
Clock_t: The integer clock t uses to represent the date as it relates to calendar time.
Time_t: Time t is an integer representing the clock time as a component of calendar time.
Struct tm: The date and time that are stored in struct tm are
gmtime() in C++/ C:
The time that is provided to UTC (Universal Time Coordinated) time is changed via the C++ function gmtime(). The ctime header file contains a definition of gmtime(). The only required parameter for the function is current_time, which defines a pointer to a time_t object.
It has the return value:
The gmtime() function should return a pointer to a struct tm when it has completed its task successfully. gmtime() is supposed to return a null pointer if an error is found.
gmtime_r() shall return the address of the structure pointed to the argument result upon successful completion. Gmtime_r() is required to return a null pointer if an error is found.
C++ program for gmtime() function:
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t curr_time;
curr_time = time(NULL);
tm *tm_gmt = gmtime(&curr_time);
cout << "Current time is -- " << tm_gmt->tm_hour << ":" << tm_gmt->tm_min << ":" << tm_gmt->tm_sec << " GMT";
return 0;
}
Output:

Explanation for the above program:
In this program, we used different header files #include<iostream>, and #include<ctime>. Iostream is the library that consists of standard input and output functions like cin and cout. Based on the argument timer, the ctime() function returns a string representing the localtime. The time_t takes pointer type of arguments, tm_hour, tm_min, and tm_sec are different arguments used to print hours, minutes, and seconds at the output, and in other words, we can also say that we used that to access the hours, minutes, and seconds. Hence in the int main() function, we give the statements that are going to work to print the GMT of your current location.
Note: the output may be different for different computer systems because GMT is currently my location at the time of execution. Greenwich mean time may differ at your time of execution.
GMT:
GMT is also referred to as Universal Time Coordinated (UTC). GMT for India is +5:30 hours. It is calculated under 0 longitude and meridian passes through Greenwich near London.
C program for gmtime() function:
#include <stdio.h>
#include<conio.h>
#include <time.h>
int main () {
struct tm *local, *gm;
time_t t;
t=time(NULL);
local = gmtime(&t );
printf("Current time:\n");
printf("Time -- %d : %d : %d\n", local->tm_hour, local->tm_min, local->tm_sec);
return 0;
}
Output:

Explanation for the above program:
In the above program, we came to see different header files #include<stdio.h>, #include<conio.h and #include<time.h> which are different C++ programming where stdio.h standard library has input and output functions, conio.h is used to include the functions from the console input output library, and time.h is used to import gmtime() functions and all other functions related to time. In the int main function, we defined the statement to print GMT of your current location.
Another simple approach of gmtime() function:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t localtime;
time(&localtime);
printf ("Coordinated Universal Time is %s\n",
asctime(gmtime(&localtime)));
}
Output:

Explanation for the above program:
The above program is the simplest of all the programs in which we tend to find Universal Time Coordinated (UTC). Along with that, we also tend to find the day, month, and date. It is a compact program where we can print all at the same time using the function asctime along with the gmtime function. Coordinated Universal Time is also known as Universal Time Coordinated.