From 174483f19c50d085e2f2f258dffa8d7d8f00e6c2 Mon Sep 17 00:00:00 2001 From: Kristopher Rahim Date: Sun, 7 Apr 2024 17:43:01 +0100 Subject: [PATCH] handle datetime --- time/date/date.go | 2 +- time/date/date_test.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/time/date/date.go b/time/date/date.go index d76fcb1..0106af2 100644 --- a/time/date/date.go +++ b/time/date/date.go @@ -119,8 +119,8 @@ func (d Date) MarshalText() ([]byte, error) { func (d *Date) Scan(v any) (err error) { switch v := v.(type) { case nil: - *d = Date{} case string: + v, _, _ = cut(v, 10) *d, err = Parse(v) default: return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type *date.Date", v) diff --git a/time/date/date_test.go b/time/date/date_test.go index c358cdb..251ff0d 100644 --- a/time/date/date_test.go +++ b/time/date/date_test.go @@ -76,7 +76,8 @@ func Test_Date_Scan(t *testing.T) { _, err = db.Exec(context.TODO(), ` create table t (id int not null, d text) strict; insert into t (id, d) values (1, '2022-10-18'); - insert into t (id, d) values (2, null);`) + insert into t (id, d) values (2, null); + insert into t (id, d) values (3, '2016-10-18T00:00:00Z');`) if err != nil { t.Fatalf("(sql3.DB).Exec: %v", err) } @@ -103,6 +104,13 @@ func Test_Date_Scan(t *testing.T) { is.Equal(d, nil) }) + + t.Run("Long", func(t *testing.T) { + is := is.NewRelaxed(t) + var d Date + err = db.QueryRow(context.TODO(), `select d from t where id = 3`).Scan(&d) + is.NoErr(err) // sql3.DB).QueryRow + }) } func Test_Date_Value(t *testing.T) {