Accept nil *time.Time in Time.Set

non-blocking
Jack Christensen 2021-02-20 09:28:14 -06:00
parent 6830cc0984
commit abeb337246
2 changed files with 9 additions and 0 deletions

View File

@ -42,6 +42,12 @@ func (dst *Time) Set(src interface{}) error {
int64(value.Second())*microsecondsPerSecond +
int64(value.Nanosecond())/1000
*dst = Time{Microseconds: usec, Status: Present}
case *time.Time:
if value == nil {
*dst = Time{Status: Null}
} else {
return dst.Set(*value)
}
default:
if originalSrc, ok := underlyingTimeType(src); ok {
return dst.Set(originalSrc)

View File

@ -48,6 +48,9 @@ func TestTimeSet(t *testing.T) {
{source: time.Date(1970, 1, 1, 0, 0, 0, 1000, time.UTC), result: pgtype.Time{Microseconds: 1, Status: pgtype.Present}},
{source: time.Date(1999, 12, 31, 23, 59, 59, 999999999, time.UTC), result: pgtype.Time{Microseconds: 86399999999, Status: pgtype.Present}},
{source: time.Date(2015, 1, 1, 0, 0, 0, 2000, time.Local), result: pgtype.Time{Microseconds: 2, Status: pgtype.Present}},
{source: func(t time.Time) *time.Time { return &t }(time.Date(2015, 1, 1, 0, 0, 0, 2000, time.Local)), result: pgtype.Time{Microseconds: 2, Status: pgtype.Present}},
{source: nil, result: pgtype.Time{Status: pgtype.Null}},
{source: (*time.Time)(nil), result: pgtype.Time{Status: pgtype.Null}},
{source: _time(time.Date(1970, 1, 1, 0, 0, 0, 3000, time.UTC)), result: pgtype.Time{Microseconds: 3, Status: pgtype.Present}},
}