Hello everyone in this article, I will introduce you to Mobule Time in Python, I will explain to you what their concept is, their syntax and usage. You can find out in my article!
In this article, we will learn more about the Time
module. We will learn how to use the various time related functions identified in the time
module and examples.
Python has a module named time
to handle time
-related tasks. To use functions defined in the module, we need to import the module first.
1 2 | import time |
Below are the most commonly used time related functions
Python time.time ()
The time ()
function returns the detailed time to the number of seconds.
1 2 3 4 | import time seconds = time.time() print("Seconds since epoch =", seconds) |
Python time.ctime ()
The time.ctime ()
function time.ctime ()
time as an argument and returns a string representing the current time.
1 2 3 4 5 6 7 | import time # seconds passed since epoch seconds = 1545925769.9618232 local_time = time.ctime(seconds) print("Local time:", local_time) |
If you run the program, the results will be as follows:
1 2 | Local time: Thu Dec 27 15:49:29 2018 |
Python time.sleep ()
The sleep ()
function pauses the current thread for a certain time (the time is in seconds).
1 2 3 4 5 6 | import time print("This is printed immediately.") time.sleep(2.4) print("This is printed after 2.4 seconds.") |
Python time.localtime ()
The localtime ()
function takes the number of seconds passed as an argument and returns struct_time
in struct_time
with the current local time.
1 2 3 4 5 6 7 | import time result = time.localtime(1545925769) print("result:", result) print("nyear:", result.tm_year) print("tm_hour:", result.tm_hou |
When you run the program, the results will be as follows:
1 2 3 4 5 | result: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_isdst=0) year: 2018 tm_hour: 15 |
Python time.gmtime ()
The gmtime ()
function takes the number of seconds passed as an argument and returns struct_time
will be UTC .
1 2 3 4 5 6 7 | import time result = time.gmtime(1545925769) print("result:", result) print("nyear:", result.tm_year) print("tm_hour:", result.tm_hour) |
When you run the program, the results will be as follows:
1 2 3 4 5 | result = time.struct_time(tm_year=2018, tm_mon=12, tm_mday=28, tm_hour=8, tm_min=44, tm_sec=4, tm_wday=4, tm_yday=362, tm_isdst=0) year = 2018 tm_hour = 8 |
Python time.mktime ()
The mktime ()
function will take struct_time
(or a tuple containing 9 elements corresponding to struct_time
) as an argument and return the number of seconds elapsed in local time. It will be essentially the inverse of the localtime ()
function.
1 2 3 4 5 6 7 | import time t = (2018, 12, 28, 8, 44, 4, 4, 362, 0) local_time = time.mktime(t) print("Local time:", local_time) |
I will have the example below to show how mktime ()
and localtime ()
are related to each other.
1 2 3 4 5 6 7 8 9 10 11 12 | import time seconds = 1545925769 # returns struct_time t = time.localtime(seconds) print("t1: ", t) # returns seconds from struct_time s = time.mktime(t) print("s:", seconds) |
When you run the program, the results will be as follows:
1 2 3 4 | t1: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_isdst=0) s: 1545925769.0 |
Python time.asctime ()
The asctime ()
function will take struct_time
(or a tuple containing 9 elements corresponding to struct_time
) as an argument and return the specified detail time in the format from the date to the current time in seconds.
1 2 3 4 5 6 7 | import time t = (2018, 12, 28, 8, 44, 4, 4, 362, 0) result = time.asctime(t) print("Result:", result) |
When you run the program, the results will be as follows:
1 2 | Result: Fri Dec 28 08:44:04 2018 |
Python time.strftime ()
The strftime ()
function takes struct_time
(or its corresponding tuple) as an argument and returns the value of the time based on the format you specify.
1 2 3 4 5 6 7 | import time named_tuple = time.localtime() # get struct_time time_string = time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple) print(time_string) |
When you run the program, the results will be as follows:
1 2 | 12/28/2018, 09:47:41 |
Below %Y
, %m
, %d
, %H
will be specified.
%Y
: The year will be formatted as [0001, …, 2018, 2019, …, 9999]%m
: Month will be formatted as [01, 02, …, 11, 12]%d
: The date will be formatted as [01, 02, …, 30, 31]%H
: The hour will be formatted [00, 01, …, 22, 23]%M
: Minutes will be formatted [00, 01, …, 58, 59]%S
: Seconds will be formatted [00, 01, …, 58, 59]
Conclude
Below I have introduced to you about Module Time
as well as some frequently used Time
functions in Python. If you have any questions, please leave a comment below.