Skip to content

Commit

Permalink
Merge #5
Browse files Browse the repository at this point in the history
5: Add #TimestampFromV1 function r=Jaskaranbir a=Jaskaranbir

Just another convenience _function-proxy_.

Co-authored-by: Jaskaranbir <jaskaranbir.dhillon@gmail.com>
  • Loading branch information
ninja-bruh and Jaskaranbir committed Oct 19, 2018
2 parents 878f821 + 2c420cf commit 7e7a30e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func (u *UUID) UnmarshalCQL(info gocql.TypeInfo, data []byte) error {
return u.UnmarshalBinary(data)
}

// TimestampFromV1 returns the Timestamp embedded within a V1 UUID.
// Returns an error if the UUID is any version other than 1.
func TimestampFromV1(u UUID) (uuid.Timestamp, error) {
return uuid.TimestampFromV1(u.UUID)
}

// randomHWAddrFunc generates a random MAC address for V1-UUID.
func randomHWAddrFunc() (net.HardwareAddr, error) {
// From: https://stackoverflow.com/questions/21018729/generate-mac-address-in-go
Expand Down
23 changes: 23 additions & 0 deletions uuid_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ var _ = Describe("UUID", func() {
})
})

Context("time from UUID V1 is requested", func() {
It("should return timestamp if UUID is V1", func() {
u, err := NewV1()
Expect(err).ToNot(HaveOccurred())

expectedTime, err := uuid.TimestampFromV1(u.UUID)
Expect(err).ToNot(HaveOccurred())

actualTime, err := TimestampFromV1(u)
Expect(err).ToNot(HaveOccurred())

Expect(expectedTime).To(Equal(actualTime))
})

It("should return error if UUID other than V1 is specified", func() {
u, err := NewV4()
Expect(err).ToNot(HaveOccurred())

_, err = TimestampFromV1(u)
Expect(err).To(HaveOccurred())
})
})

Context("parsing to UUID", func() {
Describe("FromBytes", func() {
It("should parse bytes to UUID", func() {
Expand Down

0 comments on commit 7e7a30e

Please sign in to comment.