Skip to content

Commit

Permalink
feat: add contains methods
Browse files Browse the repository at this point in the history
Closes #8
  • Loading branch information
MicaelJarniac committed Dec 21, 2021
1 parent eb83bbf commit 6d5a604
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/timeranges/_timeranges.py
Expand Up @@ -46,9 +46,12 @@ def validate(self) -> None:
def __attrs_post_init__(self) -> None:
self.validate()

def __contains__(self, t: time) -> bool:
def contains(self, t: time) -> bool:
return self.start <= t <= self.end

def __contains__(self, t: time) -> bool:
return self.contains(t)


@attr.define
class TimeRanges:
Expand Down Expand Up @@ -90,9 +93,12 @@ def merge(self, interpolate: timedelta = timedelta(0)) -> None:
def __attrs_post_init__(self) -> None:
self.validate()

def __contains__(self, t: time) -> bool:
def contains(self, t: time) -> bool:
return any(t in time_range for time_range in self.time_ranges)

def __contains__(self, t: time) -> bool:
return self.contains(t)


@attr.define
class WeekRange:
Expand All @@ -110,7 +116,7 @@ def merge(self, interpolate: timedelta = timedelta(0)) -> None:
def __attrs_post_init__(self) -> None:
self.validate()

def __contains__(self, dt: datetime) -> bool:
def contains(self, dt: datetime) -> bool:
tz = self.timezone
if tz is not None:
dt = dt.astimezone(tz)
Expand All @@ -119,3 +125,6 @@ def __contains__(self, dt: datetime) -> bool:
if day_range is not None:
return dt.time() in day_range
return False

def __contains__(self, dt: datetime) -> bool:
return self.contains(dt)

0 comments on commit 6d5a604

Please sign in to comment.