diff --git a/interval.go b/interval.go index 3a91c595..309e880c 100644 --- a/interval.go +++ b/interval.go @@ -16,6 +16,8 @@ const ( microsecondsPerSecond = 1000000 microsecondsPerMinute = 60 * microsecondsPerSecond microsecondsPerHour = 60 * microsecondsPerMinute + microsecondsPerDay = 24 * microsecondsPerHour + microsecondsPerMonth = 30 * microsecondsPerDay ) type Interval struct { @@ -67,10 +69,8 @@ func (src *Interval) AssignTo(dst interface{}) error { case Present: switch v := dst.(type) { case *time.Duration: - if src.Days > 0 || src.Months > 0 { - return errors.Errorf("interval with months or days cannot be decoded into %T", dst) - } - *v = time.Duration(src.Microseconds) * time.Microsecond + us := int64(src.Months)*microsecondsPerMonth + int64(src.Days)*microsecondsPerDay + src.Microseconds + *v = time.Duration(us) * time.Microsecond return nil default: if nextDst, retry := GetAssignToDstType(dst); retry { diff --git a/interval_test.go b/interval_test.go index 6a4787e0..1ee094d7 100644 --- a/interval_test.go +++ b/interval_test.go @@ -2,9 +2,12 @@ package pgtype_test import ( "testing" + "time" "github.com/jackc/pgtype" "github.com/jackc/pgtype/testutil" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestIntervalTranscode(t *testing.T) { @@ -61,3 +64,11 @@ func TestIntervalNormalize(t *testing.T) { }, }) } + +func TestIntervalLossyConversionToDuration(t *testing.T) { + interval := &pgtype.Interval{Months: 1, Days: 1, Status: pgtype.Present} + var d time.Duration + err := interval.AssignTo(&d) + require.NoError(t, err) + assert.EqualValues(t, int64(2678400000000000), d.Nanoseconds()) +}