Skip to content

Commit

Permalink
get tz name from zoneinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
zhexuany committed Sep 7, 2018
1 parent b2bfd8f commit ec6a822
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
16 changes: 15 additions & 1 deletion executor/distsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ func closeAll(objs ...Closeable) error {
return errors.Trace(err)
}

func getTZNameFromFileName(path string) (string, error) {
// phase1 only support read /etc/localtime which is a softlink to zoneinfo file
substr := "share/zoneinfo"
if strings.Contains(path, substr) {
idx := strings.Index(path, substr)
return string(path[idx+len(substr)+1:]), nil
}
return "", errors.New("only support softlink has share/zoneinfo as a suffix" + path)
}

// zone returns the current timezone name and timezone offset in seconds.
// In compatible with MySQL, we change `Local` to `System`.
// TODO: Golang team plan to return system timezone name intead of
Expand All @@ -127,7 +137,11 @@ func zone(sctx sessionctx.Context) (string, int64) {
var name string
name = loc.String()
if name == "Local" {
name = "System"
path, err := filepath.EvalSymlinks("/etc/localtime")
if err != nil {
return "Sysytem", int64(offset)
}
return getTZNameFromFileName(path), int64(offset)
}

return name, int64(offset)
Expand Down
6 changes: 6 additions & 0 deletions executor/distsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,9 @@ func (s *testSuite) TestUniqueKeyNullValueSelect(c *C) {
res = tk.MustQuery("select * from t where id is null;")
res.Check(testkit.Rows("<nil> a", "<nil> b", "<nil> c"))
}

func (s *testSuite) TestgetTZNameFromFileName(c *C) {
tz, err := getTZNameFromFileName("/user/share/zoneinfo/Asia/Shanghai")
c.Assert(err, IsNil)
c.Assert(tz, Equals, "Asia/Shanghai")
}
15 changes: 15 additions & 0 deletions store/mockstore/mocktikv/cop_handler_dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bytes"
"fmt"
"io"
"strings"
"sync"
"time"

Expand All @@ -41,6 +42,7 @@ import (
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"path/filepath"
)

var dummySlice = make([]byte, 0)
Expand Down Expand Up @@ -164,13 +166,26 @@ func (h *rpcHandler) buildDAGExecutor(req *coprocessor.Request) (*dagContext, ex
return ctx, e, dagReq, err
}

func getTZNameFromFileName(path string) (string, error) {
// phase1 only support read /etc/localtime which is a softlink to zoneinfo file
substr := "share/zoneinfo"
if strings.Contains(path, substr) {
idx := strings.Index(path, substr)
return string(path[idx+len(substr)+1:]), nil
}
return "", errors.New("only support softlink has share/zoneinfo as a suffix" + path)
}

// constructTimeZone constructs timezone by name first. When the timezone name
// is set, the daylight saving problem must be considered. Otherwise the
// timezone offset in seconds east of UTC is used to constructed the timezone.
func constructTimeZone(name string, offset int) (*time.Location, error) {
if name != "" {
// no need to care about case since name is retreved
// from go std library call
return LocCache.getLoc(name)
}

return time.FixedZone("", offset), nil
}

Expand Down

0 comments on commit ec6a822

Please sign in to comment.