From c885e124d406a04acf783e446cda0149b62f05a9 Mon Sep 17 00:00:00 2001 From: Danny Milosavljevic Date: Wed, 8 Mar 2023 23:25:58 +0100 Subject: [PATCH] views: _api_occurrences: Support handling of timezone-aware datetime strings (2022-01-01T01:00:00+01:00). --- schedule/views.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/schedule/views.py b/schedule/views.py index 54829477..b9ed2661 100644 --- a/schedule/views.py +++ b/schedule/views.py @@ -351,20 +351,11 @@ def _api_occurrences(start, end, calendar_slug, timezone): if not start or not end: raise ValueError("Start and end parameters are required") # version 2 of full calendar - # TODO: improve this code with date util package if "-" in start: - def convert(ddatetime): if ddatetime: - ddatetime = ddatetime.split(" ")[0] - try: - return datetime.datetime.strptime(ddatetime, "%Y-%m-%d") - except ValueError: - # try a different date string format first before failing - return datetime.datetime.strptime(ddatetime, "%Y-%m-%dT%H:%M:%S") - + return dateutil.parser.parse(ddatetime) else: - def convert(ddatetime): return datetime.datetime.utcfromtimestamp(float(ddatetime)) @@ -379,8 +370,8 @@ def convert(ddatetime): elif settings.USE_TZ: # If USE_TZ is True, make start and end dates aware in UTC timezone utc = pytz.UTC - start = utc.localize(start) - end = utc.localize(end) + start = utc.localize(start) if start.tzinfo is None else start + end = utc.localize(end) if end.tzinfo is None else end if calendar_slug: # will raise DoesNotExist exception if no match