Skip to content

Commit

Permalink
Improve ISODuration string formatting (#4123)
Browse files Browse the repository at this point in the history
- Use `strconv.FormatFloat` for consistent output
- Add tests for rounding and formatting edge cases
  • Loading branch information
mastercactapus authored Nov 8, 2024
1 parent f557e93 commit 02e6f88
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion util/timeutil/isoduration.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (dur ISODuration) String() string {
sec := dur.SecondPart
// round to microseconds
sec = math.Round(sec*1e6) / 1e6
fmt.Fprintf(&b, "%gS", sec)
fmt.Fprintf(&b, "%sS", strconv.FormatFloat(sec, 'f', -1, 64))
}

return b.String()
Expand Down
3 changes: 2 additions & 1 deletion util/timeutil/isoduration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func TestISODuration_String(t *testing.T) {
check("P1D", ISODuration{DayPart: 1})
check("PT1H", ISODuration{HourPart: 1})
check("P1YT0.1S", ISODuration{YearPart: 1, SecondPart: 0.1})
check("P1YT0.123457S", ISODuration{YearPart: 1, SecondPart: 0.1234567}) // Note: rounded to 6 decimal places, so the 6th digit is 7 instead of 6.
check("P1YT0.000076S", ISODuration{YearPart: 1, SecondPart: 7.5809468881749e-05}) // Ensure we don't print an exponent.

check("P1Y2M3W4DT5H6M7S", ISODuration{
YearPart: 1,
Expand All @@ -39,7 +41,6 @@ func TestISODuration_String(t *testing.T) {
}

func TestParseISODuration(t *testing.T) {

check := func(desc string, iso string, exp ISODuration) {
t.Helper()

Expand Down

0 comments on commit 02e6f88

Please sign in to comment.