]> Cypherpunks repositories - gotai64n.git/commitdiff
More tests v4.1.0
authorSergey Matveev <stargrave@stargrave.org>
Fri, 22 Nov 2024 15:05:18 +0000 (18:05 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Fri, 22 Nov 2024 15:13:11 +0000 (18:13 +0300)
leapsecs.go
tai.go
tai_test.go

index 4bad6a2e415bcffd33823482fd2d2d8f6bbad569..dc768294353cd71de0f674fcc5a6a4ed303d293b 100644 (file)
@@ -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 52b9d73a1d9db2e5f105a7ede10235a95c897e34..8335213dae173855466422b17d09c8916735222a 100644 (file)
--- 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)
 }
index bce160a38428d42423a0cd96d886756f2540f08b..b48175c9006e612730dfaf4e7eb5835ae4a4fafb 100644 (file)
@@ -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) {