From 561ed63ac782ccb7f29c93aae5906ee797de5703 Mon Sep 17 00:00:00 2001 From: pavel Date: Wed, 8 Apr 2026 21:54:57 +0200 Subject: [PATCH] timezone --- .env.example | 1 + README.md | 1 + internal/config/config.go | 2 ++ internal/http/app.go | 9 ++++++++- internal/http/render.go | 8 ++++++-- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 1e9326e..c2e1cd6 100644 --- a/.env.example +++ b/.env.example @@ -4,3 +4,4 @@ SESSION_KEY=dev-session-key-change-me APP_ENV=development ADMIN_EMAIL=admin@superbanka.local ADMIN_PASSWORD=admin123456 +TIMEZONE=UTC diff --git a/README.md b/README.md index 6b78ea1..53bda15 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ The app listens on `http://localhost:8080` by default. Postgres is exposed on `localhost:5433` to avoid conflicts with any existing local instance. The app loads `.env` automatically on startup. Existing shell environment variables still take precedence over values in `.env`. +Set `TIMEZONE` to any IANA timezone name such as `UTC` or `Europe/Prague` to control how timestamps are rendered in the UI. Default bootstrap admin credentials: diff --git a/internal/config/config.go b/internal/config/config.go index b648910..b9690bb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -14,6 +14,7 @@ type Config struct { AppEnv string AdminEmail string AdminPass string + Timezone string } func Load() Config { @@ -28,6 +29,7 @@ func Load() Config { AppEnv: getenv("APP_ENV", "development"), AdminEmail: getenv("ADMIN_EMAIL", "admin@superbanka.local"), AdminPass: getenv("ADMIN_PASSWORD", "admin123456"), + Timezone: getenv("TIMEZONE", "UTC"), } } diff --git a/internal/http/app.go b/internal/http/app.go index 4d24bcd..c65107d 100644 --- a/internal/http/app.go +++ b/internal/http/app.go @@ -97,8 +97,15 @@ func NewApp(cfg config.Config) (*App, error) { return nil, fmt.Errorf("run migrations: %w", err) } + location, err := time.LoadLocation(cfg.Timezone) + if err != nil { + dbPool.Close() + return nil, fmt.Errorf("load timezone %q: %w", cfg.Timezone, err) + } + time.Local = location + store := db.NewStore(dbPool) - renderer, err := NewRenderer() + renderer, err := NewRenderer(location) if err != nil { dbPool.Close() return nil, err diff --git a/internal/http/render.go b/internal/http/render.go index bdc171f..bb70f9d 100644 --- a/internal/http/render.go +++ b/internal/http/render.go @@ -25,7 +25,11 @@ type Renderer struct { templates *template.Template } -func NewRenderer() (*Renderer, error) { +func NewRenderer(location *time.Location) (*Renderer, error) { + if location == nil { + location = time.UTC + } + funcs := template.FuncMap{ "money": platform.FormatCZK, "dt": func(t time.Time) string { @@ -33,7 +37,7 @@ func NewRenderer() (*Renderer, error) { return "" } - return t.Format("02. 01. 2006 15:04") + return t.In(location).Format("02. 01. 2006 15:04") }, }