From 72142e07f1baebde340f4f6dde38d4500d72378f Mon Sep 17 00:00:00 2001 From: ondrej-salat <78552892+ondrej-salat@users.noreply.github.com> Date: Sun, 8 Feb 2026 17:23:51 +0100 Subject: [PATCH] refactor Event; prepare for merge --- pkg/core/constants.go | 1 - pkg/core/event.go | 26 ++++++++++++++--------- pkg/core/utils.go | 49 ++++++++++++++++++++++++++++++++++++++----- 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/pkg/core/constants.go b/pkg/core/constants.go index 0271310..e67fcd4 100644 --- a/pkg/core/constants.go +++ b/pkg/core/constants.go @@ -14,7 +14,6 @@ const ( None TimeUnit = iota Day Week - TwoWeeks Month Year ) diff --git a/pkg/core/event.go b/pkg/core/event.go index c13b003..d7a1507 100644 --- a/pkg/core/event.go +++ b/pkg/core/event.go @@ -8,16 +8,22 @@ import ( ) type Event struct { - Id uuid.UUID `json:"id"` // shouldn't change (different id = different event) - Title string `json:"title"` - Location string `json:"location"` - From time.Time `json:"from"` - To time.Time `json:"to"` - Duration time.Duration `json:"duration"` - Notes string `json:"notes"` - Repetition TimeUnit `json:"repetition"` - RepeatExceptions []string `json:"repeat_exceptions"` - ParentId uuid.UUID `json:"parentId"` + Id uuid.UUID `json:"id"` // shouldn't change (different id = different event) + Title string `json:"title"` + Location string `json:"location"` + From time.Time `json:"from"` + To time.Time `json:"to"` + Description string `json:"description"` + Repeat *Repetition `json:"repeat"` // nil if not repeating +} + +type Repetition struct { + Frequency TimeUnit `json:"frequency"` + Interval uint `json:"interval"` + Until time.Time `json:"until"` + Count uint `json:"count"` + Exceptions []time.Time `json:"exceptions"` + ParentId uuid.UUID `json:"parentId"` } func (e *Event) Validate() error { diff --git a/pkg/core/utils.go b/pkg/core/utils.go index cccfb85..3d7bb74 100644 --- a/pkg/core/utils.go +++ b/pkg/core/utils.go @@ -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: @@ -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: @@ -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) @@ -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 +}