mirror of
https://github.com/golang/go
synced 2024-09-24 03:10:16 -06:00
time: add Duration.Abs
Fixes #51414
Change-Id: Ia3b1674f2a902c8396fe029397536643a3bc1784
GitHub-Last-Rev: 67159648af
GitHub-Pull-Request: golang/go#51739
Reviewed-on: https://go-review.googlesource.com/c/go/+/393515
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
This commit is contained in:
parent
737837c9d4
commit
0bbd05b826
1
api/next/51414.txt
Normal file
1
api/next/51414.txt
Normal file
@ -0,0 +1 @@
|
||||
pkg time, method (Duration) Abs() Duration #51414
|
@ -815,6 +815,19 @@ func (d Duration) Round(m Duration) Duration {
|
||||
return maxDuration // overflow
|
||||
}
|
||||
|
||||
// Abs returns the absolute value of d.
|
||||
// As a special case, math.MinInt64 is converted to math.MaxInt64.
|
||||
func (d Duration) Abs() Duration {
|
||||
switch {
|
||||
case d >= 0:
|
||||
return d
|
||||
case d == minDuration:
|
||||
return maxDuration
|
||||
default:
|
||||
return -d
|
||||
}
|
||||
}
|
||||
|
||||
// Add returns the time t+d.
|
||||
func (t Time) Add(d Duration) Time {
|
||||
dsec := int64(d / 1e9)
|
||||
|
@ -1240,6 +1240,30 @@ func TestDurationRound(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
var durationAbsTests = []struct {
|
||||
d Duration
|
||||
want Duration
|
||||
}{
|
||||
{0, 0},
|
||||
{1, 1},
|
||||
{-1, 1},
|
||||
{1 * Minute, 1 * Minute},
|
||||
{-1 * Minute, 1 * Minute},
|
||||
{minDuration, maxDuration},
|
||||
{minDuration + 1, maxDuration},
|
||||
{minDuration + 2, maxDuration - 1},
|
||||
{maxDuration, maxDuration},
|
||||
{maxDuration - 1, maxDuration - 1},
|
||||
}
|
||||
|
||||
func TestDurationAbs(t *testing.T) {
|
||||
for _, tt := range durationAbsTests {
|
||||
if got := tt.d.Abs(); got != tt.want {
|
||||
t.Errorf("Duration(%s).Abs() = %s; want: %s", tt.d, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var defaultLocTests = []struct {
|
||||
name string
|
||||
f func(t1, t2 Time) bool
|
||||
|
Loading…
Reference in New Issue
Block a user