python-caldav / caldav

Apache License 2.0
319 stars 94 forks source link

date_search with datetime.now() is returning events in the past #207

Closed jakeprice-me closed 2 years ago

jakeprice-me commented 2 years ago

Hello, I'm working on a script to notify me one hour before an event is due to occur, via Telegram.

However, I'm having some issues using date_search. It fetches events within the next hour (and no further), but it also seems to be returning events on the specified day that have already occurred (including all day events which in Fastmail at least "start" at 00:00).

Here's the example's I'm working with today (screenshot from my Fastmail calendar).

Screenshot from 2022-08-14 21-04-20

Here's the code I'm using to search for events.

    with caldav.DAVClient(url=url, username=username, password=password) as client:

        # Fetch calendar
        cal = client.principal().calendar(name=calendarid)

        events = cal.date_search(
            start=datetime.datetime.now(),
            end=datetime.datetime.now() + datetime.timedelta(hours=1),
            expand=True,
        )

        for event in events:
            e = event.instance.vevent
            item_date = e.dtstart.value.strftime("%Y-%m-%d %H:%M")
            item_summary = e.summary.value
            item = f"{item_date} {item_summary}"
            print(item)

This is what it's returning at 20:47 tonight. You can see the two events in the past.

Upcoming Event
2022-08-14 00:00 Test 1 All Day
2022-08-14 20:00 Upcoming Test
2022-08-14 21:00 Upcoming Test 2

Any ideas what might be going on?

tobixen commented 2 years ago

Hm ... not sure if this covers the examples given above, but ...

Due to the first one, I believe you will in any case need to filter the events on the client side ... and if you need to do any filtering on the client side, then probably it doesn't matter much if the date search returns any extra events. It's worse if too few events are passed ... i.e. if it's a timezone issue.

jakeprice-me commented 2 years ago

Thanks for the quick reply @tobixen that makes perfect sense and would explain the behavior I'm seeing.

Will do some filtering on my side.