Att Time Gmtime
# Python2.x Python time gmtime() Method
* * *
## Description
The Python time gmtime() function converts a timestamp to a struct_time object in UTC timezone (timezone 0). The optional parameter sec represents the number of seconds since 1970-1-1.
The struct_time object contains various information about the time, such as year, month, day, hour, minute, etc.
## Syntax
The syntax for the gmtime() method is:
time.gmtime()
## Parameters
* sec -- The number of seconds to convert to a time.struct_time type object, which is a timestamp.
## Return Value
This function does not return any value.
## Example
The following example demonstrates the usage of the gmtime() function:
## Example
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
# Get the GMT representation of the current time
gmt_time =time.gmtime()
print("GMT Time:")
print(gmt_time)
The output of the above example is:
GMT Time: time.struct_time(tm_year=2023, tm_mon=11, tm_mday=23, tm_hour=6, tm_min=27, tm_sec=52, tm_wday=3, tm_yday=327, tm_isdst=0)
Get the GMT representation for a specified timestamp:
## Example
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
timestamp =1609459200# 2021-01-01 00:00:00
gmt_time_custom =time.gmtime(timestamp)
print("GMT Time for specified timestamp:")
print(gmt_time_custom)
Please note that the struct_time object returned by gmtime() is a named tuple, and you can access its individual fields by index or attribute.
For example, to get the year, you can use **gmt_time.tm_year**:
## Example
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
# Get the GMT representation of the current time
gmt_time =time.gmtime()
year = gmt_time.tm_year
print("Year:")
print(year)
YouTip