time.gmtime() Method in Python

Introduction:

In this tutorial, we are learning about the time.gmtime() method in Python. The Time module provides many time-related functions and is Python's standard utility module. The gmtime() method converts the time in seconds to struct_time in UTC format, where the dst flag is always zero. If sec is not provided or the value is None, then the current time returned by time() is used.

The Time module's time.gmtime() method is used to convert time in seconds from time to time.struct_time object in UTC format; where the tm_isdst attribute is always 0. The time in seconds since local time edit a time.struct_time object returns, using the time.localtime() method. This method returns a time.struct_time object named tuple interface. The available values in the time.struct_time object are:

Index No.AttributeValues of attribute
0tm_hourrange [0, 23]
1tm_yearAn example is, 1990
2tm_monrange [1, 12]
3tm_minrange [0, 59]
4tm_mdayrange [1, 31]
5tm_ydayrange [1, 366]
6tm_secrange [0, 61]
7tm_wdayrange [0, 6], Monday is 0, and Sunday is 6
8tm_isdst0, 1 or -1
N/Atm_zoneIt is the abbreviation of the time zone name
N/Atm_gmtoffIt is the offset east of the UTC in seconds

Syntax:

The syntax of the time.gmtime() method in Python is given in below -

Parameters:

The parameter of the time.gmtime() method in Python is given below -

  • secs: It is optional. An integer or floating point value representing the duration in seconds. The fractional parts of the number of seconds are ignored. If the secs parameter is not provided or None. The current time returned by the time.time() method is used.

Return Value:

The time.gmtime() method returns an object of the class "time.struct_time".

Program Code 1:

Here we give the program code of the time.gmtime() method in Python. The code is given below -

Output:

Now, we run the above code and find the output from it. The output is given below -

time.struct_time(tm_year=2024, tm_mon=3, tm_mday=21, tm_hour=17, tm_min=56, tm_sec=11, tm_wday=3, tm_yday=81, tm_isdst=0)

Program Code 2:

Here we give another program code of the time.gmtime() method in Python. The code is given below -

Output:

Now, we run the above code and find the output from it. The output is given below -

The gmtime is: time.struct_time(tm_year=2024, tm_mon=3, tm_mday=21, tm_hour=18, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=81, tm_isdst=0)

Program Code 3:

Here we give another program code of the time.gmtime() method in Python. The code is given below -

Output:

Now, we run the above code and find the output from it. The output is given below -

The time.struct_time object in seconds is: 600000
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=7, tm_hour=22, tm_min=40, tm_sec=0, tm_wday=2, tm_yday=7, tm_isdst=0)

The time.struct_time object in seconds is: 550000.8546
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=7, tm_hour=8, tm_min=46, tm_sec=40, tm_wday=2, tm_yday=7, tm_isdst=0)

Conclusion:

In this tutorial, we are learning about the time.gmtime() method in Python. The gmtime() function, like the localtime() function, returns a time-struct time object. It accepts an argument in seconds since the epoch and returns the struct time in UTC format. Here, we also learn some program code for the time.gmtime() method in Python.