Skip to content

Commit

Permalink
utils.FromUnixMilli: Replace logic by time.UnixMilli
Browse files Browse the repository at this point in the history
Required for incremental configurations in icinga-notifications as it
introduces time comparisons: Icinga/icinga-notifications#5

References #753.
  • Loading branch information
oxzi committed Apr 30, 2024
1 parent 1e3dea1 commit 066abea
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pkg/types/unix_milli_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"github.com/icinga/icingadb/pkg/utils"
"github.com/stretchr/testify/require"
"testing"
"time"
Expand All @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/lib/pq"
"github.com/pkg/errors"
"golang.org/x/exp/utf8string"
"math"
"net"
"os"
"path/filepath"
Expand All @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"github.com/stretchr/testify/require"
"testing"
"time"
)

func TestChanFromSlice(t *testing.T) {
Expand Down Expand Up @@ -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))
}

0 comments on commit 066abea

Please sign in to comment.