time writes milliseconds using ISO8601 format
This commit is contained in:
parent
b9cbb0dac5
commit
c6e5948534
|
@ -93,7 +93,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
rfc2822 = "Mon Jan 02 15:04:05 -0700 2006"
|
iso8601 = "2006-01-02T15:04:05.000Z" // forced microseconds
|
||||||
)
|
)
|
||||||
|
|
||||||
// NOTE: do not access typeInfos directly, but call GetTypeInfo()
|
// NOTE: do not access typeInfos directly, but call GetTypeInfo()
|
||||||
|
@ -731,7 +731,7 @@ func readReflectJSON(rv reflect.Value, rt reflect.Type, o interface{}, err *erro
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Info(Fmt("Read time: %v", str))
|
log.Info(Fmt("Read time: %v", str))
|
||||||
t, err_ := time.Parse(rfc2822, str)
|
t, err_ := time.Parse(iso8601, str)
|
||||||
if err_ != nil {
|
if err_ != nil {
|
||||||
*err = err_
|
*err = err_
|
||||||
return
|
return
|
||||||
|
@ -908,8 +908,8 @@ func writeReflectJSON(rv reflect.Value, rt reflect.Type, w io.Writer, n *int64,
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if rt == timeType {
|
if rt == timeType {
|
||||||
// Special case: time.Time
|
// Special case: time.Time
|
||||||
t := rv.Interface().(time.Time)
|
t := rv.Interface().(time.Time).UTC()
|
||||||
str := t.Format(rfc2822)
|
str := t.Format(iso8601)
|
||||||
jsonBytes, err_ := json.Marshal(str)
|
jsonBytes, err_ := json.Marshal(str)
|
||||||
if err_ != nil {
|
if err_ != nil {
|
||||||
*err = err_
|
*err = err_
|
||||||
|
|
14
wire/time.go
14
wire/time.go
|
@ -3,15 +3,25 @@ package wire
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
. "github.com/tendermint/tendermint/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Time
|
/*
|
||||||
|
Writes nanoseconds since epoch but with millisecond precision.
|
||||||
|
This is to ease compatibility with Javascript etc.
|
||||||
|
*/
|
||||||
|
|
||||||
func WriteTime(t time.Time, w io.Writer, n *int64, err *error) {
|
func WriteTime(t time.Time, w io.Writer, n *int64, err *error) {
|
||||||
WriteInt64(t.UnixNano(), w, n, err)
|
nanosecs := t.UnixNano()
|
||||||
|
millisecs := nanosecs / 1000000
|
||||||
|
WriteInt64(millisecs*1000000, w, n, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadTime(r io.Reader, n *int64, err *error) time.Time {
|
func ReadTime(r io.Reader, n *int64, err *error) time.Time {
|
||||||
t := ReadInt64(r, n, err)
|
t := ReadInt64(r, n, err)
|
||||||
|
if t%1000000 != 0 {
|
||||||
|
PanicSanity("Time cannot have sub-millisecond precision")
|
||||||
|
}
|
||||||
return time.Unix(0, t)
|
return time.Unix(0, t)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue