Python 3 – time gmtime() Method
In Python 3, the time module provides us with various methods for time-related operations. The gmtime()
method is one of them. This method takes no argument and returns the current time in Greenwich Mean Time (GMT) format. The gmtime()
method returns the current time in a tuple format with the following structure:
struct_time(tm_year=2018, tm_mon=9, tm_mday=25, tm_hour=4, tm_min=34, tm_sec=14, tm_wday=1, tm_yday=268, tm_isdst=0)
Here, tm_year
represents the year in which the current time is, tm_mon
represents the month which ranges from 1 to 12, tm_mday
represents the day of the month which ranges from 1 to 31, tm_hour
represents the hour of the day which ranges from 0 to 23, tm_min
represents the minute of the hour which ranges from 0 to 59, tm_sec
represents the second of the minute which ranges from 0 to 59, tm_wday
represents the day of the week which ranges from 0 to 6, tm_yday
represents the day of the year which ranges from 1 to 366, and tm_isdst
represents the daylight saving time.
Example
Let’s dive into an example that will demonstrate how the gmtime()
method works.
import time
current_time = time.gmtime()
print("Current time in GMT format:", current_time)
Output:
Current time in GMT format: time.struct_time(tm_year=2022, tm_mon=6, tm_mday=22, tm_hour=2, tm_min=6, tm_sec=15, tm_wday=2, tm_yday=173, tm_isdst=0)
As we can see in the output, the gmtime()
method returns the current time in GMT format in a tuple format.
Conclusion
In conclusion, the gmtime()
method of the time module in Python 3 is a very useful method for obtaining the current GMT time. It is very simple to use and returns the current GMT time in a tuple format that can be accessed and utilized as needed. The gmtime()
method can be used in various applications that require the current GMT time, such as time-stamping, logging, and so on. By using the gmtime()
method, developers can ensure the accuracy of their applications by obtaining the current GMT time rather than relying on a local clock.