Skip to content

Commit ce0265f

Browse files
authored
fix EligibleAt null / NewNullTime (#471)
* fix EligibleAt null / NewNullTime * test
1 parent e092c7c commit ce0265f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

database/types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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.
2628
func NewNullTime(t time.Time) sql.NullTime {
2729
return sql.NullTime{
2830
Time: t,
29-
Valid: true,
31+
Valid: t != time.Time{},
3032
}
3133
}
3234

database/types_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

0 commit comments

Comments
 (0)