Python time
is a module that allows you to handle dates in the programming language. This module offers many functions and uses UNIX time and objects struct_time
.
The Python module time
plays a crucial role in the management of time -programming data. This module makes it possible to carry out various operations, such as thepresent time, formatting temporal data, and duration calculation when performing functions. Before you can use these features, it is necessary to import the module time
Using the following keyword:
Then you can use different functions with the module. We show you the most important in the following paragraphs. If you are looking for an alternative to Python time
use Python datetime
.
Managed NextCloud of Ionos Cloud
Work as a team in your own cloud
- Data security
- Integrated collaboration tools
- Accommodation in European data centers
time.time()
and the meaning of the unix time
The function time.time()
De Python is based on the concept of UNIX time, which marks a precise moment in the past used as a starting point to measure time in a system. For Windows and many UNIX systems, this starting point is the January 1, 1970 at 00:00:00 UTC. The Python programming language is also based on this moment. This calculation since that date is commonly designated under the term « time elapsed since theepoch ». The function time.time()
Provides the total number of seconds sold since this initial moment of the 1970s. To obtain this measurement, you can use the following code:
import time
temps_passé = time.time()
print("Secondes écoulées depuis l’epoch : ", temps_passé)
python
As we wrote the text, we received this result:
time.ctime()
: indicate the dates in a readable manner
Given that the unix timeless is not very practical to use, the Python module time
Allows you to use it as foundation for a simpler representation of the date. This allows you to benefit from the accuracy of this time measurement while obtaining a more readable date format. The suitable function for this conversion is time.ctime()
which directly transforms UNIX time into a date. Here is the corresponding code:
import time
temps_passé = 1716816325
temps_actuel = time.ctime(temps_passé)
print("Ceci est l’heure actuelle : ", temps_actuel)
python
Our outing then looks like this. Local time is taken into account:
Ceci est l’heure actuelle : Mon May 27 2024 15:25:25
python
Advice
The ideal solution to work on your web project: with Deploy Now from Ionos, deploy sites and applications via Github! You thus benefit from a reliable infrastructure at an advantageous price.
time.sleep()
: Plan the execution of a program
Python time
However, can be much more than simply displaying the time. The module is also used to control the execution of a program or to delay it. The corresponding function is called time.sleep()
and presents itself as:
import time
print("Ceci s’affiche immédiatement... ")
time.sleep(5)
print("...et ceci après 5 secondes.")
python
The next outing is then displayed:
Ceci est affiché immédiatement…
...et ceci après 5 secondes.
python
The second part is only displayed after five seconds. To know what Python is used for time.sleep()
Exactly, see our detailed article in Digital Guide.
Objects struct_time
for python time
Many functions of the Python module time
rely on theobject struct_time
as a base. It is generally used as a parameter or return value and gives a tuple whose value can be indicated by the index or the attribute. The following attributes come into account:
Index | Attribute | Description | Possible values |
---|---|---|---|
0 | tm_eyar | Year | 0000,…, 2024,…, 9999 |
1 | tm_mon | Indication of the month | 1, 2, 3,…, 12 |
2 | TM_MDAY | Day of the month | 1, 2, 3, 31 |
3 | TM_HOUR | Hour in hours | 0, 1, 2,…, 23 |
4 | tm_min | Hour 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 in a year | 1, 2, 3,…, 366 |
8 | TM_ISDST | Summer or winter time | 0 (winter), 1 (summer) or -1 (no indication) |
time.localtime()
: link between struct_time
and time from Epoch
The function time.localtime()
uses an object struct_time
Based on the Unix time. It looks like this in the code:
import time
indication_de_temps = time.time()
heure_actuelle = time.localtime(indication_de_temps)
print(heure_actuelle)
python
The output is as follows:
time.struct_time(tm_year=2024, tm_mon=5, tm_day=27, tm_hour=15, tm_min=25, tm_sec=25, tm_wday=0, tm_yday=148, tm_isdst=1)
python
It is also possible to choose any value for the parameter localtime()
. The code then presents itself as follows:
import time
indication_de_temps = time.time()
heure_actuelle = time.localtime(1716816325)
print(heure_actuelle)
python
The output corresponds again to the example 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)
python
time.mktime()
: the equivalent of localtime()
The function time.mktime()
In the Python module time
acts as the reverse function of localtime()
. You provide an object struct_time
In parameter, and the function returns the hourly stamp corresponding to the number of seconds sold from Epoch. Here's how to use the code:
import time
indication_de_temps = time.mktime(local_time)
print(indication_de_temps)
python
At the output, you get a value in seconds in this format:
time.gmtime()
: universal time coordinated for display
time.gmtime()
largely corresponds to time.localtime()
. The object struct_time
However, is returned here in coordinated universal time. Here is the code:
import time
time_stamp = time.time()
heure_universelle = time.gmtime(time_stamp)
print(heure_universelle)
python
Your outing with this Python function time
therefore differs from the example above to a few details:
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)
python
time.asctime()
: objects struct_time
in character strings
You can use time.asctime()
To convert an object struct_time
in a python chain (string). This could for example look like:
import time
heure = time.localtime()
indication_de_lecture = time.asctime(heure)
print(indication_de_lecture)
python
You get the output directly:
Mon May 27 15:25:25 2024
python
time.strftime()
: Create a character string with Python time
Different variations are possible with time.strftime()
. Here you use different format codes to represent current time in the desired format. To give you an overview of the different format codes, we present here some of the most important:
- %Y : represents the year in format 0001,…, 2024,…, 9999
- %M : represent the month and use the figures from 01 (January) to 12 (December)
- %d : represents the day of 01 of the month until 31
- %H : represent the time in the spectrum from 00 to 23
- %M : represent the minutes and is represented from 00 to 59
- %S : represent the second from 00 to 61
In the code, it could for example look like this:
import time
heure = time.localtime()
indication_de_lecture = time.strftime("%d-%m-%Y, %H:%M:%S", heure)
print(indication_de_lecture)
python
In this case, the exit would be as follows:
27-05-2024, 15:25:25
python
time.strptime()
: Convert chains of characters into objects struct_time
There is also an alternative to this in python time
:: time.strptime()
uses a character string to release an object struct_time
. Here is the corresponding code:
import time
heure = time.strptime(indication_lisible, "%d-%m-%Y, %H : %M:%S")
print(heure)
python
We then get this outing again:
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)
python