Skip to content

Commit

Permalink
GODRIVER-300 add Time(time.Time) to ValueConstructor
Browse files Browse the repository at this point in the history
Change-Id: I986962c2c67510b4c68e87bf52712ee41a90dd58
  • Loading branch information
Isabella Siu committed Oct 24, 2018
1 parent 3cd3fa5 commit e763e20
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bson/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,11 @@ func (ValueConstructor) DateTime(dt int64) *Value {
return EC.DateTime("", dt).value
}

// Time creates a datetime value from the argument.
func (ValueConstructor) Time(t time.Time) *Value {
return EC.Time("", t).value
}

// Null creates a null value from the argument.
func (ValueConstructor) Null() *Value {
return EC.Null("").value
Expand Down
20 changes: 20 additions & 0 deletions bson/constructor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,26 @@ func TestConstructor(t *testing.T) {
requireValuesEqual(t, expected, actual)
})

t.Run("time", func(t *testing.T) {
buf := []byte{
// type
0x9,
// key
0x0,
// value
0xC9, 0x6C, 0x3C, 0xAF, 0x60, 0x1, 0x0, 0x0,
}

expected := &Value{start: 0, offset: 2, data: buf, d: nil}

date := time.Date(2018, 1, 1, 1, 1, 1, int(1*time.Millisecond), time.UTC)
actualTime := VC.Time(date)
actualDateTime := VC.DateTime(date.UnixNano() / 1e6)

requireValuesEqual(t, expected, actualTime)
requireValuesEqual(t, expected, actualDateTime)
})

t.Run("Null", func(t *testing.T) {
buf := []byte{
// type
Expand Down

0 comments on commit e763e20

Please sign in to comment.