Python มีไลบรารี่ให้อ่าน แสดง และรีเซ็ตข้อมูลเวลาได้หลายวิธีโดยใช้โมดูล "เวลา" วันที่ เวลา และเวลาวันที่เป็นอ็อบเจ็กต์ใน Python ดังนั้นเมื่อใดก็ตามที่เราดำเนินการใดๆ กับพวกมัน เราจะจัดการกับอ็อบเจ็กต์จริงๆ ไม่ใช่สตริงหรือการประทับเวลา
ในส่วนนี้ เราจะพูดถึงโมดูล "เวลา" ซึ่งช่วยให้เราจัดการกับการดำเนินการต่างๆ ได้ตรงเวลา
โมดูลเวลาเป็นไปตามแบบแผน "EPOCH" ซึ่งหมายถึงจุดที่เวลาเริ่มต้นขึ้น ในระบบ Unix เวลา “EPOCH” เริ่มตั้งแต่ 1 มกราคม เวลา 00:00 น. 1970 ถึงปี 2038
ในการกำหนดค่าเวลา EPOCH ในระบบของคุณ เพียงพิมพ์โค้ดด้านล่าง -
>>> import time >>> time.gmtime(0)
ผลลัพธ์
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
ติ๊กใน python?
ขีดหมายถึงช่วงเวลาที่เป็นตัวเลขทศนิยมที่วัดเป็นหน่วยวินาที บางครั้งเราได้รับเวลาเป็น Daylight Saving Time (DST) โดยที่นาฬิกาเดินไปข้างหน้า 1 ชั่วโมงในช่วงฤดูร้อนและกลับมาอีกครั้งในฤดูใบไม้ร่วง .
ฟังก์ชันทั่วไปส่วนใหญ่ใน Python Time Module -
1. ฟังก์ชัน time.time()
time() เป็นฟังก์ชันหลักของโมดูลเวลา มันวัดจำนวนวินาทีตั้งแต่ยุคเป็นค่าทศนิยม
ไวยากรณ์
time.time()
โปรแกรมสาธิตการทำงานด้านบน:
import time
print("Number of seconds elapsed since the epoch are : ", time.time()) ผลลัพธ์
Number of seconds elapsed since the epoch are : 1553262407.0398576
เราสามารถใช้ฟังก์ชัน python time เพื่อคำนวณเวลา Wall-clock ที่ผ่านไประหว่างจุดสองจุด
ด้านล่างนี้คือโปรแกรมคำนวณเวลานาฬิกาแขวน:
import time
start = time.time()
print("Time elapsed on working...")
time.sleep(0.9)
end = time.time()
print("Time consumed in working: ",end - start) ผลลัพธ์
Time elapsed on working... Time consumed in working: 0.9219651222229004
2. ฟังก์ชั่น time.clock()
ฟังก์ชัน time.clock() คืนค่าเวลาของตัวประมวลผล ใช้สำหรับการทดสอบประสิทธิภาพ/การเปรียบเทียบ
ไวยากรณ์
time.clock()
ฟังก์ชัน clock() จะคืนค่าเวลาที่ถูกต้องของโปรแกรมและจะแม่นยำกว่าฟังก์ชันอื่น
มาเขียนโปรแกรมโดยใช้ฟังก์ชันสองเวลาด้านบน (ที่กล่าวถึงข้างต้น) เพื่อแยกความแตกต่าง:
import time
template = 'time()# {:0.2f}, clock()# {:0.2f}'
print(template.format(time.time(), time.clock()))
for i in range(5, 0, -1):
print('---Sleeping for: ', i, 'sec.')
time.sleep(i)
print(template.format(time.time(), time.clock())
) ผลลัพธ์
time()# 1553263728.08, clock()# 0.00 ---Sleeping for: 5 sec. time()# 1553263733.14, clock()# 5.06 ---Sleeping for: 4 sec. time()# 1553263737.25, clock()# 9.17 ---Sleeping for: 3 sec. time()# 1553263740.30, clock()# 12.22 ---Sleeping for: 2 sec. time()# 1553263742.36, clock()# 14.28 ---Sleeping for: 1 sec. time()# 1553263743.42, clock()# 15.34
3. ฟังก์ชัน time.ctime()
ฟังก์ชัน time.time() ใช้เวลาใน "วินาทีตั้งแต่ยุค" เป็นอินพุตและแปลเป็นค่าสตริงที่มนุษย์อ่านได้ตามเวลาท้องถิ่น หากไม่มีการส่งผ่านอาร์กิวเมนต์ ระบบจะส่งกลับเวลาปัจจุบัน
import time
print('The current local time is :', time.ctime())
newtime = time.time() + 60
print('60 secs from now :', time.ctime(newtime)) ผลลัพธ์
The current local time is : Fri Mar 22 19:43:11 2019 60 secs from now : Fri Mar 22 19:44:11 2019
4. ฟังก์ชัน time.sleep()
ฟังก์ชัน time.sleep() หยุดการทำงานของเธรดปัจจุบันตามจำนวนวินาทีที่ระบุ ส่งค่าทศนิยมเป็นอินพุตเพื่อให้ได้เวลานอนที่แม่นยำยิ่งขึ้น
สามารถใช้ฟังก์ชัน sleep() ได้ในสถานการณ์ที่เราต้องรอให้ไฟล์ปิดเสร็จหรือปล่อยให้ฐานข้อมูลทำงาน
import time
# using ctime() to display present time
print ("Time starts from : ",end="")
print (time.ctime())
# using sleep() to suspend execution
print ('Waiting for 5 sec.')
time.sleep(5)
# using ctime() to show present time
print ("Time ends at : ",end="")
print (time.ctime()) ผลลัพธ์
Time starts from : Fri Mar 22 20:00:00 2019 Waiting for 5 sec. Time ends at : Fri Mar 22 20:00:05 2019
5. time.struct_time class
time.struct_time เป็นโครงสร้างข้อมูลเดียวที่มีอยู่ในโมดูลเวลา มีอินเทอร์เฟซ tuple ที่มีชื่อและสามารถเข้าถึงได้ผ่านดัชนีหรือชื่อแอตทริบิวต์
ไวยากรณ์
time.struct_time
คลาสนี้มีประโยชน์เมื่อคุณต้องการเข้าถึงฟิลด์เฉพาะของวันที่
คลาสนี้มีฟังก์ชันมากมาย เช่น localtime(), gmtime() และส่งคืนอ็อบเจ็กต์ struct_time
import time
print(' Current local time:', time.ctime())
t = time.localtime()
print('Day of month:', t.tm_mday)
print('Day of week :', t.tm_wday)
print('Day of year :', t.tm_yday) ผลลัพธ์
Current local time: Fri Mar 22 20:10:25 2019 Day of month: 22 Day of week : 4 Day of year : 81
6. ฟังก์ชัน time.strftime()
ฟังก์ชันนี้ใช้ tuple หรือ struct_time ในอาร์กิวเมนต์ที่สอง และแปลงเป็นสตริงตามรูปแบบที่ระบุในอาร์กิวเมนต์แรก
ไวยากรณ์
time.strftime()
ด้านล่างนี้คือโปรแกรมสำหรับใช้งานฟังก์ชัน time.strftime() -
import time
now = time.localtime(time.time())
print("Current date time is: ",time.asctime(now))
print(time.strftime("%y/%m/%d %H:%M", now))
print(time.strftime("%a %b %d", now))
print(time.strftime("%c", now))
print(time.strftime("%I %p", now))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", now)) ผลลัพธ์
Current date time is: Fri Mar 22 20:13:43 2019 19/03/22 20:13 Fri Mar 22 Fri Mar 22 20:13:43 2019 08 PM 2019-03-22 20:13:43 India Standard Time
ตรวจสอบเขตเวลาในหลาม
มีสองคุณสมบัติเวลาซึ่งให้ข้อมูลเขตเวลาแก่คุณ -
1. time.timezone
ส่งกลับค่าชดเชยของเขตเวลาท้องถิ่น (ไม่ใช่ DST) ในรูปแบบ UTC
>>> time.timezone -19800
2. time.tzname – ส่งกลับ tuple ที่มีโซนเวลาท้องถิ่นที่ไม่ใช่ DST และ DST
>>> time.tzname
('India Standard Time', 'India Daylight Time')