Skip to content

Commit

Permalink
Cherry-pick 5468f5d
Browse files Browse the repository at this point in the history
Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
vitess-bot[bot] authored and dbussink committed Jan 16, 2025
1 parent d9fed2f commit 551ddf9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go/vt/vtgate/engine/fake_vcursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/evalengine/fn_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
17 changes: 13 additions & 4 deletions go/vt/vtgate/safe_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
18 changes: 12 additions & 6 deletions go/vt/vtgate/safe_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 551ddf9

Please sign in to comment.