Merge pull request #1196 from yanllllk/yys

This commit is contained in:
John Guo
2021-04-01 09:27:59 +08:00
committed by GitHub
2 changed files with 70 additions and 0 deletions

29
os/gtime/gtime_sql.go Normal file
View File

@ -0,0 +1,29 @@
package gtime
import (
"database/sql/driver"
)
//Scanner is an interface used by Scan.
//Scan value from database
//database/sql
func (t *Time) Scan(value interface{}) error {
if t == nil {
return nil
}
newTime := New(value)
t.Time = newTime.Time
return nil
}
// Valuer is the interface providing the Value method. database/sql/driver
// Value insert into mysql need this function.
func (t *Time) Value() (driver.Value, error) {
if t == nil {
return nil, nil
}
if t.IsZero() {
return nil, nil
}
return t.Time, nil
}

View File

@ -0,0 +1,41 @@
package gtime_test
import (
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/test/gtest"
"testing"
)
func TestTime_Scan(t1 *testing.T) {
gtest.C(t1, func(t *gtest.T) {
tt := gtime.Time{}
//test string
s := gtime.Now().String()
t.Assert(tt.Scan(s), nil)
t.Assert(tt.String(), s)
//test nano
n := gtime.TimestampNano()
t.Assert(tt.Scan(n), nil)
t.Assert(tt.TimestampNano(), n)
//test nil
none := (*gtime.Time)(nil)
t.Assert(none.Scan(nil), nil)
t.Assert(none, nil)
})
}
func TestTime_Value(t1 *testing.T) {
gtest.C(t1, func(t *gtest.T) {
tt := gtime.Now()
s, err := tt.Value()
t.Assert(err, nil)
t.Assert(s, tt.Time)
//test nil
none := (*gtime.Time)(nil)
s, err = none.Value()
t.Assert(err, nil)
t.Assert(s, nil)
})
}