프로젝트를 진행하다보니 매주 몇요일 몇시 몇분 부터 몇시 몇분 까지 스케줄을 짜야 하는 경우가 발생하였다.
주단위 반복 스케줄에 이용하면 괜찮을 것 같다
set_time_list 에 시간 리스트를 넣어야 되며 형태는 다음과 같다.
{
"dayWeek": "MON",
"startHour": 9,
"startMin": 30,
"endHour": 10,
"endMin": 20
}
체크 클래스
import datetime
class TimeIncludeChk:
def __init__(self):
self.week_dict = {}
def set_time_list(self, time_list):
for week in range(0, 6 + 1):
self.week_dict[self.week_idx_to_str(week)] = []
for t in time_list:
self.week_dict[t.get("dayWeek")].append(
{'startHour': t.get("startHour"), 'startMin': t.get("startMin"), 'endHour': t.get("endHour"),
'endMin': t.get("endMin")})
def is_include_time_now(self):
now = datetime.datetime.now()
week_idx = self.week_idx_to_str(now.weekday())
hour = now.hour
minute = now.minute
nv = self.eval_hour_min_val(hour, minute)
for w in self.week_dict.get(week_idx):
st = self.eval_hour_min_val(w['startHour'], w['startMin'])
ed = self.eval_hour_min_val(w['endHour'], w['endMin'])
if st <= nv <= ed:
return True
return False
@staticmethod
def eval_hour_min_val(hour, min):
return (hour * 100) + min
@staticmethod
def week_idx_to_str(idx: int):
if idx == 0:
return "MON"
elif idx == 1:
return "TUE"
elif idx == 2:
return "WED"
elif idx == 3:
return "THU"
elif idx == 4:
return "FRI"
elif idx == 5:
return "SAT"
else:
return "SUN"
동작 샘플
from TimeIncludeChk import TimeIncludeChk
datas = [
{'no': 276, 'fountainNo': 'DE0000000007', 'dayWeek': 'MON', 'dayWeekIdx': 0, 'startHour': 9, 'startMin': 0, 'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000, 'offLight': 'Y'},
{'no': 283, 'fountainNo': 'DE0000000007', 'dayWeek': 'MON', 'dayWeekIdx': 0, 'startHour': 13, 'startMin': 0, 'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000, 'offLight': 'N'},
{'no': 277, 'fountainNo': 'DE0000000007', 'dayWeek': 'TUE', 'dayWeekIdx': 1, 'startHour': 9, 'startMin': 0, 'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000, 'offLight': 'Y'},
{'no': 284, 'fountainNo': 'DE0000000007', 'dayWeek': 'TUE', 'dayWeekIdx': 1, 'startHour': 13, 'startMin': 0, 'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000, 'offLight': 'N'},
{'no': 278, 'fountainNo': 'DE0000000007', 'dayWeek': 'WED', 'dayWeekIdx': 2, 'startHour': 9, 'startMin': 0, 'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000, 'offLight': 'Y'},
{'no': 285, 'fountainNo': 'DE0000000007', 'dayWeek': 'WED', 'dayWeekIdx': 2, 'startHour': 13, 'startMin': 0, 'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000, 'offLight': 'N'},
{'no': 286, 'fountainNo': 'DE0000000007', 'dayWeek': 'THU', 'dayWeekIdx': 3, 'startHour': 13, 'startMin': 0, 'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000, 'offLight': 'N'},
{'no': 279, 'fountainNo': 'DE0000000007', 'dayWeek': 'THU', 'dayWeekIdx': 3, 'startHour': 9, 'startMin': 0, 'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000, 'offLight': 'Y'},
{'no': 280, 'fountainNo': 'DE0000000007', 'dayWeek': 'FRI', 'dayWeekIdx': 4, 'startHour': 9, 'startMin': 0, 'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000, 'offLight': 'Y'},
{'no': 287, 'fountainNo': 'DE0000000007', 'dayWeek': 'FRI', 'dayWeekIdx': 4, 'startHour': 13, 'startMin': 0, 'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000, 'offLight': 'N'},
{'no': 281, 'fountainNo': 'DE0000000007', 'dayWeek': 'SAT', 'dayWeekIdx': 5, 'startHour': 9, 'startMin': 0, 'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000, 'offLight': 'Y'},
{'no': 288, 'fountainNo': 'DE0000000007', 'dayWeek': 'SAT', 'dayWeekIdx': 5, 'startHour': 13, 'startMin': 0, 'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000, 'offLight': 'N'},
{'no': 282, 'fountainNo': 'DE0000000007', 'dayWeek': 'SUN', 'dayWeekIdx': 6, 'startHour': 9, 'startMin': 0, 'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000, 'offLight': 'Y'},
{'no': 289, 'fountainNo': 'DE0000000007', 'dayWeek': 'SUN', 'dayWeekIdx': 6, 'startHour': 13, 'startMin': 0, 'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000, 'offLight': 'N'}
]
chk = TimeIncludeChk()
chk.set_time_list(datas)
print(chk.is_include_time_now())