What is Python’s time module?
Python time
is a module for working with date and time information in the programming language. The module uses Unix time and struct_time
objects, and can be used in different ways.
What is the Python time
module and how can it be used?
The Python time
module is essential for implementing and handling time data in the programming language. With this module, it’s possible to display the current time, format dates and times, and specify a timeframe for running a function. Before you can use Python time
, you first need to import the module using the import
keyword:
import time
pythonNow, you can use the module together with different functions. We’ll go over the most important ones in the following sections. If you’re looking for an alternative to Python time
, you can also use Python datetime.
- Industry-leading security
- Communication and collaboration tools
- Hosted and developed in Europe
What is time.time()
and the Unix timestamp?
time.time()
is a function in Python that returns the current time in seconds using the Unix Epoch as its starting point. The Unix Epoch (January 1, 1970 at 00:00:00 (Coordinated Universal Time)) is used by Windows and countless Unix systems as a starting point for measuring time. Python also uses the Unix Epoch. This type of calculation is also referred to as ‘seconds since the Epoch’. Here’s what the code looks like:
import time
elapsed_time = time.time()
print("Elapsed seconds since Epoch: ", elapsed_time)
pythonThe output for this code constantly changes, reflecting the time when the code is executed. For example, when writing this article, we received the following output:
1716816325
pythonHow to make dates readable using time.ctime()
Since having the output in seconds is usually not very useful, Python c.time()
offers programmers a way to convert the Unix timestamp into a readable date. This allows you to benefit from the preciseness of time()
without having to sacrifice readability. Here’s the code:
import time
elapsed_time = 1716816325
current_time = time.ctime(elapsed_time)
print("This is the current time: ", current_time)
pythonThe local time is used and the output looks like this:
This is the current time: Mon May 27 2024 15:25:25
pythonDeploy Now from IONOS is the ideal solution for web projects. Deploy changes in your code with GitHub and benefit from real-time adjustments. Find the right package for your project!
How to schedule program execution with time.sleep()
Python’s time
module can do more than just return the current time though. The module can also be used to control or delay the execution of a program. You can do this using time.sleep()
. Here’s an example:
import time
print("This text is displayed immediately… ")
time.sleep(5)
print("…and this one is displayed 5 seconds later.")
pythonThis is what the output looks like:
This text is displayed immediately…
…and this one is displayed 5 seconds later.
pythonThe second part is displayed five seconds after the first text is displayed. For useful ways to use Python time.sleep(), check out the article on this function in our Digital Guide.
What is a struct_time
object?
struct_time
is an object used by many of the functions in Python’s time
module. It’s a named tuple containing values that can be accessed by index or attribute, and it’s often used as a parameter or return value. The following attributes can be found in a struct_time
object:
Index | Attribute | Description | Possible Values |
---|---|---|---|
0 | tm_year | Year | 0000, …, 2024, …, 9999 |
1 | tm_mon | Month | 1, 2, 3, …, 12 |
2 | tm_mday | Day of the month | 1, 2, 3, 31 |
3 | tm_hour | Time in hours | 0, 1, 2, …, 23 |
4 | tm_min | Time in minutes | 0, 1, 2, …, 59 |
5 | tm_sec | Time in seconds | 0, 1, 2, …, 60, 61 |
6 | tm_wday | Day of the week | 0 (for Monday), …, 6 (for Sunday) |
7 | tm_yday | Day of the year | 1, 2, 3, …, 366 |
8 | tm_isdst | Daylight Saving Time | 0 (Winter), 1 (Summer) or -1 (Information not available) |
time.localtime()
: Connecting struct_time
with Epoch time
The function time.localtime()
uses a Unix timestamp to generate a struct_time
object. Here’s how to write the code:
import time
timestamp = time.time()
current_time = time.localtime(timestamp)
print(current_time)
pythonThis gives us the following output:
time.struct_time(tm_year=2024, tm_mon=5, tm_mday=27, tm_hour=15, tm_min=25, tm_sec=25, tm_wday=0, tm_yday=148, tm_isdst=1)
pythonAlternatively, you can also enter a value for the parameter localtime()
. This is what the code looks like:
import time
timestamp = time.time()
current_time = time.localtime(1716816325)
print(current_time)
pythonThe value we chose for the parameter gives us the same output as above:
time.struct_time(tm_year=2024, tm_mon=5, tm_mday=27, tm_hour=15, tm_min=25, tm_sec=25, tm_wday=0, tm_yday=148, tm_isdst=1)
pythontime.mktime()
: The opposite of localtime()
time.mktime()
is a function from the Python time
module that is essentially the opposite of localtime()
.With this function, Python converts a struct_time
object, which is passed as the argument, to a timestamp, which shows the number of seconds that have passed since the Epoch. Here’s what the code looks like:
import time
timestamp = time.mktime(local_time)
print(timestamp)
pythonThe output is in seconds and is formatted like this:
1716816325
pythontime.gmtime()
: Displaying Coordinated Universal Time
time.gmtime()
is similar to time.localtime()
in many respects. However, the struct_time
object returned by time.gmtime()
is based on Coordinated Universal Time (UTC) rather than local time. Here’s the code:
import time
time_stamp = time.time()
utc_time = time.gmtime(time_stamp)
print(utc_time)
pythonSome of the values output below differ from the output in the last example:
time.struct_time(tm_year=2024, tm_mon=5, tm_mday=27, tm_hour=13, tm_min=25, tm_sec=25, tm_wday=0, tm_yday=148, tm_isdst=0)
pythontime.asctime()
: Converting struct_time
objects into strings
You can use time.asctime()
to convert a struct_time
object into a Python string. Here’s an example:
import time
point_in_time = time.localtime()
readable_format = time.asctime(point_in_time)
print(readable_format)
pythonThis outputs a date that is easy to understand:
Mon May 27 15:25:25 2024
pythontime.strftime()
: Creating a string with Python time
With time.strftime()
, you can use different codes to format time data in different ways that are easy to read. Here are some of the most important codes you can use with this function:
- %Y: Represents the year (Format: 0001, …, 2024, …, 9999)
- %m: Represents the month, using numbers 01 (January) to 12 (December)
- %d: Represents the day of the month, using numbers 01 to 31
- %H: Represents the hour, using numbers 00 to 23
- %M: Represents the minute, using numbers 00 to 59
- %S: Represents the second, using numbers 00 bis 61
Here’s an example:
import time
point_in_time = time.localtime()
readable_format = time.strftime("%d-%m-%Y, %H:%M:%S", point_in_time)
print(readable_format)
pythonHere’s what the output looks like:
27-05-2024, 15:25:25
pythontime.strptime()
: Converting strings into struct_time
objects
Python’s time
module also lets you convert strings into struct_time
objects with time.strptime()
. Here’s how:
import time
point_in_time = time.strptime(readable_format, "%d-%m-%Y, %H: %M:%S")
print(point_in_time)
pythonThis gives us the following output:
time.struct_time(tm_year=2024, tm_mon=5, tm_mday=27, tm_hour=15, tm_min=25, tm_sec=25, tm_wday=0, tm_yday=148, tm_isdst=1)
pythonExplore our Digital Guide to find out more about Python. In addition to instructions on how to install Python, we also have a Python tutorial that covers the basics of the programming language as well as an article on Python operators.