diff --git a/go/vt/vtgate/engine/fake_vcursor_test.go b/go/vt/vtgate/engine/fake_vcursor_test.go index 1ba0abfa2ef..46480d3998e 100644 --- a/go/vt/vtgate/engine/fake_vcursor_test.go +++ b/go/vt/vtgate/engine/fake_vcursor_test.go @@ -140,7 +140,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 5a253799b7f..fc18efa80aa 100644 --- a/go/vt/vtgate/evalengine/fn_time.go +++ b/go/vt/vtgate/evalengine/fn_time.go @@ -242,7 +242,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 @@ -687,7 +687,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 45fff46f629..9828d0a77d4 100644 --- a/go/vt/vtgate/safe_session.go +++ b/go/vt/vtgate/safe_session.go @@ -26,7 +26,7 @@ import ( "google.golang.org/protobuf/proto" "vitess.io/vitess/go/mysql/datetime" - + "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/srvtopo" "vitess.io/vitess/go/vt/sysvars" @@ -563,13 +563,22 @@ func (session *SafeSession) HasSystemVariables() (found bool) { func (session *SafeSession) TimeZone() *time.Location { session.mu.Lock() - tz, ok := session.SystemVariables["time_zone"] + zoneSQL, ok := session.SystemVariables["time_zone"] session.mu.Unlock() if !ok { - return nil + return time.Local + } + + tz, err := sqltypes.DecodeStringSQL(zoneSQL) + if err != nil { + return time.Local + } + + loc, err := datetime.ParseTimeZone(tz) + if err != nil { + return time.Local } - loc, _ := datetime.ParseTimeZone(tz) return loc } diff --git a/go/vt/vtgate/safe_session_test.go b/go/vt/vtgate/safe_session_test.go index 21bb2d6697a..f3a78d7f497 100644 --- a/go/vt/vtgate/safe_session_test.go +++ b/go/vt/vtgate/safe_session_test.go @@ -73,25 +73,31 @@ func TestTimeZone(t *testing.T) { want string }{ { - tz: "Europe/Amsterdam", + tz: "", + want: time.Local.String(), + }, + { + tz: "'Europe/Amsterdam'", want: "Europe/Amsterdam", }, { - tz: "+02:00", + tz: "'+02:00'", want: "UTC+02:00", }, { 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())