Skip to content

Commit 4d82ab9

Browse files
author
roman.tarasov
committed
fix *time.Time encoding
1 parent e131124 commit 4d82ab9

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

bson/encode.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ func NewDocumentEncoder() DocumentEncoder {
122122
return &encoder{}
123123
}
124124

125+
func convertTimeToInt64(t time.Time) int64 {
126+
return t.Unix()*1000+int64(t.Nanosecond()/1e6)
127+
}
128+
125129
func (e *encoder) Encode(v interface{}) error {
126130
var err error
127131

@@ -438,7 +442,10 @@ func (e *encoder) encodeSliceAsArray(rval reflect.Value, minsize bool) ([]*Value
438442
vals = append(vals, VC.Decimal128(t))
439443
continue
440444
case time.Time:
441-
vals = append(vals, VC.DateTime(t.Unix()*1000+int64(t.Nanosecond()/1e6)))
445+
vals = append(vals, VC.DateTime(convertTimeToInt64(t)))
446+
continue
447+
case *time.Time:
448+
vals = append(vals, VC.DateTime(convertTimeToInt64(*t)))
442449
continue
443450
}
444451

@@ -521,7 +528,10 @@ func (e *encoder) encodeStruct(val reflect.Value) ([]*Element, error) {
521528
elems = append(elems, EC.Decimal128(key, t))
522529
continue
523530
case time.Time:
524-
elems = append(elems, EC.DateTime(key, t.Unix()*1000+int64(t.Nanosecond()/1e6)))
531+
elems = append(elems, EC.DateTime(key, convertTimeToInt64(t)))
532+
continue
533+
case *time.Time:
534+
elems = append(elems, EC.DateTime(key, convertTimeToInt64(*t)))
525535
continue
526536
}
527537
field = e.underlyingVal(field)

bson/encode_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ func reflectionEncoderTest(t *testing.T) {
871871
AA json.Number
872872
AB *url.URL
873873
AC decimal.Decimal128
874+
AD *time.Time
874875
}{
875876
A: true,
876877
B: 123,
@@ -904,6 +905,7 @@ func reflectionEncoderTest(t *testing.T) {
904905
AA: json.Number("10.10"),
905906
AB: murl,
906907
AC: decimal128,
908+
AD: &now,
907909
},
908910
docToBytes(NewDocument(
909911
EC.Boolean("a", true),
@@ -934,6 +936,7 @@ func reflectionEncoderTest(t *testing.T) {
934936
EC.Double("aa", 10.10),
935937
EC.String("ab", murl.String()),
936938
EC.Decimal128("ac", decimal128),
939+
EC.DateTime("ad", now.UnixNano()/int64(time.Millisecond)),
937940
)),
938941
nil,
939942
},
@@ -970,6 +973,7 @@ func reflectionEncoderTest(t *testing.T) {
970973
AA []json.Number
971974
AB []*url.URL
972975
AC []decimal.Decimal128
976+
AD []*time.Time
973977
}{
974978
A: []bool{true},
975979
B: []int32{123},
@@ -1005,6 +1009,7 @@ func reflectionEncoderTest(t *testing.T) {
10051009
AA: []json.Number{json.Number("5"), json.Number("10.10")},
10061010
AB: []*url.URL{murl},
10071011
AC: []decimal.Decimal128{decimal128},
1012+
AD: []*time.Time{&now, &now},
10081013
},
10091014
docToBytes(NewDocument(
10101015
EC.ArrayFromElements("a", VC.Boolean(true)),
@@ -1035,6 +1040,7 @@ func reflectionEncoderTest(t *testing.T) {
10351040
EC.ArrayFromElements("aa", VC.Int64(5), VC.Double(10.10)),
10361041
EC.ArrayFromElements("ab", VC.String(murl.String())),
10371042
EC.ArrayFromElements("ac", VC.Decimal128(decimal128)),
1043+
EC.ArrayFromElements("ad", VC.DateTime(now.UnixNano()/int64(time.Millisecond)), VC.DateTime(now.UnixNano()/int64(time.Millisecond))),
10381044
)),
10391045
nil,
10401046
},

0 commit comments

Comments
 (0)