teambition / rrule-go

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

2.29 Not present #58

Open herculewyy opened 1 year ago

herculewyy commented 1 year ago
    var dd = "2023-01-30 12:45:23"

loc, _ := time.LoadLocation("Local")

dt, _ := time.ParseInLocation("2006-01-02 15:04:05", dd, loc)

r, _ = rrule.NewRRule(rrule.ROption{
    Freq: rrule.MONTHLY,
    //Bymonthday: []int{},
    //  Bymonth:    []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, // 取哪一月的数据
    Interval: 1,
    Until:    time.Now().AddDate(0, 5, 0),
    Dtstart:  dt,
})

//set.ExDate(time.Now())
for _, t2 := range r.All() {
    fmt.Println(t2.Format("2006-01-02 15:04:05"))
}

result: 2023-01-30 12:45:23 2023-03-30 12:45:23 2023-04-30 12:45:23 2023-05-30 12:45:23 2023-06-30 12:45:23

2023-02-28 12:45:23 How to achieve?

thank you

herculewyy commented 1 year ago

Starting from 1.31, the frequency is every month, then I want to get the last day of February (2.28 or 2.29)

while-loop commented 1 year ago

Feb 29, 2023 is not a real date

Maybe something along the lines of the 30th of every month or last day.

Probably done with a ruleset

KaiChen1008 commented 5 months ago

the following code works for me

func printTime(ts []time.Time) {
    for _, t := range ts {
        fmt.Println(t)
    }
}

func main() {
    loc, _ := time.LoadLocation("Local")
    curr := time.Date(2024, 1, 28, 12, 45, 23, 0, loc)
    r, _ := rrule.NewRRule(rrule.ROption{
        Freq:       rrule.MONTHLY,
        Interval:   1,
        Bymonthday: []int{-1},
        Dtstart:    curr,
        Until:      curr.AddDate(0, 5, 0),
    })

    printTime(r.All())
}

output:

2024-01-31 12:45:23 +0800 CST
2024-02-29 12:45:23 +0800 CST
2024-03-31 12:45:23 +0800 CST
2024-04-30 12:45:23 +0800 CST
2024-05-31 12:45:23 +0800 CST