#!/usr/bin/env python3.4
#March 27th 2017
#Class
import time
class DayPlanner:
def __init__(self):
self.schedule = []
def add(self, datetime, activity):
tt = time.strptime(datetime, '%Y-%m-%d %H:%M')
self.schedule.append( [time.mktime(tt), activity] )
def delete(self, datetime, activity):
tt = time.mktime(time.strptime(datetime, '%Y-%m-%d %H:%M'))
print("\n")
for i in range(len(self.schedule)):
if activity == self.schedule[i][1] and tt == self.schedule[i][0]:
print("[[Match Found]]")
print("\n")
del self.schedule[i]
break
def show(self):
for i in sorted(self.schedule):
print('{:s} {:s}'.format(time.ctime(i[0]), i[1]) )
#-------
sched = DayPlanner()
while True:
date = input('date and time? ')
if date == '':
break
act = input('activity? ')
sched.add(date, act)
print("\n")
sched.show()
print("\n")
while True:
date = input('Which date and time should I delete? ')
if date == '':
break
act = input('which activity should I delete? ')
sched.delete(date,act)
print("\n")
print("New Schedule")
sched.show()
print("\n")
print("\n")
while True:
date = input('Which Date should I show the schedule for?')
if date == '':
break
print("\n")
print("Schedule for the day")
sched.show()
所以,我有一个很好的设置来添加和删除我的条目,即使时间格式是疯狂的输入全部。
要添加到计划中,您需要输入确切的格式,例如2017-5-5 3:00
。
我的主要问题是如何格式化最后sched.show
仅显示当天的日程安排?
我知道我需要对def show()
进行更改。
我应该创建一个新的def
并让它专门显示当天吗?
我在这里画一个空白,希望得到一些帮助!
答案 0 :(得分:0)
def showday(self,day):
for i in sorted(self.schedule):
sec = i[0]
tt1=time.localtime(sec)
stt=time.strptime(day,'%Y-%m-%d')
if stt.tm_year == tt1.tm_year and stt.tm_mon == tt1.tm_mon and stt.tm_mday == tt1.tm_mday:
print(time.ctime(sec), i[1])
我用这个def解决了我的问题。我最大的资源是jupyter找到一些关于时间的帮助。我不会说谎花了一点但是我把它弄清楚它的效果非常好。发布我的存档解决方案,以防万一其他人提出类似的东西。