From d886cb68bcc3ebabd601ac3242504de55a42987a Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Tue, 4 Aug 2026 14:44:40 +0300 Subject: [PATCH] Make EAT the default app timezone for date filters. Centralize Africa/Addis_Ababa and interpret YYYY-MM-DD query bounds in EAT instead of UTC. Co-authored-by: Cursor --- internal/domain/analytics_filter.go | 22 ++++--------- internal/domain/timezone.go | 33 +++++++++++++++++++ internal/domain/timezone_test.go | 30 +++++++++++++++++ .../web_server/handlers/payments_admin.go | 5 +-- 4 files changed, 73 insertions(+), 17 deletions(-) create mode 100644 internal/domain/timezone.go create mode 100644 internal/domain/timezone_test.go diff --git a/internal/domain/analytics_filter.go b/internal/domain/analytics_filter.go index 9f386827..4e253414 100644 --- a/internal/domain/analytics_filter.go +++ b/internal/domain/analytics_filter.go @@ -15,16 +15,13 @@ const ( AnalyticsFilterYearMonth = "year_month" AnalyticsFilterCustom = "custom" - AnalyticsTimezoneName = "Africa/Addis_Ababa" + // AnalyticsTimezoneName is an alias of the app default timezone (EAT). + AnalyticsTimezoneName = AppTimezoneName ) -// AnalyticsTimezone is East Africa Time (EAT, UTC+3), used for analytics calendar buckets. +// AnalyticsTimezone is the app default timezone (EAT), used for analytics calendar buckets. func AnalyticsTimezone() *time.Location { - loc, err := time.LoadLocation(AnalyticsTimezoneName) - if err != nil { - return time.FixedZone("EAT", 3*60*60) - } - return loc + return AppTimezone() } // AnalyticsDateFilter describes the effective reporting window for dashboard analytics. @@ -55,7 +52,7 @@ type AnalyticsDateFilter struct { func ParseAnalyticsDateFilter(c *fiber.Ctx) (AnalyticsDateFilter, error) { loc := AnalyticsTimezone() now := time.Now().In(loc) - today := dateOnlyIn(loc, now) + today := DateOnly(now) fromRaw := strings.TrimSpace(c.Query("from")) toRaw := strings.TrimSpace(c.Query("to")) @@ -176,16 +173,11 @@ func parseAnalyticsPaymentMethod(raw string) (*string, error) { func parseAnalyticsDateIn(raw string, loc *time.Location) (time.Time, error) { if t, err := time.Parse(time.RFC3339, raw); err == nil { - return dateOnlyIn(loc, t), nil + return DateOnly(t), nil } t, err := time.ParseInLocation("2006-01-02", raw, loc) if err != nil { return time.Time{}, err } - return dateOnlyIn(loc, t), nil -} - -func dateOnlyIn(loc *time.Location, t time.Time) time.Time { - t = t.In(loc) - return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc) + return DateOnly(t), nil } diff --git a/internal/domain/timezone.go b/internal/domain/timezone.go new file mode 100644 index 00000000..447a3dbb --- /dev/null +++ b/internal/domain/timezone.go @@ -0,0 +1,33 @@ +package domain + +import "time" + +// AppTimezoneName is the default business timezone for Yimaru (East Africa Time). +const AppTimezoneName = "Africa/Addis_Ababa" + +// AppTimezone returns Africa/Addis_Ababa (EAT, UTC+3). Used as the default for +// calendar day filters, analytics buckets, and admin-facing date interpretation. +func AppTimezone() *time.Location { + loc, err := time.LoadLocation(AppTimezoneName) + if err != nil { + return time.FixedZone("EAT", 3*60*60) + } + return loc +} + +// ParseDateOnly parses YYYY-MM-DD as midnight in the app timezone (EAT). +func ParseDateOnly(raw string) (time.Time, error) { + return time.ParseInLocation("2006-01-02", raw, AppTimezone()) +} + +// DateOnly truncates t to midnight in the app timezone (EAT). +func DateOnly(t time.Time) time.Time { + loc := AppTimezone() + local := t.In(loc) + return time.Date(local.Year(), local.Month(), local.Day(), 0, 0, 0, 0, loc) +} + +// NextDateOnly returns the next calendar day at midnight in the app timezone. +func NextDateOnly(t time.Time) time.Time { + return DateOnly(t).AddDate(0, 0, 1) +} diff --git a/internal/domain/timezone_test.go b/internal/domain/timezone_test.go new file mode 100644 index 00000000..7ca69976 --- /dev/null +++ b/internal/domain/timezone_test.go @@ -0,0 +1,30 @@ +package domain + +import ( + "testing" + "time" +) + +func TestParseDateOnly_usesEAT(t *testing.T) { + got, err := ParseDateOnly("2026-08-01") + if err != nil { + t.Fatal(err) + } + want := time.Date(2026, time.August, 1, 0, 0, 0, 0, AppTimezone()) + if !got.Equal(want) { + t.Fatalf("got %v want %v", got, want) + } + if got.UTC().Hour() != 21 { + t.Fatalf("EAT midnight should be 21:00 UTC previous day, got %v", got.UTC()) + } +} + +func TestDateOnly_truncatesInEAT(t *testing.T) { + // 2026-07-31 21:30 UTC = 2026-08-01 00:30 EAT + in := time.Date(2026, time.July, 31, 21, 30, 0, 0, time.UTC) + got := DateOnly(in) + want := time.Date(2026, time.August, 1, 0, 0, 0, 0, AppTimezone()) + if !got.Equal(want) { + t.Fatalf("got %v want %v", got, want) + } +} diff --git a/internal/web_server/handlers/payments_admin.go b/internal/web_server/handlers/payments_admin.go index 1725dc62..04a309b7 100644 --- a/internal/web_server/handlers/payments_admin.go +++ b/internal/web_server/handlers/payments_admin.go @@ -200,8 +200,9 @@ func parseQueryTime(raw string) (time.Time, error) { if t, err := time.Parse(time.RFC3339, raw); err == nil { return t.UTC(), nil } - if t, err := time.Parse("2006-01-02", raw); err == nil { - return t.UTC(), nil + // Date-only values are interpreted as midnight in the app timezone (EAT). + if t, err := domain.ParseDateOnly(raw); err == nil { + return t, nil } return time.Time{}, fmt.Errorf("unsupported time format") }