OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / tendermint / go-wire / time.go
1 package wire
2
3 import (
4         "io"
5         "time"
6
7         cmn "github.com/tendermint/tmlibs/common"
8 )
9
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
18         if nanosecs < 0 {
19                 cmn.PanicSanity("can't encode times below 1970")
20         } else {
21                 WriteInt64(millisecs*1000000, w, n, err)
22         }
23 }
24
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)
31         if t < 0 {
32                 *err = ErrBinaryReadInvalidTimeNegative
33                 return time.Time{}
34         }
35         if t%1000000 != 0 {
36                 *err = ErrBinaryReadInvalidTimeSubMillisecond
37                 return time.Time{}
38         }
39         return time.Unix(0, t)
40 }