teambition / rrule-go

Go library for working with recurrence rules for calendar dates.
MIT License
310 stars 57 forks source link

How to check if two RRules overlapping #32

Closed suren4Kites closed 5 years ago

suren4Kites commented 5 years ago

If I have one RRule for weekly and another for monthly. Is there any way that I can check whether overlapping is possible?

zensh commented 5 years ago
package main

import (
    "fmt"

    "github.com/teambition/rrule-go"
)

func main() {
    r1, _ := rrule.StrToRRule("FREQ=DAILY;DTSTART=19970902T015959Z;COUNT=1000")
    r2, _ := rrule.StrToRRule("FREQ=DAILY;DTSTART=19980902T015959Z;COUNT=100")
    all := r1.All()
    cur := 0
    iter := r2.Iterator()

    for {
        val, ok := iter()
        if !ok {
            break
        }

        for {
            if cur >= len(all) {
                break
            }

            if val.After(all[cur]) {
                cur++
                continue
            } else if val.Equal(all[cur]) {
                fmt.Println("overlapping ", val)
                cur++
            }

            break
        }
    }
}