With practical examples, let’s take you through some of the most useful ways to use the time library.
Getting Started With the time Library
The time module is one of the most intriguing treasures in Python’s versatile library repository. It’s built into Python, so you don’t need to install it separately. You just need to import it into your code to start using it.
Although Python’s time library has many methods, and it may seem overwhelming at first, we’ll focus on the ones you’re likely to use most often.
To start using the time module, import it in your script with the following statement:
Get the Default Epoch
The epoch signature of the time library starts on the first day and hour of 1970 on Unix systems by default. This is platform-dependent, though. So the epoch or starting time and date may be different, depending on the operating system you’re using.
To view the default epoch on your OS:
The above tuple output (an object of type time.struct_time) contains the time structure of the time module. It’s where the library stores date and time details by default. From the result, tm_year dates back to 1970, with some other properties having zero as their default value. By the way, gmtime returns the Greenwich Mean Time which may be different from your local time zone.
You can get the local epoch using time.localtime() instead:
Running the above code may change the values of individual properties, depending on your local time zone. For example, during British Summer Time in the UK, tm_hour will be “1” instead of GMT’s “0”.
See the Current Time and Date Detail
To get the current time as a time.struct_time object, call time.gmtime() without any arguments:
As you did in the previous examples, replace time.gmtime() with time.localtime() to see your current local time.
Calculate the Time Between the Epoch and the Current Time
Use the time.time() function to see the number of seconds between the current time and the epoch:
To see the year difference:
Extract the Current Local Time
It’s not often necessary in practice, but you can access each of the values in a time.struct_time object by accessing its properties directly.
For example, to get the current hour, minute, and year from your current local time:
To automate the output instead of hardcoding it as we did above, you can output the current local time using the time.ctime() function.
Here’s how to see the current local time using time.ctime():
But the above option doesn’t give you any control over the output.
To manipulate the output, you can stringify the time format using time.strftime() with string annotations:
In the above code, %a represents the abbreviated current day name. The %p represents morning or afternoon, in your locale. In English, this translates to either AM or PM.
You can take a look at all possible string annotations for strftime() in the Python docs in your free time. Feel free to tweak them as you like, to format a time to your specific requirements. You can experiment by adding more string options to the above example and seeing what they output. For instance, you can replace %I with %H to get a 24-hour format of the current time instead.
Schedule Code With Python’s time Module
You can delay code execution using the time.sleep() function. This function can be helpful when you need to schedule requests or run a multithreaded program using Python.
Let’s see how it works:
Note that time.sleep() only accepts float and integer arguments, which it translates to seconds. So if you’re scheduling for the next hour or minute, you’ll need to convert these to seconds.
To delay the output by an hour, for instance:
Create a Countdown Timer Using time.sleep()
Using the time.sleep() function and Python’s for loop, let’s create a countdown timer that accepts a user’s input:
When you run the above code and set your time value, it decreases the specified value every second until it reaches zero.
Make Proper Use of Python’s time Library
You now know how to access the current time from your Python code and manipulate time and date output. The time module is also a valuable tool for tracking requests on the server end when you’re using Python for backend programming.
And because the outputs are mostly integers or floats, you can operate on them as you like to get your time in hours, days, months, or years.