lib-recur icon indicating copy to clipboard operation
lib-recur copied to clipboard

Wrong instance returned by RecurrenceSetIterator

Open luvk1412 opened this issue 4 years ago • 3 comments

While iterating through instances using RecurrenceSetIterator, if start time provided is not a valid instance, then start time is also returned as one of the instance. Is this expected ? If Yes, can some way be suggested to find only valid dates ? Example code :

  public static void main(String[] args) throws InvalidRecurrenceRuleException {
    ZonedDateTime startTime = ZonedDateTime.parse("2022-04-27T00:38:20Z", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    String rrule = "FREQ=WEEKLY;INTERVAL=1;BYDAY=TH";
    String timezone = "UTC";
    ZoneId zoneId = ZoneId.of(timezone);
    RecurrenceSet recurrenceSet = new RecurrenceSet();
    RecurrenceRule recurrenceRule = new RecurrenceRule(rrule);
    recurrenceSet.addInstances(new RecurrenceRuleAdapter(recurrenceRule));
    RecurrenceSetIterator it = recurrenceSet.iterator(TimeZone.getTimeZone(timezone), startTime.toInstant().toEpochMilli());
    for (int itr = 0; itr < 10; ++itr) {
      ZonedDateTime instant = Instant.ofEpochMilli(it.next()).atZone(zoneId);
      System.out.println("Instant " + itr + ", date: " + instant + ", day: " + instant.getDayOfWeek());
    }
  }

Outputs :

Instant 0, date: 2022-04-27T00:38:20Z[UTC], day: WEDNESDAY
Instant 1, date: 2022-04-28T00:38:20Z[UTC], day: THURSDAY
Instant 2, date: 2022-05-05T00:38:20Z[UTC], day: THURSDAY
Instant 3, date: 2022-05-12T00:38:20Z[UTC], day: THURSDAY
Instant 4, date: 2022-05-19T00:38:20Z[UTC], day: THURSDAY
Instant 5, date: 2022-05-26T00:38:20Z[UTC], day: THURSDAY
Instant 6, date: 2022-06-02T00:38:20Z[UTC], day: THURSDAY
Instant 7, date: 2022-06-09T00:38:20Z[UTC], day: THURSDAY
Instant 8, date: 2022-06-16T00:38:20Z[UTC], day: THURSDAY
Instant 9, date: 2022-06-23T00:38:20Z[UTC], day: THURSDAY

But wednesday is not a valid instance.

When i expand instances by adding same input on : https://recurrence-expansion-service.appspot.com/ , then instances provided are correct, can you please suggest the correct way used in website above ?

luvk1412 avatar Apr 12 '22 21:04 luvk1412

While iterating through instances using RecurrenceSetIterator, if start time provided is not a valid instance, then start time is also returned as one of the instance. Is this expected ? If Yes, can some way be suggested to find only valid dates ? Example code :

  public static void main(String[] args) throws InvalidRecurrenceRuleException {
    ZonedDateTime startTime = ZonedDateTime.parse("2022-04-27T00:38:20Z", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    String rrule = "FREQ=WEEKLY;INTERVAL=1;BYDAY=TH";
    String timezone = "UTC";
    ZoneId zoneId = ZoneId.of(timezone);
    RecurrenceSet recurrenceSet = new RecurrenceSet();
    RecurrenceRule recurrenceRule = new RecurrenceRule(rrule);
    recurrenceSet.addInstances(new RecurrenceRuleAdapter(recurrenceRule));
    RecurrenceSetIterator it = recurrenceSet.iterator(TimeZone.getTimeZone(timezone), startTime.toInstant().toEpochMilli());
    for (int itr = 0; itr < 10; ++itr) {
      ZonedDateTime instant = Instant.ofEpochMilli(it.next()).atZone(zoneId);
      System.out.println("Instant " + itr + ", date: " + instant + ", day: " + instant.getDayOfWeek());
    }
  }

Outputs :

Instant 0, date: 2022-04-27T00:38:20Z[UTC], day: WEDNESDAY
Instant 1, date: 2022-04-28T00:38:20Z[UTC], day: THURSDAY
Instant 2, date: 2022-05-05T00:38:20Z[UTC], day: THURSDAY
Instant 3, date: 2022-05-12T00:38:20Z[UTC], day: THURSDAY
Instant 4, date: 2022-05-19T00:38:20Z[UTC], day: THURSDAY
Instant 5, date: 2022-05-26T00:38:20Z[UTC], day: THURSDAY
Instant 6, date: 2022-06-02T00:38:20Z[UTC], day: THURSDAY
Instant 7, date: 2022-06-09T00:38:20Z[UTC], day: THURSDAY
Instant 8, date: 2022-06-16T00:38:20Z[UTC], day: THURSDAY
Instant 9, date: 2022-06-23T00:38:20Z[UTC], day: THURSDAY

But wednesday is not a valid instance.

When i expand instances by adding same input on : https://recurrence-expansion-service.appspot.com/ , then instances provided are correct, can you please suggest the correct way used in website above ?

Have you solved the problem? I have the same question

401error avatar Oct 09 '22 09:10 401error

@401error no

luvk1412 avatar Oct 09 '22 09:10 luvk1412

This is a duplicate of #35.

mrimvo avatar Oct 28 '22 10:10 mrimvo

I have rewritten the recurrence set iterator code. Now you can chose to iterate or omit the first instance if it's not synchronized with the rule. Hope this works for everyone. Please check out README.md for guidance and let me know if you have any questions.

dmfs avatar Nov 06 '22 17:11 dmfs