gmtime() Function in C/C++
The gmtime() function in C++ change the time, which is given to UTC(Universal Time Coordinated) time (i.e., the time at the GMT timezone). The gmtime() is defined in ctime header file.
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: The function accepts one mandatory parameter current_time : which specifies a pointer to a time_t object.
Return Value: The function returns two type of values described below:
- On Success, returns a pointer to a tm object
- Otherwise, Null pointer is returned
Below programs illustrate the above function.
Program 1:
// C++ program to illustrate the // gmtime() function #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 functin time(¤t;_time); // gets the current-time ptime = gmtime(¤t;_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; } |
Current time: Beijing ( China ):16:40:21 Delhi ( India ): 3:40:21
Program 2:
// C++ program to illustrate the // gmtime() function #include <stdio.h> #include <time.h> #define UTC (0) #define ART (-3) int main() { time_t current_time; struct tm* ptime; time(¤t;_time); ptime = gmtime(¤t;_time); printf("Current time:\n"); printf("Monrovia ( Liberia ) :%2d:%02d:%02d\n", (ptime->tm_hour + UTC) % 24, ptime->tm_min, ptime->tm_sec); printf("Buenos Aires ( Argentina ) :%2d:%02d:%02d\n", (ptime->tm_hour + ART) % 24, ptime->tm_min, ptime->tm_sec); return 0; } |
Current time: Monrovia ( Liberia ) : 8:40:22 Buenos Aires ( Argentina ) : 5:40:22
Recommended Posts:
- Function Overloading vs Function Overriding in C++
- fma() function in C++
- map key_comp() function in C++ STL
- max() function for valarray in C++
- strlwr() function in C
- imag() function in C++
- real() function in C++
- valarray log() function in C++
- valarray abs() function in C++
- valarray end() function in C++
- map rbegin() function in C++ STL
- valarray sin() function in C++
- valarray pow() function in C++
- valarray tan() function in C++
- valarray cos() function in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



