teambition / rrule-go

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

Nanoseconds in DTSTART breaks between method #56

Closed MDUK0001 closed 1 year ago

MDUK0001 commented 1 year ago

If a time object with nanoseconds is supplied to RRule.DTStart(), if we then call RRule.Between() with inc = true, the first instance is not returned. However the same query when DTStart does not contain nanoseconds includes the first instance.

Sample program:

package main

import (
    "fmt"
    "time"

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

func main() {

    startDate := time.Date(2023, 01, 10, 16, 07, 0, 0, time.UTC)
    fmt.Println("Without nanoseconds:", startDate)
    run(startDate)

    startDate = time.Date(2023, 01, 10, 16, 07, 0, 1234, time.UTC)
    fmt.Println("With nanoseconds:", startDate)
    run(startDate)

}

func run(startDate time.Time) {
    rule := "FREQ=WEEKLY;INTERVAL=1;COUNT=10"
    r, _ := rrule.StrToRRule(rule)
    endDate := startDate.AddDate(0, 0, 30)
    r.DTStart(startDate)
    between := r.Between(startDate, endDate, true)
    for _, i := range between {
        fmt.Println(i)
    }
}

Output:

Without nanoseconds: 2023-01-10 16:07:00 +0000 UTC
2023-01-10 16:07:00 +0000 UTC
2023-01-17 16:07:00 +0000 UTC
2023-01-24 16:07:00 +0000 UTC
2023-01-31 16:07:00 +0000 UTC
2023-02-07 16:07:00 +0000 UTC
With nanoseconds: 2023-01-10 16:07:00.000001234 +0000 UTC
2023-01-17 16:07:00 +0000 UTC
2023-01-24 16:07:00 +0000 UTC
2023-01-31 16:07:00 +0000 UTC
2023-02-07 16:07:00 +0000 UTC

The first entry (2023-01-10 16:07:00 +0000 UTC) is missing when nanoseconds is supplied.

MDUK0001 commented 1 year ago

I am wondering if it is because the Iterator method needs nanoseconds adding?

zensh commented 1 year ago

Nanoseconds is not supported. Time will be truncated to second. https://github.com/teambition/rrule-go/blob/master/rrule.go#L936

MDUK0001 commented 1 year ago

I see. If nanoseconds isn't supported by DTStart, shouldn't it also not be supported by Between and other methods?

zensh commented 1 year ago

Yes

MDUK0001 commented 1 year ago

Raised a PR, would this work?

zensh commented 1 year ago

It may cause some exceptions on existing application scenarios. I would like to update documents.