Skip to content

Commit

Permalink
Merge pull request #2035 from exekias/fix-interval
Browse files Browse the repository at this point in the history
Fix interval encoding to allow 0s and avoid extra spaces
  • Loading branch information
jackc authored May 31, 2024
2 parents 572d7ff + 9f4a264 commit ec557e8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
32 changes: 14 additions & 18 deletions pgtype/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,29 +132,25 @@ func (encodePlanIntervalCodecText) Encode(value any, buf []byte) (newBuf []byte,

if interval.Days != 0 {
buf = append(buf, strconv.FormatInt(int64(interval.Days), 10)...)
buf = append(buf, " day"...)
buf = append(buf, " day "...)
}

if interval.Microseconds != 0 {
buf = append(buf, " "...)

absMicroseconds := interval.Microseconds
if absMicroseconds < 0 {
absMicroseconds = -absMicroseconds
buf = append(buf, '-')
}
absMicroseconds := interval.Microseconds
if absMicroseconds < 0 {
absMicroseconds = -absMicroseconds
buf = append(buf, '-')
}

hours := absMicroseconds / microsecondsPerHour
minutes := (absMicroseconds % microsecondsPerHour) / microsecondsPerMinute
seconds := (absMicroseconds % microsecondsPerMinute) / microsecondsPerSecond
hours := absMicroseconds / microsecondsPerHour
minutes := (absMicroseconds % microsecondsPerHour) / microsecondsPerMinute
seconds := (absMicroseconds % microsecondsPerMinute) / microsecondsPerSecond

timeStr := fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
buf = append(buf, timeStr...)
timeStr := fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
buf = append(buf, timeStr...)

microseconds := absMicroseconds % microsecondsPerSecond
if microseconds != 0 {
buf = append(buf, fmt.Sprintf(".%06d", microseconds)...)
}
microseconds := absMicroseconds % microsecondsPerSecond
if microseconds != 0 {
buf = append(buf, fmt.Sprintf(".%06d", microseconds)...)
}

return buf, nil
Expand Down
20 changes: 20 additions & 0 deletions pgtype/interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxtest"
"github.com/stretchr/testify/assert"
)

func TestIntervalCodec(t *testing.T) {
Expand Down Expand Up @@ -136,3 +137,22 @@ func TestIntervalCodec(t *testing.T) {
{nil, new(pgtype.Interval), isExpectedEq(pgtype.Interval{})},
})
}

func TestIntervalTextEncode(t *testing.T) {
m := pgtype.NewMap()

successfulTests := []struct {
source pgtype.Interval
result string
}{
{source: pgtype.Interval{Months: 2, Days: 1, Microseconds: 0, Valid: true}, result: "2 mon 1 day 00:00:00"},
{source: pgtype.Interval{Months: 0, Days: 0, Microseconds: 0, Valid: true}, result: "00:00:00"},
{source: pgtype.Interval{Months: 0, Days: 0, Microseconds: 6 * 60 * 1000000, Valid: true}, result: "00:06:00"},
{source: pgtype.Interval{Months: 0, Days: 1, Microseconds: 6*60*1000000 + 30, Valid: true}, result: "1 day 00:06:00.000030"},
}
for i, tt := range successfulTests {
buf, err := m.Encode(pgtype.DateOID, pgtype.TextFormatCode, tt.source, nil)
assert.NoErrorf(t, err, "%d", i)
assert.Equalf(t, tt.result, string(buf), "%d", i)
}
}

0 comments on commit ec557e8

Please sign in to comment.