fix(caldav): parse timestamps in configured timezone

This commit is contained in:
Dominik Pschenitschni
2025-06-10 22:17:40 +02:00
committed by kolaente
parent a160048cc3
commit 0ca26dbf17
2 changed files with 21 additions and 11 deletions

View File

@@ -491,23 +491,20 @@ func caldavTimeToTimestamp(ianaProperty ics.IANAProperty) time.Time {
var err error var err error
tzParameter := ianaProperty.ICalParameters["TZID"] tzParameter := ianaProperty.ICalParameters["TZID"]
if len(tzParameter) > 0 { if len(tzParameter) > 0 {
loc, err := time.LoadLocation(tzParameter[0]) loc, locErr := time.LoadLocation(tzParameter[0])
if err != nil { if locErr != nil {
log.Warningf("Error while parsing caldav timezone %s: %s", tzParameter[0], err) log.Warningf("Error while parsing caldav timezone %s: %s", tzParameter[0], locErr)
} else { } else {
t, err = time.ParseInLocation(format, tstring, loc) t, err = time.ParseInLocation(format, tstring, loc)
if err != nil {
log.Warningf("Error while parsing caldav time %s to TimeStamp: %s at location %s", tstring, loc, err)
} else {
t = t.In(config.GetTimeZone())
return t
}
} }
} else {
t, err = time.ParseInLocation(format, tstring, config.GetTimeZone())
} }
t, err = time.Parse(format, tstring)
if err != nil { if err != nil {
log.Warningf("Error while parsing caldav time %s to TimeStamp: %s", tstring, err) log.Warningf("Error while parsing caldav time %s to TimeStamp: %s", tstring, err)
return time.Time{} return time.Time{}
} }
return t
return t.In(config.GetTimeZone())
} }

View File

@@ -24,6 +24,7 @@ import (
"code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/models"
ics "github.com/arran4/golang-ical"
"gopkg.in/d4l3k/messagediff.v1" "gopkg.in/d4l3k/messagediff.v1"
) )
@@ -717,3 +718,15 @@ END:VCALENDAR`,
}) })
} }
} }
func TestCaldavTimeToTimestamp_NoTZID(t *testing.T) {
config.InitDefaultConfig()
prop := ics.IANAProperty{BaseProperty: ics.BaseProperty{Value: "20181201T011204", ICalParameters: map[string][]string{}}}
got := caldavTimeToTimestamp(prop)
want := time.Date(2018, 12, 1, 1, 12, 4, 0, config.GetTimeZone())
if !got.Equal(want) || got.Location().String() != config.GetTimeZone().String() {
t.Fatalf("caldavTimeToTimestamp() = %v, want %v", got, want)
}
}