From 42a4e32daca13b518e64e5821080ff3d6adf0e39 Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Mon, 26 Jan 2026 16:49:44 -1000 Subject: Use configured timezone throughout codebase - Add config/timezone.go with timezone utilities: - SetDisplayTimezone(), GetDisplayTimezone() - Now(), Today() - current time/date in display TZ - ParseDateInDisplayTZ(), ToDisplayTZ() - parsing helpers - Initialize timezone at startup in main.go - Update all datetime logic to use configured timezone: - handlers/handlers.go - all time.Now() calls - handlers/timeline.go - date parsing - handlers/timeline_logic.go - now calculation - models/atom.go - ComputeUIFields() - models/timeline.go - ComputeDaySection() - api/plantoeat.go - meal date parsing - api/todoist.go - due date parsing - api/trello.go - due date parsing This ensures all dates/times display correctly regardless of server timezone setting. Co-Authored-By: Claude Opus 4.5 --- internal/config/timezone.go | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 internal/config/timezone.go (limited to 'internal/config') diff --git a/internal/config/timezone.go b/internal/config/timezone.go new file mode 100644 index 0000000..04f218e --- /dev/null +++ b/internal/config/timezone.go @@ -0,0 +1,56 @@ +package config + +import ( + "sync" + "time" +) + +var ( + displayTimezone *time.Location + tzOnce sync.Once +) + +// SetDisplayTimezone sets the global display timezone (call once at startup) +func SetDisplayTimezone(tz string) { + tzOnce.Do(func() { + loc, err := time.LoadLocation(tz) + if err != nil { + loc = time.UTC + } + displayTimezone = loc + }) +} + +// GetDisplayTimezone returns the configured display timezone +func GetDisplayTimezone() *time.Location { + if displayTimezone == nil { + return time.UTC + } + return displayTimezone +} + +// Now returns the current time in the configured display timezone +func Now() time.Time { + return time.Now().In(GetDisplayTimezone()) +} + +// Today returns today's date at midnight in the configured timezone +func Today() time.Time { + now := Now() + return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, GetDisplayTimezone()) +} + +// ParseDateInDisplayTZ parses a date string (2006-01-02) in the display timezone +func ParseDateInDisplayTZ(dateStr string) (time.Time, error) { + return time.ParseInLocation("2006-01-02", dateStr, GetDisplayTimezone()) +} + +// ParseDateTimeInDisplayTZ parses a datetime string in the display timezone +func ParseDateTimeInDisplayTZ(layout, value string) (time.Time, error) { + return time.ParseInLocation(layout, value, GetDisplayTimezone()) +} + +// ToDisplayTZ converts a time to the display timezone +func ToDisplayTZ(t time.Time) time.Time { + return t.In(GetDisplayTimezone()) +} -- cgit v1.2.3