Easy Tutorial
❮ C Exercise Example80 C Function Gets ❯

C Library Function - gmtime()

C Standard Library - <time.h>

Description

The C library function struct tm *gmtime(const time_t *timer) uses the value of timer to fill the tm structure with the time in Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT).

Declaration

Here is the declaration for the gmtime() function.

struct tm *gmtime(const time_t *timer)

Parameters

Return Value

The function returns a pointer to a tm structure filled with the time information. Below are the details of the 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 flag               */    
};

Example

The following example demonstrates the use of the 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);
}

Let's compile and run the above program, which will produce the following result:

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

C Standard Library - <time.h>

❮ C Exercise Example80 C Function Gets ❯