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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

Current time:
Beijing ( China ):16:40:21
Delhi ( India ): 3:40:21

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

Current time:
Monrovia ( Liberia ) : 8:40:22
Buenos Aires ( Argentina ) : 5:40:22


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.