Commit 4983a0b7 authored by Joe Tsai's avatar Joe Tsai

Revert "time: optimize Sub"

This reverts commit CL 131196 because there is a bug
in the calculation of nanoseconds.

Fixes #33677

Change-Id: Ic8e94c547ee29b8aeda1b9a5cb9764dbf47b14b4
Reviewed-on: https://go-review.googlesource.com/c/go/+/190497
Run-TryBot: Andrew Bonventre <andybons@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarEmmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: default avatarAndrew Bonventre <andybons@golang.org>
parent 0212f041
......@@ -906,33 +906,16 @@ func (t Time) Sub(u Time) Duration {
}
return d
}
ts, us := t.sec(), u.sec()
var sec, nsec, d int64
ssub := ts - us
if (ssub < ts) != (us > 0) {
goto overflow
}
if ssub < int64(minDuration/Second) || ssub > int64(maxDuration/Second) {
goto overflow
}
sec = ssub * int64(Second)
nsec = int64(t.nsec() - u.nsec())
d = sec + nsec
if (d > sec) != (nsec > 0) {
goto overflow
}
return Duration(d)
overflow:
if t.Before(u) {
d := Duration(t.sec()-u.sec())*Second + Duration(t.nsec()-u.nsec())
// Check for overflow or underflow.
switch {
case u.Add(d).Equal(t):
return d // d is correct
case t.Before(u):
return minDuration // t - u is negative out of range
}
default:
return maxDuration // t - u is positive out of range
}
}
// Since returns the time elapsed since t.
......
......@@ -1008,14 +1008,6 @@ func TestSub(t *testing.T) {
}
}
func BenchmarkSub(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, st := range subTests {
st.t.Sub(st.u)
}
}
}
var nsDurationTests = []struct {
d Duration
want int64
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment