Browse Source

time: Add tests + fix Micro vs Milli error

Gernot Vormayr 1 year ago
parent
commit
7bc23f016d
4 changed files with 107 additions and 5 deletions
  1. 1 1
      pcap/pcap.go
  2. 1 1
      pcapgo/read.go
  3. 32 3
      time.go
  4. 73 0
      time_test.go

+ 1 - 1
pcap/pcap.go

@@ -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
 }

+ 1 - 1
pcapgo/read.go

@@ -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
 }

+ 32 - 3
time.go

@@ -6,7 +6,11 @@
 
 package gopacket
 
-import "fmt"
+import (
+	"fmt"
+	"math"
+	"time"
+)
 
 // TimestampResolution represents the resolution of timestamps in Base^Exponent.
 type TimestampResolution struct {
@@ -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}

+ 73 - 0
time_test.go

@@ -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)
+		}
+	}
+}