# -*- coding: utf-8 -*-
"""
시간대 가용성 체크 유틸리티
회의실의 예약 가능한 시간대를 찾습니다.
"""
import logging
logger = logging.getLogger(__name__)
class TimeChecker:
"""회의실 시간대 가용성 체크 클래스"""
@staticmethod
def time_to_minutes(time_str: str) -> int:
"""
HH:MM을 분으로 변환
Args:
time_str: "HH:MM" 형식의 시간
Returns:
int: 자정부터의 분 단위 시간
Examples:
>>> TimeChecker.time_to_minutes("09:30")
570
"""
h, m = map(int, time_str.split(':'))
return h * 60 + m
@classmethod
def check_available_slots(cls, room_id: int, reservations: list,
start_hour: int = 12, duration: int = 2) -> tuple:
"""
특정 회의실의 가능한 시간대 찾기
Args:
room_id: 회의실 ID
reservations: 전체 예약 목록
start_hour: 검색 시작 시간 (기본: 12시)
duration: 필요 시간 (시간 단위, 기본: 2시간)
Returns:
tuple: (가능한 시간대 리스트, 기존 예약 정보 리스트)
Examples:
>>> slots, reservations = TimeChecker.check_available_slots(123, all_res, 12, 2)
>>> print(slots)
['12:00-14:00', '15:00-17:00']
"""
# 해당 회의실의 예약 찾기
room_reservations = []
for res in reservations:
try:
targets = res.get('reservation', {}).get('reservationTargets', [])
if targets and targets[0].get('room', {}).get('id') == room_id:
start_time = res['startDateTime'][11:16]
end_time = res['endDateTime'][11:16]
room_reservations.append({
'start': cls.time_to_minutes(start_time),
'end': cls.time_to_minutes(end_time),
'title': res.get('reservation', {}).get('title', '제목없음')
})
except Exception as e:
logger.debug(f"예약 파싱 실패: {str(e)}")
continue
# 가능한 시간대 찾기
available_slots = []
end_search_hour = 19 - duration # 19시 종료 가능하도록
for hour in range(start_hour, end_search_hour + 1):
slot_start = hour * 60
slot_end = slot_start + (duration * 60)
# 이 시간대가 비어있는지 확인
is_available = True
for res in room_reservations:
# 겹치는지 확인 (시작 < 기존종료 AND 종료 > 기존시작)
if not (slot_end <= res['start'] or slot_start >= res['end']):
is_available = False
break
if is_available:
available_slots.append(f"{hour:02d}:00-{(hour+duration):02d}:00")
return available_slots, room_reservations