Skip to content

Commit

Permalink
builtin/time: Use mysql.Duration instead of mysql.Time and add length…
Browse files Browse the repository at this point in the history
… check
  • Loading branch information
chenyanzhe committed Dec 20, 2015
1 parent 8d938f3 commit 7822670
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 31 deletions.
22 changes: 7 additions & 15 deletions expression/builtin/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func convertToTime(arg interface{}, tp byte) (interface{}, error) {
return t, nil
}

func convertToDuration(arg interface{}) (interface{}, error) {
func convertToDuration(arg interface{}, fsp int) (interface{}, error) {
f := types.NewFieldType(mysql.TypeDuration)
f.Decimal = mysql.MaxFsp
f.Decimal = fsp

v, err := types.Convert(arg, f)
if err != nil {
Expand Down Expand Up @@ -79,7 +79,7 @@ func builtinDay(args []interface{}, ctx map[interface{}]interface{}) (interface{

// See http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_hour
func builtinHour(args []interface{}, ctx map[interface{}]interface{}) (interface{}, error) {
v, err := convertToDuration(args[0])
v, err := convertToDuration(args[0], mysql.MaxFsp)
if err != nil || types.IsNil(v) {
return v, err
}
Expand All @@ -91,7 +91,7 @@ func builtinHour(args []interface{}, ctx map[interface{}]interface{}) (interface

// See http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_minute
func builtinMinute(args []interface{}, ctx map[interface{}]interface{}) (interface{}, error) {
v, err := convertToDuration(args[0])
v, err := convertToDuration(args[0], mysql.MaxFsp)
if err != nil || types.IsNil(v) {
return v, err
}
Expand All @@ -103,7 +103,7 @@ func builtinMinute(args []interface{}, ctx map[interface{}]interface{}) (interfa

// See http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_second
func builtinSecond(args []interface{}, ctx map[interface{}]interface{}) (interface{}, error) {
v, err := convertToDuration(args[0])
v, err := convertToDuration(args[0], mysql.MaxFsp)
if err != nil || types.IsNil(v) {
return v, err
}
Expand All @@ -115,7 +115,7 @@ func builtinSecond(args []interface{}, ctx map[interface{}]interface{}) (interfa

// See http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_microsecond
func builtinMicroSecond(args []interface{}, ctx map[interface{}]interface{}) (interface{}, error) {
v, err := convertToDuration(args[0])
v, err := convertToDuration(args[0], mysql.MaxFsp)
if err != nil || types.IsNil(v) {
return v, err
}
Expand Down Expand Up @@ -318,15 +318,7 @@ func builtinCurrentTime(args []interface{}, ctx map[interface{}]interface{}) (in
return nil, errors.Trace(err)
}
}

t := mysql.Time{
Time: time.Now(),
Type: mysql.TypeDuration,
// set unspecified for later round
Fsp: mysql.UnspecifiedFsp,
}

return t.RoundFrac(int(fsp))
return convertToDuration(time.Now().Format("15:04:05.000000"), fsp)
}

func checkFsp(arg interface{}) (int, error) {
Expand Down
14 changes: 8 additions & 6 deletions expression/builtin/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,27 +277,29 @@ func (s *testBuiltinSuite) TestCurrentDate(c *C) {
}

func (s *testBuiltinSuite) TestCurrentTime(c *C) {
tfStr := "15:04:05"

last := time.Now()
v, err := builtinCurrentTime(nil, nil)
c.Assert(err, IsNil)
n, ok := v.(mysql.Time)
n, ok := v.(mysql.Duration)
c.Assert(ok, IsTrue)
c.Assert(n.String(), HasLen, 8)
c.Assert(n.String(), GreaterEqual, last.Format(mysql.CurrentTimeFormat))
c.Assert(n.String(), GreaterEqual, last.Format(tfStr))

v, err = builtinCurrentTime([]interface{}{3}, nil)
c.Assert(err, IsNil)
n, ok = v.(mysql.Time)
n, ok = v.(mysql.Duration)
c.Assert(ok, IsTrue)
c.Assert(n.String(), HasLen, 12)
c.Assert(n.String(), GreaterEqual, last.Format(mysql.CurrentTimeFormat))
c.Assert(n.String(), GreaterEqual, last.Format(tfStr))

v, err = builtinCurrentTime([]interface{}{6}, nil)
c.Assert(err, IsNil)
n, ok = v.(mysql.Time)
n, ok = v.(mysql.Duration)
c.Assert(ok, IsTrue)
c.Assert(n.String(), HasLen, 15)
c.Assert(n.String(), GreaterEqual, last.Format(mysql.CurrentTimeFormat))
c.Assert(n.String(), GreaterEqual, last.Format(tfStr))

v, err = builtinCurrentTime([]interface{}{-1}, nil)
c.Assert(err, NotNil)
Expand Down
13 changes: 3 additions & 10 deletions mysql/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ var (

// Time format without fractional seconds precision.
const (
DateFormat = "2006-01-02"
CurrentTimeFormat = "15:04:05"
TimeFormat = "2006-01-02 15:04:05"
DateFormat = "2006-01-02"
TimeFormat = "2006-01-02 15:04:05"
// TimeFSPFormat is time format with fractional seconds precision.
TimeFSPFormat = "2006-01-02 15:04:05.000000"
)
Expand Down Expand Up @@ -126,13 +125,7 @@ func (t Time) String() string {
return t.Time.Format(DateFormat)
}

var tfStr string
if t.Type == TypeDuration {
tfStr = CurrentTimeFormat
} else {
tfStr = TimeFormat
}

tfStr := TimeFormat
if t.Fsp > 0 {
tfStr = fmt.Sprintf("%s.%s", tfStr, strings.Repeat("0", t.Fsp))
}
Expand Down

0 comments on commit 7822670

Please sign in to comment.