Python | time.mktime() method - GeeksforGeeks

17 Sep, 2019
time.mktime() method of Time module is used to convert a time.struct_time object or a tuple containing 9 elements corresponding to time.struct_time object to time in seconds passed since epoch in local time.
This method is the inverse function of time.localtime() which converts the time expressed in seconds since the epoch to a time.struct_time object in local time.
Following are the values present in time.struct_time object:
Note: The epoch is the point where the time starts and is platform dependent. On Windows and most Unix systems, the epoch is January 1, 1970, 00:00:00 (UTC) and leap seconds are not counted towards the time in seconds since the epoch. To check what the epoch is on a given platform we can use time.gmtime(0).
Syntax: time.mktime(t)
Parameter:t : A time.struct_time object or a tuple containing 9 elements corresponding to time.struct_time object
Return type: This method returns a float value which represents the time expressed in seconds since the epoch.
Code #1: Use of time.mktime() method
# Python program to explain time.mktime() method     # importing time module import time     # time.gmtime() method will returns# a time.struct_time object in UTC# for the time expressed in seconds# since the epochseconds = 1000000obj1 = time.gmtime(seconds)  # Print time.struct_time object (in UTC)print(obj1)  # Convert the time.struct_time# object to local time expressed in# seconds since the epoch# using time.mktime() methodtime_sec = time.mktime(obj1)  # Print the local time in secondsprint("\nLocal time (in seconds):", time_sec)    # time.strptime() method parse # a string representing a time# according to the given format# and returns a time.struct_time object  # Time string t = "14 Sep 2019 10:50:00"  # Parse the time string using# time.strptime() methodobj2 = time.strptime(t, "% d % b % Y % H:% M:% S")  # Convert the time.struct_time# object to local time expressed in# seconds since the epoch# using time.mktime() methodtime_sec = time.mktime(obj2)  # Print the local time in secondsprint("\nLocal time (in seconds):", time_sec)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=12, tm_hour=13, tm_min=46,
tm_sec=40, tm_wday=0, tm_yday=12, tm_isdst=0)

Local time (in seconds): 980200.0

Local time (in seconds): 1568438400.0

Code #2: If parameter is a tuple
# Python program to explain time.mktime() method     # importing time module import time     # A tuple containing 9 elements# corresponding to time.struct_time object# for example: consider the below object# time.struct_time(tm_year = 2019, tm_mon = 9, tm_mday = 13, # tm_hour = 1, tm_min = 30, tm_sec = 26, tm_wday = 4,# tm_yday = 256, tm_isdst = 0)  # Tuple corresponding to above # time.struct_time object will betup = (2019, 9, 13, 1, 30, 26, 4, 256, 0)  # Convert the above specified tuple# to local time expressed in seconds# since the epoch # using time.mktime() methodtime_sec = time.mktime(tup)  # Print the timeprint("Local Time (in seconds since the epoch):", time_sec)
Local Time (in seconds since the epoch): 1568318426.0

Code #3: To show time.mktime() method is inverse function of time.localtime() method
# Python program to explain time.mktime() method     # importing time module import time     # Get the current time# expressed in seconds # since the epoch using# time.time() methodcurr_time = time.time()   # Print the value# returned by time.time() methodprint("Current time (in seconds since the epoch):", curr_time)    # Convert the time expressed in seconds# since the epoch to# a time.struct_time object# in local time using# time.localtime() method obj = time.localtime(curr_time)  # Print the time.struct_time objectprint("\ntime.struct_time object:")print(obj, "\n")    # Convert the time.struct_time object# back to the time expressed# in seconds since the epoch# in local time using# time.mktime() methodtime_sec = time.mktime(obj)  # Print the timeprint("Time (in seconds since the epoch):", time_sec) 
Current time (in seconds since the epoch): 1568318426.2286296

time.struct_time object:
time.struct_time(tm_year=2019, tm_mon=9, tm_mday=13, tm_hour=1, tm_min=30,
tm_sec=26, tm_wday=4, tm_yday=256, tm_isdst=0) 

Time (in seconds since the epoch): 1568318426.0

References: https://docs.python.org/3/library/time.html#time.mktime
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()