From 4030c7397afeb5d781e0e913e0c5c5082937f41d Mon Sep 17 00:00:00 2001 From: yumaojun03 <18108053819@163.com> Date: Thu, 12 Mar 2020 12:27:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dftime=20sql=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/ftime/sql.go | 21 +++++++++++++-------- types/ftime/time.go | 4 ++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/types/ftime/sql.go b/types/ftime/sql.go index 37cfa41..e3889bd 100644 --- a/types/ftime/sql.go +++ b/types/ftime/sql.go @@ -2,22 +2,27 @@ package ftime import ( "database/sql/driver" + "errors" ) // Scan 实现sql的反序列化 -func (t Time) Scan(value interface{}) { - switch UsedFormatType { - case TIMESTAMP: - value = t.timestamp() - case TEXT: - value = t.formatText() +func (t *Time) Scan(value interface{}) error { + if t == nil { + return nil + } + + switch v := value.(type) { + case int64: + return t.parseTSInt64(v) + case string: + return t.parseTS(v) default: - value = t.timestamp() + return errors.New("unsupport type") } } // Value 实现sql的序列化 -func (t *Time) Value() (driver.Value, error) { +func (t Time) Value() (driver.Value, error) { switch UsedFormatType { case TIMESTAMP: return t.timestamp(), nil diff --git a/types/ftime/time.go b/types/ftime/time.go index a9c7eb6..cf07497 100644 --- a/types/ftime/time.go +++ b/types/ftime/time.go @@ -100,6 +100,10 @@ func (t Time) formatText() []byte { func (t Time) timestamp() int64 { var ts int64 + if t.T().IsZero() { + return 0 + } + switch UsedTimestampLength { case Length10: ts = time.Time(t).Unix()