Skip to content

Commit

Permalink
interp: avoid negative elapsed durations in the time builtin
Browse files Browse the repository at this point in the history
Change the operation to calculate the elapsed seconds from Remainder to Mod,
as Remainder can return negative values.

Fixes mvdan#767.
  • Loading branch information
Francisco Miamoto authored Dec 6, 2021
1 parent 1963ae1 commit f965c62
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3713,6 +3713,8 @@ func TestElapsedString(t *testing.T) {
true,
"610.00",
},
{31 * time.Second, false, "0m31.000s"},
{102 * time.Second, false, "1m42.000s"},
}
for _, tc := range tests {
t.Run(tc.in.String(), func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion interp/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ func elapsedString(d time.Duration, posix bool) string {
return fmt.Sprintf("%.2f", d.Seconds())
}
min := int(d.Minutes())
sec := math.Remainder(d.Seconds(), 60.0)
sec := math.Mod(d.Seconds(), 60.0)
return fmt.Sprintf("%dm%.3fs", min, sec)
}

Expand Down

0 comments on commit f965c62

Please sign in to comment.