Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
Signed-off-by: Shuaipeng Yu <[email protected]>
  • Loading branch information
jackysp committed Nov 22, 2019
1 parent 3400584 commit c9e7a7d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 61 deletions.
54 changes: 5 additions & 49 deletions util/timeutil/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@ package timeutil

import (
"fmt"
"path/filepath"
"strings"
"sync"
"syscall"
"time"

"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)

// init initializes `locCache`.
Expand Down Expand Up @@ -54,51 +48,13 @@ type locCache struct {
// InferSystemTZ reads system timezone from `TZ`, the path of the soft link of `/etc/localtime`. If both of them are failed, system timezone will be set to `UTC`.
// It is exported because we need to use it during bootstrap stage. And it should be only used at that stage.
func InferSystemTZ() string {
// consult $TZ to find the time zone to use.
// no $TZ means use the system default /etc/localtime.
// $TZ="" means use UTC.
// $TZ="foo" means use /usr/share/zoneinfo/foo.
tz, ok := syscall.Getenv("TZ")
switch {
case !ok:
path, err1 := filepath.EvalSymlinks("/etc/localtime")
if err1 == nil {
name, err2 := inferTZNameFromFileName(path)
if err2 == nil {
return name
}
logutil.BgLogger().Error("infer timezone failed", zap.Error(err2))
}
logutil.BgLogger().Error("locate timezone files failed", zap.Error(err1))
case tz != "" && tz != "UTC":
_, err := time.LoadLocation(tz)
if err == nil {
return tz
}
tz := time.Local.String()
if tz != "Local" {
return tz
}
return "UTC"
}

// inferTZNameFromFileName gets IANA timezone name from zoneinfo path.
// TODO: It will be refined later. This is just a quick fix.
func inferTZNameFromFileName(path string) (string, error) {
// phase1 only support read /etc/localtime which is a softlink to zoneinfo file
substr := "zoneinfo"
// macOs MoJave changes the sofe link of /etc/localtime from
// "/var/db/timezone/tz/2018e.1.0/zoneinfo/Asia/Shanghai"
// to "/usr/share/zoneinfo.default/Asia/Shanghai"
substrMojave := "zoneinfo.default"

if idx := strings.Index(path, substrMojave); idx != -1 {
return string(path[idx+len(substrMojave)+1:]), nil
}

if idx := strings.Index(path, substr); idx != -1 {
return string(path[idx+len(substr)+1:]), nil
}
return "", fmt.Errorf("path %s is not supported", path)
}

// SystemLocation returns time.SystemLocation's IANA timezone location. It is TiDB's global timezone location.
func SystemLocation() *time.Location {
loc, err := LoadLocation(systemTZ)
Expand Down Expand Up @@ -152,8 +108,8 @@ func LoadLocation(name string) (*time.Location, error) {
func Zone(loc *time.Location) (string, int64) {
_, offset := time.Now().In(loc).Zone()
name := loc.String()
// when we found name is "System", we have no chice but push down
// "System" to tikv side.
// when we found name is "System", we have no choice but push down
// "System" to TiKV side.
if name == "Local" {
name = "System"
}
Expand Down
12 changes: 0 additions & 12 deletions util/timeutil/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@ func TestT(t *testing.T) {

type testTimeSuite struct{}

func (s *testTimeSuite) TestgetTZNameFromFileName(c *C) {
tz, err := inferTZNameFromFileName("/user/share/zoneinfo/Asia/Shanghai")

c.Assert(err, IsNil)
c.Assert(tz, Equals, "Asia/Shanghai")

tz, err = inferTZNameFromFileName("/usr/share/zoneinfo.default/Asia/Shanghai")

c.Assert(err, IsNil)
c.Assert(tz, Equals, "Asia/Shanghai")
}

func (s *testTimeSuite) TestLocal(c *C) {
os.Setenv("TZ", "Asia/Shanghai")
systemTZ = InferSystemTZ()
Expand Down

0 comments on commit c9e7a7d

Please sign in to comment.