Python - TimeTuple
In Python, a time.struct_time object, also known as a TimeTuple, is a tuple that represents a point in time. It has 9 attributes: tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, and tm_isdst.
- tm_year: year of the timestamp
- tm_mon: month of the timestamp (1-12)
- tm_mday: day of the timestamp (1-31)
- tm_hour: hour of the timestamp (0-23)
- tm_min: minute of the timestamp (0-59)
- tm_sec: second of the timestamp (0-59)
- tm_wday: day of the week (0-6, where Monday is 0)
- tm_yday: day of the year (1-366)
- tm_isdst: flag indicating whether daylight saving time is in effect at the time (0 or 1)
This tuple is used to represent time in many Python modules, such as the time module, which provides various functions to manipulate and format time values.
Here's an example of usin-g the time module in Python to get the current time as a time.struct_time object:
import time
# Get the current time as a time.struct_time object
current_time = time.localtime()
# Print the current time in a readable format
print(f"Current time: {current_time.tm_hour}:{current_time.tm_min}:{current_time.tm_sec}")
This code will output the current time in the format HH:MM:SS, where HH is the hour, MM is the minute, and SS is the second. The time.localtime() function returns a time.struct_time object that contains various attributes such as tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, etc. We can access these attributes using dot notation (e.g. current_time.tm_hour) to get the values of each field.