diff --git a/time.go b/time.go index 16a2a393..237c4b5b 100644 --- a/time.go +++ b/time.go @@ -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) diff --git a/time_test.go b/time_test.go index bf6365ef..0af42b1e 100644 --- a/time_test.go +++ b/time_test.go @@ -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}}, }