Time Functionality In Python
Published: Aug 6, 2021
Last updated: Aug 6, 2021
This is Day 18 of the #100DaysOfPython challenge.
This post will use the time
All of the code used in this post can be found in my GitHub repository
Prerequisites
Getting started
Let's create the hello-python-time-module
directory and install Pillow.
# Make the `hello-python-time-module` directory $ mkdir hello-python-time-module $ cd hello-python-time-module # Init the virtual environment $ pipenv --three $ pipenv install --dev ipython
At this stage, we are ready to explore using the time
module using iPython
.
To do so, run pipenv run ipython
from the command line to open up the REPL.
Importing the module
First off, we will want to import the time
module. We can do so from within the REPL with the following:
import time
Once imported, we can check that we have access to the time
module by checking the __name__
attribute and playing around with a few methods.
time.__name__ # 'time' time.time() # 1628200068.664737
The method time
that we called on the module returns the time in seconds since the epoch as a floating point number.
On most systems, the epoch is January 1st, 1970 at midnight. This is currently referred to as Unix time.
Getting the local time
We can also use the time module to get the local or GM time, as well as format the time to a more readable format.
time.localtime() # time.struct_time(tm_year=2021, tm_mon=8, tm_mday=6, tm_hour=7, tm_min=46, tm_sec=20, tm_wday=4, tm_yday=218, tm_isdst=0) time.gmtime() # time.struct_time(tm_year=2021, tm_mon=8, tm_mday=5, tm_hour=21, tm_min=46, tm_sec=32, tm_wday=3, tm_yday=217, tm_isdst=0) time.asctime(time.localtime()) # 'Fri Aug 6 07:51:53 2021'
Formatting and parsing
We have control over how we format and parse time with the strftime
strptime
time.strptime("30 Nov 00", "%d %b %y") # time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1) time.strftime("%m/%d/%Y, %H:%M:%S") # '08/06/2021, 07:56:44' time.strftime("%d %b %y", time.strptime("30 Nov 00", "%d %b %y")) # '30 Nov 00'
Sleep
We can use the time module to set a timer and sleep for a certain amount of time.
time.sleep(5) # notice that the REPL does not return until 5 seconds have passed
This can be useful when looping through intervals based on conditional logic.
Comparing times
We can use the datetime
time
objects to datetime
objects.
We need to do so with the datetime.datetime.fromtimestamp
method.
import datetime # Check time now is less than 1 second later datetime.datetime.fromtimestamp(time.time()) < datetime.datetime.now() + datetime.timedelta(seconds=1) # True # Check time now is after 1 second before datetime.datetime.fromtimestamp(time.time()) < datetime.datetime.now() - datetime.timedelta(seconds=1) # False
For more examples of datetime
comparisons, see my post on Datetime In Python
Summary
Today's post demonstrates some usages of the time
We covered a number of the standard methods and finished with an example on how to compare using the datetime
Resources and further reading
Photo credit: pawel_czerwinski
Dennis O'Keeffe
Melbourne, Australia
1,200+ PEOPLE ALREADY JOINED ❤️️
Get fresh posts + news direct to your inbox.
No spam. We only send you relevant content.
Time Functionality In Python
Introduction