Skip to content

Commit

Permalink
handle datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
adoublef committed Apr 7, 2024
1 parent b622592 commit 174483f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion time/date/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion time/date/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down

0 comments on commit 174483f

Please sign in to comment.