mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
Merge pull request #1196 from yanllllk/yys
This commit is contained in:
29
os/gtime/gtime_sql.go
Normal file
29
os/gtime/gtime_sql.go
Normal 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
|
||||
}
|
||||
41
os/gtime/gtime_z_unit_sql_test.go
Normal file
41
os/gtime/gtime_z_unit_sql_test.go
Normal 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)
|
||||
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user