From d21b9bb478097af8197ef8c091279061896b61bd Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Fri, 22 Nov 2024 18:05:18 +0300 Subject: [PATCH] More tests --- leapsecs.go | 17 +++++------- tai.go | 4 +-- tai_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 12 deletions(-) diff --git a/leapsecs.go b/leapsecs.go index 4bad6a2..dc76829 100644 --- a/leapsecs.go +++ b/leapsecs.go @@ -29,18 +29,15 @@ var Leapsecs LeapsecsList // Add leap seconds to the time (convert TAI to UTC). func (leapsecs LeapsecsList) Add(tai time.Time) (utc time.Time) { - var i int - v := tai.Unix() - for ; i < len(leapsecs); i++ { - if v < leapsecs[i] { - break + orig := tai.Unix() + v := orig + v += Leapsecs1972 + for _, leapsec := range Leapsecs { + if v >= leapsec { + v++ } } - diff := Leapsecs1972 + int64(i) - if v+diff == leapsecs[i] { - diff++ - } - utc = tai.Add(time.Second * time.Duration(diff)) + utc = tai.Add(time.Second * time.Duration(v-orig)) return } diff --git a/tai.go b/tai.go index 52b9d73..8335213 100644 --- a/tai.go +++ b/tai.go @@ -51,12 +51,12 @@ func (tai *TAI64N) FromTime(src time.Time) { } func (tai *TAI64) Time() time.Time { - return time.Unix(int64(binary.BigEndian.Uint64(tai[:TAI64Size]))-Base, 0) + return time.Unix(int64(binary.BigEndian.Uint64(tai[:TAI64Size]))-Base, 0).In(time.UTC) } func (tai *TAI64N) Time() time.Time { return time.Unix( int64(binary.BigEndian.Uint64(tai[:TAI64Size]))-Base, int64(binary.BigEndian.Uint32(tai[TAI64Size:])), - ) + ).In(time.UTC) } diff --git a/tai_test.go b/tai_test.go index bce160a..b48175c 100644 --- a/tai_test.go +++ b/tai_test.go @@ -17,6 +17,7 @@ package tai64n import ( "testing" + "testing/quick" "time" ) @@ -93,6 +94,79 @@ func TestVectors(t *testing.T) { if isLeap { t.Fatal("unexpectedly leap") } + + tm, err = Decode("40000000586846a6") + if err != nil { + t.Fatal(err) + } + tm, isLeap = Leapsecs.Sub(tm) + ref = time.Date(2017, 1, 1, 0, 0, 1, 0, time.UTC) + if !tm.Equal(ref) { + t.Fatal("UTC != reference") + } + if isLeap { + t.Fatal("unexpectedly leap") + } +} + +func TestLeaped(t *testing.T) { + tm := time.Date(2016, 12, 31, 23, 59, 59, 0, time.UTC) + tm = Leapsecs.Add(tm) + var tai TAI64 + tai.FromTime(tm) + if tai.Encode() != "@40000000586846a3" { + t.Fatal("bad 2016-12-31 23:59:59") + } + + tm = time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC) + tm = Leapsecs.Add(tm) + tai.FromTime(tm) + if tai.Encode() != "@40000000586846a5" { + t.Fatal("bad 2017-01-01 00:00:00") + } + + tm = time.Date(2017, 1, 1, 0, 0, 1, 0, time.UTC) + tm = Leapsecs.Add(tm) + tai.FromTime(tm) + if tai.Encode() != "@40000000586846a6" { + t.Fatal("bad 2017-01-01 00:00:01") + } +} + +func TestLeapsecsSymmetric(t *testing.T) { + f := func(secs int64) bool { + tm := time.Unix(secs, 0) + got, isLeap := Leapsecs.Sub(Leapsecs.Add(tm)) + if isLeap { + return false + } + return tm == got + } + if err := quick.Check(f, nil); err != nil { + t.Error(err) + } +} + +func Test1969(t *testing.T) { + tm := time.Date(1969, 1, 1, 0, 0, 0, 0, time.UTC) + tm = Leapsecs.Add(tm) + var tai TAI64 + tai.FromTime(tm) + tm = tai.Time() + tm, _ = Leapsecs.Sub(tm) + if tm != time.Date(1969, 1, 1, 0, 0, 0, 0, time.UTC) { + t.Fatal("bad 1969 without ms") + } + + tm = time.Date(1969, 1, 1, 0, 0, 0, 1, time.UTC) + tm = Leapsecs.Add(tm) + var tain TAI64N + tain.FromTime(tm) + tm = tain.Time() + tm, _ = Leapsecs.Sub(tm) + if tm != time.Date(1969, 1, 1, 0, 0, 0, 1, time.UTC) { + t.Fatal("bad 1969 with ms=1") + } } func BenchmarkTAI64(b *testing.B) { -- 2.48.1