diff --git a/database/gdb/gdb_z_mysql_types_test.go b/database/gdb/gdb_z_mysql_types_test.go index f261b0e2c..afe03a5d5 100644 --- a/database/gdb/gdb_z_mysql_types_test.go +++ b/database/gdb/gdb_z_mysql_types_test.go @@ -8,6 +8,7 @@ package gdb_test import ( "fmt" + "github.com/gogf/gf/os/gtime" "testing" "github.com/gogf/gf/frame/g" @@ -15,6 +16,7 @@ import ( "github.com/gogf/gf/test/gtest" ) +// All types testing. func Test_Types(t *testing.T) { gtest.C(t, func(t *gtest.T) { if _, err := db.Exec(fmt.Sprintf(` @@ -72,6 +74,29 @@ func Test_Types(t *testing.T) { t.Assert(one["double"].String(), data["double"]) t.Assert(one["bit"].Int(), data["bit"]) t.Assert(one["tinyint"].Bool(), data["tinyint"]) - t.Assert(one["tinyint"].Bool(), data["tinyint"]) + + type T struct { + Id int + Blob []byte + Binary []byte + Date *gtime.Time + Time *gtime.Time + Decimal float64 + Double float64 + Bit int8 + TinyInt bool + } + var obj *T + err = db.Table("types").Struct(&obj) + t.Assert(err, nil) + t.Assert(obj.Id, 1) + t.Assert(obj.Blob, data["blob"]) + t.Assert(obj.Binary, data["binary"]) + t.Assert(obj.Date.Format("Y-m-d"), data["date"]) + t.Assert(obj.Time.String(), `0000-01-01 10:00:01`) + t.Assert(obj.Decimal, -123.46) + t.Assert(obj.Double, data["double"]) + t.Assert(obj.Bit, data["bit"]) + t.Assert(obj.TinyInt, data["tinyint"]) }) } diff --git a/os/gtime/gtime.go b/os/gtime/gtime.go index 88c38d460..e03a8715c 100644 --- a/os/gtime/gtime.go +++ b/os/gtime/gtime.go @@ -350,9 +350,9 @@ func StrToTime(str string, format ...string) (*Time, error) { } } } - if year <= 0 { - return nil, errors.New("invalid time string:" + str) - } + //if year <= 0 { + // return nil, errors.New("invalid time string:" + str) + //} return NewFromTime(time.Date(year, time.Month(month), day, hour, min, sec, nsec, local)), nil } @@ -367,7 +367,7 @@ func ConvertZone(strTime string, toZone string, fromZone ...string) (*Time, erro if l, err := time.LoadLocation(fromZone[0]); err != nil { return nil, err } else { - t.Time = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Time.Second(), t.Time.Nanosecond(), l) + t.Time = time.Date(t.Year(), time.Month(t.Month()), t.Day(), t.Hour(), t.Minute(), t.Time.Second(), t.Time.Nanosecond(), l) } } if l, err := time.LoadLocation(toZone); err != nil { diff --git a/os/gtime/gtime_time.go b/os/gtime/gtime_time.go index 746a8f5ec..4584b661e 100644 --- a/os/gtime/gtime_time.go +++ b/os/gtime/gtime_time.go @@ -179,6 +179,11 @@ func (t *Time) TimestampNanoStr() string { return strconv.FormatInt(t.TimestampNano(), 10) } +// Month returns the month of the year specified by t. +func (t *Time) Month() int { + return int(t.Time.Month()) +} + // Second returns the second offset within the minute specified by t, // in the range [0, 59]. func (t *Time) Second() int { diff --git a/util/gconv/gconv_z_unit_time_test.go b/util/gconv/gconv_z_unit_time_test.go index 335e599e5..608ad6b67 100644 --- a/util/gconv/gconv_z_unit_time_test.go +++ b/util/gconv/gconv_z_unit_time_test.go @@ -24,6 +24,20 @@ func Test_Time(t *testing.T) { }) gtest.C(t, func(t *gtest.T) { s := "01:02:03.456" + t.AssertEQ(gconv.GTime(s).Hour(), 1) + t.AssertEQ(gconv.GTime(s).Minute(), 2) + t.AssertEQ(gconv.GTime(s).Second(), 3) + t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s)) + t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time) + }) + gtest.C(t, func(t *gtest.T) { + s := "0000-01-01 01:02:03" + t.AssertEQ(gconv.GTime(s).Year(), 0) + t.AssertEQ(gconv.GTime(s).Month(), 1) + t.AssertEQ(gconv.GTime(s).Day(), 1) + t.AssertEQ(gconv.GTime(s).Hour(), 1) + t.AssertEQ(gconv.GTime(s).Minute(), 2) + t.AssertEQ(gconv.GTime(s).Second(), 3) t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s)) t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time) })