The Wayback Machine - https://web.archive.org/web/20240701111545/https://www.geeksforgeeks.org/python-time-mktime-method/
Open In App

Python | time.mktime() method

Last Updated : 17 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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 epoch
seconds = 1000000
obj1 = 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() method
time_sec = time.mktime(obj1)
  
# Print the local time in seconds
print("\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() method
obj2 = 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() method
time_sec = time.mktime(obj2)
  
# Print the local time in seconds
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




# 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 be
tup = (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() method
time_sec = time.mktime(tup)
  
# Print the time
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




# 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() method
curr_time = time.time() 
  
# Print the value
# returned by time.time() method
print("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 object
print("\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() method
time_sec = time.mktime(obj)
  
# Print the time
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



Previous Article
Next Article

Similar Reads

Time difference between expected time and given time
Given the initial clock time h1:m1 and the present clock time h2:m2, denoting hour and minutes in 24-hours clock format. The present clock time h2:m2 may or may not be correct. Also given a variable K which denotes the number of hours passed. The task is to calculate the delay in seconds i.e. time difference between expected time and given time. Ex
5 min read
Pandas Series dt.time | Extract Time from Time Stamp in Series
The Series.dt.time attribute returns a NumPy array containing time values of the timestamps in a Pandas series. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:22']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx sr = pd.to_datetime
2 min read
Python | time.time() method
Time module in Python provides various time-related functions. This module comes under Python’s standard utility modules. time.time() method of Time module is used to get the time in seconds since epoch. The handling of leap seconds is platform dependent. Note: The epoch is the point where the time starts, and is platform dependent. On Windows and
2 min read
Time Functions in Python | Set 1 (time(), ctime(), sleep()...)
Python has defined a module, "time" which allows us to handle various operations regarding time, its conversions and representations, which find its use in various applications in life. The beginning of time started measuring from 1 January, 12:00 am, 1970 and this very time is termed as "epoch" in Python. Operations on Time in Python Python time.t
4 min read
Python program to find difference between current time and given time
Given two times h1:m1 and h2:m2 denoting hours and minutes in 24 hours clock format. The current clock time is given by h1:m1. The task is to calculate the difference between two times in minutes and print the difference between two times in h:m format. Examples: Input : h1=7, m1=20, h2=9, m2=45 Output : 2 : 25 The current time is 7 : 20 and given
2 min read
How To Highlight a Time Range in Time Series Plot in Python with Matplotlib?
A time series plot is a plot which contains data which is being measured over a period of time, for example, a gross domestic product of a country, the population of the world and many other data. Sometimes we want to highlight a specific period of the timeline so that it is easier for the observer to read specific data. We can highlight a time ran
4 min read
MoviePy – Changing Image and Time at same time of Video Clip
In this article we will see how we can change display frames and time line of the video file clip in MoviePy at a same time. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF's. We use fl_image method to alter the display frames and fl_time method to alter the time, although we can do these task
2 min read
Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
Edge detection is one of the fundamental image-processing tasks used in various Computer Vision tasks to identify the boundary or sharp changes in the pixel intensity. It plays a crucial role in object detection, image segmentation and feature extraction from the image. In Real-time edge detection, the image frame coming from a live webcam or video
5 min read
Python | time.monotonic() method
Python time.monotonic() method is used to get the value of a monotonic clock. A monotonic clock is a clock that can not go backward. As the reference point of the returned value of the monotonic clock is undefined, only the difference between the results of consecutive calls is valid. Python time.monotonic() method Syntax: Syntax: time.monotonic()
2 min read
Python | time.get_clock_info() method
Time module in Python provides various time related functions. time.get_clock_info() method in Time module is used to get the information on the specified clock name. This method return the information as a namespace object. The name of supported clocks and the method used to read that clock value are as follow: clock : time.clock() monotonic : tim
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg