Skip to content

Commit a278f03

Browse files
committed
Merge branch 'pr/84'
Change-Id: I2c41b0394bf7438c76bfdf04e6afb58e63e3c48c
2 parents 7147ef1 + b001244 commit a278f03

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

bson/element_test.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ func TestElement(t *testing.T) {
11351135
})
11361136
}
11371137
})
1138-
t.Run("UTC dateTime", func(t *testing.T) {
1138+
t.Run("UTC dateTime as time.Time", func(t *testing.T) {
11391139
var empty time.Time
11401140
testCases := []struct {
11411141
name string
@@ -1163,6 +1163,50 @@ func TestElement(t *testing.T) {
11631163
},
11641164
}
11651165

1166+
for _, tc := range testCases {
1167+
t.Run(tc.name, func(t *testing.T) {
1168+
defer func() {
1169+
fault := recover()
1170+
if fault != tc.fault {
1171+
t.Errorf("Did not return the correct error for panic. got %v; want %v", fault, tc.fault)
1172+
}
1173+
}()
1174+
1175+
val := tc.elem.value.Time()
1176+
if val != tc.val {
1177+
t.Errorf("Did not return correct value. got %v; want %v", val, tc.val)
1178+
}
1179+
})
1180+
}
1181+
})
1182+
t.Run("UTC dateTime", func(t *testing.T) {
1183+
var empty int64
1184+
testCases := []struct {
1185+
name string
1186+
elem *Element
1187+
val int64
1188+
fault error
1189+
}{
1190+
{"Nil Value", &Element{nil}, empty, ErrUninitializedElement},
1191+
{"Empty Element value",
1192+
&Element{&Value{start: 0, offset: 0, data: nil}}, empty, ErrUninitializedElement,
1193+
},
1194+
{"Empty Element data",
1195+
&Element{&Value{start: 0, offset: 2, data: nil}}, empty, ErrUninitializedElement,
1196+
},
1197+
{"Not UTC dateTime",
1198+
&Element{&Value{start: 0, offset: 2, data: []byte{0x01, 0x00}}}, empty,
1199+
ElementTypeError{"compact.Element.dateTime", Type(0x01)},
1200+
},
1201+
{"Success",
1202+
&Element{&Value{
1203+
start: 0, offset: 2,
1204+
data: []byte{0x09, 0x00, 0x80, 0x38, 0x17, 0xB0, 0x60, 0x01, 0x00, 0x00},
1205+
}},
1206+
1514782800000, nil,
1207+
},
1208+
}
1209+
11661210
for _, tc := range testCases {
11671211
t.Run(tc.name, func(t *testing.T) {
11681212
defer func() {

bson/value.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -598,19 +598,34 @@ func (v *Value) BooleanOK() (bool, bool) {
598598
return v.Boolean(), true
599599
}
600600

601-
// DateTime returns the BSON datetime value the Value represents. It panics if the value is a BSON
602-
// type other than datetime.
603-
func (v *Value) DateTime() time.Time {
601+
// DateTime returns the BSON datetime value the Value represents as a
602+
// unix timestamp. It panics if the value is a BSON type other than datetime.
603+
func (v *Value) DateTime() int64 {
604604
if v == nil || v.offset == 0 || v.data == nil {
605605
panic(ErrUninitializedElement)
606606
}
607607
if v.data[v.start] != '\x09' {
608608
panic(ElementTypeError{"compact.Element.dateTime", Type(v.data[v.start])})
609609
}
610-
i := v.getUint64()
610+
return int64(v.getUint64())
611+
}
612+
613+
// Time returns the BSON datetime value the Value represents. It panics if the value is a BSON
614+
// type other than datetime.
615+
func (v *Value) Time() time.Time {
616+
i := v.DateTime()
611617
return time.Unix(int64(i)/1000, int64(i)%1000*1000000)
612618
}
613619

620+
// TimeOK is the same as Time, except it returns a boolean instead of
621+
// panicking.
622+
func (v *Value) TimeOK() (time.Time, bool) {
623+
if v == nil || v.offset == 0 || v.data == nil || Type(v.data[v.start]) != TypeDateTime {
624+
return time.Time{}, false
625+
}
626+
return v.Time(), true
627+
}
628+
614629
// Regex returns the BSON regex value the Value represents. It panics if the value is a BSON
615630
// type other than regex.
616631
func (v *Value) Regex() (pattern, options string) {
@@ -638,9 +653,9 @@ func (v *Value) Regex() (pattern, options string) {
638653

639654
// DateTimeOK is the same as DateTime, except it returns a boolean instead of
640655
// panicking.
641-
func (v *Value) DateTimeOK() (time.Time, bool) {
656+
func (v *Value) DateTimeOK() (int64, bool) {
642657
if v == nil || v.offset == 0 || v.data == nil || Type(v.data[v.start]) != TypeDateTime {
643-
return time.Time{}, false
658+
return 0, false
644659
}
645660
return v.DateTime(), true
646661
}

0 commit comments

Comments
 (0)