Skip to content

Commit cdd1956

Browse files
refactor(calendar): extract timezone resolution to shared helper
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 38471f9 commit cdd1956

3 files changed

Lines changed: 30 additions & 39 deletions

File tree

internal/cmd/calendar_event_days.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,8 @@ func wrapEventWithDaysWithTimezone(event *calendar.Event, calendarTimezone strin
3737
return nil
3838
}
3939
startDay, endDay := eventDaysOfWeek(event)
40-
calendarTimezone = strings.TrimSpace(calendarTimezone)
4140
evTimezone := eventTimezone(event)
42-
43-
if loc == nil && calendarTimezone != "" {
44-
if loaded, err := time.LoadLocation(calendarTimezone); err == nil {
45-
loc = loaded
46-
} else {
47-
calendarTimezone = ""
48-
}
49-
}
50-
if calendarTimezone == "" {
51-
calendarTimezone = evTimezone
52-
if loc == nil && calendarTimezone != "" {
53-
if loaded, err := time.LoadLocation(calendarTimezone); err == nil {
54-
loc = loaded
55-
} else {
56-
calendarTimezone = ""
57-
}
58-
}
59-
}
41+
calendarTimezone, loc = resolveEventTimezone(event, calendarTimezone, loc)
6042

6143
startLocal := formatEventLocal(event.Start, loc)
6244
endLocal := formatEventLocal(event.End, loc)
@@ -152,3 +134,31 @@ func loadEventLocation(tz string) (*time.Location, bool) {
152134
}
153135
return loc, true
154136
}
137+
138+
// resolveEventTimezone resolves the timezone and location for an event.
139+
// It tries the calendar timezone first, then falls back to the event's timezone.
140+
// Returns the resolved timezone name and location. If loc is already provided,
141+
// it will be used as-is (only calendarTimezone may be updated for display).
142+
func resolveEventTimezone(event *calendar.Event, calendarTimezone string, loc *time.Location) (string, *time.Location) {
143+
calendarTimezone = strings.TrimSpace(calendarTimezone)
144+
evTimezone := eventTimezone(event)
145+
146+
if loc == nil && calendarTimezone != "" {
147+
if loaded, err := time.LoadLocation(calendarTimezone); err == nil {
148+
loc = loaded
149+
} else {
150+
calendarTimezone = ""
151+
}
152+
}
153+
if calendarTimezone == "" {
154+
calendarTimezone = evTimezone
155+
if loc == nil && calendarTimezone != "" {
156+
if loaded, err := time.LoadLocation(calendarTimezone); err == nil {
157+
loc = loaded
158+
} else {
159+
calendarTimezone = ""
160+
}
161+
}
162+
}
163+
return calendarTimezone, loc
164+
}

internal/cmd/calendar_print.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,8 @@ func printCalendarEventWithTimezone(u *ui.UI, event *calendar.Event, calendarTim
1818
if u == nil || event == nil {
1919
return
2020
}
21-
calendarTimezone = strings.TrimSpace(calendarTimezone)
2221
eventTimezone := eventTimezone(event)
23-
24-
if loc == nil && calendarTimezone != "" {
25-
if loaded, err := time.LoadLocation(calendarTimezone); err == nil {
26-
loc = loaded
27-
} else {
28-
calendarTimezone = ""
29-
}
30-
}
31-
if calendarTimezone == "" {
32-
calendarTimezone = eventTimezone
33-
if loc == nil && calendarTimezone != "" {
34-
if loaded, err := time.LoadLocation(calendarTimezone); err == nil {
35-
loc = loaded
36-
} else {
37-
calendarTimezone = ""
38-
}
39-
}
40-
}
22+
calendarTimezone, loc = resolveEventTimezone(event, calendarTimezone, loc)
4123

4224
u.Out().Printf("id\t%s", event.Id)
4325
u.Out().Printf("summary\t%s", orEmpty(event.Summary, "(no title)"))

internal/cmd/calendar_time.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"context"
5-
"fmt"
65
"os"
76
"time"
87

0 commit comments

Comments
 (0)