Python ir definējis a modulis 'laiks', kas ļauj mums veikt dažādas darbības, kas saistītas ar laiku, tā pārveidojumiem un attēlojumiem, kas tiek izmantoti dažādās dzīves lietojumprogrammās. Laika sākums sākās no plkst 1970. gada 1. janvāris 12:00 un šis laiks tiek saukts par " laikmets ' Python.
Darbības uz laiku Python
Python laiks.laiks() Funkcija
Python laiks () funkcija tiek izmantota, lai saskaitītu skaitu sekundes pagājušas kopš laikmeta .
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())
Python time.gmtime() funkcija
Python gmtime() funkcija atgriež a struktūra ar 9 vērtībām katrs secīgi apzīmē laika atribūtu. Tas pārvērš sekundes laika atribūtos (dienas, gadi mēneši utt.) līdz noteiktām sekundēm no laikmeta. Ja sekundes nav minētas, laiks tiek skaitīts līdz šim brīdim. Struktūras atribūtu tabula ir dota zemāk.
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())
Izvade:
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)
Python time.asctime() un time.ctime() funkcija
Python time.asctime() funkcija aizņem laika attiecinātu virkni, ko rada gmtime() un atgriež a 24 rakstzīmju virkne, kas apzīmē laiku . Python laiks.ctime() funkcija atgriež a 24 rakstzīmju laika virkne bet ņem sekundes kā argumentu un aprēķina laiku līdz minētajām sekundēm . Ja neviens arguments netiek nodots, laiks tiek aprēķināts līdz šim brīdim.
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())
Izvade:
Time calculated using asctime() is : Tue Aug 2 07:47:02 2016 Time calculated using ctime() is : Tue Aug 2 07:47:02 2016
Python time.sleep() Funkcija
Šo metodi izmanto, lai apturēt programmas izpildi uz argumentos norādīto laiku.
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())
Izvade:
Start Execution : Tue Aug 2 07:59:03 2016 Stop Execution : Tue Aug 2 07:59:07 2016
Python time.mktime() funkcija
Šajā piemērā mēs esam izveidojuši a struct_time objekts ar vērtību virkni katram tā laukam, tad mēs esam nodevuši objektu uz time.mktime() lai to pārvērstu par peldošā komata skaitli, kas apzīmē sekunžu skaitu kopš Unix laikmeta.
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)
Izvade:
Seconds since epoch: 1683709200.0
Python time.localtime() funkcija
Šajā piemērā mēs saucam time.localtime() bez argumenta, lai iegūtu pašreizējo vietējo laiku kā struct_time.
Python3import time current_time = time.localtime() print(current_time)
Izvade:
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)
Python time.strftime() Funkcija
Kā pirmais arguments tiek izmantota formāta virkne, kas norāda vēlamo izvades virknes formātu.
Python3import time now = time.localtime() formatted_time = time.strftime('%Y-%m-%d %H:%M:%S' now) print(formatted_time)
Izvade:
2023-05-10 13:42:04