diff --git a/server/server.go b/server/server.go index 2b48d4a..d4ab599 100644 --- a/server/server.go +++ b/server/server.go @@ -6,10 +6,11 @@ import ( "net/http" "os" "path" + "regexp" "strconv" "strings" "sync" - "regexp" + "time" "emperror.dev/errors" "github.com/apex/log" @@ -146,6 +147,25 @@ func (s *Server) Context() context.Context { return s.ctx } +// DetermineServerTimezone checks the envvars for a non-empty SERVER_TIMEZONE key, +// validates if it's a valid timezone, and returns it. If not, returns the defaultTimezone. +func DetermineServerTimezone(envvars map[string]interface{}, defaultTimezone string) string { + // Check if SERVER_TIMEZONE exists in envvars and is non-empty + timezone, ok := envvars["SERVER_TIMEZONE"].(string) + if ok && timezone != "" { + // Validate the timezone + _, err := time.LoadLocation(timezone) + if err == nil { + // Valid timezone, return it + return timezone + } + // Invalid timezone, fall through to return defaultTimezone + } + + // Return the defaultTimezone if SERVER_TIMEZONE is not set, empty, or invalid + return defaultTimezone +} + // parseInvocation parses the start command in the same way we already do in the entrypoint // We can use this to set the container command with all variables replaced. func parseInvocation(invocation string, envvars map[string]interface{}, memory int64, port int, ip string) (parsed string) { @@ -191,8 +211,7 @@ func parseInvocation(invocation string, envvars map[string]interface{}, memory i // server instance. func (s *Server) GetEnvironmentVariables() []string { out := []string{ - // TODO: allow this to be overridden by the user. - fmt.Sprintf("TZ=%s", config.Get().System.Timezone), + fmt.Sprintf("TZ=%s", DetermineServerTimezone(s.Config().EnvVars, config.Get().System.Timezone)), fmt.Sprintf("STARTUP=%s", parseInvocation(s.Config().Invocation, s.Config().EnvVars, s.MemoryLimit(), s.Config().Allocations.DefaultMapping.Port, s.Config().Allocations.DefaultMapping.Ip)), fmt.Sprintf("SERVER_MEMORY=%d", s.MemoryLimit()), fmt.Sprintf("SERVER_IP=%s", s.Config().Allocations.DefaultMapping.Ip),