Use the local timezone when parsing Todoist due dates (#45994)

pull/47705/head
Sam Steele 2021-03-09 23:52:32 -05:00 committed by GitHub
parent 62e49e545b
commit bf64421be9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 8 deletions

View File

@ -226,15 +226,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
) )
def _parse_due_date(data: dict) -> datetime: def _parse_due_date(data: dict, gmt_string) -> datetime:
"""Parse the due date dict into a datetime object.""" """Parse the due date dict into a datetime object."""
# Add time information to date only strings. # Add time information to date only strings.
if len(data["date"]) == 10: if len(data["date"]) == 10:
data["date"] += "T00:00:00" data["date"] += "T00:00:00"
# If there is no timezone provided, use UTC. if dt.parse_datetime(data["date"]).tzinfo is None:
if data["timezone"] is None: data["date"] += gmt_string
data["date"] += "Z" return dt.as_utc(dt.parse_datetime(data["date"]))
return dt.parse_datetime(data["date"])
class TodoistProjectDevice(CalendarEventDevice): class TodoistProjectDevice(CalendarEventDevice):
@ -407,7 +406,9 @@ class TodoistProjectData:
# Generally speaking, that means right now. # Generally speaking, that means right now.
task[START] = dt.utcnow() task[START] = dt.utcnow()
if data[DUE] is not None: if data[DUE] is not None:
task[END] = _parse_due_date(data[DUE]) task[END] = _parse_due_date(
data[DUE], self._api.state["user"]["tz_info"]["gmt_string"]
)
if self._due_date_days is not None and ( if self._due_date_days is not None and (
task[END] > dt.utcnow() + self._due_date_days task[END] > dt.utcnow() + self._due_date_days
@ -529,9 +530,19 @@ class TodoistProjectData:
for task in project_task_data: for task in project_task_data:
if task["due"] is None: if task["due"] is None:
continue continue
due_date = _parse_due_date(task["due"]) due_date = _parse_due_date(
task["due"], self._api.state["user"]["tz_info"]["gmt_string"]
)
midnight = dt.as_utc(
dt.parse_datetime(
due_date.strftime("%Y-%m-%d")
+ "T00:00:00"
+ self._api.state["user"]["tz_info"]["gmt_string"]
)
)
if start_date < due_date < end_date: if start_date < due_date < end_date:
if due_date.hour == 0 and due_date.minute == 0: if due_date == midnight:
# If the due date has no time data, return just the date so that it # If the due date has no time data, return just the date so that it
# will render correctly as an all day event on a calendar. # will render correctly as an all day event on a calendar.
due_date_value = due_date.strftime("%Y-%m-%d") due_date_value = due_date.strftime("%Y-%m-%d")