diff --git a/cdc/server.go b/cdc/server.go index 4b6aeb034c5..87bc969e270 100644 --- a/cdc/server.go +++ b/cdc/server.go @@ -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, } diff --git a/cdc/sink/mysql.go b/cdc/sink/mysql.go index c02dc8f17a2..0940d151b6c 100644 --- a/cdc/sink/mysql.go +++ b/cdc/sink/mysql.go @@ -265,7 +265,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) } diff --git a/cmd/server.go b/cmd/server.go index afd69ed603b..eb859f60877 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -7,7 +7,6 @@ import ( "strconv" "strings" "syscall" - "time" "github.com/pingcap/errors" "github.com/pingcap/log" @@ -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") diff --git a/pkg/util/tz.go b/pkg/util/tz.go new file mode 100644 index 00000000000..c9a6ff11191 --- /dev/null +++ b/pkg/util/tz.go @@ -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) +}