Skip to content

Commit

Permalink
Do not encode interval microseconds when they are 0
Browse files Browse the repository at this point in the history
This make the encode match what postgres does
  • Loading branch information
exekias committed Mar 19, 2024
1 parent b6e5548 commit 33f6139
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions pgtype/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,27 @@ 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"...)
}

absMicroseconds := interval.Microseconds
if absMicroseconds < 0 {
absMicroseconds = -absMicroseconds
buf = append(buf, '-')
}
if interval.Microseconds != 0 {
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
microseconds := absMicroseconds % microsecondsPerSecond
hours := absMicroseconds / microsecondsPerHour
minutes := (absMicroseconds % microsecondsPerHour) / microsecondsPerMinute
seconds := (absMicroseconds % microsecondsPerMinute) / microsecondsPerSecond
microseconds := absMicroseconds % microsecondsPerSecond

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

timeStr := fmt.Sprintf("%02d:%02d:%02d.%06d", hours, minutes, seconds, microseconds)
buf = append(buf, timeStr...)
return buf, nil
}

Expand Down

0 comments on commit 33f6139

Please sign in to comment.