The time.gmtime()
function converts seconds representing a timestamp since the epoch into a UTC ( Coordinated Universal Time) in form of a struct_time
object. A struct_time
object has attributes that makes it easier to access the individual components of time i.e year, month, day, hour, minutes, seconds, etc.
The main difference between localtime() and gmtime()
functions is that localtime()
returns the local time of the machine where the code is running, while gmtime()
returns the current Greenwich Mean Time (GMT) including timezone offset.
gmtime(secs = None)
copy
secs |
Optional argument for seconds representing time since the epoch to be converted into UTC time. If not given, the function will use the currentsystem time. |
The function returns a struct_time
object representing the time in Coordinated Universal Time (UTC). The returned object includes a time tuple that includes fields such as year, month, day, hour, minute, second, and microsecond, along with other attributes related to date and time.
Accessing individual elements of a struct_time object
We can access the individual components of the timestamp represented by a struct_time
object using its various attributes.
Convert a struct_time object into a formatted string
We can convert the returned struct_time
object into a formatted, human-readable string.
You can on time module usage to see more on formatting date and time.