diff options
author | Ondrej Fabry <ofabry@cisco.com> | 2020-10-23 11:40:18 +0200 |
---|---|---|
committer | Ondrej Fabry <ofabry@cisco.com> | 2020-10-30 10:00:00 +0000 |
commit | 0f46871b4cc45f2c3bd5bdb0aa0f7615795a2c6d (patch) | |
tree | 3d3de8febc3e2becfbbbd648538fc5dd1748374f /binapi | |
parent | cb540dc166c12180adba024d5b8fd463d2582928 (diff) |
Fix encoding for float64 and generate conversion for Timestamp
- fixes encoding/decoding of float64 - uses little endian (contrary to all other types)
- generates helper methods for vpe_types.Timestamp type
- adds usage code to simple-client and binapi-types examples
Change-Id: I2e83eee0629eb67964049406c50c7ee0a692ccaf
Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'binapi')
-rw-r--r-- | binapi/vpe_types/vpe_types.ba.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/binapi/vpe_types/vpe_types.ba.go b/binapi/vpe_types/vpe_types.ba.go index 473880d..b4d324f 100644 --- a/binapi/vpe_types/vpe_types.ba.go +++ b/binapi/vpe_types/vpe_types.ba.go @@ -15,6 +15,7 @@ package vpe_types import ( "strconv" + "time" api "git.fd.io/govpp.git/api" ) @@ -79,6 +80,33 @@ type Timedelta float64 // Timestamp defines alias 'timestamp'. type Timestamp float64 +func NewTimestamp(t time.Time) Timestamp { + sec := int64(t.Unix()) + nsec := int32(t.Nanosecond()) + ns := float64(sec) + float64(nsec/1e9) + return Timestamp(ns) +} +func (x Timestamp) ToTime() time.Time { + ns := int64(x * 1e9) + sec := ns / 1e9 + nsec := ns % 1e9 + return time.Unix(sec, nsec) +} +func (x Timestamp) String() string { + return x.ToTime().String() +} +func (x *Timestamp) MarshalText() ([]byte, error) { + return []byte(x.ToTime().Format(time.RFC3339Nano)), nil +} +func (x *Timestamp) UnmarshalText(text []byte) error { + t, err := time.Parse(time.RFC3339Nano, string(text)) + if err != nil { + return err + } + *x = NewTimestamp(t) + return nil +} + // Version defines type 'version'. type Version struct { Major uint32 `binapi:"u32,name=major" json:"major,omitempty"` |