mangstadt / biweekly

biweekly is an iCalendar library written in Java.
BSD 2-Clause "Simplified" License
323 stars 44 forks source link

Calculation of until #105

Closed CCBow-501 closed 2 years ago

CCBow-501 commented 3 years ago

Hello,

We are using this Library to generate instances from a series. Our series model consists of startDate (LocalDate) endDate (LocalDate) and a timezone.

So we are building a Recurrence like this:

Recurrence.Builder builder = new Recurrence.Builder(Frequency.WEEKLY)
                .workweekStarts(MONDAY)
                .interval(1)
                .byDay(dayOfWeek);

If our series has no endDate we don't configure until and everything works fine. If endDate is set it is converted to ICalDate with the time 23:59:59 and the timeZone of the series:

LocalTime time = LocalTime.of(23, 59, 59);
Instant instant = ZonedDateTime.of(endDate, time, zoneId).toInstant();
ICalDate iCalDate = new ICalDate(Date.from(instant));

For an endDate of 2020-04-26 and a timeZone with a negative offset (e.g America/Managua) the conversion results in an instant of 2020-04-27T05:59:59Z

This leads to a different calculation of until in 0.6.3 and 0.6.5 which was changed in this commit: https://github.com/mangstadt/biweekly/commit/f1632351d3f023e8337e7bad34292f27f0176618#diff-46dc7f39fb49bcfadc7a87f606664f40713f0903572689551ddc87e89348b444

With the old version and the re-convert with the tzid of the RecurrenceIterator:

DateValue untilUtc = (until == null) ? null : Google2445Utils.convert(until, tzid);

untilUtc resulted in 20200426T235959

With the new implementation:

TimeZone utc = TimeZone.getTimeZone("UTC");
untilUtc = Google2445Utils.convert(until, utc);

until gets treated like UTC and results in 20200427T055959

Is there a reason the new calculation completely ignores the timezone of the iterator?

Kind regards Denis

mangstadt commented 3 years ago

Hi Denis,

The RecurrenceIterator requires the until date to be in UTC time due to how the date iteration code is implemented. This is one of the problems that this commit fixed, which I seemed to have neglected to mention in the commit message.

However, RecurrenceIterator still takes the time zone into account when iterating over the dates in the recurrence (at least, it's supposed to!).