From 1e52d72e038ee3ca67c2da21bcad96b31b960ba5 Mon Sep 17 00:00:00 2001 From: Yeagerist0 <180412661+Yeagerist0@users.noreply.github.com> Date: Mon, 19 Oct 2026 00:17:18 +0530 Subject: [PATCH] events: keep the fractional part in DurationSeconds and DurationMinutes UnmarshalJSON decoded the number as a float64 and then converted it to time.Duration before multiplying, which truncates the fraction: 1.5 seconds became 1s and 1.5 minutes became 1m. Multiply in float64 first. --- events/duration.go | 4 ++-- events/duration_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 events/duration_test.go diff --git a/events/duration.go b/events/duration.go index 7952265d..00094709 100644 --- a/events/duration.go +++ b/events/duration.go @@ -15,7 +15,7 @@ func (duration *DurationSeconds) UnmarshalJSON(data []byte) error { return err } - *duration = DurationSeconds(time.Duration(seconds) * time.Second) + *duration = DurationSeconds(time.Duration(seconds * float64(time.Second))) return nil } @@ -34,7 +34,7 @@ func (duration *DurationMinutes) UnmarshalJSON(data []byte) error { return err } - *duration = DurationMinutes(time.Duration(minutes) * time.Minute) + *duration = DurationMinutes(time.Duration(minutes * float64(time.Minute))) return nil } diff --git a/events/duration_test.go b/events/duration_test.go new file mode 100644 index 00000000..712be18c --- /dev/null +++ b/events/duration_test.go @@ -0,0 +1,27 @@ +package events + +import ( + "encoding/json" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestDurationSecondsKeepsFraction(t *testing.T) { + var d DurationSeconds + require.NoError(t, json.Unmarshal([]byte("1.5"), &d)) + require.Equal(t, 1500*time.Millisecond, time.Duration(d)) + + require.NoError(t, json.Unmarshal([]byte("36"), &d)) + require.Equal(t, 36*time.Second, time.Duration(d)) +} + +func TestDurationMinutesKeepsFraction(t *testing.T) { + var d DurationMinutes + require.NoError(t, json.Unmarshal([]byte("1.5"), &d)) + require.Equal(t, 90*time.Second, time.Duration(d)) + + require.NoError(t, json.Unmarshal([]byte("60"), &d)) + require.Equal(t, 60*time.Minute, time.Duration(d)) +}