C library - gmtime() function



The C library gmtime() function of type struct uses the value pointed by timer to fill a structure(tm) with the values that represent the corresponding time, expressed in coordinated universal time (UTC) or GMT timezone. This is useful for the operations such as logging, timestampping and scheduling effect.

Syntax

Following is the syntax of the C library gmtime() function −

struct tm *gmtime(const time_t *timer)

Parameters

This function accepts only a single parameter −

  • timeptr − This is the pointer to a time_t value representing a calendar time.

Return Value

This function returns pointer to a tm structure with the time information filled in.

Below is the list of timeptr structure −

struct tm {
   int tm_sec;         /* seconds,  range 0 to 59          */
   int tm_min;         /* minutes, range 0 to 59           */
   int tm_hour;        /* hours, range 0 to 23             */
   int tm_mday;        /* day of the month, range 1 to 31  */
   int tm_mon;         /* month, range 0 to 11             */
   int tm_year;        /* The number of years since 1900   */
   int tm_wday;        /* day of the week, range 0 to 6    */
   int tm_yday;        /* day in the year, range 0 to 365  */
   int tm_isdst;       /* daylight saving time             */	
};

Example 1

Following is the basic C library program to see the demonstration of gmtime() function.

#include <stdio.h>
#include <time.h>

#define BST (+1)
#define CCT (+8)

int main () {

   time_t rawtime;
   struct tm *info;

   time(&rawtime);
   /* Get GMT time */
   info = gmtime(&rawtime );
   
   printf("Current world clock:\n");
   printf("London : %2d:%02d\n", (info->tm_hour+BST)%24, info->tm_min);
   printf("China  : %2d:%02d\n", (info->tm_hour+CCT)%24, info->tm_min);

   return(0);
}

Output

The above code produces the following result −

Current world clock:
London : 14:10
China : 21:10

Example 2

Here, we acheive the current local time using gmtime() and prints all the local time zone of different countries.

#include <stdio.h>
#include <time.h>

#define CST (+8)
#define IND (-5)

int main() {
   time_t current_time;
   struct tm* ptime;

   time(&current_time);
   ptime = gmtime(&current_time);

   printf("Current time:\n");
   printf("Beijing (China): %02d:%02d:%02d\n", (ptime->tm_hour + CST) % 24, ptime->tm_min, ptime->tm_sec);
   printf("Delhi (India): %02d:%02d:%02d\n", (ptime->tm_hour + IND) % 24, ptime->tm_min, ptime->tm_sec);

   return 0;
}

Output

On execution of above code, we get the following result −

Current time:
Beijing (China): 16:23:24
Delhi (India): 03:23:24

Example 3

The function gmtime() illustrates the conversion of current calendar time into a textual representation(asctime_s()).

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <time.h>

int main(void) {
   struct tm tm = *gmtime(&(time_t){time(NULL)});

   printf("Current time (using asctime()): %s\n", asctime(&tm));

   #ifdef __STDC_LIB_EXT1__
   char str[50];
   asctime_s(str, sizeof str, &tm);
   printf("Current time (using asctime_s()): %s\n", str);
   #endif

   return 0;
}

Output

After execution of above code, we get the following result −

Current time (using asctime()): Tue May 14 08:33:26 2024
Advertisements