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

variable: fix outdated time shift for variable with TypeTime #39720

Merged
merged 6 commits into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions sessionctx/variable/variable.go
Expand Up @@ -383,6 +383,10 @@ func (sv *SysVar) checkTimeSystemVar(value string, vars *SessionVars) (string, e
if err != nil {
return "", err
}
// Add a modern date to it, as the timezone shift can differ across the history
// For example, the Asia/Shanghai refers to +08:05 before 1900
now := time.Now()
t = time.Date(now.Year(), now.Month(), now.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
return t.Format(FullDayTimeFormat), nil
}

Expand Down
18 changes: 18 additions & 0 deletions sessionctx/variable/variable_test.go
Expand Up @@ -669,3 +669,21 @@ func TestSkipSysvarCache(t *testing.T) {
require.True(t, GetSysVar(TiDBGCScanLockMode).SkipSysvarCache())
require.False(t, GetSysVar(TiDBEnableAsyncCommit).SkipSysvarCache())
}

func TestTimeValidationWithTimezone(t *testing.T) {
sv := SysVar{Scope: ScopeSession, Name: "mynewsysvar", Value: "23:59 +0000", Type: TypeTime}
vars := NewSessionVars(nil)

// In timezone UTC
vars.TimeZone = time.UTC
val, err := sv.Validate(vars, "23:59", ScopeSession)
require.NoError(t, err)
require.Equal(t, "23:59 +0000", val)

// In timezone Asia/Shanghai
vars.TimeZone, err = time.LoadLocation("Asia/Shanghai")
require.NoError(t, err)
val, err = sv.Validate(vars, "23:59", ScopeSession)
require.NoError(t, err)
require.Equal(t, "23:59 +0800", val)
}