Skip to content

Commit

Permalink
Merge pull request #12 from launchdarkly/eb/ch64206/time-type
Browse files Browse the repository at this point in the history
add ldtime package for time helpers
  • Loading branch information
eli-darkly authored Feb 24, 2020
2 parents 08c2cf5 + 0e5056e commit 9ce753b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ clean:
go clean

test:
@# Note, we need to specify all these packages individually for go test in order to remain 1.8-compatible
go test -race -v ./ldvalue ./ldreason ./lduser
go test -race -v ./...

$(LINTER_VERSION_FILE):
rm -f $(LINTER)
Expand Down
2 changes: 2 additions & 0 deletions ldtime/package_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package ldtime contains time-related types and functions used by LaunchDarkly packages.
package ldtime
17 changes: 17 additions & 0 deletions ldtime/unix_millis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ldtime

import "time"

// UnixMillisecondTime is a millisecond timestamp starting from the Unix epoch.
type UnixMillisecondTime uint64

// UnixMillisFromTime converts a Time value into UnixMillisecondTime.
func UnixMillisFromTime(t time.Time) UnixMillisecondTime {
ms := time.Duration(t.UnixNano()) / time.Millisecond
return UnixMillisecondTime(ms)
}

// UnixMillisNow returns the current date/time as a UnixMillisecondTime.
func UnixMillisNow() UnixMillisecondTime {
return UnixMillisFromTime(time.Now())
}
14 changes: 14 additions & 0 deletions ldtime/unix_millis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ldtime

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestUnixMillisFromTime(t *testing.T) {
tt := time.Date(1970, time.January, 1, 0, 1, 2, 0, time.UTC)
ut := UnixMillisFromTime(tt)
assert.Equal(t, uint64(62000), uint64(ut))
}
3 changes: 0 additions & 3 deletions package_info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// Package ldcommon is the base package for commonly used Go SDK types and functions.
//
// Currently there are no types in this package; they are all in subpackages such as ldvalue.
// In a future version, this package will contain the types that are most commonly needed
// when using the SDK, such as User. The subpackages contain types that applications are
// less likely to need (and that do not reference anything in the main package), such as Value.
package ldcommon

0 comments on commit 9ce753b

Please sign in to comment.