Skip to content

Commit

Permalink
util/timeutil: fix get tz at mojave (pingcap#7784)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhexuany authored and zz-jason committed Sep 27, 2018
1 parent 9fd4072 commit 8917a15
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 11 additions & 2 deletions util/timeutil/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions util/timeutil/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 8917a15

Please sign in to comment.