Skip to content

Commit 4b42f32

Browse files
committed
Do not divide then multiply as it may result in incorrect result
For example, DurationValue("4236579305ms", ...) will return 4236579304, because the result of diving 4236579305 by 1000 and then multiplying the result by 1000 will be 4.2365793049999995e+09, which when converted to int64 will result in incorrect value. Signed-off-by: Artem Fetishev <[email protected]>
1 parent 6559a88 commit 4b42f32

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

lexer.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -575,25 +575,25 @@ func parseSingleDuration(s string, step int64) (float64, error) {
575575
var mp float64
576576
switch s[len(numPart):] {
577577
case "ms":
578-
mp = 1e-3
579-
case "s":
580578
mp = 1
579+
case "s":
580+
mp = 1000
581581
case "m":
582-
mp = 60
582+
mp = 60 * 1000
583583
case "h":
584-
mp = 60 * 60
584+
mp = 60 * 60 * 1000
585585
case "d":
586-
mp = 24 * 60 * 60
586+
mp = 24 * 60 * 60 * 1000
587587
case "w":
588-
mp = 7 * 24 * 60 * 60
588+
mp = 7 * 24 * 60 * 60 * 1000
589589
case "y":
590-
mp = 365 * 24 * 60 * 60
590+
mp = 365 * 24 * 60 * 60 * 1000
591591
case "i":
592-
mp = float64(step) / 1e3
592+
mp = float64(step)
593593
default:
594594
return 0, fmt.Errorf("invalid duration suffix in %q", s)
595595
}
596-
return mp * f * 1e3, nil
596+
return mp * f, nil
597597
}
598598

599599
// scanDuration scans duration, which must start with positive num.

lexer_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ func TestDurationSuccess(t *testing.T) {
595595
// Integer durations
596596
f("123ms", 42, 123)
597597
f("-123ms", 42, -123)
598+
f("4236579305ms", 42, 4236579305)
598599
f("123s", 42, 123*1000)
599600
f("-123s", 42, -123*1000)
600601
f("123m", 42, 123*60*1000)

0 commit comments

Comments
 (0)