|
@@ -7,14 +7,17 @@ import (
|
|
"github.com/globalsign/mgo/bson"
|
|
"github.com/globalsign/mgo/bson"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+// MilliTime represents time.Time that works better with mongodb.
|
|
type MilliTime struct {
|
|
type MilliTime struct {
|
|
time.Time
|
|
time.Time
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// MarshalJSON marshals mt to json bytes.
|
|
func (mt MilliTime) MarshalJSON() ([]byte, error) {
|
|
func (mt MilliTime) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(mt.Milli())
|
|
return json.Marshal(mt.Milli())
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// UnmarshalJSON unmarshals data into mt.
|
|
func (mt *MilliTime) UnmarshalJSON(data []byte) error {
|
|
func (mt *MilliTime) UnmarshalJSON(data []byte) error {
|
|
var milli int64
|
|
var milli int64
|
|
if err := json.Unmarshal(data, &milli); err != nil {
|
|
if err := json.Unmarshal(data, &milli); err != nil {
|
|
@@ -25,14 +28,17 @@ func (mt *MilliTime) UnmarshalJSON(data []byte) error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// GetBSON returns BSON base on mt.
|
|
func (mt MilliTime) GetBSON() (interface{}, error) {
|
|
func (mt MilliTime) GetBSON() (interface{}, error) {
|
|
return mt.Time, nil
|
|
return mt.Time, nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// SetBSON sets raw into mt.
|
|
func (mt *MilliTime) SetBSON(raw bson.Raw) error {
|
|
func (mt *MilliTime) SetBSON(raw bson.Raw) error {
|
|
return raw.Unmarshal(&mt.Time)
|
|
return raw.Unmarshal(&mt.Time)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Milli returns milliseconds for mt.
|
|
func (mt MilliTime) Milli() int64 {
|
|
func (mt MilliTime) Milli() int64 {
|
|
return mt.UnixNano() / int64(time.Millisecond)
|
|
return mt.UnixNano() / int64(time.Millisecond)
|
|
}
|
|
}
|