1
0
mirror of https://github.com/json-iterator/go.git synced 2025-11-29 22:47:28 +02:00

#67 time as int64 with specified precision

This commit is contained in:
Tao Wen
2017-06-20 17:46:29 +08:00
parent 486534c67c
commit cefb2972fd
2 changed files with 13 additions and 4 deletions

View File

@@ -7,11 +7,12 @@ import (
)
// keep epoch milliseconds
func RegisterTimeAsInt64Codec() {
jsoniter.RegisterTypeEncoder("time.Time", &timeAsInt64Codec{})
func RegisterTimeAsInt64Codec(precision time.Duration) {
jsoniter.RegisterTypeEncoder("time.Time", &timeAsInt64Codec{precision})
}
type timeAsInt64Codec struct {
precision time.Duration
}
func (codec *timeAsInt64Codec) IsEmpty(ptr unsafe.Pointer) bool {
@@ -20,7 +21,7 @@ func (codec *timeAsInt64Codec) IsEmpty(ptr unsafe.Pointer) bool {
}
func (codec *timeAsInt64Codec) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
ts := *((*time.Time)(ptr))
stream.WriteInt64(ts.UnixNano())
stream.WriteInt64(ts.UnixNano() / codec.precision.Nanoseconds())
}
func (codec *timeAsInt64Codec) EncodeInterface(val interface{}, stream *jsoniter.Stream) {
jsoniter.WriteToStream(val, stream, codec)