Skip to content

Commit

Permalink
refactor: update all_day detection logic
Browse files Browse the repository at this point in the history
Instead of using a particular resolution on an object, which differs
between `datetime.date` and `datetime.datetime` objects, operate on
parent class of `datetime` and convert all `dt` to timezone-aware
`datetime` values.

This is also in accordance to the model field being a `DateTimeField`,
so we should always be passing the correctly-created object, instead of
a `datetime.date()`, raising `received a naive datetime` warnings.

Removes unused constants.

Signed-off-by: Mike Fiedler <[email protected]>
  • Loading branch information
miketheman committed Sep 20, 2024
1 parent f2e763c commit b12e856
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 8 deletions.
8 changes: 1 addition & 7 deletions events/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
from .models import EventLocation, Event, OccurringRule
from .utils import extract_date_or_datetime

DATE_RESOLUTION = timedelta(1)
TIME_RESOLUTION = timedelta(0, 0, 1)

logger = logging.getLogger(__name__)


Expand All @@ -31,10 +28,7 @@ def import_occurrence(self, event, event_data):
dt_end = dt_start

# Let's mark those occurrences as 'all-day'.
all_day = (
dt_start.resolution == DATE_RESOLUTION or
dt_end.resolution == DATE_RESOLUTION
)
all_day = dt_end - dt_start >= timedelta(days=1)

defaults = {
'dt_start': dt_start,
Expand Down
2 changes: 1 addition & 1 deletion events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def date_to_datetime(date, tzinfo=None):


def extract_date_or_datetime(dt):
if isinstance(dt, datetime.datetime):
if isinstance(dt, datetime.date):
return convert_dt_to_aware(dt)
return dt

Expand Down

0 comments on commit b12e856

Please sign in to comment.