Skip to content

Commit

Permalink
variable: fix outdated time shift for variable with TypeTime (#39720)
Browse files Browse the repository at this point in the history
close #39719
  • Loading branch information
YangKeao authored Dec 8, 2022
1 parent 4b98439 commit 754e73a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sessionctx/variable/variable.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
}

0 comments on commit 754e73a

Please sign in to comment.