From 10a3cc963787584c4c721a827fcebc04fecad32b Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 2 Apr 2018 13:52:52 +0200 Subject: [PATCH] Fixed endDate being wrong for wholeDay events (it actually should be +1) --- src/calanonsync/caldav.go | 24 ++++-------------------- src/calanonsync/caldav_test.go | 8 +++----- 2 files changed, 7 insertions(+), 25 deletions(-) diff --git a/src/calanonsync/caldav.go b/src/calanonsync/caldav.go index 9fd76ff..1778176 100644 --- a/src/calanonsync/caldav.go +++ b/src/calanonsync/caldav.go @@ -53,7 +53,7 @@ func (i ICal) UID() string { // the event data of the ICal structure. Timezone information are ignored. // The time is therefore either a local time or a UTC time, depending on // the notation. -func (i ICal) getTimeField(f string) (t time.Time, isDate bool) { +func (i ICal) getTimeField(f string) (t time.Time) { for _, s := range i.eventData { if strings.HasPrefix(s, f) { st := s[strings.LastIndex(s, ":")+1:] @@ -67,10 +67,8 @@ func (i ICal) getTimeField(f string) (t time.Time, isDate bool) { var err error if strings.Contains(s, ";VALUE=DATE") { t, err = time.ParseInLocation(ICAL_DATE, st, zone) - isDate = true } else { t, err = time.ParseInLocation(ICAL_TIME, st, zone) - isDate = false } if err != nil { log.Printf("Cannot decode %s '%s': %s", f, st, err) @@ -78,7 +76,7 @@ func (i ICal) getTimeField(f string) (t time.Time, isDate bool) { return } } - return time.Time{}, false + return } var fieldMatch = regexp.MustCompile(`^(\w+)[;:]`) @@ -104,12 +102,6 @@ func (ical *ICal) Update(newStart, newEnd time.Time, wholeDay bool) { } } - if wholeDay { - // For wholeDay events, the endDate is the beginning of the next day. For - // the formatting to work, we need to move that back into the previous day. - newEnd = newEnd.Add(-1 * time.Second) - } - formatTime := func(t time.Time) string { if wholeDay { return ";VALUE=DATE:" + t.Format(ICAL_DATE) @@ -140,20 +132,12 @@ func (ical *ICal) Update(newStart, newEnd time.Time, wholeDay bool) { // Retrieve the start time of the event. func (i ICal) Start() time.Time { - t, _ := i.getTimeField("DTSTART") - return t + return i.getTimeField("DTSTART") } // Retrieve the end time of the event. func (i ICal) End() time.Time { - t, isDate := i.getTimeField("DTEND") - if isDate { - // If the end date is a date only, we need to move that to the - // 00:00 time one day ahead. - t = t.AddDate(0, 0, 1) - } - - return t + return i.getTimeField("DTEND") } func (i *ICal) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { diff --git a/src/calanonsync/caldav_test.go b/src/calanonsync/caldav_test.go index 3fc26a5..69314da 100644 --- a/src/calanonsync/caldav_test.go +++ b/src/calanonsync/caldav_test.go @@ -81,7 +81,7 @@ func TestParseICal(t *testing.T) { end := i.End() expectedStart := time.Date(2018, 4, 7, 0, 0, 0, 0, time.Local) - expectedEnd := time.Date(2018, 4, 10, 0, 0, 0, 0, time.Local) + expectedEnd := time.Date(2018, 4, 9, 0, 0, 0, 0, time.Local) if !start.Equal(expectedStart) { t.Errorf("Unexpected start time (%s != %s)", start, expectedStart) @@ -107,8 +107,7 @@ func TestICal_Update(t *testing.T) { t.Error("End not updated correctly") } - stamp, _ := ical.getTimeField("DTSTAMP") - if stamp.Sub(time.Now()) > 5*time.Second { + if ical.getTimeField("DTSTAMP").Sub(time.Now()) > 5*time.Second { t.Error("Timestamp doesn't seem to be updated") } }) @@ -169,8 +168,7 @@ END:VCALENDAR` { t.Error("End not updated correctly") } - stamp, _ := ical.getTimeField("DTSTAMP") - if stamp.Sub(time.Now()) > 5*time.Second { + if ical.getTimeField("DTSTAMP").Sub(time.Now()) > 5*time.Second { t.Error("Timestamp doesn't seem to be updated") } })