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

close pingcap/tidb#39719
This commit is contained in:
YangKeao
2022-12-08 06:52:05 -05:00
committed by GitHub
parent 4b98439f33
commit 754e73ad61
2 changed files with 22 additions and 0 deletions

View File

@ -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
}

View File

@ -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)
}