def split_borders( range_obj: RangeDateTime ) -> List[Union[RangeDateTime, Range, None]]: starting_datetime = range_obj.starting_datetime ending_datetime = range_obj.ending_datetime delta = ending_datetime.date() - starting_datetime.date() if delta.days == 0: return [range_obj, None, None] time_obj = DateUtil.str_to_time('23:59') aux_ending_datetime = datetime.combine(starting_datetime.date(), time_obj) left_range_datetime = RangeDateTime(starting_datetime, aux_ending_datetime) time_obj = DateUtil.str_to_time('00:00') aux_starting_datetime = datetime.combine(ending_datetime.date(), time_obj) right_range_datetime = RangeDateTime(aux_starting_datetime, ending_datetime) body = None if delta.days > 1: body = Range(starting_datetime.date() + timedelta(days=1), ending_datetime.date() - timedelta(days=1)) return [left_range_datetime, body, right_range_datetime]
def test_are_intersection3(self): r1 = RangeDateTime(datetime(2019, 5, 1, 10, 0, 0), datetime(2019, 5, 4, 10, 0, 0)) r2 = RangeDateTime(datetime(2019, 5, 4, 10, 0, 1), datetime(2019, 5, 5, 10, 0, 0)) assert not RangeDateTimeOperator.are_intersection(r1, r2)
def test_split_borders_4(self): range_datetime = RangeDateTime(datetime(2019, 5, 1, 10, 0, 0), datetime(2019, 5, 1, 10, 0, 0)) response = RangeDateTimeOperator.split_borders(range_datetime) expected = [ RangeDateTime(datetime(2019, 5, 1, 10, 0, 0), datetime(2019, 5, 1, 10, 0, 0)), None, None, ] assert response == expected
def get_intersection(r1: RangeDateTime, r2: RangeDateTime) -> Optional[RangeDateTime]: if RangeDateTimeOperator.are_intersection(r1, r2): return RangeDateTime( max(r1.starting_datetime, r2.starting_datetime), min(r1.ending_datetime, r2.ending_datetime)) return None
def test_split_borders_1(self): range_datetime = RangeDateTime(datetime(2019, 5, 1, 10, 0, 0), datetime(2019, 5, 4, 10, 0, 0)) response = RangeDateTimeOperator.split_borders(range_datetime) expected = [ RangeDateTime(datetime(2019, 5, 1, 10, 0, 0), datetime(2019, 5, 1, 23, 59, 0)), Range(datetime(2019, 5, 2).date(), datetime(2019, 5, 3).date()), RangeDateTime(datetime(2019, 5, 4, 0, 0, 0), datetime(2019, 5, 4, 10, 0, 0)), ] assert response == expected
def get_as_a_range_datetime(self) -> RangeDateTime: starting_date = self.day_off_assignation.starting_date starting_time = self.day_off_assignation.starting_time ending_date = self.day_off_assignation.ending_date ending_time = self.day_off_assignation.ending_time return RangeDateTime(datetime.combine(starting_date, starting_time), datetime.combine(ending_date, ending_time))
def get_real_intersection(self, date_obj: dateclass, aux_intersection: RangeDateTime): starting_datetime = datetime.combine( date_obj, aux_intersection.starting_datetime.time()) ending_datetime = datetime.combine( date_obj, aux_intersection.ending_datetime.time()) return RangeDateTime(starting_datetime, ending_datetime)
def create_range_datetime(starting_time: time, ending_time: time, base_date: dateclass) -> RangeDateTime: starting_datetime = datetime.combine(base_date, starting_time) if starting_time > ending_time: next_base_date = base_date + timedelta(days=1) ending_datetime = datetime.combine(next_base_date, ending_time) else: ending_datetime = datetime.combine(base_date, ending_time) return RangeDateTime(starting_datetime, ending_datetime)
def day_to_range_datetime(self, day, collision_type): if collision_type == PREVIOUS: day_starting_datetime = datetime.combine(self.base_prev_date, day.starting_time) elif collision_type == CURRENT: day_starting_datetime = datetime.combine(self.base_current_date, day.starting_time) elif collision_type == NEXT: day_starting_datetime = datetime.combine(self.base_next_date, day.starting_time) else: raise Exception(f'Collision type not allowed: {collision_type}') ending_date = day_starting_datetime.date() if day.starting_time > day.ending_time: ending_date = ending_date + timedelta(days=1) day_ending_datetime = datetime.combine(ending_date, day.ending_time) return RangeDateTime(day_starting_datetime, day_ending_datetime)
def test_is_in_5(self): day_off_assignations_data = [{ 'person_id': 1, 'starting_date': '2019-1-1', 'ending_date': '2019-1-6', 'starting_time': '06:00', 'ending_time': '05:59' }] day_off_assignations = create_proxy_day_off_assignation( day_off_assignations_data) facade = DayOffAssignationFacade(day_off_assignations[0]) range_datetime = RangeDateTime( datetime(2019, 1, 2, 1, 10), datetime(2019, 1, 4, 2, 10), ) assert facade.is_in(range_datetime)
def test_is_not_in_6(self): day_off_assignations_data = [{ 'person_id': 1, 'starting_date': '2019-1-1', 'ending_date': '2019-1-9', 'starting_time': '20:00', 'ending_time': '08:00' }] day_off_assignations = create_proxy_day_off_assignation( day_off_assignations_data) facade = DayOffAssignationFacade(day_off_assignations[0]) range_datetime = RangeDateTime( datetime(2019, 1, 2, 20, 59), datetime(2019, 1, 3, 8, 1), ) assert not facade.is_in(range_datetime)
def _build_example_from_date( self, date_obj: dateclass) -> Optional[RangeDateTime]: starting_date = self.day_off_assignation.starting_date ending_date = self.day_off_assignation.ending_date if starting_date <= date_obj <= ending_date: example_starting_datetime = datetime.combine( date_obj, self.day_off_assignation.starting_time) if self.one_day_to_another(): example_ending_datetime = datetime.combine( date_obj + timedelta(days=1), self.day_off_assignation.ending_time) else: example_ending_datetime = datetime.combine( date_obj, self.day_off_assignation.ending_time) return RangeDateTime(example_starting_datetime, example_ending_datetime) else: return None
def test_are_intersection1(self): r1 = RangeDateTime(datetime(2019, 5, 1), datetime(2019, 5, 4)) r2 = RangeDateTime(datetime(2019, 5, 3), datetime(2019, 5, 5)) assert RangeDateTimeOperator.are_intersection(r1, r2)