Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/limayaml/defaults_unix.go
2611 views
1
//go:build !windows
2
3
// SPDX-FileCopyrightText: Copyright The Lima Authors
4
// SPDX-License-Identifier: Apache-2.0
5
6
package limayaml
7
8
import (
9
"errors"
10
"fmt"
11
"os"
12
"path/filepath"
13
"strings"
14
"time"
15
16
"github.com/sirupsen/logrus"
17
)
18
19
func hostTimeZone() string {
20
if tzBytes, err := os.ReadFile("/etc/timezone"); err == nil {
21
if tz := strings.TrimSpace(string(tzBytes)); tz != "" {
22
if _, err := time.LoadLocation(tz); err != nil {
23
logrus.Warnf("invalid timezone found in /etc/timezone: %v", err)
24
} else {
25
return tz
26
}
27
}
28
}
29
30
if zoneinfoFile, err := filepath.EvalSymlinks("/etc/localtime"); err == nil {
31
if tz, err := extractTZFromPath(zoneinfoFile); err != nil {
32
logrus.Warnf("failed to extract timezone from %s: %v", zoneinfoFile, err)
33
} else {
34
return tz
35
}
36
}
37
38
logrus.Warn("unable to determine host timezone, falling back to default value")
39
return ""
40
}
41
42
func extractTZFromPath(zoneinfoFile string) (string, error) {
43
if zoneinfoFile == "" {
44
return "", errors.New("invalid zoneinfo file path")
45
}
46
47
if _, err := os.Stat(zoneinfoFile); os.IsNotExist(err) {
48
return "", fmt.Errorf("zoneinfo file does not exist: %s", zoneinfoFile)
49
}
50
51
for dir := filepath.Dir(zoneinfoFile); dir != filepath.Dir(dir); dir = filepath.Dir(dir) {
52
if _, err := os.Stat(filepath.Join(dir, "Etc", "UTC")); err == nil {
53
return filepath.Rel(dir, zoneinfoFile)
54
}
55
}
56
57
return "", errors.New("timezone base directory not found")
58
}
59
60