update time type

master v1.0.4
李光春 2 years ago
parent 682e71b24d
commit b9968fb9a4

@ -8,7 +8,7 @@ import (
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
const Version = "1.0.3" const Version = "1.0.4"
type Client struct { type Client struct {
Db *mongo.Client // 驱动 Db *mongo.Client // 驱动

@ -2,13 +2,49 @@ package gomongo
import ( import (
"github.com/dtapps/gotime" "github.com/dtapps/gotime"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
"time" "time"
) )
// BsonTime 类型 // BsonTime 类型
type BsonTime string type BsonTime time.Time
// BsonTime 时间类型 // Value 时间类型
func (c *Client) BsonTime(value time.Time) string { func (t BsonTime) Value() string {
return gotime.SetCurrent(value).Bson() return gotime.SetCurrent(time.Time(t)).Bson()
}
// MarshalJSON 实现json序列化
func (t BsonTime) MarshalJSON() ([]byte, error) {
//log.Println("MarshalJSON")
b := make([]byte, 0)
b = append(b, gotime.SetCurrent(time.Time(t)).Bson()...)
return b, nil
}
// UnmarshalJSON 实现json反序列化
func (t *BsonTime) UnmarshalJSON(data []byte) (err error) {
//log.Println("UnmarshalJSON")
t1 := gotime.SetCurrentParse(string(data))
*t = BsonTime(t1.Time)
return
}
// MarshalBSONValue 实现bson序列化
func (t BsonTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
//log.Println("MarshalBSONValue")
targetTime := gotime.SetCurrent(time.Time(t)).Bson()
return bson.MarshalValue(targetTime)
}
// UnmarshalBSONValue 实现bson反序列化
func (t *BsonTime) UnmarshalBSONValue(t2 bsontype.Type, data []byte) error {
//log.Println("UnmarshalBSONValue")
t1 := gotime.SetCurrentParse(string(data))
//if string(data) == "" {
// return errors.New(fmt.Sprintf("%s, %s, %s", "读取数据失败:", t2, data))
//}
*t = BsonTime(t1.Time)
return nil
} }

Loading…
Cancel
Save