1. 程式人生 > >Python編寫某個時刻完成某個任務

Python編寫某個時刻完成某個任務

%d 完成 print time __main__ tree from sel 當前時間

<?xml version="1.0"?>
<data>
<work name="work1">
<message>上課</message>
<showtime>2017-11-04 11:38:20</showtime>
</work>
<work name="work2">
<message>參加XXX會議</message>
<showtime>2017-11-04 11:38:22</showtime>
</work>
<work name="work3">
<message>參加XXX聚會</message>
<showtime>2017-11-04 11:38:25</showtime>
</work>
</data>



# _*_ encoding:utf-8 _*_
__data__ = ‘ 8:58‘

import os,sys
from datetime import datetime
import xml.etree.cElementTree as ET


class ClockMsg():

def ShowMsg(self, message):
print (message)

def WaitTime(self, message, showtime):
strnext_time = datetime.strptime(showtime, "%Y-%m-%d %H:%M:%S") #提醒時間
while True:
str_now = datetime.now() #獲取當前時間
str_now_time = str_now.strftime(‘%Y-%m-%d %H:%M:%S‘)

       if str(str_now_time) > str(strnext_time):
          self.ShowMsg("輸入時間不能早於當前時間")
            return
            if str(str_now_time) == str(strnext_time):
self.ShowMsg(message)
return

def GetMsg(self):
tree = ET.parse("message.xml") #打開xml文檔
root = tree.getroot() #獲得root節點
for work in root.findall(‘work‘): #找到root節點下的所有work節點
message = work.find(‘message‘).text #子節點下節點message的值
showtime = work.find(‘showtime‘).text #子節點下節點showtime的值
self.WaitTime(message, showtime)

if __name__ == "__main__":
clockmsg = ClockMsg()
clockmsg.GetMsg()

Python編寫某個時刻完成某個任務