tendermint/wire/time.go

28 lines
574 B
Go
Raw Normal View History

2015-07-25 15:45:45 -07:00
package wire
2014-06-16 16:39:25 -07:00
import (
2014-07-01 14:50:24 -07:00
"io"
"time"
. "github.com/tendermint/tendermint/common"
2014-06-16 16:39:25 -07:00
)
/*
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) {
nanosecs := t.UnixNano()
millisecs := nanosecs / 1000000
WriteInt64(millisecs*1000000, w, n, err)
}
func ReadTime(r io.Reader, n *int64, err *error) time.Time {
t := ReadInt64(r, n, err)
if t%1000000 != 0 {
PanicSanity("Time cannot have sub-millisecond precision")
}
2014-10-07 13:39:21 -07:00
return time.Unix(0, t)
2014-06-16 16:39:25 -07:00
}