From f1ee03afe3b7bfd49e3f238b08031d2c35c2395f Mon Sep 17 00:00:00 2001 From: dsp Date: Thu, 27 Aug 2015 12:12:15 -0600 Subject: mrt: add helper function for timestamp Commiting helper function on MRTHeader to return a golang time.Time from the timestamp. Also commiting the relevant test function. --- packet/mrt.go | 6 ++++++ packet/mrt_test.go | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/packet/mrt.go b/packet/mrt.go index 369e8063..b259be7f 100644 --- a/packet/mrt.go +++ b/packet/mrt.go @@ -21,6 +21,7 @@ import ( "fmt" "math" "net" + "time" ) const ( @@ -139,6 +140,11 @@ func NewMRTHeader(timestamp uint32, t MRTType, subtype MRTSubTyper, l uint32) (* }, nil } +func (h *MRTHeader) GetTime() time.Time { + t := int64(h.Timestamp) + return time.Unix(t, 0) +} + type MRTMessage struct { Header MRTHeader Body Body diff --git a/packet/mrt_test.go b/packet/mrt_test.go index c43d45e0..11a233ec 100644 --- a/packet/mrt_test.go +++ b/packet/mrt_test.go @@ -41,6 +41,17 @@ func TestMrtHdr(t *testing.T) { assert.Equal(t, reflect.DeepEqual(h1, h2), true) } +func TestMrtHdrTime(t *testing.T) { + h1, err := NewMRTHeader(10, TABLE_DUMPv2, RIB_IPV4_MULTICAST, 20) + if err != nil { + t.Fatal(err) + } + ttime := time.Unix(10, 0) + htime := h1.GetTime() + t.Logf("this timestamp should be 10s after epoch:%v", htime) + assert.Equal(t, h1.GetTime(), ttime) +} + func testPeer(t *testing.T, p1 *Peer) { b1, err := p1.Serialize() if err != nil { -- cgit v1.2.3