forked from jaekwon/go-wire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
40 lines (36 loc) · 1.17 KB
/
time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package wire
import (
"io"
"time"
cmn "github.com/tendermint/tmlibs/common"
)
// WriteTime writes the number of nanoseconds, with millisecond resolution,
// since January 1, 1970 UTC, to the Writer as an Int64.
// Milliseconds are used to ease compatibility with Javascript,
// which does not support finer resolution.
// NOTE: panics if the given time is less than January 1, 1970 UTC
func WriteTime(t time.Time, w io.Writer, n *int, err *error) {
nanosecs := t.UnixNano()
millisecs := nanosecs / 1000000
if nanosecs < 0 {
cmn.PanicSanity("can't encode times below 1970")
} else {
WriteInt64(millisecs*1000000, w, n, err)
}
}
// ReadTime reads an Int64 from the Reader, interprets it as
// the number of nanoseconds since January 1, 1970 UTC, and
// returns the corresponding time. If the Int64 read is less than zero,
// or not a multiple of a million, it sets the error and returns the default time.
func ReadTime(r io.Reader, n *int, err *error) time.Time {
t := ReadInt64(r, n, err)
if t < 0 {
*err = ErrBinaryReadInvalidTimeNegative
return time.Time{}
}
if t%1000000 != 0 {
*err = ErrBinaryReadInvalidTimeSubMillisecond
return time.Time{}
}
return time.Unix(0, t)
}