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 <cursoragent@cursor.com>
This commit is contained in:
parent
651e9e7409
commit
d886cb68bc
|
|
@ -15,16 +15,13 @@ const (
|
||||||
AnalyticsFilterYearMonth = "year_month"
|
AnalyticsFilterYearMonth = "year_month"
|
||||||
AnalyticsFilterCustom = "custom"
|
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 {
|
func AnalyticsTimezone() *time.Location {
|
||||||
loc, err := time.LoadLocation(AnalyticsTimezoneName)
|
return AppTimezone()
|
||||||
if err != nil {
|
|
||||||
return time.FixedZone("EAT", 3*60*60)
|
|
||||||
}
|
|
||||||
return loc
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnalyticsDateFilter describes the effective reporting window for dashboard analytics.
|
// AnalyticsDateFilter describes the effective reporting window for dashboard analytics.
|
||||||
|
|
@ -55,7 +52,7 @@ type AnalyticsDateFilter struct {
|
||||||
func ParseAnalyticsDateFilter(c *fiber.Ctx) (AnalyticsDateFilter, error) {
|
func ParseAnalyticsDateFilter(c *fiber.Ctx) (AnalyticsDateFilter, error) {
|
||||||
loc := AnalyticsTimezone()
|
loc := AnalyticsTimezone()
|
||||||
now := time.Now().In(loc)
|
now := time.Now().In(loc)
|
||||||
today := dateOnlyIn(loc, now)
|
today := DateOnly(now)
|
||||||
|
|
||||||
fromRaw := strings.TrimSpace(c.Query("from"))
|
fromRaw := strings.TrimSpace(c.Query("from"))
|
||||||
toRaw := strings.TrimSpace(c.Query("to"))
|
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) {
|
func parseAnalyticsDateIn(raw string, loc *time.Location) (time.Time, error) {
|
||||||
if t, err := time.Parse(time.RFC3339, raw); err == nil {
|
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)
|
t, err := time.ParseInLocation("2006-01-02", raw, loc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return time.Time{}, err
|
return time.Time{}, err
|
||||||
}
|
}
|
||||||
return dateOnlyIn(loc, t), nil
|
return DateOnly(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)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
33
internal/domain/timezone.go
Normal file
33
internal/domain/timezone.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
30
internal/domain/timezone_test.go
Normal file
30
internal/domain/timezone_test.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -200,8 +200,9 @@ func parseQueryTime(raw string) (time.Time, error) {
|
||||||
if t, err := time.Parse(time.RFC3339, raw); err == nil {
|
if t, err := time.Parse(time.RFC3339, raw); err == nil {
|
||||||
return t.UTC(), nil
|
return t.UTC(), nil
|
||||||
}
|
}
|
||||||
if t, err := time.Parse("2006-01-02", raw); err == nil {
|
// Date-only values are interpreted as midnight in the app timezone (EAT).
|
||||||
return t.UTC(), nil
|
if t, err := domain.ParseDateOnly(raw); err == nil {
|
||||||
|
return t, nil
|
||||||
}
|
}
|
||||||
return time.Time{}, fmt.Errorf("unsupported time format")
|
return time.Time{}, fmt.Errorf("unsupported time format")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user