Unverified Commit c568d328 authored by Whyrusleeping's avatar Whyrusleeping Committed by GitHub

Merge pull request #41 from whyrusleeping/feat/use-unix-nano

use unix nanoseconds for encoding Cbortime
parents 9d087a72 656e08ce
...@@ -696,43 +696,22 @@ func (ci *CborInt) UnmarshalCBOR(r io.Reader) error { ...@@ -696,43 +696,22 @@ func (ci *CborInt) UnmarshalCBOR(r io.Reader) error {
type CborTime time.Time type CborTime time.Time
func (ct *CborTime) MarshalCBOR(w io.Writer) error { func (ct *CborTime) MarshalCBOR(w io.Writer) error {
b, err := (*time.Time)(ct).MarshalBinary() nsecs := ct.Time().UnixNano()
if err != nil {
return err
}
if err := CborWriteHeader(w, MajByteString, uint64(len(b))); err != nil { cbi := CborInt(nsecs)
return err
}
if _, err := w.Write(b); err != nil { return cbi.MarshalCBOR(w)
return err
}
return nil
} }
func (ct *CborTime) UnmarshalCBOR(r io.Reader) error { func (ct *CborTime) UnmarshalCBOR(r io.Reader) error {
t, l, err := CborReadHeader(r) var cbi CborInt
if err != nil { if err := cbi.UnmarshalCBOR(r); err != nil {
return err return err
} }
if t != MajByteString { t := time.Unix(0, int64(cbi))
return fmt.Errorf("CborTime expects to find a byte array (got %d)", t)
}
buf := make([]byte, l)
if _, err := io.ReadFull(r, buf); err != nil {
return err
}
tm := time.Time{}
if err := tm.UnmarshalBinary(buf); err != nil {
return err
}
*ct = (CborTime)(tm) *ct = (CborTime)(t)
return nil return nil
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment