Skip to content

Commit 795a41f

Browse files
committed
Forced timezone with godotenv.
1 parent 5a51227 commit 795a41f

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ AWS_ENDPOINT_URL_S3=http://host.docker.internal:9001
4242
AWS_EC2_METADATA_DISABLED="true"
4343

4444
MINIO_ROOT_USER=_peerdb_minioadmin
45-
MINIO_ROOT_PASSWORD=_peerdb_minioadmin
45+
MINIO_ROOT_PASSWORD=_peerdb_minioadmin
46+
47+
TZ='UTC'

flow/internal/testutil/env.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,24 @@ import (
66
"os"
77
"path/filepath"
88
"sync/atomic"
9+
"time"
910

1011
"github.com/joho/godotenv"
1112
)
1213

14+
const timeZoneEnvKey = "TZ"
15+
1316
var loadedEnv atomic.Bool
1417

18+
func forceTimeZone(tzString string) error {
19+
location, err := time.LoadLocation(tzString)
20+
if err != nil {
21+
return err
22+
}
23+
time.Local = location
24+
return nil
25+
}
26+
1527
// LoadEnv walks up from the current directory until the project root
1628
// is found and loads the .env file if it exists.
1729
// After the first call, subsequent calls to LoadEnv are no-ops.
@@ -40,6 +52,16 @@ func LoadEnv() {
4052
if err := godotenv.Load(envPath); err != nil {
4153
slog.ErrorContext(ctx, "LoadEnv: failed to load .env", "path", envPath, "error", err)
4254
}
55+
56+
maybeForcedTZ := os.Getenv(timeZoneEnvKey)
57+
58+
if maybeForcedTZ != "" {
59+
slog.InfoContext(ctx, "LoadEnv: attempting to force time zone from environment variable", "key", timeZoneEnvKey, "forced_value", maybeForcedTZ)
60+
if err := forceTimeZone(maybeForcedTZ); err != nil {
61+
slog.ErrorContext(ctx, "LoadEnv: failed to force time zone", "error", err)
62+
}
63+
}
64+
4365
return
4466
}
4567

0 commit comments

Comments
 (0)