Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pkg/core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const (
None TimeUnit = iota
Day
Week
TwoWeeks
Month
Year
)
49 changes: 44 additions & 5 deletions pkg/core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ func addUnit(t time.Time, value int, unit TimeUnit) time.Time {
return t.AddDate(0, 0, value)
case Week:
return t.AddDate(0, 0, 7*value)
case TwoWeeks:
return t.AddDate(0, 0, 14*value)
case Month:
return t.AddDate(0, value, 0)
case Year:
Expand All @@ -19,6 +17,43 @@ func addUnit(t time.Time, value int, unit TimeUnit) time.Time {
}
}

func getFirstCandidate2(searchStart time.Time, event *Event) time.Time {
switch event.Repeat.Frequency {
case Day:
diffHours := searchStart.Sub(event.From).Hours()
cycleHours := 24.0 * float64(event.Repeat.Interval)
cyclesPassed := int(diffHours / cycleHours)
days := cyclesPassed * int(event.Repeat.Interval)
return addUnit(event.From, days, Day)
case Week:
diffHours := searchStart.Sub(event.From).Hours()
cycleHours := 24.0 * 7 * float64(event.Repeat.Interval)
cyclesPassed := int(diffHours / cycleHours)
weeks := cyclesPassed * int(event.Repeat.Interval)
return addUnit(event.From, weeks, Week)
case Month:
diffMonths := (searchStart.Year()-event.From.Year())*12 + int(searchStart.Month()-event.From.Month())
cycles := diffMonths / int(event.Repeat.Interval)
months := cycles * int(event.Repeat.Interval)
candidate := addUnit(event.From, months, Month)
if candidate.Before(searchStart) {
candidate = addUnit(event.From, int(event.Repeat.Interval), Month)
}
return candidate
case Year:
diffYears := searchStart.Year() - event.From.Year()
cycles := diffYears / int(event.Repeat.Interval)
years := cycles * int(event.Repeat.Interval)
candidate := addUnit(event.From, years, Year)
if candidate.Before(searchStart) {
candidate = addUnit(event.From, int(event.Repeat.Interval), Year)
}
return candidate
default:
return event.From
}
}

func getFirstCandidate(eventStart, searchStart time.Time, unit TimeUnit) time.Time {
switch unit {
case Day:
Expand All @@ -27,9 +62,6 @@ func getFirstCandidate(eventStart, searchStart time.Time, unit TimeUnit) time.Ti
case Week:
weeks := int(searchStart.Sub(eventStart).Hours() / (24 * 7))
return addUnit(eventStart, weeks, Week)
case TwoWeeks:
twoWeeks := int(searchStart.Sub(eventStart).Hours() / (24 * 14))
return addUnit(eventStart, twoWeeks, TwoWeeks)
case Month:
months := (searchStart.Year()-eventStart.Year())*12 + int(searchStart.Month()-eventStart.Month())
return addUnit(eventStart, months, Month)
Expand All @@ -40,3 +72,10 @@ func getFirstCandidate(eventStart, searchStart time.Time, unit TimeUnit) time.Ti
return eventStart
}
}

func getEarlierTime(t1, t2 time.Time) time.Time {
if t1.Before(t2) {
return t1
}
return t2
}