Skip to content
Related Articles

Related Articles

Improve Article

gmtime() Function in C/C++

  • Last Updated : 20 Jul, 2020
Geek Week

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 function
    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;
}
Output:
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;
}
Output:
Current time:
Monrovia ( Liberia ) : 8:40:22
Buenos Aires ( Argentina ) : 5:40:22
We provide nothing but the best curated videos and practice problems for our students. Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today!



My Personal Notes arrow_drop_up
Recommended Articles
Page :