gmtime() Function in C (original) (raw)

Last Updated : 6 Oct, 2023

The gmtime() function in C takes a pointer to type t_time value which represents type in seconds and converts it to struct tm. In this article, we will learn gmtime() Function in the C programming language.

The struct tm type can hold the individual components of time in UTC(Universal Time Coordinated) or GMT time (i.e., the time in the GMT timezone) time zones. The C gmtime() function is defined in header file.

Syntax of gmtime()

tm* gmtime ( const time_t* current_time )

Parameters

Return Value

Examples of gmtime()

Example 1

C `

// 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(&current_time);

// gets the current-time
ptime = gmtime(&current_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 ):20:00:04 Delhi ( India ): 7:00:04

Example 2

C `

// C++ program to illustrate the // gmtime() function #include <stdio.h> #include <time.h> #define UTC (0) #define ART (-3)

int main() { // object time_t current_time;

  // pointer
struct tm* ptime; 
  
  // use time function
time(&current_time); 

  // print the current time
ptime = gmtime(&current_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 ) :11:45:36 Buenos Aires ( Argentina ) : 8:45:36