Skip to content

Commit e763e20

Browse files
author
Isabella Siu
committed
GODRIVER-300 add Time(time.Time) to ValueConstructor
Change-Id: I986962c2c67510b4c68e87bf52712ee41a90dd58
1 parent 3cd3fa5 commit e763e20

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

bson/constructor.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,11 @@ func (ValueConstructor) DateTime(dt int64) *Value {
634634
return EC.DateTime("", dt).value
635635
}
636636

637+
// Time creates a datetime value from the argument.
638+
func (ValueConstructor) Time(t time.Time) *Value {
639+
return EC.Time("", t).value
640+
}
641+
637642
// Null creates a null value from the argument.
638643
func (ValueConstructor) Null() *Value {
639644
return EC.Null("").value

bson/constructor_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,26 @@ func TestConstructor(t *testing.T) {
731731
requireValuesEqual(t, expected, actual)
732732
})
733733

734+
t.Run("time", func(t *testing.T) {
735+
buf := []byte{
736+
// type
737+
0x9,
738+
// key
739+
0x0,
740+
// value
741+
0xC9, 0x6C, 0x3C, 0xAF, 0x60, 0x1, 0x0, 0x0,
742+
}
743+
744+
expected := &Value{start: 0, offset: 2, data: buf, d: nil}
745+
746+
date := time.Date(2018, 1, 1, 1, 1, 1, int(1*time.Millisecond), time.UTC)
747+
actualTime := VC.Time(date)
748+
actualDateTime := VC.DateTime(date.UnixNano() / 1e6)
749+
750+
requireValuesEqual(t, expected, actualTime)
751+
requireValuesEqual(t, expected, actualDateTime)
752+
})
753+
734754
t.Run("Null", func(t *testing.T) {
735755
buf := []byte{
736756
// type

0 commit comments

Comments
 (0)