Ενότητα χρόνου Python (με παραδείγματα)

Σε αυτό το άρθρο, θα διερευνήσουμε λεπτομερώς την ενότητα χρόνου. Θα μάθουμε να χρησιμοποιούμε διάφορες συναφείς με το χρόνο συναρτήσεις που καθορίζονται στην ενότητα χρόνου με τη βοήθεια παραδειγμάτων.

Η Python έχει μια ενότητα που ονομάζεται timeγια να χειρίζεται εργασίες που σχετίζονται με το χρόνο. Για να χρησιμοποιήσουμε τις λειτουργίες που ορίζονται στη λειτουργική μονάδα, πρέπει πρώτα να εισαγάγουμε τη λειτουργική μονάδα. Δείτε πώς:

 import time

Εδώ χρησιμοποιούνται συνήθως συναφείς με το χρόνο συναρτήσεις.

Python time.time ()

Η time()συνάρτηση επιστρέφει τον αριθμό των δευτερολέπτων που πέρασαν από την εποχή.

Για το σύστημα Unix, January 1, 1970, 00:00:00στο UTC είναι η εποχή (το σημείο από το οποίο αρχίζει ο χρόνος).

 import time seconds = time.time() print("Seconds since epoch =", seconds) 

Python time.ctime ()

Η time.ctime()συνάρτηση διαρκεί δευτερόλεπτα από την εποχή ως όρισμα και επιστρέφει μια συμβολοσειρά που αντιπροσωπεύει την τοπική ώρα.

 import time # seconds passed since epoch seconds = 1545925769.9618232 local_time = time.ctime(seconds) print("Local time:", local_time) 

Εάν εκτελέσετε το πρόγραμμα, η έξοδος θα είναι όπως:

 Τοπική ώρα: Πέμ 27 Δεκεμβρίου 15:49:29 2018

Python time.sleep ()

Η sleep()συνάρτηση αναστέλλει (καθυστερήσεις) εκτέλεση του τρέχοντος νήματος για τον δεδομένο αριθμό δευτερολέπτων.

 import time print("This is printed immediately.") time.sleep(2.4) print("This is printed after 2.4 seconds.") 

Για να μάθετε περισσότερα, επισκεφθείτε: Python sleep ().

Πριν μιλήσουμε για άλλες συναρτήσεις που σχετίζονται με το χρόνο, ας εξερευνήσουμε την time.struct_timeτάξη εν συντομία.

time.struct_time Class

Πολλές συναρτήσεις στη λειτουργική timeμονάδα, όπως gmtime(), asctime()κ.λπ. είτε λαμβάνουν time.struct_timeαντικείμενο ως επιχείρημα είτε επιστρέφουν.

Εδώ είναι ένα παράδειγμα time.struct_timeαντικειμένου.

 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) 
Δείκτης Χαρακτηριστικό Αξίες
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; Η Δευτέρα είναι 0
7 tm_yday 1, 2,…, 366
8 tm_isdst 0, 1 ή -1

Οι τιμές (στοιχεία) του time.struct_timeαντικειμένου είναι προσβάσιμες χρησιμοποιώντας δείκτες και χαρακτηριστικά.

Python time.localtime ()

Η localtime()συνάρτηση παίρνει τον αριθμό των δευτερολέπτων που πέρασαν από την εποχή ως όρισμα και επιστρέφει struct_timeσε τοπική ώρα .

 import time result = time.localtime(1545925769) print("result:", result) print("year:", result.tm_year) print("tm_hour:", result.tm_hour) 

Όταν εκτελείτε το πρόγραμμα, η έξοδος θα είναι όπως:

 αποτέλεσμα: 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) έτος: 2018 tm_hour: 15 

Εάν δεν υπάρχει όρισμα ή Noneμεταβιβάζεται localtime(), χρησιμοποιείται η τιμή που επιστρέφεται από time().

Python time.gmtime ()

The gmtime() function takes the number of seconds passed since epoch as an argument and returns struct_time in UTC.

 import time result = time.gmtime(1545925769) print("result:", result) print("year:", result.tm_year) print("tm_hour:", result.tm_hour) 

When you run the program, the output will be:

 result = time.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) year = 2018 tm_hour = 8 

If no argument or None is passed to gmtime(), the value returned by time() is used.

Python time.mktime()

The mktime() function takes struct_time (or a tuple containing 9 elements corresponding to struct_time) as an argument and returns the seconds passed since epoch in local time. Basically, it's the inverse function of localtime().

 import time t = (2018, 12, 28, 8, 44, 4, 4, 362, 0) local_time = time.mktime(t) print("Local time:", local_time) 

The example below shows how mktime() and localtime() are related.

 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 - μήνας (01, 02,…, 11, 12)
  • %d - ημέρα (01, 02,…, 30, 31)
  • %H - ώρα (00, 01,…, 22, 23
  • %M - λεπτά (00, 01,…, 58, 59)
  • %S - δεύτερο (00, 01,…, 58, 61)

Για να μάθετε περισσότερα, επισκεφθείτε τη διεύθυνση: time.strftime ().

Python time.strptime ()

Η strptime()συνάρτηση αναλύει μια συμβολοσειρά που αντιπροσωπεύει τον χρόνο και τις επιστροφές struct_time.

 import time time_string = "21 June, 2018" result = time.strptime(time_string, "%d %B, %Y") print(result) 

Όταν εκτελείτε το πρόγραμμα, η έξοδος θα είναι:

 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) 

ενδιαφέροντα άρθρα...