u01jmg3 / ics-parser

Parser for iCalendar Events • PHP 8+, 7 (≥ 7.4), 5 (≥ 5.6)
MIT License
448 stars 144 forks source link

Getting an error bad format P-1D on a file on the system. #281

Closed gulati-ofasi closed 3 years ago

gulati-ofasi commented 3 years ago

Description of the Issue:

I have a webcal link that I download to a file called example.ics when I try to parse the file I get the error

`[2021-03-03 23:11:35] dev.DEBUG: Exception: DateInterval::__construct(): Unknown or bad format (P-1D) in /Users/u/code/p/vendor/johngrogg/ics-parser/src/ICal/ICal.php:516 Stack trace:

0 /Users/u/code/p/vendor/johngrogg/ics-parser/src/ICal/ICal.php(516): DateInterval->__construct('P-1D')`

Steps to Reproduce:

Copy the ICS file in your storage/app and call the getBlockedTimes. I have added both the code snippet (code.txt) and ics file (example.txt) code.txt example.txt

s0600204 commented 3 years ago

Hi there,

The problem seems unconnected to the file you have provided (but thank you for providing it anyway).

Instead, the issue appears to be that you have specified a negative number for filterDaysBefore.

gulati-ofasi commented 3 years ago

@s0600204 Thanks for the quick reply . Can you explain filterDaysBefore please .. from my understanding I need to set it like this +/- 1? or just 1? I am trying to ignore all events before yesterday. So if today is the 4th I want to see all events from 3rd (including 3rd) to the next 4 weeks

s0600204 commented 3 years ago

As I understand it:

Calendars, particularly when all recurrence rules are expanded, can be sizable and contain many, many events. There are two points at which the quantity of events returned by the parser can be reduced to keep things manageable, one of these points being the filterDaysBefore/filterDaysAfter pairing. However, at present, this pair of filters is applied before any timezone calculations are performed and it is calculated from the current time (as well as day) that the parser is run.

As such, we recommend making filterDaysBefore or filterDaysAfter +1 larger. So if you wanted to exclude events prior to 7 days ago, you would provide 8 to filterDaysBefore, to make sure that any events that start on or after midnight (in the timezone of the server) 7 days previously are not filtered out prematurely.

From one perspective, you might think of this as removing a day, as you're asking for the filter to be set a day earlier. Current day -1. (But the value passed to filterDaysBefore goes up.)

And yes, events that start a few hours before midnight-7-days-ago (server time) in the above example will also slip through, but the filterDays*-pair is intended to reduce memory usage and improve parsing speed (by culling all events that are definitely outside the desired window of time), not as an accurate way of getting events of a specific date-range (for that, use the eventsFromRange() method).

Does that help?

u01jmg3 commented 3 years ago

Closing as an answer has been provided