From 8917a15fe177abb82ca54fc75f019e5a48080655 Mon Sep 17 00:00:00 2001 From: Zhexuan Yang Date: Thu, 27 Sep 2018 09:43:18 +0800 Subject: [PATCH] util/timeutil: fix get tz at mojave (#7784) --- executor/executor_test.go | 1 + util/timeutil/time.go | 13 +++++++++++-- util/timeutil/time_test.go | 6 ++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/executor/executor_test.go b/executor/executor_test.go index d126c61a19fc2..d9ccc3b18cb5d 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -2369,6 +2369,7 @@ func (s *testSuite) TestTimezonePushDown(c *C) { systemTZ := timeutil.SystemLocation() c.Assert(systemTZ.String(), Not(Equals), "System") + c.Assert(systemTZ.String(), Not(Equals), "Local") ctx := context.Background() count := 0 ctx1 := context.WithValue(ctx, "CheckSelectRequestHook", func(req *kv.Request) { diff --git a/util/timeutil/time.go b/util/timeutil/time.go index 4aa212e232172..e3527238a1a23 100644 --- a/util/timeutil/time.go +++ b/util/timeutil/time.go @@ -46,7 +46,7 @@ var zoneSources = []string{ "/usr/share/lib/zoneinfo/", "/usr/lib/locale/TZ/", // this is for macOS - "/var/db/timezone/zoneinfo", + "/var/db/timezone/zoneinfo/", } // locCache is a simple map with lock. It stores all used timezone during the lifetime of tidb instance. @@ -93,6 +93,15 @@ func InferSystemTZ() string { 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 } @@ -149,7 +158,7 @@ func Zone(loc *time.Location) (string, int64) { _, offset := time.Now().In(loc).Zone() var name string name = loc.String() - // when we found name is "SystemLocation", we have no chice but push down + // when we found name is "System", we have no chice but push down // "System" to tikv side. if name == "Local" { name = "System" diff --git a/util/timeutil/time_test.go b/util/timeutil/time_test.go index e23e42f73503d..c354b889e14d3 100644 --- a/util/timeutil/time_test.go +++ b/util/timeutil/time_test.go @@ -34,6 +34,12 @@ 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") }