From 682e71b24db746449c78acf8982f8049e9d1bb45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Sat, 30 Apr 2022 23:35:02 +0800 Subject: [PATCH] add bson time --- common.go | 51 --------------------------------------------------- time.go | 3 +++ 2 files changed, 3 insertions(+), 51 deletions(-) delete mode 100644 common.go diff --git a/common.go b/common.go deleted file mode 100644 index 4375888..0000000 --- a/common.go +++ /dev/null @@ -1,51 +0,0 @@ -package gomongo - -import ( - "errors" - "fmt" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsontype" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" - "time" -) - -// https://chowdera.com/2021/01/20210128154417121y.html - -type JsonTime time.Time - -const ( - timeFormart = "2006-01-02 15:04:05" -) - -// UnmarshalJSON 实现json反序列化,从传递的字符串中解析成时间对象 -func (t *JsonTime) UnmarshalJSON(data []byte) (err error) { - now, err := time.ParseInLocation(`"`+timeFormart+`"`, string(data), time.Local) - *t = JsonTime(now) - return -} - -// MarshalJSON 实现json序列化,将时间转换成字符串byte数组 -func (t JsonTime) MarshalJSON() ([]byte, error) { - b := make([]byte, 0, len(timeFormart)+2) - b = append(b, '"') - b = time.Time(t).AppendFormat(b, timeFormart) - b = append(b, '"') - return b, nil -} - -// MarshalBSONValue mongodb是存储bson格式,因此需要实现序列化bsonvalue(这里不能实现MarshalBSON,MarshalBSON是处理Document的),将时间转换成mongodb能识别的primitive.DateTime -func (t *JsonTime) MarshalBSONValue() (bsontype.Type, []byte, error) { - targetTime := primitive.NewDateTimeFromTime(time.Time(*t)) - return bson.MarshalValue(targetTime) -} - -// UnmarshalBSONValue 实现bson反序列化,从mongodb中读取数据转换成time.Time格式,这里用到了bsoncore中的方法读取数据转换成datetime然后再转换成time.Time -func (t *JsonTime) UnmarshalBSONValue(t2 bsontype.Type, data []byte) error { - v, _, valid := bsoncore.ReadValue(data, t2) - if valid == false { - return errors.New(fmt.Sprintf("%s, %s, %s", "读取数据失败:", t2, data)) - } - *t = JsonTime(v.Time()) - return nil -} diff --git a/time.go b/time.go index 55f2b13..d5972de 100644 --- a/time.go +++ b/time.go @@ -5,6 +5,9 @@ import ( "time" ) +// BsonTime 类型 +type BsonTime string + // BsonTime 时间类型 func (c *Client) BsonTime(value time.Time) string { return gotime.SetCurrent(value).Bson()