Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timeutil: do not use system files to verify timezone #13593

Merged
merged 7 commits into from
Nov 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions util/timeutil/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package timeutil

import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
Expand All @@ -41,13 +40,6 @@ var locCa *locCache

// systemTZ is current TiDB's system timezone name.
var systemTZ string
var zoneSources = []string{
"/usr/share/zoneinfo/",
"/usr/share/lib/zoneinfo/",
"/usr/lib/locale/TZ/",
// this is for macOS
"/var/db/timezone/zoneinfo/",
}

// locCache is a simple map with lock. It stores all used timezone during the lifetime of tidb instance.
// Talked with Golang team about whether they can have some forms of cache policy available for programmer,
Expand All @@ -60,7 +52,7 @@ type locCache struct {
}

// InferSystemTZ reads system timezone from `TZ`, the path of the soft link of `/etc/localtime`. If both of them are failed, system timezone will be set to `UTC`.
// It is exported because we need to use it during bootstap stage. And it should be only used at that stage.
// It is exported because we need to use it during bootstrap stage. And it should be only used at that stage.
func InferSystemTZ() string {
// consult $TZ to find the time zone to use.
// no $TZ means use the system default /etc/localtime.
Expand All @@ -79,10 +71,9 @@ func InferSystemTZ() string {
}
logutil.BgLogger().Error("locate timezone files failed", zap.Error(err1))
case tz != "" && tz != "UTC":
for _, source := range zoneSources {
if _, err := os.Stat(source + tz); err == nil {
return tz
}
_, err := time.LoadLocation(tz)
if err == nil {
return tz
}
}
return "UTC"
Expand Down Expand Up @@ -161,8 +152,8 @@ func LoadLocation(name string) (*time.Location, error) {
func Zone(loc *time.Location) (string, int64) {
_, offset := time.Now().In(loc).Zone()
name := loc.String()
// when we found name is "System", we have no chice but push down
// "System" to tikv side.
// when we found name is "System", we have no choice but push down
// "System" to TiKV side.
if name == "Local" {
name = "System"
}
Expand Down