totn C Functions

C Language: gmtime function
(Convert to Greenwich Mean Time)

In the C Programming Language, the gmtime function converts a calendar time (pointed to by timer) and returns a pointer to a structure containing a UTC (or Greenwich Mean Time) value.

Syntax

The syntax for the gmtime function in the C Language is:

struct tm *gmtime(const time_t *timer);

Parameters or Arguments

timer
A pointer to an object of type time_t that contains a time value to convert.

Returns

The gmtime function returns a pointer to a structure containing a UTC (Greenwich Mean Time) value describing a local time pointed to by timer.

Required Header

In the C Language, the required header for the gmtime function is:

#include <time.h>

Applies To

In the C Language, the gmtime function can be used in the following versions:

  • ANSI/ISO 9899-1990

gmtime Example

Let's look at an example to see how you would use the gmtime function in a C program:

/* Example using gmtime by TechOnTheNet.com */

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

#define PDT (-7)

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    struct tm *gtime;
    time_t now;

    /* Read the current system time */
    time(&now);

    /* Convert the system time to GMT (now UTC) */
    gtime = gmtime(&now);

    /* Display the time in PDT and UTC */
    printf ("Pacific Daylight Time: %2d:%02d\n", (gtime->tm_hour + PDT) % 24, gtime->tm_min);
    printf ("Universal Time:        %2d:%02d\n", gtime->tm_hour % 24, gtime->tm_min);

    return 0;
}

When compiled and run, this application will output the current time in both the Pacific Daylight and Universal timezones. When we ran the application at 2:14PM, the output was the following:

Pacific Daylight Time: 14:14
Universal Time:        21:14

Similar Functions

Other C functions that are similar to the gmtime function: