Skip to content

Commit

Permalink
*: fix wrong timezone when env TZ is not set (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoppro authored Apr 28, 2020
1 parent 3df52d5 commit b54e3d1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cdc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var defaultServerOptions = options{
pdEndpoints: "http://127.0.0.1:2379",
statusHost: "127.0.0.1",
statusPort: defaultStatusPort,
timezone: time.Local,
timezone: nil,
gcTTL: DefaultCDCGCSafePointTTL,
}

Expand Down
1 change: 0 additions & 1 deletion cdc/sink/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ var defaultParams = params{
}

func configureSinkURI(dsnCfg *dmysql.Config, tz *time.Location) (string, error) {
dsnCfg.Loc = tz
if dsnCfg.Params == nil {
dsnCfg.Params = make(map[string]string, 1)
}
Expand Down
20 changes: 11 additions & 9 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strconv"
"strings"
"syscall"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/log"
Expand Down Expand Up @@ -54,15 +53,18 @@ func runEServer(cmd *cobra.Command, args []string) error {
return errors.Annotatef(err, "invalid status address: %s", statusAddr)
}

var opts []cdc.ServerOption
opts = append(opts, cdc.PDEndpoints(serverPdAddr), cdc.StatusHost(addrs[0]), cdc.StatusPort(int(statusPort)), cdc.GCTTL(gcTTL))
if strings.ToLower(timezone) != "system" {
tz, err := time.LoadLocation(timezone)
if err != nil {
return errors.Annotate(err, "can not load timezone")
}
opts = append(opts, cdc.Timezone(tz))
tz, err := util.GetTimezone(timezone)
if err != nil {
return errors.Annotate(err, "can not load timezone")
}

opts := []cdc.ServerOption{
cdc.PDEndpoints(serverPdAddr),
cdc.StatusHost(addrs[0]),
cdc.StatusPort(int(statusPort)),
cdc.GCTTL(gcTTL),
cdc.Timezone(tz)}

server, err := cdc.NewServer(opts...)
if err != nil {
return errors.Annotate(err, "new server")
Expand Down
34 changes: 34 additions & 0 deletions pkg/util/tz.go
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)
}

0 comments on commit b54e3d1

Please sign in to comment.