Python | time.mktime() method
Last Updated :
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:
Index |
Attribute |
Values |
0 |
tm_year |
(for example, 1993) |
1 |
tm_mon |
range [1, 12] |
2 |
tm_mday |
range [1, 31] |
3 |
tm_hour |
range [0, 23] |
4 |
tm_min |
range [0, 59] |
5 |
tm_sec |
range [0, 61] |
6 |
tm_wday |
range [0, 6], Monday is 0 |
7 |
tm_yday |
range [1, 366] |
8 |
tm_isdst |
0, 1 or -1 |
N/A |
tm_zone |
abbreviation of timezone name |
N/A |
tm_gmtoff |
offset east of UTC in seconds |
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
import time
seconds = 1000000
obj1 = time.gmtime(seconds)
print (obj1)
time_sec = time.mktime(obj1)
print ( "\nLocal time (in seconds):" , time_sec)
t = "14 Sep 2019 10:50:00"
obj2 = time.strptime(t, "% d % b % Y % H:% M:% S" )
time_sec = time.mktime(obj2)
print ( "\nLocal time (in seconds):" , time_sec)
|
Output:
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
import time
tup = ( 2019 , 9 , 13 , 1 , 30 , 26 , 4 , 256 , 0 )
time_sec = time.mktime(tup)
print ( "Local Time (in seconds since the epoch):" , time_sec)
|
Output:
Local Time (in seconds since the epoch): 1568318426.0
Code #3: To show time.mktime()
method is inverse function of time.localtime()
method
import time
curr_time = time.time()
print ( "Current time (in seconds since the epoch):" , curr_time)
obj = time.localtime(curr_time)
print ( "\ntime.struct_time object:" )
print (obj, "\n" )
time_sec = time.mktime(obj)
print ( "Time (in seconds since the epoch):" , time_sec)
|
Output:
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
Share your thoughts in the comments
Please Login to comment...