File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -23,10 +23,12 @@ func NewNullString(s string) sql.NullString {
2323 }
2424}
2525
26+ // NewNullTime returns a sql.NullTime with the given time.Time. If the time is
27+ // the zero value, the NullTime is invalid.
2628func NewNullTime (t time.Time ) sql.NullTime {
2729 return sql.NullTime {
2830 Time : t ,
29- Valid : true ,
31+ Valid : t != time. Time {} ,
3032 }
3133}
3234
Original file line number Diff line number Diff line change 1+ package database
2+
3+ import (
4+ "testing"
5+ "time"
6+
7+ "github.com/stretchr/testify/require"
8+ )
9+
10+ func TestNewNullTime (t * testing.T ) {
11+ var t1 time.Time
12+ nt1 := NewNullTime (t1 )
13+ require .False (t , nt1 .Valid )
14+
15+ t1 = time .Now ()
16+ nt1 = NewNullTime (t1 )
17+ require .True (t , nt1 .Valid )
18+ require .Equal (t , t1 , nt1 .Time )
19+ }
You can’t perform that action at this time.
0 commit comments