diff --git a/pkg/pcic/chunk.go b/pkg/pcic/chunk.go index d66ce7d..66740a9 100644 --- a/pkg/pcic/chunk.go +++ b/pkg/pcic/chunk.go @@ -144,6 +144,13 @@ func (c *Chunk) TimeStamp() time.Time { return time.Unix(int64(c.timestampSec), int64(c.timestampNSec)) } +func (c *Chunk) SetTimestamp(value time.Time) { + c.timestampSec = uint32(value.Unix()) + seconds := time.Unix(int64(c.timestampSec), 0) + c.timestampNSec = uint32(value.UnixNano() - seconds.UnixNano()) + fmt.Printf("Seconds: %d Nanoseconds: %d\n", c.timestampSec, c.timestampNSec) +} + // Bytes return the data the current Chunk is holding func (c *Chunk) Bytes() []byte { return c.data @@ -191,6 +198,14 @@ func (c *Chunk) MarshalBinary() (data []byte, err error) { blob[offsetOfStatusCode:offsetOfTimeStampSec], c.statusCode, ) + binary.LittleEndian.PutUint32( + blob[offsetOfTimeStampSec:offsetOfTimeStampNsec], + c.timestampSec, + ) + binary.LittleEndian.PutUint32( + blob[offsetOfTimeStampNsec:offsetOfData], + c.timestampNSec, + ) return blob, nil } diff --git a/pkg/pcic/chunk_test.go b/pkg/pcic/chunk_test.go index ac04521..11a1143 100644 --- a/pkg/pcic/chunk_test.go +++ b/pkg/pcic/chunk_test.go @@ -417,3 +417,31 @@ func TestFrameStatus(t *testing.T) { "The status of the original and the clone do not match", ) } + +func TestChunkTimeStamp(t *testing.T) { + now := time.Now() + chunk := pcic.NewChunk() + chunk.SetTimestamp(now) + assert.Equal( + t, + now.UnixNano(), + chunk.TimeStamp().UnixNano(), + "No error expected when setting the time stamp", + ) + clone := pcic.NewChunk() + data, err := chunk.MarshalBinary() + assert.NoError(t, + err, + "No error expected when creating a binary clone", + ) + assert.NoError(t, + clone.UnmarshalBinary(data), + "No error expected when creating a clone from the bytes", + ) + assert.Equal( + t, + chunk.TimeStamp(), + clone.TimeStamp(), + "The status of the original and the clone do not match", + ) +}