README | 8 ++++++++ cmd/tai64nlocal/main.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tai64n.go | 6 +++--- diff --git a/README b/README index 2128f22fa12948d38040c08bfd70a6196aec5dd5324fec768f6ae1ad0d7622a1..b0866d205926adc0285a3dd0cd4f601a645c3469dd0ca59e6f683bc2e73fc36a 100644 --- a/README +++ b/README @@ -1 +1,9 @@ Pure Go TAI64N (http://cr.yp.to/libtai/tai64.html) implementation. +cmd/tai64nlocal contains similar to DJB's one utility. +Example TAI64N creation: + + import "go.cypherpunks.ru/tai64n" + + tai := new(tai64n.TAI64N) + tai64n.FromTime(time.Now(), tai) + println(tai.Encode()) // @400000005fd24ce33323c4d1 diff --git a/cmd/tai64nlocal/main.go b/cmd/tai64nlocal/main.go new file mode 100644 index 0000000000000000000000000000000000000000..8eb37b68bd9477a1bfa378273896287be2a382a943667ab1e421d5830c1a3346 --- /dev/null +++ b/cmd/tai64nlocal/main.go @@ -0,0 +1,58 @@ +/* +go.cypherpunks.ru/tai64n -- Pure Go TAI64N implementation +Copyright (C) 2020 Sergey Matveev + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package main + +import ( + "bufio" + "log" + "os" + "strings" + "time" + + "go.cypherpunks.ru/tai64n" +) + +func main() { + log.SetFlags(0) + scanner := bufio.NewScanner(os.Stdin) + var err error + var s string + var sep int + var t time.Time + for { + if !scanner.Scan() { + if err = scanner.Err(); err != nil { + log.Fatalln(err) + } + break + } + s = scanner.Text() + if s[0] != '@' { + os.Stdout.WriteString(s + "\n") + } + sep = strings.IndexByte(s, byte(' ')) + if sep == -1 { + os.Stdout.WriteString(s + "\n") + } + t, err = tai64n.Decode(s[1:sep]) + if err != nil { + log.Fatalln(err) + } + os.Stdout.WriteString(t.Format(tai64n.LocalFmt) + s[sep:] + "\n") + } +} diff --git a/tai64n.go b/tai64n.go index e6ffd97a1a43af3e8d4fae5051de02a67abf56bfddb97497a9961413b8594979..ab8acfff0731d778a071bedf6a3684df50da9641cd8ed4ef86eb152ca6e5e635 100644 --- a/tai64n.go +++ b/tai64n.go @@ -33,9 +33,9 @@ ) type TAI64N [Size]byte -func FromTime(t time.Time, tai *TAI64N) { - binary.BigEndian.PutUint64(tai[:], uint64(Base)+uint64(t.Unix())) - binary.BigEndian.PutUint32(tai[8:], uint32(t.Nanosecond())) +func FromTime(src time.Time, dst *TAI64N) { + binary.BigEndian.PutUint64(dst[:], uint64(Base)+uint64(src.Unix())) + binary.BigEndian.PutUint32(dst[8:], uint32(src.Nanosecond())) } func ToTime(tai []byte) time.Time {