Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix caldav TZ interpretation of all day events #48642

Merged
merged 2 commits into from
Jun 29, 2021

Conversation

franc6
Copy link
Contributor

@franc6 franc6 commented Apr 2, 2021

Proposed change

In the caldav component, all-day events are treated as if they are from 00:00 to 00:00 in UTC, instead of in the local timezone. This code fixes the logic which interprets date-only events as being that date in UTC to being that date in local time. This is similar to how datetimes without a timezone are treated.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

Note: There's already a test for this change, but it was passing because the tests set the timezone to UTC and there's no good way to change it on a per-test basis (changing the global DEFAULT_TIME_ZONE is a bad idea since the tests aren't synchronous, and the patch decorator doesn't work because await is used for the component setup which references DEFAULT_TIME_ZONE and causes failure). Several tests fail (before and after this PR) if the tests are forced to run in another timezone (by modifying homeassistant/util/dt.py). However, the all-day tests will pass when the timezone is not UTC (after this PR).

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@homeassistant
Copy link
Contributor

Hi @franc6,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@MartinHjelmare MartinHjelmare changed the title Fixed #25814 by correcting TZ interpretation of all day of events Fix caldav TZ interpretation of all day events Apr 2, 2021
mshish added a commit to mshish/O365-HomeAssistant that referenced this pull request May 22, 2021
Attempts to fix PTST#85
Two changes:
1. Bring in fix from home-assistant/core#48642 which doesn't make much of a difference here (branch doesn't appear to get hit with all day events).
1. Fix the timezone shift for all day events. I'm not sure this is in the right place since it appears o365 is returning the event as if it were on GMT (but it's not). This may only work if the all day event was actually in the local timezone.
@github-actions
Copy link

github-actions bot commented Jun 6, 2021

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
Thank you for your contributions.

@github-actions github-actions bot added the stale label Jun 6, 2021
@franc6
Copy link
Contributor Author

franc6 commented Jun 6, 2021

Is there something still missing from this PR? The bot wants to remove it because there's been no activity for a while.

@github-actions github-actions bot removed the stale label Jun 6, 2021
@MartinHjelmare
Copy link
Member

MartinHjelmare commented Jun 7, 2021

For the tests we can use a pytest fixture that has autouse=True and yields and then resets the original timezone. This will allow the test to change the default timezone.

ORIG_TZ = dt_util.DEFAULT_TIME_ZONE
@pytest.fixture(autouse=True)
def restore_ts():
"""Restore default TZ."""
yield
dt_util.DEFAULT_TIME_ZONE = ORIG_TZ

@balloob
Copy link
Member

balloob commented Jun 7, 2021

@franc6 can you add a test to make sure it works now.

@franc6
Copy link
Contributor Author

franc6 commented Jun 8, 2021 via email

@franc6
Copy link
Contributor Author

franc6 commented Jun 9, 2021

A few notes to myself:

  1. The tests are running synchronously, so changing DEFAULT_TIME_ZONE isn't an issue.
  2. It looks like the all-day tests were passing without the patch when forced to a different timezone, because the date being used was within the same day still, and I missed that earlier.
  3. Because different time zones overlap with UTC differently, we need to change what dt.now is returning to check multiple time combinations, since the attributes won't include the timezone, and do get adjusted properly. Only the state is affected, this PR fixes the order in which events are returned which, in turn, may also affect which events are returned.
  4. Unit tests should ensure that homeassistant.dt.util.now() returns a date in the correct time zone, not UTC. That will make the tests match real-world conditions, allowing the bug to be seen more easily.

Please notes that unit tests were inadequate in the past, or 28514
would have been caught.  The new tests set one of three time zones
explicitly for all day event tests, and the all day event tests have
been modified to patch the return value of homeassistant.util.dt.now to
be in the local time zone, instead of always UTC.  By having it return
times in the local time zone, the tests better match real-world usage --
homeassistant.util.dt.now wouldn't return a UTC time when a different
time zone is set.

The unit tests now check for a single occurrence all day event at the
beginning of the local day, in the middle of the local day, and at the
end of the local day.  Each check is run in three separate time zones,
Asia/Baghdad, UTC, and America/New_York.  The times are chose such that
the early in the local day time for Asia/Baghdad is actually the day
before in UTC, and the late in the local day time for America/New_York
is actually the next day in UTC.  This guarantees that when the tests
pass, there are no internal checks that shifted what should be a local
time as if it were UTC being shifted to local time, which was bug home-assistant#25814.

Similar changes were made for the recurring all day event test, too.

Note that if you run the new unit tests without the fixed code in this
patch, that the following tests will fail:

test_all_day_event_returned_early[pyloop-baghdad]
test_all_day_event_returned_late[pyloop-new_york]
test_event_rrule_all_day_late[pyloop-new_york]

After applying the fix, all tests will succeed.  If you're wondering why
test_event_rrule_all_day_early[pyloop-baghdad] doesn't fail without the
fix, it's because we're not checking on the first occurrence of the
event.  If we did that, the test would fail just like
test_all_day_event_returned_early[pyloop-baghdad].  However, since by
checking the first occurrence we're running the same condition that
test_all_day_event_returned_early[pyloop-baghdad] already checks, it
would be redundant to add a test which fails for a recurring event in
that time zone.  The whole point of the test_event_rrule_all_day tests
is to ensure that recurrences are returned when it's not the first
occurrence.
@franc6
Copy link
Contributor Author

franc6 commented Jun 10, 2021

OK, unit tests have been updated. Please see the comment for the commit for the unit tests to explain the reasons for the changes. I strongly recommend running the updated unit tests WITHOUT the fix, so the failures can be observed, then apply the fix, and observe the tests pass. :)
If you've been watching this conversation, please note that I updated my "notes to myself" to remove incorrect information. Let me know if there are further questions or edits you'd like to see.

@balloob balloob merged commit 3f66709 into home-assistant:dev Jun 29, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jun 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

caldav all day events are UTC only
6 participants