अजगर समय मॉड्यूल (उदाहरण के साथ)

इस लेख में, हम विस्तार से समय मॉड्यूल का पता लगाएंगे। हम समय मॉड्यूल में परिभाषित विभिन्न समय-संबंधित कार्यों का उपयोग उदाहरणों की मदद से करना सीखेंगे।

पायथन में timeसमय से संबंधित कार्यों को संभालने के लिए एक मॉड्यूल है। मॉड्यूल में परिभाषित कार्यों का उपयोग करने के लिए, हमें पहले मॉड्यूल को आयात करना होगा। ऐसे:

 import time

यहां आमतौर पर समय-संबंधित कार्यों का उपयोग किया जाता है।

अजगर समय.समय ()

time()समारोह रिटर्न सेकंड की संख्या अवधि के बाद पारित कर दिया।

यूनिक्स प्रणाली के लिए, January 1, 1970, 00:00:00पर यूटीसी युग (बिंदु जहां समय शुरू होता है) है।

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

अजगर का समय। समय ()

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

अजगर का समय। सो ()

sleep()सेकंड की दी गई संख्या के लिए मौजूदा सूत्र के समारोह निलंबित (देरी) निष्पादन।

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

अधिक जानने के लिए, जाएँ: पायथन स्लीप ()।

इससे पहले कि हम अन्य समय-संबंधित कार्यों के बारे में बात करें, आइए time.struct_timeसंक्षेप में कक्षा का अन्वेषण करें ।

time.struct_time क्लास

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_yday = 3, tm_yday = 361, tm_isdst = 0) 
सूचकांक विशेषता मान
tm_year 0000,…।, 2018,…, 9999
1 है tm_mon 1, 2,…, 12
tm_mday 1, 2,…, 31
tm_hour 0, 1,…, 23
tm_min 0, 1,…, 59
tm_sec 0, 1,…, 61
tm_wday 0, 1,…, 6; सोमवार 0 है
tm_yday 1, 2,…, 366
tm_isdst 0, 1 या -1

time.struct_timeऑब्जेक्ट के मूल्य (तत्व) दोनों सूचकांकों और विशेषताओं का उपयोग करके सुलभ हैं।

अजगर का समय

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: t t 

यदि कोई तर्क या Noneपारित नहीं किया जाता है localtime(), तो लौटाए गए मान time()का उपयोग किया जाता है।

अजगर समय.समय ()

gmtime()समारोह एक तर्क और रिटर्न के रूप में अवधि के बाद पारित कर दिया सेकंड की संख्या लेता struct_timeमें यूटीसी

 import time result = time.gmtime(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 = 28, tm_hour = 8, tm_min = 44, tm_sec = 4, tm_wday = 4, tm_yday = 362, tm_isdst = 0) वर्ष = 2018 = t8 

यदि कोई तर्क या Noneपारित नहीं किया जाता है gmtime(), तो लौटाए गए मान time()का उपयोग किया जाता है।

अजगर समय

mktime()समारोह लेता है struct_time(या एक टपल 9 इसी तत्व युक्त struct_time) एक तर्क के रूप और सेकंड स्थानीय समय में अवधि के बाद पारित कर दिया देता है। असल में, इसका उलटा कार्य है localtime()

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

नीचे दिए गए उदाहरण से पता चलता है कि कैसे mktime()और localtime()संबंधित हैं।

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

अजगर का समय

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_yday = 3, tm_yday = 172, tm_isdst = -1) 

दिलचस्प लेख...