Skip to content

Commit 2a811fc

Browse files
committed
apply suggested optimization
1 parent 2993d6e commit 2a811fc

3 files changed

Lines changed: 125 additions & 14 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.vscode
22
.idea
33
.DS_Store
4+
tzdata/output.tar.gz

‎tzlocal/tz_unix.go‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,36 @@ func inferFromPath(p string) (string, error) {
2323
return "", fmt.Errorf("cannot infer timezone name from path: %q", p)
2424
}
2525

26-
// LocalTZ will run `/etc/localtime` and get the timezone from the resulting value `/usr/share/zoneinfo/America/New_York`
27-
func LocalTZ() (string, error) {
28-
var name string
29-
fi, err := os.Lstat(localZoneFile)
26+
func localTZ(localZoneFile string) (string, error) {
27+
target, err := os.Readlink(localZoneFile)
3028
if err != nil {
31-
err = fmt.Errorf("failed to stat %q: %w", localZoneFile, err)
32-
return name, err
29+
fi, statErr := os.Lstat(localZoneFile)
30+
if statErr != nil {
31+
return "", fmt.Errorf("failed to stat %q: %w", localZoneFile, statErr)
32+
}
33+
if fi.Mode()&os.ModeSymlink == 0 {
34+
return "", fmt.Errorf("%q is not a symlink - cannot infer name", localZoneFile)
35+
}
36+
return "", err
3337
}
3438

35-
if (fi.Mode() & os.ModeSymlink) == 0 {
36-
err = fmt.Errorf("%q is not a symlink - cannot infer name", localZoneFile)
37-
return name, err
39+
if !filepath.IsAbs(target) {
40+
dir, err := filepath.EvalSymlinks(filepath.Dir(localZoneFile))
41+
if err != nil {
42+
return "", err
43+
}
44+
target = filepath.Join(dir, target)
3845
}
3946

40-
p, err := filepath.EvalSymlinks(localZoneFile)
47+
p, err := filepath.EvalSymlinks(target)
4148
if err != nil {
42-
return name, err
49+
return "", err
4350
}
4451

45-
// handles 1 & 2 part zone names
46-
name, err = inferFromPath(p)
47-
return name, err
52+
return inferFromPath(p)
53+
}
54+
55+
// LocalTZ gets the timezone name by resolving the /etc/localtime symlink.
56+
func LocalTZ() (string, error) {
57+
return localTZ(localZoneFile)
4858
}

‎tzlocal/tz_unix_test.go‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
package tzlocal
55

66
import (
7+
"os"
8+
"path/filepath"
9+
"strings"
710
"testing"
811
"time"
912
)
@@ -68,6 +71,103 @@ func Test_inferFromPath(t *testing.T) {
6871
}
6972
}
7073

74+
func TestLocalTZFromPath(t *testing.T) {
75+
t.Run("absolute target", func(t *testing.T) {
76+
root := t.TempDir()
77+
target := createZoneinfoFile(t, root, "Europe/Tallinn")
78+
localtime := filepath.Join(root, "etc", "localtime")
79+
mustMkdirAll(t, filepath.Dir(localtime))
80+
mustSymlink(t, target, localtime)
81+
82+
assertLocalTZ(t, localtime, "Europe/Tallinn")
83+
})
84+
85+
t.Run("relative target through symlinked parent", func(t *testing.T) {
86+
root := t.TempDir()
87+
realRoot := filepath.Join(root, "real")
88+
createZoneinfoFile(t, realRoot, "Europe/Berlin")
89+
realEtc := filepath.Join(realRoot, "etc")
90+
mustMkdirAll(t, realEtc)
91+
mustSymlink(t,
92+
filepath.Join("..", "usr", "share", "zoneinfo", "Europe", "Berlin"),
93+
filepath.Join(realEtc, "localtime"),
94+
)
95+
mustSymlink(t, realEtc, filepath.Join(root, "etc"))
96+
97+
assertLocalTZ(t, filepath.Join(root, "etc", "localtime"), "Europe/Berlin")
98+
})
99+
100+
t.Run("regular file through symlinked parent", func(t *testing.T) {
101+
root := t.TempDir()
102+
target := createZoneinfoFile(t, root, "localtime")
103+
mustSymlink(t, filepath.Dir(target), filepath.Join(root, "etc"))
104+
105+
_, err := localTZ(filepath.Join(root, "etc", "localtime"))
106+
if err == nil || !strings.Contains(err.Error(), "is not a symlink") {
107+
t.Fatalf("localTZ() error = %v, want a not-a-symlink error", err)
108+
}
109+
})
110+
111+
t.Run("missing localtime", func(t *testing.T) {
112+
localtime := filepath.Join(t.TempDir(), "etc", "localtime")
113+
114+
_, err := localTZ(localtime)
115+
if err == nil || !strings.Contains(err.Error(), "failed to stat") {
116+
t.Fatalf("localTZ() error = %v, want a failed-to-stat error", err)
117+
}
118+
})
119+
120+
t.Run("dangling symlink", func(t *testing.T) {
121+
root := t.TempDir()
122+
localtime := filepath.Join(root, "etc", "localtime")
123+
mustMkdirAll(t, filepath.Dir(localtime))
124+
mustSymlink(t, filepath.Join(root, "usr", "share", "zoneinfo", "Missing"), localtime)
125+
126+
_, err := localTZ(localtime)
127+
if err == nil {
128+
t.Fatal("localTZ() error = nil, want an error for a dangling symlink")
129+
}
130+
if strings.Contains(err.Error(), "failed to stat") {
131+
t.Fatalf("localTZ() error = %v, final symlink exists and should not fail its stat check", err)
132+
}
133+
})
134+
}
135+
136+
func createZoneinfoFile(t *testing.T, root, name string) string {
137+
t.Helper()
138+
p := filepath.Join(root, "usr", "share", "zoneinfo", filepath.FromSlash(name))
139+
mustMkdirAll(t, filepath.Dir(p))
140+
if err := os.WriteFile(p, nil, 0o644); err != nil {
141+
t.Fatalf("os.WriteFile(%q) failed: %v", p, err)
142+
}
143+
return p
144+
}
145+
146+
func mustMkdirAll(t *testing.T, path string) {
147+
t.Helper()
148+
if err := os.MkdirAll(path, 0o755); err != nil {
149+
t.Fatalf("os.MkdirAll(%q) failed: %v", path, err)
150+
}
151+
}
152+
153+
func mustSymlink(t *testing.T, oldname, newname string) {
154+
t.Helper()
155+
if err := os.Symlink(oldname, newname); err != nil {
156+
t.Fatalf("os.Symlink(%q, %q) failed: %v", oldname, newname, err)
157+
}
158+
}
159+
160+
func assertLocalTZ(t *testing.T, localtime, want string) {
161+
t.Helper()
162+
got, err := localTZ(localtime)
163+
if err != nil {
164+
t.Fatalf("localTZ() failed: %v", err)
165+
}
166+
if got != want {
167+
t.Fatalf("localTZ() = %q, want %q", got, want)
168+
}
169+
}
170+
71171
func TestRuntimeTZ(t *testing.T) {
72172
got, err := RuntimeTZ()
73173
if err != nil {

0 commit comments

Comments
 (0)