-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimezone_unix.go
238 lines (186 loc) · 5.16 KB
/
timezone_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//go:build darwin || linux
package timezone
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/yookoala/realpath"
)
var timezoneRegex = regexp.MustCompile(`^\s*(TIMEZONE|ZONE)\s*=\s*\"(?P<tz>.*)\"$`)
// Name tries to find the local timezone configuration. It returns the timezone name
// if found. If not, an error is returned.
func Name() (string, error) {
// first try the ENV setting
if tzenv := parseEnv(); tzenv != "" {
return tzenv, nil
}
// now look for distribution specific configuration files
// that contain the timezone name.
timezones := []string{}
timezones = append(timezones, parseFromConfigFile([]string{
"/etc/timezone",
"/var/db/zoneinfo"})...)
timezones = append(timezones, parseFromClock([]string{
"/etc/sysconfig/clock",
"/etc/conf.d/clock"})...)
parsed := parseSymlink("/etc/localtime")
if parsed != "" {
timezones = append(timezones, parsed)
}
return resolveTimezones(timezones, "/usr/share/zoneinfo")
}
// parseEnv parses timezone from TZ env var.
func parseEnv() string {
tzenv := os.Getenv("TZ")
if tzenv == "" {
return ""
}
if _, ok := timezones[tzenv]; ok {
return tzenv
}
if filepath.IsAbs(tzenv) && fileExists(tzenv) {
// it's a file specification
parts := strings.Split(tzenv, string(os.PathSeparator))
// is it a zone info zone?
joined := strings.Join(parts[len(parts)-2:], "/")
if _, ok := timezones[joined]; ok {
return joined
}
// maybe it's a short one, like UTC?
if _, ok := timezones[parts[len(parts)-1]]; ok {
return parts[len(parts)-1]
}
}
return ""
}
// parse parses timezone from configuration files.
func parseFromConfigFile(paths []string) []string {
timezones := []string{}
for _, configfile := range paths {
data, err := os.ReadFile(configfile)
if err != nil {
continue
}
etctz := strings.Trim(string(data), "/ \t\r\n")
if etctz == "" {
continue
}
lines := strings.Split(strings.ReplaceAll(etctz, "\r\n", "\n"), "\n")
for _, line := range lines {
// get rid of host definitions and comments
if strings.Contains(line, " ") {
line = strings.SplitN(line, " ", 2)[0]
}
if strings.Contains(line, "#") {
line = strings.SplitN(line, "#", 2)[0]
}
line = strings.TrimSpace(line)
if line != "" {
timezones = append(timezones, strings.ReplaceAll(line, " ", "_"))
}
}
}
return timezones
}
// parseFromClock parses timezone from clock files.
// CentOS has a ZONE setting in /etc/sysconfig/clock,
// OpenSUSE has a TIMEZONE setting in /etc/sysconfig/clock and
// Gentoo has a TIMEZONE setting in /etc/conf.d/clock.
func parseFromClock(paths []string) []string {
timezones := []string{}
for _, filename := range paths {
file, err := os.Open(filename)
if err != nil {
continue
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// look for the TIMEZONE|ZONE= setting
match := timezoneRegex.FindStringSubmatch(line)
paramsMap := make(map[string]string)
for i, name := range timezoneRegex.SubexpNames() {
if i > 0 && i <= len(match) {
paramsMap[name] = match[i]
}
}
if len(paramsMap) == 0 || paramsMap["tz"] == "" {
continue
}
timezones = append(timezones, strings.ReplaceAll(paramsMap["tz"], " ", "_"))
}
}
return timezones
}
// parseSymlink parses symbolic link to resolve timezone name.
// The systemd distributions use symlinks that include the zone name.
func parseSymlink(path string) string {
fileInfo, err := os.Lstat(path)
if err != nil {
return ""
}
// is symlink?
if fileInfo.Mode()&os.ModeSymlink != 0 {
tz, err := os.Readlink(path)
if err != nil {
return ""
}
idx := strings.Index(tz, "zoneinfo/")
if idx == -1 {
return ""
}
return strings.ReplaceAll(tz[idx+9:], " ", "_")
}
return ""
}
// resolveTimezones resolves conflicted timezones. Otherwise returns an error.
func resolveTimezones(timezones []string, zoneinfo string) (string, error) {
if len(timezones) == 0 {
return "", nil
}
if len(timezones) == 1 {
return timezones[0], nil
}
// multiple configs. See if they match
var filtered []string
depth := len(strings.Split(zoneinfo, string(os.PathSeparator)))
for _, tzname := range timezones {
// look them up in '/usr/share/zoneinfo', and find what they really point to
path, err := realpath.Realpath(filepath.Join(zoneinfo, tzname))
if err != nil {
continue
}
name := strings.Join(strings.Split(path, string(os.PathSeparator))[depth:], "/")
filtered = appendIfMissing(filtered, name)
}
if len(filtered) == 1 {
return filtered[0], nil
}
if len(filtered) > 1 {
message := "multiple conflicting time zone configurations found:\n"
for _, v := range filtered {
message += fmt.Sprintf("%s\n", v)
}
message += "Fix the configuration, or set the time zone in a TZ environment variable"
return "", errors.New(message)
}
return "", nil
}
func appendIfMissing(slice []string, s string) []string {
for _, item := range slice {
if item == s {
return slice
}
}
return append(slice, s)
}
// fileExists checks if a file or directory exist.
func fileExists(fp string) bool {
_, err := os.Stat(fp)
return err == nil || os.IsExist(err)
}