Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions go/mysql/datetime/time_zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2019 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package datetime
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

license missing


import (
"fmt"
"strconv"
"time"

vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)

func unknownTimeZone(tz string) error {
return vterrors.NewErrorf(vtrpcpb.Code_INVALID_ARGUMENT, vterrors.UnknownTimeZone, "Unknown or incorrect time zone: '%s'", tz)
}

func ParseTimeZone(tz string) (*time.Location, error) {
// Needs to be checked first since time.LoadLocation("") returns UTC.
if tz == "" {
return nil, unknownTimeZone(tz)
}
loc, err := time.LoadLocation(tz)
if err == nil {
return loc, nil
}

// MySQL also handles timezone formats in the form of the
// offset from UTC, so we'll try that if the above fails.
// This format is always something in the form of +HH:MM or -HH:MM.
if len(tz) != 6 {
return nil, unknownTimeZone(tz)
}
if tz[0] != '+' && tz[0] != '-' {
return nil, unknownTimeZone(tz)
}
if tz[3] != ':' {
return nil, unknownTimeZone(tz)
}
neg := tz[0] == '-'
hours, err := strconv.ParseUint(tz[1:3], 10, 4)
if err != nil {
return nil, unknownTimeZone(tz)
}
minutes, err := strconv.ParseUint(tz[4:], 10, 6)
if err != nil {
return nil, unknownTimeZone(tz)
}
if minutes > 59 {
return nil, unknownTimeZone(tz)
}

// MySQL only supports timezones in the range of -13:59 to +14:00.
if neg && hours > 13 {
return nil, unknownTimeZone(tz)
}
if !neg && (hours > 14 || hours == 14 && minutes > 0) {
return nil, unknownTimeZone(tz)
}
offset := int(hours)*60*60 + int(minutes)*60
if neg {
offset = -offset
}
return time.FixedZone(fmt.Sprintf("UTC%s", tz), offset), nil
}
77 changes: 77 additions & 0 deletions go/mysql/datetime/time_zone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2019 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package datetime
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

license missing


import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseTimeZone(t *testing.T) {
testCases := []struct {
tz string
want string
}{
{
tz: "Europe/Amsterdam",
want: "Europe/Amsterdam",
},
{
tz: "",
want: "Unknown or incorrect time zone: ''",
},
{
tz: "+02:00",
want: "UTC+02:00",
},
{
tz: "+14:00",
want: "UTC+14:00",
},
{
tz: "+14:01",
want: "Unknown or incorrect time zone: '+14:01'",
},
{
tz: "-13:59",
want: "UTC-13:59",
},
{
tz: "-14:00",
want: "Unknown or incorrect time zone: '-14:00'",
},
{
tz: "-15:00",
want: "Unknown or incorrect time zone: '-15:00'",
},
{
tz: "foo",
want: "Unknown or incorrect time zone: 'foo'",
},
}

for _, tc := range testCases {

zone, err := ParseTimeZone(tc.tz)
if err != nil {
assert.Equal(t, tc.want, err.Error())
} else {
assert.Equal(t, tc.want, zone.String())
}
}
}
1 change: 1 addition & 0 deletions go/mysql/sql_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ var stateToMysqlCode = map[vterrors.State]mysqlCode{
vterrors.NoSuchSession: {num: ERUnknownComError, state: SSNetError},
vterrors.OperandColumns: {num: EROperandColumns, state: SSWrongNumberOfColumns},
vterrors.WrongValueCountOnRow: {num: ERWrongValueCountOnRow, state: SSWrongValueCountOnRow},
vterrors.UnknownTimeZone: {num: ERUnknownTimeZone, state: SSUnknownSQLState},
}

func getStateToMySQLState(state vterrors.State) mysqlCode {
Expand Down
3 changes: 3 additions & 0 deletions go/vt/vterrors/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ const (
// server not available
ServerNotAvailable

// unknown timezone
UnknownTimeZone

// No state should be added below NumOfStates
NumOfStates
)
Expand Down
6 changes: 4 additions & 2 deletions go/vt/vtgate/safe_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (

"google.golang.org/protobuf/proto"

"vitess.io/vitess/go/mysql/datetime"

"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/srvtopo"
"vitess.io/vitess/go/vt/sysvars"
Expand Down Expand Up @@ -553,9 +555,9 @@ func (session *SafeSession) TimeZone() *time.Location {
session.mu.Unlock()

if !ok {
return time.Local
return nil
}
loc, _ := time.LoadLocation(tz)
loc, _ := datetime.ParseTimeZone(tz)
return loc
}

Expand Down
34 changes: 34 additions & 0 deletions go/vt/vtgate/safe_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package vtgate
import (
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

querypb "vitess.io/vitess/go/vt/proto/query"
Expand Down Expand Up @@ -64,3 +66,35 @@ func TestPrequeries(t *testing.T) {
t.Errorf("got %v but wanted %v", preQueries, want)
}
}

func TestTimeZone(t *testing.T) {
testCases := []struct {
tz string
want string
}{
{
tz: "Europe/Amsterdam",
want: "Europe/Amsterdam",
},
{
tz: "+02:00",
want: "UTC+02:00",
},
{
tz: "foo",
want: (*time.Location)(nil).String(),
},
}

for _, tc := range testCases {
t.Run(tc.tz, func(t *testing.T) {
session := NewSafeSession(&vtgatepb.Session{
SystemVariables: map[string]string{
"time_zone": tc.tz,
},
})

assert.Equal(t, tc.want, session.TimeZone().String())
})
}
}