From b9cf6379e2d86857efef85db00b01bfc66c9c4e2 Mon Sep 17 00:00:00 2001 From: "vitess-bot[bot]" <108069721+vitess-bot[bot]@users.noreply.github.com> Date: Thu, 16 Jan 2025 07:15:34 +0100 Subject: [PATCH] Cherry-pick 5468f5db7db117f955aaf9c56182f05fc496c14b Signed-off-by: Dirkjan Bussink --- go/vt/vtgate/engine/fake_vcursor_test.go | 2 +- go/vt/vtgate/evalengine/fn_time.go | 4 ++-- go/vt/vtgate/safe_session.go | 11 ++++++----- go/vt/vtgate/safe_session_test.go | 14 ++++++++++---- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/go/vt/vtgate/engine/fake_vcursor_test.go b/go/vt/vtgate/engine/fake_vcursor_test.go index 9ba4fdc6a6b..4c7ed3b3689 100644 --- a/go/vt/vtgate/engine/fake_vcursor_test.go +++ b/go/vt/vtgate/engine/fake_vcursor_test.go @@ -164,7 +164,7 @@ func (t *noopVCursor) Environment() *vtenv.Environment { } func (t *noopVCursor) TimeZone() *time.Location { - return nil + return time.Local } func (t *noopVCursor) SQLMode() string { diff --git a/go/vt/vtgate/evalengine/fn_time.go b/go/vt/vtgate/evalengine/fn_time.go index 2d5e12f518d..322b89faafb 100644 --- a/go/vt/vtgate/evalengine/fn_time.go +++ b/go/vt/vtgate/evalengine/fn_time.go @@ -256,7 +256,7 @@ func (call *builtinNow) constant() bool { func (call *builtinSysdate) eval(env *ExpressionEnv) (eval, error) { now := SystemTime() - if tz := env.currentTimezone(); tz != nil { + if tz := env.currentTimezone(); tz != time.Local { now = now.In(tz) } return newEvalDateTime(datetime.NewDateTimeFromStd(now), int(call.prec), false), nil @@ -701,7 +701,7 @@ func (b *builtinFromUnixtime) eval(env *ExpressionEnv) (eval, error) { } t := time.Unix(sec, frac) - if tz := env.currentTimezone(); tz != nil { + if tz := env.currentTimezone(); tz != time.Local { t = t.In(tz) } diff --git a/go/vt/vtgate/safe_session.go b/go/vt/vtgate/safe_session.go index 1d57c63ef35..dda4005774c 100644 --- a/go/vt/vtgate/safe_session.go +++ b/go/vt/vtgate/safe_session.go @@ -569,17 +569,18 @@ func (session *SafeSession) TimeZone() *time.Location { session.mu.Unlock() if !ok { - return nil + return time.Local } tz, err := sqltypes.DecodeStringSQL(zoneSQL) if err != nil { - return nil + return time.Local } - loc, _ := datetime.ParseTimeZone(tz) - // it's safe to ignore the error - if we get an error, loc will be nil, - // and this is exactly the behaviour we want anyway + loc, err := datetime.ParseTimeZone(tz) + if err != nil { + return time.Local + } return loc } diff --git a/go/vt/vtgate/safe_session_test.go b/go/vt/vtgate/safe_session_test.go index ce681fe7fd3..f3a78d7f497 100644 --- a/go/vt/vtgate/safe_session_test.go +++ b/go/vt/vtgate/safe_session_test.go @@ -72,6 +72,10 @@ func TestTimeZone(t *testing.T) { tz string want string }{ + { + tz: "", + want: time.Local.String(), + }, { tz: "'Europe/Amsterdam'", want: "Europe/Amsterdam", @@ -82,16 +86,18 @@ func TestTimeZone(t *testing.T) { }, { tz: "foo", - want: (*time.Location)(nil).String(), + want: time.Local.String(), }, } for _, tc := range testCases { t.Run(tc.tz, func(t *testing.T) { + sysvars := map[string]string{} + if tc.tz != "" { + sysvars["time_zone"] = tc.tz + } session := NewSafeSession(&vtgatepb.Session{ - SystemVariables: map[string]string{ - "time_zone": tc.tz, - }, + SystemVariables: sysvars, }) assert.Equal(t, tc.want, session.TimeZone().String())