Skip to content

Commit fd84b47

Browse files
committed
unix: simplify inferFromPath
Refactor inferFromPath: use only string manipulation for zero allocations. '/' is now hard-coded as the file path separator, but this is a fine assumption for this purely non-Windows implementation of TZLocal.
1 parent 9af4692 commit fd84b47

1 file changed

Lines changed: 5 additions & 14 deletions

File tree

‎tzlocal/tz_unix.go‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,14 @@ import (
1313
const localZoneFile = "/etc/localtime" // symlinked file - set by OS
1414

1515
func inferFromPath(p string) (string, error) {
16-
var name string
17-
var err error
18-
19-
parts := strings.Split(p, string(filepath.Separator))
20-
for i := range parts {
21-
if parts[i] == "zoneinfo" || parts[i] == "zoneinfo.default" {
22-
parts = parts[i+1:]
23-
break
16+
for _, base := range []string{"/zoneinfo/", "/zoneinfo.default/"} {
17+
i := strings.LastIndex(p, base)
18+
if i >= 0 {
19+
return p[i+len(base):], nil
2420
}
2521
}
2622

27-
if len(parts) < 1 {
28-
err = fmt.Errorf("cannot infer timezone name from path: %q", p)
29-
return name, err
30-
}
31-
32-
return filepath.Join(parts...), nil
23+
return "", fmt.Errorf("cannot infer timezone name from path: %q", p)
3324
}
3425

3526
// LocalTZ will run `/etc/localtime` and get the timezone from the resulting value `/usr/share/zoneinfo/America/New_York`

0 commit comments

Comments
 (0)