Skip to content

Commit

Permalink
Use ISO definition of week-based year
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jul 20, 2020
1 parent 396df7d commit a1920dc
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
7 changes: 2 additions & 5 deletions src/main/java/net/fortuna/ical4j/model/Recur.java
Expand Up @@ -40,10 +40,7 @@
import java.io.IOException;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalUnit;
import java.time.temporal.*;
import java.util.*;

/**
Expand Down Expand Up @@ -177,7 +174,7 @@ public enum Frequency {

private final List<Integer> yearDayList = new NumberList(ChronoField.DAY_OF_YEAR.range(), true);

private final List<Integer> weekNoList = new NumberList(ChronoField.ALIGNED_WEEK_OF_YEAR.range(), true);
private final List<Integer> weekNoList = new NumberList(IsoFields.WEEK_OF_WEEK_BASED_YEAR.range(), true);

private final List<Integer> monthList = new NumberList(ChronoField.MONTH_OF_YEAR.range(), false);

Expand Down
Expand Up @@ -11,8 +11,6 @@
import java.util.ArrayList;
import java.util.List;

import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;

/**
* Applies BYWEEKNO rules specified in this Recur instance to the specified date list. If no BYWEEKNO rules are
* specified the date list is returned unmodified.
Expand Down Expand Up @@ -48,12 +46,12 @@ public List<T> transform(List<T> dates) {
// if (numWeeksInYear < weekNo) {
// continue;
// }
candidate = withTemporalField(date, ALIGNED_WEEK_OF_YEAR, weekNo);
candidate = withTemporalField(date, IsoFields.WEEK_OF_WEEK_BASED_YEAR, weekNo);
} else {
if (numWeeksInYear < -weekNo) {
continue;
}
candidate = withTemporalField(date, ALIGNED_WEEK_OF_YEAR, numWeeksInYear + 1 + weekNo);
candidate = withTemporalField(date, IsoFields.WEEK_OF_WEEK_BASED_YEAR, numWeeksInYear + 1 + weekNo);
}
weekNoDates.add(candidate);
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/groovy/net/fortuna/ical4j/model/RecurSpec.groovy
Expand Up @@ -162,11 +162,11 @@ class RecurSpec extends Specification {

where:
rule | wkst | start | end || expected
'2,52,53' | 'MO' | '20110101' | '20131231' || ['20110115', '20111231', '20120114', '20121229', '20130112', '20131228']
'2,52,53' | 'MO' | '20110108' | '20131231' || ['20110115', '20111231', '20120114', '20121229', '20130112', '20131228']
// If WKST is Wed, then we'll have 53 weeks in 2011.
'2,52,53' | 'WE' | '20110101' | '20131231' || ['20110108', '20111224', '20111231', '20120114', '20121229', '20130112', '20131228']
'2,52,53' | 'WE' | '20110101' | '20131231' || ['20110101', '20110108', '20111224', '20111231', '20120114', '20121229', '20130112', '20131228']
'-2,-52,-53' | 'MO' | '20110101' | '20131231' || ['20110108', '20111224', '20120107', '20121222', '20130105', '20131221']
'-2,-52,-53' | 'WE' | '20110101' | '20131231' || ['20110101', '20110108', '20111224', '20120107', '20121222', '20130105', '20131221']
'-2,-52,-53' | 'WE' | '20110101' | '20131231' || ['20110108', '20111224', '20120107', '20121222', '20130105', '20131221']
}

@Unroll
Expand Down Expand Up @@ -330,7 +330,7 @@ class RecurSpec extends Specification {

where:
rule | start | end | expected
'FREQ=WEEKLY;INTERVAL=2;BYDAY=SU' | '20110101' | '20110201' | ['20110102', '20110116', '20110130']
'FREQ=WEEKLY;INTERVAL=2;BYDAY=SU' | '20110101' | '20110201' | ['20110109', '20110123']
}

@Unroll
Expand Down
Expand Up @@ -40,6 +40,7 @@ class ByWeekNoRuleTest extends Specification {
where:
byWeekNoPart | dates | expectedResult
'2,52,53' | [LocalDate.parse('20110101', dateFormat)] | [LocalDate.parse('20110108', dateFormat), LocalDate.parse('20111224', dateFormat)]
// due to ISO 8601 definition '2011-01-01' is in the 52nd week of 2010..
'2,52,53' | [LocalDate.parse('20110101', dateFormat)] | [LocalDate.parse('20100116', dateFormat), LocalDate.parse('20110101', dateFormat)]
}
}

0 comments on commit a1920dc

Please sign in to comment.