C++ gmtime()

The gmtime() is defined in <ctime> header file.

gmtime() Prototype

tm* gmtime(const time_t* time_ptr);

The gmtime() function takes a pointer of type time_t as its argument and returns a pointer object of type tm. The value returned by gmtime() function is the time at the GMT timezone.

Then, the hours, minutes and seconds can be accessed using tm_hour, tm_min and tm_sec respectively.


gmtime() Parameters

  • time_ptr: pointer to a time_t object to be converted.

gmtime() Return value

  • On success, the gmtime() function returns a pointer to a tm object.
  • On failure, a null pointer is returned.

Example: How gmtime() function works?

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	time_t curr_time;
	curr_time = time(NULL);

	tm *tm_gmt = gmtime(&curr_time);
	cout << "Current time : " << tm_gmt->tm_hour << ":" << tm_gmt->tm_min << ":" << tm_gmt->tm_sec << " GMT";
	return 0;
}

When you run the program, the output will be:

Current time : 13:26:28 GMT
Did you find this article helpful?