Skip to content

Commit

Permalink
util/types: re-implement check time related (#2233)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Dec 14, 2016
1 parent 14b6696 commit 4741960
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 123 deletions.
3 changes: 0 additions & 3 deletions util/types/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
package types

import (
"fmt"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/mysql"
)
Expand Down Expand Up @@ -57,7 +55,6 @@ func (s *testTimeSuite) TestTimeFormatMethod(c *C) {
for i, t := range tblDate {
tm, err := ParseTime(t.Input, mysql.TypeDatetime, 6)
c.Assert(err, IsNil, Commentf("parse time fail: %s", t.Input))
fmt.Println("lala", tm.Time.Microsecond())

str, err := tm.Format(t.Format)
c.Assert(err, IsNil, Commentf("time format fail: %d", i))
Expand Down
14 changes: 12 additions & 2 deletions util/types/mytime.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,19 @@ func (t mysqlTime) ISOWeek() (int, int) {
}

func (t mysqlTime) GoTime() (gotime.Time, error) {
err := checkTime(int(t.year), int(t.month), int(t.day), int(t.hour), int(t.minute), int(t.second), int(t.microsecond))
// gotime.Time can't represent month 0 or day 0, date contains 0 would be converted to a nearest date,
// For example, 2006-12-00 00:00:00 would become 2015-11-30 23:59:59.
tm := gotime.Date(t.Year(), gotime.Month(t.Month()), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Microsecond()*1000, gotime.Local)
return tm, errors.Trace(err)
year, month, day := tm.Date()
hour, minute, second := tm.Clock()
microsec := tm.Nanosecond() / 1000
// This function will check the result, and return an error if it's not the same with the origin input.
if year != t.Year() || int(month) != t.Month() || day != t.Day() ||
hour != t.Hour() || minute != t.Minute() || second != t.Second() ||
microsec != t.Microsecond() {
return tm, errors.Trace(ErrInvalidTimeFormat)
}
return tm, nil
}

func newMysqlTime(year, month, day, hour, minute, second, microsecond int) mysqlTime {
Expand Down
Loading

0 comments on commit 4741960

Please sign in to comment.