C Time Functions - C gmtime






Converts time to Coordinated Universal Time (UTC).

Prototype

struct tm * gmtime (const time_t * timer);

Parameter

This function has the following parameter.

timer
Pointer to time_t type containing a time value.

Return

A pointer to a tm structure with its members filled with the UTC time representation of timer.

Example


#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, gmtime */
//w  w  w  . j  a v  a  2s.  c om
#define MST (-7)
#define UTC (0)
#define CCT (+8)

int main ()
{
  time_t rawtime;
  struct tm * ptm;

  time ( &rawtime );

  ptm = gmtime ( &rawtime );

  puts ("Current time around the World:");
  printf ("Phoenix, AZ (U.S.) : %2d:%02d\n", (ptm->tm_hour+MST)%24, ptm->tm_min);
  printf ("Reykjavik (Iceland) : %2d:%02d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min);
  printf ("Beijing (China) : %2d:%02d\n", (ptm->tm_hour+CCT)%24, ptm->tm_min);

  return 0;
} 

       

The code above generates the following result.





Example 2


#include <time.h>
#include <stdio.h>
 
int main(void){
    time_t t = time(NULL);
    printf("UTC:   %s", asctime(gmtime(&t)));
    printf("local: %s", asctime(localtime(&t)));
 
}

The code above generates the following result.