C++ Standard Library C++ STL Library

C++ <ctime> - gmtime() Function



The C++ <ctime> gmtime() function converts the value pointed by timer into calendar time, expressed in UTC time (time at the GMT timezone). The returned value a pointer to a tm object filled with the values representing UTC time for timer on success, or null pointer otherwise.

Syntax

struct tm * gmtime (const time_t * timer);

Parameters

timer Specify pointer to a time_t object to convert.

Return Value

Returns a pointer to a tm object filled with the values representing UTC time for timer on success, or null pointer otherwise. The structure may be shared between localtime(), gmtime(), and ctime(), and may be overwritten on each invocation of any of these functions.

Example:

The example below shows the usage of gmtime() function.

#include <iostream>
#include <ctime>
using namespace std;
 
int main (){
  time_t t = time(NULL);
  struct tm * timeinfo = gmtime(&t);

  //displaying the result 
  cout<<"Current UTC time & date: "<<
       asctime(timeinfo);   
  
  return 0;
}

The output of the above code will be:

Current UTC time & date: Fri Apr 16 10:35:38 2021

❮ C++ <ctime> Library