Python a défini un module « temps » qui nous permet de gérer diverses opérations concernant le temps, ses conversions et représentations qui trouvent son utilisation dans diverses applications de la vie. Le début des temps a commencé à mesurer à partir de 1er janvier 00h00 1970 et cette fois-ci est appelée « époque ' en Python.
Opérations à temps en Python
Fonction Python time.time()
Heure Python() La fonction est utilisée pour compter le nombre de secondes écoulées depuis l'époque .
Python3
# Python code to demonstrate the working of # time() # importing 'time' module for time operations import time # using time() to display time since epoch print ('Seconds elapsed since the epoch are : 'end='') print (time.time())
Fonction Python time.gmtime()
Python gmtime() la fonction renvoie un structure avec 9 valeurs chacun représentant un attribut temporel en séquence. Il convertit secondes en attributs de temps (jours, années, mois, etc.) jusqu'à quelques secondes spécifiées à partir de l'époque. Si aucune seconde n'est mentionnée, le temps est calculé jusqu'à présent. La table attributaire de la structure est donnée ci-dessous.
Index Attributes Values 0 tm_year 2008 1 tm_mon 1 to 12 2 tm_mday 1 to 31 3 tm_hour 0 to 23 4 tm_min 0 to 59 5 tm_sec 0 to 61 (60 or 61 are leap-seconds) 6 tm_wday 0 to 6 7 tm_yday 1 to 366 8 tm_isdst -1 0 1 where -1 means Library determines DSTPython3
# Python code to demonstrate the working of gmtime() import time # using gmtime() to return the time attribute structure print ('Time calculated acc. to given seconds is : ') print (time.gmtime())
Sortir:
Time calculated acc. to given seconds is : time.struct_time(tm_year=2016 tm_mon=8 tm_mday=2 tm_hour=7 tm_min=12 tm_sec=31 tm_wday=1 tm_yday=215 tm_isdst=0)
Fonction Python time.asctime() et time.ctime()
Python time.asctime() la fonction prend une chaîne attribuée au temps produite par gmtime() et renvoie un Chaîne de 24 caractères indiquant l'heure . Python time.ctime() la fonction renvoie un Chaîne temporelle de 24 caractères mais prend quelques secondes comme argument et calcule le temps jusqu'aux secondes mentionnées . Si aucun argument n'est transmis, le temps est calculé jusqu'à présent.
Python3
# Python code to demonstrate the working of # asctime() and ctime() # importing 'time' module for time operations import time # initializing time using gmtime() ti = time.gmtime() # using asctime() to display time acc. to time mentioned print ('Time calculated using asctime() is : 'end='') print (time.asctime(ti)) # using ctime() to display time string using seconds print ('Time calculated using ctime() is : ' end='') print (time.ctime())
Sortir:
Time calculated using asctime() is : Tue Aug 2 07:47:02 2016 Time calculated using ctime() is : Tue Aug 2 07:47:02 2016
Fonction Python time.sleep()
Cette méthode est utilisée pour arrêter l'exécution du programme pendant la durée spécifiée dans les arguments.
Python3# Python code to demonstrate the working of # sleep() # importing 'time' module for time operations import time # using ctime() to show present time print ('Start Execution : 'end='') print (time.ctime()) # using sleep() to hault execution time.sleep(4) # using ctime() to show present time print ('Stop Execution : 'end='') print (time.ctime())
Sortir:
Start Execution : Tue Aug 2 07:59:03 2016 Stop Execution : Tue Aug 2 07:59:07 2016
Fonction Python time.mktime()
Dans cet exemple, nous avons créé un struct_time objet avec un tuple de valeurs pour chacun de ses champs alors nous avons passé l'objet au temps.mktime() pour le convertir en un nombre à virgule flottante représentant le nombre de secondes depuis l'époque Unix.
Python3import time # Create a struct_time object representing a date and time my_time = time.strptime('2023-05-10 14:30:00' '%Y-%m-%d %H:%M:%S') # Convert the struct_time object to a floating-point number seconds_since_epoch = time.mktime(my_time) print('Seconds since epoch:' seconds_since_epoch)
Sortir:
Seconds since epoch: 1683709200.0
Fonction Python time.localtime()
Dans cet exemple, nous appelons heure.localtime() sans argument pour obtenir l'heure locale actuelle sous forme de struct_time.
Python3import time current_time = time.localtime() print(current_time)
Sortir:
time.struct_time(tm_year=2023 tm_mon=5 tm_mday=10 tm_hour=12 tm_min=42 tm_sec=51 tm_wday=2 tm_yday=130 tm_isdst=0)
Fonction Python time.strftime()
Il prend une chaîne de format comme premier argument qui spécifie le format souhaité de la chaîne de sortie.
Python3import time now = time.localtime() formatted_time = time.strftime('%Y-%m-%d %H:%M:%S' now) print(formatted_time)
Sortir:
2023-05-10 13:42:04