Skip to content

Commit

Permalink
add default Until time to rule, #24
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Mar 8, 2019
1 parent 0c695cc commit 74e8f1c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/teambition/rrule-go

go 1.12
11 changes: 11 additions & 0 deletions rrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type ROption struct {
// documented in the iCalendar RFC, including support for caching of results.
type RRule struct {
OrigOptions ROption
Options ROption
freq Frequency
dtstart time.Time
interval int
Expand Down Expand Up @@ -148,6 +149,10 @@ func NewRRule(arg ROption) (*RRule, error) {
r.interval = arg.Interval
}
r.count = arg.Count
if arg.Until.IsZero() {
// add largest representable duration (approximately 290 years).
arg.Until = arg.Dtstart.Add(time.Duration(1<<63 - 1))
}
r.until = arg.Until
r.wkst = arg.Wkst.weekday
for _, pos := range arg.Bysetpos {
Expand Down Expand Up @@ -213,6 +218,7 @@ func NewRRule(arg ROption) (*RRule, error) {
r.bysecond = arg.Bysecond
}

r.Options = arg
// Calculate the timeset if needed
r.calculateTimeset()

Expand Down Expand Up @@ -790,6 +796,11 @@ func (r *RRule) DTStart(dt time.Time) {
r.calculateTimeset()
}

// Until set a new Until for the rule and recalculates the timeset if needed.
func (r *RRule) Until(ut time.Time) {
r.until = ut
}

// calculateTimeset calculates the timeset if needed.
func (r *RRule) calculateTimeset() {
// Reset the timeset value
Expand Down
31 changes: 31 additions & 0 deletions rrule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3594,6 +3594,22 @@ func TestDTStartWithMicroseconds(t *testing.T) {
}
}

func TestUntil(t *testing.T) {
r1, _ := NewRRule(ROption{Freq: DAILY,
Dtstart: time.Date(1997, 9, 2, 0, 0, 0, 0, time.UTC)})
r1.Until(time.Date(1998, 9, 2, 0, 0, 0, 0, time.UTC))

r2, _ := NewRRule(ROption{Freq: DAILY,
Dtstart: time.Date(1997, 9, 2, 0, 0, 0, 0, time.UTC),
Until: time.Date(1998, 9, 2, 0, 0, 0, 0, time.UTC)})

v1 := r1.All()
v2 := r2.All()
if !timesEqual(v1, v2) {
t.Errorf("get %v, want %v", v1, v2)
}
}

func TestMaxYear(t *testing.T) {
r, _ := NewRRule(ROption{Freq: YEARLY,
Count: 3,
Expand Down Expand Up @@ -3680,6 +3696,21 @@ func TestBetweenInc(t *testing.T) {
}
}

func TestAllWithDefaultUtil(t *testing.T) {
r, _ := NewRRule(ROption{Freq: YEARLY,
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})

value := r.All()
if len(value) > 300 || len(value) < 200 {
t.Errorf("No default Util time")
}

r, _ = NewRRule(ROption{Freq: YEARLY})
if len(r.All()) != len(value) {
t.Errorf("No default Util time")
}
}

func TestWeekdayGetters(t *testing.T) {
wd := Weekday{n: 2, weekday: 0}
if wd.N() != 2 {
Expand Down
12 changes: 11 additions & 1 deletion rruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func (set *Set) Recurrence() []string {
func (set *Set) DTStart(dtstart time.Time) {
set.dtstart = dtstart

for _, r := range append(set.rrule, set.exrule...) {
for _, r := range set.rrule {
r.DTStart(set.dtstart)
}

for _, r := range set.exrule {
r.DTStart(set.dtstart)
}
}
Expand All @@ -53,6 +57,9 @@ func (set *Set) GetDTStart() time.Time {

// RRule include the given rrule instance in the recurrence set generation.
func (set *Set) RRule(rrule *RRule) {
if !set.dtstart.IsZero() {
rrule.DTStart(set.dtstart)
}
set.rrule = append(set.rrule, rrule)
}

Expand All @@ -75,6 +82,9 @@ func (set *Set) GetRDate() []time.Time {
// Dates which are part of the given recurrence rules will not be generated,
// even if some inclusive rrule or rdate matches them.
func (set *Set) ExRule(exrule *RRule) {
if !set.dtstart.IsZero() {
exrule.DTStart(set.dtstart)
}
set.exrule = append(set.exrule, exrule)
}

Expand Down
16 changes: 16 additions & 0 deletions rruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ func TestSet(t *testing.T) {
}
}

func TestSetOverlapping(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY,
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
v1 := set.All()
if len(v1) > 300 || len(v1) < 200 {
t.Errorf("No default Util time")
}
set.ExRule(r)
v2 := set.All()
if len(v2) != 0 {
t.Errorf("Should no values when RRule and ExRule overlapping")
}
}

func TestSetString(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY, Count: 1, Byweekday: []Weekday{TU},
Expand Down

0 comments on commit 74e8f1c

Please sign in to comment.