Funzioni temporali in Python | Imposta 1 (time(), ctime(), sleep()...)

Python ha definito a modulo 'tempo' che ci permette di gestire varie operazioni riguardanti il ​​tempo, le sue conversioni e rappresentazioni che trovano il suo utilizzo in varie applicazioni nella vita. L'inizio del tempo ha iniziato a misurare da 1 gennaio 00:00 1970 e proprio questa volta è definita " epoca ' in Python.

Operazioni sul tempo in Python 

Funzione Python time.time()

Tempo di Python() la funzione viene utilizzata per contare il numero di secondi trascorsi dall'epoca

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  ())   

Funzione Python time.gmtime()

Python gmtime() la funzione restituisce a struttura con 9 valori ciascuno rappresenta un attributo temporale in sequenza. Si converte secondi negli attributi temporali (giorni anni mesi ecc.) fino ai secondi specificati dall'epoca. Se non vengono menzionati i secondi, il tempo viene calcolato fino ad oggi. La tabella degli attributi della struttura è riportata di seguito. 

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 DST 
Python3
   # 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  ())   

Produzione: 

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) 

Funzione Python time.asctime() e time.ctime()

Python tempo.asctime() La funzione accetta una stringa con assegnazione temporale prodotta da ora dell'ora() e restituisce a Stringa di 24 caratteri che indica l'ora . Python tempo.ctime() la funzione restituisce a Stringa temporale di 24 caratteri ma prende i secondi come argomento e calcola il tempo fino ai secondi menzionati . Se non viene passato alcun argomento, il tempo viene calcolato fino al presente.

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  ())   

Produzione: 

Time calculated using asctime() is : Tue Aug 2 07:47:02 2016 Time calculated using ctime() is : Tue Aug 2 07:47:02 2016 

Funzione Python time.sleep()

Questo metodo è utilizzato per interrompere l'esecuzione del programma per il tempo specificato negli argomenti.

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  ())   

Produzione: 

Start Execution : Tue Aug 2 07:59:03 2016 Stop Execution : Tue Aug 2 07:59:07 2016 

Funzione Python time.mktime()

In questo esempio abbiamo creato un file struct_time oggetto con una tupla di valori per ciascuno dei suoi campi, abbiamo passato l'oggetto al file tempo.mktime() per convertirlo in un numero a virgola mobile che rappresenta il numero di secondi trascorsi dall'epoca di Unix.

Python3
   import   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  )   

Produzione:

Seconds since epoch: 1683709200.0 

Funzione Python time.localtime()

In questo esempio chiamiamo ora.ora locale() senza argomenti per ottenere l'ora locale corrente come struct_time.

Python3
   import   time   current_time   =   time  .  localtime  ()   print  (  current_time  )   

Produzione:

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) 

Funzione Python time.strftime()

Prende una stringa di formato come primo argomento che specifica il formato desiderato della stringa di output.

Python3
   import   time   now   =   time  .  localtime  ()   formatted_time   =   time  .  strftime  (  '%Y-%m-  %d   %H:%M:%S'     now  )   print  (  formatted_time  )   

Produzione:

2023-05-10 13:42:04