From 83865613705c8c15ddf764396ace55b057102a52 Mon Sep 17 00:00:00 2001 From: Woodrow Douglass Date: Wed, 27 Mar 2019 13:45:01 -0400 Subject: [PATCH] add some tests to ntp timestamp conversion Relates to #15 --- packetizer.go | 8 ++++---- packetizer_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 packetizer_test.go diff --git a/packetizer.go b/packetizer.go index 22da422..5778c28 100644 --- a/packetizer.go +++ b/packetizer.go @@ -42,10 +42,10 @@ func NewPacketizer(mtu int, pt uint8, ssrc uint32, payloader Payloader, sequence } } -func toNtpTime(t time.Time) int64 { - var s int64 - var f int64 - u := t.UnixNano() +func toNtpTime(t time.Time) uint64 { + var s uint64 + var f uint64 + u := uint64(t.UnixNano()) s = u / 1e9 s += 0x83AA7E80 //offset in seconds between unix epoch and ntp epoch f = u % 1e9 diff --git a/packetizer_test.go b/packetizer_test.go new file mode 100644 index 0000000..0e20a86 --- /dev/null +++ b/packetizer_test.go @@ -0,0 +1,26 @@ +package rtp + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestNtpConversion(t *testing.T) { + loc := time.FixedZone("UTC-5", -5*60*60) + + tests := []struct { + t time.Time + n uint64 + }{ + {t: time.Date(1985, time.June, 23, 4, 0, 0, 0, loc), n: 0xa0c65b1000000000}, + {t: time.Date(1999, time.December, 31, 23, 59, 59, 500000, loc), n: 0xbc18084f0020c49b}, + {t: time.Date(2019, time.March, 27, 13, 39, 30, 8675309, loc), n: 0xe04641e202388b88}, + } + + for _, in := range tests { + out := toNtpTime(in.t) + assert.Equal(t, in.n, out) + } +}