Skip to content

Commit

Permalink
time: Add tests + fix Micro vs Milli error
Browse files Browse the repository at this point in the history
  • Loading branch information
notti committed Jan 5, 2019
1 parent a7dbaf2 commit 7bc23f0
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pcap/pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ func (p *Handle) SnapLen() int {
// Resolution returns the timestamp resolution of acquired timestamps before scaling to NanosecondTimestampResolution.
func (p *Handle) Resolution() gopacket.TimestampResolution {
if p.nanoSecsFactor == 1 {
return gopacket.TimestampResolutionMillisecond
return gopacket.TimestampResolutionMicrosecond
}
return gopacket.TimestampResolutionNanosecond
}
Expand Down
2 changes: 1 addition & 1 deletion pcapgo/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (r *Reader) String() string {
// Resolution returns the timestamp resolution of acquired timestamps before scaling to NanosecondTimestampResolution.
func (r *Reader) Resolution() gopacket.TimestampResolution {
if r.nanoSecsFactor == 1 {
return gopacket.TimestampResolutionMillisecond
return gopacket.TimestampResolutionMicrosecond
}
return gopacket.TimestampResolutionNanosecond
}
35 changes: 32 additions & 3 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

package gopacket

import "fmt"
import (
"fmt"
"math"
"time"
)

// TimestampResolution represents the resolution of timestamps in Base^Exponent.
type TimestampResolution struct {
Expand All @@ -17,11 +21,36 @@ func (t TimestampResolution) String() string {
return fmt.Sprintf("%d^%d", t.Base, t.Exponent)
}

// ToDuration returns the smallest representable time difference as a time.Duration
func (t TimestampResolution) ToDuration() time.Duration {
if t.Base == 0 {
return 0
}
if t.Exponent == 0 {
return time.Second
}
switch t.Base {
case 10:
return time.Duration(math.Pow10(t.Exponent + 9))
case 2:
if t.Exponent < 0 {
return time.Second >> uint(-t.Exponent)
}
return time.Second << uint(t.Exponent)
default:
// this might loose precision
return time.Duration(float64(time.Second) * math.Pow(float64(t.Base), float64(t.Exponent)))
}
}

// TimestampResolutionInvalid represents an invalid timestamp resolution
var TimestampResolutionInvalid = TimestampResolution{}

// TimestampResolutionMillisecond is a resolution of 10^-6s
var TimestampResolutionMillisecond = TimestampResolution{10, -6}
// TimestampResolutionMillisecond is a resolution of 10^-3s
var TimestampResolutionMillisecond = TimestampResolution{10, -3}

// TimestampResolutionMicrosecond is a resolution of 10^-6s
var TimestampResolutionMicrosecond = TimestampResolution{10, -6}

// TimestampResolutionNanosecond is a resolution of 10^-9s
var TimestampResolutionNanosecond = TimestampResolution{10, -9}
Expand Down
73 changes: 73 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2019 The GoPacket Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.

package gopacket

import (
"testing"
"time"
)

func TestToDuration(t *testing.T) {
for i, test := range []struct {
r TimestampResolution
d time.Duration
}{
{
TimestampResolutionMillisecond,
time.Millisecond,
},
{
TimestampResolutionMicrosecond,
time.Microsecond,
},
{
TimestampResolutionNanosecond,
time.Nanosecond,
},
{
TimestampResolutionNTP,
0, // this is not representable since it's ~0.233 nanoseconds
},
{
TimestampResolution{2, -16},
15258,
},
{
TimestampResolution{2, 1},
2 * time.Second,
},
{
TimestampResolution{10, 1},
10 * time.Second,
},
{
TimestampResolution{10, 0},
time.Second,
},
{
TimestampResolution{2, 0},
time.Second,
},
{
TimestampResolution{0, 0},
0,
},
{
TimestampResolution{3, 2},
9 * time.Second,
},
{
TimestampResolution{3, -2},
111111111,
},
} {
d := test.r.ToDuration()
if d != test.d {
t.Errorf("%d: resolution: %s want: %d got: %d", i, test.r, test.d, d)
}
}
}

0 comments on commit 7bc23f0

Please sign in to comment.