Skip to content

Commit

Permalink
Merge pull request #8 from wangjohn/master
Browse files Browse the repository at this point in the history
Add rrule.Set.String() and rrule.Set.Recurrences()
  • Loading branch information
damoye committed Apr 19, 2018
2 parents 80ade14 + b4f2fd1 commit a8479ea
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
19 changes: 19 additions & 0 deletions rruleset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rrule

import (
"fmt"
"sort"
"time"
)
Expand All @@ -13,6 +14,24 @@ type Set struct {
exdate []time.Time
}

// Recurrence returns a slice of all the recurrence rules for a set
func (set *Set) Recurrence() []string {
res := []string{}
for _, item := range set.rrule {
res = append(res, fmt.Sprintf("RRULE:%s", item))
}
for _, item := range set.rdate {
res = append(res, fmt.Sprintf("RDATE:%s", timeToStr(item)))
}
for _, item := range set.exrule {
res = append(res, fmt.Sprintf("EXRULE:%s", item))
}
for _, item := range set.exdate {
res = append(res, fmt.Sprintf("EXDATE:%s", timeToStr(item)))
}
return res
}

// RRule include the given rrule instance in the recurrence set generation.
func (set *Set) RRule(rrule *RRule) {
set.rrule = append(set.rrule, rrule)
Expand Down
42 changes: 42 additions & 0 deletions rruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,48 @@ func TestSet(t *testing.T) {
}
}

func TestSetString(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY, Count: 1, Byweekday: []Weekday{TU},
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
r, _ = NewRRule(ROption{Freq: YEARLY, Count: 3, Byweekday: []Weekday{TH},
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.ExRule(r)
set.ExDate(time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC))
set.ExDate(time.Date(1997, 9, 11, 9, 0, 0, 0, time.UTC))
set.ExDate(time.Date(1997, 9, 18, 9, 0, 0, 0, time.UTC))
set.RDate(time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC))
set.RDate(time.Date(1997, 9, 9, 9, 0, 0, 0, time.UTC))

want := `RRULE:FREQ=YEARLY;DTSTART=19970902T090000Z;COUNT=1;BYDAY=TU
RDATE:19970904T090000Z
RDATE:19970909T090000Z
EXRULE:FREQ=YEARLY;DTSTART=19970902T090000Z;COUNT=3;BYDAY=TH
EXDATE:19970904T090000Z
EXDATE:19970911T090000Z
EXDATE:19970918T090000Z`
value := set.String()
if want != value {
t.Errorf("get %v, want %v", value, want)
}
}

func TestSetRecurrence(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY, Count: 1, Byweekday: []Weekday{TU},
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
value := set.Recurrence()
if len(value) != 1 {
t.Errorf("Wrong length for recurrence got=%v want=%v", len(value), 1)
}
want := "RRULE:FREQ=YEARLY;DTSTART=19970902T090000Z;COUNT=1;BYDAY=TU"
if value[0] != want {
t.Errorf("get %v, want %v", value[0], want)
}
}

func TestSetDate(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY, Count: 1, Byweekday: []Weekday{TU},
Expand Down
13 changes: 11 additions & 2 deletions str.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import (
"time"
)

const (
strformat = "20060102T150405Z"
)

func timeToStr(time time.Time) string {
return time.UTC().Format("20060102T150405Z")
return time.UTC().Format(strformat)
}

func strToTime(str string) (time.Time, error) {
return time.Parse("20060102T150405Z", str)
return time.Parse(strformat, str)
}

func (f Frequency) String() string {
Expand Down Expand Up @@ -200,6 +204,11 @@ func (r *RRule) String() string {
return r.OrigOptions.String()
}

func (set *Set) String() string {
res := set.Recurrence()
return strings.Join(res, "\n")
}

// StrToRRule converts string to RRule
func StrToRRule(rfcString string) (*RRule, error) {
option, e := StrToROption(rfcString)
Expand Down

0 comments on commit a8479ea

Please sign in to comment.