@@ -20,6 +20,7 @@ import (
2020 "strings"
2121 "sync"
2222 "text/template"
23+ "time"
2324
2425 "github.com/coreos/go-semver/semver"
2526 "github.com/docker/go-units"
@@ -137,21 +138,22 @@ func MACAddress(uniqueID string) string {
137138
138139func hostTimeZone () string {
139140 // WSL2 will automatically set the timezone
140- if runtime .GOOS != "windows" {
141- tz , err := os .ReadFile ("/etc/timezone" )
142- if err == nil {
143- return strings .TrimSpace (string (tz ))
144- }
145- zoneinfoFile , err := filepath .EvalSymlinks ("/etc/localtime" )
146- if err == nil {
147- for baseDir := filepath .Dir (zoneinfoFile ); baseDir != "/" ; baseDir = filepath .Dir (baseDir ) {
148- if _ , err = os .Stat (filepath .Join (baseDir , "Etc/UTC" )); err == nil {
149- return strings .TrimPrefix (zoneinfoFile , baseDir + "/" )
150- }
151- }
152- logrus .Warnf ("could not locate zoneinfo directory from %q" , zoneinfoFile )
141+ if runtime .GOOS == "windows" {
142+ return ""
143+ }
144+
145+ if tzBytes , err := os .ReadFile ("/etc/timezone" ); err == nil {
146+ if tz := strings .TrimSpace (string (tzBytes )); isValidTimezone (tz ) {
147+ return tz
153148 }
154149 }
150+
151+ if zoneinfoFile , err := filepath .EvalSymlinks ("/etc/localtime" ); err == nil {
152+ if tz := extractTimezoneFromPath (zoneinfoFile ); isValidTimezone (tz ) {
153+ return tz
154+ }
155+ }
156+
155157 return ""
156158}
157159
@@ -1309,3 +1311,27 @@ func unique(s []string) []string {
13091311 }
13101312 return list
13111313}
1314+
1315+ func isValidTimezone (tz string ) bool {
1316+ if tz == "" {
1317+ return false
1318+ }
1319+ _ , err := time .LoadLocation (tz )
1320+ if err != nil {
1321+ if len (tz ) > 30 {
1322+ tz = tz [:30 ] + "..."
1323+ }
1324+ logrus .Warnf ("invalid timezone %q" , tz )
1325+ return false
1326+ }
1327+ return true
1328+ }
1329+
1330+ func extractTimezoneFromPath (zoneinfoFile string ) string {
1331+ for baseDir := filepath .Dir (zoneinfoFile ); baseDir != "/" ; baseDir = filepath .Dir (baseDir ) {
1332+ if _ , err := os .Stat (filepath .Join (baseDir , "Etc/UTC" )); err == nil {
1333+ return strings .TrimPrefix (zoneinfoFile , baseDir + string (os .PathSeparator ))
1334+ }
1335+ }
1336+ return ""
1337+ }
0 commit comments