Pull request: Fix duration
Merge in DNS/adguard-home from fix-duration to master Squashed commit of the following: commit b6d960076e6263718ec612bc7a998c48fb92079f Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Aug 27 15:24:37 2021 +0300 home: imp docs & fmt commit b3d1e5dbbb9c9abe92b10a51cc1f8d7afee73e12 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Aug 27 15:16:20 2021 +0300 home: fix duration
This commit is contained in:
parent
9f52adf33d
commit
77821ec816
|
@ -23,22 +23,30 @@ type Duration struct {
|
||||||
//
|
//
|
||||||
func (d Duration) String() (str string) {
|
func (d Duration) String() (str string) {
|
||||||
str = d.Duration.String()
|
str = d.Duration.String()
|
||||||
secs := d.Seconds()
|
|
||||||
var secsInt int
|
|
||||||
if secsInt = int(secs); float64(secsInt) != secs || secsInt%60 != 0 {
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
tailMin = len(`0s`)
|
tailMin = len(`0s`)
|
||||||
tailMinSec = len(`0m0s`)
|
tailMinSec = len(`0m0s`)
|
||||||
|
|
||||||
|
secsInHour = time.Hour / time.Second
|
||||||
|
minsInHour = time.Hour / time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
if (secsInt%3600)/60 != 0 {
|
switch rounded := d.Duration / time.Second; {
|
||||||
return str[:len(str)-tailMin]
|
case
|
||||||
}
|
rounded == 0,
|
||||||
|
rounded*time.Second != d.Duration,
|
||||||
|
rounded%60 != 0:
|
||||||
|
// Return the uncutted value if it's either equal to zero or has
|
||||||
|
// fractions of a second or even whole seconds in it.
|
||||||
|
return str
|
||||||
|
|
||||||
return str[:len(str)-tailMinSec]
|
case (rounded%secsInHour)/minsInHour != 0:
|
||||||
|
return str[:len(str)-tailMin]
|
||||||
|
|
||||||
|
default:
|
||||||
|
return str[:len(str)-tailMinSec]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalText implements the encoding.TextMarshaler interface for Duration.
|
// MarshalText implements the encoding.TextMarshaler interface for Duration.
|
||||||
|
|
|
@ -46,6 +46,9 @@ func TestDuration_String(t *testing.T) {
|
||||||
}, {
|
}, {
|
||||||
name: "1m1.001s",
|
name: "1m1.001s",
|
||||||
val: time.Minute + time.Second + time.Millisecond,
|
val: time.Minute + time.Second + time.Millisecond,
|
||||||
|
}, {
|
||||||
|
name: "0s",
|
||||||
|
val: 0,
|
||||||
}}
|
}}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
|
Loading…
Reference in New Issue