Skip to content

Commit

Permalink
sessionctx: add time zone check (#9822) (#10000)
Browse files Browse the repository at this point in the history
  • Loading branch information
WangXiangUSTC authored and zz-jason committed Apr 2, 2019
1 parent 9436866 commit ebc9904
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 11 additions & 0 deletions sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,20 @@ func parseTimeZone(s string) (*time.Location, error) {
}

// The value can be given as a string indicating an offset from UTC, such as '+10:00' or '-6:00'.
// The time zone's value should in [-12:59,+14:00].
if strings.HasPrefix(s, "+") || strings.HasPrefix(s, "-") {
d, err := types.ParseDuration(nil, s[1:], 0)
if err == nil {
if s[0] == '-' {
if d.Duration > 12*time.Hour+59*time.Minute {
return nil, ErrUnknownTimeZone.GenWithStackByArgs(s)
}
} else {
if d.Duration > 14*time.Hour {
return nil, ErrUnknownTimeZone.GenWithStackByArgs(s)
}
}

ofst := int(d.Duration / time.Second)
if s[0] == '-' {
ofst = -ofst
Expand Down
18 changes: 14 additions & 4 deletions sessionctx/variable/varsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,26 @@ func (s *testVarsutilSuite) TestVarsutil(c *C) {
expect string
compareValue bool
diff time.Duration
err error
}{
{"Europe/Helsinki", "Europe/Helsinki", true, -2 * time.Hour},
{"US/Eastern", "US/Eastern", true, 5 * time.Hour},
{"Europe/Helsinki", "Europe/Helsinki", true, -2 * time.Hour, nil},
{"US/Eastern", "US/Eastern", true, 5 * time.Hour, nil},
//TODO: Check it out and reopen this case.
//{"SYSTEM", "Local", false, 0},
{"+10:00", "", true, -10 * time.Hour},
{"-6:00", "", true, 6 * time.Hour},
{"+10:00", "", true, -10 * time.Hour, nil},
{"-6:00", "", true, 6 * time.Hour, nil},
{"+14:00", "", true, -14 * time.Hour, nil},
{"-12:59", "", true, 12*time.Hour + 59*time.Minute, nil},
{"+14:01", "", false, -14 * time.Hour, ErrUnknownTimeZone.GenWithStackByArgs("+14:01")},
{"-13:00", "", false, 13 * time.Hour, ErrUnknownTimeZone.GenWithStackByArgs("-13:00")},
}
for _, tt := range tests {
err = SetSessionSystemVar(v, TimeZone, types.NewStringDatum(tt.input))
if tt.err != nil {
c.Assert(err, NotNil)
continue
}

c.Assert(err, IsNil)
c.Assert(v.TimeZone.String(), Equals, tt.expect)
if tt.compareValue {
Expand Down

0 comments on commit ebc9904

Please sign in to comment.