]> Cypherpunks.ru repositories - goredo.git/commitdiff
Move tai64n to separate module
authorSergey Matveev <stargrave@stargrave.org>
Thu, 10 Dec 2020 14:00:36 +0000 (17:00 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 10 Dec 2020 14:18:00 +0000 (17:18 +0300)
go.mod
go.sum
run.go
tai64n.go

diff --git a/go.mod b/go.mod
index ba61fc7be0b67a98c921c90dd05c9c52e463d9f3..f731fc58ce0f17b537158205d6c055eb88593477 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -4,6 +4,7 @@ go 1.14
 
 require (
        go.cypherpunks.ru/recfile v0.3.0
+       go.cypherpunks.ru/tai64n v0.1.0
        golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9
        golang.org/x/sys v0.0.0-20201117222635-ba5294a509c7
        golang.org/x/term v0.0.0-20201117132131-f5c789dd3221
diff --git a/go.sum b/go.sum
index 0d9a4e3d36bdb8573b4234eaaf4d95c7b8cc0271..1a103d7b67cf3edf1e8d0aef873f9d85186c5361 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,7 @@
 go.cypherpunks.ru/recfile v0.3.0 h1:aZRMMst8hoNOIhGjCA/VxjTN5VBSEN4W4zyhWF/7xSU=
 go.cypherpunks.ru/recfile v0.3.0/go.mod h1:p1ZUMeyQQbQg+ICtKH3+Zt59QLI0tCZYZj/75Vp1buk=
+go.cypherpunks.ru/tai64n v0.1.0 h1:XT1ys6lbo4/bjDQpMA8Xu5TCx6Y6aAYYYn5G0quE8sk=
+go.cypherpunks.ru/tai64n v0.1.0/go.mod h1:mjuUq/ZQAOEKvzAAl25RIrN6JExWA4fRkOs7o7OVvYE=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9 h1:phUcVbl53swtrUN8kQEXFhUxPlIlWyBfKmidCu7P95o=
 golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
diff --git a/run.go b/run.go
index e40ad1d5d5e4b65a262ea385b4a0e10df3274fa6..ac607a696b0ceea461d0147e9ef555ad8f663f59 100644 (file)
--- a/run.go
+++ b/run.go
@@ -21,7 +21,6 @@ package main
 
 import (
        "bufio"
-       "encoding/hex"
        "errors"
        "flag"
        "fmt"
@@ -35,6 +34,7 @@ import (
        "time"
 
        "go.cypherpunks.ru/recfile"
+       "go.cypherpunks.ru/tai64n"
        "golang.org/x/sys/unix"
 )
 
@@ -409,7 +409,7 @@ func runScript(tgtOrig string, errs chan error, traced bool) error {
                go func() {
                        scanner := bufio.NewScanner(stderr)
                        var line string
-                       ts := new(TAI64N)
+                       ts := new(tai64n.TAI64N)
                        for scanner.Scan() {
                                line = scanner.Text()
                                if strings.HasPrefix(line, childStderrPrefix) {
@@ -418,9 +418,9 @@ func runScript(tgtOrig string, errs chan error, traced bool) error {
                                        continue
                                }
                                if fdStderr != nil {
-                                       tai64nNow(ts)
+                                       tai64n.FromTime(time.Now(), ts)
                                        LogMutex.Lock()
-                                       fmt.Fprintf(fdStderr, "@%s %s\n", hex.EncodeToString(ts[:]), line)
+                                       fmt.Fprintf(fdStderr, "%s %s\n", ts.Encode(), line)
                                        LogMutex.Unlock()
                                }
                                if StderrSilent {
index e6dd28e7fd420cc9d3bc7c46b6115823efbe7c39..c4278354414c4f5dd5eb3ccacad8d6436aad98d3 100644 (file)
--- a/tai64n.go
+++ b/tai64n.go
@@ -19,36 +19,18 @@ package main
 
 import (
        "bufio"
-       "encoding/binary"
-       "encoding/hex"
-       "errors"
        "io"
        "strings"
        "time"
-)
 
-const (
-       TAI64NSize     = 12
-       TAI64NBase     = 0x400000000000000a
-       TAI64NLocalFmt = "2006-01-02 15:04:05.000000000"
+       "go.cypherpunks.ru/tai64n"
 )
 
-type TAI64N [TAI64NSize]byte
-
-func tai64nNow(ts *TAI64N) {
-       t := time.Now()
-       binary.BigEndian.PutUint64(ts[:], uint64(TAI64NBase)+uint64(t.Unix()))
-       binary.BigEndian.PutUint32(ts[8:], uint32(t.Nanosecond()))
-}
-
 func tai64nLocal(dst io.StringWriter, src io.Reader) error {
        scanner := bufio.NewScanner(src)
        var err error
        var s string
        var sep int
-       var ts []byte
-       var secs int64
-       var nano int64
        var t time.Time
        for {
                if !scanner.Scan() {
@@ -66,17 +48,11 @@ func tai64nLocal(dst io.StringWriter, src io.Reader) error {
                if sep == -1 {
                        dst.WriteString(s + "\n")
                }
-               ts, err = hex.DecodeString(s[1:sep])
+               t, err = tai64n.Decode(s[1:sep])
                if err != nil {
                        return err
                }
-               if len(ts) != TAI64NSize {
-                       return errors.New("invalid ts length")
-               }
-               secs = int64(binary.BigEndian.Uint64(ts[:8]))
-               nano = int64(binary.BigEndian.Uint32(ts[8:]))
-               t = time.Unix(secs-TAI64NBase, nano)
-               dst.WriteString(t.Format(TAI64NLocalFmt) + s[sep:] + "\n")
+               dst.WriteString(t.Format(tai64n.LocalFmt) + s[sep:] + "\n")
        }
        return nil
 }