improve time string parsing for invalid datetime

This commit is contained in:
John
2020-02-01 20:14:24 +08:00
parent 91bbff6ced
commit 4c3af63076
4 changed files with 15 additions and 8 deletions

View File

@ -8,10 +8,6 @@ func main() {
db := g.DB()
db.SetDebug(true)
one, e := db.Table("order.order o").LeftJoin("user.user u", "o.uid=u.id").Where("u.id", 1).One()
if e != nil {
panic(e)
}
g.Dump(one)
db.Table("user").Fields("DISTINCT id,nickname").Filter().All()
}

View File

@ -43,8 +43,8 @@ func Test_Types(t *testing.T) {
"binary": []byte("abcdefgh"),
"date": "2018-10-24",
"time": "10:00:01",
"decimal": 123.456,
"double": 123.456,
"decimal": -123.456,
"double": -123.456,
"bit": 2,
"tinyint": true,
"bool": false,
@ -61,7 +61,7 @@ func Test_Types(t *testing.T) {
gtest.Assert(one["binary"].String(), data["binary"])
gtest.Assert(one["date"].String(), data["date"])
gtest.Assert(one["time"].String(), data["time"])
gtest.Assert(one["decimal"].String(), 123.46)
gtest.Assert(one["decimal"].String(), -123.46)
gtest.Assert(one["double"].String(), data["double"])
gtest.Assert(one["bit"].Int(), data["bit"])
gtest.Assert(one["tinyint"].Bool(), data["tinyint"])

View File

@ -316,6 +316,9 @@ func StrToTime(str string, format ...string) (*Time, error) {
}
}
}
if year <= 0 || month <= 0 || day <= 0 || hour < 0 || min < 0 || sec < 0 || nsec < 0 {
return nil, errors.New("invalid time string:" + str)
}
// It finally converts all time to UTC time zone.
return NewFromTime(time.Date(year, time.Month(month), day, hour, min, sec, nsec, local)), nil
}

View File

@ -94,6 +94,14 @@ func Test_Format(t *testing.T) {
})
}
func Test_Format_ZeroString(t *testing.T) {
gtest.Case(t, func() {
timeTemp, err := gtime.StrToTime("0000-00-00 00:00:00")
gtest.AssertNE(err, nil)
gtest.Assert(timeTemp.String(), "")
})
}
func Test_FormatTo(t *testing.T) {
gtest.Case(t, func() {
timeTemp := gtime.Now()