Skip to content

Commit

Permalink
Merge pull request #99 from dimovnike/97-datetime-panic
Browse files Browse the repository at this point in the history
#97: Fixed DateTime UnmarshalBSONValue panics on a null bson value
  • Loading branch information
youyuanwu committed Feb 5, 2022
2 parents 4710d14 + 9835f0b commit 7391b8a
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"database/sql/driver"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"
Expand Down Expand Up @@ -227,6 +228,15 @@ func (t DateTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
// assumed to be valid. UnmarshalBSONValue must copy the BSON value bytes if it
// wishes to retain the data after returning.
func (t *DateTime) UnmarshalBSONValue(tpe bsontype.Type, data []byte) error {
if tpe == bsontype.Null {
*t = DateTime{}
return nil
}

if len(data) != 8 {
return errors.New("bson date field length not exactly 8 bytes")
}

i64 := int64(binary.LittleEndian.Uint64(data))
// TODO: Use bsonprim.DateTime.Time() method
*t = DateTime(time.Unix(i64/1000, i64%1000*1000000))
Expand Down

0 comments on commit 7391b8a

Please sign in to comment.