-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: fix wrong timezone when env
TZ
is not set (#512)
- Loading branch information
leoppro
authored
Apr 28, 2020
1 parent
3df52d5
commit b54e3d1
Showing
4 changed files
with
46 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package util | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// GetTimezone returns the timezone specified by the name | ||
func GetTimezone(name string) (*time.Location, error) { | ||
switch strings.ToLower(name) { | ||
case "", "system", "local": | ||
return GetLocalTimezone() | ||
default: | ||
return time.LoadLocation(name) | ||
} | ||
} | ||
|
||
// GetLocalTimezone returns the timezone in local system | ||
func GetLocalTimezone() (*time.Location, error) { | ||
if time.Local.String() != "Local" { | ||
return time.Local, nil | ||
} | ||
str, err := os.Readlink("/etc/localtime") | ||
if err != nil { | ||
return nil, err | ||
} | ||
// the linked path of `/etc/localtime` | ||
// MacOS: /var/db/timezone/zoneinfo/Asia/Shanghai | ||
// Linux: /etc/usr/share/zoneinfo/Asia/Shanghai | ||
tzName := path.Join(path.Base(path.Dir(str)), path.Base(str)) | ||
return time.LoadLocation(tzName) | ||
} |