U ovom ćemo članku detaljno istražiti vremenski modul. Naučit ćemo koristiti različite funkcije povezane s vremenom definirane u vremenskom modulu uz pomoć primjera.
Python ima modul imenovan time
za obradu zadataka vezanih uz vrijeme. Da bismo koristili funkcije definirane u modulu, prvo moramo uvesti modul. Evo kako:
import time
Ovdje su najčešće korištene funkcije povezane s vremenom.
Python time.time ()
Na time()
funkcija vraća broj sekundi prošlo od epohe.
Za Unix sustav, January 1, 1970, 00:00:00
na UTC je epoha (točka kada vrijeme počinje).
import time seconds = time.time() print("Seconds since epoch =", seconds)
Python time.ctime ()
time.ctime()
Funkcija nekoliko sekundi prošlo od epohe kao argument i vraća string koji predstavlja lokalnom vremenu.
import time # seconds passed since epoch seconds = 1545925769.9618232 local_time = time.ctime(seconds) print("Local time:", local_time)
Ako pokrenete program, izlaz će biti otprilike:
Lokalno vrijeme: Thu Dec 27 15:49:29 2018
Python time.sleep ()
sleep()
Funkcija obustavlja (kašnjenja) izvršenje tekućeg niti za određeni broj sekundi.
import time print("This is printed immediately.") time.sleep(2.4) print("This is printed after 2.4 seconds.")
Da biste saznali više, posjetite: Python sleep ().
Prije nego što razgovaramo o ostalim funkcijama vezanim uz vrijeme, istražimo time.struct_time
ukratko nastavu.
vrijeme.struct_time Razred
Nekoliko funkcija u time
modulu, kao što su gmtime()
, asctime()
itd bilo uzeti time.struct_time
objekt kao argument ili ga vratite.
Evo primjera time.struct_time
objekta.
time.struct_time (tm_year = 2018, tm_mon = 12, tm_mday = 27, tm_hour = 6, tm_min = 35, tm_sec = 17, tm_wday = 3, tm_yday = 361, tm_isdst = 0)
Indeks | Atribut | Vrijednosti |
---|---|---|
0 | tm_year | 0000,…., 2018,…, 9999 |
1 | tm_mon | 1, 2,…, 12 |
2 | tm_mday | 1, 2,…, 31 |
3 | tm_hour | 0, 1,…, 23 |
4 | tm_min | 0, 1,…, 59 |
5 | tm_sec | 0, 1,…, 61 |
6 | tm_wday | 0, 1,…, 6; Ponedjeljak je 0 |
7 | tm_yday | 1, 2,…, 366 |
8 | tm_isdst | 0, 1 ili -1 |
Vrijednostima (elementima) time.struct_time
objekta moguće je pristupiti pomoću indeksa i atributa.
Python time.localtime ()
localtime()
Funkcija uzima broj sekundi proteklih od epohe kao argument i vraća struct_time
u lokalnom vremenu .
import time result = time.localtime(1545925769) print("result:", result) print("year:", result.tm_year) print("tm_hour:", result.tm_hour)
Kada pokrenete program, izlaz će biti otprilike:
rezultat: time.struct_time (tm_year = 2018, tm_mon = 12, tm_mday = 27, tm_hour = 15, tm_min = 49, tm_sec = 29, tm_wday = 3, tm_yday = 361, tm_isdst = 0) godina: 2018 tm_hour: 15
Ako nijedan argument ili None
je prošao localtime()
, vrijednost vraća time()
se koristi.
Python time.gmtime ()
gmtime()
Funkcija uzima broj sekundi proteklih od epohe kao argument i vraća struct_time
u UTC .
import time result = time.gmtime(1545925769) print("result:", result) print("year:", result.tm_year) print("tm_hour:", result.tm_hour)
Kada pokrenete program, izlaz će biti:
rezultat = vrijeme.struct_time (tm_year = 2018, tm_mon = 12, tm_mday = 28, tm_hour = 8, tm_min = 44, tm_sec = 4, tm_wday = 4, tm_yday = 362, tm_isdst = 0) godina = 2018 tm_hour = 8
Ako nijedan argument ili None
je prošao gmtime()
, vrijednost vraća time()
se koristi.
Python time.mktime ()
mktime()
Funkcija traje struct_time
(ili torka sadrži 9 elemenata odgovara struct_time
) kao argument i vraća sekundi prošlo od epohe u lokalnom vremenu. U osnovi, to je inverzna funkcija od localtime()
.
import time t = (2018, 12, 28, 8, 44, 4, 4, 362, 0) local_time = time.mktime(t) print("Local time:", local_time)
Primjer u nastavku pokazuje kako mktime()
i kako localtime()
su povezani.
import time seconds = 1545925769 # returns struct_time t = time.localtime(seconds) print("t1: ", t) # returns seconds from struct_time s = time.mktime(t) print("s:", seconds)
When you run the program, the output will be something like:
t1: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_isdst=0) s: 1545925769.0
Python time.asctime()
The asctime()
function takes struct_time
(or a tuple containing 9 elements corresponding to struct_time
) as an argument and returns a string representing it. Here's an example:
import time t = (2018, 12, 28, 8, 44, 4, 4, 362, 0) result = time.asctime(t) print("Result:", result)
When you run the program, the output will be:
Result: Fri Dec 28 08:44:04 2018
Python time.strftime()
The strftime()
function takes struct_time
(or tuple corresponding to it) as an argument and returns a string representing it based on the format code used. For example,
import time named_tuple = time.localtime() # get struct_time time_string = time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple) print(time_string)
When you run the program, the output will be something like:
12/28/2018, 09:47:41
Here, %Y
, %m
, %d
, %H
etc. are format codes.
%Y
- year (0001,… , 2018, 2019,… , 9999)%m
- mjesec (01, 02,…, 11, 12)%d
- dan (01, 02,…, 30, 31)%H
- sat (00, 01, …, 22, 23%M
- minute (00, 01,…, 58, 59)%S
- druga (00, 01,…, 58, 61)
Da biste saznali više, posjetite: time.strftime ().
Python time.strptime ()
strptime()
Funkcija analizira niz koji predstavlja vrijeme i vraća struct_time
.
import time time_string = "21 June, 2018" result = time.strptime(time_string, "%d %B, %Y") print(result)
Kada pokrenete program, izlaz će biti:
time.struct_time (tm_year = 2018, tm_mon = 6, tm_mday = 21, tm_hour = 0, tm_min = 0, tm_sec = 0, tm_wday = 3, tm_yday = 172, tm_isdst = -1)