diff --git a/sessionctx/variable/variable.go b/sessionctx/variable/variable.go index 2792e373cdda1..b47036bfdd6d1 100644 --- a/sessionctx/variable/variable.go +++ b/sessionctx/variable/variable.go @@ -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 } diff --git a/sessionctx/variable/variable_test.go b/sessionctx/variable/variable_test.go index 023cc75e7cba7..b346f5f609e46 100644 --- a/sessionctx/variable/variable_test.go +++ b/sessionctx/variable/variable_test.go @@ -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) +}