Fixed endDate being wrong for wholeDay events (it actually should be +1)

This commit is contained in:
Andreas Schneider 2018-04-02 13:52:52 +02:00
parent 88fe09795e
commit 10a3cc9637
2 changed files with 7 additions and 25 deletions

View File

@ -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 {

View File

@ -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")
}
})