Time in Python is sometimes represented using the struct_time
data structure in the time module or simply as a tuple made of 9 elements i.e (year, month, day, hour, minute, second, weekday, day_of_year, dst).
The struct_time
class simply provides a more structured way of representing the 9 components of time.
The work of the mktime()
function.
The mktime()
function in the time
module converts a struct_time
object or a tuple containing the 9 element of time to a Unix timestamp (seconds since the epoch).
mktime(t)
t |
A struct_time object, or a tuple with time components. |
The function returns a floating point value representing the number seconds since the epoch of the input date.
In the above example, we used the time.localtime()
function without any argument to get a struct_time
object representing the current local time, then we used the mktime()
function to convert the struct_time
object to the equivalent timestamp.