English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, we will discuss the time module in detail. We will learn to use different time-related functions defined in the time module through examples.
Python has a module named time to handle tasks related to time. To use the functions defined in the module, we need to import the module first. It's like this:
import time
Here are some commonly used time-related functions.
The time() function returns the number of seconds since the epoch.
For Unix systems, January 1, 1970, 00:00:00 atUTCis the epoch (where time begins).
import time seconds = time.time() print("Seconds since epoch =", seconds)
The time.ctime() function returns a string representing local time with the number of seconds since the epoch as an argument.
import time # Seconds since the epoch seconds = 1545925769.9618232 local_time = time.ctime(seconds) print("Local time:", local_time)
If you run the program, the output will be similar to:
Local time: Thu Dec 27 15:49:29 2018
The sleep() function pauses (delays) the execution of the current thread for the given number of seconds.
import time print("This is printed immediately.") time.sleep(2.4) print("This is2.4Printed after 1 second. ")
For more information, please visit:Python sleep().
Before discussing other time-related functions, let's briefly explore the time.struct_time class.
Several functions in the time module (such as gmtime(), asctime(), etc.) take a time.struct_time object as an argument or return it.
This is an instance of the time.struct_time object.
time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour =6, tm_min=35, tm_sec=17, tm_wday =3, tm_yday=361, tm_isdst=0)
index | attribute | attribute value |
---|---|---|
0 | tm_year | 0000, .... ,2018, ... ,9999 |
1 | tm_mon | 1,2, ... ,12 |
2 | tm_mday | 1,2, ... ,31 |
3 | tm_hour | 0,1, ... ,23 |
4 | tm_min | 0,1, ... ,59 |
5 | tm_sec | 0,1, ... ,61 |
6 | tm_wday | 0, 1, ... , 6; Monday is 0 |
7 | tm_yday | 1,2, ... ,366 |
8 | tm_isdst | 0,1Or-1 |
You can access the values (elements) of the time.struct_time object using an index or properties.
The localtime() function takes the number of seconds since the epoch as an argument and returns it with a comma.localtimeReturns struct_time.
import time result = time.localtime(1545925769) print("result:", result) print("\nyear:", result.tm_year) print("tm_hour:", result.tm_hour)
When you run the program, the output will be similar to:
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
If no parameter or None is passed to localtime(), the return value of time() is used.
The gmtime() function takes the number of seconds elapsed since epoch as a parameter, and returns struct_time asUTCReturns.
import time result = time.gmtime(1545925769) print("result:", result) print("\nyear:", result.tm_year) print("tm_hour:", result.tm_hour)
When running this program, the output is:
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
If no parameter or None is passed to gmtime(), the return value of time() is used.
The mktime() function takes struct_time (or containing9A tuple of an element corresponds to struct_time) as a parameter, and returns the number of seconds elapsed since the epoch of local time. Essentially, it is the inverse function of localtime().
import time t = (2018, 12, 28, 8, 44, 4, 4, 362, 0) local_time = time.mktime(t) print("Local time:", local_time)
The following example shows the relationship between mktime() and localtime().
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 output will be similar to:
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
The asctime() function takes struct_time (or containing9A tuple of an element corresponds to struct_time) as a parameter, and returns a string representing it. Here is an example:
import time t = (2018, 12, 28, 8, 44, 4, 4, 362, 0) result = time.asctime(t) print("Result:", result)
When running this program, the output is:
Result: Fri Dec 28 08:44:04 2018
The strftime() function takes a struct_time (or its corresponding tuple) as a parameter and returns a string representing it based on the format codes used. For example,
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 output will be similar to:
12/28/2018, 09:47:41
Here %Y, %m, %d, %H, etc. are format codes.
%Y -years [0001, ... ,2018,2019, ... ,9999]
%m -months [01, 02, ... ,11,12]
%d -days [01, 02, ... ,30,31]
%H -hours [00, 01, ... ,22,23
%M -minutes [00, 01, ... ,58,59]
%S -seconds [00, 01, ... ,58,61]
For more information, please visit:time.strftime().
The strptime() function parses the string representing the time and returns a struct_time.
import time time_string = "21 June, 2018" result = time.strptime(time_string, "%d %B, %Y") print(result)
When running this program, the output is:
time.struct_time(tm_year=2018, tm_mon=6, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=172, tm_isdst=-1)