Module time in Python
The next tutorial in the series of lessons on handling Date / Time in Python, TipsMake.com will work with you to find out details about module time and functions related to the time specified in this module. Let's follow it!
Python has a time module used to handle time-related tasks . To use the functions defined in the module, first import this module, do the following:
import time
Here are the most commonly used time-related functions.
The most used functions
- time.time ()
- time.ctime ()
- time.sleep ()
- Class time.struct_time
- time.localtime ()
- time.gmtime ()
- time.mktime ()
- time.asctime ()
- time.strftime ()
- time.strptime ()
time.time ()
The time () function returns the number of seconds from the epoch, or the timestamp value
For Unix systems, 00:00:00 on January 1, 1970 UTC hours are called epochs (the start of time).
import time seconds = time.time() print("So giay tinh tu epoch:", seconds)
The result will look like:
So giay tinh tu epoch: 1562922590.7720907
time.ctime ()
This method converts a time represented by the number of seconds from the epoch to a string representation.
import time # số giây tính từ epoch # viet boi TipsMake.com seconds = 1562983783.9618232 local_time = time.ctime(seconds) print("Local time:", local_time)
Run the program, the result returns the date and time corresponding to the number of seconds transmitted:
Local time: Sat Jul 13 09:09:43 2019
If not passing seconds, the program returns the current time value.
time.sleep ()
The sleep () function stops executing the current thread in the number of seconds passed.
import time print ("Start :", time.ctime()) time.sleep(3) print ("End :", time.ctime())
This method does not return any values, but only the executable delay. Try running the program to see the delay.
Start : Sat Jul 13 09:33:52 2019 End : Sat Jul 13 09:33:57 2019
Before going on to other time-related functions, learn through class time.struct_time .
Class time.struct_time
Some functions in module time, such as gmtime (), asctime () . have time.struct_time is the returned object.
Example of time.struct_time result .
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)
tm_year
Current year: 0000, ., 2018, ., 9999 1 tm_mon
Current month: 1, 2, ., 12 2 tm_mday
Current day: 1, 2, ., 31 3 tm_hour
Current time: 0, 1, ., 23 4 tm_min
Current time: 0, 1, ., 59 5 tm_sec
Current seconds: 0, 1, ., 61 6 tm_wday
Day of the week : 0, 1, ., 6; Monday is calculated at 0 7 tm_yday
Day of the year: 1, 2, ., 366 8 tm_isdst
Determine DST: 0, 1 or -1 time.localtime ()
The localtime () function in the time module takes the number of seconds passed into the argument and returns struct_time in local time.
import time result = time.localtime(1562983783) print("Ket qua:", result) print("nNam:", result.tm_year) print("Gio:", result.tm_hour)
Run the program, the result is:
Ket qua: time.struct_time(tm_year=2019, tm_mon=7, tm_mday=13, tm_hour=9, tm_min=9, tm_sec=43, tm_wday=5, tm_yday=194, tm_isdst=0) Nam: 2019 Gio: 9
If no number of seconds is provided or the value is transferred, the current time returned from the time () function will be used.
time.gmtime ()
The function gmtime () in the time module takes the number of seconds passed as an argument and returns the struct_time by UTC time.
import time result = time.gmtime(1562983783) print("Ket qua:", result) print("nNam:", result.tm_year) print("Gio:", result.tm_hour)
Run the program, the result is:
Ket qua: time.struct_time(tm_year=2019, tm_mon=7, tm_mday=13, tm_hour=2, m_min=9, tm_sec=43, tm_wday=5, tm_yday=194, tm_isdst=0) Nam: 2019 Gio: 2
If no number of seconds is provided or the value is transferred, the current time returned from the time () function will be used.
time.mktime ()
The mktime () function in the time module takes struct_time (or a tuple containing 9 elements corresponding to struct_time) as the argument and returns the number of seconds from the local time epoch. This is the inverse function of localtime ().
import time t = (2019, 7, 13, 9, 9, 43, 5, 194, 0) local_time = time.mktime(t) print("Gio dia phuong:", local_time)
Run the program, the result is:
Gio dia phuong: 1562983783.0
The example below shows how mktime () and localtime () are related.
import time seconds = 1562983783 # trả về struct_time # viet boi TipsMake.com t = time.localtime(seconds) print("t1: ", t) # trả về giây từ struct_time s = time.mktime(t) print("ns:", seconds)
Result:
t1: time.struct_time(tm_year=2019, tm_mon=7, tm_mday=13, tm_hour=9, tm_min=9, tm_sec=43, tm_wday=5, tm_yday=194, tm_isdst=0) s: 1562983783
time.asctime ()
The asctime () function in the time module takes struct_time (or a tuple containing 9 elements corresponding to struct_time) as the argument and returns a string representing that time.
import time t = (2019, 7, 13, 9, 9, 43, 5, 194, 0) result = time.asctime(t) print("Ket qua:", result)
Program results returned:
Ket qua: Sat Jul 13 09:09:43 2019
time.strftime ()
The strftime () function in the time module takes struct_time (or a tuple corresponding to struct_time) as the argument and returns a string representing the time based on the input format code.
import time named_tuple = time.localtime() # lấy struct_time time_string = time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple) print(time_string)
Run the program, the result is:
07/15/2019, 08:46:58
In this example,% Y,% m,% d,% H are format codes .
- % Y: year [0001, ., 2018, 2019, ., 9999]
- % m: month [01, 02, ., 11, 12]
- % d: date [01, 02, ., 30, 31]
- % H: hour [00, 01, ., 22, 23
- % M: month [00, 01, ., 58, 59]
- % S: seconds [00, 01, ., 58, 61]
Read more: strftime function () in the datetime Python module
time.strptime ()
The strptime () function in the time module analyzes a string representing a time, time, and returns struct_time.
import time time_string = "17 July, 2019" result = time.strptime(time_string, "%d %B, %Y") print(result)
The returned result has the form:
time.struct_time(tm_year=2019, tm_mon=7, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=198, tm_isdst=-1)
Read more: strptime () function in the datetime Python module
Previous lesson: Convert the timestamp value in Python
Next lesson: Function sleep () in Python
You should read it
May be interested
- The Module in AngularJSangularjs supports module-oriented approach. the module is used to distinguish the logic, service and application processing ... and make the code clear.
- More than 100 Python exercises have solutions (sample code)more than 100 python code examples are shared by guy zhiwehu on github, however, the solution of this series is written on the old python version. following tipsmake.com will be vietnameseized and edited to suit python 3.x to help you learn and practice python.
- Utility Module in Node.jsthe node.js library module provides many utility modules that help you a lot while developing applications on node.js. the following table lists and describes these utility modules.
- Strftime () function in Pythonthis article shows you how to convert date, time, and time objects into strings corresponding to a given format.
- Bookmark 5 best Python programming learning websitesif you are a developer or you are studying and want to stick with this industry, learn python to add a highlight in your journey.
- For in Python loopin this article, we will learn more about for for loop in python as well as its variations, how to use for to repeat a string of elements in python such as list, string or other iterative objects.
- Manage files and folders in Pythonpython also provides a variety of methods to handle various directory-related operations. in this article, we will learn about managing files and directories in python, namely creating folders, renaming folders, listing folders and working with them.
- Multiple choice quiz about Python - Part 3today's topic quantrimang wants to challenge you is about file and exception handling in python. let's try the following 15 questions!
- 5 choose the best Python IDE for youin order to learn well python, it is essential that you find yourself an appropriate ide to develop. quantrimang would like to introduce some of the best environments to help improve your productivity.
- What is Python? Why choose Python?python is a powerful, high-level, object-oriented programming language, created by guido van rossum. python is easy to learn and emerging as one of the best introductory programming languages for people who are first exposed to programming languages.