mirror of
https://github.com/go-vikunja/vikunja.git
synced 2026-07-20 21:14:30 -05:00
fix(caldav): parse ISO 8601 week durations in reminder triggers (#3183)
This commit is contained in:
@@ -32,12 +32,13 @@ func ParseISO8601Duration(str string) time.Duration {
|
||||
|
||||
years := parseDurationPart(matches[2], time.Hour*24*365)
|
||||
months := parseDurationPart(matches[3], time.Hour*24*30)
|
||||
days := parseDurationPart(matches[4], time.Hour*24)
|
||||
hours := parseDurationPart(matches[5], time.Hour)
|
||||
minutes := parseDurationPart(matches[6], time.Second*60)
|
||||
seconds := parseDurationPart(matches[7], time.Second)
|
||||
weeks := parseDurationPart(matches[4], time.Hour*24*7)
|
||||
days := parseDurationPart(matches[5], time.Hour*24)
|
||||
hours := parseDurationPart(matches[6], time.Hour)
|
||||
minutes := parseDurationPart(matches[7], time.Second*60)
|
||||
seconds := parseDurationPart(matches[8], time.Second)
|
||||
|
||||
duration := years + months + days + hours + minutes + seconds
|
||||
duration := years + months + weeks + days + hours + minutes + seconds
|
||||
|
||||
if matches[1] == "-" {
|
||||
return -duration
|
||||
@@ -45,7 +46,7 @@ func ParseISO8601Duration(str string) time.Duration {
|
||||
return duration
|
||||
}
|
||||
|
||||
var durationRegex = regexp.MustCompile(`([-+])?P([\d\.]+Y)?([\d\.]+M)?([\d\.]+D)?T?([\d\.]+H)?([\d\.]+M)?([\d\.]+?S)?`)
|
||||
var durationRegex = regexp.MustCompile(`([-+])?P([\d\.]+Y)?([\d\.]+M)?([\d\.]+W)?([\d\.]+D)?T?([\d\.]+H)?([\d\.]+M)?([\d\.]+?S)?`)
|
||||
|
||||
func parseDurationPart(value string, unit time.Duration) time.Duration {
|
||||
if len(value) != 0 {
|
||||
|
||||
@@ -34,6 +34,30 @@ func TestParseISO8601Duration(t *testing.T) {
|
||||
dur := ParseISO8601Duration("-P1DT1H1M1S")
|
||||
expected, _ := time.ParseDuration("-25h1m1s")
|
||||
|
||||
assert.Equal(t, expected, dur)
|
||||
})
|
||||
t.Run("weeks", func(t *testing.T) {
|
||||
dur := ParseISO8601Duration("P1W")
|
||||
expected, _ := time.ParseDuration("168h")
|
||||
|
||||
assert.Equal(t, expected, dur)
|
||||
})
|
||||
t.Run("negative weeks", func(t *testing.T) {
|
||||
dur := ParseISO8601Duration("-P2W")
|
||||
expected, _ := time.ParseDuration("-336h")
|
||||
|
||||
assert.Equal(t, expected, dur)
|
||||
})
|
||||
t.Run("weeks combined with days", func(t *testing.T) {
|
||||
dur := ParseISO8601Duration("P1W2D")
|
||||
expected, _ := time.ParseDuration("216h")
|
||||
|
||||
assert.Equal(t, expected, dur)
|
||||
})
|
||||
t.Run("weeks combined with other units", func(t *testing.T) {
|
||||
dur := ParseISO8601Duration("P1W2DT3H4M5S")
|
||||
expected, _ := time.ParseDuration("219h4m5s")
|
||||
|
||||
assert.Equal(t, expected, dur)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user