diff --git a/pkg/types/unix_milli_test.go b/pkg/types/unix_milli_test.go index 985fa2ac1..818cff030 100644 --- a/pkg/types/unix_milli_test.go +++ b/pkg/types/unix_milli_test.go @@ -1,6 +1,7 @@ package types import ( + "github.com/icinga/icingadb/pkg/utils" "github.com/stretchr/testify/require" "testing" "time" @@ -16,6 +17,7 @@ func TestUnixMilli_MarshalJSON(t *testing.T) { {"zero", UnixMilli{}, `null`}, {"epoch", UnixMilli(time.Unix(0, 0)), `0`}, {"nonzero", UnixMilli(time.Unix(1234567890, 62500000)), `1234567890062`}, + {"ilovefloats", UnixMilli(utils.FromUnixMilli(1714489037339)), `1714489037339`}, } for _, st := range subtests { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 4b0fe1df0..01c03d958 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -9,7 +9,6 @@ import ( "github.com/lib/pq" "github.com/pkg/errors" "golang.org/x/exp/utf8string" - "math" "net" "os" "path/filepath" @@ -21,9 +20,7 @@ import ( // FromUnixMilli creates and returns a time.Time value // from the given milliseconds since the Unix epoch ms. func FromUnixMilli(ms int64) time.Time { - sec, dec := math.Modf(float64(ms) / 1e3) - - return time.Unix(int64(sec), int64(dec*(1e9))) + return time.UnixMilli(ms) } // Name returns the declared name of type t. diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index b0ea54b8f..de34c76e3 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -3,6 +3,7 @@ package utils import ( "github.com/stretchr/testify/require" "testing" + "time" ) func TestChanFromSlice(t *testing.T) { @@ -52,3 +53,8 @@ func requireClosedEmpty(t *testing.T, ch <-chan int) { require.Fail(t, "receiving should not block") } } + +func TestFromUnixMilli_Float(t *testing.T) { + const ms = 1714489037339 + require.Equal(t, time.UnixMilli(ms), FromUnixMilli(ms)) +}