mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve time string parsing for invalid datetime
This commit is contained in:
@ -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()
|
||||
|
||||
}
|
||||
|
||||
@ -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"])
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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()
|
||||
|
||||
Reference in New Issue
Block a user