2 goredo -- redo implementation on pure Go
3 Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 TAI64NBase = 0x400000000000000a
33 TAI64NLocalFmt = "2006-01-02 15:04:05.000000000"
36 type TAI64N [TAI64NSize]byte
38 func tai64nNow(ts *TAI64N) {
40 binary.BigEndian.PutUint64(ts[:], uint64(TAI64NBase)+uint64(t.Unix()))
41 binary.BigEndian.PutUint32(ts[8:], uint32(t.Nanosecond()))
44 func tai64nLocal(dst io.StringWriter, src io.Reader) error {
45 scanner := bufio.NewScanner(src)
55 if err = scanner.Err(); err != nil {
63 dst.WriteString(s + "\n")
65 sep = strings.IndexByte(s, byte(' '))
67 dst.WriteString(s + "\n")
69 ts, err = hex.DecodeString(s[1:sep])
73 if len(ts) != TAI64NSize {
74 return errors.New("invalid ts length")
76 secs = int64(binary.BigEndian.Uint64(ts[:8]))
77 nano = int64(binary.BigEndian.Uint32(ts[8:]))
78 t = time.Unix(secs-TAI64NBase, nano)
79 dst.WriteString(t.Format(TAI64NLocalFmt) + s[sep:] + "\n")