Python time gmtime() Method



The Python time gmtime() method is used to get the time elapsed since the epoch, in the form of an object. The object is represented using the keyword "struct_time", and contains various fields to specify the elements of the time. This method accepts a floating-point number, referring to the number of seconds elapsed, as an argument and provides the UTC time in the form of an object.

Unlike the ctime() method, this method returns the object representation of the UTC time.

Note:If the argument is not passed or is passed as None, the method uses the value returned by time() as its argument, by default. The dst_flag field of the struct_time object will always be 0.

Syntax

Following is the syntax for the Python time gmtime() method −

time.gmtime([ sec ])

Parameters

  • sec − (Optional) A floating-point number representing the number of seconds elapsed.

Return Value

This method returns the time elapsed since the epoch in a system as an object.

Example

The following example shows the usage of the Python time gmtime() method. We are not passing any values to the optional parameter of this method. Hence, the method takes the return value of the time() method as its argument by default. The method returns the current time as an object.

import time

gt = time.gmtime()
print("Current UTC time:", gt)

When we run above program, it produces following result −

Current UTC time: time.struct_time(tm_year=2023, tm_mon=1, tm_mday=9, tm_hour=12, tm_min=28, tm_sec=40, tm_wday=0, tm_yday=9, tm_isdst=0)

Example

If the argument passed is an integer representing seconds elapsed from the epoch of the system, the method returns the object representation of the date after the elapsed time.

In the example below, we are trying to find the date after 1000 seconds from the epoch. The epoch of the system this program is executed in, is "Thu Jan 1 00:00:00 1970" in UTC time. This method returns the time after the elapsed seconds.

import time

# Passing the seconds elapsed as an argument to this method
gt = time.gmtime(1000)
print("Python UTC time:", gt)

Python UTC time: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=16, tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0)

Example

The gmtime() method can also be used to get the epoch of a system, in UTC time.

The method is said to return the time after the given elapsed seconds, calculated based on the epoch of a system. Thus, if we pass the argument as '0', the method returns the epoch of the system. If one needs to find the local time of the epoch, ctime() method is used.

import time

# Passing the seconds elapsed as an argument to this method
gt = time.gmtime(0)
print("The epoch of the system:", gt)

Let us compile and run the program above, to produce the following result −

The epoch of the system: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

Example

This method does not consider the fractional part of the floating-point number arguments that represent elapsed seconds.

Let us pass 2.99 seconds as an argument to the gmtime() method. Even if the number is almost equal to 3 seconds, the method is expected to completely ignore the fractional part and consider the argument as only 2 seconds. It is shown in the example below.

import time

# Passing the seconds elapsed as an argument to this method
gt = time.gmtime(2.99)
print("Time after elapsed seconds:", gt)

If we compile and run the program above, the output is as follows −

Time after elapsed seconds: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=2, tm_wday=3, tm_yday=1, tm_isdst=0)
python_date_time.htm
Advertisements