7 cmn "github.com/tendermint/tmlibs/common"
10 // WriteTime writes the number of nanoseconds, with millisecond resolution,
11 // since January 1, 1970 UTC, to the Writer as an Int64.
12 // Milliseconds are used to ease compatibility with Javascript,
13 // which does not support finer resolution.
14 // NOTE: panics if the given time is less than January 1, 1970 UTC
15 func WriteTime(t time.Time, w io.Writer, n *int, err *error) {
16 nanosecs := t.UnixNano()
17 millisecs := nanosecs / 1000000
19 cmn.PanicSanity("can't encode times below 1970")
21 WriteInt64(millisecs*1000000, w, n, err)
25 // ReadTime reads an Int64 from the Reader, interprets it as
26 // the number of nanoseconds since January 1, 1970 UTC, and
27 // returns the corresponding time. If the Int64 read is less than zero,
28 // or not a multiple of a million, it sets the error and returns the default time.
29 func ReadTime(r io.Reader, n *int, err *error) time.Time {
30 t := ReadInt64(r, n, err)
32 *err = ErrBinaryReadInvalidTimeNegative
36 *err = ErrBinaryReadInvalidTimeSubMillisecond
39 return time.Unix(0, t)